adjkerntz.c revision 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 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 <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> 51 52#include "pathnames.h" 53 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]; 64 /* Avoid time_t here, can be unsigned long or worse */ 65 long offset, utcsec, localsec, diff; 66 time_t initial_sec, final_sec; 67 int ch, init = -1, verbose = 0; 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; 76 case 'a': /* adjustment call, use saved offset */ 77 if (init != -1) 78 goto usage; 79 init = 0; 80 break; 81 case 'v': /* verbose */ 82 verbose = 1; 83 break; 84 default: 85 usage: 86 fprintf(stderr, "Usage:\n\ 87\tadjkerntz -i [-v]\t(initial call from /etc/rc)\n\ 88\tadjkerntz -a [-v]\t(adjustment call from crontab)\n"); 89 return 2; 90 } 91 if (init == -1) 92 goto usage; 93 94 if (access(_PATH_CLOCK, F_OK)) 95 return 0; 96 97 /* Restore saved offset */ 98 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)"); 104 return 1; 105 } 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 } 114 115 /* get the actual local timezone difference */ 116 initial_sec = tv.tv_sec; 117 local = *localtime(&initial_sec); 118 utc = *gmtime(&initial_sec); 119 utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */ 120 /* because it assumed local time */ 121 122 /* calculate local CMOS diff from GMT */ 123 124 utcsec = mktime(&utc); 125 localsec = mktime(&local); 126 if (utcsec == -1 || localsec == -1) { 127 /* 128 * XXX user can only control local time, and it is 129 * unacceptable to fail here for -i. 2:30 am in the 130 * middle of the nonexistent hour means 3:30 am. 131 */ 132 fprintf(stderr, 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 141 diff = offset - tz.tz_minuteswest * 60 - kern_offset; 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 */ 150 local = *localtime(&final_sec); 151 utc = *gmtime(&final_sec); 152 utc.tm_isdst = local.tm_isdst; /* Use current timezone for mktime(), */ 153 /* because it assumed local time */ 154 155 utcsec = mktime(&utc); 156 localsec = mktime(&local); 157 if (utcsec == -1 || localsec == -1) { 158 /* 159 * XXX as above. The user has even less control, 160 * but perhaps we never get here. 161 */ 162 fprintf(stderr, 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 171 diff = offset - tz.tz_minuteswest * 60 - kern_offset; 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 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) { 203 if (settimeofday(stv, stz)) { 204 perror("settimeofday"); 205 return 1; 206 } 207 } 208 209/****** End of critical section ******/ 210 211 if (verbose) 212 printf("Calculated zone offset difference: %ld seconds\n", 213 diff); 214 215 return 0; 216} 217 218