VirtualBox

Changeset 86412 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Oct 2, 2020 11:39:26 AM (4 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
140713
Message:

IPRT/pipe: Adding RTPipeCloseEx w/ a fLeaveOpen parameter so we can prevent leaks via RTHandleGetStandard. Adding RTPIPE_N_LEAVE_OPEN to RTPipeFromNative. bugref:9841

Location:
trunk/src/VBox/Runtime/r3
Files:
3 edited

Legend:

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

    r82968 r86412  
    6868    /** Set if this is the read end, clear if it's the write end. */
    6969    bool                fRead;
     70    /** RTPipeFromNative: Leave open. */
     71    bool                fLeaveOpen;
    7072    /** Whether the pipe is in blocking or non-blocking mode. */
    7173    bool                fBlocking;
     
    195197                    pThisR->fRead           = true;
    196198                    pThisW->fRead           = false;
     199                    pThisR->fLeaveOpen      = false;
     200                    pThisW->fLeaveOpen      = false;
    197201                    pThisR->fBlocking       = false;
    198202                    pThisW->fBlocking       = true;
     
    227231
    228232
    229 RTDECL(int)  RTPipeClose(RTPIPE hPipe)
     233RTDECL(int)  RTPipeCloseEx(RTPIPE hPipe, bool fLeaveOpen)
    230234{
    231235    RTPIPEINTERNAL *pThis = hPipe;
     
    243247
    244248    /* Don't call DosDisConnectNPipe! */
    245     DosClose(pThis->hPipe);
     249    if (!fLeaveOpen && !pThis->fLeaveOpen)
     250        DosClose(pThis->hPipe);
    246251    pThis->hPipe = (HPIPE)-1;
    247252
     
    261266
    262267
     268RTDECL(int)  RTPipeClose(RTPIPE hPipe)
     269{
     270    return RTPipeCloseEx(hPipe, false /*fLeaveOpen*/);
     271}
     272
     273
    263274RTDECL(int)  RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags)
    264275{
    265276    AssertPtrReturn(phPipe, VERR_INVALID_POINTER);
    266     AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK), VERR_INVALID_PARAMETER);
     277    AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK_FN), VERR_INVALID_PARAMETER);
    267278    AssertReturn(!!(fFlags & RTPIPE_N_READ) != !!(fFlags & RTPIPE_N_WRITE), VERR_INVALID_PARAMETER);
    268279
     
    333344        pThis->hPipe           = hNative;
    334345        pThis->hev             = NULLHANDLE;
    335         pThis->fRead           = !!(fFlags & RTPIPE_N_READ);
     346        pThis->fRead           = RT_BOOL(fFlags & RTPIPE_N_READ);
     347        pThis->fLeaveOpen      = RT_BOOL(fFlags & RTPIPE_N_LEAVE_OPEN);
    336348        pThis->fBlocking       = !(fPipeState & NP_NOWAIT);
    337349        //pThis->fBrokenPipe     = false;
  • trunk/src/VBox/Runtime/r3/posix/pipe-posix.cpp

    r82968 r86412  
    7070    /** Set if this is the read end, clear if it's the write end. */
    7171    bool                fRead;
     72    /** RTPipeFromNative: Leave it open on RTPipeClose. */
     73    bool                fLeaveOpen;
    7274    /** Atomically operated state variable.
    7375     *
     
    181183            if (pThisW)
    182184            {
    183                 pThisR->u32Magic = RTPIPE_MAGIC;
    184                 pThisW->u32Magic = RTPIPE_MAGIC;
    185                 pThisR->fd       = aFds[0];
    186                 pThisW->fd       = aFds[1];
    187                 pThisR->fRead    = true;
    188                 pThisW->fRead    = false;
    189                 pThisR->u32State = RTPIPE_POSIX_BLOCKING;
    190                 pThisW->u32State = RTPIPE_POSIX_BLOCKING;
     185                pThisR->u32Magic    = RTPIPE_MAGIC;
     186                pThisW->u32Magic    = RTPIPE_MAGIC;
     187                pThisR->fd          = aFds[0];
     188                pThisW->fd          = aFds[1];
     189                pThisR->fRead       = true;
     190                pThisW->fRead       = false;
     191                pThisR->fLeaveOpen  = false;
     192                pThisW->fLeaveOpen  = false;
     193                pThisR->u32State    = RTPIPE_POSIX_BLOCKING;
     194                pThisW->u32State    = RTPIPE_POSIX_BLOCKING;
    191195
    192196                *phPipeRead  = pThisR;
     
    213217
    214218
    215 RTDECL(int)  RTPipeClose(RTPIPE hPipe)
     219RTDECL(int)  RTPipeCloseEx(RTPIPE hPipe, bool fLeaveOpen)
    216220{
    217221    RTPIPEINTERNAL *pThis = hPipe;
     
    228232    int fd = pThis->fd;
    229233    pThis->fd = -1;
    230     close(fd);
     234    if (!fLeaveOpen && !pThis->fLeaveOpen)
     235        close(fd);
    231236
    232237    if (ASMAtomicReadU32(&pThis->u32State) & RTPIPE_POSIX_USERS_MASK)
     
    241246}
    242247
     248
     249RTDECL(int)  RTPipeClose(RTPIPE hPipe)
     250{
     251    return RTPipeCloseEx(hPipe, false /*fLeaveOpen*/);
     252}
     253
     254
    243255RTDECL(int)  RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags)
    244256{
    245257    AssertPtrReturn(phPipe, VERR_INVALID_POINTER);
    246     AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK), VERR_INVALID_PARAMETER);
     258    AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK_FN), VERR_INVALID_PARAMETER);
    247259    AssertReturn(!!(fFlags & RTPIPE_N_READ) != !!(fFlags & RTPIPE_N_WRITE), VERR_INVALID_PARAMETER);
    248260
     
    268280        return VERR_NO_MEMORY;
    269281
    270     pThis->u32Magic = RTPIPE_MAGIC;
    271     pThis->fd       = hNative;
    272     pThis->fRead    = !!(fFlags & RTPIPE_N_READ);
    273     pThis->u32State = fFd & O_NONBLOCK ? 0 : RTPIPE_POSIX_BLOCKING;
     282    pThis->u32Magic   = RTPIPE_MAGIC;
     283    pThis->fd         = hNative;
     284    pThis->fRead      = RT_BOOL(fFlags & RTPIPE_N_READ);
     285    pThis->fLeaveOpen = RT_BOOL(fFlags & RTPIPE_N_LEAVE_OPEN);
     286    pThis->u32State   = fFd & O_NONBLOCK ? 0 : RTPIPE_POSIX_BLOCKING;
    274287
    275288    /*
  • trunk/src/VBox/Runtime/r3/win/pipe-win.cpp

    r82968 r86412  
    6767    /** Set if this is the read end, clear if it's the write end. */
    6868    bool                fRead;
     69    /** RTPipeFromNative: Leave native handle open on RTPipeClose. */
     70    bool                fLeaveOpen;
    6971    /** Set if there is already pending I/O. */
    7072    bool                fIOPending;
     
    284286                            pThisR->fRead               = true;
    285287                            pThisW->fRead               = false;
     288                            pThisR->fLeaveOpen          = false;
     289                            pThisW->fLeaveOpen          = false;
    286290                            //pThisR->fIOPending        = false;
    287291                            //pThisW->fIOPending        = false;
     
    400404
    401405
    402 RTDECL(int)  RTPipeClose(RTPIPE hPipe)
     406RTDECL(int)  RTPipeCloseEx(RTPIPE hPipe, bool fLeaveOpen)
    403407{
    404408    RTPIPEINTERNAL *pThis = hPipe;
     
    418422        rtPipeWriteCheckCompletion(pThis);
    419423
    420     CloseHandle(pThis->hPipe);
     424    if (!fLeaveOpen && !pThis->fLeaveOpen)
     425        CloseHandle(pThis->hPipe);
    421426    pThis->hPipe = INVALID_HANDLE_VALUE;
    422427
     
    436441
    437442
     443RTDECL(int)  RTPipeClose(RTPIPE hPipe)
     444{
     445    return RTPipeCloseEx(hPipe, false /*fLeaveOpen*/);
     446}
     447
     448
    438449RTDECL(int)  RTPipeFromNative(PRTPIPE phPipe, RTHCINTPTR hNativePipe, uint32_t fFlags)
    439450{
    440451    AssertPtrReturn(phPipe, VERR_INVALID_POINTER);
    441     AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK), VERR_INVALID_PARAMETER);
     452    AssertReturn(!(fFlags & ~RTPIPE_N_VALID_MASK_FN), VERR_INVALID_PARAMETER);
    442453    AssertReturn(!!(fFlags & RTPIPE_N_READ) != !!(fFlags & RTPIPE_N_WRITE), VERR_INVALID_PARAMETER);
    443454
     
    482493            pThis->u32Magic             = RTPIPE_MAGIC;
    483494            pThis->hPipe                = hNative;
    484             pThis->fRead                = !!(fFlags & RTPIPE_N_READ);
     495            pThis->fRead                = RT_BOOL(fFlags & RTPIPE_N_READ);
     496            pThis->fLeaveOpen           = RT_BOOL(fFlags & RTPIPE_N_LEAVE_OPEN);
    485497            //pThis->fIOPending         = false;
    486498            //pThis->fZeroByteRead      = false;
     
    510522                    pThis->hPipe = hNative2;
    511523                    if (rtPipeQueryNtInfo(pThis, &Info))
     524                    {
     525                        pThis->fLeaveOpen = false;
    512526                        rc = VINF_SUCCESS;
     527                    }
    513528                    else
    514529                    {
     
    552567                    if (hNative2 != INVALID_HANDLE_VALUE)
    553568                    {
    554                         if (   hNative != GetStdHandle(STD_INPUT_HANDLE)
     569                        if (   !(fFlags & RTPIPE_N_LEAVE_OPEN)
     570                            && hNative != GetStdHandle(STD_INPUT_HANDLE)
    555571                            && hNative != GetStdHandle(STD_OUTPUT_HANDLE)
    556                             && hNative != GetStdHandle(STD_ERROR_HANDLE))
     572                            && hNative != GetStdHandle(STD_ERROR_HANDLE) )
    557573                            CloseHandle(hNative);
    558574                    }
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