Deleted Added
full compact
adjkerntz.c (4090) adjkerntz.c (4107)
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

--- 27 unchanged lines hidden (view full) ---

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>
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

--- 27 unchanged lines hidden (view full) ---

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>
44#include <stdlib.h>
45#include <unistd.h>
46#include <syslog.h>
47#include <sys/stat.h>
48#include <sys/time.h>
49#include <sys/param.h>
50#include <machine/cpu.h>
51#include <sys/sysctl.h>
52
53#include "pathnames.h"
54
55#define REPORT_PERIOD (30*60)
56
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
57int main(argc, argv)
58 int argc;
59 char **argv;
60{
61 struct tm local, utc;
62 struct timeval tv, *stv;
63 struct timezone tz, *stz;
64 int kern_offset;
65 size_t len;
66 int mib[2];
67 /* Avoid time_t here, can be unsigned long or worse */
68 long offset, utcsec, localsec, diff;
69 time_t initial_sec, final_sec;
70 int ch, init = -1;
71 int disrtcset, need_restore = 0;
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;
72
73 while ((ch = getopt(argc, argv, "ai")) != EOF)
74 switch((char)ch) {
75 case 'i': /* initial call, save offset */
76 if (init != -1)
77 goto usage;
78 init = 1;
79 break;

--- 10 unchanged lines hidden (view full) ---

90 return 2;
91 }
92 if (init == -1)
93 goto usage;
94
95 if (access(_PATH_CLOCK, F_OK))
96 return 0;
97
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;

--- 10 unchanged lines hidden (view full) ---

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
98 openlog("adjkerntz", LOG_PID|LOG_PERROR, LOG_DAEMON);
99
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
100again:
101
115again:
116
117 (void) sigprocmask(SIG_BLOCK, &mask, NULL);
118 (void) signal(SIGTERM, fake);
119
102/****** Critical section, do all things as fast as possible ******/
103
104 /* get local CMOS clock and possible kernel offset */
105 if (gettimeofday(&tv, &tz)) {
106 syslog(LOG_ERR, "gettimeofday: %m");
107 return 1;
108 }
109

--- 11 unchanged lines hidden (view full) ---

121 if (utcsec == -1 || localsec == -1) {
122 /*
123 * XXX user can only control local time, and it is
124 * unacceptable to fail here for init. 2:30 am in the
125 * middle of the nonexistent hour means 3:30 am.
126 */
127 syslog(LOG_WARNING,
128 "Nonexistent local time -- will retry after %d secs", REPORT_PERIOD);
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

--- 11 unchanged lines hidden (view full) ---

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);
129 (void) sleep(REPORT_PERIOD);
130 goto again;
131 }
132 offset = utcsec - localsec;
133
134 mib[0] = CTL_MACHDEP;
135 mib[1] = CPU_ADJKERNTZ;
136 len = sizeof(kern_offset);

--- 23 unchanged lines hidden (view full) ---

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 syslog(LOG_WARNING,
167 "Nonexistent (final) local time -- will retry after %d secs", REPORT_PERIOD);
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);

--- 23 unchanged lines hidden (view full) ---

180 localsec = mktime(&local);
181 if (utcsec == -1 || localsec == -1) {
182 /*
183 * XXX as above. The user has even less control,
184 * but perhaps we never get here.
185 */
186 syslog(LOG_WARNING,
187 "Nonexistent (final) local time -- will retry after %d secs", REPORT_PERIOD);
188 (void) signal(SIGTERM, SIG_DFL);
189 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
168 (void) sleep(REPORT_PERIOD);
169 goto again;
170 }
171 offset = utcsec - localsec;
172
173 /* correct the kerneltime for this diffs */
174 /* subtract kernel offset, if present, old offset too */
175

--- 60 unchanged lines hidden (view full) ---

236 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
237 syslog(LOG_ERR, "sysctl(restore_disrtcset): %m");
238 return 1;
239 }
240 }
241
242/****** End of critical section ******/
243
190 (void) sleep(REPORT_PERIOD);
191 goto again;
192 }
193 offset = utcsec - localsec;
194
195 /* correct the kerneltime for this diffs */
196 /* subtract kernel offset, if present, old offset too */
197

--- 60 unchanged lines hidden (view full) ---

258 if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
259 syslog(LOG_ERR, "sysctl(restore_disrtcset): %m");
260 return 1;
261 }
262 }
263
264/****** End of critical section ******/
265
266 if (init) {
267 init = 0;
268 /* wait for signals and acts like -a */
269 (void) sigsuspend(&emask);
270 goto again;
271 }
272
244 return 0;
245}
273 return 0;
274}