Deleted Added
full compact
adjkerntz.c (5077) adjkerntz.c (5078)
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 <signal.h>
45#include <stdlib.h>
46#include <unistd.h>
47#include <syslog.h>
48#include <sys/stat.h>
49#include <sys/time.h>
50#include <sys/param.h>
51#include <machine/cpu.h>
52#include <sys/sysctl.h>
53
54#include "pathnames.h"
55
56#define REPORT_PERIOD (30*60)
57
58void fake() {}
59
60int main(argc, argv)
61 int argc;
62 char **argv;
63{
64 struct tm local, utc;
65 struct timeval tv, *stv;
66 struct timezone tz, *stz;
67 int kern_offset;
68 size_t len;
69 int mib[2];
70 /* Avoid time_t here, can be unsigned long or worse */
71 long offset, utcsec, localsec, diff;
72 time_t initial_sec, final_sec;
73 int ch, init = -1;
74 int disrtcset, need_restore = 0;
75 sigset_t mask, emask;
76
77 while ((ch = getopt(argc, argv, "ai")) != EOF)
78 switch((char)ch) {
79 case 'i': /* initial call, save offset */
80 if (init != -1)
81 goto usage;
82 init = 1;
83 break;
84 case 'a': /* adjustment call, use saved offset */
85 if (init != -1)
86 goto usage;
87 init = 0;
88 break;
89 default:
90 usage:
91 fprintf(stderr, "Usage:\n\
92\tadjkerntz -i\t(initial call from /etc/rc)\n\
93\tadjkerntz -a\t(adjustment call from crontab)\n");
94 return 2;
95 }
96 if (init == -1)
97 goto usage;
98
99 if (access(_PATH_CLOCK, F_OK))
100 return 0;
101
102 sigemptyset(&mask);
103 sigemptyset(&emask);
104 sigaddset(&mask, SIGTERM);
105
106 openlog("adjkerntz", LOG_PID|LOG_PERROR, LOG_DAEMON);
107
108 (void) signal(SIGHUP, SIG_IGN);
109
110 if (init && daemon(0, 1)) {
111 syslog(LOG_ERR, "daemon: %m");
112 return 1;
113 }
114
115again:
116
117 (void) sigprocmask(SIG_BLOCK, &mask, NULL);
118 (void) signal(SIGTERM, fake);
119
120/****** Critical section, do all things as fast as possible ******/
121
122 /* get local CMOS clock and possible kernel offset */
123 if (gettimeofday(&tv, &tz)) {
124 syslog(LOG_ERR, "gettimeofday: %m");
125 return 1;
126 }
127
128 /* get the actual local timezone difference */
129 initial_sec = tv.tv_sec;
130 local = *localtime(&initial_sec);
131 utc = *gmtime(&initial_sec);
132 utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */
133 /* because it assumed local time */
134
135 /* calculate local CMOS diff from GMT */
136
137 utcsec = mktime(&utc);
138 localsec = mktime(&local);
139 if (utcsec == -1 || localsec == -1) {
140 /*
141 * XXX user can only control local time, and it is
142 * unacceptable to fail here for init. 2:30 am in the
143 * middle of the nonexistent hour means 3:30 am.
144 */
145 syslog(LOG_WARNING,
146 "Nonexistent local time -- will retry after %d secs", REPORT_PERIOD);
147 (void) signal(SIGTERM, SIG_DFL);
148 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
149 (void) sleep(REPORT_PERIOD);
150 goto again;
151 }
152 offset = utcsec - localsec;
153
154 mib[0] = CTL_MACHDEP;
155 mib[1] = CPU_ADJKERNTZ;
156 len = sizeof(kern_offset);
157 if (sysctl(mib, 2, &kern_offset, &len, NULL, 0) == -1) {
158 syslog(LOG_ERR, "sysctl(get_offset): %m");
159 return 1;
160 }
161
162 stv = NULL;
163 stz = NULL;
164
165 /* correct the kerneltime for this diffs */
166 /* subtract kernel offset, if present, old offset too */
167
168 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
169
170 if (diff != 0) {
171
172 /* Yet one step for final time */
173
174 final_sec = tv.tv_sec + diff;
175
176 /* get the actual local timezone difference */
177 local = *localtime(&final_sec);
178 utc = *gmtime(&final_sec);
179 utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */
180 /* because it assumed local time */
181
182 utcsec = mktime(&utc);
183 localsec = mktime(&local);
184 if (utcsec == -1 || localsec == -1) {
185 /*
186 * XXX as above. The user has even less control,
187 * but perhaps we never get here.
188 */
189 syslog(LOG_WARNING,
190 "Nonexistent (final) local time -- will retry after %d secs", REPORT_PERIOD);
191 (void) signal(SIGTERM, SIG_DFL);
192 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
193 (void) sleep(REPORT_PERIOD);
194 goto again;
195 }
196 offset = utcsec - localsec;
197
198 /* correct the kerneltime for this diffs */
199 /* subtract kernel offset, if present, old offset too */
200
201 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
202
203 if (diff != 0) {
204 tv.tv_sec += diff;
205 tv.tv_usec = 0; /* we are restarting here... */
206 stv = &tv;
207 }
208 }
209
210 if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
211 tz.tz_dsttime = tz.tz_minuteswest = 0; /* zone info is garbage */
212 stz = &tz;
213 }
214
215 /* if init and something will be changed, don't touch RTC at all */
216 if (init && (stv != NULL || kern_offset != offset)) {
217 mib[0] = CTL_MACHDEP;
218 mib[1] = CPU_DISRTCSET;
219 len = sizeof(disrtcset);
220 if (sysctl(mib, 2, &disrtcset, &len, NULL, 0) == -1) {
221 syslog(LOG_ERR, "sysctl(get_disrtcset): %m");
222 return 1;
223 }
224 if (disrtcset == 0) {
225 disrtcset = 1;
226 need_restore = 1;
227 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
228 syslog(LOG_ERR, "sysctl(set_disrtcset): %m");
229 return 1;
230 }
231 }
232 }
233
234 if (( (init && (stv != NULL || stz != NULL))
235 || (stz != NULL && stv == NULL)
236 )
237 && settimeofday(stv, stz)
238 ) {
239 syslog(LOG_ERR, "settimeofday: %m");
240 return 1;
241 }
242
243 /* init: don't write RTC, !init: write RTC */
244 if (kern_offset != offset) {
245 kern_offset = offset;
246 mib[0] = CTL_MACHDEP;
247 mib[1] = CPU_ADJKERNTZ;
248 len = sizeof(kern_offset);
249 if (sysctl(mib, 2, NULL, NULL, &kern_offset, len) == -1) {
250 syslog(LOG_ERR, "sysctl(update_offset): %m");
251 return 1;
252 }
253 }
254
255 if (need_restore) {
256 need_restore = 0;
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 <signal.h>
45#include <stdlib.h>
46#include <unistd.h>
47#include <syslog.h>
48#include <sys/stat.h>
49#include <sys/time.h>
50#include <sys/param.h>
51#include <machine/cpu.h>
52#include <sys/sysctl.h>
53
54#include "pathnames.h"
55
56#define REPORT_PERIOD (30*60)
57
58void fake() {}
59
60int main(argc, argv)
61 int argc;
62 char **argv;
63{
64 struct tm local, utc;
65 struct timeval tv, *stv;
66 struct timezone tz, *stz;
67 int kern_offset;
68 size_t len;
69 int mib[2];
70 /* Avoid time_t here, can be unsigned long or worse */
71 long offset, utcsec, localsec, diff;
72 time_t initial_sec, final_sec;
73 int ch, init = -1;
74 int disrtcset, need_restore = 0;
75 sigset_t mask, emask;
76
77 while ((ch = getopt(argc, argv, "ai")) != EOF)
78 switch((char)ch) {
79 case 'i': /* initial call, save offset */
80 if (init != -1)
81 goto usage;
82 init = 1;
83 break;
84 case 'a': /* adjustment call, use saved offset */
85 if (init != -1)
86 goto usage;
87 init = 0;
88 break;
89 default:
90 usage:
91 fprintf(stderr, "Usage:\n\
92\tadjkerntz -i\t(initial call from /etc/rc)\n\
93\tadjkerntz -a\t(adjustment call from crontab)\n");
94 return 2;
95 }
96 if (init == -1)
97 goto usage;
98
99 if (access(_PATH_CLOCK, F_OK))
100 return 0;
101
102 sigemptyset(&mask);
103 sigemptyset(&emask);
104 sigaddset(&mask, SIGTERM);
105
106 openlog("adjkerntz", LOG_PID|LOG_PERROR, LOG_DAEMON);
107
108 (void) signal(SIGHUP, SIG_IGN);
109
110 if (init && daemon(0, 1)) {
111 syslog(LOG_ERR, "daemon: %m");
112 return 1;
113 }
114
115again:
116
117 (void) sigprocmask(SIG_BLOCK, &mask, NULL);
118 (void) signal(SIGTERM, fake);
119
120/****** Critical section, do all things as fast as possible ******/
121
122 /* get local CMOS clock and possible kernel offset */
123 if (gettimeofday(&tv, &tz)) {
124 syslog(LOG_ERR, "gettimeofday: %m");
125 return 1;
126 }
127
128 /* get the actual local timezone difference */
129 initial_sec = tv.tv_sec;
130 local = *localtime(&initial_sec);
131 utc = *gmtime(&initial_sec);
132 utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */
133 /* because it assumed local time */
134
135 /* calculate local CMOS diff from GMT */
136
137 utcsec = mktime(&utc);
138 localsec = mktime(&local);
139 if (utcsec == -1 || localsec == -1) {
140 /*
141 * XXX user can only control local time, and it is
142 * unacceptable to fail here for init. 2:30 am in the
143 * middle of the nonexistent hour means 3:30 am.
144 */
145 syslog(LOG_WARNING,
146 "Nonexistent local time -- will retry after %d secs", REPORT_PERIOD);
147 (void) signal(SIGTERM, SIG_DFL);
148 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
149 (void) sleep(REPORT_PERIOD);
150 goto again;
151 }
152 offset = utcsec - localsec;
153
154 mib[0] = CTL_MACHDEP;
155 mib[1] = CPU_ADJKERNTZ;
156 len = sizeof(kern_offset);
157 if (sysctl(mib, 2, &kern_offset, &len, NULL, 0) == -1) {
158 syslog(LOG_ERR, "sysctl(get_offset): %m");
159 return 1;
160 }
161
162 stv = NULL;
163 stz = NULL;
164
165 /* correct the kerneltime for this diffs */
166 /* subtract kernel offset, if present, old offset too */
167
168 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
169
170 if (diff != 0) {
171
172 /* Yet one step for final time */
173
174 final_sec = tv.tv_sec + diff;
175
176 /* get the actual local timezone difference */
177 local = *localtime(&final_sec);
178 utc = *gmtime(&final_sec);
179 utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */
180 /* because it assumed local time */
181
182 utcsec = mktime(&utc);
183 localsec = mktime(&local);
184 if (utcsec == -1 || localsec == -1) {
185 /*
186 * XXX as above. The user has even less control,
187 * but perhaps we never get here.
188 */
189 syslog(LOG_WARNING,
190 "Nonexistent (final) local time -- will retry after %d secs", REPORT_PERIOD);
191 (void) signal(SIGTERM, SIG_DFL);
192 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
193 (void) sleep(REPORT_PERIOD);
194 goto again;
195 }
196 offset = utcsec - localsec;
197
198 /* correct the kerneltime for this diffs */
199 /* subtract kernel offset, if present, old offset too */
200
201 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
202
203 if (diff != 0) {
204 tv.tv_sec += diff;
205 tv.tv_usec = 0; /* we are restarting here... */
206 stv = &tv;
207 }
208 }
209
210 if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
211 tz.tz_dsttime = tz.tz_minuteswest = 0; /* zone info is garbage */
212 stz = &tz;
213 }
214
215 /* if init and something will be changed, don't touch RTC at all */
216 if (init && (stv != NULL || kern_offset != offset)) {
217 mib[0] = CTL_MACHDEP;
218 mib[1] = CPU_DISRTCSET;
219 len = sizeof(disrtcset);
220 if (sysctl(mib, 2, &disrtcset, &len, NULL, 0) == -1) {
221 syslog(LOG_ERR, "sysctl(get_disrtcset): %m");
222 return 1;
223 }
224 if (disrtcset == 0) {
225 disrtcset = 1;
226 need_restore = 1;
227 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
228 syslog(LOG_ERR, "sysctl(set_disrtcset): %m");
229 return 1;
230 }
231 }
232 }
233
234 if (( (init && (stv != NULL || stz != NULL))
235 || (stz != NULL && stv == NULL)
236 )
237 && settimeofday(stv, stz)
238 ) {
239 syslog(LOG_ERR, "settimeofday: %m");
240 return 1;
241 }
242
243 /* init: don't write RTC, !init: write RTC */
244 if (kern_offset != offset) {
245 kern_offset = offset;
246 mib[0] = CTL_MACHDEP;
247 mib[1] = CPU_ADJKERNTZ;
248 len = sizeof(kern_offset);
249 if (sysctl(mib, 2, NULL, NULL, &kern_offset, len) == -1) {
250 syslog(LOG_ERR, "sysctl(update_offset): %m");
251 return 1;
252 }
253 }
254
255 if (need_restore) {
256 need_restore = 0;
257 mib[0] = CTL_MACHDEP;
258 mib[1] = CPU_DISRTCSET;
257 disrtcset = 0;
258 len = sizeof(disrtcset);
259 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
260 syslog(LOG_ERR, "sysctl(restore_disrtcset): %m");
261 return 1;
262 }
263 }
264
265/****** End of critical section ******/
266
267 if (init) {
268 init = 0;
269 /* wait for signals and acts like -a */
270 (void) sigsuspend(&emask);
271 goto again;
272 }
273
274 return 0;
275}
259 disrtcset = 0;
260 len = sizeof(disrtcset);
261 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
262 syslog(LOG_ERR, "sysctl(restore_disrtcset): %m");
263 return 1;
264 }
265 }
266
267/****** End of critical section ******/
268
269 if (init) {
270 init = 0;
271 /* wait for signals and acts like -a */
272 (void) sigsuspend(&emask);
273 goto again;
274 }
275
276 return 0;
277}