adjkerntz.c revision 1092
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 <sys/stat.h>
47#include <sys/time.h>
48
49#include "pathnames.h"
50
51char storage[] = _PATH_OFFSET;
52
53int main(argc, argv)
54	int argc;
55	char **argv;
56{
57	struct tm local, utc;
58	struct timeval tv, *stv;
59	struct timezone tz, *stz;
60	/* Avoid time_t here, can be unsigned long or worse */
61	long offset, oldoffset, utcsec, localsec, diff;
62	time_t initial_sec, final_sec;
63	int ch, init = -1, verbose = 0;
64	FILE *f;
65
66	while ((ch = getopt(argc, argv, "aiv")) != EOF)
67		switch((char)ch) {
68		case 'i':               /* initial call, save offset */
69			if (init != -1)
70				goto usage;
71			init = 1;
72			break;
73		case 'a':               /* adjustment call, use saved offset */
74			if (init != -1)
75				goto usage;
76			init = 0;
77			break;
78		case 'v':               /* verbose */
79			verbose = 1;
80			break;
81		default:
82		usage:
83			fprintf(stderr, "Usage:\n\
84\tadjkerntz -i [-v]\t(initial call from /etc/rc)\n\
85\tadjkerntz -a [-v]\t(adjustment call from crontab)\n");
86			return 2;
87		}
88	if (init == -1)
89		goto usage;
90
91	if (access(_PATH_CLOCK, F_OK))
92		return 0;
93
94	/* Restore saved offset */
95
96	if (!init) {
97		if ((f = fopen(storage, "r")) == NULL) {
98			perror(storage);
99			return 1;
100		}
101		if (fscanf(f, "%ld", &oldoffset) != 1) {
102			fprintf(stderr, "Misformatted offset in %s\n",
103				storage);
104			return 1;
105		}
106		(void) fclose(f);
107	}
108	else
109		oldoffset = 0;
110
111/****** Critical section, do all things as fast as possible ******/
112
113	/* get local CMOS clock and possible kernel offset */
114	if (gettimeofday(&tv, &tz)) {
115		perror("gettimeofday");
116		return 1;
117	}
118
119	/* get the actual local timezone difference */
120	initial_sec = tv.tv_sec;
121	local = *localtime(&initial_sec);
122	utc = *gmtime(&initial_sec);
123	utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */
124				       /* because it assumed local time */
125
126	/* calculate local CMOS diff from GMT */
127
128	utcsec = mktime(&utc);
129	localsec = mktime(&local);
130	if (utcsec == -1 || localsec == -1) {
131		/*
132		 * XXX user can only control local time, and it is
133		 * unacceptable to fail here for -i.  2:30 am in the
134		 * middle of the nonexistent hour means 3:30 am.
135		 */
136		fprintf(stderr,
137			"Nonexistent local time - try again in an hour\n");
138		return 1;
139	}
140	offset = utcsec - localsec;
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 - oldoffset;
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			fprintf(stderr,
167		"Nonexistent (final) local time - try again in an hour\n");
168			return 1;
169		}
170		offset = utcsec - localsec;
171
172		/* correct the kerneltime for this diffs */
173		/* subtract kernel offset, if present, old offset too */
174
175		diff = offset - tz.tz_minuteswest * 60 - oldoffset;
176
177		if (diff != 0) {
178			tv.tv_sec += diff;
179			tv.tv_usec = 0;       /* we are restarting here... */
180			stv = &tv;
181		}
182		else
183			stv = NULL;
184	}
185	else
186		stv = NULL;
187
188	if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
189		tz.tz_dsttime = tz.tz_minuteswest = 0;  /* zone info is garbage */
190		stz = &tz;
191	}
192	else
193		stz = NULL;
194
195	if (stz != NULL || stv != NULL) {
196		if (settimeofday(stv, stz)) {
197			perror("settimeofday");
198			return 1;
199		}
200	}
201
202/****** End of critical section ******/
203
204	if (verbose)
205		printf("Calculated zone offset difference: %ld seconds\n",
206		       diff);
207
208	if (offset != oldoffset) {
209		(void) umask(022);
210		/* Save offset for next calls from crontab */
211		if ((f = fopen(storage, "w")) == NULL) {
212			perror(storage);
213			return 1;
214		}
215		fprintf(f, "%ld\n", offset);
216		if (fclose(f)) {
217			perror(storage);
218			return 1;
219		}
220	}
221
222	return 0;
223}
224
225