From e627c3b00e7109b5110b94fb8253859a29f35d42 Mon Sep 17 00:00:00 2001 From: Katayama Hirofumi MZ Date: Fri, 3 Nov 2023 22:56:58 +0900 Subject: [PATCH] [W32TIME] Remember date/time sync settings (#5866) Based on KRosUser's patch. - In the W32TmServiceMain function, the time check loop does check the registry value. CORE-19292 --- base/services/w32time/w32time.c | 63 ++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/base/services/w32time/w32time.c b/base/services/w32time/w32time.c index 69c6bd15abe..0d5508a7b2c 100644 --- a/base/services/w32time/w32time.c +++ b/base/services/w32time/w32time.c @@ -230,8 +230,6 @@ ControlHandler(DWORD request) default: break; } - - return; } @@ -239,8 +237,12 @@ VOID WINAPI W32TmServiceMain(DWORD argc, LPWSTR *argv) { - int result; + LONG error; DWORD dwInterval; + HKEY hKey; + WCHAR szData[8]; + DWORD cbData; + BOOL bNoSync; UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); @@ -279,29 +281,44 @@ W32TmServiceMain(DWORD argc, LPWSTR *argv) ServiceStatus.dwCurrentState = SERVICE_RUNNING; SetServiceStatus(hStatus, &ServiceStatus); - /* The worker loop of a service */ + /* The service's worker loop */ for (;;) { - result = SetTime(); + /* The default is NoSync */ + bNoSync = TRUE; - if (result) - DPRINT("W32Time Service failed to set clock.\n"); - else - DPRINT("W32Time Service successfully set clock.\n"); - - if (result) + /* TODO: Use RegNotifyChangeKeyValue() when implemented */ + if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, + L"SYSTEM\\CurrentControlSet\\Services\\W32Time\\Parameters", + 0, + KEY_QUERY_VALUE, + &hKey) == ERROR_SUCCESS) { - /* In general we do not want to stop this service for a single - * Internet read failure but there may be other reasons for which - * we really might want to stop it. - * Therefore this code is left here to make it easy to stop this - * service when the correct conditions can be determined, but it - * is left commented out. - ServiceStatus.dwCurrentState = SERVICE_STOPPED; - ServiceStatus.dwWin32ExitCode = result; - SetServiceStatus(hStatus, &ServiceStatus); - return; - */ + cbData = sizeof(szData); + RegQueryValueExW(hKey, L"Type", NULL, NULL, (LPBYTE)szData, &cbData); + szData[ARRAYSIZE(szData) - 1] = UNICODE_NULL; /* Avoid buffer overrun */ + bNoSync = (_wcsicmp(szData, L"NoSync") == 0); + RegCloseKey(hKey); + } + + if (!bNoSync) + { + error = SetTime(); + if (error != ERROR_SUCCESS) + { + DPRINT("W32Time Service failed to set clock: 0x%08lX\n", error); +#if 0 + /* + * In general, we do not want to stop this service for a single + * Internet read failure but there may be other reasons for which + * we really might want to stop it. Therefore this code is left here. + */ + ServiceStatus.dwCurrentState = SERVICE_STOPPED; + ServiceStatus.dwWin32ExitCode = error; + SetServiceStatus(hStatus, &ServiceStatus); + return; +#endif + } } if (WaitForSingleObject(hStopEvent, dwInterval * 1000) == WAIT_OBJECT_0) @@ -317,11 +334,9 @@ W32TmServiceMain(DWORD argc, LPWSTR *argv) return; } } - return; } - BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,