VirtualBox

source: vbox/trunk/include/iprt/localipc.h@ 93176

Last change on this file since 93176 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.0 KB
Line 
1/** @file
2 * IPRT - Local IPC Server & Client.
3 */
4
5/*
6 * Copyright (C) 2006-2022 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef IPRT_INCLUDED_localipc_h
27#define IPRT_INCLUDED_localipc_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/cdefs.h>
33#include <iprt/types.h>
34#include <iprt/thread.h>
35
36#ifdef IN_RING0
37# error "There are no RTLocalIpc APIs available Ring-0 Host Context!"
38#endif
39
40
41RT_C_DECLS_BEGIN
42
43/** @defgroup grp_rt_localipc RTLocalIpc - Local IPC
44 * @ingroup grp_rt
45 * @{
46 */
47
48/** Handle to a local IPC server instance. */
49typedef struct RTLOCALIPCSERVERINT *RTLOCALIPCSERVER;
50/** Pointer to a local IPC server handle. */
51typedef RTLOCALIPCSERVER *PRTLOCALIPCSERVER;
52/** Local IPC server handle nil value. */
53#define NIL_RTLOCALIPCSERVER ((RTLOCALIPCSERVER)0)
54
55/** Handle to a local ICP session instance. */
56typedef struct RTLOCALIPCSESSIONINT *RTLOCALIPCSESSION;
57/** Pointer to a local ICP session handle. */
58typedef RTLOCALIPCSESSION *PRTLOCALIPCSESSION;
59/** Local ICP session handle nil value. */
60#define NIL_RTLOCALIPCSESSION ((RTLOCALIPCSESSION)0)
61
62
63
64/**
65 * Create a local IPC server.
66 *
67 * @returns IPRT status code.
68 * @retval VINF_SUCCESS on success and *phServer containing the instance handle.
69 *
70 * @param phServer Where to put the server instance handle.
71 * @param pszName The server name. This must be unique and not include
72 * any special chars or slashes. It will be morphed into a
73 * unique platform specific identifier.
74 * @param fFlags Flags, see RTLOCALIPC_FLAGS_*.
75 */
76RTDECL(int) RTLocalIpcServerCreate(PRTLOCALIPCSERVER phServer, const char *pszName, uint32_t fFlags);
77
78/** @name RTLocalIpcServerCreate flags
79 * @{ */
80/** Native name, as apposed to a portable one. */
81#define RTLOCALIPC_FLAGS_NATIVE_NAME RT_BIT_32(0)
82/** The mask of valid flags. */
83#define RTLOCALIPC_FLAGS_VALID_MASK UINT32_C(0x00000001)
84/** @} */
85
86/**
87 * Destroys a local IPC server.
88 *
89 * @returns IPRT status code.
90 * @retval VINF_SUCCESS if still other references or NIL.
91 * @retval VINF_OBJECT_DESTROYED if actually destroyed.
92 *
93 * @param hServer The server handle. The nil value is quietly ignored (VINF_SUCCESS).
94 */
95RTDECL(int) RTLocalIpcServerDestroy(RTLOCALIPCSERVER hServer);
96
97/**
98 * Grant the specified group access to the local IPC server socket.
99 *
100 * @returns IPRT status code.
101 * @param hServer The server handle.
102 * @param gid Group ID.
103 */
104RTDECL(int) RTLocalIpcServerGrantGroupAccess(RTLOCALIPCSERVER hServer, RTGID gid);
105
106/**
107 * Listen for clients.
108 *
109 * @returns IPRT status code.
110 * @retval VINF_SUCCESS on success and *phClientSession containing the session handle.
111 * @retval VERR_CANCELLED if the listening was interrupted by RTLocalIpcServerCancel().
112 *
113 * @param hServer The server handle.
114 * @param phClientSession Where to store the client session handle on success.
115 *
116 */
117RTDECL(int) RTLocalIpcServerListen(RTLOCALIPCSERVER hServer, PRTLOCALIPCSESSION phClientSession);
118
119/**
120 * Cancel the current or subsequent RTLocalIpcServerListen call.
121 *
122 * @returns IPRT status code.
123 * @param hServer The server handle. The nil value is quietly ignored (VINF_SUCCESS).
124 */
125RTDECL(int) RTLocalIpcServerCancel(RTLOCALIPCSERVER hServer);
126
127
128/**
129 * Connects to a local IPC server.
130 *
131 * This is used a client process (or thread).
132 *
133 * @returns IPRT status code.
134 * @retval VINF_SUCCESS on success and *phSession holding the session handle.
135 *
136 * @param phSession Where to store the sesson handle on success.
137 * @param pszName The server name (see RTLocalIpcServerCreate for details).
138 * @param fFlags Flags, RTLOCALIPC_C_FLAGS_XXX.
139 */
140RTDECL(int) RTLocalIpcSessionConnect(PRTLOCALIPCSESSION phSession, const char *pszName, uint32_t fFlags);
141
142/** @name RTLOCALIPC_C_FLAGS_XXX - RTLocalIpcSessionConnect flags
143 * @{ */
144/** Native name, as apposed to a portable one. */
145#define RTLOCALIPC_C_FLAGS_NATIVE_NAME RT_BIT_32(0)
146/** The mask of valid flags. */
147#define RTLOCALIPC_C_FLAGS_VALID_MASK UINT32_C(0x00000001)
148/** @} */
149
150/**
151 * Closes the local IPC session.
152 *
153 * This can be used with sessions created by both RTLocalIpcSessionConnect
154 * and RTLocalIpcServerListen. It will release one cancel pending I/O and
155 * relase one reference (typically the implict reference from the create API).
156 *
157 * @returns IPRT status code.
158 * @retval VINF_SUCCESS if still other references or NIL.
159 * @retval VINF_OBJECT_DESTROYED if session destroyed.
160 *
161 * @param hSession The session handle. The nil value is quietly ignored (VINF_SUCCESS).
162 */
163RTDECL(int) RTLocalIpcSessionClose(RTLOCALIPCSESSION hSession);
164
165/**
166 * Retain a refence to the given session.
167 *
168 * @returns New reference count, UINT32_MAX if the handle is invalid.
169 * @param hSession The session handle.
170 */
171RTDECL(uint32_t) RTLocalIpcSessionRetain(RTLOCALIPCSESSION hSession);
172
173/**
174 * Releases a refence to the given session.
175 *
176 * This differs from RTLocalIpcSessionClose in that it won't cancel any pending
177 * I/O. So, better call RTLocalIpcSessionClose if you want to terminate the
178 * session.
179 *
180 * @returns New reference count, 0 if NIL handle, UINT32_MAX if the handle is
181 * invalid.
182 * @param hSession The session handle.
183 */
184RTDECL(uint32_t) RTLocalIpcSessionRelease(RTLOCALIPCSESSION hSession);
185
186
187/**
188 * Receive data from the other end of an local IPC session.
189 *
190 * This will block if there isn't any data.
191 *
192 * @returns IPRT status code.
193 * @retval VERR_CANCELLED if the operation was cancelled by RTLocalIpcSessionCancel.
194 *
195 * @param hSession The session handle.
196 * @param pvBuf Where to store the data.
197 * @param cbToRead How much to read. This is exact request if
198 * pcbRead is NULL, otherwise it's an upper limit.
199 * @param pcbRead Optional argument for indicating a partial read
200 * and returning the number of bytes actually read.
201 */
202RTDECL(int) RTLocalIpcSessionRead(RTLOCALIPCSESSION hSession, void *pvBuf, size_t cbToRead, size_t *pcbRead);
203
204/**
205 * Receive pending data from the other end of an local IPC session.
206 *
207 * This will not block to wait for data.
208 *
209 * @returns IPRT status code.
210 * @retval VINF_TRY_AGAIN if no pending data (*pcbRead is set to 0).
211 * @retval VERR_CANCELLED if a previous operation was cancelled by
212 * RTLocalIpcSessionCancel (this operation isn't cancellable).
213 *
214 * @param hSession The session handle.
215 * @param pvBuf Where to store the data.
216 * @param cbToRead How much to read (upper limit).
217 * @param pcbRead Where to return exactly how much was read.
218 */
219RTDECL(int) RTLocalIpcSessionReadNB(RTLOCALIPCSESSION hSession, void *pvBuf, size_t cbToRead, size_t *pcbRead);
220
221/**
222 * Send data to the other end of an local IPC session.
223 *
224 * This may or may not block until the data is received by the other party,
225 * this is an implementation detail. If you want to make sure that the data
226 * has been received you should always call RTLocalIpcSessionFlush().
227 *
228 * @returns IPRT status code.
229 * @retval VERR_CANCELLED if the operation was cancelled by RTLocalIpcSessionCancel.
230 *
231 * @param hSession The session handle.
232 * @param pvBuf The data to write.
233 * @param cbToWrite How much to write.
234 */
235RTDECL(int) RTLocalIpcSessionWrite(RTLOCALIPCSESSION hSession, const void *pvBuf, size_t cbToWrite);
236
237/**
238 * Flush any buffered data and (perhaps) wait for the other party to receive it.
239 *
240 * The waiting for the other party to receive the data is
241 * implementation dependent.
242 *
243 * @returns IPRT status code.
244 * @retval VERR_CANCELLED if the operation was cancelled by RTLocalIpcSessionCancel.
245 *
246 * @param hSession The session handle.
247 */
248RTDECL(int) RTLocalIpcSessionFlush(RTLOCALIPCSESSION hSession);
249
250/**
251 * Wait for data to become ready for reading or for the session to be
252 * disconnected.
253 *
254 * @returns IPRT status code.
255 * @retval VINF_SUCCESS when there is data to read.
256 * @retval VERR_TIMEOUT if no data became available within the specified period (@a cMillies)
257 * @retval VERR_BROKEN_PIPE if the session was disconnected.
258 * @retval VERR_CANCELLED if the operation was cancelled by RTLocalIpcSessionCancel.
259 *
260 * @param hSession The session handle.
261 * @param cMillies The number of milliseconds to wait. Use RT_INDEFINITE_WAIT
262 * to wait forever.
263 *
264 * @remark VERR_INTERRUPTED will not be returned. If this is desired at some later point
265 * add a RTLocalIpcSessionWaitForDataNoResume() variant like we're using elsewhere.
266 */
267RTDECL(int) RTLocalIpcSessionWaitForData(RTLOCALIPCSESSION hSession, uint32_t cMillies);
268
269/**
270 * Cancells a pending or subsequent operation.
271 *
272 * Not all methods are cancellable, only those which are specfied
273 * returning VERR_CANCELLED. The others are assumed to not be blocking
274 * for ever and ever. However, the cancel is sticky, so the session must
275 * basically be trashed (closed) after calling this method.
276 *
277 * @returns IPRT status code.
278 *
279 * @param hSession The session handle.
280 */
281RTDECL(int) RTLocalIpcSessionCancel(RTLOCALIPCSESSION hSession);
282
283/**
284 * Query the process ID of the other party.
285 *
286 * This is an optional feature which may not be implemented, so don't
287 * depend on it and check for VERR_NOT_SUPPORTED.
288 *
289 * @returns IPRT status code.
290 * @retval VINF_SUCCESS and *pProcess on success.
291 * @retval VERR_CANCELLED if the operation was cancelled by RTLocalIpcSessionCancel.
292 * @retval VERR_NOT_SUPPORTED and *pProcess = NIL_RTPROCESS if not supported.
293 *
294 * @param hSession The session handle.
295 * @param pProcess Where to store the process ID.
296 */
297RTDECL(int) RTLocalIpcSessionQueryProcess(RTLOCALIPCSESSION hSession, PRTPROCESS pProcess);
298
299/**
300 * Query the user ID of the other party.
301 *
302 * This is an optional feature which may not be implemented, so don't
303 * depend on it and check for VERR_NOT_SUPPORTED.
304 *
305 * @returns IPRT status code.
306 * @retval VINF_SUCCESS and *pUid on success.
307 * @retval VERR_CANCELLED if the operation was cancelled by RTLocalIpcSessionCancel.
308 * @retval VERR_NOT_SUPPORTED and *pUid = NIL_RTUID if not supported.
309 *
310 * @param hSession The session handle.
311 * @param pUid Where to store the user ID on success.
312 */
313RTDECL(int) RTLocalIpcSessionQueryUserId(RTLOCALIPCSESSION hSession, PRTUID pUid);
314
315/**
316 * Query the group ID of the other party.
317 *
318 * This is an optional feature which may not be implemented, so don't
319 * depend on it and check for VERR_NOT_SUPPORTED.
320 *
321 * @returns IPRT status code.
322 * @retval VINF_SUCCESS and *pUid on success.
323 * @retval VERR_CANCELLED if the operation was cancelled by RTLocalIpcSessionCancel.
324 * @retval VERR_NOT_SUPPORTED and *pGid = NIL_RTUID if not supported.
325 *
326 * @param hSession The session handle.
327 * @param pGid Where to store the group ID on success.
328 */
329RTDECL(int) RTLocalIpcSessionQueryGroupId(RTLOCALIPCSESSION hSession, PRTGID pGid);
330
331/** @} */
332RT_C_DECLS_END
333
334#endif /* !IPRT_INCLUDED_localipc_h */
335
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette