adjkerntz.c revision 4090
1/*
2 * Copyright (C) 1993 by Andrew A. Chernov, Moscow, Russia.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#ifndef lint
28char copyright[] =
29"@(#)Copyright (C) 1993 by Andrew A. Chernov, Moscow, Russia.\n\
30 All rights reserved.\n";
31#endif /* not lint */
32
33/*
34 * Andrew A. Chernov   <ache@astral.msk.su>    Dec 20 1993
35 *
36 * Fix kernel time value if machine run wall CMOS clock
37 * (and /etc/wall_cmos_clock file present)
38 * using zoneinfo rules or direct TZ environment variable set.
39 * Use Joerg Wunsch idea for seconds accurate offset calculation
40 * with Garrett Wollman and Bruce Evans fixes.
41 *
42 */
43#include <stdio.h>
44#include <stdlib.h>
45#include <unistd.h>
46#include <syslog.h>
47#include <sys/stat.h>
48#include <sys/time.h>
49#include <sys/param.h>
50#include <machine/cpu.h>
51#include <sys/sysctl.h>
52
53#include "pathnames.h"
54
55#define REPORT_PERIOD (30*60)
56
57int main(argc, argv)
58	int argc;
59	char **argv;
60{
61	struct tm local, utc;
62	struct timeval tv, *stv;
63	struct timezone tz, *stz;
64	int kern_offset;
65	size_t len;
66	int mib[2];
67	/* Avoid time_t here, can be unsigned long or worse */
68	long offset, utcsec, localsec, diff;
69	time_t initial_sec, final_sec;
70	int ch, init = -1;
71	int disrtcset, need_restore = 0;
72
73	while ((ch = getopt(argc, argv, "ai")) != EOF)
74		switch((char)ch) {
75		case 'i':               /* initial call, save offset */
76			if (init != -1)
77				goto usage;
78			init = 1;
79			break;
80		case 'a':               /* adjustment call, use saved offset */
81			if (init != -1)
82				goto usage;
83			init = 0;
84			break;
85		default:
86		usage:
87			fprintf(stderr, "Usage:\n\
88\tadjkerntz -i\t(initial call from /etc/rc)\n\
89\tadjkerntz -a\t(adjustment call from crontab)\n");
90  			return 2;
91		}
92	if (init == -1)
93		goto usage;
94
95	if (access(_PATH_CLOCK, F_OK))
96		return 0;
97
98	openlog("adjkerntz", LOG_PID|LOG_PERROR, LOG_DAEMON);
99
100again:
101
102/****** Critical section, do all things as fast as possible ******/
103
104	/* get local CMOS clock and possible kernel offset */
105	if (gettimeofday(&tv, &tz)) {
106		syslog(LOG_ERR, "gettimeofday: %m");
107		return 1;
108	}
109
110	/* get the actual local timezone difference */
111	initial_sec = tv.tv_sec;
112	local = *localtime(&initial_sec);
113	utc = *gmtime(&initial_sec);
114	utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */
115				       /* because it assumed local time */
116
117	/* calculate local CMOS diff from GMT */
118
119	utcsec = mktime(&utc);
120	localsec = mktime(&local);
121	if (utcsec == -1 || localsec == -1) {
122		/*
123		 * XXX user can only control local time, and it is
124		 * unacceptable to fail here for init.  2:30 am in the
125		 * middle of the nonexistent hour means 3:30 am.
126		 */
127		syslog(LOG_WARNING,
128		"Nonexistent local time -- will retry after %d secs", REPORT_PERIOD);
129		(void) sleep(REPORT_PERIOD);
130		goto again;
131	}
132	offset = utcsec - localsec;
133
134	mib[0] = CTL_MACHDEP;
135	mib[1] = CPU_ADJKERNTZ;
136	len = sizeof(kern_offset);
137	if (sysctl(mib, 2, &kern_offset, &len, NULL, 0) == -1) {
138		syslog(LOG_ERR, "sysctl(get_offset): %m");
139		return 1;
140	}
141
142	/* correct the kerneltime for this diffs */
143	/* subtract kernel offset, if present, old offset too */
144
145	diff = offset - tz.tz_minuteswest * 60 - kern_offset;
146
147	if (diff != 0) {
148
149		/* Yet one step for final time */
150
151		final_sec = tv.tv_sec + diff;
152
153		/* get the actual local timezone difference */
154		local = *localtime(&final_sec);
155		utc = *gmtime(&final_sec);
156		utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */
157					       /* because it assumed local time */
158
159		utcsec = mktime(&utc);
160		localsec = mktime(&local);
161		if (utcsec == -1 || localsec == -1) {
162			/*
163			 * XXX as above.  The user has even less control,
164			 * but perhaps we never get here.
165			 */
166			syslog(LOG_WARNING,
167		"Nonexistent (final) local time -- will retry after %d secs", REPORT_PERIOD);
168			(void) sleep(REPORT_PERIOD);
169			goto again;
170		}
171		offset = utcsec - localsec;
172
173		/* correct the kerneltime for this diffs */
174		/* subtract kernel offset, if present, old offset too */
175
176		diff = offset - tz.tz_minuteswest * 60 - kern_offset;
177
178		if (diff != 0) {
179			tv.tv_sec += diff;
180			tv.tv_usec = 0;       /* we are restarting here... */
181			stv = &tv;
182		}
183		else
184			stv = NULL;
185	}
186	else
187		stv = NULL;
188
189	if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
190		tz.tz_dsttime = tz.tz_minuteswest = 0;  /* zone info is garbage */
191		stz = &tz;
192	}
193	else
194		stz = NULL;
195
196	if (stz != NULL || stv != NULL) {
197		if (init && stv != NULL) {
198			mib[0] = CTL_MACHDEP;
199			mib[1] = CPU_DISRTCSET;
200			len = sizeof(disrtcset);
201			if (sysctl(mib, 2, &disrtcset, &len, NULL, 0) == -1) {
202				syslog(LOG_ERR, "sysctl(get_disrtcset): %m");
203				return 1;
204			}
205			if (disrtcset == 0) {
206				disrtcset = 1;
207				need_restore = 1;
208				if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
209					syslog(LOG_ERR, "sysctl(set_disrtcset): %m");
210					return 1;
211				}
212			}
213		}
214		/* stz means that kernel zone shifted */
215		/* clock needs adjustment even if !init */
216		if ((init || stz != NULL) && settimeofday(stv, stz)) {
217			syslog(LOG_ERR, "settimeofday: %m");
218			return 1;
219		}
220	}
221
222	if (kern_offset != offset) {
223		kern_offset = offset;
224		mib[0] = CTL_MACHDEP;
225		mib[1] = CPU_ADJKERNTZ;
226		len = sizeof(kern_offset);
227		if (sysctl(mib, 2, NULL, NULL, &kern_offset, len) == -1) {
228			syslog(LOG_ERR, "sysctl(update_offset): %m");
229			return 1;
230		}
231	}
232
233	if (need_restore) {
234		need_restore = 0;
235		disrtcset = 0;
236		if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
237			syslog(LOG_ERR, "sysctl(restore_disrtcset): %m");
238			return 1;
239		}
240	}
241
242/****** End of critical section ******/
243
244	return 0;
245}
246