VirtualBox

source: vbox/trunk/include/iprt/pipe.h@ 86716

Last change on this file since 86716 was 86412, checked in by vboxsync, 4 years ago

IPRT/pipe: Adding RTPipeCloseEx w/ a fLeaveOpen parameter so we can prevent leaks via RTHandleGetStandard. Adding RTPIPE_N_LEAVE_OPEN to RTPipeFromNative. bugref:9841

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