1 | /** @file
|
---|
2 | * IPRT - Anonymous Pipes.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2010-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_pipe_h
|
---|
27 | #define IPRT_INCLUDED_pipe_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/fs.h>
|
---|
35 |
|
---|
36 | RT_C_DECLS_BEGIN
|
---|
37 |
|
---|
38 | /** @defgroup grp_rt_pipe RTPipe - Anonymous Pipes
|
---|
39 | * @ingroup grp_rt
|
---|
40 | *
|
---|
41 | * @note The current Windows implementation has some peculiarities,
|
---|
42 | * especially with respect to the write side where the it is possible
|
---|
43 | * to write one extra pipe buffer sized block of data when the pipe
|
---|
44 | * buffer is full.
|
---|
45 | *
|
---|
46 | * @{
|
---|
47 | */
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Create an anonymous pipe.
|
---|
51 | *
|
---|
52 | * @returns IPRT status code.
|
---|
53 | * @param phPipeRead Where to return the read end of the pipe.
|
---|
54 | * @param phPipeWrite Where to return the write end of the pipe.
|
---|
55 | * @param fFlags A combination of RTPIPE_C_XXX defines.
|
---|
56 | */
|
---|
57 | RTDECL(int) RTPipeCreate(PRTPIPE phPipeRead, PRTPIPE phPipeWrite, uint32_t fFlags);
|
---|
58 |
|
---|
59 | /** @name RTPipeCreate flags.
|
---|
60 | * @{ */
|
---|
61 | /** Mark the read end as inheritable. */
|
---|
62 | #define RTPIPE_C_INHERIT_READ RT_BIT(0)
|
---|
63 | /** Mark the write end as inheritable. */
|
---|
64 | #define RTPIPE_C_INHERIT_WRITE RT_BIT(1)
|
---|
65 | /** Mask of valid flags. */
|
---|
66 | #define RTPIPE_C_VALID_MASK UINT32_C(0x00000003)
|
---|
67 | /** @} */
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Closes one end of a pipe created by RTPipeCreate.
|
---|
71 | *
|
---|
72 | * @returns IPRT status code.
|
---|
73 | * @param hPipe The pipe end to close.
|
---|
74 | */
|
---|
75 | RTDECL(int) RTPipeClose(RTPIPE hPipe);
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Closes one end of a pipe created by RTPipeCreate, extended version.
|
---|
79 | *
|
---|
80 | * @returns IPRT status code.
|
---|
81 | * @param hPipe The pipe end to close.
|
---|
82 | * @param fLeaveOpen Wheter to leave the underlying native handle open
|
---|
83 | * (for RTPipeClose() this is @c false).
|
---|
84 | */
|
---|
85 | RTDECL(int) RTPipeCloseEx(RTPIPE hPipe, bool fLeaveOpen);
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Creates an IPRT pipe handle from a native one.
|
---|
89 | *
|
---|
90 | * Do NOT use the native handle after passing it to this function, IPRT owns it
|
---|
91 | * and might even have closed in some cases (in order to gain some query
|
---|
92 | * information access on Windows).
|
---|
93 | *
|
---|
94 | * @returns IPRT status code.
|
---|
95 | * @param phPipe Where to return the pipe handle.
|
---|
96 | * @param hNativePipe The native pipe handle.
|
---|
97 | * @param fFlags Pipe flags, RTPIPE_N_XXX.
|
---|
98 | */
|
---|
99 | RTDECL(int) RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags);
|
---|
100 |
|
---|
101 | /** @name RTPipeFromNative flags.
|
---|
102 | * @{ */
|
---|
103 | /** The read end. */
|
---|
104 | #define RTPIPE_N_READ RT_BIT(0)
|
---|
105 | /** The write end. */
|
---|
106 | #define RTPIPE_N_WRITE RT_BIT(1)
|
---|
107 | /** Make sure the pipe is inheritable if set and not inheritable when clear. */
|
---|
108 | #define RTPIPE_N_INHERIT RT_BIT(2)
|
---|
109 | /** Mask of valid flags for . */
|
---|
110 | #define RTPIPE_N_VALID_MASK UINT32_C(0x00000007)
|
---|
111 | /** RTPipeFromNative: Leave the native pipe handle open on close. */
|
---|
112 | #define RTPIPE_N_LEAVE_OPEN RT_BIT(3)
|
---|
113 | /** Mask of valid flags for RTPipeFromNative(). */
|
---|
114 | #define RTPIPE_N_VALID_MASK_FN UINT32_C(0x0000000f)
|
---|
115 | /** @} */
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Gets the native handle for an IPRT pipe handle.
|
---|
119 | *
|
---|
120 | * This is mainly for passing a pipe to a child and then closing the parent
|
---|
121 | * handle. IPRT also uses it internally to implement RTProcCreatEx and
|
---|
122 | * RTPollSetAdd on some platforms. Do NOT expect sane API behavior if used
|
---|
123 | * for any other purpose.
|
---|
124 | *
|
---|
125 | * @returns The native handle. -1 on failure.
|
---|
126 | * @param hPipe The IPRT pipe handle.
|
---|
127 | */
|
---|
128 | RTDECL(RTHCINTPTR) RTPipeToNative(RTPIPE hPipe);
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Get the creation inheritability of the pipe.
|
---|
132 | *
|
---|
133 | * @returns true if inherited by children (when pipe was created), false if not.
|
---|
134 | * @param hPipe The IPRT pipe handle.
|
---|
135 | */
|
---|
136 | RTDECL(int) RTPipeGetCreationInheritability(RTPIPE hPipe);
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Read bytes from a pipe, non-blocking.
|
---|
140 | *
|
---|
141 | * @returns IPRT status code.
|
---|
142 | * @retval VERR_WRONG_ORDER if racing a call to RTPipeReadBlocking.
|
---|
143 | * @retval VERR_BROKEN_PIPE if the remote party has disconnected and we've read
|
---|
144 | * all the buffered data.
|
---|
145 | * @retval VINF_TRY_AGAIN if no data was available. @a *pcbRead will be set to
|
---|
146 | * 0.
|
---|
147 | * @retval VERR_ACCESS_DENIED if it's a write pipe.
|
---|
148 | *
|
---|
149 | * @param hPipe The IPRT pipe handle to read from.
|
---|
150 | * @param pvBuf Where to put the bytes we read.
|
---|
151 | * @param cbToRead How much to read. Must be greater than 0.
|
---|
152 | * @param pcbRead Where to return the number of bytes that has been
|
---|
153 | * read (mandatory). This is 0 if there is no more
|
---|
154 | * bytes to read.
|
---|
155 | * @sa RTPipeReadBlocking.
|
---|
156 | */
|
---|
157 | RTDECL(int) RTPipeRead(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead);
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Read bytes from a pipe, blocking.
|
---|
161 | *
|
---|
162 | * @returns IPRT status code.
|
---|
163 | * @retval VERR_WRONG_ORDER if racing a call to RTPipeRead.
|
---|
164 | * @retval VERR_BROKEN_PIPE if the remote party has disconnected and we've read
|
---|
165 | * all the buffered data.
|
---|
166 | * @retval VERR_ACCESS_DENIED if it's a write pipe.
|
---|
167 | *
|
---|
168 | * @param hPipe The IPRT pipe handle to read from.
|
---|
169 | * @param pvBuf Where to put the bytes we read.
|
---|
170 | * @param cbToRead How much to read.
|
---|
171 | * @param pcbRead Where to return the number of bytes that has been
|
---|
172 | * read. Optional.
|
---|
173 | */
|
---|
174 | RTDECL(int) RTPipeReadBlocking(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead);
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Write bytes to a pipe, non-blocking.
|
---|
178 | *
|
---|
179 | * @returns IPRT status code.
|
---|
180 | * @retval VERR_WRONG_ORDER if racing a call to RTPipeWriteBlocking.
|
---|
181 | * @retval VERR_BROKEN_PIPE if the remote party has disconnected. Does not
|
---|
182 | * trigger when @a cbToWrite is 0.
|
---|
183 | * @retval VINF_TRY_AGAIN if no data was written. @a *pcbWritten will be set
|
---|
184 | * to 0.
|
---|
185 | * @retval VERR_ACCESS_DENIED if it's a read pipe.
|
---|
186 | *
|
---|
187 | * @param hPipe The IPRT pipe handle to write to.
|
---|
188 | * @param pvBuf What to write.
|
---|
189 | * @param cbToWrite How much to write.
|
---|
190 | * @param pcbWritten How many bytes we wrote, mandatory. The return can
|
---|
191 | * be 0.
|
---|
192 | */
|
---|
193 | RTDECL(int) RTPipeWrite(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Write bytes to a pipe, blocking.
|
---|
197 | *
|
---|
198 | * @returns IPRT status code.
|
---|
199 | * @retval VERR_WRONG_ORDER if racing a call to RTPipeWrite.
|
---|
200 | * @retval VERR_BROKEN_PIPE if the remote party has disconnected. Does not
|
---|
201 | * trigger when @a cbToWrite is 0.
|
---|
202 | * @retval VERR_ACCESS_DENIED if it's a read pipe.
|
---|
203 | *
|
---|
204 | * @param hPipe The IPRT pipe handle to write to.
|
---|
205 | * @param pvBuf What to write.
|
---|
206 | * @param cbToWrite How much to write.
|
---|
207 | * @param pcbWritten How many bytes we wrote, optional. If NULL then all
|
---|
208 | * bytes will be written.
|
---|
209 | */
|
---|
210 | RTDECL(int) RTPipeWriteBlocking(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Flushes the buffers for the specified pipe and making sure the other party
|
---|
214 | * reads them.
|
---|
215 | *
|
---|
216 | * @returns IPRT status code.
|
---|
217 | * @retval VERR_NOT_SUPPORTED if not supported by the OS.
|
---|
218 | * @retval VERR_BROKEN_PIPE if the remote party has disconnected.
|
---|
219 | * @retval VERR_ACCESS_DENIED if it's a read pipe.
|
---|
220 | *
|
---|
221 | * @param hPipe The IPRT pipe handle to flush.
|
---|
222 | */
|
---|
223 | RTDECL(int) RTPipeFlush(RTPIPE hPipe);
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Checks if the pipe is ready for reading or writing (depending on the pipe
|
---|
227 | * end).
|
---|
228 | *
|
---|
229 | * @returns IPRT status code.
|
---|
230 | * @retval VERR_TIMEOUT if the timeout was reached before the pipe was ready
|
---|
231 | * for reading/writing.
|
---|
232 | * @retval VERR_NOT_SUPPORTED if not supported by the OS?
|
---|
233 | *
|
---|
234 | * @param hPipe The IPRT pipe handle to select on.
|
---|
235 | * @param cMillies Number of milliseconds to wait. Use
|
---|
236 | * RT_INDEFINITE_WAIT to wait for ever.
|
---|
237 | */
|
---|
238 | RTDECL(int) RTPipeSelectOne(RTPIPE hPipe, RTMSINTERVAL cMillies);
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Queries the number of bytes immediately available for reading.
|
---|
242 | *
|
---|
243 | * @returns IPRT status code.
|
---|
244 | * @retval VERR_NOT_SUPPORTED if not supported by the OS. The caller shall
|
---|
245 | * handle this case.
|
---|
246 | *
|
---|
247 | * @param hPipe The IPRT read pipe handle.
|
---|
248 | * @param pcbReadable Where to return the number of bytes that is ready
|
---|
249 | * to be read.
|
---|
250 | */
|
---|
251 | RTDECL(int) RTPipeQueryReadable(RTPIPE hPipe, size_t *pcbReadable);
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Query information about a pipe (mainly a VFS I/O stream formality).
|
---|
255 | *
|
---|
256 | * The only thing we guarentee to be returned is RTFSOBJINFO::Attr.fMode being
|
---|
257 | * set to FIFO and will reflect the read/write end in the RTFS_DOS_READONLY,
|
---|
258 | * RTFS_UNIX_IRUSR and RTFS_UNIX_IWUSR bits.
|
---|
259 | *
|
---|
260 | * Some implementations sometimes provide the pipe buffer size via
|
---|
261 | * RTFSOBJINFO::cbAllocated.
|
---|
262 | *
|
---|
263 | * Some implementations sometimes provide the available read data or available
|
---|
264 | * write space via RTFSOBJINFO::cbObject.
|
---|
265 | *
|
---|
266 | * Some implementations sometimes provide valid device and/or inode numbers.
|
---|
267 | *
|
---|
268 | * @returns iprt status code.
|
---|
269 | *
|
---|
270 | * @param hPipe The IPRT read pipe handle.
|
---|
271 | * @param pObjInfo Object information structure to be filled on successful
|
---|
272 | * return.
|
---|
273 | * @param enmAddAttr Which set of additional attributes to request. Use
|
---|
274 | * RTFSOBJATTRADD_NOTHING if this doesn't matter.
|
---|
275 | */
|
---|
276 | RTDECL(int) RTPipeQueryInfo(RTPIPE hPipe, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr);
|
---|
277 |
|
---|
278 | /** @} */
|
---|
279 |
|
---|
280 | RT_C_DECLS_END
|
---|
281 |
|
---|
282 | #endif /* !IPRT_INCLUDED_pipe_h */
|
---|
283 |
|
---|