pipe-posix.cpp revision 34d9e1cb32bd0b95b641d278348417a6518d26d2
65fea56f17cd614bc8908264df980a62e1931468vboxsync/* $Id$ */
65fea56f17cd614bc8908264df980a62e1931468vboxsync/** @file
65fea56f17cd614bc8908264df980a62e1931468vboxsync * IPRT - Anonymous Pipes, POSIX Implementation.
65fea56f17cd614bc8908264df980a62e1931468vboxsync */
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync/*
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Copyright (C) 2010 Oracle Corporation
65fea56f17cd614bc8908264df980a62e1931468vboxsync *
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 *
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 *
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
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync/*******************************************************************************
65fea56f17cd614bc8908264df980a62e1931468vboxsync* Header Files *
65fea56f17cd614bc8908264df980a62e1931468vboxsync*******************************************************************************/
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <iprt/pipe.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include "internal/iprt.h"
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <iprt/asm.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <iprt/assert.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <iprt/err.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <iprt/mem.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <iprt/string.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <iprt/thread.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include "internal/magics.h"
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <errno.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <fcntl.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <limits.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <unistd.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <sys/poll.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <sys/stat.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#include <signal.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#ifdef RT_OS_LINUX
65fea56f17cd614bc8908264df980a62e1931468vboxsync# include <sys/syscall.h>
65fea56f17cd614bc8908264df980a62e1931468vboxsync#endif
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync/*******************************************************************************
65fea56f17cd614bc8908264df980a62e1931468vboxsync* Structures and Typedefs *
65fea56f17cd614bc8908264df980a62e1931468vboxsync*******************************************************************************/
65fea56f17cd614bc8908264df980a62e1931468vboxsynctypedef struct RTPIPEINTERNAL
65fea56f17cd614bc8908264df980a62e1931468vboxsync{
65fea56f17cd614bc8908264df980a62e1931468vboxsync /** Magic value (RTPIPE_MAGIC). */
65fea56f17cd614bc8908264df980a62e1931468vboxsync uint32_t u32Magic;
65fea56f17cd614bc8908264df980a62e1931468vboxsync /** The file descriptor. */
65fea56f17cd614bc8908264df980a62e1931468vboxsync int fd;
65fea56f17cd614bc8908264df980a62e1931468vboxsync /** Set if this is the read end, clear if it's the write end. */
65fea56f17cd614bc8908264df980a62e1931468vboxsync bool fRead;
65fea56f17cd614bc8908264df980a62e1931468vboxsync /** Atomically operated state variable.
65fea56f17cd614bc8908264df980a62e1931468vboxsync *
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 uint32_t volatile u32State;
65fea56f17cd614bc8908264df980a62e1931468vboxsync} RTPIPEINTERNAL;
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync/*******************************************************************************
65fea56f17cd614bc8908264df980a62e1931468vboxsync* Defined Constants And Macros *
65fea56f17cd614bc8908264df980a62e1931468vboxsync*******************************************************************************/
65fea56f17cd614bc8908264df980a62e1931468vboxsync/** @name RTPIPEINTERNAL::u32State defines
65fea56f17cd614bc8908264df980a62e1931468vboxsync * @{ */
65fea56f17cd614bc8908264df980a62e1931468vboxsync#define RTPIPE_POSIX_BLOCKING UINT32_C(0x40000000)
65fea56f17cd614bc8908264df980a62e1931468vboxsync#define RTPIPE_POSIX_SWITCHING UINT32_C(0x80000000)
65fea56f17cd614bc8908264df980a62e1931468vboxsync#define RTPIPE_POSIX_SWITCHING_BIT 31
65fea56f17cd614bc8908264df980a62e1931468vboxsync#define RTPIPE_POSIX_USERS_MASK UINT32_C(0x3fffffff)
65fea56f17cd614bc8908264df980a62e1931468vboxsync/** @} */
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync/**
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Wrapper for calling pipe2() or pipe().
65fea56f17cd614bc8908264df980a62e1931468vboxsync *
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 *
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.
65fea56f17cd614bc8908264df980a62e1931468vboxsync */
65fea56f17cd614bc8908264df980a62e1931468vboxsyncstatic int my_pipe_wrapper(int *paFds, int *piNewPipeSyscall)
65fea56f17cd614bc8908264df980a62e1931468vboxsync{
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (*piNewPipeSyscall >= 0)
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync#if defined(RT_OS_LINUX) && defined(__NR_pipe2) && defined(O_CLOEXEC)
65fea56f17cd614bc8908264df980a62e1931468vboxsync long rc = syscall(__NR_pipe2, paFds, O_CLOEXEC);
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (rc >= 0)
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (*piNewPipeSyscall == 0)
65fea56f17cd614bc8908264df980a62e1931468vboxsync *piNewPipeSyscall = 1;
65fea56f17cd614bc8908264df980a62e1931468vboxsync return (int)rc;
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync#endif
65fea56f17cd614bc8908264df980a62e1931468vboxsync *piNewPipeSyscall = -1;
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync return pipe(paFds);
65fea56f17cd614bc8908264df980a62e1931468vboxsync}
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsyncRTDECL(int) RTPipeCreate(PRTPIPE phPipeRead, PRTPIPE phPipeWrite, uint32_t fFlags)
65fea56f17cd614bc8908264df980a62e1931468vboxsync{
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertPtrReturn(phPipeRead, VERR_INVALID_POINTER);
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertPtrReturn(phPipeWrite, VERR_INVALID_POINTER);
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertReturn(!(fFlags & ~RTPIPE_C_VALID_MASK), VERR_INVALID_PARAMETER);
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync /*
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Create the pipe and clear/set the close-on-exec flag as required.
65fea56f17cd614bc8908264df980a62e1931468vboxsync */
65fea56f17cd614bc8908264df980a62e1931468vboxsync int aFds[2] = {-1, -1};
65fea56f17cd614bc8908264df980a62e1931468vboxsync static int s_iNewPipeSyscall = 0;
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (my_pipe_wrapper(aFds, &s_iNewPipeSyscall))
65fea56f17cd614bc8908264df980a62e1931468vboxsync return RTErrConvertFromErrno(errno);
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync int rc = VINF_SUCCESS;
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (s_iNewPipeSyscall > 0)
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync /* created with close-on-exec set. */
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (fFlags & RTPIPE_C_INHERIT_READ)
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (fcntl(aFds[0], F_SETFD, 0))
65fea56f17cd614bc8908264df980a62e1931468vboxsync rc = RTErrConvertFromErrno(errno);
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (fFlags & RTPIPE_C_INHERIT_WRITE)
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (fcntl(aFds[1], F_SETFD, 0))
65fea56f17cd614bc8908264df980a62e1931468vboxsync rc = RTErrConvertFromErrno(errno);
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync else
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync /* created with close-on-exec cleared. */
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (!(fFlags & RTPIPE_C_INHERIT_READ))
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (fcntl(aFds[0], F_SETFD, FD_CLOEXEC))
65fea56f17cd614bc8908264df980a62e1931468vboxsync rc = RTErrConvertFromErrno(errno);
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (!(fFlags & RTPIPE_C_INHERIT_WRITE))
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (fcntl(aFds[1], F_SETFD, FD_CLOEXEC))
65fea56f17cd614bc8908264df980a62e1931468vboxsync rc = RTErrConvertFromErrno(errno);
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (RT_SUCCESS(rc))
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync /*
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Create the two handles.
65fea56f17cd614bc8908264df980a62e1931468vboxsync */
65fea56f17cd614bc8908264df980a62e1931468vboxsync RTPIPEINTERNAL *pThisR = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (pThisR)
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync RTPIPEINTERNAL *pThisW = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (pThisW)
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThisR->u32Magic = RTPIPE_MAGIC;
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThisW->u32Magic = RTPIPE_MAGIC;
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThisR->fd = aFds[0];
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThisW->fd = aFds[1];
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThisR->fRead = true;
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThisW->fRead = false;
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThisR->u32State = RTPIPE_POSIX_BLOCKING;
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThisW->u32State = RTPIPE_POSIX_BLOCKING;
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync *phPipeRead = pThisR;
65fea56f17cd614bc8908264df980a62e1931468vboxsync *phPipeWrite = pThisW;
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync /*
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Before we leave, make sure to shut up SIGPIPE.
65fea56f17cd614bc8908264df980a62e1931468vboxsync */
65fea56f17cd614bc8908264df980a62e1931468vboxsync signal(SIGPIPE, SIG_IGN);
65fea56f17cd614bc8908264df980a62e1931468vboxsync return VINF_SUCCESS;
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync RTMemFree(pThisR);
65fea56f17cd614bc8908264df980a62e1931468vboxsync rc = VERR_NO_MEMORY;
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync else
65fea56f17cd614bc8908264df980a62e1931468vboxsync rc = VERR_NO_MEMORY;
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync close(aFds[0]);
65fea56f17cd614bc8908264df980a62e1931468vboxsync close(aFds[1]);
65fea56f17cd614bc8908264df980a62e1931468vboxsync return rc;
65fea56f17cd614bc8908264df980a62e1931468vboxsync}
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsyncRTDECL(int) RTPipeClose(RTPIPE hPipe)
65fea56f17cd614bc8908264df980a62e1931468vboxsync{
65fea56f17cd614bc8908264df980a62e1931468vboxsync RTPIPEINTERNAL *pThis = hPipe;
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (pThis == NIL_RTPIPE)
65fea56f17cd614bc8908264df980a62e1931468vboxsync return VINF_SUCCESS;
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertPtrReturn(pThis, VERR_INVALID_PARAMETER);
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE);
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync /*
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Do the cleanup.
65fea56f17cd614bc8908264df980a62e1931468vboxsync */
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTPIPE_MAGIC, RTPIPE_MAGIC), VERR_INVALID_HANDLE);
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync int fd = pThis->fd;
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThis->fd = -1;
65fea56f17cd614bc8908264df980a62e1931468vboxsync close(fd);
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (ASMAtomicReadU32(&pThis->u32State) & RTPIPE_POSIX_USERS_MASK)
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertFailed();
65fea56f17cd614bc8908264df980a62e1931468vboxsync RTThreadSleep(1);
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync RTMemFree(pThis);
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync return VINF_SUCCESS;
65fea56f17cd614bc8908264df980a62e1931468vboxsync}
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsyncRTDECL(int) RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags)
65fea56f17cd614bc8908264df980a62e1931468vboxsync{
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertPtrReturn(phPipe, VERR_INVALID_POINTER);
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK), VERR_INVALID_PARAMETER);
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertReturn(!!(fFlags & RTPIPE_N_READ) != !!(fFlags & RTPIPE_N_WRITE), VERR_INVALID_PARAMETER);
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync /*
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Get and validate the pipe handle info.
65fea56f17cd614bc8908264df980a62e1931468vboxsync */
65fea56f17cd614bc8908264df980a62e1931468vboxsync int hNative = (int)hNativePipe;
65fea56f17cd614bc8908264df980a62e1931468vboxsync struct stat st;
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
65fea56f17cd614bc8908264df980a62e1931468vboxsync int fFd = fcntl(hNative, F_GETFL, 0);
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertReturn(fFd != -1, 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 , ("%#x\n", fFd), VERR_INVALID_HANDLE);
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync /*
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Create the handle.
65fea56f17cd614bc8908264df980a62e1931468vboxsync */
65fea56f17cd614bc8908264df980a62e1931468vboxsync RTPIPEINTERNAL *pThis = (RTPIPEINTERNAL *)RTMemAlloc(sizeof(RTPIPEINTERNAL));
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (!pThis)
65fea56f17cd614bc8908264df980a62e1931468vboxsync return VERR_NO_MEMORY;
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThis->u32Magic = RTPIPE_MAGIC;
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThis->fd = hNative;
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThis->fRead = !!(fFlags & RTPIPE_N_READ);
65fea56f17cd614bc8908264df980a62e1931468vboxsync pThis->u32State = fFd & O_NONBLOCK ? 0 : RTPIPE_POSIX_BLOCKING;
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync /*
65fea56f17cd614bc8908264df980a62e1931468vboxsync * Fix up inheritability and shut up SIGPIPE and we're done.
65fea56f17cd614bc8908264df980a62e1931468vboxsync */
65fea56f17cd614bc8908264df980a62e1931468vboxsync if (fcntl(hNative, F_SETFD, fFlags & RTPIPE_N_INHERIT ? 0 : FD_CLOEXEC) == 0)
65fea56f17cd614bc8908264df980a62e1931468vboxsync {
65fea56f17cd614bc8908264df980a62e1931468vboxsync signal(SIGPIPE, SIG_IGN);
65fea56f17cd614bc8908264df980a62e1931468vboxsync *phPipe = pThis;
65fea56f17cd614bc8908264df980a62e1931468vboxsync return VINF_SUCCESS;
65fea56f17cd614bc8908264df980a62e1931468vboxsync }
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync int rc = RTErrConvertFromErrno(errno);
65fea56f17cd614bc8908264df980a62e1931468vboxsync RTMemFree(pThis);
65fea56f17cd614bc8908264df980a62e1931468vboxsync return rc;
65fea56f17cd614bc8908264df980a62e1931468vboxsync}
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsyncRTDECL(RTHCINTPTR) RTPipeToNative(RTPIPE hPipe)
65fea56f17cd614bc8908264df980a62e1931468vboxsync{
65fea56f17cd614bc8908264df980a62e1931468vboxsync RTPIPEINTERNAL *pThis = hPipe;
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertPtrReturn(pThis, -1);
65fea56f17cd614bc8908264df980a62e1931468vboxsync AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, -1);
65fea56f17cd614bc8908264df980a62e1931468vboxsync
65fea56f17cd614bc8908264df980a62e1931468vboxsync return pThis->fd;
65fea56f17cd614bc8908264df980a62e1931468vboxsync}
65fea56f17cd614bc8908264df980a62e1931468vboxsync
/**
* Prepare blocking mode.
*
* @returns VINF_SUCCESS
* @retval VERR_WRONG_ORDER
* @retval VERR_INTERNAL_ERROR_4
*
* @param pThis The pipe handle.
*/
static int rtPipeTryBlocking(RTPIPEINTERNAL *pThis)
{
/*
* Update the state.
*/
for (;;)
{
uint32_t u32State = ASMAtomicReadU32(&pThis->u32State);
uint32_t const u32StateOld = u32State;
uint32_t const cUsers = (u32State & RTPIPE_POSIX_USERS_MASK);
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 | RTPIPE_POSIX_BLOCKING;
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);
}
/**
* Prepare non-blocking mode.
*
* @returns VINF_SUCCESS
* @retval VERR_WRONG_ORDER
* @retval VERR_INTERNAL_ERROR_4
*
* @param pThis The pipe handle.
*/
static int rtPipeTryNonBlocking(RTPIPEINTERNAL *pThis)
{
/*
* Update the state.
*/
for (;;)
{
uint32_t u32State = ASMAtomicReadU32(&pThis->u32State);
uint32_t const u32StateOld = u32State;
uint32_t const cUsers = (u32State & RTPIPE_POSIX_USERS_MASK);
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, 0);
if (rc == -1)
return RTErrConvertFromErrno(errno);
return rc > 0 ? VINF_SUCCESS : VERR_TIMEOUT;
}