adjkerntz.c revision 868
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 DEVELOPERS ``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 15 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 */
61	long offset, oldoffset, utcsec, localsec, diff;
62	int ch, init = -1, verbose = 0;
63	FILE *f;
64
65	while ((ch = getopt(argc, argv, "aiv")) != EOF)
66		switch((char)ch) {
67		case 'i':               /* initial call, save offset */
68			if (init != -1)
69				goto usage;
70			init = 1;
71			break;
72		case 'a':               /* adjustment call, use saved offset */
73			if (init != -1)
74				goto usage;
75			init = 0;
76			break;
77		case 'v':               /* verbose */
78			verbose = 1;
79			break;
80		default:
81		usage:
82			fprintf(stderr, "Usage:\n\
83\tadjkerntz -i [-v]\t(initial call from /etc/rc)\n\
84\tadjkerntz -a [-v]\t(adjustment call from crontab)\n");
85			return 2;
86		}
87	if (init == -1)
88		goto usage;
89
90	if (access(_PATH_CLOCK, F_OK))
91		return 0;
92
93	/* Restore saved offset */
94
95	if (!init) {
96		if ((f = fopen(storage, "r")) == NULL) {
97			perror(storage);
98			return 1;
99		}
100		if (fscanf(f, "%ld", &oldoffset) != 1) {
101			fprintf(stderr, "Incorrect offset in %s\n", storage);
102			return 1;
103		}
104		(void) fclose(f);
105	}
106	else
107		oldoffset = 0;
108
109/****** Critical section, do all things as fast as possible ******/
110
111	/* get local CMOS clock and possible kernel offset */
112	if (gettimeofday(&tv, &tz)) {
113		perror("gettimeofday");
114		return 1;
115	}
116
117	/* get the actual local timezone difference */
118	local = *localtime(&tv.tv_sec);
119	utc = *gmtime(&tv.tv_sec);
120	utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */
121				       /* because it assumed local time */
122
123	/* calculate local CMOS diff from GMT */
124
125	utcsec = mktime(&utc);
126	localsec = mktime(&local);
127	if (utcsec == -1 || localsec == -1) {
128		fprintf(stderr, "Wrong hour to call\n");
129		return 1;
130	}
131	offset = utcsec - localsec;
132
133	/* correct the kerneltime for this diffs */
134	/* subtract kernel offset, if present, old offset too */
135
136	diff = oldoffset + tz.tz_minuteswest * 60 - offset;
137	if (diff != 0) {
138		tv.tv_sec += diff;
139		tv.tv_usec = 0;       /* we are restarting here... */
140		stv = &tv;
141	}
142	else
143		stv = NULL;
144
145	if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
146		tz.tz_dsttime = tz.tz_minuteswest = 0;  /* zone info is garbage */
147		stz = &tz;
148	}
149	else
150		stz = NULL;
151
152	if (stz != NULL || stv != NULL) {
153		if (settimeofday(stv, stz)) {
154			perror("settimeofday");
155			return 1;
156		}
157	}
158
159/****** End of critical section ******/
160
161	if (verbose)
162		printf("Calculated zone offset diffs: %ld seconds\n", diff);
163
164	if (offset != oldoffset) {
165		(void) umask(022);
166		/* Save offset for next calls from crontab */
167		if ((f = fopen(storage, "w")) == NULL) {
168			perror(storage);
169			return 1;
170		}
171		fprintf(f, "%ld\n", offset);
172		if (fclose(f)) {
173			perror(storage);
174			return 1;
175		}
176	}
177
178	return 0;
179}
180
181