adjkerntz.c revision 868
1172302Spjd/*
2172302Spjd * Copyright (C) 1993 by Andrew A. Chernov, Moscow, Russia.
3172302Spjd * All rights reserved.
4172302Spjd *
5172302Spjd * Redistribution and use in source and binary forms, with or without
6172302Spjd * modification, are permitted provided that the following conditions
7172302Spjd * are met:
8172302Spjd * 1. Redistributions of source code must retain the above copyright
9172302Spjd *    notice, this list of conditions and the following disclaimer.
10172302Spjd * 2. Redistributions in binary form must reproduce the above copyright
11172302Spjd *    notice, this list of conditions and the following disclaimer in the
12172302Spjd *    documentation and/or other materials provided with the distribution.
13172302Spjd *
14172302Spjd * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND
15172302Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16172302Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17172302Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18172302Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19172302Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20172302Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21172302Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22172302Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23172302Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24172302Spjd * SUCH DAMAGE.
25172302Spjd */
26172302Spjd
27172302Spjd#ifndef lint
28172302Spjdchar copyright[] =
29172302Spjd"@(#)Copyright (C) 1993 by Andrew A. Chernov, Moscow, Russia.\n\
30172302Spjd All rights reserved.\n";
31172302Spjd#endif /* not lint */
32172302Spjd
33172302Spjd/*
34172302Spjd * Andrew A. Chernov   <ache@astral.msk.su>    Dec 15 1993
35172302Spjd *
36172302Spjd * Fix kernel time value if machine run wall CMOS clock
37172302Spjd * (and /etc/wall_cmos_clock file present)
38172302Spjd * using zoneinfo rules or direct TZ environment variable set.
39172302Spjd * Use Joerg Wunsch idea for seconds accurate offset calculation
40172302Spjd * with Garrett Wollman and Bruce Evans fixes.
41172302Spjd *
42172302Spjd */
43172302Spjd#include <stdio.h>
44172302Spjd#include <stdlib.h>
45172302Spjd#include <unistd.h>
46172302Spjd#include <sys/stat.h>
47172302Spjd#include <sys/time.h>
48172302Spjd
49172302Spjd#include "pathnames.h"
50172302Spjd
51172302Spjdchar storage[] = _PATH_OFFSET;
52172302Spjd
53172302Spjdint main(argc, argv)
54172302Spjd	int argc;
55172302Spjd	char **argv;
56172302Spjd{
57172302Spjd	struct tm local, utc;
58172302Spjd	struct timeval tv, *stv;
59172302Spjd	struct timezone tz, *stz;
60172302Spjd	/* Avoid time_t here, can be unsigned long */
61172302Spjd	long offset, oldoffset, utcsec, localsec, diff;
62172302Spjd	int ch, init = -1, verbose = 0;
63172302Spjd	FILE *f;
64172302Spjd
65172302Spjd	while ((ch = getopt(argc, argv, "aiv")) != EOF)
66172302Spjd		switch((char)ch) {
67172302Spjd		case 'i':               /* initial call, save offset */
68172302Spjd			if (init != -1)
69172302Spjd				goto usage;
70172302Spjd			init = 1;
71172302Spjd			break;
72172302Spjd		case 'a':               /* adjustment call, use saved offset */
73172302Spjd			if (init != -1)
74172302Spjd				goto usage;
75172302Spjd			init = 0;
76172302Spjd			break;
77172302Spjd		case 'v':               /* verbose */
78172302Spjd			verbose = 1;
79172302Spjd			break;
80172302Spjd		default:
81172302Spjd		usage:
82172302Spjd			fprintf(stderr, "Usage:\n\
83172302Spjd\tadjkerntz -i [-v]\t(initial call from /etc/rc)\n\
84172302Spjd\tadjkerntz -a [-v]\t(adjustment call from crontab)\n");
85172302Spjd			return 2;
86172302Spjd		}
87172302Spjd	if (init == -1)
88172302Spjd		goto usage;
89172302Spjd
90172302Spjd	if (access(_PATH_CLOCK, F_OK))
91172302Spjd		return 0;
92172302Spjd
93172302Spjd	/* 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