ReactOS 0.4.16-dev-297-gc569aee
w32time.c File Reference
#include "w32time.h"
#include <debug.h>
#include <strsafe.h>
Include dependency graph for w32time.c:

Go to the source code of this file.

Functions

int InitService (VOID)
 
BOOL SystemSetTime (LPSYSTEMTIME lpSystemTime)
 
static DWORD UpdateSystemTime (ULONG ulTime)
 
static DWORD GetIntervalSetting (VOID)
 
DWORD SetTime (VOID)
 
VOID WINAPI ControlHandler (DWORD request)
 
VOID WINAPI W32TmServiceMain (DWORD argc, LPWSTR *argv)
 
BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
 
DWORD WINAPI W32TimeSyncNow (LPCWSTR cmdline, UINT blocking, UINT flags)
 

Variables

SERVICE_STATUS ServiceStatus
 
SERVICE_STATUS_HANDLE hStatus
 
HANDLE hStopEvent = NULL
 
static WCHAR ServiceName [] = L"W32Time"
 

Function Documentation

◆ ControlHandler()

VOID WINAPI ControlHandler ( DWORD  request)

Definition at line 214 of file w32time.c.

215{
216 switch (request)
217 {
220 DPRINT("Stopping W32Time Service\n");
221
225
226 if (hStopEvent)
228 return;
229
230 default:
231 break;
232 }
233}
BOOL WINAPI SetServiceStatus(SERVICE_STATUS_HANDLE hServiceStatus, LPSERVICE_STATUS lpServiceStatus)
Definition: sctrl.c:997
#define DPRINT
Definition: sndvol32.h:73
DWORD dwWin32ExitCode
Definition: winsvc.h:102
DWORD dwCurrentState
Definition: winsvc.h:100
Definition: tftpd.h:86
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
SERVICE_STATUS_HANDLE hStatus
Definition: w32time.c:14
HANDLE hStopEvent
Definition: w32time.c:15
SERVICE_STATUS ServiceStatus
Definition: w32time.c:13
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
#define SERVICE_CONTROL_SHUTDOWN
Definition: winsvc.h:40
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36

Referenced by W32TmServiceMain().

◆ DllMain()

BOOL WINAPI DllMain ( HINSTANCE  hinstDLL,
DWORD  fdwReason,
LPVOID  lpvReserved 
)

Definition at line 341 of file w32time.c.

344{
345 switch (fdwReason)
346 {
349 break;
350
352 break;
353 }
354
355 return TRUE;
356}
#define TRUE
Definition: types.h:120
#define DLL_PROCESS_ATTACH
Definition: compat.h:131
#define DLL_PROCESS_DETACH
Definition: compat.h:130
BOOL WINAPI DisableThreadLibraryCalls(IN HMODULE hLibModule)
Definition: loader.c:85

◆ GetIntervalSetting()

static DWORD GetIntervalSetting ( VOID  )
static

Definition at line 125 of file w32time.c.

126{
127 HKEY hKey;
129 DWORD dwSize = sizeof(dwData);
130 LONG lRet;
131
132 dwData = 0;
134 L"SYSTEM\\CurrentControlSet\\Services\\W32Time\\TimeProviders\\NtpClient",
135 0,
137 &hKey);
138 if (lRet == ERROR_SUCCESS)
139 {
140 /*
141 * This key holds the update interval in seconds.
142 * It is useful for testing to set it to a value of 10 (Decimal).
143 * This will cause the clock to try and update every 10 seconds.
144 * So you can change the time and expect it to be set back correctly in 10-20 seconds.
145 */
146 lRet = RegQueryValueExW(hKey,
147 L"SpecialPollInterval",
148 NULL,
149 NULL,
150 (LPBYTE)&dwData,
151 &dwSize);
153 }
154
155 if (lRet != ERROR_SUCCESS || dwData < 120)
157 else
158 return dwData;
159}
#define RegCloseKey(hKey)
Definition: registry.h:49
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3333
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4103
unsigned long DWORD
Definition: ntddk_ex.h:95
FxAutoRegKey hKey
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
static HANDLE ULONG_PTR dwData
Definition: file.c:35
#define KEY_QUERY_VALUE
Definition: nt_native.h:1016
#define L(x)
Definition: ntvdm.h:50
long LONG
Definition: pedump.c:60
unsigned char * LPBYTE
Definition: typedefs.h:53
#define W32TIME_POLL_INTERVAL
Definition: w32time.h:19
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12

