]>
Commit | Line | Data |
---|---|---|
7eda085c KZ |
1 | /* |
2 | * hwclock.c | |
3 | * | |
4 | * clock.c was written by Charles Hedrick, hedrick@cs.rutgers.edu, Apr 1992 | |
5 | * Modified for clock adjustments - Rob Hooft <hooft@chem.ruu.nl>, Nov 1992 | |
6 | * Improvements by Harald Koenig <koenig@nova.tat.physik.uni-tuebingen.de> | |
7 | * and Alan Modra <alan@spri.levels.unisa.edu.au>. | |
8 | * | |
9 | * Major rewrite by Bryan Henderson <bryanh@giraffe-data.com>, 96.09.19. | |
10 | * The new program is called hwclock. New features: | |
ef71b8f1 SK |
11 | * |
12 | * - You can set the hardware clock without also modifying the system | |
13 | * clock. | |
14 | * - You can read and set the clock with finer than 1 second precision. | |
15 | * - When you set the clock, hwclock automatically refigures the drift | |
16 | * rate, based on how far off the clock was before you set it. | |
7eda085c KZ |
17 | * |
18 | * Reshuffled things, added sparc code, and re-added alpha stuff | |
19 | * by David Mosberger <davidm@azstarnet.com> | |
9abb2685 | 20 | * and Jay Estabrook <jestabro@amt.tay1.dec.com> |
7eda085c KZ |
21 | * and Martin Ostermann <ost@coments.rwth-aachen.de>, aeb@cwi.nl, 990212. |
22 | * | |
ef71b8f1 | 23 | * Fix for Award 2094 bug, Dave Coffin (dcoffin@shore.net) 11/12/98 |
22853e4a | 24 | * Change of local time handling, Stefan Ring <e9725446@stud3.tuwien.ac.at> |
63cccae4 | 25 | * Change of adjtime handling, James P. Rutledge <ao112@rgfn.epcc.edu>. |
66ee8158 KZ |
26 | * |
27 | * Distributed under GPL | |
7eda085c | 28 | */ |
7eda085c KZ |
29 | /* |
30 | * Explanation of `adjusting' (Rob Hooft): | |
31 | * | |
32 | * The problem with my machine is that its CMOS clock is 10 seconds | |
33 | * per day slow. With this version of clock.c, and my '/etc/rc.local' | |
34 | * reading '/etc/clock -au' instead of '/etc/clock -u -s', this error | |
35 | * is automatically corrected at every boot. | |
36 | * | |
37 | * To do this job, the program reads and writes the file '/etc/adjtime' | |
38 | * to determine the correction, and to save its data. In this file are | |
39 | * three numbers: | |
40 | * | |
ef71b8f1 SK |
41 | * 1) the correction in seconds per day. (So if your clock runs 5 |
42 | * seconds per day fast, the first number should read -5.0) | |
43 | * 2) the number of seconds since 1/1/1970 the last time the program | |
44 | * was used | |
45 | * 3) the remaining part of a second which was leftover after the last | |
46 | * adjustment | |
7eda085c KZ |
47 | * |
48 | * Installation and use of this program: | |
49 | * | |
ef71b8f1 SK |
50 | * a) create a file '/etc/adjtime' containing as the first and only |
51 | * line: '0.0 0 0.0' | |
52 | * b) run 'clock -au' or 'clock -a', depending on whether your cmos is | |
53 | * in universal or local time. This updates the second number. | |
54 | * c) set your system time using the 'date' command. | |
55 | * d) update your cmos time using 'clock -wu' or 'clock -w' | |
56 | * e) replace the first number in /etc/adjtime by your correction. | |
57 | * f) put the command 'clock -au' or 'clock -a' in your '/etc/rc.local' | |
7eda085c KZ |
58 | */ |
59 | ||
7eda085c | 60 | #include <errno.h> |
63cccae4 | 61 | #include <getopt.h> |
33ed2d02 | 62 | #include <limits.h> |
83aa4ad7 | 63 | #include <math.h> |
998f392a SK |
64 | #include <stdarg.h> |
65 | #include <stdio.h> | |
66 | #include <stdlib.h> | |
67 | #include <string.h> | |
63cccae4 | 68 | #include <sysexits.h> |
998f392a SK |
69 | #include <sys/stat.h> |
70 | #include <sys/time.h> | |
71 | #include <time.h> | |
72 | #include <unistd.h> | |
7eda085c | 73 | |
e1f4706d SK |
74 | #define OPTUTILS_EXIT_CODE EX_USAGE |
75 | ||
998f392a | 76 | #include "c.h" |
db116df7 | 77 | #include "closestream.h" |
7eda085c | 78 | #include "nls.h" |
e1f4706d | 79 | #include "optutils.h" |
9d413ecb | 80 | #include "pathnames.h" |
4ac41d61 | 81 | #include "strutils.h" |
c7f75390 | 82 | #include "hwclock.h" |
7eda085c | 83 | |
88058a71 KZ |
84 | #ifdef HAVE_LIBAUDIT |
85 | #include <libaudit.h> | |
86 | static int hwaudit_fd = -1; | |
87 | static int hwaudit_on; | |
88 | #endif | |
89 | ||
7eda085c KZ |
90 | /* The struct that holds our hardware access routines */ |
91 | struct clock_ops *ur; | |
92 | ||
93 | #define FLOOR(arg) ((arg >= 0 ? (int) arg : ((int) arg) - 1)); | |
94 | ||
f196fd1a SB |
95 | /* Maximal clock adjustment in seconds per day. |
96 | (adjtime() glibc call has 2145 seconds limit on i386, so it is good enough for us as well, | |
97 | 43219 is a maximal safe value preventing exact_adjustment overflow.) */ | |
98 | #define MAX_DRIFT 2145.0 | |
99 | ||
da82f6fe KZ |
100 | const char *adj_file_name = NULL; |
101 | ||
7eda085c | 102 | struct adjtime { |
ef71b8f1 SK |
103 | /* |
104 | * This is information we keep in the adjtime file that tells us how | |
105 | * to do drift corrections. Elements are all straight from the | |
106 | * adjtime file, so see documentation of that file for details. | |
107 | * Exception is <dirty>, which is an indication that what's in this | |
108 | * structure is not what's in the disk file (because it has been | |
109 | * updated since read from the disk file). | |
110 | */ | |
111 | bool dirty; | |
112 | /* line 1 */ | |
113 | double drift_factor; | |
114 | time_t last_adj_time; | |
115 | double not_adjusted; | |
116 | /* line 2 */ | |
117 | time_t last_calib_time; | |
118 | /* | |
119 | * The most recent time that we set the clock from an external | |
120 | * authority (as opposed to just doing a drift adjustment) | |
121 | */ | |
122 | /* line 3 */ | |
123 | enum a_local_utc { LOCAL, UTC, UNKNOWN } local_utc; | |
124 | /* | |
125 | * To which time zone, local or UTC, we most recently set the | |
126 | * hardware clock. | |
127 | */ | |
7eda085c KZ |
128 | }; |
129 | ||
ef71b8f1 SK |
130 | /* |
131 | * We are running in debug mode, wherein we put a lot of information about | |
132 | * what we're doing to standard output. | |
133 | */ | |
4a44a54b | 134 | int debug; |
7eda085c | 135 | |
ef71b8f1 | 136 | /* Workaround for Award 4.50g BIOS bug: keep the year in a file. */ |
7eda085c | 137 | bool badyear; |
7eda085c | 138 | |
ef71b8f1 | 139 | /* User-specified epoch, used when rtc fails to return epoch. */ |
4ac41d61 | 140 | unsigned long epoch_option = -1; |
22853e4a | 141 | |
7eda085c | 142 | /* |
ef71b8f1 SK |
143 | * Almost all Award BIOS's made between 04/26/94 and 05/31/95 have a nasty |
144 | * bug limiting the RTC year byte to the range 94-99. Any year between 2000 | |
145 | * and 2093 gets changed to 2094, every time you start the system. | |
146 | * | |
147 | * With the --badyear option, we write the date to file and hope that the | |
148 | * file is updated at least once a year. I recommend putting this command | |
149 | * "hwclock --badyear" in the monthly crontab, just to be safe. | |
150 | * | |
151 | * -- Dave Coffin 11/12/98 | |
7eda085c | 152 | */ |
ef71b8f1 SK |
153 | static void write_date_to_file(struct tm *tm) |
154 | { | |
155 | FILE *fp; | |
156 | ||
9d413ecb | 157 | if ((fp = fopen(_PATH_LASTDATE, "w"))) { |
ef71b8f1 SK |
158 | fprintf(fp, "%02d.%02d.%04d\n", tm->tm_mday, tm->tm_mon + 1, |
159 | tm->tm_year + 1900); | |
db116df7 SK |
160 | if (close_stream(fp) != 0) |
161 | warn(_("cannot write %s"), _PATH_LASTDATE); | |
ef71b8f1 | 162 | } else |
9d413ecb | 163 | warn(_("cannot write %s"), _PATH_LASTDATE); |
7eda085c KZ |
164 | } |
165 | ||
ef71b8f1 SK |
166 | static void read_date_from_file(struct tm *tm) |
167 | { | |
168 | int last_mday, last_mon, last_year; | |
169 | FILE *fp; | |
170 | ||
9d413ecb | 171 | if ((fp = fopen(_PATH_LASTDATE, "r"))) { |
ef71b8f1 SK |
172 | if (fscanf(fp, "%d.%d.%d\n", &last_mday, &last_mon, &last_year) |
173 | == 3) { | |
174 | tm->tm_year = last_year - 1900; | |
175 | if ((tm->tm_mon << 5) + tm->tm_mday < | |
176 | ((last_mon - 1) << 5) + last_mday) | |
177 | tm->tm_year++; | |
178 | } | |
179 | fclose(fp); | |
180 | } | |
181 | write_date_to_file(tm); | |
7eda085c KZ |
182 | } |
183 | ||
2794995a WP |
184 | /* |
185 | * time_t to timeval conversion. | |
186 | */ | |
187 | static struct timeval t2tv(time_t timet) | |
188 | { | |
189 | struct timeval rettimeval; | |
190 | ||
191 | rettimeval.tv_sec = timet; | |
192 | rettimeval.tv_usec = 0; | |
193 | return rettimeval; | |
194 | } | |
195 | ||
ef71b8f1 SK |
196 | /* |
197 | * The difference in seconds between two times in "timeval" format. | |
198 | */ | |
199 | double time_diff(struct timeval subtrahend, struct timeval subtractor) | |
200 | { | |
201 | return (subtrahend.tv_sec - subtractor.tv_sec) | |
202 | + (subtrahend.tv_usec - subtractor.tv_usec) / 1E6; | |
7eda085c KZ |
203 | } |
204 | ||
ef71b8f1 SK |
205 | /* |
206 | * The time, in "timeval" format, which is <increment> seconds after the | |
207 | * time <addend>. Of course, <increment> may be negative. | |
208 | */ | |
209 | static struct timeval time_inc(struct timeval addend, double increment) | |
210 | { | |
211 | struct timeval newtime; | |
212 | ||
213 | newtime.tv_sec = addend.tv_sec + (int)increment; | |
214 | newtime.tv_usec = addend.tv_usec + (increment - (int)increment) * 1E6; | |
215 | ||
216 | /* | |
217 | * Now adjust it so that the microsecond value is between 0 and 1 | |
218 | * million. | |
219 | */ | |
220 | if (newtime.tv_usec < 0) { | |
221 | newtime.tv_usec += 1E6; | |
222 | newtime.tv_sec -= 1; | |
223 | } else if (newtime.tv_usec >= 1E6) { | |
224 | newtime.tv_usec -= 1E6; | |
225 | newtime.tv_sec += 1; | |
226 | } | |
227 | return newtime; | |
7eda085c KZ |
228 | } |
229 | ||
eb63b9b8 KZ |
230 | static bool |
231 | hw_clock_is_utc(const bool utc, const bool local_opt, | |
ef71b8f1 SK |
232 | const struct adjtime adjtime) |
233 | { | |
eb63b9b8 KZ |
234 | bool ret; |
235 | ||
236 | if (utc) | |
237 | ret = TRUE; /* --utc explicitly given on command line */ | |
238 | else if (local_opt) | |
239 | ret = FALSE; /* --localtime explicitly given */ | |
240 | else | |
ef71b8f1 | 241 | /* get info from adjtime file - default is UTC */ |
7894bf0f | 242 | ret = (adjtime.local_utc != LOCAL); |
eb63b9b8 KZ |
243 | if (debug) |
244 | printf(_("Assuming hardware clock is kept in %s time.\n"), | |
245 | ret ? _("UTC") : _("local")); | |
246 | return ret; | |
247 | } | |
248 | ||
ef71b8f1 SK |
249 | /* |
250 | * Read the adjustment parameters out of the /etc/adjtime file. | |
251 | * | |
252 | * Return them as the adjtime structure <*adjtime_p>. If there is no | |
253 | * /etc/adjtime file, return defaults. If values are missing from the file, | |
254 | * return defaults for them. | |
255 | * | |
256 | * return value 0 if all OK, !=0 otherwise. | |
257 | */ | |
258 | static int read_adjtime(struct adjtime *adjtime_p) | |
259 | { | |
260 | FILE *adjfile; | |
261 | int rc; /* local return code */ | |
262 | struct stat statbuf; /* We don't even use the contents of this. */ | |
263 | char line1[81]; /* String: first line of adjtime file */ | |
264 | char line2[81]; /* String: second line of adjtime file */ | |
265 | char line3[81]; /* String: third line of adjtime file */ | |
266 | long timeval; | |
267 | ||
268 | rc = stat(adj_file_name, &statbuf); | |
269 | if (rc < 0 && errno == ENOENT) { | |
270 | /* He doesn't have a adjtime file, so we'll use defaults. */ | |
271 | adjtime_p->drift_factor = 0; | |
272 | adjtime_p->last_adj_time = 0; | |
273 | adjtime_p->not_adjusted = 0; | |
274 | adjtime_p->last_calib_time = 0; | |
bf619967 | 275 | adjtime_p->local_utc = UTC; |
ef71b8f1 SK |
276 | adjtime_p->dirty = FALSE; /* don't create a zero adjfile */ |
277 | ||
278 | return 0; | |
279 | } | |
eb63b9b8 | 280 | |
ef71b8f1 SK |
281 | adjfile = fopen(adj_file_name, "r"); /* open file for reading */ |
282 | if (adjfile == NULL) { | |
8c219bf4 | 283 | warn(_("cannot open %s"), adj_file_name); |
ef71b8f1 | 284 | return EX_OSFILE; |
eb63b9b8 | 285 | } |
7eda085c KZ |
286 | |
287 | ||
ef71b8f1 SK |
288 | if (!fgets(line1, sizeof(line1), adjfile)) |
289 | line1[0] = '\0'; /* In case fgets fails */ | |
290 | if (!fgets(line2, sizeof(line2), adjfile)) | |
291 | line2[0] = '\0'; /* In case fgets fails */ | |
292 | if (!fgets(line3, sizeof(line3), adjfile)) | |
293 | line3[0] = '\0'; /* In case fgets fails */ | |
294 | ||
295 | fclose(adjfile); | |
296 | ||
297 | /* Set defaults in case values are missing from file */ | |
298 | adjtime_p->drift_factor = 0; | |
299 | adjtime_p->last_adj_time = 0; | |
300 | adjtime_p->not_adjusted = 0; | |
301 | adjtime_p->last_calib_time = 0; | |
302 | timeval = 0; | |
303 | ||
304 | sscanf(line1, "%lf %ld %lf", | |
305 | &adjtime_p->drift_factor, | |
306 | &timeval, &adjtime_p->not_adjusted); | |
307 | adjtime_p->last_adj_time = timeval; | |
308 | ||
309 | sscanf(line2, "%ld", &timeval); | |
310 | adjtime_p->last_calib_time = timeval; | |
311 | ||
312 | if (!strcmp(line3, "UTC\n")) { | |
313 | adjtime_p->local_utc = UTC; | |
314 | } else if (!strcmp(line3, "LOCAL\n")) { | |
315 | adjtime_p->local_utc = LOCAL; | |
316 | } else { | |
317 | adjtime_p->local_utc = UNKNOWN; | |
318 | if (line3[0]) { | |
111c05d3 SK |
319 | warnx(_("Warning: unrecognized third line in adjtime file\n" |
320 | "(Expected: `UTC' or `LOCAL' or nothing.)")); | |
ef71b8f1 SK |
321 | } |
322 | } | |
7eda085c | 323 | |
ef71b8f1 | 324 | adjtime_p->dirty = FALSE; |
7eda085c | 325 | |
ef71b8f1 SK |
326 | if (debug) { |
327 | printf(_ | |
328 | ("Last drift adjustment done at %ld seconds after 1969\n"), | |
329 | (long)adjtime_p->last_adj_time); | |
330 | printf(_("Last calibration done at %ld seconds after 1969\n"), | |
331 | (long)adjtime_p->last_calib_time); | |
332 | printf(_("Hardware clock is on %s time\n"), | |
333 | (adjtime_p->local_utc == | |
334 | LOCAL) ? _("local") : (adjtime_p->local_utc == | |
335 | UTC) ? _("UTC") : _("unknown")); | |
336 | } | |
337 | ||
338 | return 0; | |
339 | } | |
7eda085c | 340 | |
ef71b8f1 SK |
341 | /* |
342 | * Wait until the falling edge of the Hardware Clock's update flag so that | |
343 | * any time that is read from the clock immediately after we return will be | |
344 | * exact. | |
345 | * | |
346 | * The clock only has 1 second precision, so it gives the exact time only | |
347 | * once per second, right on the falling edge of the update flag. | |
348 | * | |
349 | * We wait (up to one second) either blocked waiting for an rtc device or in | |
350 | * a CPU spin loop. The former is probably not very accurate. | |
351 | * | |
352 | * Return 0 if it worked, nonzero if it didn't. | |
353 | */ | |
354 | static int synchronize_to_clock_tick(void) | |
355 | { | |
63cccae4 | 356 | int rc; |
7eda085c | 357 | |
ef71b8f1 SK |
358 | if (debug) |
359 | printf(_("Waiting for clock tick...\n")); | |
7eda085c | 360 | |
63cccae4 KZ |
361 | rc = ur->synchronize_to_clock_tick(); |
362 | ||
3b96a7ac KZ |
363 | if (debug) { |
364 | if (rc) | |
365 | printf(_("...synchronization failed\n")); | |
366 | else | |
367 | printf(_("...got clock tick\n")); | |
368 | } | |
63cccae4 KZ |
369 | |
370 | return rc; | |
7eda085c KZ |
371 | } |
372 | ||
ef71b8f1 SK |
373 | /* |
374 | * Convert a time in broken down format (hours, minutes, etc.) into standard | |
375 | * unix time (seconds into epoch). Return it as *systime_p. | |
376 | * | |
377 | * The broken down time is argument <tm>. This broken down time is either | |
378 | * in local time zone or UTC, depending on value of logical argument | |
379 | * "universal". True means it is in UTC. | |
380 | * | |
381 | * If the argument contains values that do not constitute a valid time, and | |
382 | * mktime() recognizes this, return *valid_p == false and *systime_p | |
383 | * undefined. However, mktime() sometimes goes ahead and computes a | |
384 | * fictional time "as if" the input values were valid, e.g. if they indicate | |
385 | * the 31st day of April, mktime() may compute the time of May 1. In such a | |
386 | * case, we return the same fictional value mktime() does as *systime_p and | |
387 | * return *valid_p == true. | |
388 | */ | |
7eda085c | 389 | static void |
9abb2685 | 390 | mktime_tz(struct tm tm, const bool universal, |
ef71b8f1 SK |
391 | bool * valid_p, time_t * systime_p) |
392 | { | |
393 | time_t mktime_result; /* The value returned by our mktime() call */ | |
394 | char *zone; /* Local time zone name */ | |
395 | ||
396 | /* | |
397 | * We use the C library function mktime(), but since it only works | |
398 | * on local time zone input, we may have to fake it out by | |
399 | * temporarily changing the local time zone to UTC. | |
400 | */ | |
401 | zone = getenv("TZ"); /* remember original time zone */ | |
402 | if (universal) { | |
d53f8ecf JP |
403 | /* Set timezone to UTC as defined by the environment |
404 | * variable TZUTC. TZUTC undefined gives the default UTC | |
405 | * zonefile which usually does not take into account leap | |
406 | * seconds. Define TZUTC to select your UTC zonefile which | |
407 | * does include leap seconds. For example, with recent GNU | |
408 | * libc's: | |
409 | * TZUTC=:/usr/share/zoneinfo/right/UTC | |
410 | */ | |
411 | setenv("TZ", getenv("TZUTC"), TRUE); | |
ef71b8f1 SK |
412 | /* |
413 | * Note: tzset() gets called implicitly by the time code, | |
414 | * but only the first time. When changing the environment | |
415 | * variable, better call tzset() explicitly. | |
416 | */ | |
417 | tzset(); | |
418 | } | |
419 | mktime_result = mktime(&tm); | |
420 | if (mktime_result == -1) { | |
421 | /* | |
422 | * This apparently (not specified in mktime() documentation) | |
423 | * means the 'tm' structure does not contain valid values | |
424 | * (however, not containing valid values does _not_ imply | |
425 | * mktime() returns -1). | |
426 | */ | |
427 | *valid_p = FALSE; | |
428 | *systime_p = 0; | |
429 | if (debug) | |
430 | printf(_("Invalid values in hardware clock: " | |
431 | "%4d/%.2d/%.2d %.2d:%.2d:%.2d\n"), | |
432 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, | |
433 | tm.tm_hour, tm.tm_min, tm.tm_sec); | |
434 | } else { | |
435 | *valid_p = TRUE; | |
436 | *systime_p = mktime_result; | |
437 | if (debug) | |
438 | printf(_ | |
439 | ("Hw clock time : %4d/%.2d/%.2d %.2d:%.2d:%.2d = " | |
440 | "%ld seconds since 1969\n"), tm.tm_year + 1900, | |
441 | tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, | |
442 | tm.tm_sec, (long)*systime_p); | |
443 | } | |
444 | /* now put back the original zone. */ | |
445 | if (zone) | |
446 | setenv("TZ", zone, TRUE); | |
447 | else | |
448 | unsetenv("TZ"); | |
449 | tzset(); | |
7eda085c KZ |
450 | } |
451 | ||
ef71b8f1 SK |
452 | /* |
453 | * Read the hardware clock and return the current time via <tm> argument. | |
454 | * | |
455 | * Use the method indicated by <method> argument to access the hardware | |
456 | * clock. | |
457 | */ | |
cdedde03 | 458 | static int |
ef71b8f1 SK |
459 | read_hardware_clock(const bool universal, bool * valid_p, time_t * systime_p) |
460 | { | |
461 | struct tm tm; | |
462 | int err; | |
7eda085c | 463 | |
ef71b8f1 SK |
464 | err = ur->read_hardware_clock(&tm); |
465 | if (err) | |
466 | return err; | |
7eda085c | 467 | |
ef71b8f1 SK |
468 | if (badyear) |
469 | read_date_from_file(&tm); | |
7eda085c | 470 | |
ef71b8f1 SK |
471 | if (debug) |
472 | printf(_ | |
473 | ("Time read from Hardware Clock: %4d/%.2d/%.2d %02d:%02d:%02d\n"), | |
474 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, | |
475 | tm.tm_min, tm.tm_sec); | |
476 | mktime_tz(tm, universal, valid_p, systime_p); | |
cdedde03 | 477 | |
ef71b8f1 | 478 | return 0; |
7eda085c KZ |
479 | } |
480 | ||
ef71b8f1 SK |
481 | /* |
482 | * Set the Hardware Clock to the time <newtime>, in local time zone or UTC, | |
483 | * according to <universal>. | |
484 | */ | |
7eda085c | 485 | static void |
9abb2685 | 486 | set_hardware_clock(const time_t newtime, |
ef71b8f1 SK |
487 | const bool universal, const bool testing) |
488 | { | |
489 | struct tm new_broken_time; | |
490 | /* | |
491 | * Time to which we will set Hardware Clock, in broken down format, | |
492 | * in the time zone of caller's choice | |
493 | */ | |
494 | ||
495 | if (universal) | |
496 | new_broken_time = *gmtime(&newtime); | |
497 | else | |
498 | new_broken_time = *localtime(&newtime); | |
7eda085c | 499 | |
ef71b8f1 SK |
500 | if (debug) |
501 | printf(_("Setting Hardware Clock to %.2d:%.2d:%.2d " | |
502 | "= %ld seconds since 1969\n"), | |
503 | new_broken_time.tm_hour, new_broken_time.tm_min, | |
504 | new_broken_time.tm_sec, (long)newtime); | |
7eda085c | 505 | |
ef71b8f1 SK |
506 | if (testing) |
507 | printf(_("Clock not changed - testing only.\n")); | |
508 | else { | |
509 | if (badyear) { | |
510 | /* | |
511 | * Write the real year to a file, then write a fake | |
512 | * year between 1995 and 1998 to the RTC. This way, | |
513 | * Award BIOS boots on 29 Feb 2000 thinking that | |
514 | * it's 29 Feb 1996. | |
515 | */ | |
516 | write_date_to_file(&new_broken_time); | |
517 | new_broken_time.tm_year = | |
518 | 95 + ((new_broken_time.tm_year + 1) & 3); | |
519 | } | |
520 | ur->set_hardware_clock(&new_broken_time); | |
521 | } | |
522 | } | |
7eda085c | 523 | |
ef71b8f1 SK |
524 | /* |
525 | * Set the Hardware Clock to the time "sethwtime", in local time zone or | |
526 | * UTC, according to "universal". | |
527 | * | |
528 | * Wait for a fraction of a second so that "sethwtime" is the value of the | |
529 | * Hardware Clock as of system time "refsystime", which is in the past. For | |
530 | * example, if "sethwtime" is 14:03:05 and "refsystime" is 12:10:04.5 and | |
531 | * the current system time is 12:10:06.0: Wait .5 seconds (to make exactly 2 | |
532 | * seconds since "refsystime") and then set the Hardware Clock to 14:03:07, | |
533 | * thus getting a precise and retroactive setting of the clock. | |
534 | * | |
535 | * (Don't be confused by the fact that the system clock and the Hardware | |
536 | * Clock differ by two hours in the above example. That's just to remind you | |
537 | * that there are two independent time scales here). | |
538 | * | |
539 | * This function ought to be able to accept set times as fractional times. | |
540 | * Idea for future enhancement. | |
541 | */ | |
7eda085c | 542 | static void |
9abb2685 | 543 | set_hardware_clock_exact(const time_t sethwtime, |
ef71b8f1 SK |
544 | const struct timeval refsystime, |
545 | const bool universal, const bool testing) | |
546 | { | |
ef71b8f1 | 547 | /* |
4a44a54b CM |
548 | * The Hardware Clock can only be set to any integer time plus one |
549 | * half second. The integer time is required because there is no | |
550 | * interface to set or get a fractional second. The additional half | |
551 | * second is because the Hardware Clock updates to the following | |
552 | * second precisely 500 ms (not 1 second!) after you release the | |
553 | * divider reset (after setting the new time) - see description of | |
554 | * DV2, DV1, DV0 in Register A in the MC146818A data sheet (and note | |
555 | * that although that document doesn't say so, real-world code seems | |
556 | * to expect that the SET bit in Register B functions the same way). | |
557 | * That means that, e.g., when you set the clock to 1:02:03, it | |
558 | * effectively really sets it to 1:02:03.5, because it will update to | |
559 | * 1:02:04 only half a second later. Our caller passes the desired | |
560 | * integer Hardware Clock time in sethwtime, and the corresponding | |
561 | * system time (which may have a fractional part, and which may or may | |
562 | * not be the same!) in refsystime. In an ideal situation, we would | |
563 | * then apply sethwtime to the Hardware Clock at refsystime+500ms, so | |
564 | * that when the Hardware Clock ticks forward to sethwtime+1s half a | |
565 | * second later at refsystime+1000ms, everything is in sync. So we | |
566 | * spin, waiting for gettimeofday() to return a time at or after that | |
567 | * time (refsystime+500ms) up to a tolerance value, initially 1ms. If | |
568 | * we miss that time due to being preempted for some other process, | |
569 | * then we increase the margin a little bit (initially 1ms, doubling | |
570 | * each time), add 1 second (or more, if needed to get a time that is | |
571 | * in the future) to both the time for which we are waiting and the | |
572 | * time that we will apply to the Hardware Clock, and start waiting | |
573 | * again. | |
574 | * | |
575 | * For example, the caller requests that we set the Hardware Clock to | |
576 | * 1:02:03, with reference time (current system time) = 6:07:08.250. | |
577 | * We want the Hardware Clock to update to 1:02:04 at 6:07:09.250 on | |
578 | * the system clock, and the first such update will occur 0.500 | |
579 | * seconds after we write to the Hardware Clock, so we spin until the | |
580 | * system clock reads 6:07:08.750. If we get there, great, but let's | |
581 | * imagine the system is so heavily loaded that our process is | |
582 | * preempted and by the time we get to run again, the system clock | |
583 | * reads 6:07:11.990. We now want to wait until the next xx:xx:xx.750 | |
584 | * time, which is 6:07:12.750 (4.5 seconds after the reference time), | |
585 | * at which point we will set the Hardware Clock to 1:02:07 (4 seconds | |
586 | * after the originally requested time). If we do that successfully, | |
587 | * then at 6:07:13.250 (5 seconds after the reference time), the | |
588 | * Hardware Clock will update to 1:02:08 (5 seconds after the | |
589 | * originally requested time), and all is well thereafter. | |
ef71b8f1 | 590 | */ |
4a44a54b CM |
591 | |
592 | time_t newhwtime = sethwtime; | |
593 | double target_time_tolerance_secs = 0.001; /* initial value */ | |
594 | double tolerance_incr_secs = 0.001; /* initial value */ | |
595 | const double RTC_SET_DELAY_SECS = 0.5; /* 500 ms */ | |
596 | const struct timeval RTC_SET_DELAY_TV = { 0, RTC_SET_DELAY_SECS * 1E6 }; | |
597 | ||
598 | struct timeval targetsystime; | |
599 | struct timeval nowsystime; | |
600 | struct timeval prevsystime = refsystime; | |
601 | double deltavstarget; | |
602 | ||
603 | timeradd(&refsystime, &RTC_SET_DELAY_TV, &targetsystime); | |
604 | ||
605 | while (1) { | |
606 | double ticksize; | |
607 | ||
608 | /* FOR TESTING ONLY: inject random delays of up to 1000ms */ | |
609 | if (debug >= 10) { | |
610 | int usec = random() % 1000000; | |
611 | printf(_("sleeping ~%d usec\n"), usec); | |
612 | usleep(usec); | |
ea0804b0 SK |
613 | } |
614 | ||
ef71b8f1 | 615 | gettimeofday(&nowsystime, NULL); |
4a44a54b CM |
616 | deltavstarget = time_diff(nowsystime, targetsystime); |
617 | ticksize = time_diff(nowsystime, prevsystime); | |
618 | prevsystime = nowsystime; | |
619 | ||
620 | if (ticksize < 0) { | |
621 | if (debug) | |
622 | printf(_("time jumped backward %.6f seconds " | |
623 | "to %ld.%06d - retargeting\n"), | |
624 | ticksize, (long)nowsystime.tv_sec, | |
625 | (int)nowsystime.tv_usec); | |
626 | /* The retarget is handled at the end of the loop. */ | |
627 | } else if (deltavstarget < 0) { | |
628 | /* deltavstarget < 0 if current time < target time */ | |
629 | if (debug >= 2) | |
630 | printf(_("%ld.%06d < %ld.%06d (%.6f)\n"), | |
631 | (long)nowsystime.tv_sec, | |
632 | (int)nowsystime.tv_usec, | |
633 | (long)targetsystime.tv_sec, | |
634 | (int)targetsystime.tv_usec, | |
635 | deltavstarget); | |
636 | continue; /* not there yet - keep spinning */ | |
637 | } else if (deltavstarget <= target_time_tolerance_secs) { | |
638 | /* Close enough to the target time; done waiting. */ | |
639 | break; | |
640 | } else /* (deltavstarget > target_time_tolerance_secs) */ { | |
641 | /* | |
642 | * We missed our window. Increase the tolerance and | |
643 | * aim for the next opportunity. | |
644 | */ | |
645 | if (debug) | |
646 | printf(_("missed it - %ld.%06d is too far " | |
647 | "past %ld.%06d (%.6f > %.6f)\n"), | |
648 | (long)nowsystime.tv_sec, | |
649 | (int)nowsystime.tv_usec, | |
650 | (long)targetsystime.tv_sec, | |
651 | (int)targetsystime.tv_usec, | |
652 | deltavstarget, | |
653 | target_time_tolerance_secs); | |
654 | target_time_tolerance_secs += tolerance_incr_secs; | |
655 | tolerance_incr_secs *= 2; | |
ea0804b0 | 656 | } |
4a44a54b CM |
657 | |
658 | /* | |
659 | * Aim for the same offset (tv_usec) within the second in | |
660 | * either the current second (if that offset hasn't arrived | |
661 | * yet), or the next second. | |
662 | */ | |
663 | if (nowsystime.tv_usec < targetsystime.tv_usec) | |
664 | targetsystime.tv_sec = nowsystime.tv_sec; | |
665 | else | |
666 | targetsystime.tv_sec = nowsystime.tv_sec + 1; | |
667 | } | |
668 | ||
669 | newhwtime = sethwtime | |
670 | + (int)(time_diff(nowsystime, refsystime) | |
671 | - RTC_SET_DELAY_SECS /* don't count this */ | |
672 | + 0.5 /* for rounding */); | |
673 | if (debug) | |
674 | printf(_("%ld.%06d is close enough to %ld.%06d (%.6f < %.6f)\n" | |
675 | "Set RTC to %ld (%ld + %d; refsystime = %ld.%06d)\n"), | |
676 | (long)nowsystime.tv_sec, (int)nowsystime.tv_usec, | |
677 | (long)targetsystime.tv_sec, (int)targetsystime.tv_usec, | |
678 | deltavstarget, target_time_tolerance_secs, | |
679 | (long)newhwtime, (long)sethwtime, | |
680 | (int)(newhwtime - sethwtime), | |
681 | (long)refsystime.tv_sec, (int)refsystime.tv_usec); | |
ef71b8f1 SK |
682 | |
683 | set_hardware_clock(newhwtime, universal, testing); | |
7eda085c KZ |
684 | } |
685 | ||
ef71b8f1 | 686 | /* |
ede32597 | 687 | * Put the time "hwctime" on standard output in display format. Except if |
ef71b8f1 SK |
688 | * hclock_valid == false, just tell standard output that we don't know what |
689 | * time it is. | |
ef71b8f1 | 690 | */ |
7eda085c | 691 | static void |
2794995a | 692 | display_time(const bool hclock_valid, struct timeval hwctime) |
ef71b8f1 SK |
693 | { |
694 | if (!hclock_valid) | |
111c05d3 SK |
695 | warnx(_ |
696 | ("The Hardware Clock registers contain values that are " | |
697 | "either invalid (e.g. 50th day of month) or beyond the range " | |
698 | "we can handle (e.g. Year 2095).")); | |
ef71b8f1 SK |
699 | else { |
700 | struct tm *lt; | |
701 | char *format = "%c"; | |
702 | char ctime_now[200]; | |
7eda085c | 703 | |
2794995a | 704 | lt = localtime(&hwctime.tv_sec); |
ef71b8f1 | 705 | strftime(ctime_now, sizeof(ctime_now), format, lt); |
2794995a | 706 | printf(_("%s .%06d seconds\n"), ctime_now, (int)hwctime.tv_usec); |
ef71b8f1 SK |
707 | } |
708 | } | |
7eda085c | 709 | |
ef71b8f1 SK |
710 | /* |
711 | * Interpret the value of the --date option, which is something like | |
712 | * "13:05:01". In fact, it can be any of the myriad ASCII strings that | |
713 | * specify a time which the "date" program can understand. The date option | |
714 | * value in question is our "dateopt" argument. | |
715 | * | |
716 | * The specified time is in the local time zone. | |
717 | * | |
718 | * Our output, "*time_p", is a seconds-into-epoch time. | |
719 | * | |
720 | * We use the "date" program to interpret the date string. "date" must be | |
721 | * runnable by issuing the command "date" to the /bin/sh shell. That means | |
722 | * in must be in the current PATH. | |
723 | * | |
724 | * If anything goes wrong (and many things can), we return return code 10 | |
725 | * and arbitrary *time_p. Otherwise, return code is 0 and *time_p is valid. | |
726 | */ | |
727 | static int interpret_date_string(const char *date_opt, time_t * const time_p) | |
728 | { | |
e8f26419 KZ |
729 | FILE *date_child_fp; |
730 | char date_resp[100]; | |
ef71b8f1 | 731 | const char magic[] = "seconds-into-epoch="; |
9abb2685 | 732 | char date_command[100]; |
ef71b8f1 SK |
733 | int retcode; /* our eventual return code */ |
734 | int rc; /* local return code */ | |
e8f26419 KZ |
735 | |
736 | if (date_opt == NULL) { | |
111c05d3 | 737 | warnx(_("No --date option specified.")); |
e8f26419 KZ |
738 | return 14; |
739 | } | |
7eda085c | 740 | |
e8f26419 KZ |
741 | /* prevent overflow - a security risk */ |
742 | if (strlen(date_opt) > sizeof(date_command) - 50) { | |
111c05d3 | 743 | warnx(_("--date argument too long")); |
e8f26419 KZ |
744 | return 13; |
745 | } | |
746 | ||
747 | /* Quotes in date_opt would ruin the date command we construct. */ | |
748 | if (strchr(date_opt, '"') != NULL) { | |
111c05d3 SK |
749 | warnx(_ |
750 | ("The value of the --date option is not a valid date.\n" | |
751 | "In particular, it contains quotation marks.")); | |
e8f26419 KZ |
752 | return 12; |
753 | } | |
754 | ||
9abb2685 | 755 | sprintf(date_command, "date --date=\"%s\" +seconds-into-epoch=%%s", |
e8f26419 KZ |
756 | date_opt); |
757 | if (debug) | |
758 | printf(_("Issuing date command: %s\n"), date_command); | |
759 | ||
760 | date_child_fp = popen(date_command, "r"); | |
761 | if (date_child_fp == NULL) { | |
111c05d3 | 762 | warn(_("Unable to run 'date' program in /bin/sh shell. " |
e8f26419 KZ |
763 | "popen() failed")); |
764 | return 10; | |
765 | } | |
766 | ||
72bcf189 | 767 | if (!fgets(date_resp, sizeof(date_resp), date_child_fp)) |
ef71b8f1 | 768 | date_resp[0] = '\0'; /* in case fgets fails */ |
e8f26419 KZ |
769 | if (debug) |
770 | printf(_("response from date command = %s\n"), date_resp); | |
ef71b8f1 | 771 | if (strncmp(date_resp, magic, sizeof(magic) - 1) != 0) { |
111c05d3 | 772 | warnx(_("The date command issued by %s returned " |
e8f26419 KZ |
773 | "unexpected results.\n" |
774 | "The command was:\n %s\n" | |
111c05d3 SK |
775 | "The response was:\n %s"), |
776 | program_invocation_short_name, date_command, date_resp); | |
e8f26419 KZ |
777 | retcode = 8; |
778 | } else { | |
779 | long seconds_since_epoch; | |
ef71b8f1 | 780 | rc = sscanf(date_resp + sizeof(magic) - 1, "%ld", |
e8f26419 KZ |
781 | &seconds_since_epoch); |
782 | if (rc < 1) { | |
111c05d3 SK |
783 | warnx(_("The date command issued by %s returned " |
784 | "something other than an integer where the " | |
785 | "converted time value was expected.\n" | |
786 | "The command was:\n %s\n" | |
787 | "The response was:\n %s\n"), | |
788 | program_invocation_short_name, date_command, | |
789 | date_resp); | |
e8f26419 KZ |
790 | retcode = 6; |
791 | } else { | |
792 | retcode = 0; | |
793 | *time_p = seconds_since_epoch; | |
9abb2685 | 794 | if (debug) |
e8f26419 KZ |
795 | printf(_("date string %s equates to " |
796 | "%ld seconds since 1969.\n"), | |
ef71b8f1 | 797 | date_opt, (long)*time_p); |
e8f26419 KZ |
798 | } |
799 | } | |
49c0c23d | 800 | pclose(date_child_fp); |
e8f26419 | 801 | |
63cccae4 | 802 | return retcode; |
7eda085c KZ |
803 | } |
804 | ||
ef71b8f1 SK |
805 | /* |
806 | * Set the System Clock to time 'newtime'. | |
807 | * | |
808 | * Also set the kernel time zone value to the value indicated by the TZ | |
809 | * environment variable and/or /usr/lib/zoneinfo/, interpreted as tzset() | |
810 | * would interpret them. | |
811 | * | |
d17a12a3 WP |
812 | * If this is the first call of settimeofday since boot, then this also sets |
813 | * the kernel variable persistent_clock_is_local so that NTP 11 minute mode | |
814 | * will update the Hardware Clock with the proper timescale. If the Hardware | |
815 | * Clock's timescale configuration is changed then a reboot is required for | |
816 | * persistent_clock_is_local to be updated. | |
817 | * | |
ef71b8f1 SK |
818 | * EXCEPT: if hclock_valid is false, just issue an error message saying |
819 | * there is no valid time in the Hardware Clock to which to set the system | |
820 | * time. | |
821 | * | |
822 | * If 'testing' is true, don't actually update anything -- just say we would | |
823 | * have. | |
824 | */ | |
9abb2685 | 825 | static int |
2794995a | 826 | set_system_clock(const bool hclock_valid, const struct timeval newtime, |
d17a12a3 | 827 | const bool testing, const bool universal) |
ef71b8f1 SK |
828 | { |
829 | int retcode; | |
830 | ||
831 | if (!hclock_valid) { | |
111c05d3 SK |
832 | warnx(_ |
833 | ("The Hardware Clock does not contain a valid time, so " | |
834 | "we cannot set the System Time from it.")); | |
ef71b8f1 SK |
835 | retcode = 1; |
836 | } else { | |
d17a12a3 | 837 | const struct timeval *tv_null = NULL; |
ef71b8f1 SK |
838 | struct tm *broken; |
839 | int minuteswest; | |
d17a12a3 | 840 | int rc = 0; |
ef71b8f1 | 841 | |
2794995a | 842 | broken = localtime(&newtime.tv_sec); |
48d7b13a | 843 | #ifdef HAVE_TM_GMTOFF |
ef71b8f1 | 844 | minuteswest = -broken->tm_gmtoff / 60; /* GNU extension */ |
22853e4a | 845 | #else |
ef71b8f1 SK |
846 | minuteswest = timezone / 60; |
847 | if (broken->tm_isdst) | |
848 | minuteswest -= 60; | |
22853e4a | 849 | #endif |
9abb2685 | 850 | |
ef71b8f1 SK |
851 | if (debug) { |
852 | printf(_("Calling settimeofday:\n")); | |
853 | printf(_("\ttv.tv_sec = %ld, tv.tv_usec = %ld\n"), | |
2794995a | 854 | (long)newtime.tv_sec, (long)newtime.tv_usec); |
ef71b8f1 SK |
855 | printf(_("\ttz.tz_minuteswest = %d\n"), minuteswest); |
856 | } | |
857 | if (testing) { | |
858 | printf(_ | |
859 | ("Not setting system clock because running in test mode.\n")); | |
860 | retcode = 0; | |
861 | } else { | |
862 | const struct timezone tz = { minuteswest, 0 }; | |
863 | ||
d17a12a3 WP |
864 | /* Set kernel persistent_clock_is_local so that 11 minute |
865 | * mode does not clobber the Hardware Clock with UTC. This | |
866 | * is only available on first call of settimeofday after boot. | |
867 | */ | |
868 | if (!universal) | |
869 | rc = settimeofday(tv_null, &tz); | |
870 | if (!rc) | |
871 | rc = settimeofday(&newtime, &tz); | |
ef71b8f1 SK |
872 | if (rc) { |
873 | if (errno == EPERM) { | |
111c05d3 SK |
874 | warnx(_ |
875 | ("Must be superuser to set system clock.")); | |
ef71b8f1 SK |
876 | retcode = EX_NOPERM; |
877 | } else { | |
111c05d3 | 878 | warn(_("settimeofday() failed")); |
ef71b8f1 SK |
879 | retcode = 1; |
880 | } | |
881 | } else | |
882 | retcode = 0; | |
883 | } | |
884 | } | |
885 | return retcode; | |
7eda085c KZ |
886 | } |
887 | ||
ef71b8f1 SK |
888 | /* |
889 | * Reset the System Clock from local time to UTC, based on its current value | |
890 | * and the timezone unless universal is TRUE. | |
891 | * | |
892 | * Also set the kernel time zone value to the value indicated by the TZ | |
893 | * environment variable and/or /usr/lib/zoneinfo/, interpreted as tzset() | |
894 | * would interpret them. | |
895 | * | |
896 | * If 'testing' is true, don't actually update anything -- just say we would | |
897 | * have. | |
898 | */ | |
899 | static int set_system_clock_timezone(const bool universal, const bool testing) | |
900 | { | |
901 | int retcode; | |
902 | struct timeval tv; | |
903 | struct tm *broken; | |
904 | int minuteswest; | |
ef71b8f1 SK |
905 | |
906 | gettimeofday(&tv, NULL); | |
907 | if (debug) { | |
908 | struct tm broken_time; | |
909 | char ctime_now[200]; | |
910 | ||
911 | broken_time = *gmtime(&tv.tv_sec); | |
912 | strftime(ctime_now, sizeof(ctime_now), "%Y/%m/%d %H:%M:%S", | |
913 | &broken_time); | |
914 | printf(_("Current system time: %ld = %s\n"), (long)tv.tv_sec, | |
915 | ctime_now); | |
916 | } | |
7eda085c | 917 | |
ef71b8f1 | 918 | broken = localtime(&tv.tv_sec); |
88a3372e | 919 | #ifdef HAVE_TM_GMTOFF |
ef71b8f1 | 920 | minuteswest = -broken->tm_gmtoff / 60; /* GNU extension */ |
88a3372e | 921 | #else |
ef71b8f1 SK |
922 | minuteswest = timezone / 60; |
923 | if (broken->tm_isdst) | |
924 | minuteswest -= 60; | |
88a3372e SJR |
925 | #endif |
926 | ||
ef71b8f1 SK |
927 | if (debug) { |
928 | struct tm broken_time; | |
929 | char ctime_now[200]; | |
930 | ||
839be2ba KZ |
931 | gettimeofday(&tv, NULL); |
932 | if (!universal) | |
933 | tv.tv_sec += minuteswest * 60; | |
934 | ||
ef71b8f1 SK |
935 | broken_time = *gmtime(&tv.tv_sec); |
936 | strftime(ctime_now, sizeof(ctime_now), "%Y/%m/%d %H:%M:%S", | |
937 | &broken_time); | |
938 | ||
939 | printf(_("Calling settimeofday:\n")); | |
940 | printf(_("\tUTC: %s\n"), ctime_now); | |
941 | printf(_("\ttv.tv_sec = %ld, tv.tv_usec = %ld\n"), | |
942 | (long)tv.tv_sec, (long)tv.tv_usec); | |
943 | printf(_("\ttz.tz_minuteswest = %d\n"), minuteswest); | |
944 | } | |
945 | if (testing) { | |
946 | printf(_ | |
947 | ("Not setting system clock because running in test mode.\n")); | |
948 | retcode = 0; | |
949 | } else { | |
910a0900 | 950 | const struct timezone tz_utc = { 0, 0 }; |
ef71b8f1 | 951 | const struct timezone tz = { minuteswest, 0 }; |
839be2ba | 952 | const struct timeval *tv_null = NULL; |
910a0900 TG |
953 | int rc = 0; |
954 | ||
955 | /* The first call to settimeofday after boot will assume the systemtime | |
956 | * is in localtime, and adjust it according to the given timezone to | |
957 | * compensate. If the systemtime is in fact in UTC, then this is wrong | |
958 | * so we first do a dummy call to make sure the time is not shifted. | |
959 | */ | |
960 | if (universal) | |
961 | rc = settimeofday(tv_null, &tz_utc); | |
962 | ||
963 | /* Now we set the real timezone. Due to the above dummy call, this will | |
964 | * only warp the systemtime if the RTC is not in UTC. */ | |
965 | if (!rc) | |
966 | rc = settimeofday(tv_null, &tz); | |
88a3372e | 967 | |
ef71b8f1 SK |
968 | if (rc) { |
969 | if (errno == EPERM) { | |
111c05d3 SK |
970 | warnx(_ |
971 | ("Must be superuser to set system clock.")); | |
ef71b8f1 SK |
972 | retcode = EX_NOPERM; |
973 | } else { | |
111c05d3 | 974 | warn(_("settimeofday() failed")); |
ef71b8f1 SK |
975 | retcode = 1; |
976 | } | |
977 | } else | |
978 | retcode = 0; | |
979 | } | |
980 | return retcode; | |
981 | } | |
982 | ||
983 | /* | |
f276d71a WP |
984 | * Refresh the last calibrated and last adjusted timestamps in <*adjtime_p> |
985 | * to facilitate future drift calculations based on this set point. | |
ef71b8f1 | 986 | * |
f276d71a WP |
987 | * With the --update-drift option: |
988 | * Update the drift factor in <*adjtime_p> based on the fact that the | |
989 | * Hardware Clock was just calibrated to <nowtime> and before that was | |
990 | * set to the <hclocktime> time scale. | |
ef71b8f1 SK |
991 | * |
992 | * EXCEPT: if <hclock_valid> is false, assume Hardware Clock was not set | |
993 | * before to anything meaningful and regular adjustments have not been done, | |
994 | * so don't adjust the drift factor. | |
995 | */ | |
7eda085c KZ |
996 | static void |
997 | adjust_drift_factor(struct adjtime *adjtime_p, | |
2794995a | 998 | const struct timeval nowtime, |
ef71b8f1 | 999 | const bool hclock_valid, |
f276d71a WP |
1000 | const struct timeval hclocktime, |
1001 | const bool update) | |
ef71b8f1 | 1002 | { |
f276d71a WP |
1003 | if (!update) { |
1004 | if (debug) | |
1005 | printf(_("Not adjusting drift factor because the " | |
1006 | "--update-drift option was not used.\n")); | |
1007 | } else if (!hclock_valid) { | |
63cccae4 KZ |
1008 | if (debug) |
1009 | printf(_("Not adjusting drift factor because the " | |
1010 | "Hardware Clock previously contained " | |
1011 | "garbage.\n")); | |
1012 | } else if (adjtime_p->last_calib_time == 0) { | |
1013 | if (debug) | |
1014 | printf(_("Not adjusting drift factor because last " | |
1015 | "calibration time is zero,\n" | |
1016 | "so history is bad and calibration startover " | |
1017 | "is necessary.\n")); | |
bbb4c273 | 1018 | } else if ((hclocktime.tv_sec - adjtime_p->last_calib_time) < 4 * 60 * 60) { |
63cccae4 KZ |
1019 | if (debug) |
1020 | printf(_("Not adjusting drift factor because it has " | |
bbb4c273 | 1021 | "been less than four hours since the last " |
63cccae4 KZ |
1022 | "calibration.\n")); |
1023 | } else if (adjtime_p->last_calib_time != 0) { | |
1024 | /* | |
f276d71a WP |
1025 | * At adjustment time we drift correct the hardware clock |
1026 | * according to the contents of the adjtime file and refresh | |
1027 | * its last adjusted timestamp. | |
63cccae4 | 1028 | * |
f276d71a WP |
1029 | * At calibration time we set the Hardware Clock and refresh |
1030 | * both timestamps in <*adjtime_p>. | |
63cccae4 | 1031 | * |
f276d71a WP |
1032 | * Here, with the --update-drift option, we also update the |
1033 | * drift factor in <*adjtime_p>. | |
63cccae4 KZ |
1034 | * |
1035 | * Let us do computation in doubles. (Floats almost suffice, | |
1036 | * but 195 days + 1 second equals 195 days in floats.) | |
1037 | */ | |
1038 | const double sec_per_day = 24.0 * 60.0 * 60.0; | |
63cccae4 | 1039 | double factor_adjust; |
f196fd1a | 1040 | double drift_factor; |
2794995a | 1041 | struct timeval last_calib; |
63cccae4 | 1042 | |
2794995a | 1043 | last_calib = t2tv(adjtime_p->last_calib_time); |
ede32597 WP |
1044 | /* |
1045 | * Correction to apply to the current drift factor. | |
1046 | * | |
1047 | * Simplified: uncorrected_drift / days_since_calibration. | |
1048 | * | |
1049 | * hclocktime is fully corrected with the current drift factor. | |
1050 | * Its difference from nowtime is the missed drift correction. | |
1051 | */ | |
2794995a WP |
1052 | factor_adjust = time_diff(nowtime, hclocktime) / |
1053 | (time_diff(nowtime, last_calib) / sec_per_day); | |
63cccae4 | 1054 | |
f196fd1a | 1055 | drift_factor = adjtime_p->drift_factor + factor_adjust; |
83aa4ad7 | 1056 | if (fabs(drift_factor) > MAX_DRIFT) { |
f196fd1a SB |
1057 | if (debug) |
1058 | printf(_("Clock drift factor was calculated as " | |
1059 | "%f seconds/day.\n" | |
1060 | "It is far too much. Resetting to zero.\n"), | |
1061 | drift_factor); | |
1062 | drift_factor = 0; | |
1063 | } else { | |
1064 | if (debug) | |
1065 | printf(_("Clock drifted %.1f seconds in the past " | |
2794995a | 1066 | "%.1f seconds\nin spite of a drift factor of " |
f196fd1a SB |
1067 | "%f seconds/day.\n" |
1068 | "Adjusting drift factor by %f seconds/day\n"), | |
2794995a WP |
1069 | time_diff(nowtime, hclocktime), |
1070 | time_diff(nowtime, last_calib), | |
f196fd1a SB |
1071 | adjtime_p->drift_factor, factor_adjust); |
1072 | } | |
1073 | ||
1074 | adjtime_p->drift_factor = drift_factor; | |
63cccae4 | 1075 | } |
2794995a | 1076 | adjtime_p->last_calib_time = nowtime.tv_sec; |
9abb2685 | 1077 | |
2794995a | 1078 | adjtime_p->last_adj_time = nowtime.tv_sec; |
9abb2685 | 1079 | |
63cccae4 | 1080 | adjtime_p->not_adjusted = 0; |
9abb2685 | 1081 | |
63cccae4 | 1082 | adjtime_p->dirty = TRUE; |
7eda085c KZ |
1083 | } |
1084 | ||
ef71b8f1 | 1085 | /* |
ede32597 WP |
1086 | * Calculate the drift correction currently needed for the |
1087 | * Hardware Clock based on the last time it was adjusted, | |
1088 | * and the current drift factor, as stored in the adjtime file. | |
ef71b8f1 | 1089 | * |
ede32597 | 1090 | * The total drift adjustment needed is stored at tdrift_p. |
ef71b8f1 | 1091 | * |
ef71b8f1 | 1092 | */ |
7eda085c | 1093 | static void |
63cccae4 | 1094 | calculate_adjustment(const double factor, |
ef71b8f1 SK |
1095 | const time_t last_time, |
1096 | const double not_adjusted, | |
2794995a | 1097 | const time_t systime, struct timeval *tdrift_p) |
ef71b8f1 SK |
1098 | { |
1099 | double exact_adjustment; | |
7eda085c | 1100 | |
ef71b8f1 SK |
1101 | exact_adjustment = |
1102 | ((double)(systime - last_time)) * factor / (24 * 60 * 60) | |
1103 | + not_adjusted; | |
2794995a WP |
1104 | tdrift_p->tv_sec = FLOOR(exact_adjustment); |
1105 | tdrift_p->tv_usec = (exact_adjustment - | |
1106 | (double)tdrift_p->tv_sec) * 1E6; | |
ef71b8f1 | 1107 | if (debug) { |
5adf126e LN |
1108 | printf(P_("Time since last adjustment is %d second\n", |
1109 | "Time since last adjustment is %d seconds\n", | |
1110 | (int)(systime - last_time)), | |
ef71b8f1 | 1111 | (int)(systime - last_time)); |
2794995a WP |
1112 | printf(_("Calculated Hardware Clock drift is %ld.%06d seconds\n"), |
1113 | (long)tdrift_p->tv_sec, (int)tdrift_p->tv_usec); | |
ef71b8f1 | 1114 | } |
7eda085c KZ |
1115 | } |
1116 | ||
ef71b8f1 SK |
1117 | /* |
1118 | * Write the contents of the <adjtime> structure to its disk file. | |
1119 | * | |
1120 | * But if the contents are clean (unchanged since read from disk), don't | |
1121 | * bother. | |
1122 | */ | |
1123 | static void save_adjtime(const struct adjtime adjtime, const bool testing) | |
1124 | { | |
1125 | char newfile[412]; /* Stuff to write to disk file */ | |
7eda085c | 1126 | |
ef71b8f1 SK |
1127 | if (adjtime.dirty) { |
1128 | /* | |
1129 | * snprintf is not always available, but this is safe as | |
1130 | * long as libc does not use more than 100 positions for %ld | |
1131 | * or %f | |
1132 | */ | |
1133 | sprintf(newfile, "%f %ld %f\n%ld\n%s\n", | |
1134 | adjtime.drift_factor, | |
1135 | (long)adjtime.last_adj_time, | |
1136 | adjtime.not_adjusted, | |
1137 | (long)adjtime.last_calib_time, | |
bf619967 | 1138 | (adjtime.local_utc == LOCAL) ? "LOCAL" : "UTC"); |
ef71b8f1 SK |
1139 | |
1140 | if (testing) { | |
1141 | printf(_ | |
1142 | ("Not updating adjtime file because of testing mode.\n")); | |
1143 | printf(_("Would have written the following to %s:\n%s"), | |
1144 | adj_file_name, newfile); | |
1145 | } else { | |
1146 | FILE *adjfile; | |
1147 | int err = 0; | |
1148 | ||
1149 | adjfile = fopen(adj_file_name, "w"); | |
1150 | if (adjfile == NULL) { | |
111c05d3 SK |
1151 | warn(_ |
1152 | ("Could not open file with the clock adjustment parameters " | |
1153 | "in it (%s) for writing"), adj_file_name); | |
ef71b8f1 SK |
1154 | err = 1; |
1155 | } else { | |
1156 | if (fputs(newfile, adjfile) < 0) { | |
111c05d3 SK |
1157 | warn(_ |
1158 | ("Could not update file with the clock adjustment " | |
1159 | "parameters (%s) in it"), | |
1160 | adj_file_name); | |
ef71b8f1 SK |
1161 | err = 1; |
1162 | } | |
db116df7 | 1163 | if (close_stream(adjfile) != 0) { |
111c05d3 SK |
1164 | warn(_ |
1165 | ("Could not update file with the clock adjustment " | |
1166 | "parameters (%s) in it"), | |
1167 | adj_file_name); | |
ef71b8f1 SK |
1168 | err = 1; |
1169 | } | |
1170 | } | |
1171 | if (err) | |
111c05d3 SK |
1172 | warnx(_ |
1173 | ("Drift adjustment parameters not updated.")); | |
ef71b8f1 SK |
1174 | } |
1175 | } | |
1176 | } | |
7eda085c | 1177 | |
ef71b8f1 SK |
1178 | /* |
1179 | * Do the adjustment requested, by 1) setting the Hardware Clock (if | |
1180 | * necessary), and 2) updating the last-adjusted time in the adjtime | |
1181 | * structure. | |
1182 | * | |
1183 | * Do not update anything if the Hardware Clock does not currently present a | |
1184 | * valid time. | |
1185 | * | |
ede32597 | 1186 | * <hclock_valid> means the Hardware Clock contains a valid time. |
ef71b8f1 | 1187 | * |
ede32597 | 1188 | * <hclocktime> is the drift corrected time read from the Hardware Clock. |
ef71b8f1 | 1189 | * |
ede32597 WP |
1190 | * <read_time> was the system time when the <hclocktime> was read, which due |
1191 | * to computational delay could be a short time ago. It is used to define a | |
1192 | * trigger point for setting the Hardware Clock. The fractional part of the | |
1193 | * Hardware clock set time is subtracted from read_time to 'refer back', or | |
1194 | * delay, the trigger point. Fractional parts must be accounted for in this | |
1195 | * way, because the Hardware Clock can only be set to a whole second. | |
ef71b8f1 SK |
1196 | * |
1197 | * <universal>: the Hardware Clock is kept in UTC. | |
1198 | * | |
1199 | * <testing>: We are running in test mode (no updating of clock). | |
1200 | * | |
ef71b8f1 | 1201 | */ |
7eda085c KZ |
1202 | static void |
1203 | do_adjustment(struct adjtime *adjtime_p, | |
2794995a | 1204 | const bool hclock_valid, const struct timeval hclocktime, |
ef71b8f1 SK |
1205 | const struct timeval read_time, |
1206 | const bool universal, const bool testing) | |
1207 | { | |
1208 | if (!hclock_valid) { | |
111c05d3 SK |
1209 | warnx(_("The Hardware Clock does not contain a valid time, " |
1210 | "so we cannot adjust it.")); | |
ef71b8f1 SK |
1211 | adjtime_p->last_calib_time = 0; /* calibration startover is required */ |
1212 | adjtime_p->last_adj_time = 0; | |
1213 | adjtime_p->not_adjusted = 0; | |
1214 | adjtime_p->dirty = TRUE; | |
1215 | } else if (adjtime_p->last_adj_time == 0) { | |
1216 | if (debug) | |
f55b4b45 KZ |
1217 | printf(_("Not setting clock because last adjustment time is zero, " |
1218 | "so history is bad.\n")); | |
83aa4ad7 | 1219 | } else if (fabs(adjtime_p->drift_factor) > MAX_DRIFT) { |
db8fc5f3 | 1220 | if (debug) |
f55b4b45 KZ |
1221 | printf(_("Not setting clock because drift factor %f is far too high.\n"), |
1222 | adjtime_p->drift_factor); | |
ef71b8f1 | 1223 | } else { |
2794995a WP |
1224 | set_hardware_clock_exact(hclocktime.tv_sec, |
1225 | time_inc(read_time, | |
1226 | -(hclocktime.tv_usec / 1E6)), | |
1227 | universal, testing); | |
1228 | adjtime_p->last_adj_time = hclocktime.tv_sec; | |
1229 | adjtime_p->not_adjusted = 0; | |
1230 | adjtime_p->dirty = TRUE; | |
ef71b8f1 | 1231 | } |
7eda085c KZ |
1232 | } |
1233 | ||
ef71b8f1 SK |
1234 | static void determine_clock_access_method(const bool user_requests_ISA) |
1235 | { | |
1236 | ur = NULL; | |
7eda085c | 1237 | |
ef71b8f1 SK |
1238 | if (user_requests_ISA) |
1239 | ur = probe_for_cmos_clock(); | |
7eda085c | 1240 | |
465e9973 | 1241 | #ifdef __linux__ |
ef71b8f1 SK |
1242 | if (!ur) |
1243 | ur = probe_for_rtc_clock(); | |
465e9973 | 1244 | #endif |
7eda085c | 1245 | |
ef71b8f1 SK |
1246 | if (debug) { |
1247 | if (ur) | |
b2d97db8 | 1248 | puts(_(ur->interface_name)); |
ef71b8f1 SK |
1249 | else |
1250 | printf(_("No usable clock interface found.\n")); | |
1251 | } | |
7eda085c KZ |
1252 | } |
1253 | ||
ef71b8f1 SK |
1254 | /* |
1255 | * Do all the normal work of hwclock - read, set clock, etc. | |
1256 | * | |
1257 | * Issue output to stdout and error message to stderr where appropriate. | |
1258 | * | |
1259 | * Return rc == 0 if everything went OK, rc != 0 if not. | |
1260 | */ | |
63cccae4 | 1261 | static int |
364cda48 | 1262 | manipulate_clock(const bool show, const bool adjust, const bool noadjfile, |
ef71b8f1 SK |
1263 | const bool set, const time_t set_time, |
1264 | const bool hctosys, const bool systohc, const bool systz, | |
1265 | const struct timeval startup_time, | |
f276d71a | 1266 | const bool utc, const bool local_opt, const bool update, |
2794995a | 1267 | const bool testing, const bool predict, const bool get) |
ef71b8f1 SK |
1268 | { |
1269 | /* Contents of the adjtime file, or what they should be. */ | |
1270 | struct adjtime adjtime; | |
1271 | bool universal; | |
1272 | /* Set if user lacks necessary authorization to access the clock */ | |
1273 | bool no_auth; | |
1274 | /* The time at which we read the Hardware Clock */ | |
1275 | struct timeval read_time; | |
1276 | /* | |
1277 | * The Hardware Clock gives us a valid time, or at | |
1278 | * least something close enough to fool mktime(). | |
1279 | */ | |
1280 | bool hclock_valid = FALSE; | |
1281 | /* | |
ede32597 WP |
1282 | * Tick synchronized time read from the Hardware Clock and |
1283 | * then drift correct for all operations except --show. | |
ef71b8f1 | 1284 | */ |
2794995a | 1285 | struct timeval hclocktime = { 0, 0 }; |
ede32597 | 1286 | /* Total Hardware Clock drift correction needed. */ |
2794995a | 1287 | struct timeval tdrift; |
ef71b8f1 | 1288 | /* local return code */ |
23341bd4 | 1289 | int rc = 0; |
ef71b8f1 SK |
1290 | |
1291 | if (!systz && !predict) { | |
1292 | no_auth = ur->get_permissions(); | |
1293 | if (no_auth) | |
1294 | return EX_NOPERM; | |
1295 | } | |
1296 | ||
2794995a | 1297 | if (!noadjfile && !(systz && (utc || local_opt))) { |
ef71b8f1 SK |
1298 | rc = read_adjtime(&adjtime); |
1299 | if (rc) | |
1300 | return rc; | |
1301 | } else { | |
2794995a | 1302 | /* A little trick to avoid writing the file if we don't have to */ |
ef71b8f1 | 1303 | adjtime.dirty = FALSE; |
ef71b8f1 SK |
1304 | } |
1305 | ||
1306 | universal = hw_clock_is_utc(utc, local_opt, adjtime); | |
1307 | ||
1308 | if ((set || systohc || adjust) && | |
1309 | (adjtime.local_utc == UTC) != universal) { | |
1310 | adjtime.local_utc = universal ? UTC : LOCAL; | |
1311 | adjtime.dirty = TRUE; | |
1312 | } | |
9abb2685 | 1313 | |
2794995a | 1314 | if (show || get || adjust || hctosys || (!noadjfile && !systz && !predict)) { |
ef71b8f1 SK |
1315 | /* data from HW-clock are required */ |
1316 | rc = synchronize_to_clock_tick(); | |
1317 | ||
1318 | /* | |
1319 | * 2 = synchronization timeout. We don't | |
1320 | * error out if the user is attempting to | |
1321 | * set the RTC - the RTC could be | |
1322 | * functioning but contain invalid time data | |
1323 | * so we still want to allow a user to set | |
1324 | * the RTC time. | |
1325 | */ | |
1326 | if (rc && rc != 2 && !set && !systohc) | |
1327 | return EX_IOERR; | |
1328 | gettimeofday(&read_time, NULL); | |
1329 | ||
1330 | /* | |
1331 | * If we can't synchronize to a clock tick, | |
1332 | * we likely can't read from the RTC so | |
1333 | * don't bother reading it again. | |
1334 | */ | |
1335 | if (!rc) { | |
1336 | rc = read_hardware_clock(universal, | |
2794995a | 1337 | &hclock_valid, &hclocktime.tv_sec); |
ef71b8f1 SK |
1338 | if (rc && !set && !systohc) |
1339 | return EX_IOERR; | |
1340 | } | |
cdedde03 | 1341 | } |
ede32597 WP |
1342 | /* |
1343 | * Calculate Hardware Clock drift for --predict with the user | |
1344 | * supplied --date option time, and with the time read from the | |
1345 | * Hardware Clock for all other operations. Apply drift correction | |
1346 | * to the Hardware Clock time for everything except --show and | |
1347 | * --predict. For --predict negate the drift correction, because we | |
1348 | * want to 'predict' a future Hardware Clock time that includes drift. | |
1349 | */ | |
2794995a WP |
1350 | hclocktime = predict ? t2tv(set_time) : hclocktime; |
1351 | calculate_adjustment(adjtime.drift_factor, | |
1352 | adjtime.last_adj_time, | |
1353 | adjtime.not_adjusted, | |
1354 | hclocktime.tv_sec, &tdrift); | |
1355 | if (!show && !predict) | |
1356 | hclocktime = time_inc(tdrift, hclocktime.tv_sec); | |
2794995a WP |
1357 | if (show || get) { |
1358 | display_time(hclock_valid, | |
1359 | time_inc(hclocktime, -time_diff | |
1360 | (read_time, startup_time))); | |
ef71b8f1 SK |
1361 | } else if (set) { |
1362 | set_hardware_clock_exact(set_time, startup_time, | |
1363 | universal, testing); | |
1364 | if (!noadjfile) | |
2794995a WP |
1365 | adjust_drift_factor(&adjtime, |
1366 | time_inc(t2tv(set_time), time_diff | |
1367 | (read_time, startup_time)), | |
f276d71a | 1368 | hclock_valid, hclocktime, update); |
ef71b8f1 | 1369 | } else if (adjust) { |
2794995a WP |
1370 | if (tdrift.tv_sec > 0 || tdrift.tv_sec < -1) |
1371 | do_adjustment(&adjtime, hclock_valid, | |
1372 | hclocktime, read_time, universal, testing); | |
1373 | else | |
1374 | printf(_("Needed adjustment is less than one second, " | |
1375 | "so not setting clock.\n")); | |
ef71b8f1 SK |
1376 | } else if (systohc) { |
1377 | struct timeval nowtime, reftime; | |
1378 | /* | |
1379 | * We can only set_hardware_clock_exact to a | |
1380 | * whole seconds time, so we set it with | |
1381 | * reference to the most recent whole | |
1382 | * seconds time. | |
1383 | */ | |
1384 | gettimeofday(&nowtime, NULL); | |
1385 | reftime.tv_sec = nowtime.tv_sec; | |
1386 | reftime.tv_usec = 0; | |
1387 | set_hardware_clock_exact((time_t) | |
1388 | reftime.tv_sec, | |
1389 | reftime, universal, testing); | |
1390 | if (!noadjfile) | |
2794995a | 1391 | adjust_drift_factor(&adjtime, nowtime, |
f276d71a | 1392 | hclock_valid, hclocktime, update); |
ef71b8f1 | 1393 | } else if (hctosys) { |
d17a12a3 WP |
1394 | rc = set_system_clock(hclock_valid, hclocktime, |
1395 | testing, universal); | |
ef71b8f1 SK |
1396 | if (rc) { |
1397 | printf(_("Unable to set system clock.\n")); | |
1398 | return rc; | |
1399 | } | |
88a3372e | 1400 | } else if (systz) { |
ef71b8f1 SK |
1401 | rc = set_system_clock_timezone(universal, testing); |
1402 | if (rc) { | |
1403 | printf(_("Unable to set system clock.\n")); | |
1404 | return rc; | |
1405 | } | |
1406 | } else if (predict) { | |
66af1c0f WP |
1407 | hclocktime = time_inc(hclocktime, (double) |
1408 | -(tdrift.tv_sec + tdrift.tv_usec / 1E6)); | |
ef71b8f1 SK |
1409 | if (debug) { |
1410 | printf(_ | |
1411 | ("At %ld seconds after 1969, RTC is predicted to read %ld seconds after 1969.\n"), | |
2794995a | 1412 | set_time, (long)hclocktime.tv_sec); |
ef71b8f1 | 1413 | } |
2794995a | 1414 | display_time(TRUE, hclocktime); |
ef71b8f1 SK |
1415 | } |
1416 | if (!noadjfile) | |
1417 | save_adjtime(adjtime, testing); | |
1418 | return 0; | |
7eda085c KZ |
1419 | } |
1420 | ||
ef71b8f1 SK |
1421 | /* |
1422 | * Get or set the Hardware Clock epoch value in the kernel, as appropriate. | |
1423 | * <getepoch>, <setepoch>, and <epoch> are hwclock invocation options. | |
1424 | * | |
1425 | * <epoch> == -1 if the user did not specify an "epoch" option. | |
1426 | */ | |
465e9973 | 1427 | #ifdef __linux__ |
390c72eb SK |
1428 | /* |
1429 | * Maintenance note: This should work on non-Alpha machines, but the | |
1430 | * evidence today (98.03.04) indicates that the kernel only keeps the epoch | |
1431 | * value on Alphas. If that is ever fixed, this function should be changed. | |
1432 | */ | |
1433 | # ifndef __alpha__ | |
7eda085c | 1434 | static void |
390c72eb SK |
1435 | manipulate_epoch(const bool getepoch __attribute__ ((__unused__)), |
1436 | const bool setepoch __attribute__ ((__unused__)), | |
4ac41d61 | 1437 | const unsigned long epoch_opt __attribute__ ((__unused__)), |
390c72eb | 1438 | const bool testing __attribute__ ((__unused__))) |
ef71b8f1 | 1439 | { |
111c05d3 SK |
1440 | warnx(_("The kernel keeps an epoch value for the Hardware Clock " |
1441 | "only on an Alpha machine.\nThis copy of hwclock was built for " | |
1442 | "a machine other than Alpha\n(and thus is presumably not running " | |
1443 | "on an Alpha now). No action taken.")); | |
390c72eb SK |
1444 | } |
1445 | # else | |
1446 | static void | |
1447 | manipulate_epoch(const bool getepoch, | |
1448 | const bool setepoch, | |
4ac41d61 | 1449 | const unsigned long epoch_opt, |
390c72eb SK |
1450 | const bool testing) |
1451 | { | |
ef71b8f1 SK |
1452 | if (getepoch) { |
1453 | unsigned long epoch; | |
1454 | ||
1455 | if (get_epoch_rtc(&epoch, 0)) | |
111c05d3 SK |
1456 | warnx(_ |
1457 | ("Unable to get the epoch value from the kernel.")); | |
ef71b8f1 SK |
1458 | else |
1459 | printf(_("Kernel is assuming an epoch value of %lu\n"), | |
1460 | epoch); | |
1461 | } else if (setepoch) { | |
1462 | if (epoch_opt == -1) | |
111c05d3 SK |
1463 | warnx(_ |
1464 | ("To set the epoch value, you must use the 'epoch' " | |
1465 | "option to tell to what value to set it.")); | |
ef71b8f1 SK |
1466 | else if (testing) |
1467 | printf(_ | |
1468 | ("Not setting the epoch to %d - testing only.\n"), | |
1469 | epoch_opt); | |
1470 | else if (set_epoch_rtc(epoch_opt)) | |
1471 | printf(_ | |
1472 | ("Unable to set the epoch value in the kernel.\n")); | |
1473 | } | |
7eda085c | 1474 | } |
390c72eb SK |
1475 | # endif /* __alpha__ */ |
1476 | #endif /* __linux__ */ | |
7eda085c | 1477 | |
83765871 OO |
1478 | /* |
1479 | * Compare the system and CMOS time and output the drift | |
1480 | * in 10 second intervals. | |
1481 | */ | |
1482 | static int compare_clock (const bool utc, const bool local_opt) | |
1483 | { | |
1484 | struct tm tm; | |
1485 | struct timeval tv; | |
1486 | struct adjtime adjtime; | |
1487 | double time1_sys, time2_sys; | |
1488 | time_t time1_hw, time2_hw; | |
1489 | bool hclock_valid = FALSE, universal, first_pass = TRUE; | |
1490 | int rc; | |
1491 | ||
68030a76 KZ |
1492 | if (ur->get_permissions()) |
1493 | return EX_NOPERM; | |
1494 | ||
83765871 OO |
1495 | /* dummy call for increased precision */ |
1496 | gettimeofday(&tv, NULL); | |
1497 | ||
1498 | rc = read_adjtime(&adjtime); | |
1499 | if (rc) | |
1500 | return rc; | |
1501 | ||
1502 | universal = hw_clock_is_utc(utc, local_opt, adjtime); | |
1503 | ||
1504 | synchronize_to_clock_tick(); | |
1505 | ur->read_hardware_clock(&tm); | |
1506 | ||
1507 | gettimeofday(&tv, NULL); | |
1508 | time1_sys = tv.tv_sec + tv.tv_usec / 1000000.0; | |
1509 | ||
1510 | mktime_tz(tm, universal, &hclock_valid, &time1_hw); | |
1511 | ||
1512 | while (1) { | |
1513 | double res; | |
1514 | ||
1515 | synchronize_to_clock_tick(); | |
1516 | ur->read_hardware_clock(&tm); | |
1517 | ||
1518 | gettimeofday(&tv, NULL); | |
1519 | time2_sys = tv.tv_sec + tv.tv_usec / 1000000.0; | |
1520 | ||
1521 | mktime_tz(tm, universal, &hclock_valid, &time2_hw); | |
1522 | ||
1523 | res = (((double) time1_hw - time1_sys) - | |
1524 | ((double) time2_hw - time2_sys)) | |
1525 | / (double) (time2_hw - time1_hw); | |
1526 | ||
1527 | if (!first_pass) | |
1528 | printf("%10.0f %10.6f %15.0f %4.0f\n", | |
1529 | (double) time2_hw, time2_sys, res * 1e6, res *1e4); | |
1530 | else { | |
1531 | first_pass = FALSE; | |
1532 | printf("hw-time system-time freq-offset-ppm tick\n"); | |
1533 | printf("%10.0f %10.6f\n", (double) time1_hw, time1_sys); | |
1534 | } | |
1535 | sleep(10); | |
1536 | } | |
1537 | ||
1538 | return 0; | |
1539 | } | |
1540 | ||
ef71b8f1 SK |
1541 | static void out_version(void) |
1542 | { | |
f6277500 | 1543 | printf(UTIL_LINUX_VERSION); |
63cccae4 KZ |
1544 | } |
1545 | ||
eb63b9b8 | 1546 | /* |
ef71b8f1 SK |
1547 | * usage - Output (error and) usage information |
1548 | * | |
1549 | * This function is called both directly from main to show usage information | |
1550 | * and as fatal function from shhopt if some argument is not understood. In | |
1551 | * case of normal usage info FMT should be NULL. In that case the info is | |
1552 | * printed to stdout. If FMT is given usage will act like fprintf( stderr, | |
1553 | * fmt, ... ), show a usage information and terminate the program | |
1554 | * afterwards. | |
1555 | */ | |
1556 | static void usage(const char *fmt, ...) | |
1557 | { | |
1558 | FILE *usageto; | |
1559 | va_list ap; | |
1560 | ||
1561 | usageto = fmt ? stderr : stdout; | |
1562 | ||
db433bf7 | 1563 | fputs(USAGE_HEADER, usageto); |
49deeeac KZ |
1564 | fputs(_(" hwclock [function] [option...]\n"), usageto); |
1565 | ||
451dbcfa BS |
1566 | fputs(USAGE_SEPARATOR, usageto); |
1567 | fputs(_("Query or set the hardware clock.\n"), usageto); | |
1568 | ||
49deeeac | 1569 | fputs(_("\nFunctions:\n"), usageto); |
cc5ec693 KZ |
1570 | fputs(_(" -h, --help show this help text and exit\n" |
1571 | " -r, --show read hardware clock and print result\n" | |
ede32597 | 1572 | " --get read hardware clock and print drift corrected result\n" |
cc5ec693 KZ |
1573 | " --set set the RTC to the time given with --date\n"), usageto); |
1574 | fputs(_(" -s, --hctosys set the system time from the hardware clock\n" | |
1575 | " -w, --systohc set the hardware clock from the current system time\n" | |
1576 | " --systz set the system time based on the current timezone\n" | |
1577 | " --adjust adjust the RTC to account for systematic drift since\n" | |
c2f0a856 SK |
1578 | " the clock was last set or adjusted\n"), usageto); |
1579 | fputs(_(" -c, --compare periodically compare the system clock with the CMOS clock\n"), usageto); | |
465e9973 | 1580 | #ifdef __linux__ |
cc5ec693 KZ |
1581 | fputs(_(" --getepoch print out the kernel's hardware clock epoch value\n" |
1582 | " --setepoch set the kernel's hardware clock epoch value to the \n" | |
1583 | " value given with --epoch\n"), usageto); | |
465e9973 | 1584 | #endif |
cc5ec693 KZ |
1585 | fputs(_(" --predict predict RTC reading at time given with --date\n" |
1586 | " -V, --version display version information and exit\n"), usageto); | |
49deeeac | 1587 | |
db433bf7 | 1588 | fputs(USAGE_OPTIONS, usageto); |
cc5ec693 KZ |
1589 | fputs(_(" -u, --utc the hardware clock is kept in UTC\n" |
1590 | " --localtime the hardware clock is kept in local time\n"), usageto); | |
465e9973 | 1591 | #ifdef __linux__ |
cc5ec693 | 1592 | fputs(_(" -f, --rtc <file> special /dev/... file to use instead of default\n"), usageto); |
465e9973 | 1593 | #endif |
49deeeac | 1594 | fprintf(usageto, _( |
cc5ec693 KZ |
1595 | " --directisa access the ISA bus directly instead of %s\n" |
1596 | " --badyear ignore RTC's year because the BIOS is broken\n" | |
1597 | " --date <time> specifies the time to which to set the hardware clock\n" | |
1598 | " --epoch <year> specifies the year which is the beginning of the\n" | |
1599 | " hardware clock's epoch value\n"), _PATH_RTC_DEV); | |
49deeeac | 1600 | fprintf(usageto, _( |
fc56c363 KZ |
1601 | " --update-drift update drift factor in %1$s (requires\n" |
1602 | " --set or --systohc)\n" | |
1603 | " --noadjfile do not access %1$s; this requires the use of\n" | |
cc5ec693 KZ |
1604 | " either --utc or --localtime\n" |
1605 | " --adjfile <file> specifies the path to the adjust file;\n" | |
fc56c363 | 1606 | " the default is %1$s\n"), _PATH_ADJTIME); |
cc5ec693 KZ |
1607 | fputs(_(" --test do not update anything, just show what would happen\n" |
1608 | " -D, --debug debugging mode\n" "\n"), usageto); | |
eb63b9b8 | 1609 | #ifdef __alpha__ |
49deeeac KZ |
1610 | fputs(_(" -J|--jensen, -A|--arc, -S|--srm, -F|--funky-toy\n" |
1611 | " tell hwclock the type of Alpha you have (see hwclock(8))\n" | |
1612 | "\n"), usageto); | |
eb63b9b8 KZ |
1613 | #endif |
1614 | ||
ef71b8f1 | 1615 | if (fmt) { |
ef71b8f1 | 1616 | va_start(ap, fmt); |
4f899788 | 1617 | vfprintf(usageto, fmt, ap); |
ef71b8f1 SK |
1618 | va_end(ap); |
1619 | } | |
9abb2685 | 1620 | |
4f899788 | 1621 | fflush(usageto); |
111c05d3 | 1622 | hwclock_exit(fmt ? EX_USAGE : EX_OK); |
eb63b9b8 KZ |
1623 | } |
1624 | ||
63cccae4 KZ |
1625 | /* |
1626 | * Returns: | |
1627 | * EX_USAGE: bad invocation | |
1628 | * EX_NOPERM: no permission | |
1629 | * EX_OSFILE: cannot open /dev/rtc or /etc/adjtime | |
1630 | * EX_IOERR: ioctl error getting or setting the time | |
1631 | * 0: OK (or not) | |
1632 | * 1: failure | |
1633 | */ | |
ef71b8f1 SK |
1634 | int main(int argc, char **argv) |
1635 | { | |
63cccae4 | 1636 | struct timeval startup_time; |
ef71b8f1 SK |
1637 | /* |
1638 | * The time we started up, in seconds into the epoch, including | |
1639 | * fractions. | |
1640 | */ | |
1641 | time_t set_time = 0; /* Time to which user said to set Hardware Clock */ | |
7eda085c | 1642 | |
ef71b8f1 | 1643 | bool permitted; /* User is permitted to do the function */ |
63cccae4 | 1644 | int rc, c; |
7eda085c | 1645 | |
63cccae4 KZ |
1646 | /* Variables set by various options; show may also be set later */ |
1647 | /* The options debug, badyear and epoch_option are global */ | |
ef71b8f1 | 1648 | bool show, set, systohc, hctosys, systz, adjust, getepoch, setepoch, |
2794995a | 1649 | predict, compare, get; |
f276d71a | 1650 | bool utc, testing, local_opt, update, noadjfile, directisa; |
63cccae4 | 1651 | char *date_opt; |
52019d88 KZ |
1652 | #ifdef __alpha__ |
1653 | bool ARCconsole, Jensen, SRM, funky_toy; | |
1654 | #endif | |
dade002a KZ |
1655 | /* Long only options. */ |
1656 | enum { | |
1657 | OPT_ADJFILE = CHAR_MAX + 1, | |
1658 | OPT_BADYEAR, | |
1659 | OPT_DATE, | |
1660 | OPT_DIRECTISA, | |
1661 | OPT_EPOCH, | |
2794995a | 1662 | OPT_GET, |
dade002a KZ |
1663 | OPT_GETEPOCH, |
1664 | OPT_LOCALTIME, | |
1665 | OPT_NOADJFILE, | |
1666 | OPT_PREDICT_HC, | |
1667 | OPT_SET, | |
1668 | OPT_SETEPOCH, | |
1669 | OPT_SYSTZ, | |
f276d71a WP |
1670 | OPT_TEST, |
1671 | OPT_UPDATE | |
dade002a | 1672 | }; |
33ed2d02 SK |
1673 | |
1674 | static const struct option longopts[] = { | |
1675 | {"adjust", 0, 0, 'a'}, | |
83765871 | 1676 | {"compare", 0, 0, 'c'}, |
33ed2d02 SK |
1677 | {"help", 0, 0, 'h'}, |
1678 | {"show", 0, 0, 'r'}, | |
1679 | {"hctosys", 0, 0, 's'}, | |
1680 | {"utc", 0, 0, 'u'}, | |
1681 | {"version", 0, 0, 'v'}, | |
1682 | {"systohc", 0, 0, 'w'}, | |
1683 | {"debug", 0, 0, 'D'}, | |
1684 | #ifdef __alpha__ | |
1685 | {"ARC", 0, 0, 'A'}, | |
1686 | {"arc", 0, 0, 'A'}, | |
1687 | {"Jensen", 0, 0, 'J'}, | |
1688 | {"jensen", 0, 0, 'J'}, | |
1689 | {"SRM", 0, 0, 'S'}, | |
1690 | {"srm", 0, 0, 'S'}, | |
1691 | {"funky-toy", 0, 0, 'F'}, | |
1692 | #endif | |
1693 | {"set", 0, 0, OPT_SET}, | |
1694 | #ifdef __linux__ | |
1695 | {"getepoch", 0, 0, OPT_GETEPOCH}, | |
1696 | {"setepoch", 0, 0, OPT_SETEPOCH}, | |
1697 | #endif | |
1698 | {"noadjfile", 0, 0, OPT_NOADJFILE}, | |
1699 | {"localtime", 0, 0, OPT_LOCALTIME}, | |
1700 | {"badyear", 0, 0, OPT_BADYEAR}, | |
1701 | {"directisa", 0, 0, OPT_DIRECTISA}, | |
1702 | {"test", 0, 0, OPT_TEST}, | |
1703 | {"date", 1, 0, OPT_DATE}, | |
1704 | {"epoch", 1, 0, OPT_EPOCH}, | |
1705 | #ifdef __linux__ | |
1706 | {"rtc", 1, 0, 'f'}, | |
1707 | #endif | |
1708 | {"adjfile", 1, 0, OPT_ADJFILE}, | |
1709 | {"systz", 0, 0, OPT_SYSTZ}, | |
1710 | {"predict-hc", 0, 0, OPT_PREDICT_HC}, | |
2794995a | 1711 | {"get", 0, 0, OPT_GET}, |
f276d71a | 1712 | {"update-drift",0, 0, OPT_UPDATE}, |
33ed2d02 SK |
1713 | {NULL, 0, NULL, 0} |
1714 | }; | |
1715 | ||
dade002a KZ |
1716 | static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */ |
1717 | { 'a','r','s','w', | |
2794995a WP |
1718 | OPT_GET, OPT_GETEPOCH, OPT_PREDICT_HC, |
1719 | OPT_SET, OPT_SETEPOCH, OPT_SYSTZ }, | |
dade002a KZ |
1720 | { 'u', OPT_LOCALTIME}, |
1721 | { OPT_ADJFILE, OPT_NOADJFILE }, | |
f276d71a | 1722 | { OPT_NOADJFILE, OPT_UPDATE }, |
dade002a KZ |
1723 | { 0 } |
1724 | }; | |
1725 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
1726 | ||
63cccae4 KZ |
1727 | /* Remember what time we were invoked */ |
1728 | gettimeofday(&startup_time, NULL); | |
7eda085c | 1729 | |
88058a71 KZ |
1730 | #ifdef HAVE_LIBAUDIT |
1731 | hwaudit_fd = audit_open(); | |
1732 | if (hwaudit_fd < 0 && !(errno == EINVAL || errno == EPROTONOSUPPORT || | |
1733 | errno == EAFNOSUPPORT)) { | |
ef71b8f1 SK |
1734 | /* |
1735 | * You get these error codes only when the kernel doesn't | |
1736 | * have audit compiled in. | |
1737 | */ | |
111c05d3 | 1738 | warnx(_("Unable to connect to audit system")); |
88058a71 KZ |
1739 | return EX_NOPERM; |
1740 | } | |
1741 | #endif | |
63cccae4 | 1742 | setlocale(LC_ALL, ""); |
66ee8158 | 1743 | #ifdef LC_NUMERIC |
ef71b8f1 SK |
1744 | /* |
1745 | * We need LC_CTYPE and LC_TIME and LC_MESSAGES, but must avoid | |
1746 | * LC_NUMERIC since it gives problems when we write to /etc/adjtime. | |
1747 | * - gqueri@mail.dotcom.fr | |
1748 | */ | |
63cccae4 | 1749 | setlocale(LC_NUMERIC, "C"); |
66ee8158 | 1750 | #endif |
63cccae4 KZ |
1751 | bindtextdomain(PACKAGE, LOCALEDIR); |
1752 | textdomain(PACKAGE); | |
db116df7 | 1753 | atexit(close_stdout); |
63cccae4 KZ |
1754 | |
1755 | /* Set option defaults */ | |
ef71b8f1 | 1756 | show = set = systohc = hctosys = systz = adjust = noadjfile = predict = |
f276d71a | 1757 | compare = get = update = FALSE; |
b618d6ee | 1758 | getepoch = setepoch = utc = local_opt = directisa = testing = debug = FALSE; |
52019d88 | 1759 | #ifdef __alpha__ |
b618d6ee | 1760 | ARCconsole = Jensen = SRM = funky_toy = badyear = FALSE; |
52019d88 | 1761 | #endif |
63cccae4 KZ |
1762 | date_opt = NULL; |
1763 | ||
dade002a | 1764 | while ((c = getopt_long(argc, argv, |
83765871 | 1765 | "?hvVDacrsuwAJSFf:", longopts, NULL)) != -1) { |
dade002a KZ |
1766 | |
1767 | err_exclusive_options(c, longopts, excl, excl_st); | |
1768 | ||
63cccae4 KZ |
1769 | switch (c) { |
1770 | case 'D': | |
4a44a54b | 1771 | ++debug; |
63cccae4 KZ |
1772 | break; |
1773 | case 'a': | |
1774 | adjust = TRUE; | |
1775 | break; | |
83765871 OO |
1776 | case 'c': |
1777 | compare = TRUE; | |
1778 | break; | |
63cccae4 KZ |
1779 | case 'r': |
1780 | show = TRUE; | |
1781 | break; | |
1782 | case 's': | |
1783 | hctosys = TRUE; | |
1784 | break; | |
1785 | case 'u': | |
1786 | utc = TRUE; | |
1787 | break; | |
1788 | case 'w': | |
1789 | systohc = TRUE; | |
1790 | break; | |
1791 | #ifdef __alpha__ | |
1792 | case 'A': | |
1793 | ARCconsole = TRUE; | |
1794 | break; | |
1795 | case 'J': | |
1796 | Jensen = TRUE; | |
1797 | break; | |
1798 | case 'S': | |
1799 | SRM = TRUE; | |
1800 | break; | |
1801 | case 'F': | |
1802 | funky_toy = TRUE; | |
1803 | break; | |
1804 | #endif | |
33ed2d02 | 1805 | case OPT_SET: |
63cccae4 KZ |
1806 | set = TRUE; |
1807 | break; | |
465e9973 | 1808 | #ifdef __linux__ |
33ed2d02 | 1809 | case OPT_GETEPOCH: |
63cccae4 KZ |
1810 | getepoch = TRUE; |
1811 | break; | |
33ed2d02 | 1812 | case OPT_SETEPOCH: |
63cccae4 KZ |
1813 | setepoch = TRUE; |
1814 | break; | |
465e9973 | 1815 | #endif |
33ed2d02 | 1816 | case OPT_NOADJFILE: |
63cccae4 KZ |
1817 | noadjfile = TRUE; |
1818 | break; | |
33ed2d02 | 1819 | case OPT_LOCALTIME: |
ef71b8f1 | 1820 | local_opt = TRUE; /* --localtime */ |
63cccae4 | 1821 | break; |
33ed2d02 | 1822 | case OPT_BADYEAR: |
63cccae4 KZ |
1823 | badyear = TRUE; |
1824 | break; | |
33ed2d02 | 1825 | case OPT_DIRECTISA: |
63cccae4 KZ |
1826 | directisa = TRUE; |
1827 | break; | |
33ed2d02 | 1828 | case OPT_TEST: |
ef71b8f1 | 1829 | testing = TRUE; /* --test */ |
63cccae4 | 1830 | break; |
33ed2d02 | 1831 | case OPT_DATE: |
ef71b8f1 | 1832 | date_opt = optarg; /* --date */ |
63cccae4 | 1833 | break; |
33ed2d02 | 1834 | case OPT_EPOCH: |
4ac41d61 | 1835 | epoch_option = /* --epoch */ |
99c95585 | 1836 | strtoul_or_err(optarg, _("invalid epoch argument")); |
63cccae4 | 1837 | break; |
33ed2d02 | 1838 | case OPT_ADJFILE: |
ef71b8f1 | 1839 | adj_file_name = optarg; /* --adjfile */ |
da82f6fe | 1840 | break; |
33ed2d02 | 1841 | case OPT_SYSTZ: |
ef71b8f1 | 1842 | systz = TRUE; /* --systz */ |
88a3372e | 1843 | break; |
33ed2d02 | 1844 | case OPT_PREDICT_HC: |
ef71b8f1 | 1845 | predict = TRUE; /* --predict-hc */ |
2e5627fa | 1846 | break; |
2794995a WP |
1847 | case OPT_GET: |
1848 | get = TRUE; /* --get */ | |
1849 | break; | |
f276d71a WP |
1850 | case OPT_UPDATE: |
1851 | update = TRUE; /* --update-drift */ | |
1852 | break; | |
465e9973 | 1853 | #ifdef __linux__ |
88681c5f | 1854 | case 'f': |
ef71b8f1 | 1855 | rtc_dev_name = optarg; /* --rtc */ |
88681c5f | 1856 | break; |
465e9973 | 1857 | #endif |
ef71b8f1 | 1858 | case 'v': /* --version */ |
63cccae4 KZ |
1859 | case 'V': |
1860 | out_version(); | |
1861 | return 0; | |
ef71b8f1 | 1862 | case 'h': /* --help */ |
63cccae4 KZ |
1863 | case '?': |
1864 | default: | |
1865 | usage(NULL); | |
1866 | } | |
1867 | } | |
7eda085c | 1868 | |
63cccae4 KZ |
1869 | argc -= optind; |
1870 | argv += optind; | |
eb63b9b8 | 1871 | |
88058a71 KZ |
1872 | #ifdef HAVE_LIBAUDIT |
1873 | if (testing != TRUE) { | |
1874 | if (adjust == TRUE || hctosys == TRUE || systohc == TRUE || | |
ef71b8f1 | 1875 | set == TRUE || setepoch == TRUE) { |
88058a71 KZ |
1876 | hwaudit_on = TRUE; |
1877 | } | |
1878 | } | |
1879 | #endif | |
63cccae4 KZ |
1880 | if (argc > 0) { |
1881 | usage(_("%s takes no non-option arguments. " | |
111c05d3 SK |
1882 | "You supplied %d.\n"), program_invocation_short_name, |
1883 | argc); | |
63cccae4 | 1884 | } |
7eda085c | 1885 | |
da82f6fe | 1886 | if (!adj_file_name) |
7528fae9 | 1887 | adj_file_name = _PATH_ADJTIME; |
da82f6fe | 1888 | |
dade002a | 1889 | if (noadjfile && !utc && !local_opt) { |
111c05d3 SK |
1890 | warnx(_("With --noadjfile, you must specify " |
1891 | "either --utc or --localtime")); | |
88058a71 | 1892 | hwclock_exit(EX_USAGE); |
63cccae4 | 1893 | } |
7eda085c | 1894 | #ifdef __alpha__ |
63cccae4 KZ |
1895 | set_cmos_epoch(ARCconsole, SRM); |
1896 | set_cmos_access(Jensen, funky_toy); | |
7eda085c KZ |
1897 | #endif |
1898 | ||
2e5627fa | 1899 | if (set || predict) { |
63cccae4 KZ |
1900 | rc = interpret_date_string(date_opt, &set_time); |
1901 | /* (time-consuming) */ | |
1902 | if (rc != 0) { | |
111c05d3 SK |
1903 | warnx(_("No usable set-to time. " |
1904 | "Cannot set clock.")); | |
88058a71 | 1905 | hwclock_exit(EX_USAGE); |
63cccae4 KZ |
1906 | } |
1907 | } | |
7eda085c | 1908 | |
88a3372e | 1909 | if (!(show | set | systohc | hctosys | systz | adjust | getepoch |
2794995a | 1910 | | setepoch | predict | compare | get)) |
ef71b8f1 | 1911 | show = 1; /* default to show */ |
9abb2685 | 1912 | |
63cccae4 KZ |
1913 | if (getuid() == 0) |
1914 | permitted = TRUE; | |
1915 | else { | |
1916 | /* program is designed to run setuid (in some situations) */ | |
d458f94a | 1917 | if (set || systohc || adjust) { |
111c05d3 SK |
1918 | warnx(_("Sorry, only the superuser can change " |
1919 | "the Hardware Clock.")); | |
63cccae4 | 1920 | permitted = FALSE; |
88a3372e | 1921 | } else if (systz || hctosys) { |
111c05d3 SK |
1922 | warnx(_("Sorry, only the superuser can change " |
1923 | "the System Clock.")); | |
63cccae4 KZ |
1924 | permitted = FALSE; |
1925 | } else if (setepoch) { | |
111c05d3 SK |
1926 | warnx(_("Sorry, only the superuser can change the " |
1927 | "Hardware Clock epoch in the kernel.")); | |
63cccae4 KZ |
1928 | permitted = FALSE; |
1929 | } else | |
1930 | permitted = TRUE; | |
1931 | } | |
7eda085c | 1932 | |
63cccae4 | 1933 | if (!permitted) |
88058a71 | 1934 | hwclock_exit(EX_NOPERM); |
63cccae4 | 1935 | |
465e9973 | 1936 | #ifdef __linux__ |
63cccae4 KZ |
1937 | if (getepoch || setepoch) { |
1938 | manipulate_epoch(getepoch, setepoch, epoch_option, testing); | |
111c05d3 | 1939 | hwclock_exit(EX_OK); |
63cccae4 | 1940 | } |
465e9973 | 1941 | #endif |
63cccae4 KZ |
1942 | |
1943 | if (debug) | |
1944 | out_version(); | |
111c05d3 | 1945 | |
2e5627fa | 1946 | if (!systz && !predict) { |
304762d6 SJR |
1947 | determine_clock_access_method(directisa); |
1948 | if (!ur) { | |
111c05d3 SK |
1949 | warnx(_("Cannot access the Hardware Clock via " |
1950 | "any known method.")); | |
304762d6 | 1951 | if (!debug) |
111c05d3 SK |
1952 | warnx(_("Use the --debug option to see the " |
1953 | "details of our search for an access " | |
1954 | "method.")); | |
1955 | hwclock_exit(EX_SOFTWARE); | |
304762d6 | 1956 | } |
63cccae4 KZ |
1957 | } |
1958 | ||
83765871 OO |
1959 | if (compare) { |
1960 | if (compare_clock(utc, local_opt)) | |
1961 | hwclock_exit(EX_NOPERM); | |
1962 | ||
1963 | rc = EX_OK; | |
1964 | } else | |
1965 | rc = manipulate_clock(show, adjust, noadjfile, set, set_time, | |
ef71b8f1 | 1966 | hctosys, systohc, systz, startup_time, utc, |
f276d71a | 1967 | local_opt, update, testing, predict, get); |
83765871 | 1968 | |
88058a71 | 1969 | hwclock_exit(rc); |
ef71b8f1 | 1970 | return rc; /* Not reached */ |
7eda085c KZ |
1971 | } |
1972 | ||
88058a71 | 1973 | #ifdef HAVE_LIBAUDIT |
111c05d3 SK |
1974 | /* |
1975 | * hwclock_exit calls either this function or plain exit depending | |
842f9176 | 1976 | * HAVE_LIBAUDIT see also clock.h |
111c05d3 SK |
1977 | */ |
1978 | void __attribute__((__noreturn__)) hwaudit_exit(int status) | |
88058a71 KZ |
1979 | { |
1980 | if (hwaudit_on) { | |
1981 | audit_log_user_message(hwaudit_fd, AUDIT_USYS_CONFIG, | |
ef71b8f1 SK |
1982 | "changing system time", NULL, NULL, NULL, |
1983 | status ? 0 : 1); | |
88058a71 KZ |
1984 | close(hwaudit_fd); |
1985 | } | |
1986 | exit(status); | |
1987 | } | |
1988 | #endif | |
1989 | ||
ef71b8f1 SK |
1990 | /* |
1991 | * History of this program: | |
1992 | * | |
1993 | * 98.08.12 BJH Version 2.4 | |
1994 | * | |
1995 | * Don't use century byte from Hardware Clock. Add comments telling why. | |
1996 | * | |
1997 | * 98.06.20 BJH Version 2.3. | |
1998 | * | |
1999 | * Make --hctosys set the kernel timezone from TZ environment variable | |
2000 | * and/or /usr/lib/zoneinfo. From Klaus Ripke (klaus@ripke.com). | |
2001 | * | |
2002 | * 98.03.05 BJH. Version 2.2. | |
2003 | * | |
2004 | * Add --getepoch and --setepoch. | |
2005 | * | |
2006 | * Fix some word length things so it works on Alpha. | |
2007 | * | |
2008 | * Make it work when /dev/rtc doesn't have the interrupt functions. In this | |
2009 | * case, busywait for the top of a second instead of blocking and waiting | |
2010 | * for the update complete interrupt. | |
2011 | * | |
2012 | * Fix a bunch of bugs too numerous to mention. | |
2013 | * | |
2014 | * 97.06.01: BJH. Version 2.1. Read and write the century byte (Byte 50) of | |
2015 | * the ISA Hardware Clock when using direct ISA I/O. Problem discovered by | |
2016 | * job (jei@iclnl.icl.nl). | |
2017 | * | |
2018 | * Use the rtc clock access method in preference to the KDGHWCLK method. | |
2019 | * Problem discovered by Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>. | |
2020 | * | |
2021 | * November 1996: Version 2.0.1. Modifications by Nicolai Langfeldt | |
2022 | * (janl@math.uio.no) to make it compile on linux 1.2 machines as well as | |
2023 | * more recent versions of the kernel. Introduced the NO_CLOCK access method | |
455fe9a0 | 2024 | * and wrote feature test code to detect absence of rtc headers. |
ef71b8f1 SK |
2025 | * |
2026 | *************************************************************************** | |
2027 | * Maintenance notes | |
2028 | * | |
2029 | * To compile this, you must use GNU compiler optimization (-O option) in | |
2030 | * order to make the "extern inline" functions from asm/io.h (inb(), etc.) | |
2031 | * compile. If you don't optimize, which means the compiler will generate no | |
2032 | * inline functions, the references to these functions in this program will | |
2033 | * be compiled as external references. Since you probably won't be linking | |
2034 | * with any functions by these names, you will have unresolved external | |
2035 | * references when you link. | |
2036 | * | |
2037 | * The program is designed to run setuid superuser, since we need to be able | |
2038 | * to do direct I/O. (More to the point: we need permission to execute the | |
2039 | * iopl() system call). (However, if you use one of the methods other than | |
2040 | * direct ISA I/O to access the clock, no setuid is required). | |
2041 | * | |
2042 | * Here's some info on how we must deal with the time that elapses while | |
2043 | * this program runs: There are two major delays as we run: | |
2044 | * | |
2045 | * 1) Waiting up to 1 second for a transition of the Hardware Clock so | |
2046 | * we are synchronized to the Hardware Clock. | |
2047 | * 2) Running the "date" program to interpret the value of our --date | |
2048 | * option. | |
2049 | * | |
2050 | * Reading the /etc/adjtime file is the next biggest source of delay and | |
2051 | * uncertainty. | |
2052 | * | |
2053 | * The user wants to know what time it was at the moment he invoked us, not | |
2054 | * some arbitrary time later. And in setting the clock, he is giving us the | |
2055 | * time at the moment we are invoked, so if we set the clock some time | |
2056 | * later, we have to add some time to that. | |
2057 | * | |
2058 | * So we check the system time as soon as we start up, then run "date" and | |
2059 | * do file I/O if necessary, then wait to synchronize with a Hardware Clock | |
2060 | * edge, then check the system time again to see how much time we spent. We | |
2061 | * immediately read the clock then and (if appropriate) report that time, | |
2062 | * and additionally, the delay we measured. | |
2063 | * | |
2064 | * If we're setting the clock to a time given by the user, we wait some more | |
2065 | * so that the total delay is an integral number of seconds, then set the | |
2066 | * Hardware Clock to the time the user requested plus that integral number | |
2067 | * of seconds. N.B. The Hardware Clock can only be set in integral seconds. | |
2068 | * | |
2069 | * If we're setting the clock to the system clock value, we wait for the | |
2070 | * system clock to reach the top of a second, and then set the Hardware | |
2071 | * Clock to the system clock's value. | |
2072 | * | |
2073 | * Here's an interesting point about setting the Hardware Clock: On my | |
2074 | * machine, when you set it, it sets to that precise time. But one can | |
2075 | * imagine another clock whose update oscillator marches on a steady one | |
2076 | * second period, so updating the clock between any two oscillator ticks is | |
2077 | * the same as updating it right at the earlier tick. To avoid any | |
2078 | * complications that might cause, we set the clock as soon as possible | |
2079 | * after an oscillator tick. | |
2080 | * | |
2081 | * About synchronizing to the Hardware Clock when reading the time: The | |
2082 | * precision of the Hardware Clock counters themselves is one second. You | |
2083 | * can't read the counters and find out that is 12:01:02.5. But if you | |
2084 | * consider the location in time of the counter's ticks as part of its | |
2085 | * value, then its precision is as infinite as time is continuous! What I'm | |
2086 | * saying is this: To find out the _exact_ time in the hardware clock, we | |
2087 | * wait until the next clock tick (the next time the second counter changes) | |
2088 | * and measure how long we had to wait. We then read the value of the clock | |
2089 | * counters and subtract the wait time and we know precisely what time it | |
2090 | * was when we set out to query the time. | |
2091 | * | |
2092 | * hwclock uses this method, and considers the Hardware Clock to have | |
2093 | * infinite precision. | |
ef71b8f1 | 2094 | */ |