VirtualBox

Changeset 18712 in vbox for trunk/src


Ignore:
Timestamp:
Apr 3, 2009 8:15:26 PM (16 years ago)
Author:
vboxsync
Message:

VBoxService.cpp: Undid damange inflicted earlier today:

  • Don't break the code!
    • The iMain test and adjustment in VBoxServiceStartServices was completely wrong (unless I'm mistaken), so were the type.
    • Mixing status codes and exit codes => busted error handling.
  • You cannot use a va_list more than once.
  • Don't initialize big buffers without any good reason.
  • Don't ever pass a string buffer as a format string (LogRel in this case).
  • Don't mess up the syntax diagram.
  • Don't remove blank lines, they are not there by accident usually.
  • Don't change preprocessor indentiation or tests (for the same reason).
  • Finally, do not make me wast hours cleaing up the code (I should've just backed it all out).
Location:
trunk/src/VBox/Additions/common/VBoxService
Files:
3 edited

Legend:

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

    r18672 r18712  
    1 /** $Id$ */
     1/* $Id$ */
    22/** @file
    33 * VBoxService - Guest Additions Service Skeleton.
     
    55
    66/*
    7  * Copyright (C) 2009 Sun Microsystems, Inc.
     7 * Copyright (C) 2007-2009 Sun Microsystems, Inc.
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    2525*   Header Files                                                               *
    2626*******************************************************************************/
     27/** @todo LOG_GROUP*/
    2728#ifndef _MSC_VER
    2829# include <unistd.h>
     
    4041#include "VBoxServiceInternal.h"
    4142
     43
    4244/*******************************************************************************
    4345*   Global Variables                                                           *
    4446*******************************************************************************/
    4547/** The program name (derived from argv[0]). */
    46 char *g_pszProgName;
     48char *g_pszProgName =  (char *)"";
    4749/** The current verbosity level. */
    4850int g_cVerbosity = 0;
    4951/** The default service interval (the -i | --interval) option). */
    5052uint32_t g_DefaultInterval = 0;
    51 /** Shutdown the main thread. (later, for signals). */
     53/** Shutdown the main thread. (later, for signals.) */
    5254bool volatile g_fShutdown;
    5355
     
    9092static int VBoxServiceUsage(void)
    9193{
    92     RTPrintf("usage: %s [-f|--foreground] [-i|--interval <seconds>]"
    93              " [--disable-<service>] [--enable-<service>] [-h|-?|--help] [-v|--verbose]", g_pszProgName);
    94 
    95 #if defined(RT_OS_WINDOWS)
    96     RTPrintf(" [-r|--register] [-u|--unregister]");
    97 #endif
    98 
    99     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    100         RTPrintf(" %s\n", g_aServices[j].pDesc->pszUsage);
    101 
     94    RTPrintf("usage: %s [-f|--foreground] [-v|--verbose] [-i|--interval <seconds>]\n"
     95             "           [--disable-<service>] [--enable-<service>] [-h|-?|--help]\n", g_pszProgName);
     96#ifdef RT_OS_WINDOWS
     97    RTPrintf("           [-r|--register] [-u|--unregister]\n");
     98#endif
     99    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
     100        RTPrintf("           %s\n", g_aServices[j].pDesc->pszUsage);
    102101    RTPrintf("\n"
    103102             "Options:\n"
    104 #if !defined(RT_OS_WINDOWS)
    105              "    -h | -? | --help         Show this message and exit with status 1.\n"
    106 #else
    107              "    -h | -? | /? | --help    Show this message and exit with status 1.\n"
    108 #endif
    109103             "    -i | --interval          The default interval.\n"
    110104             "    -f | --foreground        Don't daemonzie the program. For debugging.\n"
    111 #if defined(RT_OS_WINDOWS)
    112              "    -r | --register          Installs the service.\n"
    113              "    -u | --unregister        Uninstall service.\n"
    114 #endif
    115              "    -v | --verbose           Increment the verbosity level. For debugging.\n");
     105             "    -v | --verbose           Increment the verbosity level. For debugging.\n"
     106             "    -h | -? | --help         Show this message and exit with status 1.\n"
     107             );
     108#ifdef RT_OS_WINDOWS
     109    RTPrintf("    -r | --register          Installs the service.\n"
     110             "    -u | --unregister        Uninstall service.\n");
     111#endif
    116112
    117113    RTPrintf("\n"
     
    159155int VBoxServiceError(const char *pszFormat, ...)
    160156{
    161     char szBuffer[1024] = { 0 };
    162157    RTStrmPrintf(g_pStdErr, "%s: error: ", g_pszProgName);
    163158
     
    165160    va_start(va, pszFormat);
    166161    RTStrmPrintfV(g_pStdErr, pszFormat, va);
    167 
    168     RTStrPrintfV(szBuffer, sizeof(szBuffer), pszFormat, va);
    169     LogRel((szBuffer));
    170 
    171162    va_end(va);
     163
     164#if 0 /* enable after 2.2 */
     165    va_start(va, pszFormat);
     166    LogRel(("%s: error: %N", g_pszProgName, pszFormat, &va));
     167    va_end(va);
     168#endif
    172169
    173170    return 1;
     
    190187        va_start(va, pszFormat);
    191188        RTStrmPrintfV(g_pStdOut, pszFormat, va);
    192 
    193 #ifdef _DEBUG
    194         char szBuffer[1024] = { 0 };
    195         RTStrPrintfV(szBuffer, sizeof(szBuffer), pszFormat, va);
    196         LogRel((szBuffer));
    197 #endif
    198 
    199189        va_end(va);
     190
     191#if 0 /* enable after 2.2 */
     192        Log(("%s: %N", g_pszProgName, pszFormat, &va));
     193#endif
    200194    }
    201195}
     
    252246}
    253247
    254 int VBoxServiceStartServices(int iMain)
    255 {
    256     int rc = 0;
     248
     249/**
     250 * Starts the service.
     251 *
     252 * @returns VBox status code, errors are fully bitched.
     253 *
     254 * @param   iMain           The index of the service that belongs to the main
     255 *                          thread. Pass ~0U if none does.
     256 */
     257int VBoxServiceStartServices(unsigned iMain)
     258{
     259    int rc;
    257260
    258261    /*
     
    263266        rc = g_aServices[j].pDesc->pfnInit();
    264267        if (RT_FAILURE(rc))
    265             return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
    266     }
    267 
    268     if (iMain == 0) /* If 0 is specified, start all services. Useful for the Windows SCM stuff. */
    269         iMain = RT_ELEMENTS(g_aServices);
     268        {
     269            VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
     270            return rc;
     271        }
     272    }
    270273
    271274    /*
    272275     * Start the service(s).
    273276     */
     277    rc = VINF_SUCCESS;
    274278    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    275279    {
     
    303307}
    304308
    305 /*
     309
     310/**
    306311 * Stops and terminates the services.
    307  */
    308 int VBoxServiceStopServices()
    309 {
    310     int rc = 0;
     312 *
     313 * This should be called even when VBoxServiceStartServices fails so it can
     314 * clean up anything that we succeeded in starting.
     315 */
     316void VBoxServiceStopServices(void)
     317{
    311318    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    312319        ASMAtomicXchgBool(&g_aServices[j].fShutdown, true);
     
    318325        if (g_aServices[j].Thread != NIL_RTTHREAD)
    319326        {
    320             rc = RTThreadWait(g_aServices[j].Thread, 30*1000, NULL);
     327            int rc = RTThreadWait(g_aServices[j].Thread, 30*1000, NULL);
    321328            if (RT_FAILURE(rc))
    322329                VBoxServiceError("service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
     
    324331        g_aServices[j].pDesc->pfnTerm();
    325332    }
    326 
    327     return rc;
    328 }
     333}
     334
    329335
    330336int main(int argc, char **argv)
    331337{
    332     int rc = 0;
    333 
    334 #if defined(RT_OS_WINDOWS)
     338    int rc;
     339
     340    /*
     341     * Init globals and such.
     342     */
     343    RTR3Init();
     344    g_pszProgName = RTPathFilename(argv[0]);
     345    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
     346    {
     347        rc = g_aServices[j].pDesc->pfnPreInit();
     348        if (RT_FAILURE(rc))
     349            return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
     350    }
     351
     352#ifdef RT_OS_WINDOWS
     353    /*
     354     * Explain the purpose of this exercise and why ignoring the result is a good idea and everything.
     355     */
    335356    /* Do not use a global namespace ("Global\\") for mutex name here, will blow up NT4 compatibility! */
    336357    HANDLE hMutexAppRunning = CreateMutex (NULL, FALSE, VBOXSERVICE_NAME);
    337     if (   (hMutexAppRunning != NULL)
    338         && (GetLastError() == ERROR_ALREADY_EXISTS))
     358    if (   hMutexAppRunning != NULL
     359        && GetLastError() == ERROR_ALREADY_EXISTS)
    339360    {
    340361        VBoxServiceError("%s is already running! Terminating.", g_pszProgName);
     
    347368
    348369    /*
    349      * Init globals and such.
    350      */
    351     RTR3Init();
    352     g_pszProgName = RTPathFilename(argv[0]);
    353     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    354     {
    355         rc = g_aServices[j].pDesc->pfnPreInit();
    356         if (RT_FAILURE(rc))
    357             return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
    358     }
    359 
    360     /*
    361370     * Parse the arguments.
    362371     */
     
    366375    {
    367376        const char *psz = argv[i];
    368         if(    (*psz != '-')
    369 #if defined(RT_OS_WINDOWS)
    370             && (*psz != '/')
    371 #endif
    372           )
     377        if (*psz != '-')
    373378            return VBoxServiceSyntax("Unknown argument '%s'\n", psz);
    374379        psz++;
     
    389394            else if (MATCHES("interval"))
    390395                psz = "i";
    391 #if defined(RT_OS_WINDOWS)
     396#ifdef RT_OS_WINDOWS
    392397            else if (MATCHES("register"))
    393398                psz = "r";
     
    452457                    break;
    453458
    454 #if defined(RT_OS_WINDOWS)
     459                case 'h':
     460                case '?':
     461                    return VBoxServiceUsage();
     462
     463#ifdef RT_OS_WINDOWS
    455464                case 'r':
    456465                    return VBoxServiceWinInstall();
     
    459468                    return VBoxServiceWinUninstall();
    460469#endif
    461 
    462                 case 'h':
    463 #if defined(RT_OS_WINDOWS)
    464                 case '?':
    465 #endif
    466                     return VBoxServiceUsage();
    467470
    468471                default:
     
    513516    if (fDaemonize && !fDaemonzied)
    514517    {
     518#ifdef RT_OS_WINDOWS
     519        /** @todo Should do something like VBoxSVC here, OR automatically re-register
     520         *        the service and start it. Involving VbglR3Daemonize isn't an option
     521         *        here.
     522         *
     523         *        Also, the idea here, IIRC, was to map the sub service to windows
     524         *        services. The todo below is for mimicking windows services on
     525         *        non-windows systems. Not sure if this is doable or not, but in anycase
     526         *        this code can be moved into -win.
     527         *
     528         *        You should return when StartServiceCtrlDispatcher, btw., not
     529         *        continue.
     530         */
     531        if (!StartServiceCtrlDispatcher(&g_aServiceTable[0]))
     532            return VBoxServiceError("StartServiceCtrlDispatcher: %u\n", GetLastError());
     533
     534#else
    515535        VBoxServiceVerbose(1, "Daemonizing...\n");
    516 #if defined(RT_OS_WINDOWS)
    517         /** @todo Replace StartServiceCtrlDispatcher() with
    518                   VbglR3Daemonize() once this has been ported
    519                   to Windows later. */
    520         StartServiceCtrlDispatcher (gs_serviceTable);
    521 #else
    522536        rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
    523537        if (RT_FAILURE(rc))
     
    529543/** @todo Make the main thread responsive to signal so it can shutdown/restart the threads on non-SIGKILL signals. */
    530544
    531     rc = VBoxServiceStartServices(iMain);
    532 
    533 #if defined(RT_OS_WINDOWS)
    534     if (fDaemonize && fDaemonzied)
    535     {
    536         while (1) {
    537             Sleep (100);       /** @todo */
    538         }
    539     }
    540 #endif
    541 
    542     rc = VBoxServiceStopServices();
    543 
    544 #if defined(RT_OS_WINDOWS)
    545     /* Release instance mutex. */
    546     if (hMutexAppRunning != NULL) {
    547         CloseHandle (hMutexAppRunning);
    548         hMutexAppRunning = NULL;
    549     }
    550 #endif
    551     return rc;
    552 }
    553 
     545#ifdef RT_OS_WINDOWS
     546    if (not using StartServiceCtrlDispatcher or auto register & starting the service)
     547#endif
     548    {
     549        /*
     550         * Start the service, enter the main threads run loop and stop them again when it returns.
     551         */
     552        rc = VBoxServiceStartServices(iMain);
     553        VBoxServiceStopServices();
     554    }
     555
     556
     557#ifdef RT_OS_WINDOWS
     558    /*
     559     * Release instance mutex if we got it.
     560     */
     561    if (hMutexAppRunning != NULL)
     562        CloseHandle(hMutexAppRunning);
     563#endif
     564
     565    return RT_SUCCESS(rc) ? 0 : 1;
     566}
     567
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceInternal.h

    r18670 r18712  
    55
    66/*
    7  * Copyright (C) 2009 Sun Microsystems, Inc.
     7 * Copyright (C) 2007-2009 Sun Microsystems, Inc.
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    2323#define ___VBoxServiceInternal_h
    2424
    25 #if defined(RT_OS_WINDOWS)
    26     #include <windows.h>
    27     #include <tchar.h>
    28     #include <process.h>
     25#ifdef RT_OS_WINDOWS
     26# include <Windows.h>
     27# include <tchar.h>   /**@todo just drop this, this will be compiled as UTF-8/ANSI. */
     28# include <process.h> /**@todo what's this here for?  */
    2929#endif
    3030
    31 /** Name of this program. */
    32 #if !defined(RT_OS_WINDOWS)
    33     #define VBOXSERVICE_NAME "VBoxService"
    34     #define VBOXSERVICE_FRIENDLY_NAME "VBoxService"
    35 #else
    36     #define VBOXSERVICE_NAME _T("VBoxService")
    37     #define VBOXSERVICE_FRIENDLY_NAME _T("VBoxService")
    38 #endif
     31/** @todo just move this into the windows specific code, it's not needed
     32 *        here. */
     33/** The service name. */
     34#define VBOXSERVICE_NAME           "VBoxService"
     35/** The friendly service name. */
     36#define VBOXSERVICE_FRIENDLY_NAME  "VBoxService"
    3937
    4038/**
     
    106104extern int g_cVerbosity;
    107105extern uint32_t g_DefaultInterval;
     106/** Windows SCM stuff.
     107 *  @todo document each of them individually, this comment only documents
     108 *  g_vboxServiceStatusCode on windows. On the other platforms it will be
     109 *  dangling.
     110 *  @todo all this should be moved to -win.cpp and exposed via functions. */
     111#ifdef RT_OS_WINDOWS
     112extern DWORD                 g_vboxServiceStatusCode;   /** @todo g_rcWinService */
     113extern SERVICE_STATUS_HANDLE g_vboxServiceStatusHandle; /** @todo g_hWinServiceStatus */
     114extern SERVICE_TABLE_ENTRY const g_aServiceTable[];     /** @todo generate on the fly, see comment in main() from the enabled sub services. */
     115#endif
    108116
    109117extern int VBoxServiceSyntax(const char *pszFormat, ...);
     
    111119extern void VBoxServiceVerbose(int iLevel, const char *pszFormat, ...);
    112120extern int VBoxServiceArgUInt32(int argc, char **argv, const char *psz, int *pi, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max);
    113 extern int VBoxServiceStartServices(int iMain);
    114 extern int VBoxServiceStopServices();
    115 
    116 #if defined(RT_OS_WINDOWS)
    117     extern int VBoxServiceWinInstall ();
    118     extern int VBoxServiceWinUninstall ();
     121extern int VBoxServiceStartServices(unsigned iMain);
     122extern void VBoxServiceStopServices(void);
     123#ifdef RT_OS_WINDOWS
     124extern int VBoxServiceWinInstall(void);
     125extern int VBoxServiceWinUninstall(void);
    119126#endif
    120127
     
    123130extern VBOXSERVICE g_Control;
    124131
    125 /** Windows SCM stuff. */
    126 #if defined(RT_OS_WINDOWS)
    127     extern DWORD                 g_vboxServiceStatusCode;
    128     extern SERVICE_STATUS_HANDLE g_vboxServiceStatusHandle;
    129     extern SERVICE_TABLE_ENTRY const gs_serviceTable[];
    130 #endif
    131 
    132132__END_DECLS
    133133
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceTimeSync.cpp

    r18679 r18712  
    116116static uint32_t g_TimeSyncMinAdjust = 1000;
    117117#else
    118  #if RT_OS_WINDOWS
    119   /** Process token. */
    120   static HANDLE g_hTokenProcess = NULL;
    121   /* Old token privileges. */
    122   static TOKEN_PRIVILEGES g_tpOld;
    123   static uint32_t g_TimeSyncMinAdjust = 100;
    124  #else
    125   static uint32_t g_TimeSyncMinAdjust = 100;
    126  #endif
     118static uint32_t g_TimeSyncMinAdjust = 100;
    127119#endif
    128120/** @see pg_vboxservice_timesync */
     
    133125/** The semaphore we're blocking on. */
    134126static RTSEMEVENTMULTI g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
     127
     128#ifdef RT_OS_WINDOWS
     129/** Process token. */
     130static HANDLE g_hTokenProcess = NULL;
     131/** Old token privileges. */
     132static TOKEN_PRIVILEGES g_OldTokenPrivileges;
     133#endif
     134
    135135
    136136/** @copydoc VBOXSERVICE::pfnPreInit */
     
    166166static DECLCALLBACK(int) VBoxServiceTimeSyncInit(void)
    167167{
    168     int rc = VINF_SUCCESS;
    169 
    170168    /*
    171169     * If not specified, find the right interval default.
     
    177175        g_TimeSyncInterval = 10 * 1000;
    178176
    179     rc = RTSemEventMultiCreate(&g_TimeSyncEvent);
     177    int rc = RTSemEventMultiCreate(&g_TimeSyncEvent);
    180178    AssertRC(rc);
    181 
    182 #if defined(RT_OS_WINDOWS)
    183     /* Adjust priviledges of this process to adjust the time. */
    184     TOKEN_PRIVILEGES tp; /* Token provileges. */
    185     DWORD dwSize = sizeof (TOKEN_PRIVILEGES);
    186     LUID luid = {0};
    187 
    188     if (!OpenProcessToken(GetCurrentProcess(),
    189                           TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &g_hTokenProcess))
     179#ifdef RT_OS_WINDOWS
     180    if (RT_SUCCESS(rc))
    190181    {
    191         VBoxServiceError("Opening process token (SE_SYSTEMTIME_NAME) failed with code %ld!\n", GetLastError());
    192         rc = VERR_PERMISSION_DENIED;
    193     }
    194     else
    195     {
    196         if(!::LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &luid))
     182        /*
     183         * Adjust priviledges of this process so we can make system time adjustments.
     184         */
     185        if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &g_hTokenProcess))
    197186        {
    198             VBoxServiceError("Looking up token privileges (SE_SYSTEMTIME_NAME) failed with code %ld!\n", GetLastError());
    199             rc = VERR_PERMISSION_DENIED;
     187            TOKEN_PRIVILEGES Tp;
     188            RT_ZERO(Tp);
     189            tp.PrivilegeCount = 1;
     190            tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
     191            if (LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tp.Privileges[0].Luid))
     192            {
     193                DWORD cbRet = sizeof(g_OldTokenPrivileges);
     194                if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), &g_OldTokenPrivileges, &cbRet))
     195                {
     196                    DWORD dwErr = GetLastError();
     197                    rc = RTErrConvertFromWin32(dwErr);
     198                    VBoxServiceError("Adjusting token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
     199                }
     200            }
     201            else
     202            {
     203                DWORD dwErr = GetLastError();
     204                rc = RTErrConvertFromWin32(dwErr);
     205                VBoxServiceError("Looking up token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
     206            }
     207
     208            if (RT_FAILURE(rc))
     209            {
     210                CloseHandle(g_hTokenProcess);
     211                g_hTokenProcess = NULL;
     212            }
    200213        }
    201 
    202         ZeroMemory (&tp, sizeof (tp));
    203         tp.PrivilegeCount = 1;
    204         tp.Privileges[0].Luid = luid;
    205         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    206 
    207         /* Adjust Token privileges. */
    208         if (!::AdjustTokenPrivileges(g_hTokenProcess, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
    209                  &g_tpOld, &dwSize))
     214        else
    210215        {
    211            VBoxServiceError("Adjusting token privileges (SE_SYSTEMTIME_NAME) failed with code %ld!\n", GetLastError());
    212            rc = VERR_PERMISSION_DENIED;
     216            DWORD dwErr = GetLastError();
     217            rc = RTErrConvertFromWin32(dwErr);
     218            VBoxServiceError("Opening process token (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
     219            g_hTokenProcess = NULL;
    213220        }
    214221    }
    215 #else
    216     /* Nothing to do here yet. */
    217 #endif
    218 
    219 #if defined(RT_OS_WINDOWS)
    220     if (RT_FAILURE(rc))
    221     {
    222         CloseHandle (g_hTokenProcess);
    223         g_hTokenProcess = NULL;
    224     }
    225 #endif
     222#endif /* RT_OS_WINDOWS */
    226223
    227224    return rc;
     
    291288                     * *NIX systems have it. Fall back on settimeofday.
    292289                     */
    293 #if defined(RT_OS_WINDOWS)
    294 
    295 
     290#ifdef RT_OS_WINDOWS
    296291                    /* Just make sure it compiles for now, but later:
    297292                     SetSystemTimeAdjustment and fall back on SetSystemTime.
    298293                     */
    299294                    //AssertFatalFailed();
    300 #else
     295#else  /* !RT_OS_WINDOWS */
    301296                    struct timeval tv;
    302 #if !defined(RT_OS_OS2) /* PORTME */
     297# if !defined(RT_OS_OS2) /* PORTME */
    303298                    RTTimeSpecGetTimeval(&Drift, &tv);
    304299                    if (adjtime(&tv, NULL) == 0)
     
    309304                    }
    310305                    else
    311 #endif
     306# endif
    312307                    {
    313308                        errno = 0;
     
    321316                                    VBoxServiceVerbose(1, "settimeofday to %s\n",
    322317                                                       RTTimeToString(RTTimeExplode(&Time, &Tmp), sz, sizeof(sz)));
    323 #ifdef DEBUG
     318# ifdef DEBUG
    324319                                if (g_cVerbosity >= 3)
    325320                                    VBoxServiceVerbose(2, "       new time %s\n",
    326321                                                       RTTimeToString(RTTimeExplode(&Time, RTTimeNow(&Tmp)), sz, sizeof(sz)));
    327 #endif
     322# endif
    328323                                cErrors = 0;
    329324                            }
     
    377372static DECLCALLBACK(void) VBoxServiceTimeSyncTerm(void)
    378373{
    379 #if defined(RT_OS_WINDOWS)
    380     /* Disable SE_SYSTEMTIME_NAME again. */
    381     DWORD dwSize = sizeof (TOKEN_PRIVILEGES);
    382     if (g_hTokenProcess && !::AdjustTokenPrivileges(g_hTokenProcess, FALSE, &g_tpOld, dwSize, NULL, NULL))
    383         VBoxServiceError("Adjusting back token privileges (SE_SYSTEMTIME_NAME) failed with code %ld!\n", GetLastError());
     374#ifdef RT_OS_WINDOWS
     375    /*
     376     * Restore the SE_SYSTEMTIME_NAME token privileges (if init succeeded).
     377     */
     378    if (g_hTokenProcess)
     379    {
     380        if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &g_tpOld, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
     381        {
     382            DWORD dwErr = GetLastError();
     383            VBoxServiceError("Restoring token privileges (SE_SYSTEMTIME_NAME) failed with code %u!\n", dwErr);
     384        }
     385        CloseHandle(g_hTokenProcess);
     386        g_hTokenProcess = NULL;
     387    }
    384388#endif
    385389
    386     RTSemEventMultiDestroy(g_TimeSyncEvent);
    387     g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
     390    if (g_TimeSyncEvent != NIL_RTSEMEVENTMULTI)
     391    {
     392        RTSemEventMultiDestroy(g_TimeSyncEvent);
     393        g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
     394    }
    388395}
    389396
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