1993-12-16 18:44:40 +00:00
|
|
|
/*
|
1995-03-26 22:51:51 +00:00
|
|
|
* Copyright (C) 1993, 1994, 1995 by Andrey A. Chernov, Moscow, Russia.
|
1993-12-16 18:44:40 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
1994-02-05 11:42:48 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
|
1993-12-16 18:44:40 +00:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
char copyright[] =
|
1995-03-26 22:51:51 +00:00
|
|
|
"@(#)Copyright (C) 1993, 1994, 1995 by Andrey A. Chernov, Moscow, Russia.\n\
|
1993-12-16 18:44:40 +00:00
|
|
|
All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
1995-03-26 22:51:51 +00:00
|
|
|
* Andrey A. Chernov <ache@astral.msk.su> Dec 20 1993
|
1993-12-16 18:44:40 +00:00
|
|
|
*
|
|
|
|
* Fix kernel time value if machine run wall CMOS clock
|
|
|
|
* (and /etc/wall_cmos_clock file present)
|
|
|
|
* using zoneinfo rules or direct TZ environment variable set.
|
|
|
|
* Use Joerg Wunsch idea for seconds accurate offset calculation
|
|
|
|
* with Garrett Wollman and Bruce Evans fixes.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
1994-11-03 03:16:16 +00:00
|
|
|
#include <signal.h>
|
1993-12-16 18:44:40 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
1994-11-01 16:22:22 +00:00
|
|
|
#include <syslog.h>
|
1993-12-16 18:44:40 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
1994-09-19 23:09:34 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
#include <sys/sysctl.h>
|
1993-12-16 18:44:40 +00:00
|
|
|
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
1994-12-26 10:43:48 +00:00
|
|
|
/*#define DEBUG*/
|
1994-11-01 16:22:22 +00:00
|
|
|
#define REPORT_PERIOD (30*60)
|
|
|
|
|
1994-11-03 03:16:16 +00:00
|
|
|
void fake() {}
|
|
|
|
|
1993-12-16 18:44:40 +00:00
|
|
|
int main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
|
|
|
struct tm local, utc;
|
|
|
|
struct timeval tv, *stv;
|
|
|
|
struct timezone tz, *stz;
|
1994-09-19 23:09:34 +00:00
|
|
|
int kern_offset;
|
|
|
|
size_t len;
|
|
|
|
int mib[2];
|
1994-02-05 11:42:48 +00:00
|
|
|
/* Avoid time_t here, can be unsigned long or worse */
|
1994-09-19 23:09:34 +00:00
|
|
|
long offset, utcsec, localsec, diff;
|
1994-02-05 11:42:48 +00:00
|
|
|
time_t initial_sec, final_sec;
|
1994-11-02 09:40:06 +00:00
|
|
|
int ch, init = -1;
|
1995-03-26 22:51:51 +00:00
|
|
|
int initial_isdst = -1, final_isdst, looping;
|
1994-11-01 06:20:52 +00:00
|
|
|
int disrtcset, need_restore = 0;
|
1994-11-03 03:16:16 +00:00
|
|
|
sigset_t mask, emask;
|
1993-12-16 18:44:40 +00:00
|
|
|
|
1994-11-02 09:40:06 +00:00
|
|
|
while ((ch = getopt(argc, argv, "ai")) != EOF)
|
|
|
|
switch((char)ch) {
|
|
|
|
case 'i': /* initial call, save offset */
|
|
|
|
if (init != -1)
|
|
|
|
goto usage;
|
|
|
|
init = 1;
|
|
|
|
break;
|
|
|
|
case 'a': /* adjustment call, use saved offset */
|
|
|
|
if (init != -1)
|
|
|
|
goto usage;
|
|
|
|
init = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage:
|
|
|
|
fprintf(stderr, "Usage:\n\
|
|
|
|
\tadjkerntz -i\t(initial call from /etc/rc)\n\
|
|
|
|
\tadjkerntz -a\t(adjustment call from crontab)\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (init == -1)
|
|
|
|
goto usage;
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1993-12-16 18:44:40 +00:00
|
|
|
if (access(_PATH_CLOCK, F_OK))
|
|
|
|
return 0;
|
|
|
|
|
1994-11-03 03:16:16 +00:00
|
|
|
sigemptyset(&mask);
|
|
|
|
sigemptyset(&emask);
|
|
|
|
sigaddset(&mask, SIGTERM);
|
|
|
|
|
1994-11-02 09:40:06 +00:00
|
|
|
openlog("adjkerntz", LOG_PID|LOG_PERROR, LOG_DAEMON);
|
|
|
|
|
1994-11-03 03:16:16 +00:00
|
|
|
(void) signal(SIGHUP, SIG_IGN);
|
|
|
|
|
|
|
|
if (init && daemon(0, 1)) {
|
|
|
|
syslog(LOG_ERR, "daemon: %m");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1994-11-02 09:40:06 +00:00
|
|
|
again:
|
|
|
|
|
1994-11-03 03:16:16 +00:00
|
|
|
(void) sigprocmask(SIG_BLOCK, &mask, NULL);
|
|
|
|
(void) signal(SIGTERM, fake);
|
|
|
|
|
1995-03-26 22:51:51 +00:00
|
|
|
diff = 0;
|
|
|
|
stv = NULL;
|
|
|
|
stz = NULL;
|
|
|
|
looping = 0;
|
|
|
|
|
|
|
|
mib[0] = CTL_MACHDEP;
|
|
|
|
mib[1] = CPU_ADJKERNTZ;
|
|
|
|
len = sizeof(kern_offset);
|
|
|
|
if (sysctl(mib, 2, &kern_offset, &len, NULL, 0) == -1) {
|
|
|
|
syslog(LOG_ERR, "sysctl(get_offset): %m");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1994-11-02 09:40:06 +00:00
|
|
|
/****** Critical section, do all things as fast as possible ******/
|
|
|
|
|
|
|
|
/* get local CMOS clock and possible kernel offset */
|
|
|
|
if (gettimeofday(&tv, &tz)) {
|
|
|
|
syslog(LOG_ERR, "gettimeofday: %m");
|
1993-12-16 18:44:40 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1994-11-02 09:40:06 +00:00
|
|
|
/* get the actual local timezone difference */
|
|
|
|
initial_sec = tv.tv_sec;
|
1995-03-26 22:51:51 +00:00
|
|
|
|
|
|
|
recalculate:
|
1994-11-02 09:40:06 +00:00
|
|
|
local = *localtime(&initial_sec);
|
1995-03-26 22:51:51 +00:00
|
|
|
if (diff == 0)
|
|
|
|
initial_isdst = local.tm_isdst;
|
1994-11-02 09:40:06 +00:00
|
|
|
utc = *gmtime(&initial_sec);
|
1995-03-26 22:51:51 +00:00
|
|
|
local.tm_isdst = utc.tm_isdst = initial_isdst;
|
1994-11-02 09:40:06 +00:00
|
|
|
|
|
|
|
/* calculate local CMOS diff from GMT */
|
|
|
|
|
1995-03-26 22:51:51 +00:00
|
|
|
utcsec = mktime(&utc);
|
|
|
|
localsec = mktime(&local);
|
1994-11-02 09:40:06 +00:00
|
|
|
if (utcsec == -1 || localsec == -1) {
|
|
|
|
/*
|
|
|
|
* XXX user can only control local time, and it is
|
|
|
|
* unacceptable to fail here for init. 2:30 am in the
|
|
|
|
* middle of the nonexistent hour means 3:30 am.
|
|
|
|
*/
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"Nonexistent local time -- will retry after %d secs", REPORT_PERIOD);
|
1994-11-03 03:16:16 +00:00
|
|
|
(void) signal(SIGTERM, SIG_DFL);
|
|
|
|
(void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
1994-11-02 09:40:06 +00:00
|
|
|
(void) sleep(REPORT_PERIOD);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
offset = utcsec - localsec;
|
1994-12-26 10:43:48 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Initial offset: %ld secs\n", offset);
|
|
|
|
#endif
|
1993-12-16 18:44:40 +00:00
|
|
|
|
1994-11-02 09:40:06 +00:00
|
|
|
/* correct the kerneltime for this diffs */
|
|
|
|
/* subtract kernel offset, if present, old offset too */
|
1993-12-20 01:53:27 +00:00
|
|
|
|
1994-11-02 09:40:06 +00:00
|
|
|
diff = offset - tz.tz_minuteswest * 60 - kern_offset;
|
|
|
|
|
|
|
|
if (diff != 0) {
|
1994-12-26 10:43:48 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Initial diff: %ld secs\n", diff);
|
|
|
|
#endif
|
1994-11-02 09:40:06 +00:00
|
|
|
/* Yet one step for final time */
|
|
|
|
|
1995-03-26 22:51:51 +00:00
|
|
|
final_sec = initial_sec + diff;
|
1993-12-20 01:53:27 +00:00
|
|
|
|
|
|
|
/* get the actual local timezone difference */
|
1994-11-02 09:40:06 +00:00
|
|
|
local = *localtime(&final_sec);
|
1995-03-26 22:51:51 +00:00
|
|
|
final_isdst = diff < 0 ? initial_isdst : local.tm_isdst;
|
|
|
|
if (diff > 0 && initial_isdst != final_isdst) {
|
|
|
|
if (looping)
|
|
|
|
goto bad_final;
|
|
|
|
looping++;
|
|
|
|
initial_isdst = final_isdst;
|
|
|
|
goto recalculate;
|
|
|
|
}
|
1994-11-02 09:40:06 +00:00
|
|
|
utc = *gmtime(&final_sec);
|
1995-03-26 22:51:51 +00:00
|
|
|
local.tm_isdst = utc.tm_isdst = final_isdst;
|
1993-12-20 01:53:27 +00:00
|
|
|
|
1995-03-26 22:51:51 +00:00
|
|
|
utcsec = mktime(&utc);
|
|
|
|
localsec = mktime(&local);
|
1993-12-20 01:53:27 +00:00
|
|
|
if (utcsec == -1 || localsec == -1) {
|
1995-03-26 22:51:51 +00:00
|
|
|
bad_final:
|
1994-02-05 11:42:48 +00:00
|
|
|
/*
|
1994-11-02 09:40:06 +00:00
|
|
|
* XXX as above. The user has even less control,
|
|
|
|
* but perhaps we never get here.
|
1994-02-05 11:42:48 +00:00
|
|
|
*/
|
1994-11-02 09:40:06 +00:00
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"Nonexistent (final) local time -- will retry after %d secs", REPORT_PERIOD);
|
1994-11-03 03:16:16 +00:00
|
|
|
(void) signal(SIGTERM, SIG_DFL);
|
|
|
|
(void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
1994-11-02 09:40:06 +00:00
|
|
|
(void) sleep(REPORT_PERIOD);
|
|
|
|
goto again;
|
1993-12-20 01:53:27 +00:00
|
|
|
}
|
|
|
|
offset = utcsec - localsec;
|
1994-12-26 10:43:48 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Final offset: %ld secs\n", offset);
|
|
|
|
#endif
|
1993-12-20 01:53:27 +00:00
|
|
|
|
|
|
|
/* correct the kerneltime for this diffs */
|
|
|
|
/* subtract kernel offset, if present, old offset too */
|
|
|
|
|
1994-09-19 23:09:34 +00:00
|
|
|
diff = offset - tz.tz_minuteswest * 60 - kern_offset;
|
1993-12-20 01:53:27 +00:00
|
|
|
|
|
|
|
if (diff != 0) {
|
1994-12-26 10:43:48 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Final diff: %ld secs\n", diff);
|
|
|
|
#endif
|
1994-11-02 09:40:06 +00:00
|
|
|
tv.tv_sec += diff;
|
|
|
|
tv.tv_usec = 0; /* we are restarting here... */
|
|
|
|
stv = &tv;
|
1993-12-20 01:53:27 +00:00
|
|
|
}
|
1994-11-02 09:40:06 +00:00
|
|
|
}
|
1993-12-16 18:44:40 +00:00
|
|
|
|
1994-11-02 09:40:06 +00:00
|
|
|
if (tz.tz_dsttime != 0 || tz.tz_minuteswest != 0) {
|
|
|
|
tz.tz_dsttime = tz.tz_minuteswest = 0; /* zone info is garbage */
|
|
|
|
stz = &tz;
|
|
|
|
}
|
1994-12-12 05:20:44 +00:00
|
|
|
|
1994-12-12 05:43:28 +00:00
|
|
|
/* if init and something will be changed, don't touch RTC at all */
|
|
|
|
if (init && (stv != NULL || kern_offset != offset)) {
|
1994-12-12 05:20:44 +00:00
|
|
|
mib[0] = CTL_MACHDEP;
|
|
|
|
mib[1] = CPU_DISRTCSET;
|
|
|
|
len = sizeof(disrtcset);
|
|
|
|
if (sysctl(mib, 2, &disrtcset, &len, NULL, 0) == -1) {
|
|
|
|
syslog(LOG_ERR, "sysctl(get_disrtcset): %m");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (disrtcset == 0) {
|
|
|
|
disrtcset = 1;
|
|
|
|
need_restore = 1;
|
|
|
|
if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
|
|
|
|
syslog(LOG_ERR, "sysctl(set_disrtcset): %m");
|
1994-11-02 09:40:06 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1994-11-01 06:20:52 +00:00
|
|
|
|
1994-12-12 05:20:44 +00:00
|
|
|
if (( (init && (stv != NULL || stz != NULL))
|
|
|
|
|| (stz != NULL && stv == NULL)
|
|
|
|
)
|
|
|
|
&& settimeofday(stv, stz)
|
|
|
|
) {
|
|
|
|
syslog(LOG_ERR, "settimeofday: %m");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init: don't write RTC, !init: write RTC */
|
1994-11-02 09:40:06 +00:00
|
|
|
if (kern_offset != offset) {
|
|
|
|
kern_offset = offset;
|
|
|
|
mib[0] = CTL_MACHDEP;
|
|
|
|
mib[1] = CPU_ADJKERNTZ;
|
|
|
|
len = sizeof(kern_offset);
|
|
|
|
if (sysctl(mib, 2, NULL, NULL, &kern_offset, len) == -1) {
|
|
|
|
syslog(LOG_ERR, "sysctl(update_offset): %m");
|
|
|
|
return 1;
|
1994-11-01 06:20:52 +00:00
|
|
|
}
|
1994-11-02 09:40:06 +00:00
|
|
|
}
|
1994-11-01 06:20:52 +00:00
|
|
|
|
1994-11-02 09:40:06 +00:00
|
|
|
if (need_restore) {
|
|
|
|
need_restore = 0;
|
1994-12-12 05:48:45 +00:00
|
|
|
mib[0] = CTL_MACHDEP;
|
|
|
|
mib[1] = CPU_DISRTCSET;
|
1994-11-02 09:40:06 +00:00
|
|
|
disrtcset = 0;
|
1994-12-12 05:20:44 +00:00
|
|
|
len = sizeof(disrtcset);
|
1994-11-02 09:40:06 +00:00
|
|
|
if (sysctl(mib, 2, NULL, NULL, &disrtcset, len) == -1) {
|
|
|
|
syslog(LOG_ERR, "sysctl(restore_disrtcset): %m");
|
|
|
|
return 1;
|
1994-10-04 19:15:13 +00:00
|
|
|
}
|
1994-11-02 09:40:06 +00:00
|
|
|
}
|
1993-12-16 18:44:40 +00:00
|
|
|
|
|
|
|
/****** End of critical section ******/
|
|
|
|
|
1994-11-03 03:16:16 +00:00
|
|
|
if (init) {
|
|
|
|
init = 0;
|
|
|
|
/* wait for signals and acts like -a */
|
|
|
|
(void) sigsuspend(&emask);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
1994-11-02 09:40:06 +00:00
|
|
|
return 0;
|
1993-12-16 18:44:40 +00:00
|
|
|
}
|