VirtualBox

Changeset 58290 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Oct 17, 2015 9:52:28 PM (9 years ago)
Author:
vboxsync
Message:

RTLocalIpc fixes.

Location:
trunk/src/VBox
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceVMInfo-win.cpp

    r58089 r58290  
    10141014#endif
    10151015            int rc2 = RTLocalIpcSessionClose(hSession);
    1016             if (RT_SUCCESS(rc))
     1016            if (RT_SUCCESS(rc) && RT_FAILURE(rc2))
    10171017                rc = rc2;
    10181018        }
  • trunk/src/VBox/Runtime/r3/posix/localipc-posix.cpp

    r58282 r58290  
    2929*   Header Files                                                               *
    3030*******************************************************************************/
     31#define LOG_GROUP RTLOGGROUP_LOCALIPC
    3132#include "internal/iprt.h"
    3233#include <iprt/localipc.h>
     
    3738#include <iprt/critsect.h>
    3839#include <iprt/mem.h>
     40#include <iprt/log.h>
    3941#include <iprt/poll.h>
    4042#include <iprt/socket.h>
     
    4446#include <sys/socket.h>
    4547#include <sys/un.h>
     48#ifndef RT_OS_OS2
     49# include <sys/poll.h>
     50# include <errno.h>
     51#endif
    4652#include <fcntl.h>
    4753#include <unistd.h>
     
    226232                        if (RT_SUCCESS(rc))
    227233                        {
     234                            LogFlow(("RTLocalIpcServerCreate: Created %p (%s)\n", pThis, pThis->Name.sun_path));
    228235                            *phServer = pThis;
    229236                            return VINF_SUCCESS;
     
    239246            rc = VERR_NO_MEMORY;
    240247    }
     248    Log(("RTLocalIpcServerCreate: failed, rc=%Rrc\n", rc));
    241249    return rc;
    242250}
     
    258266/**
    259267 * Server instance destructor.
     268 *
     269 * @returns VINF_OBJECT_DESTROYED
    260270 * @param   pThis               The server instance.
    261271 */
    262 static void rtLocalIpcServerDtor(PRTLOCALIPCSERVERINT pThis)
     272static int rtLocalIpcServerDtor(PRTLOCALIPCSERVERINT pThis)
    263273{
    264274    pThis->u32Magic = ~RTLOCALIPCSERVER_MAGIC;
    265     RTSocketRelease(pThis->hSocket);
     275    if (RTSocketRelease(pThis->hSocket) == 0)
     276        Log(("rtLocalIpcServerDtor: Released socket\n"));
     277    else
     278        Log(("rtLocalIpcServerDtor: Socket still has references (impossible?)\n"));
    266279    RTCritSectDelete(&pThis->CritSect);
    267280    unlink(pThis->Name.sun_path);
    268281    RTMemFree(pThis);
     282    return VINF_OBJECT_DESTROYED;
    269283}
    270284
     
    273287 * Releases a reference to the server instance.
    274288 *
     289 * @returns VINF_SUCCESS if only release, VINF_OBJECT_DESTROYED if destroyed.
    275290 * @param   pThis               The server instance.
    276291 */
    277 DECLINLINE(void) rtLocalIpcServerRelease(PRTLOCALIPCSERVERINT pThis)
     292DECLINLINE(int) rtLocalIpcServerRelease(PRTLOCALIPCSERVERINT pThis)
    278293{
    279294    uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
    280295    Assert(cRefs < UINT32_MAX / 2);
    281296    if (!cRefs)
    282         rtLocalIpcServerDtor(pThis);
     297        return rtLocalIpcServerDtor(pThis);
     298    return VINF_SUCCESS;
    283299}
    284300
     
    294310    RTCritSectEnter(&pThis->CritSect);
    295311    pThis->fCancelled = true;
     312    Log(("rtLocalIpcServerCancel:\n"));
    296313    if (pThis->hListenThread != NIL_RTTHREAD)
    297314        RTThreadPoke(pThis->hListenThread);
     
    320337
    321338    rtLocalIpcServerCancel(pThis);
    322     rtLocalIpcServerRelease(pThis);
    323 
    324     return VINF_SUCCESS;
     339    return rtLocalIpcServerRelease(pThis);
    325340}
    326341
     
    385400                    size_t              cbAddr = sizeof(Addr);
    386401                    RTSOCKET            hClient;
     402                    Log(("RTLocalIpcServerListen: Calling rtSocketAccept...\n"));
    387403                    rc = rtSocketAccept(pThis->hSocket, &hClient, (struct sockaddr *)&Addr, &cbAddr);
     404                    Log(("RTLocalIpcServerListen: rtSocketAccept returns %Rrc.\n", rc));
    388405
    389406                    int rc2 = RTCritSectEnter(&pThis->CritSect);
     
    407424                            rc = RTCritSectInit(&pSession->CritSect);
    408425                            if (RT_SUCCESS(rc))
     426                            {
     427                                Log(("RTLocalIpcServerListen: Returning new client session: %p\n", pSession));
    409428                                *phClientSession = pSession;
    410                             else
    411                                 RTMemFree(pSession);
     429                                break;
     430                            }
     431
     432                            RTMemFree(pSession);
    412433                        }
    413434                        else
     
    440461    rtLocalIpcServerRelease(pThis);
    441462
     463    Log(("RTLocalIpcServerListen: returns %Rrc\n", rc));
    442464    return rc;
    443465}
     
    491513                        {
    492514                            *phSession = pThis;
     515                            Log(("RTLocalIpcSessionConnect: Returns new session %p\n", pThis));
    493516                            return VINF_SUCCESS;
    494517                        }
     
    502525            rc = VERR_NO_MEMORY;
    503526    }
     527    Log(("RTLocalIpcSessionConnect: returns %Rrc\n", rc));
    504528    return rc;
    505529}
     
    520544/**
    521545 * Session instance destructor.
     546 *
     547 * @returns VINF_OBJECT_DESTROYED
    522548 * @param   pThis               The server instance.
    523549 */
    524 static void rtLocalIpcSessionDtor(PRTLOCALIPCSESSIONINT pThis)
     550static int rtLocalIpcSessionDtor(PRTLOCALIPCSESSIONINT pThis)
    525551{
    526552    pThis->u32Magic = ~RTLOCALIPCSESSION_MAGIC;
    527553    if (RTSocketRelease(pThis->hSocket) == 0)
    528         pThis->hSocket = NIL_RTSOCKET;
     554        Log(("rtLocalIpcSessionDtor: Released socket\n"));
     555    else
     556        Log(("rtLocalIpcSessionDtor: Socket still has references (impossible?)\n"));
    529557    RTCritSectDelete(&pThis->CritSect);
    530558    RTMemFree(pThis);
     559    return VINF_OBJECT_DESTROYED;
    531560}
    532561
     
    535564 * Releases a reference to the session instance.
    536565 *
     566 * @returns VINF_SUCCESS or VINF_OBJECT_DESTROYED as appropriate.
    537567 * @param   pThis               The session instance.
    538568 */
    539 DECLINLINE(void) rtLocalIpcSessionRelease(PRTLOCALIPCSESSIONINT pThis)
     569DECLINLINE(int) rtLocalIpcSessionRelease(PRTLOCALIPCSESSIONINT pThis)
    540570{
    541571    uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
    542572    Assert(cRefs < UINT32_MAX / 2);
    543573    if (!cRefs)
    544         rtLocalIpcSessionDtor(pThis);
     574        return rtLocalIpcSessionDtor(pThis);
     575    Log(("rtLocalIpcSessionRelease: %u refs left\n", cRefs));
     576    return VINF_SUCCESS;
    545577}
    546578
     
    556588    RTCritSectEnter(&pThis->CritSect);
    557589    pThis->fCancelled = true;
     590    Log(("rtLocalIpcSessionCancel:\n"));
    558591    if (pThis->hReadThread != NIL_RTTHREAD)
    559592        RTThreadPoke(pThis->hReadThread);
     
    581614     */
    582615    AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTLOCALIPCSESSION_MAGIC, RTLOCALIPCSESSION_MAGIC), VERR_WRONG_ORDER);
     616    Log(("RTLocalIpcSessionClose:\n"));
    583617
    584618    rtLocalIpcSessionCancel(pThis);
    585     rtLocalIpcSessionRelease(pThis);
    586 
    587     return VINF_SUCCESS;
     619    return rtLocalIpcSessionRelease(pThis);
    588620}
    589621
     
    606638    return VINF_SUCCESS;
    607639}
     640
     641
     642#if 0 /* maybe later */
     643/**
     644 * Checks if the socket has has a HUP condition.
     645 *
     646 * @returns true if HUP, false if no.
     647 * @param   pThis       The IPC session handle.
     648 */
     649static bool rtLocalIpcPosixHasHup(PRTLOCALIPCSESSIONINT pThis)
     650{
     651# ifndef RT_OS_OS2
     652    struct pollfd PollFd;
     653    RT_ZERO(PollFd);
     654    PollFd.fd      = RTSocketToNative(pThis->hSocket);
     655    PollFd.events  = POLLHUP;
     656    return poll(&PollFd, 1, 0) >= 1
     657       && (PollFd.revents & POLLHUP);
     658
     659# else /* RT_OS_OS2: */
     660    return false;
     661# endif
     662}
     663#endif
    608664
    609665
     
    779835
    780836                    uint32_t fEvents = 0;
     837#ifdef RT_OS_OS2
     838                    /* This doesn't give us any error condition on hangup. */
     839                    Log(("RTLocalIpcSessionWaitForData: Calling RTSocketSelectOneEx...\n"));
    781840                    rc = RTSocketSelectOneEx(pThis->hSocket, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, &fEvents, cMillies);
     841                    Log(("RTLocalIpcSessionWaitForData: RTSocketSelectOneEx returns %Rrc, fEvents=%#x\n", rc, fEvents));
     842#else
     843/** @todo RTSocketPoll */
     844                    /* POLLHUP will be set on hangup. */
     845                    struct pollfd PollFd;
     846                    RT_ZERO(PollFd);
     847                    PollFd.fd      = RTSocketToNative(pThis->hSocket);
     848                    PollFd.events  = POLLHUP | POLLERR | POLLIN;
     849                    Log(("RTLocalIpcSessionWaitForData: Calling poll...\n"));
     850                    int cFds = poll(&PollFd, 1, cMillies == RT_INDEFINITE_WAIT ? -1 : cMillies);
     851                    if (cFds >= 1)
     852                    {
     853                        fEvents = PollFd.revents & (POLLHUP | POLLERR) ? RTPOLL_EVT_ERROR : RTPOLL_EVT_READ;
     854                        rc = VINF_SUCCESS;
     855                    }
     856                    else if (rc == 0)
     857                        rc = VERR_TIMEOUT;
     858                    else
     859                        rc = RTErrConvertFromErrno(errno);
     860                    Log(("RTLocalIpcSessionWaitForData: poll returns %u (rc=%%d), revents=%#x\n", cFds, rc, PollFd.revents));
     861#endif
    782862
    783863                    int rc2 = RTCritSectEnter(&pThis->CritSect);
  • trunk/src/VBox/Runtime/r3/win/localipc-win.cpp

    r58288 r58290  
    401401/**
    402402 * Call when the reference count reaches 0.
     403 *
    403404 * Caller owns the critsect.
     405 *
     406 * @returns VINF_OBJECT_DESTROYED
    404407 * @param   pThis       The instance to destroy.
    405408 */
    406 static void rtLocalIpcServerWinDestroy(PRTLOCALIPCSERVERINT pThis)
     409static int rtLocalIpcServerWinDestroy(PRTLOCALIPCSERVERINT pThis)
    407410{
    408411    BOOL fRc = CloseHandle(pThis->hNmPipe);
     
    418421
    419422    RTMemFree(pThis);
     423    return VINF_OBJECT_DESTROYED;
    420424}
    421425
     
    450454    }
    451455    else
    452         rtLocalIpcServerWinDestroy(pThis);
    453 
     456        return rtLocalIpcServerWinDestroy(pThis);
    454457    return VINF_SUCCESS;
    455458}
     
    735738/**
    736739 * Call when the reference count reaches 0.
     740 *
    737741 * Caller owns the critsect.
     742 *
     743 * @returns VINF_OBJECT_DESTROYED
    738744 * @param   pThis       The instance to destroy.
    739745 */
    740 static void rtLocalIpcSessionWinDestroy(PRTLOCALIPCSESSIONINT pThis)
     746static int rtLocalIpcSessionWinDestroy(PRTLOCALIPCSESSIONINT pThis)
    741747{
    742748    BOOL fRc = CloseHandle(pThis->hNmPipe);
     
    752758
    753759    RTMemFree(pThis);
     760    return VINF_OBJECT_DESTROYED;
    754761}
    755762
     
    783790    }
    784791    else
    785         rtLocalIpcSessionWinDestroy(pThis);
    786 
     792        return rtLocalIpcSessionWinDestroy(pThis);
    787793    return VINF_SUCCESS;
    788794}
  • trunk/src/VBox/Runtime/testcase/tstRTLocalIpc.cpp

    r58289 r58290  
    7474    RTTESTI_CHECK_RC_RETV(RTLocalIpcServerCreate(&hIpcServer, "BasicTest", RTLOCALIPC_FLAGS_MULTI_SESSION), VINF_SUCCESS);
    7575    RTTESTI_CHECK_RC(RTLocalIpcServerCancel(hIpcServer), VINF_SUCCESS);
    76     RTTESTI_CHECK_RC(RTLocalIpcServerDestroy(hIpcServer), VINF_SUCCESS);
     76    RTTESTI_CHECK_RC(RTLocalIpcServerDestroy(hIpcServer), VINF_OBJECT_DESTROYED);
    7777
    7878    /* Client-side (per session). */
     
    120120            RTThreadSleep(8); /* windows output fudge (purely esthetical) */
    121121            RTTestIPrintf(RTTESTLVL_INFO, "testServerListenThread: Got new client connection.\n");
    122             RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hIpcSession), VINF_SUCCESS);
     122            RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hIpcSession), VINF_OBJECT_DESTROYED);
    123123        }
    124124        else
     
    143143                        VINF_SUCCESS, rcCheck);
    144144    RTTEST_CHECK_RC_RET(g_hTest, RTLocalIpcSessionClose(hClientSession),
    145                         VINF_SUCCESS, rcCheck);
     145                        VINF_OBJECT_DESTROYED, rcCheck);
    146146
    147147    return VINF_SUCCESS;
     
    214214    }
    215215
    216     RTTESTI_CHECK_RC(RTLocalIpcServerDestroy(hIpcServer), VINF_SUCCESS);
     216    RTTESTI_CHECK_RC(RTLocalIpcServerDestroy(hIpcServer), VINF_OBJECT_DESTROYED);
    217217}
    218218
     
    242242            RTTESTI_CHECK_RC(RTLocalIpcSessionWaitForData(hIpcSession, RT_MS_1MIN), VINF_SUCCESS);
    243243
    244             RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hIpcSession), VINF_SUCCESS);
     244            RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hIpcSession), VINF_OBJECT_DESTROYED);
    245245            RTTESTI_CHECK_RC_OK(RTThreadUserSignal(hSelf));
    246246        }
     
    288288    }
    289289
    290     RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hClientSession), VINF_SUCCESS);
     290    RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hClientSession), VINF_OBJECT_DESTROYED);
    291291
    292292    return VINF_SUCCESS;
     
    347347            RTTESTI_CHECK_RC(rcThread, VERR_CANCELLED);
    348348
    349         RTTESTI_CHECK_RC(RTLocalIpcServerDestroy(hIpcServer), VINF_SUCCESS);
     349        RTTESTI_CHECK_RC(RTLocalIpcServerDestroy(hIpcServer), VINF_OBJECT_DESTROYED);
    350350
    351351        /*
     
    521521            }
    522522
    523             RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hIpcSession), VINF_SUCCESS);
     523            RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hIpcSession), VINF_OBJECT_DESTROYED);
    524524            RTTESTI_CHECK_RC_OK(RTThreadUserSignal(hSelf));
    525525        }
     
    567567    }
    568568
    569     RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hClientSession), VINF_SUCCESS);
     569    RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hClientSession), VINF_OBJECT_DESTROYED);
    570570
    571571    return rc;
     
    625625            RTTESTI_CHECK_RC(rcThread, VERR_CANCELLED);
    626626
    627         RTTESTI_CHECK_RC(RTLocalIpcServerDestroy(hIpcServer), VINF_SUCCESS);
     627        RTTESTI_CHECK_RC(RTLocalIpcServerDestroy(hIpcServer), VINF_OBJECT_DESTROYED);
    628628
    629629        /*
     
    702702                    if (cNsElapsed > 2*RT_NS_1SEC_64)
    703703                    {
    704                         uint32_t uMsg = IPC_PERF_LAST_MSG;
     704                        uMsg = IPC_PERF_LAST_MSG;
    705705                        RTTESTI_CHECK_RC_BREAK(rc = RTLocalIpcSessionWrite(hIpcSession, &uMsg, sizeof(uMsg)), VINF_SUCCESS);
    706706                        break;
     
    715715            }
    716716
    717             RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hIpcSession), VINF_SUCCESS);
     717            RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hIpcSession), VINF_OBJECT_DESTROYED);
    718718            RTTESTI_CHECK_RC_OK(RTThreadUserSignal(hSelf));
    719719        }
     
    767767    }
    768768
    769     RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hClientSession), VINF_SUCCESS);
     769    RTTESTI_CHECK_RC(RTLocalIpcSessionClose(hClientSession), VINF_OBJECT_DESTROYED);
    770770    return rc;
    771771}
     
    824824            RTTESTI_CHECK_RC(rcThread, VERR_CANCELLED);
    825825
    826         RTTESTI_CHECK_RC(RTLocalIpcServerDestroy(hIpcServer), VINF_SUCCESS);
     826        RTTESTI_CHECK_RC(RTLocalIpcServerDestroy(hIpcServer), VINF_OBJECT_DESTROYED);
    827827
    828828        /*
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