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;
85 /* Avoid time_t here, can be unsigned long or worse */
86 long offset, localsec, diff;
87 time_t initial_sec, final_sec;
89 int initial_isdst = -1, final_isdst;
90 int need_restore = False, sleep_mode = False, looping,
94 while ((ch = getopt(argc, argv, "ais")) != -1)
96 case 'i': /* initial call, save offset */
101 case 'a': /* adjustment call, use saved offset */
115 if (access(_PATH_CLOCK, F_OK) != 0)
123 sigaddset(&mask, SIGTERM);
125 openlog("adjkerntz", LOG_PID|LOG_PERROR, LOG_DAEMON);
127 (void) signal(SIGHUP, SIG_IGN);
129 if (init && daemon(0,
136 syslog(LOG_ERR, "daemon: %m");
141 (void) sigprocmask(SIG_BLOCK, &mask, NULL);
142 (void) signal(SIGTERM, fake);
149 wall_clock = (access(_PATH_CLOCK, F_OK) == 0);
150 if (init && !sleep_mode) {
158 len = sizeof(kern_offset);
159 if (sysctlbyname("machdep.adjkerntz", &kern_offset, &len, NULL, 0) == -1) {
160 syslog(LOG_ERR, "sysctl(\"machdep.adjkerntz\"): %m");
164 /****** Critical section, do all things as fast as possible ******/
166 /* get local CMOS clock and possible kernel offset */
167 if (gettimeofday(&tv, &tz)) {
168 syslog(LOG_ERR, "gettimeofday: %m");
172 /* get the actual local timezone difference */
173 initial_sec = tv.tv_sec;
176 local = *localtime(&initial_sec);
178 initial_isdst = local.tm_isdst;
179 local.tm_isdst = initial_isdst;
181 /* calculate local CMOS diff from GMT */
183 localsec = mktime(&local);
184 if (localsec == -1) {
186 * XXX user can only control local time, and it is
187 * unacceptable to fail here for init. 2:30 am in the
188 * middle of the nonexistent hour means 3:30 am.
192 "Warning: nonexistent local time, try to run later.");
193 syslog(LOG_WARNING, "Giving up.");
197 "Warning: nonexistent local time.");
198 syslog(LOG_WARNING, "Will retry after %d minutes.",
200 (void) signal(SIGTERM, SIG_DFL);
201 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
202 (void) sleep(REPORT_PERIOD);
205 offset = -local.tm_gmtoff;
207 fprintf(stderr, "Initial offset: %ld secs\n", offset);
210 /* correct the kerneltime for this diffs */
211 /* subtract kernel offset, if present, old offset too */
213 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
217 fprintf(stderr, "Initial diff: %ld secs\n", diff);
219 /* Yet one step for final time */
221 final_sec = initial_sec + diff;
223 /* get the actual local timezone difference */
224 local = *localtime(&final_sec);
225 final_isdst = diff < 0 ? initial_isdst : local.tm_isdst;
226 if (diff > 0 && initial_isdst != final_isdst) {
230 initial_isdst = final_isdst;
233 local.tm_isdst = final_isdst;
235 localsec = mktime(&local);
236 if (localsec == -1) {
239 * XXX as above. The user has even less control,
240 * but perhaps we never get here.
244 "Warning: nonexistent final local time, try to run later.");
245 syslog(LOG_WARNING, "Giving up.");
249 "Warning: nonexistent final local time.");
250 syslog(LOG_WARNING, "Will retry after %d minutes.",
252 (void) signal(SIGTERM, SIG_DFL);
253 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
254 (void) sleep(REPORT_PERIOD);
257 offset = -local.tm_gmtoff;
259 fprintf(stderr, "Final offset: %ld secs\n", offset);
262 /* correct the kerneltime for this diffs */
263 /* subtract kernel offset, if present, old offset too */
265 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
269 fprintf(stderr, "Final diff: %ld secs\n", diff);
272 * stv is abused as a flag. The important value
279 if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
280 tz.tz_dsttime = tz.tz_minuteswest = 0; /* zone info is garbage */
283 if (!wall_clock && stz == NULL)
286 /* if init or UTC clock and offset/date will be changed, */
287 /* disable RTC modification for a while. */
289 if ( (init && stv != NULL)
290 || ((init || !wall_clock) && kern_offset != offset)
292 len = sizeof(disrtcset);
293 if (sysctlbyname("machdep.disable_rtc_set", &disrtcset, &len, NULL, 0) == -1) {
294 syslog(LOG_ERR, "sysctl(get: \"machdep.disable_rtc_set\"): %m");
297 if (disrtcset == 0) {
300 if (sysctlbyname("machdep.disable_rtc_set", NULL, NULL, &disrtcset, len) == -1) {
301 syslog(LOG_ERR, "sysctl(set: \"machdep.disable_rtc_set\"): %m");
307 if ( (init && (stv != NULL || stz != NULL))
308 || (stz != NULL && stv == NULL)
312 * Get the time again, as close as possible to
313 * adjusting it, to minimise drift.
314 * XXX we'd better not fail between here and
315 * restoring disrtcset, since we don't clean up
318 (void)gettimeofday(&tv, NULL);
322 if (settimeofday(stv, stz)) {
323 syslog(LOG_ERR, "settimeofday: %m");
328 /* setting machdep.adjkerntz have a side effect: resettodr(), which */
329 /* can be disabled by machdep.disable_rtc_set, so if init or UTC clock */
330 /* -- don't write RTC, else write RTC. */
332 if (kern_offset != offset) {
333 kern_offset = offset;
334 len = sizeof(kern_offset);
335 if (sysctlbyname("machdep.adjkerntz", NULL, NULL, &kern_offset, len) == -1) {
336 syslog(LOG_ERR, "sysctl(set: \"machdep.adjkerntz\"): %m");
341 len = sizeof(wall_clock);
342 if (sysctlbyname("machdep.wall_cmos_clock", NULL, NULL, &wall_clock, len) == -1) {
343 syslog(LOG_ERR, "sysctl(set: \"machdep.wall_cmos_clock\"): %m");
348 need_restore = False;
350 len = sizeof(disrtcset);
351 if (sysctlbyname("machdep.disable_rtc_set", NULL, NULL, &disrtcset, len) == -1) {
352 syslog(LOG_ERR, "sysctl(set: \"machdep.disable_rtc_set\"): %m");
357 /****** End of critical section ******/
359 if (init && wall_clock) {
361 /* wait for signals and acts like -a */
362 (void) sigsuspend(&emask);
372 fprintf(stderr, "%s\n%s\n%s\n%s\n",
373 "usage: adjkerntz -i",
374 "\t\t(initial call from /etc/rc)",
375 " adjkerntz -a [-s]",
376 "\t\t(adjustment call, -s for sleep/retry mode)");