2 * Copyright (C) 1993-1998 by Andrey A. Chernov, Moscow, Russia.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 static const char copyright[] =
30 "@(#)Copyright (C) 1993-1996 by Andrey A. Chernov, Moscow, Russia.\n\
31 All rights reserved.\n";
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Andrey A. Chernov <ache@astral.msk.su> Dec 20 1993
40 * Fix kernel time value if machine run wall CMOS clock
41 * (and /etc/wall_cmos_clock file present)
42 * using zoneinfo rules or direct TZ environment variable set.
43 * Use Joerg Wunsch idea for seconds accurate offset calculation
44 * with Garrett Wollman and Bruce Evans fixes.
53 #include <sys/param.h>
54 #include <machine/cpu.h>
55 #include <sys/sysctl.h>
57 #include "pathnames.h"
65 #define REPORT_PERIOD (30*60)
67 static void fake(int);
68 static void usage(void);
71 fake(int unused __unused)
78 main(int argc, char *argv[])
81 struct timeval tv, *stv;
82 struct timezone tz, *stz;
83 int kern_offset, wall_clock, disrtcset;
86 /* Avoid time_t here, can be unsigned long or worse */
87 long offset, localsec, diff;
88 time_t initial_sec, final_sec;
90 int initial_isdst = -1, final_isdst;
91 int need_restore = False, sleep_mode = False, looping,
95 while ((ch = getopt(argc, argv, "ais")) != -1)
97 case 'i': /* initial call, save offset */
102 case 'a': /* adjustment call, use saved offset */
116 if (access(_PATH_CLOCK, F_OK) != 0)
124 sigaddset(&mask, SIGTERM);
126 openlog("adjkerntz", LOG_PID|LOG_PERROR, LOG_DAEMON);
128 (void) signal(SIGHUP, SIG_IGN);
130 if (init && daemon(0, 1)) {
131 syslog(LOG_ERR, "daemon: %m");
136 (void) sigprocmask(SIG_BLOCK, &mask, NULL);
137 (void) signal(SIGTERM, fake);
144 wall_clock = (access(_PATH_CLOCK, F_OK) == 0);
145 if (init && !sleep_mode) {
153 mib[0] = CTL_MACHDEP;
154 mib[1] = CPU_ADJKERNTZ;
155 len = sizeof(kern_offset);
156 if (sysctl(mib, 2, &kern_offset, &len, NULL, 0) == -1) {
157 syslog(LOG_ERR, "sysctl(get_offset): %m");
161 /****** Critical section, do all things as fast as possible ******/
163 /* get local CMOS clock and possible kernel offset */
164 if (gettimeofday(&tv, &tz)) {
165 syslog(LOG_ERR, "gettimeofday: %m");
169 /* get the actual local timezone difference */
170 initial_sec = tv.tv_sec;
173 local = *localtime(&initial_sec);
175 initial_isdst = local.tm_isdst;
176 local.tm_isdst = initial_isdst;
178 /* calculate local CMOS diff from GMT */
180 localsec = mktime(&local);
181 if (localsec == -1) {
183 * XXX user can only control local time, and it is
184 * unacceptable to fail here for init. 2:30 am in the
185 * middle of the nonexistent hour means 3:30 am.
189 "Warning: nonexistent local time, try to run later.");
190 syslog(LOG_WARNING, "Giving up.");
194 "Warning: nonexistent local time.");
195 syslog(LOG_WARNING, "Will retry after %d minutes.",
197 (void) signal(SIGTERM, SIG_DFL);
198 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
199 (void) sleep(REPORT_PERIOD);
202 offset = -local.tm_gmtoff;
204 fprintf(stderr, "Initial offset: %ld secs\n", offset);
207 /* correct the kerneltime for this diffs */
208 /* subtract kernel offset, if present, old offset too */
210 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
214 fprintf(stderr, "Initial diff: %ld secs\n", diff);
216 /* Yet one step for final time */
218 final_sec = initial_sec + diff;
220 /* get the actual local timezone difference */
221 local = *localtime(&final_sec);
222 final_isdst = diff < 0 ? initial_isdst : local.tm_isdst;
223 if (diff > 0 && initial_isdst != final_isdst) {
227 initial_isdst = final_isdst;
230 local.tm_isdst = final_isdst;
232 localsec = mktime(&local);
233 if (localsec == -1) {
236 * XXX as above. The user has even less control,
237 * but perhaps we never get here.
241 "Warning: nonexistent final local time, try to run later.");
242 syslog(LOG_WARNING, "Giving up.");
246 "Warning: nonexistent final local time.");
247 syslog(LOG_WARNING, "Will retry after %d minutes.",
249 (void) signal(SIGTERM, SIG_DFL);
250 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
251 (void) sleep(REPORT_PERIOD);
254 offset = -local.tm_gmtoff;
256 fprintf(stderr, "Final offset: %ld secs\n", offset);
259 /* correct the kerneltime for this diffs */
260 /* subtract kernel offset, if present, old offset too */
262 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
266 fprintf(stderr, "Final diff: %ld secs\n", diff);
269 * stv is abused as a flag. The important value
276 if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
277 tz.tz_dsttime = tz.tz_minuteswest = 0; /* zone info is garbage */
280 if (!wall_clock && stz == NULL)
283 /* if init or UTC clock and offset/date will be changed, */
284 /* disable RTC modification for a while. */
286 if ( (init && stv != NULL)
287 || ((init || !wall_clock) && kern_offset != offset)
289 mib[0] = CTL_MACHDEP;
290 mib[1] = CPU_DISRTCSET;
291 len = sizeof(disrtcset);
292 if (sysctl(mib, 2, &disrtcset, &len, NULL, 0) == -1) {
293 syslog(LOG_ERR, "sysctl(get_disrtcset): %m");
296 if (disrtcset == 0) {
299 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
300 syslog(LOG_ERR, "sysctl(set_disrtcset): %m");
306 if ( (init && (stv != NULL || stz != NULL))
307 || (stz != NULL && stv == NULL)
311 * Get the time again, as close as possible to
312 * adjusting it, to minimise drift.
313 * XXX we'd better not fail between here and
314 * restoring disrtcset, since we don't clean up
317 if (gettimeofday(&tv, (struct timezone *)NULL)) {
318 syslog(LOG_ERR, "gettimeofday: %m");
324 if (settimeofday(stv, stz)) {
325 syslog(LOG_ERR, "settimeofday: %m");
330 /* setting CPU_ADJKERNTZ have a side effect: resettodr(), which */
331 /* can be disabled by CPU_DISRTCSET, so if init or UTC clock */
332 /* -- don't write RTC, else write RTC. */
334 if (kern_offset != offset) {
335 kern_offset = offset;
336 mib[0] = CTL_MACHDEP;
337 mib[1] = CPU_ADJKERNTZ;
338 len = sizeof(kern_offset);
339 if (sysctl(mib, 2, NULL, NULL, &kern_offset, len) == -1) {
340 syslog(LOG_ERR, "sysctl(update_offset): %m");
345 mib[0] = CTL_MACHDEP;
346 mib[1] = CPU_WALLCLOCK;
347 len = sizeof(wall_clock);
348 if (sysctl(mib, 2, NULL, NULL, &wall_clock, len) == -1) {
349 syslog(LOG_ERR, "sysctl(put_wallclock): %m");
354 need_restore = False;
355 mib[0] = CTL_MACHDEP;
356 mib[1] = CPU_DISRTCSET;
358 len = sizeof(disrtcset);
359 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
360 syslog(LOG_ERR, "sysctl(restore_disrtcset): %m");
365 /****** End of critical section ******/
367 if (init && wall_clock) {
369 /* wait for signals and acts like -a */
370 (void) sigsuspend(&emask);
380 fprintf(stderr, "%s\n%s\n%s\n%s\n",
381 "usage: adjkerntz -i",
382 "\t\t(initial call from /etc/rc)",
383 " adjkerntz -a [-s]",
384 "\t\t(adjustment call, -s for sleep/retry mode)");