VirtualBox

Changeset 28603 in vbox


Ignore:
Timestamp:
Apr 22, 2010 3:48:25 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
60450
Message:

VBoxService: improved POSIX shutdown mechanism

File:
1 edited

Legend:

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

    r28600 r28603  
    259259{
    260260    const unsigned i = (uintptr_t)pvUser;
     261
     262#ifndef RT_OS_WINDOWS
     263    /*
     264     * Block all signals for this thread. Only the main thread will handle signals.
     265     */
     266    sigset_t signalMask;
     267    sigfillset(&signalMask);
     268    pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
     269#endif
     270
    261271    int rc = g_aServices[i].pDesc->pfnWorker(&g_aServices[i].fShutdown);
    262272    ASMAtomicXchgBool(&g_aServices[i].fShutdown, true);
     
    336346        }
    337347    }
    338     if (RT_SUCCESS(rc))
     348    if (   RT_SUCCESS(rc)
     349        && iMain != ~0U)
    339350    {
    340351        /* The final service runs in the main thread. */
     
    393404
    394405#ifndef RT_OS_WINDOWS
    395 /**
    396  * Signal handler for properly shutting down the services on Posix platforms.
    397  */
    398 static void VBoxServiceSignalHandler(int /* sig */)
    399 {
    400     VBoxServiceVerbose(2, "VBoxServiceSignalHandler hit\n");
    401     /* Flag the main service that we want to terminate */
    402     g_fShutdown = 1;
    403     unsigned iMain = VBoxServiceGetStartedServices();
    404     /* Get the main thread out of the waiting loop */
    405     g_aServices[iMain].pDesc->pfnStop();
     406/*
     407 * Block all important signals, then explicitly wait until one of these signal arrives.
     408 */
     409static void VBoxServiceWaitSignal(void)
     410{
     411    sigset_t signalMask;
     412    int iSignal;
     413    sigemptyset(&signalMask);
     414    sigaddset(&signalMask, SIGHUP);
     415    sigaddset(&signalMask, SIGINT);
     416    sigaddset(&signalMask, SIGQUIT);
     417    sigaddset(&signalMask, SIGABRT);
     418    sigaddset(&signalMask, SIGTERM);
     419    pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
     420    sigwait(&signalMask, &iSignal);
     421    VBoxServiceVerbose(3, "VBoxServiceWaitSignal: Received signal %d\n", iSignal);
    406422}
    407423#endif
     
    578594    if (iMain == ~0U)
    579595        return VBoxServiceSyntax("At least one service must be enabled.\n");
     596
     597#ifndef RT_OS_WINDOWS
     598    /*
     599     * POSIX: No main service thread.
     600     */
     601    iMain = ~0U;
     602#endif
    580603
    581604    VBoxServiceVerbose(0, "%s r%s started. Verbose level = %d\n",
     
    618641        /* Run the app just like a console one if not daemonized. */
    619642#endif
    620         /** @todo Make the main thread responsive to signal so it can shutdown/restart the threads on non-SIGKILL signals. */
    621 
    622643        /*
    623          * Start the service, enter the main threads run loop and stop them again when it returns.
     644         * Windows: Start the services, enter the main threads' run loop and stop them
     645         * again when it returns.
     646         *
     647         * POSIX: Start all services and return immediately.
    624648         */
     649        rc = VBoxServiceStartServices(iMain);
    625650#ifndef RT_OS_WINDOWS
    626         struct sigaction sa;
    627         sa.sa_handler = VBoxServiceSignalHandler;
    628         sigemptyset(&sa.sa_mask);
    629         sa.sa_flags = 0;
    630         sigaction(SIGHUP, &sa, NULL);
    631         sigaction(SIGINT, &sa, NULL);
    632         sigaction(SIGQUIT, &sa, NULL);
    633         sigaction(SIGABRT, &sa, NULL);
    634         sigaction(SIGTERM, &sa, NULL);
    635 #endif
    636         rc = VBoxServiceStartServices(iMain);
     651        VBoxServiceWaitSignal();
     652#endif
    637653        VBoxServiceStopServices();
    638654#ifdef RT_OS_WINDOWS
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