pipe-posix.cpp revision c7814cf6e1240a519cbec0441e033d0e2470ed00
6739cf90e4aa3b3344768b8da241802f80ff455cvboxsync/* $Id$ */
72e560433c691132e8f21f463955515d4511c34bvboxsync/** @file
72e560433c691132e8f21f463955515d4511c34bvboxsync * IPRT - Anonymous Pipes, POSIX Implementation.
72e560433c691132e8f21f463955515d4511c34bvboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync/*
72e560433c691132e8f21f463955515d4511c34bvboxsync * Copyright (C) 2010-2013 Oracle Corporation
438870735e3606c923d51fce19d9fc3b6ebffde1vboxsync *
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 *
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 *
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
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync/*******************************************************************************
72e560433c691132e8f21f463955515d4511c34bvboxsync* Header Files *
72e560433c691132e8f21f463955515d4511c34bvboxsync*******************************************************************************/
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/pipe.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include "internal/iprt.h"
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/asm.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/assert.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/err.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/mem.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/poll.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/string.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/thread.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include "internal/magics.h"
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <errno.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <fcntl.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <limits.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <unistd.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <sys/ioctl.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <sys/poll.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <sys/stat.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <signal.h>
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync#ifdef RT_OS_LINUX
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync# include <sys/syscall.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#endif
72e560433c691132e8f21f463955515d4511c34bvboxsync#ifdef RT_OS_SOLARIS
72e560433c691132e8f21f463955515d4511c34bvboxsync# include <sys/filio.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#endif
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync#include "internal/pipe.h"
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync/*******************************************************************************
72e560433c691132e8f21f463955515d4511c34bvboxsync* Structures and Typedefs *
72e560433c691132e8f21f463955515d4511c34bvboxsync*******************************************************************************/
72e560433c691132e8f21f463955515d4511c34bvboxsynctypedef struct RTPIPEINTERNAL
72e560433c691132e8f21f463955515d4511c34bvboxsync{
72e560433c691132e8f21f463955515d4511c34bvboxsync /** Magic value (RTPIPE_MAGIC). */
72e560433c691132e8f21f463955515d4511c34bvboxsync uint32_t u32Magic;
72e560433c691132e8f21f463955515d4511c34bvboxsync /** The file descriptor. */
72e560433c691132e8f21f463955515d4511c34bvboxsync int fd;
72e560433c691132e8f21f463955515d4511c34bvboxsync /** Set if this is the read end, clear if it's the write end. */
72e560433c691132e8f21f463955515d4511c34bvboxsync bool fRead;
72e560433c691132e8f21f463955515d4511c34bvboxsync /** Atomically operated state variable.
72e560433c691132e8f21f463955515d4511c34bvboxsync *
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.
72e560433c691132e8f21f463955515d4511c34bvboxsync */
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync uint32_t volatile u32State;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync} RTPIPEINTERNAL;
72e560433c691132e8f21f463955515d4511c34bvboxsync
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync/*******************************************************************************
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync* Defined Constants And Macros *
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync*******************************************************************************/
72e560433c691132e8f21f463955515d4511c34bvboxsync/** @name RTPIPEINTERNAL::u32State defines
72e560433c691132e8f21f463955515d4511c34bvboxsync * @{ */
72e560433c691132e8f21f463955515d4511c34bvboxsync#define RTPIPE_POSIX_BLOCKING UINT32_C(0x40000000)
72e560433c691132e8f21f463955515d4511c34bvboxsync#define RTPIPE_POSIX_SWITCHING UINT32_C(0x80000000)
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync#define RTPIPE_POSIX_SWITCHING_BIT 31
72e560433c691132e8f21f463955515d4511c34bvboxsync#define RTPIPE_POSIX_USERS_MASK UINT32_C(0x3fffffff)
72e560433c691132e8f21f463955515d4511c34bvboxsync/** @} */
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync
1207f59aa62006952dbb0bf7700decf34d8caeb2vboxsync/**
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * Wrapper for calling pipe2() or pipe().
72e560433c691132e8f21f463955515d4511c34bvboxsync *
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * When using pipe2() the returned handles are marked close-on-exec and does
72e560433c691132e8f21f463955515d4511c34bvboxsync * not risk racing process creation calls on other threads.
72e560433c691132e8f21f463955515d4511c34bvboxsync *
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.
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsyncstatic int my_pipe_wrapper(int *paFds, int *piNewPipeSyscall)
72e560433c691132e8f21f463955515d4511c34bvboxsync{
72e560433c691132e8f21f463955515d4511c34bvboxsync if (*piNewPipeSyscall >= 0)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync#if defined(RT_OS_LINUX) && defined(__NR_pipe2) && defined(O_CLOEXEC)
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync long rc = syscall(__NR_pipe2, paFds, O_CLOEXEC);
72e560433c691132e8f21f463955515d4511c34bvboxsync if (rc >= 0)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync if (*piNewPipeSyscall == 0)
72e560433c691132e8f21f463955515d4511c34bvboxsync *piNewPipeSyscall = 1;
72e560433c691132e8f21f463955515d4511c34bvboxsync return (int)rc;
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync#endif
72e560433c691132e8f21f463955515d4511c34bvboxsync *piNewPipeSyscall = -1;
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync return pipe(paFds);
72e560433c691132e8f21f463955515d4511c34bvboxsync}
72e560433c691132e8f21f463955515d4511c34bvboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsyncRTDECL(int) RTPipeCreate(PRTPIPE phPipeRead, PRTPIPE phPipeWrite, uint32_t fFlags)
72e560433c691132e8f21f463955515d4511c34bvboxsync{
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync AssertPtrReturn(phPipeRead, VERR_INVALID_POINTER);
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync AssertPtrReturn(phPipeWrite, VERR_INVALID_POINTER);
72e560433c691132e8f21f463955515d4511c34bvboxsync AssertReturn(!(fFlags & ~RTPIPE_C_VALID_MASK), VERR_INVALID_PARAMETER);
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync /*
72e560433c691132e8f21f463955515d4511c34bvboxsync * Create the pipe and clear/set the close-on-exec flag as required.
72e560433c691132e8f21f463955515d4511c34bvboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsync int aFds[2] = {-1, -1};
72e560433c691132e8f21f463955515d4511c34bvboxsync static int s_iNewPipeSyscall = 0;
72e560433c691132e8f21f463955515d4511c34bvboxsync if (my_pipe_wrapper(aFds, &s_iNewPipeSyscall))
72e560433c691132e8f21f463955515d4511c34bvboxsync return RTErrConvertFromErrno(errno);
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync int rc = VINF_SUCCESS;
72e560433c691132e8f21f463955515d4511c34bvboxsync if (s_iNewPipeSyscall > 0)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync /* created with close-on-exec set. */
72e560433c691132e8f21f463955515d4511c34bvboxsync if (fFlags & RTPIPE_C_INHERIT_READ)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync if (fcntl(aFds[0], F_SETFD, 0))
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = RTErrConvertFromErrno(errno);
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync if (fFlags & RTPIPE_C_INHERIT_WRITE)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync if (fcntl(aFds[1], F_SETFD, 0))
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = RTErrConvertFromErrno(errno);
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync else
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync /* created with close-on-exec cleared. */
72e560433c691132e8f21f463955515d4511c34bvboxsync if (!(fFlags & RTPIPE_C_INHERIT_READ))
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync if (fcntl(aFds[0], F_SETFD, FD_CLOEXEC))
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = RTErrConvertFromErrno(errno);
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync if (!(fFlags & RTPIPE_C_INHERIT_WRITE))
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync if (fcntl(aFds[1], F_SETFD, FD_CLOEXEC))
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = RTErrConvertFromErrno(errno);
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync if (RT_SUCCESS(rc))
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync /*
72e560433c691132e8f21f463955515d4511c34bvboxsync * Create the two handles.
72e560433c691132e8f21f463955515d4511c34bvboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsync RTPIPEINTERNAL *pThisR = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
72e560433c691132e8f21f463955515d4511c34bvboxsync if (pThisR)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync RTPIPEINTERNAL *pThisW = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
72e560433c691132e8f21f463955515d4511c34bvboxsync if (pThisW)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync pThisR->u32Magic = RTPIPE_MAGIC;
72e560433c691132e8f21f463955515d4511c34bvboxsync pThisW->u32Magic = RTPIPE_MAGIC;
72e560433c691132e8f21f463955515d4511c34bvboxsync pThisR->fd = aFds[0];
72e560433c691132e8f21f463955515d4511c34bvboxsync pThisW->fd = aFds[1];
72e560433c691132e8f21f463955515d4511c34bvboxsync pThisR->fRead = true;
72e560433c691132e8f21f463955515d4511c34bvboxsync pThisW->fRead = false;
72e560433c691132e8f21f463955515d4511c34bvboxsync pThisR->u32State = RTPIPE_POSIX_BLOCKING;
72e560433c691132e8f21f463955515d4511c34bvboxsync pThisW->u32State = RTPIPE_POSIX_BLOCKING;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync *phPipeRead = pThisR;
72e560433c691132e8f21f463955515d4511c34bvboxsync *phPipeWrite = pThisW;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync /*
72e560433c691132e8f21f463955515d4511c34bvboxsync * Before we leave, make sure to shut up SIGPIPE.
72e560433c691132e8f21f463955515d4511c34bvboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsync signal(SIGPIPE, SIG_IGN);
72e560433c691132e8f21f463955515d4511c34bvboxsync return VINF_SUCCESS;
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync RTMemFree(pThisR);
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = VERR_NO_MEMORY;
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync else
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = VERR_NO_MEMORY;
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync close(aFds[0]);
72e560433c691132e8f21f463955515d4511c34bvboxsync close(aFds[1]);
72e560433c691132e8f21f463955515d4511c34bvboxsync return rc;
72e560433c691132e8f21f463955515d4511c34bvboxsync}
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsyncRTDECL(int) RTPipeClose(RTPIPE hPipe)
72e560433c691132e8f21f463955515d4511c34bvboxsync{
72e560433c691132e8f21f463955515d4511c34bvboxsync RTPIPEINTERNAL *pThis = hPipe;
72e560433c691132e8f21f463955515d4511c34bvboxsync if (pThis == NIL_RTPIPE)
72e560433c691132e8f21f463955515d4511c34bvboxsync return VINF_SUCCESS;
72e560433c691132e8f21f463955515d4511c34bvboxsync AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync /*
72e560433c691132e8f21f463955515d4511c34bvboxsync * Do the cleanup.
72e560433c691132e8f21f463955515d4511c34bvboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTPIPE_MAGIC, RTPIPE_MAGIC), VERR_INVALID_HANDLE);
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync int fd = pThis->fd;
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync pThis->fd = -1;
72e560433c691132e8f21f463955515d4511c34bvboxsync close(fd);
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync if (ASMAtomicReadU32(&pThis->u32State) & RTPIPE_POSIX_USERS_MASK)
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync {
8ae161fca703d669e1306fafee128914f831f72bvboxsync AssertFailed();
8ae161fca703d669e1306fafee128914f831f72bvboxsync RTThreadSleep(1);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync RTMemFree(pThis);
72e560433c691132e8f21f463955515d4511c34bvboxsync
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync return VINF_SUCCESS;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync}
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsyncRTDECL(int) RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync{
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync AssertPtrReturn(phPipe, VERR_INVALID_POINTER);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK), VERR_INVALID_PARAMETER);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync AssertReturn(!!(fFlags & RTPIPE_N_READ) != !!(fFlags & RTPIPE_N_WRITE), VERR_INVALID_PARAMETER);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync /*
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * Get and validate the pipe handle info.
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync */
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync int hNative = (int)hNativePipe;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync struct stat st;
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
72e560433c691132e8f21f463955515d4511c34bvboxsync int fFd = fcntl(hNative, F_GETFL, 0);
72e560433c691132e8f21f463955515d4511c34bvboxsync AssertReturn(fFd != -1, 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. */
72e560433c691132e8f21f463955515d4511c34bvboxsync , ("%#x\n", fFd), VERR_INVALID_HANDLE);
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync /*
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync * Create the handle.
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync */
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync RTPIPEINTERNAL *pThis = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
72e560433c691132e8f21f463955515d4511c34bvboxsync if (!pThis)
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync return VERR_NO_MEMORY;
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync pThis->u32Magic = RTPIPE_MAGIC;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync pThis->fd = hNative;
72e560433c691132e8f21f463955515d4511c34bvboxsync pThis->fRead = !!(fFlags & RTPIPE_N_READ);
72e560433c691132e8f21f463955515d4511c34bvboxsync pThis->u32State = fFd & O_NONBLOCK ? 0 : RTPIPE_POSIX_BLOCKING;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync /*
72e560433c691132e8f21f463955515d4511c34bvboxsync * Fix up inheritability and shut up SIGPIPE and we're done.
72e560433c691132e8f21f463955515d4511c34bvboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsync if (fcntl(hNative, F_SETFD, fFlags & RTPIPE_N_INHERIT ? 0 : FD_CLOEXEC) == 0)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync signal(SIGPIPE, SIG_IGN);
72e560433c691132e8f21f463955515d4511c34bvboxsync *phPipe = pThis;
72e560433c691132e8f21f463955515d4511c34bvboxsync return VINF_SUCCESS;
230bd8589bba39933ac5ec21482d6186d675e604vboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync int rc = RTErrConvertFromErrno(errno);
72e560433c691132e8f21f463955515d4511c34bvboxsync RTMemFree(pThis);
72e560433c691132e8f21f463955515d4511c34bvboxsync return rc;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync}
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsyncRTDECL(RTHCINTPTR) RTPipeToNative(RTPIPE hPipe)
72e560433c691132e8f21f463955515d4511c34bvboxsync{
72e560433c691132e8f21f463955515d4511c34bvboxsync RTPIPEINTERNAL *pThis = hPipe;
72e560433c691132e8f21f463955515d4511c34bvboxsync AssertPtrReturn(pThis, -1);
72e560433c691132e8f21f463955515d4511c34bvboxsync AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, -1);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync return pThis->fd;
72e560433c691132e8f21f463955515d4511c34bvboxsync}
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync/**
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * Prepare blocking mode.
72e560433c691132e8f21f463955515d4511c34bvboxsync *
72e560433c691132e8f21f463955515d4511c34bvboxsync * @returns VINF_SUCCESS
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * @retval VERR_WRONG_ORDER
72e560433c691132e8f21f463955515d4511c34bvboxsync * @retval VERR_INTERNAL_ERROR_4
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync *
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync * @param pThis The pipe handle.
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync */
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsyncstatic int rtPipeTryBlocking(RTPIPEINTERNAL *pThis)
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync{
72e560433c691132e8f21f463955515d4511c34bvboxsync /*
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync * Update the state.
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync */
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync for (;;)
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync {
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync uint32_t u32State = ASMAtomicReadU32(&pThis->u32State);
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync uint32_t const u32StateOld = u32State;
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync uint32_t const cUsers = (u32State & RTPIPE_POSIX_USERS_MASK);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync if (u32State & RTPIPE_POSIX_BLOCKING)
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync {
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync AssertReturn(cUsers < RTPIPE_POSIX_USERS_MASK / 2, VERR_INTERNAL_ERROR_4);
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync u32State &= ~RTPIPE_POSIX_USERS_MASK;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync u32State |= cUsers + 1;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync {
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (u32State & RTPIPE_POSIX_SWITCHING)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync break;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync return VINF_SUCCESS;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync }
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync else if (cUsers == 0)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync {
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync u32State = 1 | RTPIPE_POSIX_SWITCHING | RTPIPE_POSIX_BLOCKING;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync break;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync }
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync else
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync return VERR_WRONG_ORDER;
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync ASMNopPause();
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync }
508452243fd3328f7b9e0405d39fb9dc004e31b8vboxsync
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync /*
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync * Do the switching.
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync */
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync int fFlags = fcntl(pThis->fd, F_GETFL, 0);
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync if (fFlags != -1)
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync {
2d7e2a75f9939e8bfd6f19cfb507bb57d86b3be1vboxsync if ( !(fFlags & O_NONBLOCK)
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync || fcntl(pThis->fd, F_SETFL, fFlags & ~O_NONBLOCK) != -1)
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync {
48b14aaf804d764e2c7bec8243c379f7ff489ebbvboxsync ASMAtomicBitClear(&pThis->u32State, RTPIPE_POSIX_SWITCHING_BIT);
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync return VINF_SUCCESS;
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync }
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync }
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync ASMAtomicDecU32(&pThis->u32State);
72e560433c691132e8f21f463955515d4511c34bvboxsync return RTErrConvertFromErrno(errno);
72e560433c691132e8f21f463955515d4511c34bvboxsync}
72e560433c691132e8f21f463955515d4511c34bvboxsync
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync/**
72e560433c691132e8f21f463955515d4511c34bvboxsync * Prepare non-blocking mode.
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync *
72e560433c691132e8f21f463955515d4511c34bvboxsync * @returns VINF_SUCCESS
72e560433c691132e8f21f463955515d4511c34bvboxsync * @retval VERR_WRONG_ORDER
72e560433c691132e8f21f463955515d4511c34bvboxsync * @retval VERR_INTERNAL_ERROR_4
72e560433c691132e8f21f463955515d4511c34bvboxsync *
72e560433c691132e8f21f463955515d4511c34bvboxsync * @param pThis The pipe handle.
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsyncstatic int rtPipeTryNonBlocking(RTPIPEINTERNAL *pThis)
72e560433c691132e8f21f463955515d4511c34bvboxsync{
72e560433c691132e8f21f463955515d4511c34bvboxsync /*
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * Update the state.
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsync for (;;)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync uint32_t u32State = ASMAtomicReadU32(&pThis->u32State);
72e560433c691132e8f21f463955515d4511c34bvboxsync uint32_t const u32StateOld = u32State;
72e560433c691132e8f21f463955515d4511c34bvboxsync uint32_t const cUsers = (u32State & RTPIPE_POSIX_USERS_MASK);
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync if (!(u32State & RTPIPE_POSIX_BLOCKING))
{
AssertReturn(cUsers < RTPIPE_POSIX_USERS_MASK / 2, VERR_INTERNAL_ERROR_4);
u32State &= ~RTPIPE_POSIX_USERS_MASK;
u32State |= cUsers + 1;
if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
{
if (u32State & RTPIPE_POSIX_SWITCHING)
break;
return VINF_SUCCESS;
}
}
else if (cUsers == 0)
{
u32State = 1 | RTPIPE_POSIX_SWITCHING;
if (ASMAtomicCmpXchgU32(&pThis->u32State, u32State, u32StateOld))
break;
}
else
return VERR_WRONG_ORDER;
ASMNopPause();
}
/*
* Do the switching.
*/
int fFlags = fcntl(pThis->fd, F_GETFL, 0);
if (fFlags != -1)
{
if ( (fFlags & O_NONBLOCK)
|| fcntl(pThis->fd, F_SETFL, fFlags | O_NONBLOCK) != -1)
{
ASMAtomicBitClear(&pThis->u32State, RTPIPE_POSIX_SWITCHING_BIT);
return VINF_SUCCESS;
}
}
ASMAtomicDecU32(&pThis->u32State);
return RTErrConvertFromErrno(errno);
}
/**
* Checks if the read pipe has a HUP condition.
*
* @returns true if HUP, false if no.
* @param pThis The pipe handle (read).
*/
static bool rtPipePosixHasHup(RTPIPEINTERNAL *pThis)
{
Assert(pThis->fRead);
struct pollfd PollFd;
RT_ZERO(PollFd);
PollFd.fd = pThis->fd;
PollFd.events = POLLHUP;
return poll(&PollFd, 1, 0) >= 1
&& (PollFd.revents & POLLHUP);
}
RTDECL(int) RTPipeRead(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(pThis->fRead, VERR_ACCESS_DENIED);
AssertPtr(pcbRead);
AssertPtr(pvBuf);
int rc = rtPipeTryNonBlocking(pThis);
if (RT_SUCCESS(rc))
{
ssize_t cbRead = read(pThis->fd, pvBuf, RT_MIN(cbToRead, SSIZE_MAX));
if (cbRead >= 0)
{
if (cbRead || !cbToRead || !rtPipePosixHasHup(pThis))
*pcbRead = cbRead;
else
rc = VERR_BROKEN_PIPE;
}
else if (errno == EAGAIN)
{
*pcbRead = 0;
rc = VINF_TRY_AGAIN;
}
else
rc = RTErrConvertFromErrno(errno);
ASMAtomicDecU32(&pThis->u32State);
}
return rc;
}
RTDECL(int) RTPipeReadBlocking(RTPIPE hPipe, void *pvBuf, size_t cbToRead, size_t *pcbRead)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(pThis->fRead, VERR_ACCESS_DENIED);
AssertPtr(pvBuf);
int rc = rtPipeTryBlocking(pThis);
if (RT_SUCCESS(rc))
{
size_t cbTotalRead = 0;
while (cbToRead > 0)
{
ssize_t cbRead = read(pThis->fd, pvBuf, RT_MIN(cbToRead, SSIZE_MAX));
if (cbRead < 0)
{
rc = RTErrConvertFromErrno(errno);
break;
}
if (!cbRead && rtPipePosixHasHup(pThis))
{
rc = VERR_BROKEN_PIPE;
break;
}
/* advance */
pvBuf = (char *)pvBuf + cbRead;
cbTotalRead += cbRead;
cbToRead -= cbRead;
}
if (pcbRead)
{
*pcbRead = cbTotalRead;
if ( RT_FAILURE(rc)
&& cbTotalRead
&& rc != VERR_INVALID_POINTER)
rc = VINF_SUCCESS;
}
ASMAtomicDecU32(&pThis->u32State);
}
return rc;
}
RTDECL(int) RTPipeWrite(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(!pThis->fRead, VERR_ACCESS_DENIED);
AssertPtr(pcbWritten);
AssertPtr(pvBuf);
int rc = rtPipeTryNonBlocking(pThis);
if (RT_SUCCESS(rc))
{
if (cbToWrite)
{
ssize_t cbWritten = write(pThis->fd, pvBuf, RT_MIN(cbToWrite, SSIZE_MAX));
if (cbWritten >= 0)
*pcbWritten = cbWritten;
else if (errno == EAGAIN)
{
*pcbWritten = 0;
rc = VINF_TRY_AGAIN;
}
else
rc = RTErrConvertFromErrno(errno);
}
else
*pcbWritten = 0;
ASMAtomicDecU32(&pThis->u32State);
}
return rc;
}
RTDECL(int) RTPipeWriteBlocking(RTPIPE hPipe, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(!pThis->fRead, VERR_ACCESS_DENIED);
AssertPtr(pvBuf);
AssertPtrNull(pcbWritten);
int rc = rtPipeTryBlocking(pThis);
if (RT_SUCCESS(rc))
{
size_t cbTotalWritten = 0;
while (cbToWrite > 0)
{
ssize_t cbWritten = write(pThis->fd, pvBuf, RT_MIN(cbToWrite, SSIZE_MAX));
if (cbWritten < 0)
{
rc = RTErrConvertFromErrno(errno);
break;
}
/* advance */
pvBuf = (char const *)pvBuf + cbWritten;
cbTotalWritten += cbWritten;
cbToWrite -= cbWritten;
}
if (pcbWritten)
{
*pcbWritten = cbTotalWritten;
if ( RT_FAILURE(rc)
&& cbTotalWritten
&& rc != VERR_INVALID_POINTER)
rc = VINF_SUCCESS;
}
ASMAtomicDecU32(&pThis->u32State);
}
return rc;
}
RTDECL(int) RTPipeFlush(RTPIPE hPipe)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(!pThis->fRead, VERR_ACCESS_DENIED);
if (fsync(pThis->fd))
{
if (errno == EINVAL || errno == ENOTSUP)
return VERR_NOT_SUPPORTED;
return RTErrConvertFromErrno(errno);
}
return VINF_SUCCESS;
}
RTDECL(int) RTPipeSelectOne(RTPIPE hPipe, RTMSINTERVAL cMillies)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
struct pollfd PollFd;
RT_ZERO(PollFd);
PollFd.fd = pThis->fd;
PollFd.events = POLLHUP | POLLERR;
if (pThis->fRead)
PollFd.events |= POLLIN | POLLPRI;
else
PollFd.events |= POLLOUT;
int timeout;
if ( cMillies == RT_INDEFINITE_WAIT
|| cMillies >= INT_MAX /* lazy bird */)
timeout = -1;
else
timeout = cMillies;
int rc = poll(&PollFd, 1, timeout);
if (rc == -1)
return RTErrConvertFromErrno(errno);
return rc > 0 ? VINF_SUCCESS : VERR_TIMEOUT;
}
RTDECL(int) RTPipeQueryReadable(RTPIPE hPipe, size_t *pcbReadable)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(pThis->fRead, VERR_PIPE_NOT_READ);
AssertPtrReturn(pcbReadable, VERR_INVALID_POINTER);
int cb = 0;
int rc = ioctl(pThis->fd, FIONREAD, &cb);
if (rc != -1)
{
AssertStmt(cb >= 0, cb = 0);
*pcbReadable = cb;
return VINF_SUCCESS;
}
rc = errno;
if (rc == ENOTTY)
rc = VERR_NOT_SUPPORTED;
else
rc = RTErrConvertFromErrno(rc);
return rc;
}
int rtPipePollGetHandle(RTPIPE hPipe, uint32_t fEvents, PRTHCINTPTR phNative)
{
RTPIPEINTERNAL *pThis = hPipe;
AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(!(fEvents & RTPOLL_EVT_READ) || pThis->fRead, VERR_INVALID_PARAMETER);
AssertReturn(!(fEvents & RTPOLL_EVT_WRITE) || !pThis->fRead, VERR_INVALID_PARAMETER);
*phNative = pThis->fd;
return VINF_SUCCESS;
}