Referenced by W32TmServiceMain().

◆ InitService()

int InitService ( VOID  )

◆ SetTime()

DWORD SetTime ( VOID  )

Definition at line 163 of file w32time.c.

164{
165 ULONG ulTime;
166 LONG lRet;
167 HKEY hKey;
168 WCHAR szData[MAX_VALUE_NAME] = L"";
169 DWORD cbName = sizeof(szData);
170
171 DPRINT("Entered SetTime.\n");
172
174 L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\DateTime\\Servers",
175 0,
177 &hKey);
178 if (lRet != ERROR_SUCCESS)
179 {
180 return lRet;
181 }
182
183 lRet = RegQueryValueExW(hKey, NULL, NULL, NULL, (LPBYTE)szData, &cbName);
184 if (lRet == ERROR_SUCCESS)
185 {
186 cbName = sizeof(szData);
187 lRet = RegQueryValueExW(hKey, szData, NULL, NULL, (LPBYTE)szData, &cbName);
188 }
189
191
192 DPRINT("Time Server is '%S'.\n", szData);
193
194 /* Is the given string empty? */
195 if (cbName == 0 || szData[0] == '\0')
196 {
197 DPRINT("The time NTP server couldn't be found, the string is empty!\n");
198 return ERROR_INVALID_DATA;
199 }
200
201 ulTime = GetServerTime(szData);
202
203 if (ulTime != 0)
204 {
205 return UpdateSystemTime(ulTime);
206 }
207 else
208 return ERROR_GEN_FAILURE;
209}
#define MAX_VALUE_NAME
Definition: control.c:28
ULONG GetServerTime(LPWSTR lpAddress)
Definition: ntpclient.c:138
uint32_t ULONG
Definition: typedefs.h:59
static DWORD UpdateSystemTime(ULONG ulTime)
Definition: w32time.c:83
#define ERROR_GEN_FAILURE
Definition: winerror.h:134
#define ERROR_INVALID_DATA
Definition: winerror.h:116
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by DECLARE_INTERFACE_(), W32TimeSyncNow(), and W32TmServiceMain().

◆ SystemSetTime()

BOOL SystemSetTime ( LPSYSTEMTIME  lpSystemTime)

Definition at line 21 of file w32time.c.

22{
23 HANDLE hToken;
24 DWORD PrevSize;
25 TOKEN_PRIVILEGES priv, previouspriv;
26 BOOL Ret = FALSE;
27
28 /*
29 * Enable the SeSystemtimePrivilege privilege
30 */
31
34 &hToken))
35 {
36 priv.PrivilegeCount = 1;
38
41 &priv.Privileges[0].Luid))
42 {
43 if (AdjustTokenPrivileges(hToken,
44 FALSE,
45 &priv,
46 sizeof(previouspriv),
47 &previouspriv,
48 &PrevSize) &&
50 {
51 /*
52 * We successfully enabled it, we're permitted to change the time.
53 */
54 Ret = SetSystemTime(lpSystemTime);
55
56 /*
57 * For the sake of security, restore the previous status again
58 */
59 if (previouspriv.PrivilegeCount > 0)
60 {
62 FALSE,
63 &previouspriv,
64 0,
65 NULL,
66 0);
67 }
68 }
69 }
70 CloseHandle(hToken);
71 }
72
73 return Ret;
74}
#define FALSE
Definition: types.h:117
BOOL WINAPI LookupPrivilegeValueW(LPCWSTR lpSystemName, LPCWSTR lpPrivilegeName, PLUID lpLuid)
Definition: misc.c:782
BOOL WINAPI AdjustTokenPrivileges(HANDLE TokenHandle, BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength)
Definition: security.c:374
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:294
#define CloseHandle
Definition: compat.h:739
#define GetCurrentProcess()
Definition: compat.h:759
BOOL WINAPI SetSystemTime(IN CONST SYSTEMTIME *lpSystemTime)
Definition: time.c:412
unsigned int BOOL
Definition: ntddk_ex.h:94
$ULONG PrivilegeCount
Definition: setypes.h:1023
LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY]
Definition: setypes.h:1024
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define SE_SYSTEMTIME_NAME
Definition: winnt_old.h:406
#define TOKEN_ADJUST_PRIVILEGES
Definition: setypes.h:930
#define TOKEN_QUERY
Definition: setypes.h:928
#define SE_PRIVILEGE_ENABLED
Definition: setypes.h:63

