Commit | Line | Data |
---|---|---|
7cb69b29 AC |
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 | |
28 | char copyright[] = | |
29 | "@(#)Copyright (C) 1993 by Andrew A. Chernov, Moscow, Russia.\n\ | |
30 | All rights reserved.\n"; | |
31 | #endif /* not lint */ | |
32 | ||
33 | /* | |
89703544 | 34 | * Andrew A. Chernov <ache@astral.msk.su> Dec 20 1993 |
7cb69b29 AC |
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 | ||
51 | char storage[] = _PATH_OFFSET; | |
52 | ||
53 | int 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; | |
89703544 | 62 | time_t final_sec; |
7cb69b29 AC |
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, "Incorrect offset in %s\n", storage); | |
103 | return 1; | |
104 | } | |
105 | (void) fclose(f); | |
106 | } | |
107 | else | |
108 | oldoffset = 0; | |
109 | ||
110 | /****** Critical section, do all things as fast as possible ******/ | |
111 | ||
112 | /* get local CMOS clock and possible kernel offset */ | |
113 | if (gettimeofday(&tv, &tz)) { | |
114 | perror("gettimeofday"); | |
115 | return 1; | |
116 | } | |
117 | ||
118 | /* get the actual local timezone difference */ | |
119 | local = *localtime(&tv.tv_sec); | |
120 | utc = *gmtime(&tv.tv_sec); | |
121 | utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */ | |
122 | /* because it assumed local time */ | |
123 | ||
124 | /* calculate local CMOS diff from GMT */ | |
125 | ||
126 | utcsec = mktime(&utc); | |
127 | localsec = mktime(&local); | |
128 | if (utcsec == -1 || localsec == -1) { | |
89703544 | 129 | fprintf(stderr, "Wrong initial hour to call\n"); |
7cb69b29 AC |
130 | return 1; |
131 | } | |
132 | offset = utcsec - localsec; | |
133 | ||
134 | /* correct the kerneltime for this diffs */ | |
135 | /* subtract kernel offset, if present, old offset too */ | |
136 | ||
89703544 AC |
137 | diff = offset - tz.tz_minuteswest * 60 - oldoffset; |
138 | ||
7cb69b29 | 139 | if (diff != 0) { |
89703544 AC |
140 | |
141 | /* Yet one step for final time */ | |
142 | ||
143 | final_sec = tv.tv_sec + diff; | |
144 | ||
145 | /* get the actual local timezone difference */ | |
146 | local = *localtime(&final_sec); | |
147 | utc = *gmtime(&final_sec); | |
148 | utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */ | |
149 | /* because it assumed local time */ | |
150 | ||
151 | utcsec = mktime(&utc); | |
152 | localsec = mktime(&local); | |
153 | if (utcsec == -1 || localsec == -1) { | |
154 | fprintf(stderr, "Wrong final hour to call\n"); | |
155 | return 1; | |
156 | } | |
157 | offset = utcsec - localsec; | |
158 | ||
159 | /* correct the kerneltime for this diffs */ | |
160 | /* subtract kernel offset, if present, old offset too */ | |
161 | ||
162 | diff = offset - tz.tz_minuteswest * 60 - oldoffset; | |
163 | ||
164 | if (diff != 0) { | |
165 | tv.tv_sec += diff; | |
166 | tv.tv_usec = 0; /* we are restarting here... */ | |
167 | stv = &tv; | |
168 | } | |
169 | else | |
170 | stv = NULL; | |
7cb69b29 AC |
171 | } |
172 | else | |
173 | stv = NULL; | |
174 | ||
175 | if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) { | |
176 | tz.tz_dsttime = tz.tz_minuteswest = 0; /* zone info is garbage */ | |
177 | stz = &tz; | |
178 | } | |
179 | else | |
180 | stz = NULL; | |
181 | ||
182 | if (stz != NULL || stv != NULL) { | |
183 | if (settimeofday(stv, stz)) { | |
184 | perror("settimeofday"); | |
185 | return 1; | |
186 | } | |
187 | } | |
188 | ||
189 | /****** End of critical section ******/ | |
190 | ||
191 | if (verbose) | |
192 | printf("Calculated zone offset diffs: %ld seconds\n", diff); | |
193 | ||
194 | if (offset != oldoffset) { | |
195 | (void) umask(022); | |
196 | /* Save offset for next calls from crontab */ | |
197 | if ((f = fopen(storage, "w")) == NULL) { | |
198 | perror(storage); | |
199 | return 1; | |
200 | } | |
201 | fprintf(f, "%ld\n", offset); | |
202 | if (fclose(f)) { | |
203 | perror(storage); | |
204 | return 1; | |
205 | } | |
206 | } | |
207 | ||
208 | return 0; | |
209 | } | |
210 |