1 | /** @file
|
---|
2 | * IPRT - Anonymous Pipes.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2010-2011 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_pipe_h
|
---|
27 | #define ___iprt_pipe_h
|
---|
28 |
|
---|
29 | #include <iprt/cdefs.h>
|
---|
30 | #include <iprt/types.h>
|
---|
31 |
|
---|
32 | RT_C_DECLS_BEGIN
|
---|
33 |
|
---|
34 | /** @defgroup grp_rt_pipe RTPipe - Anonymous Pipes
|
---|
35 | * @ingroup grp_rt
|
---|
36 | * @{
|
---|
37 | */
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Create an anonymous pipe.
|
---|
41 | *
|
---|
42 | * @returns IPRT status code.
|
---|
43 | * @param phPipeRead Where to return the read end of the pipe.
|
---|
44 | * @param phPipeWrite Where to return the write end of the pipe.
|
---|
45 | * @param fFlags A combination of RTPIPE_C_XXX defines.
|
---|
46 | */
|
---|
47 | RTDECL(int) RTPipeCreate(PRTPIPE phPipeRead, PRTPIPE phPipeWrite, uint32_t fFlags);
|
---|
48 |
|
---|
49 | /** @name RTPipeCreate flags.
|
---|
50 | * @{ */
|
---|
51 | /** Mark the read end as inheritable. */
|
---|
52 | #define RTPIPE_C_INHERIT_READ RT_BIT(0)
|
---|
53 | /** Mark the write end as inheritable. */
|
---|
54 | #define RTPIPE_C_INHERIT_WRITE RT_BIT(1)
|
---|
55 | /** Mask of valid flags. */
|
---|
56 | #define RTPIPE_C_VALID_MASK UINT32_C(0x00000003)
|
---|
57 | /** @} */
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Closes one end of a pipe created by RTPipeCreate.
|
---|
61 | *
|
---|
62 | * @returns IPRT status code.
|
---|
63 | * @param hPipe The pipe end to close.
|
---|
64 | */
|
---|
65 | RTDECL(int) RTPipeClose(RTPIPE hPipe);
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Creates an IPRT pipe handle from a native one.
|
---|
69 | *
|
---|
70 | * Do NOT use the native handle after passing it to this function, IPRT owns it
|
---|
71 | * and might even have closed in some cases (in order to gain some query
|
---|
72 | * information access on Windows).
|
---|
73 | *
|
---|
74 | * @returns IPRT status code.
|
---|
75 | * @param phPipe Where to return the pipe handle.
|
---|
76 | * @param hNativePipe The native pipe handle.
|
---|
77 | * @param fFlags Pipe flags, RTPIPE_N_XXX.
|
---|
78 | */
|
---|
79 | RTDECL(int) RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags);
|
---|
80 |
|
---|
81 | /** @name RTPipeFromNative flags.
|
---|
82 | * @{ */
|
---|
83 | /** The read end. */
|
---|
84 | #define RTPIPE_N_READ RT_BIT(0)
|
---|
85 | /** The write end. */
|
---|
86 | #define RTPIPE_N_WRITE RT_BIT(1)
|
---|
87 | /** Make sure the pipe is inheritable if set and not inheritable when clear. */
|
---|
88 | #define RTPIPE_N_INHERIT RT_BIT(2)
|
---|
89 | /** Mask of valid flags. */
|
---|
90 | #define RTPIPE_N_VALID_MASK UINT32_C(0x00000007)
|
---|
91 | /** @} */
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Gets the native handle for an IPRT pipe handle.
|
---|
95 | *
|
---|
96 | * This is mainly for passing a pipe to a child and then closing the parent
|
---|
97 | * handle. IPRT also uses it internally to implement RTProcCreatEx and
|
---|
98 | * RTPollSetAdd on some platforms. Do NOT expect sane API behavior if used
|
---|
99 | * for any other purpose.
|
---|
100 | *
|
---|
101 | * @returns The native handle. -1 on failure.
|
---|
102 | * @param hPipe The IPRT pipe handle.
|
---|
103 | */
|
---|
104 | RTDECL(RTHCINTPTR) RTPipeToNative(RTPIPE hPipe);
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Read bytes from a pipe, non-blocking.
|
---|
108 | *
|
---|
109 | * @returns IPRT status code.
|
---|
110 | * @retval VERR_WRONG_ORDER if racing a call to RTPipeReadBlocking.
|
---|
111 | * @retval VERR_BROKEN_PIPE if the remote party has disconnected and we've read
|
---|
112 | * all the buffered data.
|
---|
113 | * @retval VINF_TRY_AGAIN if no data was available. @a *pcbRead will be set to
|
---|
114 | * 0.
|
---|
115 | * @retval VERR_ACCESS_DENIED if it's a write pipe.
|
---|
116 | *
|
---|
117 | * @param hPipe The IPRT pipe handle to read from.
|
---|
118 | * @param pvBuf Where to put the bytes we read.
|
---|
119 | * @param cbToRead How much to read. Must be greater than 0.
|
---|
120 | * @param pcbRead Where to return the number of bytes that has been
|
---|
121 | * read (mandatory). This is 0 if there is no more
|
---|
122 | * bytes to read.
|
---|
123 | * @sa RTPipeReadBlocking.
|
---|
124 | */
|
---|
125 | RTDECL(int) RTPipeRead(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead);
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Read bytes from a pipe, blocking.
|
---|
129 | *
|
---|
130 | * @returns IPRT status code.
|
---|
131 | * @retval VERR_WRONG_ORDER if racing a call to RTPipeRead.
|
---|
132 | * @retval VERR_BROKEN_PIPE if the remote party has disconnected and we've read
|
---|
133 | * all the buffered data.
|
---|
134 | * @retval VERR_ACCESS_DENIED if it's a write pipe.
|
---|
135 | *
|
---|
136 | * @param hPipe The IPRT pipe handle to read from.
|
---|
137 | * @param pvBuf Where to put the bytes we read.
|
---|
138 | * @param cbToRead How much to read.
|
---|
139 | * @param pcbRead Where to return the number of bytes that has been
|
---|
140 | * read. Optional.
|
---|
141 | */
|
---|
142 | RTDECL(int) RTPipeReadBlocking(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead);
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Write bytes to a pipe, non-blocking.
|
---|
146 | *
|
---|
147 | * @returns IPRT status code.
|
---|
148 | * @retval VERR_WRONG_ORDER if racing a call to RTPipeWriteBlocking.
|
---|
149 | * @retval VERR_BROKEN_PIPE if the remote party has disconnected. Does not
|
---|
150 | * trigger when @a cbToWrite is 0.
|
---|
151 | * @retval VINF_TRY_AGAIN if no data was written. @a *pcbWritten will be set
|
---|
152 | * to 0.
|
---|
153 | * @retval VERR_ACCESS_DENIED if it's a read pipe.
|
---|
154 | *
|
---|
155 | * @param hPipe The IPRT pipe handle to write to.
|
---|
156 | * @param pvBuf What to write.
|
---|
157 | * @param cbToWrite How much to write.
|
---|
158 | * @param pcbWritten How many bytes we wrote, mandatory. The return can
|
---|
159 | * be 0.
|
---|
160 | */
|
---|
161 | RTDECL(int) RTPipeWrite(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Write bytes to a pipe, blocking.
|
---|
165 | *
|
---|
166 | * @returns IPRT status code.
|
---|
167 | * @retval VERR_WRONG_ORDER if racing a call to RTPipeWrite.
|
---|
168 | * @retval VERR_BROKEN_PIPE if the remote party has disconnected. Does not
|
---|
169 | * trigger when @a cbToWrite is 0.
|
---|
170 | * @retval VERR_ACCESS_DENIED if it's a read pipe.
|
---|
171 | *
|
---|
172 | * @param hPipe The IPRT pipe handle to write to.
|
---|
173 | * @param pvBuf What to write.
|
---|
174 | * @param cbToWrite How much to write.
|
---|
175 | * @param pcbWritten How many bytes we wrote, optional. If NULL then all
|
---|
176 | * bytes will be written.
|
---|
177 | */
|
---|
178 | RTDECL(int) RTPipeWriteBlocking(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Flushes the buffers for the specified pipe and making sure the other party
|
---|
182 | * reads them.
|
---|
183 | *
|
---|
184 | * @returns IPRT status code.
|
---|
185 | * @retval VERR_NOT_SUPPORTED if not supported by the OS.
|
---|
186 | * @retval VERR_BROKEN_PIPE if the remote party has disconnected.
|
---|
187 | * @retval VERR_ACCESS_DENIED if it's a read pipe.
|
---|
188 | *
|
---|
189 | * @param hPipe The IPRT pipe handle to flush.
|
---|
190 | */
|
---|
191 | RTDECL(int) RTPipeFlush(RTPIPE hPipe);
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Checks if the pipe is ready for reading or writing (depending on the pipe
|
---|
195 | * end).
|
---|
196 | *
|
---|
197 | * @returns IPRT status code.
|
---|
198 | * @retval VERR_TIMEOUT if the timeout was reached before the pipe was ready
|
---|
199 | * for reading/writing.
|
---|
200 | * @retval VERR_NOT_SUPPORTED if not supported by the OS?
|
---|
201 | *
|
---|
202 | * @param hPipe The IPRT pipe handle to select on.
|
---|
203 | * @param cMillies Number of milliseconds to wait. Use
|
---|
204 | * RT_INDEFINITE_WAIT to wait for ever.
|
---|
205 | */
|
---|
206 | RTDECL(int) RTPipeSelectOne(RTPIPE hPipe, RTMSINTERVAL cMillies);
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Queries the number of bytes immediately available for reading.
|
---|
210 | *
|
---|
211 | * @returns IPRT status code.
|
---|
212 | * @retval VERR_NOT_SUPPORTED if not supported by the OS. The caller shall
|
---|
213 | * handle this case.
|
---|
214 | *
|
---|
215 | * @param hPipe The IPRT read pipe handle.
|
---|
216 | * @param pcbReadable Where to return the number of bytes that is ready
|
---|
217 | * to be read.
|
---|
218 | */
|
---|
219 | RTDECL(int) RTPipeQueryReadable(RTPIPE hPipe, size_t *pcbReadable);
|
---|
220 |
|
---|
221 | /** @} */
|
---|
222 |
|
---|
223 | RT_C_DECLS_END
|
---|
224 |
|
---|
225 | #endif
|
---|
226 |
|
---|