VirtualBox

Changeset 33104 in vbox for trunk


Ignore:
Timestamp:
Oct 13, 2010 12:46:59 PM (14 years ago)
Author:
vboxsync
Message:

RTPipeCreate: Use pipe2 on linux (added in 2.6.27) to avoid racing process creation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/posix/pipe-posix.cpp

    r29559 r33104  
    4747#include <sys/stat.h>
    4848#include <signal.h>
     49#ifdef RT_OS_LINUX
     50# include <sys/syscall.h>
     51#endif
    4952
    5053
     
    8285
    8386
     87
     88/**
     89 * Wrapper for calling pipe2() or pipe().
     90 *
     91 * When using pipe2() the returned handles are marked close-on-exec and does
     92 * not risk racing process creation calls on other threads.
     93 *
     94 * @returns See pipe().
     95 * @param   paFds               See pipe().
     96 * @param   piNewPipeSyscall    Where to cache which call we should used. -1 if
     97 *                              pipe(), 1 if pipe2(), 0 if not yet decided.
     98 */
     99static int my_pipe_wrapper(int *paFds, int *piNewPipeSyscall)
     100{
     101    if (*piNewPipeSyscall >= 0)
     102    {
     103#if defined(RT_OS_LINUX) && defined(__NR_pipe2)
     104        long rc = syscall(__NR_pipe2, paFds, O_CLOEXEC);
     105        if (rc >= 0)
     106        {
     107            if (*piNewPipeSyscall == 0)
     108                *piNewPipeSyscall = 1;
     109            return (int)rc;
     110        }
     111#endif
     112        *piNewPipeSyscall = -1;
     113    }
     114
     115    return pipe(paFds);
     116}
     117
     118
    84119RTDECL(int)  RTPipeCreate(PRTPIPE phPipeRead, PRTPIPE phPipeWrite, uint32_t fFlags)
    85120{
     
    89124
    90125    /*
    91      * Create the pipe and set the close-on-exec flag if requested.
     126     * Create the pipe and clear/set the close-on-exec flag as required.
    92127     */
    93128    int aFds[2] = {-1, -1};
    94     if (pipe(aFds))
     129    static int s_iNewPipeSyscall = 0;
     130    if (my_pipe_wrapper(aFds, &s_iNewPipeSyscall))
    95131        return RTErrConvertFromErrno(errno);
    96132
    97133    int rc = VINF_SUCCESS;
    98     if (!(fFlags & RTPIPE_C_INHERIT_READ))
    99     {
    100         if (fcntl(aFds[0], F_SETFD, FD_CLOEXEC))
    101             rc = RTErrConvertFromErrno(errno);
    102     }
    103 
    104     if (!(fFlags & RTPIPE_C_INHERIT_WRITE))
    105     {
    106         if (fcntl(aFds[1], F_SETFD, FD_CLOEXEC))
    107             rc = RTErrConvertFromErrno(errno);
     134    if (s_iNewPipeSyscall > 0)
     135    {
     136        /* created with close-on-exec set. */
     137        if (fFlags & RTPIPE_C_INHERIT_READ)
     138        {
     139            if (fcntl(aFds[0], F_SETFD, 0))
     140                rc = RTErrConvertFromErrno(errno);
     141        }
     142
     143        if (fFlags & RTPIPE_C_INHERIT_WRITE)
     144        {
     145            if (fcntl(aFds[1], F_SETFD, 0))
     146                rc = RTErrConvertFromErrno(errno);
     147        }
     148    }
     149    else
     150    {
     151        /* created with close-on-exec cleared. */
     152        if (!(fFlags & RTPIPE_C_INHERIT_READ))
     153        {
     154            if (fcntl(aFds[0], F_SETFD, FD_CLOEXEC))
     155                rc = RTErrConvertFromErrno(errno);
     156        }
     157
     158        if (!(fFlags & RTPIPE_C_INHERIT_WRITE))
     159        {
     160            if (fcntl(aFds[1], F_SETFD, FD_CLOEXEC))
     161                rc = RTErrConvertFromErrno(errno);
     162        }
    108163    }
    109164
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette