VirtualBox

Ignore:
Timestamp:
Apr 20, 2009 2:48:09 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
46142
Message:

VBoxService/common: More updates for Windows SCM handling.

File:
1 edited

Legend:

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

    r18712 r19031  
    162162    va_end(va);
    163163
    164 #if 0 /* enable after 2.2 */
    165164    va_start(va, pszFormat);
    166     LogRel(("%s: error: %N", g_pszProgName, pszFormat, &va));
     165    LogRel(("%s: Error: %N", g_pszProgName, pszFormat, &va));
    167166    va_end(va);
    168 #endif
    169167
    170168    return 1;
     
    247245
    248246
     247unsigned VBoxServiceGetStartedServices(void)
     248{
     249    unsigned iMain = ~0U;
     250    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
     251        if (g_aServices[j].fEnabled)
     252        {
     253            iMain = j;
     254            break;
     255        }
     256
     257   return iMain; /* Return the index of the main service (must always come last!). */
     258}
     259
    249260/**
    250261 * Starts the service.
     
    262273     * Initialize the services.
    263274     */
     275    VBoxServiceVerbose(2, "Initializing services ...\n");
    264276    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    265277    {
     
    275287     * Start the service(s).
    276288     */
     289    VBoxServiceVerbose(2, "Starting services ...\n");
    277290    rc = VINF_SUCCESS;
    278291    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
     
    282295            continue;
    283296
     297        VBoxServiceVerbose(2, "Starting service     '%s' ...\n", g_aServices[j].pDesc->pszName);
    284298        rc = RTThreadCreate(&g_aServices[j].Thread, VBoxServiceThread, (void *)(uintptr_t)j, 0,
    285299                            RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName);
     
    299313    {
    300314        /* The final service runs in the main thread. */
    301         VBoxServiceVerbose(1, "starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName);
     315        VBoxServiceVerbose(1, "Starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName);
    302316        rc = g_aServices[iMain].pDesc->pfnWorker(&g_fShutdown);
    303         VBoxServiceError("service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc);
    304     }
    305 
     317        VBoxServiceError("Service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc);
     318    }
     319
     320    /* Should never get here. */
    306321    return rc;
    307322}
     
    314329 * clean up anything that we succeeded in starting.
    315330 */
    316 void VBoxServiceStopServices(void)
    317 {
     331int VBoxServiceStopServices(void)
     332{
     333    int rc = VINF_SUCCESS;
     334
    318335    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    319336        ASMAtomicXchgBool(&g_aServices[j].fShutdown, true);
     
    327344            int rc = RTThreadWait(g_aServices[j].Thread, 30*1000, NULL);
    328345            if (RT_FAILURE(rc))
    329                 VBoxServiceError("service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
     346                VBoxServiceError("Service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
    330347        }
    331348        g_aServices[j].pDesc->pfnTerm();
    332349    }
     350
     351    VBoxServiceVerbose(2, "Stopping services returned: rc=%Rrc\n", rc);
     352    return rc;
    333353}
    334354
     
    336356int main(int argc, char **argv)
    337357{
    338     int rc;
     358    int rc = VINF_SUCCESS;
    339359
    340360    /*
     
    351371
    352372#ifdef RT_OS_WINDOWS
    353     /*
    354      * Explain the purpose of this exercise and why ignoring the result is a good idea and everything.
    355      */
    356     /* Do not use a global namespace ("Global\\") for mutex name here, will blow up NT4 compatibility! */
     373    /* Make sure only one instance of VBoxService runs at a time. Create a global mutex for that.   
     374       Do not use a global namespace ("Global\\") for mutex name here, will blow up NT4 compatibility! */
    357375    HANDLE hMutexAppRunning = CreateMutex (NULL, FALSE, VBOXSERVICE_NAME);
    358376    if (   hMutexAppRunning != NULL
     
    492510     * Check that at least one service is enabled.
    493511     */
    494     unsigned iMain = ~0U;
    495     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    496         if (g_aServices[j].fEnabled)
    497         {
    498             iMain = j;
    499             break;
    500         }
     512    unsigned iMain = VBoxServiceGetStartedServices();
    501513    if (iMain == ~0U)
    502514        return VBoxServiceSyntax("At least one service must be enabled.\n");
     
    529541         *        continue.
    530542         */
     543        VBoxServiceVerbose(2, "Starting service dispatcher ...\n");
    531544        if (!StartServiceCtrlDispatcher(&g_aServiceTable[0]))
    532545            return VBoxServiceError("StartServiceCtrlDispatcher: %u\n", GetLastError());
    533 
     546        /* Service now lives in the control dispatcher registered above. */
    534547#else
    535548        VBoxServiceVerbose(1, "Daemonizing...\n");
     
    540553#endif
    541554    }
    542 
    543 /** @todo Make the main thread responsive to signal so it can shutdown/restart the threads on non-SIGKILL signals. */
    544 
    545 #ifdef RT_OS_WINDOWS
    546     if (not using StartServiceCtrlDispatcher or auto register & starting the service)
    547 #endif
    548     {
     555    else
     556    {
     557        /** @todo Make the main thread responsive to signal so it can shutdown/restart the threads on non-SIGKILL signals. */
     558
    549559        /*
    550560         * Start the service, enter the main threads run loop and stop them again when it returns.
     
    554564    }
    555565
    556 
    557566#ifdef RT_OS_WINDOWS
    558567    /*
     
    560569     */
    561570    if (hMutexAppRunning != NULL)
    562         CloseHandle(hMutexAppRunning);
     571    {   
     572        ::CloseHandle(hMutexAppRunning);
     573        hMutexAppRunning = NULL;
     574    }
    563575#endif
    564576
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