VirtualBox

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


Ignore:
Timestamp:
Apr 21, 2009 3:13:05 PM (16 years ago)
Author:
vboxsync
Message:

VBoxService/common: First time synchronization code for Windows platforms.

Location:
trunk/src/VBox/Additions/common/VBoxService
Files:
2 edited

Legend:

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

    r19031 r19084  
    549549        rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
    550550        if (RT_FAILURE(rc))
    551             return VBoxServiceError("daemon failed: %Rrc\n", rc);
     551            return VBoxServiceError("Daemon failed: %Rrc\n", rc);
    552552        /* in-child */
    553553#endif
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceTimeSync.cpp

    r19031 r19084  
    130130static HANDLE g_hTokenProcess = NULL;
    131131/** Old token privileges. */
    132 static TOKEN_PRIVILEGES g_tkOldPrivileges;
     132static TOKEN_PRIVILEGES g_TkOldPrivileges;
     133/** Backup values for time adjustment. */
     134static DWORD g_dwWinTimeAdjustment;
     135static DWORD g_dwWinTimeIncrement;
     136static BOOL g_bWinTimeAdjustmentDisabled;
    133137#endif
    134138
     
    191195            if (LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkPriv.Privileges[0].Luid))
    192196            {
    193                 DWORD cbRet = sizeof(g_tkOldPrivileges);
    194                 if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &tkPriv, sizeof(TOKEN_PRIVILEGES), &g_tkOldPrivileges, &cbRet))
     197                DWORD cbRet = sizeof(g_TkOldPrivileges);
     198                if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &tkPriv, sizeof(TOKEN_PRIVILEGES), &g_TkOldPrivileges, &cbRet))
    195199                {
    196200                    DWORD dwErr = GetLastError();
     
    220224        }
    221225    }
     226
     227    if (!::GetSystemTimeAdjustment(&g_dwWinTimeAdjustment, &g_dwWinTimeIncrement, &g_bWinTimeAdjustmentDisabled))
     228    {
     229        DWORD dwErr = GetLastError();
     230        rc = RTErrConvertFromWin32(dwErr);
     231        VBoxServiceError("Could not get time adjustment values! Last error: %ld!\n", dwErr);
     232    }
     233    else VBoxServiceVerbose(3, "Windows time adjustment: Initially %ld (100ns) units per %ld (100 ns) units interval, disabled=%d\n",
     234                            g_dwWinTimeAdjustment, g_dwWinTimeIncrement, g_bWinTimeAdjustmentDisabled ? 1 : 0);
    222235#endif /* RT_OS_WINDOWS */
    223236
     
    289302                     */
    290303#ifdef RT_OS_WINDOWS
    291                     /* Just make sure it compiles for now, but later:
    292                      SetSystemTimeAdjustment and fall back on SetSystemTime.
    293                      */
    294                     //AssertFatalFailed();
     304                    DWORD dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwWinTimeIncrement;
     305                    BOOL bWinTimeAdjustmentDisabled;
     306                    if (!::GetSystemTimeAdjustment(&dwWinTimeAdjustment, &dwWinTimeIncrement, &bWinTimeAdjustmentDisabled))
     307                    {
     308                        VBoxServiceError("GetSystemTimeAdjustment failed, error=%ld\n", GetLastError());
     309                    }
     310                    else
     311                    {
     312                        DWORD dwDiffMax = g_dwWinTimeAdjustment * 0.50;
     313                        DWORD dwDiffNew = dwWinTimeAdjustment * 0.10;
     314
     315                        if (RTTimeSpecGetMilli(&Drift) > 0)
     316                        {
     317                            dwWinNewTimeAdjustment = dwWinTimeAdjustment + dwDiffNew;
     318                            if (dwWinNewTimeAdjustment > (g_dwWinTimeAdjustment + dwDiffMax))
     319                            {   
     320                                dwWinNewTimeAdjustment = g_dwWinTimeAdjustment + dwDiffMax;
     321                                dwDiffNew = dwDiffMax;
     322                            }
     323                        }
     324                        else
     325                        {
     326                            dwWinNewTimeAdjustment = dwWinTimeAdjustment - dwDiffNew;
     327                            if (dwWinNewTimeAdjustment < (g_dwWinTimeAdjustment - dwDiffMax))
     328                            {   
     329                                dwWinNewTimeAdjustment = g_dwWinTimeAdjustment - dwDiffMax;
     330                                dwDiffNew = dwDiffMax;
     331                            }
     332                        }
     333
     334                        VBoxServiceVerbose(3, "Windows time adjustment: Drift=%ldms\n", RTTimeSpecGetMilli(&Drift));
     335                        VBoxServiceVerbose(3, "Windows time adjustment: OrgTA=%ld, CurTA=%ld, NewTA=%ld, DiffNew=%ld, DiffMax=%ld\n",
     336                                           g_dwWinTimeAdjustment, dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwDiffNew, dwDiffMax);
     337                       
     338                        if (!::SetSystemTimeAdjustment(dwWinNewTimeAdjustment, FALSE /* Periodic adjustments enabled. */))
     339                        {   
     340                            VBoxServiceError("SetSystemTimeAdjustment failed, error=%ld\n", GetLastError());       
     341                        }
     342                    }
     343
    295344#else  /* !RT_OS_WINDOWS */
    296345                    struct timeval tv;
     
    331380#endif /* !RT_OS_WINDOWS */
    332381                }
     382                else
     383                {
     384#ifdef RT_OS_WINDOWS
     385                    if (::SetSystemTimeAdjustment(0, TRUE /* Periodic adjustments disabled. */))
     386                        VBoxServiceVerbose(3, "Windows Time Adjustment is now disabled.");
     387#endif /* !RT_OS_WINDOWS */
     388                }
    333389                break;
    334390            }
     
    378434    if (g_hTokenProcess)
    379435    {
    380         if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &g_tkOldPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
     436        if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &g_TkOldPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
    381437        {
    382438            DWORD dwErr = GetLastError();
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