- Timestamp:
- Jul 2, 2009 12:58:57 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxServiceTimeSync.cpp
r19374 r21157 198 198 { 199 199 DWORD cbRet = sizeof(g_TkOldPrivileges); 200 if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &tkPriv, sizeof(TOKEN_PRIVILEGES), &g_TkOldPrivileges, &cbRet)) 200 if (AdjustTokenPrivileges(g_hTokenProcess, FALSE, &tkPriv, sizeof(TOKEN_PRIVILEGES), &g_TkOldPrivileges, &cbRet)) 201 rc = VINF_SUCCESS; 202 else 201 203 { 202 204 DWORD dwErr = GetLastError(); … … 211 213 VBoxServiceError("Looking up token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc); 212 214 } 213 214 215 if (RT_FAILURE(rc)) 215 216 { … … 227 228 } 228 229 229 if (!::GetSystemTimeAdjustment(&g_dwWinTimeAdjustment, &g_dwWinTimeIncrement, &g_bWinTimeAdjustmentDisabled)) 230 if (GetSystemTimeAdjustment(&g_dwWinTimeAdjustment, &g_dwWinTimeIncrement, &g_bWinTimeAdjustmentDisabled)) 231 VBoxServiceVerbose(3, "Windows time adjustment: Initially %ld (100ns) units per %ld (100 ns) units interval, disabled=%d\n", 232 g_dwWinTimeAdjustment, g_dwWinTimeIncrement, g_bWinTimeAdjustmentDisabled ? 1 : 0); 233 else 230 234 { 231 235 DWORD dwErr = GetLastError(); … … 233 237 VBoxServiceError("Could not get time adjustment values! Last error: %ld!\n", dwErr); 234 238 } 235 else VBoxServiceVerbose(3, "Windows time adjustment: Initially %ld (100ns) units per %ld (100 ns) units interval, disabled=%d\n",236 g_dwWinTimeAdjustment, g_dwWinTimeIncrement, g_bWinTimeAdjustmentDisabled ? 1 : 0);237 239 #endif /* RT_OS_WINDOWS */ 238 240 … … 249 251 250 252 /* 251 * Tell the control thread that it can continue 252 * spawning services. 253 * Tell the control thread that it can continue spawning services. 253 254 */ 254 255 RTThreadUserSignal(RTThreadSelf()); 255 256 257 /* 258 * The Work Loop. 259 */ 256 260 unsigned cErrors = 0; 257 261 for (;;) … … 303 307 } 304 308 309 /** @todo move the adjustment code into two new functions, too much code 310 * here. */ 305 311 uint32_t AbsDriftMilli = RTTimeSpecGetMilli(&AbsDrift); 306 312 if (AbsDriftMilli > MinAdjust) … … 313 319 #ifdef RT_OS_WINDOWS 314 320 DWORD dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwWinTimeIncrement; 315 BOOL bWinTimeAdjustmentDisabled; 316 if (!::GetSystemTimeAdjustment(&dwWinTimeAdjustment, &dwWinTimeIncrement, &bWinTimeAdjustmentDisabled)) 317 { 318 VBoxServiceError("GetSystemTimeAdjustment failed, error=%ld\n", GetLastError()); 319 } 320 else 321 BOOL fWinTimeAdjustmentDisabled; 322 /** @todo r=bird: NT4 doesn't have GetSystemTimeAdjustment. */ 323 if (GetSystemTimeAdjustment(&dwWinTimeAdjustment, &dwWinTimeIncrement, &fWinTimeAdjustmentDisabled)) 321 324 { 322 325 DWORD dwDiffMax = g_dwWinTimeAdjustment * 0.50; 323 DWORD dwDiffNew = dwWinTimeAdjustment * 0.10;326 DWORD dwDiffNew = dwWinTimeAdjustment * 0.10; 324 327 325 328 if (RTTimeSpecGetMilli(&Drift) > 0) … … 327 330 dwWinNewTimeAdjustment = dwWinTimeAdjustment + dwDiffNew; 328 331 if (dwWinNewTimeAdjustment > (g_dwWinTimeAdjustment + dwDiffMax)) 329 { 332 { 330 333 dwWinNewTimeAdjustment = g_dwWinTimeAdjustment + dwDiffMax; 331 334 dwDiffNew = dwDiffMax; 332 335 } 333 336 } 334 else 337 else 335 338 { 336 339 dwWinNewTimeAdjustment = dwWinTimeAdjustment - dwDiffNew; 337 340 if (dwWinNewTimeAdjustment < (g_dwWinTimeAdjustment - dwDiffMax)) 338 { 341 { 339 342 dwWinNewTimeAdjustment = g_dwWinTimeAdjustment - dwDiffMax; 340 343 dwDiffNew = dwDiffMax; … … 342 345 } 343 346 344 VBoxServiceVerbose(3, "Windows time adjustment: Drift=%l dms\n", RTTimeSpecGetMilli(&Drift));345 VBoxServiceVerbose(3, "Windows time adjustment: OrgTA=%ld, CurTA=%ld, NewTA=%ld, DiffNew=%ld, DiffMax=%ld\n", 347 VBoxServiceVerbose(3, "Windows time adjustment: Drift=%lldms\n", RTTimeSpecGetMilli(&Drift)); 348 VBoxServiceVerbose(3, "Windows time adjustment: OrgTA=%ld, CurTA=%ld, NewTA=%ld, DiffNew=%ld, DiffMax=%ld\n", 346 349 g_dwWinTimeAdjustment, dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwDiffNew, dwDiffMax); 347 350 348 351 /* Is AbsDrift way too big? Then a minimum adjustment via SetSystemTimeAdjustment() would take ages. 349 352 So set the time in a hard manner. */ 350 353 if (AbsDriftMilli > (60 * 1000 * 20)) /** @todo 20 minutes here hardcoded here. Needs configurable parameter later. */ 351 354 { 352 SYSTEMTIME st = {0};353 FILETIME ft = {0};354 355 355 VBoxServiceVerbose(3, "Windows time adjustment: Setting system time directly.\n"); 356 356 357 358 /** @todo NT4 doesn't have GetSystemTimeAdjustment. */ 359 FILETIME ft; 357 360 RTTimeSpecGetNtFileTime(&HostNow, &ft); 358 if (FALSE == FileTimeToSystemTime(&ft,&st)) 359 VBoxServiceError("Cannot convert system times, error=%ld\n", GetLastError()); 360 361 if (!::SetSystemTime(&st)) 362 VBoxServiceError("SetSystemTime failed, error=%ld\n", GetLastError()); 361 SYSTEMTIME st; 362 if (FileTimeToSystemTime(&ft, &st)) 363 { 364 if (!SetSystemTime(&st)) 365 VBoxServiceError("SetSystemTime failed, error=%u\n", GetLastError()); 366 } 367 else 368 VBoxServiceError("Cannot convert system times, error=%u\n", GetLastError()); 363 369 } 364 370 else 365 371 { 366 if (! ::SetSystemTimeAdjustment(dwWinNewTimeAdjustment, FALSE /* Periodic adjustments enabled. */))367 VBoxServiceError("SetSystemTimeAdjustment failed, error=% ld\n", GetLastError());372 if (!SetSystemTimeAdjustment(dwWinNewTimeAdjustment, FALSE /* Periodic adjustments enabled. */)) 373 VBoxServiceError("SetSystemTimeAdjustment failed, error=%u\n", GetLastError()); 368 374 } 369 375 } 376 else 377 VBoxServiceError("GetSystemTimeAdjustment failed, error=%ld\n", GetLastError()); 370 378 371 379 #else /* !RT_OS_WINDOWS */ … … 410 418 { 411 419 #ifdef RT_OS_WINDOWS 412 if ( ::SetSystemTimeAdjustment(0, TRUE /* Periodic adjustments disabled. */))420 if (SetSystemTimeAdjustment(0, TRUE /* Periodic adjustments disabled. */)) 413 421 VBoxServiceVerbose(3, "Windows Time Adjustment is now disabled.\n"); 414 422 #endif /* !RT_OS_WINDOWS */
Note:
See TracChangeset
for help on using the changeset viewer.