pipe-posix.cpp revision 9c901af82dd5443ef16e2412aa6109e1e3476325
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * IPRT - Anonymous Pipes, POSIX Implementation.
c7814cf6e1240a519cbec0441e033d0e2470ed00vboxsync * Copyright (C) 2010 Oracle Corporation
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * available from http://www.virtualbox.org. This file is free software;
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * you can redistribute it and/or modify it under the terms of the GNU
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * General Public License (GPL) as published by the Free Software
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * The contents of this file may alternatively be used under the terms
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * of the Common Development and Distribution License Version 1.0
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * VirtualBox OSE distribution, in which case the provisions of the
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * CDDL are applicable instead of those of the GPL.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * You may elect to license modified versions of this file under the
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * terms and conditions of either the GPL or the CDDL or both.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync/*******************************************************************************
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync* Header Files *
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync*******************************************************************************/
3ab1cd69595235a9f15ce27945574123b0f38b46vboxsync/*******************************************************************************
3ab1cd69595235a9f15ce27945574123b0f38b46vboxsync* Structures and Typedefs *
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync*******************************************************************************/
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync /** Magic value (RTPIPE_MAGIC). */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync /** The file descriptor. */
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync /** Set if this is the read end, clear if it's the write end. */
1c0d3d017f9a45748b4839bf6622b53e83a4f1f8vboxsync /** Atomically operated state variable.
1c0d3d017f9a45748b4839bf6622b53e83a4f1f8vboxsync * - Bits 0 thru 29 - Users of the new mode.
1c0d3d017f9a45748b4839bf6622b53e83a4f1f8vboxsync * - Bit 30 - The pipe mode, set indicates blocking.
1c0d3d017f9a45748b4839bf6622b53e83a4f1f8vboxsync * - Bit 31 - Set when we're switching the mode.
1c0d3d017f9a45748b4839bf6622b53e83a4f1f8vboxsync/*******************************************************************************
1c0d3d017f9a45748b4839bf6622b53e83a4f1f8vboxsync* Defined Constants And Macros *
1c0d3d017f9a45748b4839bf6622b53e83a4f1f8vboxsync*******************************************************************************/
1c0d3d017f9a45748b4839bf6622b53e83a4f1f8vboxsync/** @name RTPIPEINTERNAL::u32State defines
4a9af9a8062589b741444d717d2dd1ed22b0f583vboxsync#define RTPIPE_POSIX_USERS_MASK UINT32_C(0x3fffffff)
4a9af9a8062589b741444d717d2dd1ed22b0f583vboxsyncRTDECL(int) RTPipeCreate(PRTPIPE phPipeRead, PRTPIPE phPipeWrite, uint32_t fFlags)
4a9af9a8062589b741444d717d2dd1ed22b0f583vboxsync AssertPtrReturn(phPipeWrite, VERR_INVALID_POINTER);
4a9af9a8062589b741444d717d2dd1ed22b0f583vboxsync AssertReturn(!(fFlags & ~RTPIPE_C_VALID_MASK), VERR_INVALID_PARAMETER);
4a9af9a8062589b741444d717d2dd1ed22b0f583vboxsync * Create the pipe and set the close-on-exec flag if requested.
4a9af9a8062589b741444d717d2dd1ed22b0f583vboxsync * Create the two handles.
4a9af9a8062589b741444d717d2dd1ed22b0f583vboxsync RTPIPEINTERNAL *pThisR = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
4a9af9a8062589b741444d717d2dd1ed22b0f583vboxsync RTPIPEINTERNAL *pThisW = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
1c0d3d017f9a45748b4839bf6622b53e83a4f1f8vboxsync * Before we leave, make sure to shut up SIGPIPE.
1c0d3d017f9a45748b4839bf6622b53e83a4f1f8vboxsync AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
1c0d3d017f9a45748b4839bf6622b53e83a4f1f8vboxsync * Do the cleanup.
3a343ca21a267ec3c54e2317e2ed18fe99b8ebbbvboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTPIPE_MAGIC, RTPIPE_MAGIC), VERR_INVALID_HANDLE);
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync if (ASMAtomicReadU32(&pThis->u32State) & RTPIPE_POSIX_USERS_MASK)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsyncRTDECL(int) RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK), VERR_INVALID_PARAMETER);
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync AssertReturn(!!(fFlags & RTPIPE_N_READ) != !!(fFlags & RTPIPE_N_WRITE), VERR_INVALID_PARAMETER);
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Get and validate the pipe handle info.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync AssertReturn(fstat(hNative, &st) == 0, RTErrConvertFromErrno(errno));
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync AssertMsgReturn(S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode), ("%#x (%o)\n", st.st_mode, st.st_mode), VERR_INVALID_HANDLE);
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync AssertMsgReturn( (fFd & O_ACCMODE) == (fFlags & RTPIPE_N_READ ? O_RDONLY : O_WRONLY)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync || (fFd & O_ACCMODE) == O_RDWR /* Solaris creates bi-directional pipes. */
73ba84f95f918cc170be38908ad240fbb2f8f354vboxsync * Create the handle.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync RTPIPEINTERNAL *pThis = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync pThis->u32State = fFd & O_NONBLOCK ? 0 : RTPIPE_POSIX_BLOCKING;
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Fix up inheritability and shut up SIGPIPE and we're done.
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync if (fcntl(hNative, F_SETFD, fFlags & RTPIPE_N_INHERIT ? 0 : FD_CLOEXEC) == 0)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * Prepare blocking mode.
73ba84f95f918cc170be38908ad240fbb2f8f354vboxsync * @returns VINF_SUCCESS
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync * @retval VERR_WRONG_ORDER
73ba84f95f918cc170be38908ad240fbb2f8f354vboxsync * @retval VERR_INTERNAL_ERROR_4
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync * @param pThis The pipe handle.
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync * Update the state.
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync uint32_t u32State = ASMAtomicReadU32(&pThis->u32State);
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync uint32_t const cUsers = (u32State & RTPIPE_POSIX_USERS_MASK);
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync AssertReturn(cUsers < RTPIPE_POSIX_USERS_MASK / 2, VERR_INTERNAL_ERROR_4);
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync else if (cUsers == 0)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync u32State = 1 | RTPIPE_POSIX_SWITCHING | RTPIPE_POSIX_BLOCKING;
359416647a711739d1b14addbf399178949a1a60vboxsync if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync * Do the switching.
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync || fcntl(pThis->fd, F_SETFL, fFlags & ~O_NONBLOCK) != -1)
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync ASMAtomicBitClear(&pThis->u32State, RTPIPE_POSIX_SWITCHING_BIT);
359416647a711739d1b14addbf399178949a1a60vboxsync * Prepare non-blocking mode.
359416647a711739d1b14addbf399178949a1a60vboxsync * @returns VINF_SUCCESS
359416647a711739d1b14addbf399178949a1a60vboxsync * @retval VERR_WRONG_ORDER
bdbed0b8e7fb553d01417fdc976a76f3b287dbe2vboxsync * @retval VERR_INTERNAL_ERROR_4
6902a98267d5180fb081cb5273751d0a628bf04dvboxsync * @param pThis The pipe handle.
1389294d44ac76b0a25f5655756c9d39855a73efvboxsyncstatic int rtPipeTryNonBlocking(RTPIPEINTERNAL *pThis)
1389294d44ac76b0a25f5655756c9d39855a73efvboxsync * Update the state.
73ba84f95f918cc170be38908ad240fbb2f8f354vboxsync uint32_t u32State = ASMAtomicReadU32(&pThis->u32State);
73ba84f95f918cc170be38908ad240fbb2f8f354vboxsync uint32_t const cUsers = (u32State & RTPIPE_POSIX_USERS_MASK);
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync AssertReturn(cUsers < RTPIPE_POSIX_USERS_MASK / 2, VERR_INTERNAL_ERROR_4);
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync else if (cUsers == 0)
9055f61bb57d2a625c6434d55beac7565c3b3c0dvboxsync if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
359416647a711739d1b14addbf399178949a1a60vboxsync * Do the switching.
359416647a711739d1b14addbf399178949a1a60vboxsync || fcntl(pThis->fd, F_SETFL, fFlags | O_NONBLOCK) != -1)
359416647a711739d1b14addbf399178949a1a60vboxsync ASMAtomicBitClear(&pThis->u32State, RTPIPE_POSIX_SWITCHING_BIT);
359416647a711739d1b14addbf399178949a1a60vboxsync * Checks if the read pipe has a HUP condition.
359416647a711739d1b14addbf399178949a1a60vboxsync * @returns true if HUP, false if no.
359416647a711739d1b14addbf399178949a1a60vboxsync * @param pThis The pipe handle (read).
359416647a711739d1b14addbf399178949a1a60vboxsyncstatic bool rtPipePosixHasHup(RTPIPEINTERNAL *pThis)
359416647a711739d1b14addbf399178949a1a60vboxsyncRTDECL(int) RTPipeRead(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead)
359416647a711739d1b14addbf399178949a1a60vboxsync AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
359416647a711739d1b14addbf399178949a1a60vboxsync ssize_t cbRead = read(pThis->fd, pvBuf, RT_MIN(cbToRead, SSIZE_MAX));
359416647a711739d1b14addbf399178949a1a60vboxsync if (cbRead || !cbToRead || !rtPipePosixHasHup(pThis))
359416647a711739d1b14addbf399178949a1a60vboxsyncRTDECL(int) RTPipeReadBlocking(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead)
359416647a711739d1b14addbf399178949a1a60vboxsync AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
ad290511521ce8388a9926b165241ecf83c330a7vboxsync ssize_t cbRead = read(pThis->fd, pvBuf, RT_MIN(cbToRead, SSIZE_MAX));
329df9696e709dc71611f504a4774f323545be0avboxsync /* advance */
4ecd4ad59281328476ad14f2baa51716b6f5f804vboxsyncRTDECL(int) RTPipeWrite(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
359416647a711739d1b14addbf399178949a1a60vboxsync AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
359416647a711739d1b14addbf399178949a1a60vboxsync ssize_t cbWritten = write(pThis->fd, pvBuf, RT_MIN(cbToWrite, SSIZE_MAX));
359416647a711739d1b14addbf399178949a1a60vboxsyncRTDECL(int) RTPipeWriteBlocking(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
359416647a711739d1b14addbf399178949a1a60vboxsync AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
359416647a711739d1b14addbf399178949a1a60vboxsync ssize_t cbWritten = write(pThis->fd, pvBuf, RT_MIN(cbToWrite, SSIZE_MAX));
359416647a711739d1b14addbf399178949a1a60vboxsync /* advance */
359416647a711739d1b14addbf399178949a1a60vboxsync AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
359416647a711739d1b14addbf399178949a1a60vboxsyncRTDECL(int) RTPipeSelectOne(RTPIPE hPipe, RTMSINTERVAL cMillies)