VirtualBox

Changeset 36328 in vbox for trunk/src/VBox/Additions


Ignore:
Timestamp:
Mar 21, 2011 4:45:21 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
70659
Message:

VBoxService.cpp: Re r70385: Check if CreateMutex fails. Minor cleanups.

File:
1 edited

Legend:

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

    r36185 r36328  
    122122 * @returns 1.
    123123 */
    124 static int VBoxServiceUsage(void)
     124static int vboxServiceUsage(void)
    125125{
    126126    RTPrintf("Usage:\n"
     
    287287 * @param   pvUser          The service index.
    288288 */
    289 static DECLCALLBACK(int) VBoxServiceThread(RTTHREAD ThreadSelf, void *pvUser)
     289static DECLCALLBACK(int) vboxServiceThread(RTTHREAD ThreadSelf, void *pvUser)
    290290{
    291291    const unsigned i = (uintptr_t)pvUser;
     
    308308
    309309/**
    310  * Check if at least one service should be started.
    311  */
    312 static bool VBoxServiceCheckStartedServices(void)
    313 {
    314     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    315         if (g_aServices[j].fEnabled)
    316             return true;
    317 
    318    return false;
     310 * Count the number of enabled services.
     311 */
     312static unsigned vboxServiceCountEnabledServices(void)
     313{
     314    unsigned cEnabled = 0;
     315    for (unsigned i = 0; i < RT_ELEMENTS(g_aServices); i++)
     316        cEnabled += g_aServices[i].fEnabled;
     317   return cEnabled;
    319318}
    320319
     
    365364
    366365        VBoxServiceVerbose(2, "Starting service     '%s' ...\n", g_aServices[j].pDesc->pszName);
    367         rc = RTThreadCreate(&g_aServices[j].Thread, VBoxServiceThread, (void *)(uintptr_t)j, 0,
     366        rc = RTThreadCreate(&g_aServices[j].Thread, vboxServiceThread, (void *)(uintptr_t)j, 0,
    368367                            RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName);
    369368        if (RT_FAILURE(rc))
     
    585584
    586585    /*
    587      * Parse the arguments.
     586     * Parse the arguments                                       .
     587     *                                                           .
     588     * Note! This code predates RTGetOpt, thus the manual parsing.
    588589     */
    589590    bool fDaemonize = true;
     
    682683                case 'h':
    683684                case '?':
    684                     return VBoxServiceUsage();
     685                    return vboxServiceUsage();
    685686
    686687#ifdef RT_OS_WINDOWS
     
    716717    }
    717718
     719    /* Check that at least one service is enabled. */
     720    if (vboxServiceCountEnabledServices() == 0)
     721        return VBoxServiceSyntax("At least one service must be enabled.\n");
     722
    718723#ifdef RT_OS_WINDOWS
    719724    /*
    720725     * Make sure only one instance of VBoxService runs at a time.  Create a
    721726     * global mutex for that.
     727     *
     728     * Note! The \\Global\ namespace was introduced with Win2K, thus the
     729     *       version check.
     730     * Note! If the mutex exists CreateMutex will open it and set last error to
     731     *       ERROR_ALREADY_EXISTS.
    722732     */
    723733    OSVERSIONINFOEX OSInfoEx;
     
    725735    OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    726736
     737    SetLastError(NO_ERROR);
    727738    HANDLE hMutexAppRunning;
    728     if (    GetVersionEx((LPOSVERSIONINFO) &OSInfoEx)
     739    if (    GetVersionEx((LPOSVERSIONINFO)&OSInfoEx)
    729740        &&  OSInfoEx.dwPlatformId == VER_PLATFORM_WIN32_NT
    730741        &&  OSInfoEx.dwMajorVersion >= 5 /* NT 5.0 a.k.a W2K */)
    731     {
    732742        hMutexAppRunning = CreateMutex(NULL, FALSE, VBOXSERVICE_NAME_GLOBAL);
    733     }
    734743    else
    735     {
    736         /* On older Windows OSes (like NT4) don't use the global namespace
    737          * needed for terminal servers on Win2K+. */
    738744        hMutexAppRunning = CreateMutex(NULL, FALSE, VBOXSERVICE_NAME);
    739     }
    740     if (   hMutexAppRunning != NULL
    741         && GetLastError() == ERROR_ALREADY_EXISTS)
     745    if (hMutexAppRunning == NULL)
     746    {
     747        VBoxServiceError("CreateMutex failed with last error %u! Terminating", GetLastError());
     748        return RTEXITCODE_FAILURE;
     749    }
     750    if (GetLastError() == ERROR_ALREADY_EXISTS)
    742751    {
    743752        VBoxServiceError("%s is already running! Terminating.", g_pszProgName);
    744 
    745         /* Close the mutex for this application instance. */
    746753        CloseHandle(hMutexAppRunning);
    747         hMutexAppRunning = NULL;
    748 
    749754        return RTEXITCODE_FAILURE;
    750755    }
    751 #else /* !RT_OS_WINDOWS */
    752     /** @todo Add PID file creation here. */
    753 #endif /* RT_OS_WINDOWS */
    754 
    755     /*
    756      * Check that at least one service is enabled.
    757      */
    758     if (!VBoxServiceCheckStartedServices())
    759         return VBoxServiceSyntax("At least one service must be enabled.\n");
     756#else  /* !RT_OS_WINDOWS */
     757    /** @todo Add PID file creation here? */
     758#endif /* !RT_OS_WINDOWS */
    760759
    761760    VBoxServiceVerbose(0, "%s r%s started. Verbose level = %d\n",
     
    807806#ifdef RT_OS_WINDOWS
    808807    /*
    809      * Release instance mutex if we got it.
    810      */
    811     if (hMutexAppRunning != NULL)
    812     {
    813         ::CloseHandle(hMutexAppRunning);
    814         hMutexAppRunning = NULL;
    815     }
     808     * Cleanup mutex.
     809     */
     810    CloseHandle(hMutexAppRunning);
    816811#endif
    817812
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