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
28 static const char copyright[] =
29 "@(#)Copyright (C) 1993-1996 by Andrey A. Chernov, Moscow, Russia.\n\
30 All rights reserved.\n";
34 static const char rcsid[] =
35 "$FreeBSD: src/sbin/adjkerntz/adjkerntz.c,v 1.25.2.1 2001/07/30 10:38:04 dd Exp $";
39 * Andrey A. Chernov <ache@astral.msk.su> Dec 20 1993
41 * Fix kernel time value if machine run wall CMOS clock
42 * (and /etc/wall_cmos_clock file present)
43 * using zoneinfo rules or direct TZ environment variable set.
44 * Use Joerg Wunsch idea for seconds accurate offset calculation
45 * with Garrett Wollman and Bruce Evans fixes.
54 #include <sys/param.h>
55 #include <machine/cpu.h>
56 #include <sys/sysctl.h>
58 #include "pathnames.h"
66 #define REPORT_PERIOD (30*60)
68 static void fake __P((int));
69 static void usage __P((void));
84 struct timeval tv, *stv;
85 struct timezone tz, *stz;
86 int kern_offset, wall_clock, disrtcset;
89 /* Avoid time_t here, can be unsigned long or worse */
90 long offset, localsec, diff;
91 time_t initial_sec, final_sec;
93 int initial_isdst = -1, final_isdst;
94 int need_restore = False, sleep_mode = False, looping,
98 while ((ch = getopt(argc, argv, "ais")) != -1)
100 case 'i': /* initial call, save offset */
105 case 'a': /* adjustment call, use saved offset */
119 if (access(_PATH_CLOCK, F_OK) != 0)
127 sigaddset(&mask, SIGTERM);
129 openlog("adjkerntz", LOG_PID|LOG_PERROR, LOG_DAEMON);
131 (void) signal(SIGHUP, SIG_IGN);
133 if (init && daemon(0, 1)) {
134 syslog(LOG_ERR, "daemon: %m");
139 (void) sigprocmask(SIG_BLOCK, &mask, NULL);
140 (void) signal(SIGTERM, fake);
147 wall_clock = (access(_PATH_CLOCK, F_OK) == 0);
148 if (init && !sleep_mode) {
154 mib[0] = CTL_MACHDEP;
155 mib[1] = CPU_ADJKERNTZ;
156 len = sizeof(kern_offset);
157 if (sysctl(mib, 2, &kern_offset, &len, NULL, 0) == -1) {
158 syslog(LOG_ERR, "sysctl(get_offset): %m");
162 /****** Critical section, do all things as fast as possible ******/
164 /* get local CMOS clock and possible kernel offset */
165 if (gettimeofday(&tv, &tz)) {
166 syslog(LOG_ERR, "gettimeofday: %m");
170 /* get the actual local timezone difference */
171 initial_sec = tv.tv_sec;
174 local = *localtime(&initial_sec);
176 initial_isdst = local.tm_isdst;
177 local.tm_isdst = initial_isdst;
179 /* calculate local CMOS diff from GMT */
181 localsec = mktime(&local);
182 if (localsec == -1) {
184 * XXX user can only control local time, and it is
185 * unacceptable to fail here for init. 2:30 am in the
186 * middle of the nonexistent hour means 3:30 am.
190 "Warning: nonexistent local time, try to run later.");
191 syslog(LOG_WARNING, "Giving up.");
195 "Warning: nonexistent local time.");
196 syslog(LOG_WARNING, "Will retry after %d minutes.",
198 (void) signal(SIGTERM, SIG_DFL);
199 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
200 (void) sleep(REPORT_PERIOD);
203 offset = -local.tm_gmtoff;
205 fprintf(stderr, "Initial offset: %ld secs\n", offset);
208 /* correct the kerneltime for this diffs */
209 /* subtract kernel offset, if present, old offset too */
211 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
215 fprintf(stderr, "Initial diff: %ld secs\n", diff);
217 /* Yet one step for final time */
219 final_sec = initial_sec + diff;
221 /* get the actual local timezone difference */
222 local = *localtime(&final_sec);
223 final_isdst = diff < 0 ? initial_isdst : local.tm_isdst;
224 if (diff > 0 && initial_isdst != final_isdst) {
228 initial_isdst = final_isdst;
231 local.tm_isdst = final_isdst;
233 localsec = mktime(&local);
234 if (localsec == -1) {
237 * XXX as above. The user has even less control,
238 * but perhaps we never get here.
242 "Warning: nonexistent final local time, try to run later.");
243 syslog(LOG_WARNING, "Giving up.");
247 "Warning: nonexistent final local time.");
248 syslog(LOG_WARNING, "Will retry after %d minutes.",
250 (void) signal(SIGTERM, SIG_DFL);
251 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
252 (void) sleep(REPORT_PERIOD);
255 offset = -local.tm_gmtoff;
257 fprintf(stderr, "Final offset: %ld secs\n", offset);
260 /* correct the kerneltime for this diffs */
261 /* subtract kernel offset, if present, old offset too */
263 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
267 fprintf(stderr, "Final diff: %ld secs\n", diff);
270 * stv is abused as a flag. The important value
277 if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
278 tz.tz_dsttime = tz.tz_minuteswest = 0; /* zone info is garbage */
281 if (!wall_clock && stz == NULL)
284 /* if init or UTC clock and offset/date will be changed, */
285 /* disable RTC modification for a while. */
287 if ( (init && stv != NULL)
288 || ((init || !wall_clock) && kern_offset != offset)
290 mib[0] = CTL_MACHDEP;
291 mib[1] = CPU_DISRTCSET;
292 len = sizeof(disrtcset);
293 if (sysctl(mib, 2, &disrtcset, &len, NULL, 0) == -1) {
294 syslog(LOG_ERR, "sysctl(get_disrtcset): %m");
297 if (disrtcset == 0) {
300 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
301 syslog(LOG_ERR, "sysctl(set_disrtcset): %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 if (gettimeofday(&tv, (struct timezone *)NULL)) {
319 syslog(LOG_ERR, "gettimeofday: %m");
325 if (settimeofday(stv, stz)) {
326 syslog(LOG_ERR, "settimeofday: %m");
331 /* setting CPU_ADJKERNTZ have a side effect: resettodr(), which */
332 /* can be disabled by CPU_DISRTCSET, so if init or UTC clock */
333 /* -- don't write RTC, else write RTC. */
335 if (kern_offset != offset) {
336 kern_offset = offset;
337 mib[0] = CTL_MACHDEP;
338 mib[1] = CPU_ADJKERNTZ;
339 len = sizeof(kern_offset);
340 if (sysctl(mib, 2, NULL, NULL, &kern_offset, len) == -1) {
341 syslog(LOG_ERR, "sysctl(update_offset): %m");
346 mib[0] = CTL_MACHDEP;
347 mib[1] = CPU_WALLCLOCK;
348 len = sizeof(wall_clock);
349 if (sysctl(mib, 2, NULL, NULL, &wall_clock, len) == -1) {
350 syslog(LOG_ERR, "sysctl(put_wallclock): %m");
355 need_restore = False;
356 mib[0] = CTL_MACHDEP;
357 mib[1] = CPU_DISRTCSET;
359 len = sizeof(disrtcset);
360 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
361 syslog(LOG_ERR, "sysctl(restore_disrtcset): %m");
366 /****** End of critical section ******/
368 if (init && wall_clock) {
370 /* wait for signals and acts like -a */
371 (void) sigsuspend(&emask);
381 fprintf(stderr, "%s\n%s\n%s\n%s\n",
382 "usage: adjkerntz -i",
383 "\t\t(initial call from /etc/rc)",
384 " adjkerntz -a [-s]",
385 "\t\t(adjustment call, -s for sleep/retry mode)");