pipe-posix.cpp revision c7814cf6e1240a519cbec0441e033d0e2470ed00
72e560433c691132e8f21f463955515d4511c34bvboxsync * IPRT - Anonymous Pipes, POSIX Implementation.
72e560433c691132e8f21f463955515d4511c34bvboxsync * Copyright (C) 2010-2013 Oracle Corporation
72e560433c691132e8f21f463955515d4511c34bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
72e560433c691132e8f21f463955515d4511c34bvboxsync * available from http://www.virtualbox.org. This file is free software;
72e560433c691132e8f21f463955515d4511c34bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
72e560433c691132e8f21f463955515d4511c34bvboxsync * General Public License (GPL) as published by the Free Software
72e560433c691132e8f21f463955515d4511c34bvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
72e560433c691132e8f21f463955515d4511c34bvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
72e560433c691132e8f21f463955515d4511c34bvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
72e560433c691132e8f21f463955515d4511c34bvboxsync * The contents of this file may alternatively be used under the terms
72e560433c691132e8f21f463955515d4511c34bvboxsync * of the Common Development and Distribution License Version 1.0
72e560433c691132e8f21f463955515d4511c34bvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
72e560433c691132e8f21f463955515d4511c34bvboxsync * VirtualBox OSE distribution, in which case the provisions of the
72e560433c691132e8f21f463955515d4511c34bvboxsync * CDDL are applicable instead of those of the GPL.
72e560433c691132e8f21f463955515d4511c34bvboxsync * You may elect to license modified versions of this file under the
72e560433c691132e8f21f463955515d4511c34bvboxsync * terms and conditions of either the GPL or the CDDL or both.
72e560433c691132e8f21f463955515d4511c34bvboxsync/*******************************************************************************
72e560433c691132e8f21f463955515d4511c34bvboxsync* Header Files *
72e560433c691132e8f21f463955515d4511c34bvboxsync*******************************************************************************/
72e560433c691132e8f21f463955515d4511c34bvboxsync/*******************************************************************************
72e560433c691132e8f21f463955515d4511c34bvboxsync* Structures and Typedefs *
72e560433c691132e8f21f463955515d4511c34bvboxsync*******************************************************************************/
72e560433c691132e8f21f463955515d4511c34bvboxsync /** Magic value (RTPIPE_MAGIC). */
72e560433c691132e8f21f463955515d4511c34bvboxsync /** The file descriptor. */
72e560433c691132e8f21f463955515d4511c34bvboxsync /** Set if this is the read end, clear if it's the write end. */
72e560433c691132e8f21f463955515d4511c34bvboxsync /** Atomically operated state variable.
72e560433c691132e8f21f463955515d4511c34bvboxsync * - Bits 0 thru 29 - Users of the new mode.
72e560433c691132e8f21f463955515d4511c34bvboxsync * - Bit 30 - The pipe mode, set indicates blocking.
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * - Bit 31 - Set when we're switching the mode.
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync/*******************************************************************************
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync* Defined Constants And Macros *
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync*******************************************************************************/
72e560433c691132e8f21f463955515d4511c34bvboxsync/** @name RTPIPEINTERNAL::u32State defines
72e560433c691132e8f21f463955515d4511c34bvboxsync#define RTPIPE_POSIX_USERS_MASK UINT32_C(0x3fffffff)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * Wrapper for calling pipe2() or pipe().
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * When using pipe2() the returned handles are marked close-on-exec and does
72e560433c691132e8f21f463955515d4511c34bvboxsync * not risk racing process creation calls on other threads.
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * @returns See pipe().
72e560433c691132e8f21f463955515d4511c34bvboxsync * @param paFds See pipe().
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync * @param piNewPipeSyscall Where to cache which call we should used. -1 if
72e560433c691132e8f21f463955515d4511c34bvboxsync * pipe(), 1 if pipe2(), 0 if not yet decided.
72e560433c691132e8f21f463955515d4511c34bvboxsyncstatic int my_pipe_wrapper(int *paFds, int *piNewPipeSyscall)
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync#if defined(RT_OS_LINUX) && defined(__NR_pipe2) && defined(O_CLOEXEC)
72e560433c691132e8f21f463955515d4511c34bvboxsync return (int)rc;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsyncRTDECL(int) RTPipeCreate(PRTPIPE phPipeRead, PRTPIPE phPipeWrite, uint32_t fFlags)
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync AssertPtrReturn(phPipeWrite, VERR_INVALID_POINTER);
72e560433c691132e8f21f463955515d4511c34bvboxsync AssertReturn(!(fFlags & ~RTPIPE_C_VALID_MASK), VERR_INVALID_PARAMETER);
72e560433c691132e8f21f463955515d4511c34bvboxsync * Create the pipe and clear/set the close-on-exec flag as required.
72e560433c691132e8f21f463955515d4511c34bvboxsync static int s_iNewPipeSyscall = 0;
72e560433c691132e8f21f463955515d4511c34bvboxsync /* created with close-on-exec set. */
72e560433c691132e8f21f463955515d4511c34bvboxsync /* created with close-on-exec cleared. */
72e560433c691132e8f21f463955515d4511c34bvboxsync * Create the two handles.
72e560433c691132e8f21f463955515d4511c34bvboxsync RTPIPEINTERNAL *pThisR = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
72e560433c691132e8f21f463955515d4511c34bvboxsync RTPIPEINTERNAL *pThisW = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
72e560433c691132e8f21f463955515d4511c34bvboxsync * Before we leave, make sure to shut up SIGPIPE.
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
72e560433c691132e8f21f463955515d4511c34bvboxsync * Do the cleanup.
72e560433c691132e8f21f463955515d4511c34bvboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTPIPE_MAGIC, RTPIPE_MAGIC), VERR_INVALID_HANDLE);
72e560433c691132e8f21f463955515d4511c34bvboxsync if (ASMAtomicReadU32(&pThis->u32State) & RTPIPE_POSIX_USERS_MASK)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsyncRTDECL(int) RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK), VERR_INVALID_PARAMETER);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync AssertReturn(!!(fFlags & RTPIPE_N_READ) != !!(fFlags & RTPIPE_N_WRITE), VERR_INVALID_PARAMETER);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * Get and validate the pipe handle info.
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync AssertReturn(fstat(hNative, &st) == 0, RTErrConvertFromErrno(errno));
72e560433c691132e8f21f463955515d4511c34bvboxsync AssertMsgReturn(S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode), ("%#x (%o)\n", st.st_mode, st.st_mode), VERR_INVALID_HANDLE);
72e560433c691132e8f21f463955515d4511c34bvboxsync AssertMsgReturn( (fFd & O_ACCMODE) == (fFlags & RTPIPE_N_READ ? O_RDONLY : O_WRONLY)
72e560433c691132e8f21f463955515d4511c34bvboxsync || (fFd & O_ACCMODE) == O_RDWR /* Solaris creates bi-directional pipes. */
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync * Create the handle.
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync RTPIPEINTERNAL *pThis = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
72e560433c691132e8f21f463955515d4511c34bvboxsync pThis->u32State = fFd & O_NONBLOCK ? 0 : RTPIPE_POSIX_BLOCKING;
72e560433c691132e8f21f463955515d4511c34bvboxsync * Fix up inheritability and shut up SIGPIPE and we're done.
72e560433c691132e8f21f463955515d4511c34bvboxsync if (fcntl(hNative, F_SETFD, fFlags & RTPIPE_N_INHERIT ? 0 : FD_CLOEXEC) == 0)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * Prepare blocking mode.
72e560433c691132e8f21f463955515d4511c34bvboxsync * @returns VINF_SUCCESS
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * @retval VERR_WRONG_ORDER
72e560433c691132e8f21f463955515d4511c34bvboxsync * @retval VERR_INTERNAL_ERROR_4
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync * @param pThis The pipe handle.
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync * Update the state.
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync uint32_t u32State = ASMAtomicReadU32(&pThis->u32State);
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync uint32_t const cUsers = (u32State & RTPIPE_POSIX_USERS_MASK);
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync AssertReturn(cUsers < RTPIPE_POSIX_USERS_MASK / 2, VERR_INTERNAL_ERROR_4);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync else if (cUsers == 0)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync u32State = 1 | RTPIPE_POSIX_SWITCHING | RTPIPE_POSIX_BLOCKING;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync * Do the switching.
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync || fcntl(pThis->fd, F_SETFL, fFlags & ~O_NONBLOCK) != -1)
48b14aaf804d764e2c7bec8243c379f7ff489ebbvboxsync ASMAtomicBitClear(&pThis->u32State, RTPIPE_POSIX_SWITCHING_BIT);
72e560433c691132e8f21f463955515d4511c34bvboxsync * Prepare non-blocking mode.
72e560433c691132e8f21f463955515d4511c34bvboxsync * @returns VINF_SUCCESS
72e560433c691132e8f21f463955515d4511c34bvboxsync * @retval VERR_WRONG_ORDER
72e560433c691132e8f21f463955515d4511c34bvboxsync * @retval VERR_INTERNAL_ERROR_4
72e560433c691132e8f21f463955515d4511c34bvboxsync * @param pThis The pipe handle.
72e560433c691132e8f21f463955515d4511c34bvboxsyncstatic int rtPipeTryNonBlocking(RTPIPEINTERNAL *pThis)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * Update the state.
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync uint32_t u32State = ASMAtomicReadU32(&pThis->u32State);
72e560433c691132e8f21f463955515d4511c34bvboxsync uint32_t const cUsers = (u32State & RTPIPE_POSIX_USERS_MASK);
return VINF_SUCCESS;
else if (cUsers == 0)
return VERR_WRONG_ORDER;
ASMNopPause();
return VINF_SUCCESS;
if (cbRead >= 0)
*pcbRead = 0;
return rc;
while (cbToRead > 0)
if (cbRead < 0)
if (pcbRead)
&& cbTotalRead
return rc;
if (cbToWrite)
if (cbWritten >= 0)
*pcbWritten = 0;
*pcbWritten = 0;
return rc;
RTDECL(int) RTPipeWriteBlocking(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
while (cbToWrite > 0)
if (cbWritten < 0)
if (pcbWritten)
return rc;
return VERR_NOT_SUPPORTED;
return VINF_SUCCESS;
int timeout;
int cb = 0;
return VINF_SUCCESS;
return rc;
return VINF_SUCCESS;