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