1 | /* $Id: VBoxServiceTimeSync.cpp 30019 2010-06-04 07:56:11Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Guest Additions TimeSync Service.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /** @page pg_vboxservice_timesync The Time Sync Service
|
---|
20 | *
|
---|
21 | * The time sync service plays along with the Time Manager (TM) in the VMM
|
---|
22 | * to keep the guest time accurate using the host machine as reference.
|
---|
23 | * TM will try its best to make sure all timer ticks gets delivered so that
|
---|
24 | * there isn't normally any need to adjust the guest time.
|
---|
25 | *
|
---|
26 | * There are three normal (= acceptable) cases:
|
---|
27 | * -# When the service starts up. This is because ticks and such might
|
---|
28 | * be lost during VM and OS startup. (Need to figure out exactly why!)
|
---|
29 | * -# When the TM is unable to deliver all the ticks and swallows a
|
---|
30 | * backlog of ticks. The threshold for this is configurable with
|
---|
31 | * a default of 60 seconds.
|
---|
32 | * -# The time is adjusted on the host. This can be caused manually by
|
---|
33 | * the user or by some time sync daemon (NTP, LAN server, etc.).
|
---|
34 | *
|
---|
35 | * There are a number of very odd case where adjusting is needed. Here
|
---|
36 | * are some of them:
|
---|
37 | * -# Timer device emulation inaccurancies (like rounding).
|
---|
38 | * -# Inaccurancies in time source VirtualBox uses.
|
---|
39 | * -# The Guest and/or Host OS doesn't perform proper time keeping. This
|
---|
40 | * come about as a result of OS and/or hardware issues.
|
---|
41 | *
|
---|
42 | * The TM is our source for the host time and will make adjustments for
|
---|
43 | * current timer delivery lag. The simplistic approach taken by TM is to
|
---|
44 | * adjust the host time by the current guest timer delivery lag, meaning that
|
---|
45 | * if the guest is behind 1 second with PIT/RTC/++ ticks this should be reflected
|
---|
46 | * in the guest wall time as well.
|
---|
47 | *
|
---|
48 | * Now, there is any amount of trouble we can cause by changing the time.
|
---|
49 | * Most applications probably uses the wall time when they need to measure
|
---|
50 | * things. A walltime that is being juggled about every so often, even if just
|
---|
51 | * a little bit, could occationally upset these measurements by for instance
|
---|
52 | * yielding negative results.
|
---|
53 | *
|
---|
54 | * This bottom line here is that the time sync service isn't really supposed
|
---|
55 | * to do anything and will try avoid having to do anything when possible.
|
---|
56 | *
|
---|
57 | * The implementation uses the latency it takes to query host time as the
|
---|
58 | * absolute maximum precision to avoid messing up under timer tick catchup
|
---|
59 | * and/or heavy host/guest load. (Rational is that a *lot* of stuff may happen
|
---|
60 | * on our way back from ring-3 and TM/VMMDev since we're taking the route
|
---|
61 | * thru the inner EM loop with it's force action processing.)
|
---|
62 | *
|
---|
63 | * But this latency has to be measured from our perspective, which means it
|
---|
64 | * could just as easily come out as 0. (OS/2 and Windows guest only updates
|
---|
65 | * the current time when the timer ticks for instance.) The good thing is
|
---|
66 | * that this isn't really a problem since we won't ever do anything unless
|
---|
67 | * the drift is noticable.
|
---|
68 | *
|
---|
69 | * It now boils down to these three (configuration) factors:
|
---|
70 | * -# g_TimesyncMinAdjust - The minimum drift we will ever bother with.
|
---|
71 | * -# g_TimesyncLatencyFactor - The factor we multiply the latency by to
|
---|
72 | * calculate the dynamic minimum adjust factor.
|
---|
73 | * -# g_TimesyncMaxLatency - When to start discarding the data as utterly
|
---|
74 | * useless and take a rest (someone is too busy to give us good data).
|
---|
75 | * -# g_TimeSyncSetThreshold - The threshold at which we will just set the time
|
---|
76 | * instead of trying to adjust it (milliseconds).
|
---|
77 | */
|
---|
78 |
|
---|
79 | /*******************************************************************************
|
---|
80 | * Header Files *
|
---|
81 | *******************************************************************************/
|
---|
82 | #ifdef RT_OS_WINDOWS
|
---|
83 | # include <Windows.h>
|
---|
84 | # include <winbase.h> /** @todo r=bird: Why is this here? Windows.h should include winbase.h... */
|
---|
85 | #else
|
---|
86 | # include <unistd.h>
|
---|
87 | # include <errno.h>
|
---|
88 | # include <time.h>
|
---|
89 | # include <sys/time.h>
|
---|
90 | #endif
|
---|
91 |
|
---|
92 | #include <iprt/thread.h>
|
---|
93 | #include <iprt/string.h>
|
---|
94 | #include <iprt/semaphore.h>
|
---|
95 | #include <iprt/time.h>
|
---|
96 | #include <iprt/assert.h>
|
---|
97 | #include <VBox/VBoxGuestLib.h>
|
---|
98 | #include "VBoxServiceInternal.h"
|
---|
99 | #include "VBoxServiceUtils.h"
|
---|
100 |
|
---|
101 |
|
---|
102 | /*******************************************************************************
|
---|
103 | * Global Variables *
|
---|
104 | *******************************************************************************/
|
---|
105 | /** The timesync interval (millseconds). */
|
---|
106 | uint32_t g_TimeSyncInterval = 0;
|
---|
107 | /**
|
---|
108 | * @see pg_vboxservice_timesync
|
---|
109 | *
|
---|
110 | * @remark OS/2: There is either a 1 second resolution on the DosSetDateTime
|
---|
111 | * API or a but in the settimeofday implementation. Thus, don't
|
---|
112 | * bother unless there is at least a 1 second drift.
|
---|
113 | */
|
---|
114 | #ifdef RT_OS_OS2
|
---|
115 | static uint32_t g_TimeSyncMinAdjust = 1000;
|
---|
116 | #else
|
---|
117 | static uint32_t g_TimeSyncMinAdjust = 100;
|
---|
118 | #endif
|
---|
119 | /** @see pg_vboxservice_timesync */
|
---|
120 | static uint32_t g_TimeSyncLatencyFactor = 8;
|
---|
121 | /** @see pg_vboxservice_timesync */
|
---|
122 | static uint32_t g_TimeSyncMaxLatency = 250;
|
---|
123 | /** @see pg_vboxservice_timesync */
|
---|
124 | static uint32_t g_TimeSyncSetThreshold = 20*60*1000;
|
---|
125 | /** Whether the next adjustment should just set the time instead of trying to
|
---|
126 | * adjust it. This is used to implement --timesync-set-start. */
|
---|
127 | static bool volatile g_fTimeSyncSetNext = false;
|
---|
128 |
|
---|
129 | /** Current error count. Used to knowing when to bitch and when not to. */
|
---|
130 | static uint32_t g_cTimeSyncErrors = 0;
|
---|
131 |
|
---|
132 | /** The semaphore we're blocking on. */
|
---|
133 | static RTSEMEVENTMULTI g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
|
---|
134 |
|
---|
135 | #ifdef RT_OS_WINDOWS
|
---|
136 | /** Process token. */
|
---|
137 | static HANDLE g_hTokenProcess = NULL;
|
---|
138 | /** Old token privileges. */
|
---|
139 | static TOKEN_PRIVILEGES g_TkOldPrivileges;
|
---|
140 | /** Backup values for time adjustment. */
|
---|
141 | static DWORD g_dwWinTimeAdjustment;
|
---|
142 | static DWORD g_dwWinTimeIncrement;
|
---|
143 | static BOOL g_bWinTimeAdjustmentDisabled;
|
---|
144 | #endif
|
---|
145 |
|
---|
146 |
|
---|
147 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
148 | static DECLCALLBACK(int) VBoxServiceTimeSyncPreInit(void)
|
---|
149 | {
|
---|
150 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
151 | /** @todo Merge this function with VBoxServiceTimeSyncOption() to generalize
|
---|
152 | * the "command line args override guest property values" behavior. */
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * Read the service options from the VM's guest properties.
|
---|
156 | * Note that these options can be overriden by the command line options later.
|
---|
157 | */
|
---|
158 | uint32_t uGuestPropSvcClientID;
|
---|
159 | int rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
|
---|
160 | if (RT_FAILURE(rc))
|
---|
161 | {
|
---|
162 | VBoxServiceError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
|
---|
163 | }
|
---|
164 | else
|
---|
165 | {
|
---|
166 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-interval",
|
---|
167 | &g_TimeSyncInterval, 50, UINT32_MAX - 1);
|
---|
168 | if ( RT_SUCCESS(rc)
|
---|
169 | || rc == VERR_NOT_FOUND)
|
---|
170 | {
|
---|
171 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-min-adjust",
|
---|
172 | &g_TimeSyncMinAdjust, 0, 3600000);
|
---|
173 | }
|
---|
174 | if ( RT_SUCCESS(rc)
|
---|
175 | || rc == VERR_NOT_FOUND)
|
---|
176 | {
|
---|
177 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-latency-factor",
|
---|
178 | &g_TimeSyncLatencyFactor, 1, 1024);
|
---|
179 | }
|
---|
180 | if ( RT_SUCCESS(rc)
|
---|
181 | || rc == VERR_NOT_FOUND)
|
---|
182 | {
|
---|
183 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-max-latency",
|
---|
184 | &g_TimeSyncMaxLatency, 1, 3600000);
|
---|
185 | }
|
---|
186 | if ( RT_SUCCESS(rc)
|
---|
187 | || rc == VERR_NOT_FOUND)
|
---|
188 | {
|
---|
189 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold",
|
---|
190 | &g_TimeSyncSetThreshold, 0, 7*24*60*60*1000 /* a week */);
|
---|
191 | }
|
---|
192 | if ( RT_SUCCESS(rc)
|
---|
193 | || rc == VERR_NOT_FOUND)
|
---|
194 | {
|
---|
195 | char *pszValue;
|
---|
196 | rc = VBoxServiceReadProp(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-start",
|
---|
197 | &pszValue, NULL /* ppszFlags */, NULL /* puTimestamp */);
|
---|
198 | if (RT_SUCCESS(rc))
|
---|
199 | {
|
---|
200 | g_fTimeSyncSetNext = true;
|
---|
201 | RTStrFree(pszValue);
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
|
---|
206 | }
|
---|
207 |
|
---|
208 | if (rc == VERR_NOT_FOUND) /* If a value is not found, don't be sad! */
|
---|
209 | rc = VINF_SUCCESS;
|
---|
210 | return rc;
|
---|
211 | #else
|
---|
212 | /* Nothing to do here yet. */
|
---|
213 | return VINF_SUCCESS;
|
---|
214 | #endif
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
219 | static DECLCALLBACK(int) VBoxServiceTimeSyncOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
220 | {
|
---|
221 | int rc = -1;
|
---|
222 | if (ppszShort)
|
---|
223 | /* no short options */;
|
---|
224 | else if (!strcmp(argv[*pi], "--timesync-interval"))
|
---|
225 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
226 | &g_TimeSyncInterval, 50, UINT32_MAX - 1);
|
---|
227 | else if (!strcmp(argv[*pi], "--timesync-min-adjust"))
|
---|
228 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
229 | &g_TimeSyncMinAdjust, 0, 3600000);
|
---|
230 | else if (!strcmp(argv[*pi], "--timesync-latency-factor"))
|
---|
231 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
232 | &g_TimeSyncLatencyFactor, 1, 1024);
|
---|
233 | else if (!strcmp(argv[*pi], "--timesync-max-latency"))
|
---|
234 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
235 | &g_TimeSyncMaxLatency, 1, 3600000);
|
---|
236 | else if (!strcmp(argv[*pi], "--timesync-set-threshold"))
|
---|
237 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
238 | &g_TimeSyncSetThreshold, 0, 7*24*60*60*1000); /* a week */
|
---|
239 | else if (!strcmp(argv[*pi], "--timesync-set-start"))
|
---|
240 | {
|
---|
241 | g_fTimeSyncSetNext = true;
|
---|
242 | rc = VINF_SUCCESS;
|
---|
243 | }
|
---|
244 |
|
---|
245 | return rc;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
250 | static DECLCALLBACK(int) VBoxServiceTimeSyncInit(void)
|
---|
251 | {
|
---|
252 | /*
|
---|
253 | * If not specified, find the right interval default.
|
---|
254 | * Then create the event sem to block on.
|
---|
255 | */
|
---|
256 | if (!g_TimeSyncInterval)
|
---|
257 | g_TimeSyncInterval = g_DefaultInterval * 1000;
|
---|
258 | if (!g_TimeSyncInterval)
|
---|
259 | g_TimeSyncInterval = 10 * 1000;
|
---|
260 |
|
---|
261 | int rc = RTSemEventMultiCreate(&g_TimeSyncEvent);
|
---|
262 | AssertRC(rc);
|
---|
263 | #ifdef RT_OS_WINDOWS
|
---|
264 | if (RT_SUCCESS(rc))
|
---|
265 | {
|
---|
266 | /*
|
---|
267 | * Adjust priviledges of this process so we can make system time adjustments.
|
---|
268 | */
|
---|
269 | if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &g_hTokenProcess))
|
---|
270 | {
|
---|
271 | TOKEN_PRIVILEGES tkPriv;
|
---|
272 | RT_ZERO(tkPriv);
|
---|
273 | tkPriv.PrivilegeCount = 1;
|
---|
274 | tkPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
---|
275 | if (LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkPriv.Privileges[0].Luid))
|
---|
276 | {
|
---|
277 | DWORD cbRet = sizeof(g_TkOldPrivileges);
|
---|
278 | if (AdjustTokenPrivileges(g_hTokenProcess, FALSE, &tkPriv, sizeof(TOKEN_PRIVILEGES), &g_TkOldPrivileges, &cbRet))
|
---|
279 | rc = VINF_SUCCESS;
|
---|
280 | else
|
---|
281 | {
|
---|
282 | DWORD dwErr = GetLastError();
|
---|
283 | rc = RTErrConvertFromWin32(dwErr);
|
---|
284 | VBoxServiceError("Adjusting token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
|
---|
285 | }
|
---|
286 | }
|
---|
287 | else
|
---|
288 | {
|
---|
289 | DWORD dwErr = GetLastError();
|
---|
290 | rc = RTErrConvertFromWin32(dwErr);
|
---|
291 | VBoxServiceError("Looking up token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
|
---|
292 | }
|
---|
293 | if (RT_FAILURE(rc))
|
---|
294 | {
|
---|
295 | CloseHandle(g_hTokenProcess);
|
---|
296 | g_hTokenProcess = NULL;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | else
|
---|
300 | {
|
---|
301 | DWORD dwErr = GetLastError();
|
---|
302 | rc = RTErrConvertFromWin32(dwErr);
|
---|
303 | VBoxServiceError("Opening process token (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
|
---|
304 | g_hTokenProcess = NULL;
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | if (GetSystemTimeAdjustment(&g_dwWinTimeAdjustment, &g_dwWinTimeIncrement, &g_bWinTimeAdjustmentDisabled))
|
---|
309 | VBoxServiceVerbose(3, "Windows time adjustment: Initially %ld (100ns) units per %ld (100 ns) units interval, disabled=%d\n",
|
---|
310 | g_dwWinTimeAdjustment, g_dwWinTimeIncrement, g_bWinTimeAdjustmentDisabled ? 1 : 0);
|
---|
311 | else
|
---|
312 | {
|
---|
313 | DWORD dwErr = GetLastError();
|
---|
314 | rc = RTErrConvertFromWin32(dwErr);
|
---|
315 | VBoxServiceError("Could not get time adjustment values! Last error: %ld!\n", dwErr);
|
---|
316 | }
|
---|
317 | #endif /* RT_OS_WINDOWS */
|
---|
318 |
|
---|
319 | return rc;
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * Try adjust the time using adjtime or similar.
|
---|
325 | *
|
---|
326 | * @returns true on success, false on failure.
|
---|
327 | *
|
---|
328 | * @param pDrift The time adjustment.
|
---|
329 | */
|
---|
330 | static bool VBoxServiceTimeSyncAdjust(PCRTTIMESPEC pDrift)
|
---|
331 | {
|
---|
332 | #ifdef RT_OS_WINDOWS
|
---|
333 | /** @todo r=bird: NT4 doesn't have GetSystemTimeAdjustment according to MSDN. */
|
---|
334 | /** @todo r=bird: g_hTokenProcess cannot be NULL here.
|
---|
335 | * VBoxServiceTimeSyncInit will fail and the service will not be
|
---|
336 | * started with it being NULL. VBoxServiceTimeSyncInit OTOH will *NOT*
|
---|
337 | * be called until the service thread has terminated. If anything
|
---|
338 | * else is the case, there is buggy code somewhere.*/
|
---|
339 | if (g_hTokenProcess == NULL) /* Is the token already closed when shutting down? */
|
---|
340 | return false;
|
---|
341 |
|
---|
342 | DWORD dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwWinTimeIncrement;
|
---|
343 | BOOL fWinTimeAdjustmentDisabled;
|
---|
344 | if (GetSystemTimeAdjustment(&dwWinTimeAdjustment, &dwWinTimeIncrement, &fWinTimeAdjustmentDisabled))
|
---|
345 | {
|
---|
346 | DWORD dwDiffMax = g_dwWinTimeAdjustment * 0.50;
|
---|
347 | DWORD dwDiffNew = dwWinTimeAdjustment * 0.10;
|
---|
348 |
|
---|
349 | if (RTTimeSpecGetMilli(pDrift) > 0)
|
---|
350 | {
|
---|
351 | dwWinNewTimeAdjustment = dwWinTimeAdjustment + dwDiffNew;
|
---|
352 | if (dwWinNewTimeAdjustment > (g_dwWinTimeAdjustment + dwDiffMax))
|
---|
353 | {
|
---|
354 | dwWinNewTimeAdjustment = g_dwWinTimeAdjustment + dwDiffMax;
|
---|
355 | dwDiffNew = dwDiffMax;
|
---|
356 | }
|
---|
357 | }
|
---|
358 | else
|
---|
359 | {
|
---|
360 | dwWinNewTimeAdjustment = dwWinTimeAdjustment - dwDiffNew;
|
---|
361 | if (dwWinNewTimeAdjustment < (g_dwWinTimeAdjustment - dwDiffMax))
|
---|
362 | {
|
---|
363 | dwWinNewTimeAdjustment = g_dwWinTimeAdjustment - dwDiffMax;
|
---|
364 | dwDiffNew = dwDiffMax;
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | VBoxServiceVerbose(3, "Windows time adjustment: Drift=%lldms\n", RTTimeSpecGetMilli(pDrift));
|
---|
369 | VBoxServiceVerbose(3, "Windows time adjustment: OrgTA=%ld, CurTA=%ld, NewTA=%ld, DiffNew=%ld, DiffMax=%ld\n",
|
---|
370 | g_dwWinTimeAdjustment, dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwDiffNew, dwDiffMax);
|
---|
371 | if (SetSystemTimeAdjustment(dwWinNewTimeAdjustment, FALSE /* Periodic adjustments enabled. */))
|
---|
372 | {
|
---|
373 | g_cTimeSyncErrors = 0;
|
---|
374 | return true;
|
---|
375 | }
|
---|
376 |
|
---|
377 | if (g_cTimeSyncErrors++ < 10)
|
---|
378 | VBoxServiceError("SetSystemTimeAdjustment failed, error=%u\n", GetLastError());
|
---|
379 | }
|
---|
380 | else if (g_cTimeSyncErrors++ < 10)
|
---|
381 | VBoxServiceError("GetSystemTimeAdjustment failed, error=%ld\n", GetLastError());
|
---|
382 |
|
---|
383 | #elif defined(RT_OS_OS2)
|
---|
384 | /* No API for doing gradual time adjustments. */
|
---|
385 |
|
---|
386 | #else /* PORTME */
|
---|
387 | /*
|
---|
388 | * Try use adjtime(), most unix-like systems have this.
|
---|
389 | */
|
---|
390 | struct timeval tv;
|
---|
391 | RTTimeSpecGetTimeval(pDrift, &tv);
|
---|
392 | if (adjtime(&tv, NULL) == 0)
|
---|
393 | {
|
---|
394 | if (g_cVerbosity >= 1)
|
---|
395 | VBoxServiceVerbose(1, "adjtime by %RDtimespec\n", pDrift);
|
---|
396 | g_cTimeSyncErrors = 0;
|
---|
397 | return true;
|
---|
398 | }
|
---|
399 | #endif
|
---|
400 |
|
---|
401 | /* failed */
|
---|
402 | return false;
|
---|
403 | }
|
---|
404 |
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Cancels any pending time adjustment.
|
---|
408 | *
|
---|
409 | * Called when we've caught up and before calls to VBoxServiceTimeSyncSet.
|
---|
410 | */
|
---|
411 | static void VBoxServiceTimeSyncCancelAdjust(void)
|
---|
412 | {
|
---|
413 | #ifdef RT_OS_WINDOWS
|
---|
414 | /** @todo r=bird: g_hTokenProcess cannot be NULL here. See argumentation in
|
---|
415 | * VBoxServiceTimeSyncAdjust. */
|
---|
416 | if (g_hTokenProcess == NULL) /* No process token (anymore)? */
|
---|
417 | return;
|
---|
418 | if (SetSystemTimeAdjustment(0, TRUE /* Periodic adjustments disabled. */))
|
---|
419 | VBoxServiceVerbose(3, "Windows Time Adjustment is now disabled.\n");
|
---|
420 | else if (g_cTimeSyncErrors++ < 10)
|
---|
421 | VBoxServiceError("SetSystemTimeAdjustment(,disable) failed, error=%u\n", GetLastError());
|
---|
422 | #endif /* !RT_OS_WINDOWS */
|
---|
423 | }
|
---|
424 |
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Try adjust the time using adjtime or similar.
|
---|
428 | *
|
---|
429 | * @returns true on success, false on failure.
|
---|
430 | *
|
---|
431 | * @param pDrift The time adjustment.
|
---|
432 | */
|
---|
433 | static void VBoxServiceTimeSyncSet(PCRTTIMESPEC pDrift)
|
---|
434 | {
|
---|
435 | /*
|
---|
436 | * Query the current time, adjust it by adding the drift and set it.
|
---|
437 | */
|
---|
438 | RTTIMESPEC NewGuestTime;
|
---|
439 | int rc = RTTimeSet(RTTimeSpecAdd(RTTimeNow(&NewGuestTime), pDrift));
|
---|
440 | if (RT_SUCCESS(rc))
|
---|
441 | {
|
---|
442 | /* Succeeded - reset the error count and log the change. */
|
---|
443 | g_cTimeSyncErrors = 0;
|
---|
444 |
|
---|
445 | if (g_cVerbosity >= 1)
|
---|
446 | {
|
---|
447 | char sz[64];
|
---|
448 | RTTIME Time;
|
---|
449 | VBoxServiceVerbose(1, "time set to %s\n",
|
---|
450 | RTTimeToString(RTTimeExplode(&Time, &NewGuestTime), sz, sizeof(sz)));
|
---|
451 | #ifdef DEBUG
|
---|
452 | RTTIMESPEC Tmp;
|
---|
453 | if (g_cVerbosity >= 3)
|
---|
454 | VBoxServiceVerbose(3, " now %s\n",
|
---|
455 | RTTimeToString(RTTimeExplode(&Time, RTTimeNow(&Tmp)), sz, sizeof(sz)));
|
---|
456 | #endif
|
---|
457 | }
|
---|
458 | }
|
---|
459 | else if (g_cTimeSyncErrors++ < 10)
|
---|
460 | VBoxServiceError("RTTimeSet(%RDtimespec) failed: %Rrc\n", &NewGuestTime, rc);
|
---|
461 | }
|
---|
462 |
|
---|
463 |
|
---|
464 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
465 | DECLCALLBACK(int) VBoxServiceTimeSyncWorker(bool volatile *pfShutdown)
|
---|
466 | {
|
---|
467 | RTTIME Time;
|
---|
468 | char sz[64];
|
---|
469 | int rc = VINF_SUCCESS;
|
---|
470 |
|
---|
471 | /*
|
---|
472 | * Tell the control thread that it can continue spawning services.
|
---|
473 | */
|
---|
474 | RTThreadUserSignal(RTThreadSelf());
|
---|
475 |
|
---|
476 | /*
|
---|
477 | * The Work Loop.
|
---|
478 | */
|
---|
479 | for (;;)
|
---|
480 | {
|
---|
481 | /*
|
---|
482 | * Try get a reliable time reading.
|
---|
483 | */
|
---|
484 | int cTries = 3;
|
---|
485 | do
|
---|
486 | {
|
---|
487 | /* query it. */
|
---|
488 | RTTIMESPEC GuestNow0, GuestNow, HostNow;
|
---|
489 | RTTimeNow(&GuestNow0);
|
---|
490 | int rc2 = VbglR3GetHostTime(&HostNow);
|
---|
491 | if (RT_FAILURE(rc2))
|
---|
492 | {
|
---|
493 | if (g_cTimeSyncErrors++ < 10)
|
---|
494 | VBoxServiceError("VbglR3GetHostTime failed; rc2=%Rrc\n", rc2);
|
---|
495 | break;
|
---|
496 | }
|
---|
497 | RTTimeNow(&GuestNow);
|
---|
498 |
|
---|
499 | /* calc latency and check if it's ok. */
|
---|
500 | RTTIMESPEC GuestElapsed = GuestNow;
|
---|
501 | RTTimeSpecSub(&GuestElapsed, &GuestNow0);
|
---|
502 | if ((uint32_t)RTTimeSpecGetMilli(&GuestElapsed) < g_TimeSyncMaxLatency)
|
---|
503 | {
|
---|
504 | /*
|
---|
505 | * Calculate the adjustment threshold and the current drift.
|
---|
506 | */
|
---|
507 | uint32_t MinAdjust = RTTimeSpecGetMilli(&GuestElapsed) * g_TimeSyncLatencyFactor;
|
---|
508 | if (MinAdjust < g_TimeSyncMinAdjust)
|
---|
509 | MinAdjust = g_TimeSyncMinAdjust;
|
---|
510 |
|
---|
511 | RTTIMESPEC Drift = HostNow;
|
---|
512 | RTTimeSpecSub(&Drift, &GuestNow);
|
---|
513 | if (RTTimeSpecGetMilli(&Drift) < 0)
|
---|
514 | MinAdjust += g_TimeSyncMinAdjust; /* extra buffer against moving time backwards. */
|
---|
515 |
|
---|
516 | RTTIMESPEC AbsDrift = Drift;
|
---|
517 | RTTimeSpecAbsolute(&AbsDrift);
|
---|
518 | if (g_cVerbosity >= 3)
|
---|
519 | {
|
---|
520 | VBoxServiceVerbose(3, "Host: %s (MinAdjust: %RU32 ms)\n",
|
---|
521 | RTTimeToString(RTTimeExplode(&Time, &HostNow), sz, sizeof(sz)), MinAdjust);
|
---|
522 | VBoxServiceVerbose(3, "Guest: - %s => %RDtimespec drift\n",
|
---|
523 | RTTimeToString(RTTimeExplode(&Time, &GuestNow), sz, sizeof(sz)),
|
---|
524 | &Drift);
|
---|
525 | }
|
---|
526 |
|
---|
527 | uint32_t AbsDriftMilli = RTTimeSpecGetMilli(&AbsDrift);
|
---|
528 | if (AbsDriftMilli > MinAdjust)
|
---|
529 | {
|
---|
530 | /*
|
---|
531 | * Ok, the drift is above the threshold.
|
---|
532 | *
|
---|
533 | * Try a gradual adjustment first, if that fails or the drift is
|
---|
534 | * too big, fall back on just setting the time.
|
---|
535 | */
|
---|
536 |
|
---|
537 | if ( AbsDriftMilli > g_TimeSyncSetThreshold
|
---|
538 | || g_fTimeSyncSetNext
|
---|
539 | || !VBoxServiceTimeSyncAdjust(&Drift))
|
---|
540 | {
|
---|
541 | VBoxServiceTimeSyncCancelAdjust();
|
---|
542 | VBoxServiceTimeSyncSet(&Drift);
|
---|
543 | }
|
---|
544 | }
|
---|
545 | else
|
---|
546 | VBoxServiceTimeSyncCancelAdjust();
|
---|
547 | break;
|
---|
548 | }
|
---|
549 | VBoxServiceVerbose(3, "%RDtimespec: latency too high (%RDtimespec) sleeping 1s\n", GuestElapsed);
|
---|
550 | RTThreadSleep(1000);
|
---|
551 | } while (--cTries > 0);
|
---|
552 |
|
---|
553 | /* Clear the set-next/set-start flag. */
|
---|
554 | g_fTimeSyncSetNext = false;
|
---|
555 |
|
---|
556 | /*
|
---|
557 | * Block for a while.
|
---|
558 | *
|
---|
559 | * The event semaphore takes care of ignoring interruptions and it
|
---|
560 | * allows us to implement service wakeup later.
|
---|
561 | */
|
---|
562 | if (*pfShutdown)
|
---|
563 | break;
|
---|
564 | int rc2 = RTSemEventMultiWait(g_TimeSyncEvent, g_TimeSyncInterval);
|
---|
565 | if (*pfShutdown)
|
---|
566 | break;
|
---|
567 | if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
|
---|
568 | {
|
---|
569 | VBoxServiceError("RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
|
---|
570 | rc = rc2;
|
---|
571 | break;
|
---|
572 | }
|
---|
573 | }
|
---|
574 |
|
---|
575 | VBoxServiceTimeSyncCancelAdjust();
|
---|
576 | RTSemEventMultiDestroy(g_TimeSyncEvent);
|
---|
577 | g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
|
---|
578 | return rc;
|
---|
579 | }
|
---|
580 |
|
---|
581 |
|
---|
582 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
583 | static DECLCALLBACK(void) VBoxServiceTimeSyncStop(void)
|
---|
584 | {
|
---|
585 | RTSemEventMultiSignal(g_TimeSyncEvent);
|
---|
586 | }
|
---|
587 |
|
---|
588 |
|
---|
589 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
590 | static DECLCALLBACK(void) VBoxServiceTimeSyncTerm(void)
|
---|
591 | {
|
---|
592 | #ifdef RT_OS_WINDOWS
|
---|
593 | /*
|
---|
594 | * Restore the SE_SYSTEMTIME_NAME token privileges (if init succeeded).
|
---|
595 | */
|
---|
596 | if (g_hTokenProcess)
|
---|
597 | {
|
---|
598 | if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &g_TkOldPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
|
---|
599 | {
|
---|
600 | DWORD dwErr = GetLastError();
|
---|
601 | VBoxServiceError("Restoring token privileges (SE_SYSTEMTIME_NAME) failed with code %u!\n", dwErr);
|
---|
602 | }
|
---|
603 | CloseHandle(g_hTokenProcess);
|
---|
604 | g_hTokenProcess = NULL;
|
---|
605 | }
|
---|
606 | #endif /* !RT_OS_WINDOWS */
|
---|
607 |
|
---|
608 | if (g_TimeSyncEvent != NIL_RTSEMEVENTMULTI)
|
---|
609 | {
|
---|
610 | RTSemEventMultiDestroy(g_TimeSyncEvent);
|
---|
611 | g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
|
---|
612 | }
|
---|
613 | }
|
---|
614 |
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * The 'timesync' service description.
|
---|
618 | */
|
---|
619 | VBOXSERVICE g_TimeSync =
|
---|
620 | {
|
---|
621 | /* pszName. */
|
---|
622 | "timesync",
|
---|
623 | /* pszDescription. */
|
---|
624 | "Time synchronization",
|
---|
625 | /* pszUsage. */
|
---|
626 | " [--timesync-interval <ms>] [--timesync-min-adjust <ms>]\n"
|
---|
627 | " [--timesync-latency-factor <x>] [--timesync-max-latency <ms>]\n"
|
---|
628 | " [--timesync-set-threshold <ms>] [--timesync-set-start]"
|
---|
629 | ,
|
---|
630 | /* pszOptions. */
|
---|
631 | " --timesync-interval Specifies the interval at which to synchronize the\n"
|
---|
632 | " time with the host. The default is 10000 ms.\n"
|
---|
633 | " --timesync-min-adjust The minimum absolute drift value measured in\n"
|
---|
634 | " milliseconds to make adjustments for.\n"
|
---|
635 | " The default is 1000 ms on OS/2 and 100 ms elsewhere.\n"
|
---|
636 | " --timesync-latency-factor\n"
|
---|
637 | " The factor to multiply the time query latency with\n"
|
---|
638 | " to calculate the dynamic minimum adjust time.\n"
|
---|
639 | " The default is 8 times.\n"
|
---|
640 | " --timesync-max-latency The max host timer query latency to accept.\n"
|
---|
641 | " The default is 250 ms.\n"
|
---|
642 | " --timesync-set-threshold\n"
|
---|
643 | " The absolute drift threshold, given as milliseconds,\n"
|
---|
644 | " where to start setting the time instead of trying to\n"
|
---|
645 | " adjust it. The default is 20 min.\n"
|
---|
646 | " --timesync-set-start Set the time when starting the time sync service.\n"
|
---|
647 | ,
|
---|
648 | /* methods */
|
---|
649 | VBoxServiceTimeSyncPreInit,
|
---|
650 | VBoxServiceTimeSyncOption,
|
---|
651 | VBoxServiceTimeSyncInit,
|
---|
652 | VBoxServiceTimeSyncWorker,
|
---|
653 | VBoxServiceTimeSyncStop,
|
---|
654 | VBoxServiceTimeSyncTerm
|
---|
655 | };
|
---|
656 |
|
---|