1 | /* $Id: VBoxServiceTimeSync.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Guest Additions TimeSync Service.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2015 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 inaccuracies (like rounding).
|
---|
38 | * -# Inaccuracies 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 occasionally 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 noticeable.
|
---|
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 | /*********************************************************************************************************************************
|
---|
81 | * Header Files *
|
---|
82 | *********************************************************************************************************************************/
|
---|
83 | #ifdef RT_OS_WINDOWS
|
---|
84 | # include <Windows.h>
|
---|
85 | # include <winbase.h> /** @todo r=bird: Why is this here? Windows.h should include winbase.h... */
|
---|
86 | #else
|
---|
87 | # include <unistd.h>
|
---|
88 | # include <errno.h>
|
---|
89 | # include <time.h>
|
---|
90 | # include <sys/time.h>
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | #include <iprt/thread.h>
|
---|
94 | #include <iprt/string.h>
|
---|
95 | #include <iprt/semaphore.h>
|
---|
96 | #include <iprt/time.h>
|
---|
97 | #include <iprt/assert.h>
|
---|
98 | #include <VBox/VBoxGuestLib.h>
|
---|
99 | #include "VBoxServiceInternal.h"
|
---|
100 | #include "VBoxServiceUtils.h"
|
---|
101 |
|
---|
102 |
|
---|
103 | /*********************************************************************************************************************************
|
---|
104 | * Global Variables *
|
---|
105 | *********************************************************************************************************************************/
|
---|
106 | /** The timesync interval (milliseconds). */
|
---|
107 | uint32_t g_TimeSyncInterval = 0;
|
---|
108 | /**
|
---|
109 | * @see pg_vboxservice_timesync
|
---|
110 | *
|
---|
111 | * @remark OS/2: There is either a 1 second resolution on the DosSetDateTime
|
---|
112 | * API or a but in the settimeofday implementation. Thus, don't
|
---|
113 | * bother unless there is at least a 1 second drift.
|
---|
114 | */
|
---|
115 | #ifdef RT_OS_OS2
|
---|
116 | static uint32_t g_TimeSyncMinAdjust = 1000;
|
---|
117 | #else
|
---|
118 | static uint32_t g_TimeSyncMinAdjust = 100;
|
---|
119 | #endif
|
---|
120 | /** @see pg_vboxservice_timesync */
|
---|
121 | static uint32_t g_TimeSyncLatencyFactor = 8;
|
---|
122 | /** @see pg_vboxservice_timesync */
|
---|
123 | static uint32_t g_TimeSyncMaxLatency = 250;
|
---|
124 | /** @see pg_vboxservice_timesync */
|
---|
125 | static uint32_t g_TimeSyncSetThreshold = 20*60*1000;
|
---|
126 | /** Whether the next adjustment should just set the time instead of trying to
|
---|
127 | * adjust it. This is used to implement --timesync-set-start. */
|
---|
128 | static bool volatile g_fTimeSyncSetNext = false;
|
---|
129 | /** Whether to set the time when the VM was restored. */
|
---|
130 | static bool g_fTimeSyncSetOnRestore = true;
|
---|
131 |
|
---|
132 | /** Current error count. Used to knowing when to bitch and when not to. */
|
---|
133 | static uint32_t g_cTimeSyncErrors = 0;
|
---|
134 |
|
---|
135 | /** The semaphore we're blocking on. */
|
---|
136 | static RTSEMEVENTMULTI g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
|
---|
137 |
|
---|
138 | /** The VM session ID. Changes whenever the VM is restored or reset. */
|
---|
139 | static uint64_t g_idTimeSyncSession;
|
---|
140 |
|
---|
141 | #ifdef RT_OS_WINDOWS
|
---|
142 | /** Process token. */
|
---|
143 | static HANDLE g_hTokenProcess = NULL;
|
---|
144 | /** Old token privileges. */
|
---|
145 | static TOKEN_PRIVILEGES g_TkOldPrivileges;
|
---|
146 | /** Backup values for time adjustment. */
|
---|
147 | static DWORD g_dwWinTimeAdjustment;
|
---|
148 | static DWORD g_dwWinTimeIncrement;
|
---|
149 | static BOOL g_bWinTimeAdjustmentDisabled;
|
---|
150 | #endif
|
---|
151 |
|
---|
152 |
|
---|
153 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
154 | static DECLCALLBACK(int) VBoxServiceTimeSyncPreInit(void)
|
---|
155 | {
|
---|
156 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
157 | /** @todo Merge this function with VBoxServiceTimeSyncOption() to generalize
|
---|
158 | * the "command line args override guest property values" behavior. */
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Read the service options from the VM's guest properties.
|
---|
162 | * Note that these options can be overridden by the command line options later.
|
---|
163 | */
|
---|
164 | uint32_t uGuestPropSvcClientID;
|
---|
165 | int rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
|
---|
166 | if (RT_FAILURE(rc))
|
---|
167 | {
|
---|
168 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
169 | {
|
---|
170 | VBoxServiceVerbose(0, "VMInfo: Guest property service is not available, skipping\n");
|
---|
171 | rc = VINF_SUCCESS;
|
---|
172 | }
|
---|
173 | else
|
---|
174 | VBoxServiceError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
|
---|
175 | }
|
---|
176 | else
|
---|
177 | {
|
---|
178 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-interval",
|
---|
179 | &g_TimeSyncInterval, 50, UINT32_MAX - 1);
|
---|
180 | if ( RT_SUCCESS(rc)
|
---|
181 | || rc == VERR_NOT_FOUND)
|
---|
182 | {
|
---|
183 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-min-adjust",
|
---|
184 | &g_TimeSyncMinAdjust, 0, 3600000);
|
---|
185 | }
|
---|
186 | if ( RT_SUCCESS(rc)
|
---|
187 | || rc == VERR_NOT_FOUND)
|
---|
188 | {
|
---|
189 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-latency-factor",
|
---|
190 | &g_TimeSyncLatencyFactor, 1, 1024);
|
---|
191 | }
|
---|
192 | if ( RT_SUCCESS(rc)
|
---|
193 | || rc == VERR_NOT_FOUND)
|
---|
194 | {
|
---|
195 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-max-latency",
|
---|
196 | &g_TimeSyncMaxLatency, 1, 3600000);
|
---|
197 | }
|
---|
198 | if ( RT_SUCCESS(rc)
|
---|
199 | || rc == VERR_NOT_FOUND)
|
---|
200 | {
|
---|
201 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold",
|
---|
202 | &g_TimeSyncSetThreshold, 0, 7*24*60*60*1000 /* a week */);
|
---|
203 | }
|
---|
204 | if ( RT_SUCCESS(rc)
|
---|
205 | || rc == VERR_NOT_FOUND)
|
---|
206 | {
|
---|
207 | char *pszValue;
|
---|
208 | rc = VBoxServiceReadProp(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-start",
|
---|
209 | &pszValue, NULL /* ppszFlags */, NULL /* puTimestamp */);
|
---|
210 | if (RT_SUCCESS(rc))
|
---|
211 | {
|
---|
212 | g_fTimeSyncSetNext = true;
|
---|
213 | RTStrFree(pszValue);
|
---|
214 | }
|
---|
215 | }
|
---|
216 | if ( RT_SUCCESS(rc)
|
---|
217 | || rc == VERR_NOT_FOUND)
|
---|
218 | {
|
---|
219 | uint32_t value;
|
---|
220 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-on-restore",
|
---|
221 | &value, 1, 1);
|
---|
222 | if (RT_SUCCESS(rc))
|
---|
223 | g_fTimeSyncSetOnRestore = !!value;
|
---|
224 | }
|
---|
225 |
|
---|
226 | VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
|
---|
227 | }
|
---|
228 |
|
---|
229 | if (rc == VERR_NOT_FOUND) /* If a value is not found, don't be sad! */
|
---|
230 | rc = VINF_SUCCESS;
|
---|
231 | return rc;
|
---|
232 | #else
|
---|
233 | /* Nothing to do here yet. */
|
---|
234 | return VINF_SUCCESS;
|
---|
235 | #endif
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
240 | static DECLCALLBACK(int) VBoxServiceTimeSyncOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
241 | {
|
---|
242 | int rc = -1;
|
---|
243 | uint32_t value;
|
---|
244 | if (ppszShort)
|
---|
245 | /* no short options */;
|
---|
246 | else if (!strcmp(argv[*pi], "--timesync-interval"))
|
---|
247 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
248 | &g_TimeSyncInterval, 50, UINT32_MAX - 1);
|
---|
249 | else if (!strcmp(argv[*pi], "--timesync-min-adjust"))
|
---|
250 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
251 | &g_TimeSyncMinAdjust, 0, 3600000);
|
---|
252 | else if (!strcmp(argv[*pi], "--timesync-latency-factor"))
|
---|
253 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
254 | &g_TimeSyncLatencyFactor, 1, 1024);
|
---|
255 | else if (!strcmp(argv[*pi], "--timesync-max-latency"))
|
---|
256 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
257 | &g_TimeSyncMaxLatency, 1, 3600000);
|
---|
258 | else if (!strcmp(argv[*pi], "--timesync-set-threshold"))
|
---|
259 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
260 | &g_TimeSyncSetThreshold, 0, 7*24*60*60*1000); /* a week */
|
---|
261 | else if (!strcmp(argv[*pi], "--timesync-set-start"))
|
---|
262 | {
|
---|
263 | g_fTimeSyncSetNext = true;
|
---|
264 | rc = VINF_SUCCESS;
|
---|
265 | }
|
---|
266 | else if (!strcmp(argv[*pi], "--timesync-set-on-restore"))
|
---|
267 | {
|
---|
268 | rc = VBoxServiceArgUInt32(argc, argv, "", pi, &value, 1, 1);
|
---|
269 | if (RT_SUCCESS(rc))
|
---|
270 | g_fTimeSyncSetOnRestore = !!value;
|
---|
271 | }
|
---|
272 |
|
---|
273 | return rc;
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
278 | static DECLCALLBACK(int) VBoxServiceTimeSyncInit(void)
|
---|
279 | {
|
---|
280 | /*
|
---|
281 | * If not specified, find the right interval default.
|
---|
282 | * Then create the event sem to block on.
|
---|
283 | */
|
---|
284 | if (!g_TimeSyncInterval)
|
---|
285 | g_TimeSyncInterval = g_DefaultInterval * 1000;
|
---|
286 | if (!g_TimeSyncInterval)
|
---|
287 | g_TimeSyncInterval = 10 * 1000;
|
---|
288 |
|
---|
289 | VbglR3GetSessionId(&g_idTimeSyncSession);
|
---|
290 | /* The status code is ignored as this information is not available with VBox < 3.2.10. */
|
---|
291 |
|
---|
292 | int rc = RTSemEventMultiCreate(&g_TimeSyncEvent);
|
---|
293 | AssertRC(rc);
|
---|
294 | #ifdef RT_OS_WINDOWS
|
---|
295 | if (RT_SUCCESS(rc))
|
---|
296 | {
|
---|
297 | /*
|
---|
298 | * Adjust privileges of this process so we can make system time adjustments.
|
---|
299 | */
|
---|
300 | if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &g_hTokenProcess))
|
---|
301 | {
|
---|
302 | TOKEN_PRIVILEGES tkPriv;
|
---|
303 | RT_ZERO(tkPriv);
|
---|
304 | tkPriv.PrivilegeCount = 1;
|
---|
305 | tkPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
|
---|
306 | if (LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkPriv.Privileges[0].Luid))
|
---|
307 | {
|
---|
308 | DWORD cbRet = sizeof(g_TkOldPrivileges);
|
---|
309 | if (AdjustTokenPrivileges(g_hTokenProcess, FALSE, &tkPriv, sizeof(TOKEN_PRIVILEGES), &g_TkOldPrivileges, &cbRet))
|
---|
310 | rc = VINF_SUCCESS;
|
---|
311 | else
|
---|
312 | {
|
---|
313 | DWORD dwErr = GetLastError();
|
---|
314 | rc = RTErrConvertFromWin32(dwErr);
|
---|
315 | VBoxServiceError("VBoxServiceTimeSyncInit: Adjusting token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
|
---|
316 | }
|
---|
317 | }
|
---|
318 | else
|
---|
319 | {
|
---|
320 | DWORD dwErr = GetLastError();
|
---|
321 | rc = RTErrConvertFromWin32(dwErr);
|
---|
322 | VBoxServiceError("VBoxServiceTimeSyncInit: Looking up token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
|
---|
323 | }
|
---|
324 | if (RT_FAILURE(rc))
|
---|
325 | {
|
---|
326 | CloseHandle(g_hTokenProcess);
|
---|
327 | g_hTokenProcess = NULL;
|
---|
328 | }
|
---|
329 | }
|
---|
330 | else
|
---|
331 | {
|
---|
332 | DWORD dwErr = GetLastError();
|
---|
333 | rc = RTErrConvertFromWin32(dwErr);
|
---|
334 | VBoxServiceError("VBoxServiceTimeSyncInit: Opening process token (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
|
---|
335 | g_hTokenProcess = NULL;
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | if (GetSystemTimeAdjustment(&g_dwWinTimeAdjustment, &g_dwWinTimeIncrement, &g_bWinTimeAdjustmentDisabled))
|
---|
340 | VBoxServiceVerbose(3, "VBoxServiceTimeSyncInit: Initially %ld (100ns) units per %ld (100 ns) units interval, disabled=%d\n",
|
---|
341 | g_dwWinTimeAdjustment, g_dwWinTimeIncrement, g_bWinTimeAdjustmentDisabled ? 1 : 0);
|
---|
342 | else
|
---|
343 | {
|
---|
344 | DWORD dwErr = GetLastError();
|
---|
345 | rc = RTErrConvertFromWin32(dwErr);
|
---|
346 | VBoxServiceError("VBoxServiceTimeSyncInit: Could not get time adjustment values! Last error: %ld!\n", dwErr);
|
---|
347 | }
|
---|
348 | #endif /* RT_OS_WINDOWS */
|
---|
349 |
|
---|
350 | return rc;
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * Try adjust the time using adjtime or similar.
|
---|
356 | *
|
---|
357 | * @returns true on success, false on failure.
|
---|
358 | *
|
---|
359 | * @param pDrift The time adjustment.
|
---|
360 | */
|
---|
361 | static bool VBoxServiceTimeSyncAdjust(PCRTTIMESPEC pDrift)
|
---|
362 | {
|
---|
363 | #ifdef RT_OS_WINDOWS
|
---|
364 | /** @todo r=bird: NT4 doesn't have GetSystemTimeAdjustment according to MSDN. */
|
---|
365 | /** @todo r=bird: g_hTokenProcess cannot be NULL here.
|
---|
366 | * VBoxServiceTimeSyncInit will fail and the service will not be
|
---|
367 | * started with it being NULL. VBoxServiceTimeSyncInit OTOH will *NOT*
|
---|
368 | * be called until the service thread has terminated. If anything
|
---|
369 | * else is the case, there is buggy code somewhere.*/
|
---|
370 | if (g_hTokenProcess == NULL) /* Is the token already closed when shutting down? */
|
---|
371 | return false;
|
---|
372 |
|
---|
373 | DWORD dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwWinTimeIncrement;
|
---|
374 | BOOL fWinTimeAdjustmentDisabled;
|
---|
375 | if (GetSystemTimeAdjustment(&dwWinTimeAdjustment, &dwWinTimeIncrement, &fWinTimeAdjustmentDisabled))
|
---|
376 | {
|
---|
377 | DWORD dwDiffMax = g_dwWinTimeAdjustment * 0.50;
|
---|
378 | DWORD dwDiffNew = dwWinTimeAdjustment * 0.10;
|
---|
379 |
|
---|
380 | if (RTTimeSpecGetMilli(pDrift) > 0)
|
---|
381 | {
|
---|
382 | dwWinNewTimeAdjustment = dwWinTimeAdjustment + dwDiffNew;
|
---|
383 | if (dwWinNewTimeAdjustment > (g_dwWinTimeAdjustment + dwDiffMax))
|
---|
384 | {
|
---|
385 | dwWinNewTimeAdjustment = g_dwWinTimeAdjustment + dwDiffMax;
|
---|
386 | dwDiffNew = dwDiffMax;
|
---|
387 | }
|
---|
388 | }
|
---|
389 | else
|
---|
390 | {
|
---|
391 | dwWinNewTimeAdjustment = dwWinTimeAdjustment - dwDiffNew;
|
---|
392 | if (dwWinNewTimeAdjustment < (g_dwWinTimeAdjustment - dwDiffMax))
|
---|
393 | {
|
---|
394 | dwWinNewTimeAdjustment = g_dwWinTimeAdjustment - dwDiffMax;
|
---|
395 | dwDiffNew = dwDiffMax;
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | VBoxServiceVerbose(3, "VBoxServiceTimeSyncAdjust: Drift=%lldms\n", RTTimeSpecGetMilli(pDrift));
|
---|
400 | VBoxServiceVerbose(3, "VBoxServiceTimeSyncAdjust: OrgTA=%ld, CurTA=%ld, NewTA=%ld, DiffNew=%ld, DiffMax=%ld\n",
|
---|
401 | g_dwWinTimeAdjustment, dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwDiffNew, dwDiffMax);
|
---|
402 | if (SetSystemTimeAdjustment(dwWinNewTimeAdjustment, FALSE /* Periodic adjustments enabled. */))
|
---|
403 | {
|
---|
404 | g_cTimeSyncErrors = 0;
|
---|
405 | return true;
|
---|
406 | }
|
---|
407 |
|
---|
408 | if (g_cTimeSyncErrors++ < 10)
|
---|
409 | VBoxServiceError("VBoxServiceTimeSyncAdjust: SetSystemTimeAdjustment failed, error=%u\n", GetLastError());
|
---|
410 | }
|
---|
411 | else if (g_cTimeSyncErrors++ < 10)
|
---|
412 | VBoxServiceError("VBoxServiceTimeSyncAdjust: GetSystemTimeAdjustment failed, error=%ld\n", GetLastError());
|
---|
413 |
|
---|
414 | #elif defined(RT_OS_OS2) || defined(RT_OS_HAIKU)
|
---|
415 | /* No API for doing gradual time adjustments. */
|
---|
416 |
|
---|
417 | #else /* PORTME */
|
---|
418 | /*
|
---|
419 | * Try use adjtime(), most unix-like systems have this.
|
---|
420 | */
|
---|
421 | struct timeval tv;
|
---|
422 | RTTimeSpecGetTimeval(pDrift, &tv);
|
---|
423 | if (adjtime(&tv, NULL) == 0)
|
---|
424 | {
|
---|
425 | if (g_cVerbosity >= 1)
|
---|
426 | VBoxServiceVerbose(1, "VBoxServiceTimeSyncAdjust: adjtime by %RDtimespec\n", pDrift);
|
---|
427 | g_cTimeSyncErrors = 0;
|
---|
428 | return true;
|
---|
429 | }
|
---|
430 | #endif
|
---|
431 |
|
---|
432 | /* failed */
|
---|
433 | return false;
|
---|
434 | }
|
---|
435 |
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * Cancels any pending time adjustment.
|
---|
439 | *
|
---|
440 | * Called when we've caught up and before calls to VBoxServiceTimeSyncSet.
|
---|
441 | */
|
---|
442 | static void VBoxServiceTimeSyncCancelAdjust(void)
|
---|
443 | {
|
---|
444 | #ifdef RT_OS_WINDOWS
|
---|
445 | /** @todo r=bird: g_hTokenProcess cannot be NULL here. See argumentation in
|
---|
446 | * VBoxServiceTimeSyncAdjust. */
|
---|
447 | if (g_hTokenProcess == NULL) /* No process token (anymore)? */
|
---|
448 | return;
|
---|
449 | if (SetSystemTimeAdjustment(0, TRUE /* Periodic adjustments disabled. */))
|
---|
450 | VBoxServiceVerbose(3, "VBoxServiceTimeSyncCancelAdjust: Windows Time Adjustment is now disabled.\n");
|
---|
451 | else if (g_cTimeSyncErrors++ < 10)
|
---|
452 | VBoxServiceError("VBoxServiceTimeSyncCancelAdjust: SetSystemTimeAdjustment(,disable) failed, error=%u\n", GetLastError());
|
---|
453 | #endif /* !RT_OS_WINDOWS */
|
---|
454 | }
|
---|
455 |
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * Try adjust the time using adjtime or similar.
|
---|
459 | *
|
---|
460 | * @returns true on success, false on failure.
|
---|
461 | *
|
---|
462 | * @param pDrift The time adjustment.
|
---|
463 | */
|
---|
464 | static void VBoxServiceTimeSyncSet(PCRTTIMESPEC pDrift)
|
---|
465 | {
|
---|
466 | /*
|
---|
467 | * Query the current time, adjust it by adding the drift and set it.
|
---|
468 | */
|
---|
469 | RTTIMESPEC NewGuestTime;
|
---|
470 | int rc = RTTimeSet(RTTimeSpecAdd(RTTimeNow(&NewGuestTime), pDrift));
|
---|
471 | if (RT_SUCCESS(rc))
|
---|
472 | {
|
---|
473 | /* Succeeded - reset the error count and log the change. */
|
---|
474 | g_cTimeSyncErrors = 0;
|
---|
475 |
|
---|
476 | if (g_cVerbosity >= 1)
|
---|
477 | {
|
---|
478 | char sz[64];
|
---|
479 | RTTIME Time;
|
---|
480 | VBoxServiceVerbose(1, "time set to %s\n",
|
---|
481 | RTTimeToString(RTTimeExplode(&Time, &NewGuestTime), sz, sizeof(sz)));
|
---|
482 | #ifdef DEBUG
|
---|
483 | RTTIMESPEC Tmp;
|
---|
484 | if (g_cVerbosity >= 3)
|
---|
485 | VBoxServiceVerbose(3, " now %s\n",
|
---|
486 | RTTimeToString(RTTimeExplode(&Time, RTTimeNow(&Tmp)), sz, sizeof(sz)));
|
---|
487 | #endif
|
---|
488 | }
|
---|
489 | }
|
---|
490 | else if (g_cTimeSyncErrors++ < 10)
|
---|
491 | VBoxServiceError("VBoxServiceTimeSyncSet: RTTimeSet(%RDtimespec) failed: %Rrc\n", &NewGuestTime, rc);
|
---|
492 | }
|
---|
493 |
|
---|
494 |
|
---|
495 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
496 | DECLCALLBACK(int) VBoxServiceTimeSyncWorker(bool volatile *pfShutdown)
|
---|
497 | {
|
---|
498 | RTTIME Time;
|
---|
499 | char sz[64];
|
---|
500 | int rc = VINF_SUCCESS;
|
---|
501 |
|
---|
502 | /*
|
---|
503 | * Tell the control thread that it can continue spawning services.
|
---|
504 | */
|
---|
505 | RTThreadUserSignal(RTThreadSelf());
|
---|
506 |
|
---|
507 | /*
|
---|
508 | * The Work Loop.
|
---|
509 | */
|
---|
510 | for (;;)
|
---|
511 | {
|
---|
512 | /*
|
---|
513 | * Try get a reliable time reading.
|
---|
514 | */
|
---|
515 | int cTries = 3;
|
---|
516 | do
|
---|
517 | {
|
---|
518 | /* query it. */
|
---|
519 | RTTIMESPEC GuestNow0, GuestNow, HostNow;
|
---|
520 | RTTimeNow(&GuestNow0);
|
---|
521 | int rc2 = VbglR3GetHostTime(&HostNow);
|
---|
522 | if (RT_FAILURE(rc2))
|
---|
523 | {
|
---|
524 | if (g_cTimeSyncErrors++ < 10)
|
---|
525 | VBoxServiceError("VBoxServiceTimeSyncWorker: VbglR3GetHostTime failed; rc2=%Rrc\n", rc2);
|
---|
526 | break;
|
---|
527 | }
|
---|
528 | RTTimeNow(&GuestNow);
|
---|
529 |
|
---|
530 | /* calc latency and check if it's ok. */
|
---|
531 | RTTIMESPEC GuestElapsed = GuestNow;
|
---|
532 | RTTimeSpecSub(&GuestElapsed, &GuestNow0);
|
---|
533 | if ((uint32_t)RTTimeSpecGetMilli(&GuestElapsed) < g_TimeSyncMaxLatency)
|
---|
534 | {
|
---|
535 | /*
|
---|
536 | * Set the time once after we were restored.
|
---|
537 | * (Of course only if the drift is bigger than MinAdjust)
|
---|
538 | */
|
---|
539 | uint32_t TimeSyncSetThreshold = g_TimeSyncSetThreshold;
|
---|
540 | if (g_fTimeSyncSetOnRestore)
|
---|
541 | {
|
---|
542 | uint64_t idNewSession = g_idTimeSyncSession;
|
---|
543 | VbglR3GetSessionId(&idNewSession);
|
---|
544 | if (idNewSession != g_idTimeSyncSession)
|
---|
545 | {
|
---|
546 | VBoxServiceVerbose(3, "VBoxServiceTimeSyncWorker: The VM session ID changed, forcing resync.\n");
|
---|
547 | TimeSyncSetThreshold = 0;
|
---|
548 | g_idTimeSyncSession = idNewSession;
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 | /*
|
---|
553 | * Calculate the adjustment threshold and the current drift.
|
---|
554 | */
|
---|
555 | uint32_t MinAdjust = RTTimeSpecGetMilli(&GuestElapsed) * g_TimeSyncLatencyFactor;
|
---|
556 | if (MinAdjust < g_TimeSyncMinAdjust)
|
---|
557 | MinAdjust = g_TimeSyncMinAdjust;
|
---|
558 |
|
---|
559 | RTTIMESPEC Drift = HostNow;
|
---|
560 | RTTimeSpecSub(&Drift, &GuestNow);
|
---|
561 | if (RTTimeSpecGetMilli(&Drift) < 0)
|
---|
562 | MinAdjust += g_TimeSyncMinAdjust; /* extra buffer against moving time backwards. */
|
---|
563 |
|
---|
564 | RTTIMESPEC AbsDrift = Drift;
|
---|
565 | RTTimeSpecAbsolute(&AbsDrift);
|
---|
566 | if (g_cVerbosity >= 3)
|
---|
567 | {
|
---|
568 | VBoxServiceVerbose(3, "VBoxServiceTimeSyncWorker: Host: %s (MinAdjust: %RU32 ms)\n",
|
---|
569 | RTTimeToString(RTTimeExplode(&Time, &HostNow), sz, sizeof(sz)), MinAdjust);
|
---|
570 | VBoxServiceVerbose(3, "VBoxServiceTimeSyncWorker: Guest: - %s => %RDtimespec drift\n",
|
---|
571 | RTTimeToString(RTTimeExplode(&Time, &GuestNow), sz, sizeof(sz)),
|
---|
572 | &Drift);
|
---|
573 | }
|
---|
574 |
|
---|
575 | uint32_t AbsDriftMilli = RTTimeSpecGetMilli(&AbsDrift);
|
---|
576 | if (AbsDriftMilli > MinAdjust)
|
---|
577 | {
|
---|
578 | /*
|
---|
579 | * Ok, the drift is above the threshold.
|
---|
580 | *
|
---|
581 | * Try a gradual adjustment first, if that fails or the drift is
|
---|
582 | * too big, fall back on just setting the time.
|
---|
583 | */
|
---|
584 |
|
---|
585 | if ( AbsDriftMilli > TimeSyncSetThreshold
|
---|
586 | || g_fTimeSyncSetNext
|
---|
587 | || !VBoxServiceTimeSyncAdjust(&Drift))
|
---|
588 | {
|
---|
589 | VBoxServiceTimeSyncCancelAdjust();
|
---|
590 | VBoxServiceTimeSyncSet(&Drift);
|
---|
591 | }
|
---|
592 | }
|
---|
593 | else
|
---|
594 | VBoxServiceTimeSyncCancelAdjust();
|
---|
595 | break;
|
---|
596 | }
|
---|
597 | VBoxServiceVerbose(3, "VBoxServiceTimeSyncWorker: %RDtimespec: latency too high (%RDtimespec) sleeping 1s\n", GuestElapsed);
|
---|
598 | RTThreadSleep(1000);
|
---|
599 | } while (--cTries > 0);
|
---|
600 |
|
---|
601 | /* Clear the set-next/set-start flag. */
|
---|
602 | g_fTimeSyncSetNext = false;
|
---|
603 |
|
---|
604 | /*
|
---|
605 | * Block for a while.
|
---|
606 | *
|
---|
607 | * The event semaphore takes care of ignoring interruptions and it
|
---|
608 | * allows us to implement service wakeup later.
|
---|
609 | */
|
---|
610 | if (*pfShutdown)
|
---|
611 | break;
|
---|
612 | int rc2 = RTSemEventMultiWait(g_TimeSyncEvent, g_TimeSyncInterval);
|
---|
613 | if (*pfShutdown)
|
---|
614 | break;
|
---|
615 | if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
|
---|
616 | {
|
---|
617 | VBoxServiceError("VBoxServiceTimeSyncWorker: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
|
---|
618 | rc = rc2;
|
---|
619 | break;
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 | VBoxServiceTimeSyncCancelAdjust();
|
---|
624 | RTSemEventMultiDestroy(g_TimeSyncEvent);
|
---|
625 | g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
|
---|
626 | return rc;
|
---|
627 | }
|
---|
628 |
|
---|
629 |
|
---|
630 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
631 | static DECLCALLBACK(void) VBoxServiceTimeSyncStop(void)
|
---|
632 | {
|
---|
633 | RTSemEventMultiSignal(g_TimeSyncEvent);
|
---|
634 | }
|
---|
635 |
|
---|
636 |
|
---|
637 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
638 | static DECLCALLBACK(void) VBoxServiceTimeSyncTerm(void)
|
---|
639 | {
|
---|
640 | #ifdef RT_OS_WINDOWS
|
---|
641 | /*
|
---|
642 | * Restore the SE_SYSTEMTIME_NAME token privileges (if init succeeded).
|
---|
643 | */
|
---|
644 | if (g_hTokenProcess)
|
---|
645 | {
|
---|
646 | if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &g_TkOldPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
|
---|
647 | {
|
---|
648 | DWORD dwErr = GetLastError();
|
---|
649 | VBoxServiceError("VBoxServiceTimeSyncTerm: Restoring token privileges (SE_SYSTEMTIME_NAME) failed with code %u!\n", dwErr);
|
---|
650 | }
|
---|
651 | CloseHandle(g_hTokenProcess);
|
---|
652 | g_hTokenProcess = NULL;
|
---|
653 | }
|
---|
654 | #endif /* !RT_OS_WINDOWS */
|
---|
655 |
|
---|
656 | if (g_TimeSyncEvent != NIL_RTSEMEVENTMULTI)
|
---|
657 | {
|
---|
658 | RTSemEventMultiDestroy(g_TimeSyncEvent);
|
---|
659 | g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
|
---|
660 | }
|
---|
661 | }
|
---|
662 |
|
---|
663 |
|
---|
664 | /**
|
---|
665 | * The 'timesync' service description.
|
---|
666 | */
|
---|
667 | VBOXSERVICE g_TimeSync =
|
---|
668 | {
|
---|
669 | /* pszName. */
|
---|
670 | "timesync",
|
---|
671 | /* pszDescription. */
|
---|
672 | "Time synchronization",
|
---|
673 | /* pszUsage. */
|
---|
674 | " [--timesync-interval <ms>] [--timesync-min-adjust <ms>]\n"
|
---|
675 | " [--timesync-latency-factor <x>] [--timesync-max-latency <ms>]\n"
|
---|
676 | " [--timesync-set-threshold <ms>] [--timesync-set-start]\n"
|
---|
677 | " [--timesync-set-on-restore 0|1]"
|
---|
678 | ,
|
---|
679 | /* pszOptions. */
|
---|
680 | " --timesync-interval Specifies the interval at which to synchronize the\n"
|
---|
681 | " time with the host. The default is 10000 ms.\n"
|
---|
682 | " --timesync-min-adjust The minimum absolute drift value measured in\n"
|
---|
683 | " milliseconds to make adjustments for.\n"
|
---|
684 | " The default is 1000 ms on OS/2 and 100 ms elsewhere.\n"
|
---|
685 | " --timesync-latency-factor\n"
|
---|
686 | " The factor to multiply the time query latency with\n"
|
---|
687 | " to calculate the dynamic minimum adjust time.\n"
|
---|
688 | " The default is 8 times.\n"
|
---|
689 | " --timesync-max-latency The max host timer query latency to accept.\n"
|
---|
690 | " The default is 250 ms.\n"
|
---|
691 | " --timesync-set-threshold\n"
|
---|
692 | " The absolute drift threshold, given as milliseconds,\n"
|
---|
693 | " where to start setting the time instead of trying to\n"
|
---|
694 | " adjust it. The default is 20 min.\n"
|
---|
695 | " --timesync-set-start Set the time when starting the time sync service.\n"
|
---|
696 | " --timesync-set-on-restore 0|1\n"
|
---|
697 | " Immediately set the time when the VM was restored.\n"
|
---|
698 | " 1 = set (default), 0 = don't set.\n"
|
---|
699 | ,
|
---|
700 | /* methods */
|
---|
701 | VBoxServiceTimeSyncPreInit,
|
---|
702 | VBoxServiceTimeSyncOption,
|
---|
703 | VBoxServiceTimeSyncInit,
|
---|
704 | VBoxServiceTimeSyncWorker,
|
---|
705 | VBoxServiceTimeSyncStop,
|
---|
706 | VBoxServiceTimeSyncTerm
|
---|
707 | };
|
---|
708 |
|
---|