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
26 * @(#)Copyright (C) 1993-1996 by Andrey A. Chernov, Moscow, Russia. All rights reserved.
27 * $FreeBSD: src/sbin/adjkerntz/adjkerntz.c,v 1.25.2.1 2001/07/30 10:38:04 dd Exp $
28 * $DragonFly: src/sbin/adjkerntz/adjkerntz.c,v 1.3 2003/09/28 14:39:16 hmp Exp $
32 * Andrey A. Chernov <ache@astral.msk.su> Dec 20 1993
34 * Fix kernel time value if machine run wall CMOS clock
35 * (and /etc/wall_cmos_clock file present)
36 * using zoneinfo rules or direct TZ environment variable set.
37 * Use Joerg Wunsch idea for seconds accurate offset calculation
38 * with Garrett Wollman and Bruce Evans fixes.
47 #include <sys/param.h>
48 #include <machine/cpu.h>
49 #include <sys/sysctl.h>
51 #include "pathnames.h"
59 #define REPORT_PERIOD (30*60)
61 static void fake __P((int));
62 static void usage __P((void));
65 fake(int unused __unused)
71 int main(int argc, char **argv)
74 struct timeval tv, *stv;
75 struct timezone tz, *stz;
76 int kern_offset, wall_clock, disrtcset;
79 /* Avoid time_t here, can be unsigned long or worse */
80 long offset, localsec, diff;
81 time_t initial_sec, final_sec;
83 int initial_isdst = -1, final_isdst;
84 int need_restore = False, sleep_mode = False, looping,
88 while ((ch = getopt(argc, argv, "ais")) != -1)
90 case 'i': /* initial call, save offset */
95 case 'a': /* adjustment call, use saved offset */
109 if (access(_PATH_CLOCK, F_OK) != 0)
117 sigaddset(&mask, SIGTERM);
119 openlog("adjkerntz", LOG_PID|LOG_PERROR, LOG_DAEMON);
121 (void) signal(SIGHUP, SIG_IGN);
123 if (init && daemon(0, 1)) {
124 syslog(LOG_ERR, "daemon: %m");
129 (void) sigprocmask(SIG_BLOCK, &mask, NULL);
130 (void) signal(SIGTERM, fake);
137 wall_clock = (access(_PATH_CLOCK, F_OK) == 0);
138 if (init && !sleep_mode) {
144 mib[0] = CTL_MACHDEP;
145 mib[1] = CPU_ADJKERNTZ;
146 len = sizeof(kern_offset);
147 if (sysctl(mib, 2, &kern_offset, &len, NULL, 0) == -1) {
148 syslog(LOG_ERR, "sysctl(get_offset): %m");
152 /****** Critical section, do all things as fast as possible ******/
154 /* get local CMOS clock and possible kernel offset */
155 if (gettimeofday(&tv, &tz)) {
156 syslog(LOG_ERR, "gettimeofday: %m");
160 /* get the actual local timezone difference */
161 initial_sec = tv.tv_sec;
164 local = *localtime(&initial_sec);
166 initial_isdst = local.tm_isdst;
167 local.tm_isdst = initial_isdst;
169 /* calculate local CMOS diff from GMT */
171 localsec = mktime(&local);
172 if (localsec == -1) {
174 * XXX user can only control local time, and it is
175 * unacceptable to fail here for init. 2:30 am in the
176 * middle of the nonexistent hour means 3:30 am.
180 "Warning: nonexistent local time, try to run later.");
181 syslog(LOG_WARNING, "Giving up.");
185 "Warning: nonexistent local time.");
186 syslog(LOG_WARNING, "Will retry after %d minutes.",
188 (void) signal(SIGTERM, SIG_DFL);
189 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
190 (void) sleep(REPORT_PERIOD);
193 offset = -local.tm_gmtoff;
195 fprintf(stderr, "Initial offset: %ld secs\n", offset);
198 /* correct the kerneltime for this diffs */
199 /* subtract kernel offset, if present, old offset too */
201 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
205 fprintf(stderr, "Initial diff: %ld secs\n", diff);
207 /* Yet one step for final time */
209 final_sec = initial_sec + diff;
211 /* get the actual local timezone difference */
212 local = *localtime(&final_sec);
213 final_isdst = diff < 0 ? initial_isdst : local.tm_isdst;
214 if (diff > 0 && initial_isdst != final_isdst) {
218 initial_isdst = final_isdst;
221 local.tm_isdst = final_isdst;
223 localsec = mktime(&local);
224 if (localsec == -1) {
227 * XXX as above. The user has even less control,
228 * but perhaps we never get here.
232 "Warning: nonexistent final local time, try to run later.");
233 syslog(LOG_WARNING, "Giving up.");
237 "Warning: nonexistent final local time.");
238 syslog(LOG_WARNING, "Will retry after %d minutes.",
240 (void) signal(SIGTERM, SIG_DFL);
241 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
242 (void) sleep(REPORT_PERIOD);
245 offset = -local.tm_gmtoff;
247 fprintf(stderr, "Final offset: %ld secs\n", offset);
250 /* correct the kerneltime for this diffs */
251 /* subtract kernel offset, if present, old offset too */
253 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
257 fprintf(stderr, "Final diff: %ld secs\n", diff);
260 * stv is abused as a flag. The important value
267 if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
268 tz.tz_dsttime = tz.tz_minuteswest = 0; /* zone info is garbage */
271 if (!wall_clock && stz == NULL)
274 /* if init or UTC clock and offset/date will be changed, */
275 /* disable RTC modification for a while. */
277 if ( (init && stv != NULL)
278 || ((init || !wall_clock) && kern_offset != offset)
280 mib[0] = CTL_MACHDEP;
281 mib[1] = CPU_DISRTCSET;
282 len = sizeof(disrtcset);
283 if (sysctl(mib, 2, &disrtcset, &len, NULL, 0) == -1) {
284 syslog(LOG_ERR, "sysctl(get_disrtcset): %m");
287 if (disrtcset == 0) {
290 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
291 syslog(LOG_ERR, "sysctl(set_disrtcset): %m");
297 if ( (init && (stv != NULL || stz != NULL))
298 || (stz != NULL && stv == NULL)
302 * Get the time again, as close as possible to
303 * adjusting it, to minimise drift.
304 * XXX we'd better not fail between here and
305 * restoring disrtcset, since we don't clean up
308 if (gettimeofday(&tv, (struct timezone *)NULL)) {
309 syslog(LOG_ERR, "gettimeofday: %m");
315 if (settimeofday(stv, stz)) {
316 syslog(LOG_ERR, "settimeofday: %m");
321 /* setting CPU_ADJKERNTZ have a side effect: resettodr(), which */
322 /* can be disabled by CPU_DISRTCSET, so if init or UTC clock */
323 /* -- don't write RTC, else write RTC. */
325 if (kern_offset != offset) {
326 kern_offset = offset;
327 mib[0] = CTL_MACHDEP;
328 mib[1] = CPU_ADJKERNTZ;
329 len = sizeof(kern_offset);
330 if (sysctl(mib, 2, NULL, NULL, &kern_offset, len) == -1) {
331 syslog(LOG_ERR, "sysctl(update_offset): %m");
336 mib[0] = CTL_MACHDEP;
337 mib[1] = CPU_WALLCLOCK;
338 len = sizeof(wall_clock);
339 if (sysctl(mib, 2, NULL, NULL, &wall_clock, len) == -1) {
340 syslog(LOG_ERR, "sysctl(put_wallclock): %m");
345 need_restore = False;
346 mib[0] = CTL_MACHDEP;
347 mib[1] = CPU_DISRTCSET;
349 len = sizeof(disrtcset);
350 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
351 syslog(LOG_ERR, "sysctl(restore_disrtcset): %m");
356 /****** End of critical section ******/
358 if (init && wall_clock) {
360 /* wait for signals and acts like -a */
361 (void) sigsuspend(&emask);
371 fprintf(stderr, "%s\n%s\n%s\n%s\n",
372 "usage: adjkerntz -i",
373 "\t\t(initial call from /etc/rc)",
374 " adjkerntz -a [-s]",
375 "\t\t(adjustment call, -s for sleep/retry mode)");