Boolean adjFreq(Integer32 adj) { #ifndef __WINDOWS__ #ifndef CONFIG_MPC831X struct timex t; #endif #endif if(adj > ADJ_FREQ_MAX) adj = ADJ_FREQ_MAX; else if(adj < -ADJ_FREQ_MAX) adj = -ADJ_FREQ_MAX; #ifdef CONFIG_MPC831X if (++temp_debug_max_adjustments < 10000) mpc831x_adj_addend(adj); return (TRUE); #elif defined(__WINDOWS__) return(!( SetSystemTimeAdjustment((adj/100),FALSE))); #else t.modes = MOD_FREQUENCY; t.freq = adj*((1<<16)/1000); return !adjtimex(&t); #endif }
BOOL My_SetSystemTimeAdjustment() { DWORD dwTimeAdjustment=NULL; BOOL bTimeAdjustmentDisabled=NULL; BOOL returnVal_Real = NULL; BOOL returnVal_Intercepted = NULL; DWORD error_Real = 0; DWORD error_Intercepted = 0; disableInterception(); returnVal_Real = SetSystemTimeAdjustment (dwTimeAdjustment,bTimeAdjustmentDisabled); error_Real = GetLastError(); enableInterception(); returnVal_Intercepted = SetSystemTimeAdjustment (dwTimeAdjustment,bTimeAdjustmentDisabled); error_Intercepted = GetLastError(); return ((returnVal_Real == returnVal_Intercepted) && (error_Real == error_Intercepted)); }
static void hugsprim_SetSystemTimeAdjustment_7(HugsStackPtr hugs_root) { HsWord32 arg1; HsBool arg2; HsBool res1; arg1 = hugs->getWord32(); arg2 = hugs->getBool(); res1 = SetSystemTimeAdjustment(arg1, arg2); hugs->putBool(res1); hugs->returnIO(hugs_root,1); }
/** * Cancels any pending time adjustment. * * Called when we've caught up and before calls to vgsvcTimeSyncSet. */ static void vgsvcTimeSyncCancelAdjust(void) { #ifdef RT_OS_WINDOWS /** @todo r=bird: g_hTokenProcess cannot be NULL here. See argumentation in * vgsvcTimeSyncAdjust. */ if (g_hTokenProcess == NULL) /* No process token (anymore)? */ return; if (SetSystemTimeAdjustment(0, TRUE /* Periodic adjustments disabled. */)) VGSvcVerbose(3, "vgsvcTimeSyncCancelAdjust: Windows Time Adjustment is now disabled.\n"); else if (g_cTimeSyncErrors++ < 10) VGSvcError("vgsvcTimeSyncCancelAdjust: SetSystemTimeAdjustment(,disable) failed, error=%u\n", GetLastError()); #endif /* !RT_OS_WINDOWS */ }
void reset_winnt_time(void) { /* restore the clock frequency back to its original value */ if (!SetSystemTimeAdjustment(0, TRUE)) { msyslog(LOG_ERR, "Failed to reset clock state, SetSystemTimeAdjustment(): %m"); } /************ Added back in 2003-01-26 *****************/ /* read the current system time, and write it back to force CMOS update: */ { SYSTEMTIME st; GetSystemTime(&st); SetSystemTime(&st); } }
BOOL CallSetSystemTimeAdjustment(DWORD dwTimeAdjustment) { HANDLE hToken; BOOL bTimeAdjustmentDisabled = FALSE; if (dwTimeAdjustment == 0) bTimeAdjustmentDisabled = TRUE; // else // dwTimeAdjustment = atoi(argv[2]); // 100ns /* Get hToken */ if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { PrintErrorMessage((__FILE__), "OpenProcessToken()", __LINE__); exit(1); } /* enable SE_SYSTEMTIME_NAME Privilege */ if (!SetPrivilege(hToken, SE_SYSTEMTIME_NAME, TRUE)) { CloseHandle(hToken); PrintErrorMessage((__FILE__), "SetPrivilege(hToken, SE_SYSTEMTIME_NAME, TRUE)", __LINE__); exit(1); } CloseHandle(hToken); /* Call SetSystemTimeAdjustment() */ printf("SetSystemTimeAdjustment() Calling...\n"); if (!SetSystemTimeAdjustment(dwTimeAdjustment, bTimeAdjustmentDisabled)) { if (bTimeAdjustmentDisabled) PrintErrorMessage((__FILE__), "SetSystemTimeAdjustment(0, TRUE)", __LINE__); else PrintErrorMessage((__FILE__), "SetSystemTimeAdjustment(dwTimeAdjustment, FALSE)", __LINE__); exit(1); } else { printf("dwTimeAdjustment: %ld [100-nanosecond unit]\n", dwTimeAdjustment); printf("bTimeAdjustmentDisabled : %s \n", bTimeAdjustmentDisabled ? "True (Sync RTC see KB232488)" : "False (Ignore RTC)"); } return TRUE; }
int main(int argc, char* argv[]) { DWORD dwTmAdj; cout<<"please input time adjustment (unit is 100ns):\n"; cin>>dwTmAdj; cout<<"ok! use time adjustment "<<dwTmAdj<<"\n"; if(EnableAdjustTimePrivilege(true)){ if(!SetSystemTimeAdjustment(dwTmAdj,false)){ cout<<"set time adjustment fail! err code "<<GetLastError()<<"\n"; return false; }else{ cout<<"set time adjustment succeed!\n"; } EnableAdjustTimePrivilege(false); }else{ return false; } return 0; }
/** * Try adjust the time using adjtime or similar. * * @returns true on success, false on failure. * * @param pDrift The time adjustment. */ static bool vgsvcTimeSyncAdjust(PCRTTIMESPEC pDrift) { #ifdef RT_OS_WINDOWS /** @todo r=bird: g_hTokenProcess cannot be NULL here. * vgsvcTimeSyncInit will fail and the service will not be started with * it being NULL. vgsvcTimeSyncInit OTOH will *NOT* be called until the * service thread has terminated. If anything * else is the case, there is buggy code somewhere.*/ if (g_hTokenProcess == NULL) /* Is the token already closed when shutting down? */ return false; DWORD dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwWinTimeIncrement; BOOL fWinTimeAdjustmentDisabled; if (GetSystemTimeAdjustment(&dwWinTimeAdjustment, &dwWinTimeIncrement, &fWinTimeAdjustmentDisabled)) { DWORD dwDiffMax = g_dwWinTimeAdjustment * 0.50; DWORD dwDiffNew = dwWinTimeAdjustment * 0.10; if (RTTimeSpecGetMilli(pDrift) > 0) { dwWinNewTimeAdjustment = dwWinTimeAdjustment + dwDiffNew; if (dwWinNewTimeAdjustment > (g_dwWinTimeAdjustment + dwDiffMax)) { dwWinNewTimeAdjustment = g_dwWinTimeAdjustment + dwDiffMax; dwDiffNew = dwDiffMax; } } else { dwWinNewTimeAdjustment = dwWinTimeAdjustment - dwDiffNew; if (dwWinNewTimeAdjustment < (g_dwWinTimeAdjustment - dwDiffMax)) { dwWinNewTimeAdjustment = g_dwWinTimeAdjustment - dwDiffMax; dwDiffNew = dwDiffMax; } } VGSvcVerbose(3, "vgsvcTimeSyncAdjust: Drift=%lldms\n", RTTimeSpecGetMilli(pDrift)); VGSvcVerbose(3, "vgsvcTimeSyncAdjust: OrgTA=%ld, CurTA=%ld, NewTA=%ld, DiffNew=%ld, DiffMax=%ld\n", g_dwWinTimeAdjustment, dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwDiffNew, dwDiffMax); if (SetSystemTimeAdjustment(dwWinNewTimeAdjustment, FALSE /* Periodic adjustments enabled. */)) { g_cTimeSyncErrors = 0; return true; } if (g_cTimeSyncErrors++ < 10) VGSvcError("vgsvcTimeSyncAdjust: SetSystemTimeAdjustment failed, error=%u\n", GetLastError()); } else if (g_cTimeSyncErrors++ < 10) VGSvcError("vgsvcTimeSyncAdjust: GetSystemTimeAdjustment failed, error=%ld\n", GetLastError()); #elif defined(RT_OS_OS2) || defined(RT_OS_HAIKU) /* No API for doing gradual time adjustments. */ #else /* PORTME */ /* * Try use adjtime(), most unix-like systems have this. */ struct timeval tv; RTTimeSpecGetTimeval(pDrift, &tv); if (adjtime(&tv, NULL) == 0) { if (g_cVerbosity >= 1) VGSvcVerbose(1, "vgsvcTimeSyncAdjust: adjtime by %RDtimespec\n", pDrift); g_cTimeSyncErrors = 0; return true; } #endif /* failed */ return false; }
/* * adj_systime - called once every second to make system time adjustments. * Returns 1 if okay, 0 if trouble. */ int adj_systime( double now ) { double dtemp; u_char isneg = 0; int rc; long dwTimeAdjustment; /* * Add the residual from the previous adjustment to the new * adjustment, bound and round. */ dtemp = sys_residual + now; sys_residual = 0; if (dtemp < 0) { isneg = 1; dtemp = -dtemp; } if (dtemp > NTP_MAXFREQ) dtemp = NTP_MAXFREQ; dtemp = dtemp * 1e6; if (isneg) dtemp = -dtemp; /* dtemp is in micro seconds. NT uses 100 ns units, * so a unit change in dwTimeAdjustment corresponds * to slewing 10 ppm on a 100 Hz system. * Calculate the number of 100ns units to add, * using OS tick frequency as per suggestion from Harry Pyle, * and leave the remainder in dtemp */ dwTimeAdjustment = (DWORD)( dtemp / ppm_per_adjust_unit + (isneg ? -0.5 : 0.5)) ; dtemp += (double) -dwTimeAdjustment * ppm_per_adjust_unit; /* only adjust the clock if adjustment changes */ if (last_Adj != dwTimeAdjustment) { last_Adj = dwTimeAdjustment; # ifdef DEBUG if (debug > 1) printf("SetSystemTimeAdjustment( %ld) + (%ld)\n", dwTimeAdjustment, units_per_tick); # endif dwTimeAdjustment += units_per_tick; rc = !SetSystemTimeAdjustment(dwTimeAdjustment, FALSE); } else rc = 0; if (rc) { msyslog(LOG_ERR, "Can't adjust time: %m"); return 0; } else { sys_residual = dtemp / 1000000.0; } #ifdef DEBUG if (debug > 6) printf("adj_systime: adj %.9f -> remaining residual %.9f\n", now, sys_residual); #endif return 1; }