Referenced by UpdateSystemTime().

◆ UpdateSystemTime()

static DWORD UpdateSystemTime ( ULONG  ulTime)
static

Definition at line 83 of file w32time.c.

84{
85 FILETIME ftNew;
87 SYSTEMTIME stNew;
88
89 /* Time at 1st Jan 1900 */
90 stNew.wYear = 1900;
91 stNew.wMonth = 1;
92 stNew.wDay = 1;
93 stNew.wHour = 0;
94 stNew.wMinute = 0;
95 stNew.wSecond = 0;
96 stNew.wMilliseconds = 0;
97
98 /* Convert to a file time */
99 if (!SystemTimeToFileTime(&stNew, &ftNew))
100 {
101 return GetLastError();
102 }
103
104 /* Add on the time passed since 1st Jan 1900 */
105 li = *(LARGE_INTEGER *)&ftNew;
106 li.QuadPart += (LONGLONG)10000000 * ulTime;
107 ftNew = * (FILETIME *)&li;
108
109 /* Convert back to a system time */
110 if (!FileTimeToSystemTime(&ftNew, &stNew))
111 {
112 return GetLastError();
113 }
114
115 if (!SystemSetTime(&stNew))
116 {
117 return GetLastError();
118 }
119
120 return ERROR_SUCCESS;
121}
BOOL WINAPI FileTimeToSystemTime(IN CONST FILETIME *lpFileTime, OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:188
BOOL WINAPI SystemTimeToFileTime(IN CONST SYSTEMTIME *lpSystemTime, OUT LPFILETIME lpFileTime)
Definition: time.c:158
LARGE_INTEGER li
Definition: fxtimerapi.cpp:235
WORD wYear
Definition: winbase.h:930
WORD wMilliseconds
Definition: winbase.h:937
WORD wMonth
Definition: winbase.h:931
WORD wHour
Definition: winbase.h:934
WORD wSecond
Definition: winbase.h:936
WORD wMinute
Definition: winbase.h:935
WORD wDay
Definition: winbase.h:933
int64_t LONGLONG
Definition: typedefs.h:68
LONGLONG QuadPart
Definition: typedefs.h:114
BOOL SystemSetTime(LPSYSTEMTIME lpSystemTime)
Definition: w32time.c:21

Referenced by SetTime().

◆ W32TimeSyncNow()

DWORD WINAPI W32TimeSyncNow ( LPCWSTR  cmdline,
UINT  blocking,
UINT  flags 
)

Definition at line 360 of file w32time.c.

363{
365 result = SetTime();
366 if (result)
367 {
368 DPRINT("W32TimeSyncNow failed and clock not set.\n");
369 }
370 else
371 {
372 DPRINT("W32TimeSyncNow succeeded and clock set.\n");
373 }
374 return result;
375}
GLuint64EXT * result
Definition: glext.h:11304
DWORD SetTime(VOID)
Definition: w32time.c:163

Referenced by UpdateThread().

◆ W32TmServiceMain()

VOID WINAPI W32TmServiceMain ( DWORD  argc,
LPWSTR argv 
)

Definition at line 238 of file w32time.c.

239{
240 LONG error;
241 DWORD dwInterval;
242 HKEY hKey;
243 WCHAR szData[8];
244 DWORD cbData;
245 BOOL bNoSync;
246
249
257
260 if (!hStatus)
261 {
262 /* Registering Control Handler failed */
263 return;
264 }
265
266 /* Create the stop event */
268 if (hStopEvent == NULL)
269 {
274 return;
275 }
276
277 /* Get the interval */
278 dwInterval = GetIntervalSetting();
279
280 /* We report the running status to SCM. */
283
284 /* The service's worker loop */
285 for (;;)
286 {
287 /* The default is NoSync */
288 bNoSync = TRUE;
289
290 /* TODO: Use RegNotifyChangeKeyValue() when implemented */
292 L"SYSTEM\\CurrentControlSet\\Services\\W32Time\\Parameters",
293 0,
295 &hKey) == ERROR_SUCCESS)
296 {
297 cbData = sizeof(szData);
298 RegQueryValueExW(hKey, L"Type", NULL, NULL, (LPBYTE)szData, &cbData);
299 szData[ARRAYSIZE(szData) - 1] = UNICODE_NULL; /* Avoid buffer overrun */
300 bNoSync = (_wcsicmp(szData, L"NoSync") == 0);
302 }
303
304 if (!bNoSync)
305 {
306 error = SetTime();
307 if (error != ERROR_SUCCESS)
308 {
309 DPRINT("W32Time Service failed to set clock: 0x%08lX\n", error);
310#if 0
311 /*
312 * In general, we do not want to stop this service for a single
313 * Internet read failure but there may be other reasons for which
314 * we really might want to stop it. Therefore this code is left here.
315 */
319 return;
320#endif
321 }
322 }
323
324 if (WaitForSingleObject(hStopEvent, dwInterval * 1000) == WAIT_OBJECT_0)
325 {
328
333 DPRINT("Stopped W32Time Service\n");
334 return;
335 }
336 }
337}
static int argc
Definition: ServiceArgs.c:12
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define error(str)
Definition: mkdosfs.c:1605
#define argv
Definition: mplay32.c:18
#define UNICODE_NULL
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:325
SERVICE_STATUS_HANDLE WINAPI RegisterServiceCtrlHandlerW(LPCWSTR lpServiceName, LPHANDLER_FUNCTION lpHandlerProc)
Definition: sctrl.c:742
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
DWORD dwServiceType
Definition: winsvc.h:99
DWORD dwControlsAccepted
Definition: winsvc.h:101
DWORD dwWaitHint
Definition: winsvc.h:105
DWORD dwCheckPoint
Definition: winsvc.h:104
DWORD dwServiceSpecificExitCode
Definition: winsvc.h:103
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:651
static WCHAR ServiceName[]
Definition: w32time.c:16
static DWORD GetIntervalSetting(VOID)
Definition: w32time.c:125
VOID WINAPI ControlHandler(DWORD request)
Definition: w32time.c:214
#define WAIT_OBJECT_0
Definition: winbase.h:432
#define SERVICE_STOPPED
Definition: winsvc.h:21
#define SERVICE_ACCEPT_STOP
Definition: winsvc.h:28
#define SERVICE_START_PENDING
Definition: winsvc.h:22
#define SERVICE_RUNNING
Definition: winsvc.h:24
#define SERVICE_ACCEPT_SHUTDOWN
Definition: winsvc.h:30
#define SERVICE_WIN32
Definition: cmtypes.h:964

Variable Documentation

◆ hStatus

Definition at line 14 of file w32time.c.

Referenced by ControlHandler(), SamSsServiceMain(), W32TmServiceMain(), and WndProc().

◆ hStopEvent

HANDLE hStopEvent = NULL

Definition at line 15 of file w32time.c.

Referenced by ControlHandler(), and W32TmServiceMain().

◆ ServiceName

WCHAR ServiceName[] = L"W32Time"
static

Definition at line 16 of file w32time.c.

Referenced by W32TmServiceMain().

◆ ServiceStatus

SERVICE_STATUS ServiceStatus

Definition at line 13 of file w32time.c.

Referenced by ControlHandler(), and W32TmServiceMain().