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

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

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

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

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#include <sys/param.h>
49#include <machine/cpu.h>
50#include <sys/sysctl.h>
48
49#include "pathnames.h"
50
51
52#include "pathnames.h"
53
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;
54int main(argc, argv)
55 int argc;
56 char **argv;
57{
58 struct tm local, utc;
59 struct timeval tv, *stv;
60 struct timezone tz, *stz;
61 int kern_offset;
62 size_t len;
63 int mib[2];
60 /* Avoid time_t here, can be unsigned long or worse */
64 /* Avoid time_t here, can be unsigned long or worse */
61 long offset, oldoffset, utcsec, localsec, diff;
65 long offset, utcsec, localsec, diff;
62 time_t initial_sec, final_sec;
63 int ch, init = -1, verbose = 0;
66 time_t initial_sec, final_sec;
67 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;

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

88 if (init == -1)
89 goto usage;
90
91 if (access(_PATH_CLOCK, F_OK))
92 return 0;
93
94 /* Restore saved offset */
95
68
69 while ((ch = getopt(argc, argv, "aiv")) != EOF)
70 switch((char)ch) {
71 case 'i': /* initial call, save offset */
72 if (init != -1)
73 goto usage;
74 init = 1;
75 break;

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

91 if (init == -1)
92 goto usage;
93
94 if (access(_PATH_CLOCK, F_OK))
95 return 0;
96
97 /* Restore saved offset */
98
96 if (!init) {
97 if ((f = fopen(storage, "r")) == NULL) {
98 perror(storage);
99 mib[0] = CTL_MACHDEP;
100 mib[1] = CPU_ADJKERNTZ;
101 len = sizeof(kern_offset);
102 if (sysctl(mib, 2, &kern_offset, &len, NULL, 0) == -1) {
103 perror("sysctl(get_offset)");
99 return 1;
100 }
104 return 1;
105 }
101 if (fscanf(f, "%ld", &oldoffset) != 1) {
102 fprintf(stderr, "Misformatted offset in %s\n",
103 storage);
104 return 1;
105 }
106 (void) fclose(f);
107 }
108 else
109 oldoffset = 0;
110
111/****** Critical section, do all things as fast as possible ******/
112
113 /* get local CMOS clock and possible kernel offset */
114 if (gettimeofday(&tv, &tz)) {
115 perror("gettimeofday");
116 return 1;
117 }

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

137 "Nonexistent local time - try again in an hour\n");
138 return 1;
139 }
140 offset = utcsec - localsec;
141
142 /* correct the kerneltime for this diffs */
143 /* subtract kernel offset, if present, old offset too */
144
106
107/****** Critical section, do all things as fast as possible ******/
108
109 /* get local CMOS clock and possible kernel offset */
110 if (gettimeofday(&tv, &tz)) {
111 perror("gettimeofday");
112 return 1;
113 }

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

133 "Nonexistent local time - try again in an hour\n");
134 return 1;
135 }
136 offset = utcsec - localsec;
137
138 /* correct the kerneltime for this diffs */
139 /* subtract kernel offset, if present, old offset too */
140
145 diff = offset - tz.tz_minuteswest * 60 - oldoffset;
141 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
146
147 if (diff != 0) {
148
149 /* Yet one step for final time */
150
151 final_sec = tv.tv_sec + diff;
152
153 /* get the actual local timezone difference */

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

167 "Nonexistent (final) local time - try again in an hour\n");
168 return 1;
169 }
170 offset = utcsec - localsec;
171
172 /* correct the kerneltime for this diffs */
173 /* subtract kernel offset, if present, old offset too */
174
142
143 if (diff != 0) {
144
145 /* Yet one step for final time */
146
147 final_sec = tv.tv_sec + diff;
148
149 /* get the actual local timezone difference */

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

163 "Nonexistent (final) local time - try again in an hour\n");
164 return 1;
165 }
166 offset = utcsec - localsec;
167
168 /* correct the kerneltime for this diffs */
169 /* subtract kernel offset, if present, old offset too */
170
175 diff = offset - tz.tz_minuteswest * 60 - oldoffset;
171 diff = offset - tz.tz_minuteswest * 60 - kern_offset;
176
177 if (diff != 0) {
178 tv.tv_sec += diff;
179 tv.tv_usec = 0; /* we are restarting here... */
180 stv = &tv;
181 }
182 else
183 stv = NULL;
184 }
185 else
186 stv = NULL;
187
172
173 if (diff != 0) {
174 tv.tv_sec += diff;
175 tv.tv_usec = 0; /* we are restarting here... */
176 stv = &tv;
177 }
178 else
179 stv = NULL;
180 }
181 else
182 stv = NULL;
183
184 if (kern_offset != offset) {
185 kern_offset = offset;
186 mib[0] = CTL_MACHDEP;
187 mib[1] = CPU_ADJKERNTZ;
188 len = sizeof(kern_offset);
189 if (sysctl(mib, 2, NULL, NULL, &kern_offset, len) == -1) {
190 perror("sysctl(update_offset)");
191 return 1;
192 }
193 }
194
188 if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
189 tz.tz_dsttime = tz.tz_minuteswest = 0; /* zone info is garbage */
190 stz = &tz;
191 }
192 else
193 stz = NULL;
194
195 if (stz != NULL || stv != NULL) {

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

200 }
201
202/****** End of critical section ******/
203
204 if (verbose)
205 printf("Calculated zone offset difference: %ld seconds\n",
206 diff);
207
195 if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
196 tz.tz_dsttime = tz.tz_minuteswest = 0; /* zone info is garbage */
197 stz = &tz;
198 }
199 else
200 stz = NULL;
201
202 if (stz != NULL || stv != NULL) {

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

207 }
208
209/****** End of critical section ******/
210
211 if (verbose)
212 printf("Calculated zone offset difference: %ld seconds\n",
213 diff);
214
208 if (offset != oldoffset) {
209 (void) umask(022);
210 /* Save offset for next calls from crontab */
211 if ((f = fopen(storage, "w")) == NULL) {
212 perror(storage);
213 return 1;
214 }
215 fprintf(f, "%ld\n", offset);
216 if (fclose(f)) {
217 perror(storage);
218 return 1;
219 }
220 }
221
222 return 0;
223}
224
215 return 0;
216}
217