pipe.h revision 33698994f509c2b3a92e81a828ee7a0e01b81de0
e9c0d852b4db112840ba14c704766576cffd623cvboxsync/** @file
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * IPRT - Anonymous Pipes.
e9c0d852b4db112840ba14c704766576cffd623cvboxsync */
e9c0d852b4db112840ba14c704766576cffd623cvboxsync
e9c0d852b4db112840ba14c704766576cffd623cvboxsync/*
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync * Copyright (C) 2010 Sun Microsystems, Inc.
e9c0d852b4db112840ba14c704766576cffd623cvboxsync *
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * available from http://www.virtualbox.org. This file is free software;
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * you can redistribute it and/or modify it under the terms of the GNU
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * General Public License (GPL) as published by the Free Software
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
e9c0d852b4db112840ba14c704766576cffd623cvboxsync *
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * The contents of this file may alternatively be used under the terms
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync * of the Common Development and Distribution License Version 1.0
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync * VirtualBox OSE distribution, in which case the provisions of the
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * CDDL are applicable instead of those of the GPL.
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync *
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync * You may elect to license modified versions of this file under the
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync * terms and conditions of either the GPL or the CDDL or both.
e9c0d852b4db112840ba14c704766576cffd623cvboxsync *
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync * additional information or have any questions.
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync */
e9c0d852b4db112840ba14c704766576cffd623cvboxsync
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync#ifndef ___iprt_pipe_h
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync#define ___iprt_pipe_h
e9c0d852b4db112840ba14c704766576cffd623cvboxsync
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync#include <iprt/cdefs.h>
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync#include <iprt/types.h>
e9c0d852b4db112840ba14c704766576cffd623cvboxsync
e9c0d852b4db112840ba14c704766576cffd623cvboxsyncRT_C_DECLS_BEGIN
e9c0d852b4db112840ba14c704766576cffd623cvboxsync
e9c0d852b4db112840ba14c704766576cffd623cvboxsync/** @defgroup grp_rt_pipe RTPipe - Anonymous Pipes
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * @ingroup grp_rt
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * @{
e9c0d852b4db112840ba14c704766576cffd623cvboxsync */
e9c0d852b4db112840ba14c704766576cffd623cvboxsync
e9c0d852b4db112840ba14c704766576cffd623cvboxsync/**
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * Create an anonymous pipe.
e9c0d852b4db112840ba14c704766576cffd623cvboxsync *
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * @returns IPRT status code.
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * @param phPipeRead Where to return the read end of the pipe.
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * @param phPipeWrite Where to return the write end of the pipe.
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * @param fFlags A combination of RTPIPE_C_XXX defines.
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync */
1612ddf488e6d1a459dc02cd7078215985f87081vboxsyncRTDECL(int) RTPipeCreate(PRTPIPE phPipeRead, PRTPIPE phPipeWrite, uint32_t fFlags);
e9c0d852b4db112840ba14c704766576cffd623cvboxsync
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync/** @name RTPipeCreate flags.
e9c0d852b4db112840ba14c704766576cffd623cvboxsync * @{ */
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync/** Mark the read end as inheritable. */
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync#define RTPIPE_C_INHERIT_READ RT_BIT(0)
e9c0d852b4db112840ba14c704766576cffd623cvboxsync/** Mark the write end as inheritable. */
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync#define RTPIPE_C_INHERIT_WRITE RT_BIT(1)
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync/** Mask of valid flags. */
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync#define RTPIPE_C_VALID_MASK UINT32_C(0x00000003)
1612ddf488e6d1a459dc02cd7078215985f87081vboxsync/** @} */
/**
* Closes one end of a pipe created by RTPipeCreate.
*
* @returns IPRT status code.
* @param hPipe The pipe end to close.
*/
RTDECL(int) RTPipeClose(RTPIPE hPipe);
/**
* Creates an IPRT pipe handle from a native one.
*
* Do NOT use the native handle after passing it to this function, IPRT owns it
* and might even have closed in some cases (in order to gain some query
* information access on Windows).
*
* @returns IPRT status code.
* @param phPipe Where to return the pipe handle.
* @param hNativePipe The native pipe handle.
* @param fFlags Pipe flags, RTPIPE_N_XXX.
*/
RTDECL(int) RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags);
/** @name RTPipeFromNative flags.
* @{ */
/** The read end. */
#define RTPIPE_N_READ RT_BIT(0)
/** The write end. */
#define RTPIPE_N_WRITE RT_BIT(1)
/** Make sure the pipe is inheritable if set and not inheritable when clear. */
#define RTPIPE_N_INHERIT RT_BIT(2)
/** Mask of valid flags. */
#define RTPIPE_N_VALID_MASK UINT32_C(0x00000003)
/** @} */
/**
* Gets the native handle for an IPRT pipe handle.
*
* This is mainly for passing a pipe to a child and then closing the parent
* handle. IPRT also uses it internally to implement RTProcCreatEx and
* RTPollSetAdd on some platforms. Do NOT expect sane API behavior if used
* for any other purpose.
*
* @returns The native handle. -1 on failure.
* @param hPipe The IPRT pipe handle.
*/
RTDECL(RTHCINTPTR) RTPipeToNative(RTPIPE hPipe);
/**
* Read bytes from a pipe, non-blocking.
*
* @returns IPRT status code.
* @retval VERR_WRONG_ORDER if racing a call to RTPipeReadBlocking.
* @retval VERR_BROKEN_PIPE if the remote party has disconnected and we've read
* all the buffered data.
* @retval VINF_TRY_AGAIN if no data was available. @a *pcbRead will be set to
* 0.
* @retval VERR_ACCESS_DENIED if it's a write pipe.
*
* @param hPipe The IPRT pipe handle to read from.
* @param pvBuf Where to put the bytes we read.
* @param cbToRead How much to read. Must be greater than 0.
* @param pcbRead Where to return the number of bytes that has been
* read (mandatory). This is 0 if there is no more
* bytes to read.
* @sa RTPipeReadBlocking.
*/
RTDECL(int) RTPipeRead(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead);
/**
* Read bytes from a pipe, blocking.
*
* @returns IPRT status code.
* @retval VERR_WRONG_ORDER if racing a call to RTPipeRead.
* @retval VERR_BROKEN_PIPE if the remote party has disconnected and we've read
* all the buffered data.
* @retval VERR_ACCESS_DENIED if it's a write pipe.
*
* @param hPipe The IPRT pipe handle to read from.
* @param pvBuf Where to put the bytes we read.
* @param cbToRead How much to read.
* @param pcbRead Where to return the number of bytes that has been
* read. Optional.
*/
RTDECL(int) RTPipeReadBlocking(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead);
/**
* Write bytes to a pipe, non-blocking.
*
* @returns IPRT status code.
* @retval VERR_WRONG_ORDER if racing a call to RTPipeWriteBlocking.
* @retval VERR_BROKEN_PIPE if the remote party has disconnected. Does not
* trigger when @a cbToWrite is 0.
* @retval VINF_TRY_AGAIN if no data was written. @a *pcbWritten will be set
* to 0.
* @retval VERR_ACCESS_DENIED if it's a read pipe.
*
* @param hPipe The IPRT pipe handle to write to.
* @param pvBuf What to write.
* @param cbToWrite How much to write.
* @param pcbWritten How many bytes we wrote, mandatory. The return can
* be 0.
*/
RTDECL(int) RTPipeWrite(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
/**
* Write bytes to a pipe, blocking.
*
* @returns IPRT status code.
* @retval VERR_WRONG_ORDER if racing a call to RTPipeWrite.
* @retval VERR_BROKEN_PIPE if the remote party has disconnected. Does not
* trigger when @a cbToWrite is 0.
* @retval VERR_ACCESS_DENIED if it's a read pipe.
*
* @param hPipe The IPRT pipe handle to write to.
* @param pvBuf What to write.
* @param cbToWrite How much to write.
* @param pcbWritten How many bytes we wrote, optional. If NULL then all
* bytes will be written.
*/
RTDECL(int) RTPipeWriteBlocking(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten);
/**
* Flushes the buffers for the specified pipe and making sure the other party
* reads them.
*
* @returns IPRT status code.
* @retval VERR_NOT_SUPPORTED if not supported by the OS.
* @retval VERR_BROKEN_PIPE if the remote party has disconnected.
* @retval VERR_ACCESS_DENIED if it's a read pipe.
*
* @param hPipe The IPRT pipe handle to flush.
*/
RTDECL(int) RTPipeFlush(RTPIPE hPipe);
/**
* Checks if the pipe is ready for reading or writing (depending on the pipe
* end).
*
* @returns IPRT status code.
* @retval VERR_TIMEOUT if the timeout was reached before the pipe was ready
* for reading/writing.
* @retval VERR_NOT_SUPPORTED if not supported by the OS?
*
* @param hPipe The IPRT pipe handle to select on.
* @param cMillies Number of milliseconds to wait. Use
* RT_INDEFINITE_WAIT to wait for ever.
*/
RTDECL(int) RTPipeSelectOne(RTPIPE hPipe, RTMSINTERVAL cMillies);
/** @} */
RT_C_DECLS_END
#endif