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