1 /*************************************************************************/
2 /* (c) Copyright Tai Jin, 1988. All Rights Reserved. */
3 /* Hewlett-Packard Laboratories. */
5 /* Permission is hereby granted for unlimited modification, use, and */
6 /* distribution. This software is made available with no warranty of */
7 /* any kind, express or implied. This copyright notice must remain */
8 /* intact in all versions of this software. */
10 /* The author would appreciate it if any bug fixes and enhancements were */
11 /* to be sent back to him for incorporation into future versions of this */
12 /* software. Please send changes to tai@iag.hp.com or ken@sdd.hp.com. */
13 /*************************************************************************/
16 static char RCSid[] = "adjtimed.c,v 3.1 1993/07/06 01:04:45 jbj Exp";
21 * This daemon adjusts the rate of the system clock a la BSD's adjtime().
22 * The adjtime() routine uses SYSV messages to communicate with this daemon.
24 * Caveat: This emulation uses an undocumented kernel variable. As such, it
25 * cannot be guaranteed to work in future HP-UX releases. Fortunately,
26 * it will no longer be needed in HPUX 10.01 and later.
29 #include <sys/param.h>
30 #include <sys/types.h>
41 #include "ntp_syslog.h"
42 #include "ntp_stdlib.h"
46 double atof (const char *);
48 int InitClockRate (void);
49 int AdjustClockRate (register struct timeval *delta, register struct timeval *olddelta);
50 long GetClockRate (void);
51 int SetClockRate (long);
52 void ResetClockRate (void);
56 #define MILLION 1000000L
58 /* emacs cc-mode goes nuts if we split the next line... */
59 #define tvtod(tv) ((double)tv.tv_sec + ((double)tv.tv_usec / (double)MILLION))
61 char *progname = NULL;
65 static double oldrate = 0.0;
73 struct timeval remains;
83 openlog("adjtimed", LOG_PID, LOG_LOCAL6);
85 openlog("adjtimed", LOG_PID);
88 while ((ch = ntp_getopt(argc, argv, "hkrvdfp:")) != EOF) {
92 if ((mqid = msgget(KEY, 0)) != -1) {
93 if (msgctl(mqid, IPC_RMID, (struct msqid_ds *)0) == -1) {
94 msyslog(LOG_ERR, "remove old message queue: %m");
95 perror("adjtimed: remove old message queue");
106 ++verbose, nofork = 1;
118 fputs("adjtimed: -p option ignored\n", stderr);
122 puts("usage: adjtimed -hkrvdf");
124 puts("-k\tkill existing adjtimed, if any");
125 puts("-r\trestart (kills existing adjtimed, if any)");
126 puts("-v\tdebug output (repeat for more output)");
127 puts("-d\tsyslog output (repeat for more output)");
129 msyslog(LOG_ERR, "usage error");
137 close(fileno(stdin));
138 close(fileno(stdout));
139 close(fileno(stderr));
142 if ((fd = open("/dev/tty")) != -1) {
143 ioctl(fd, TIOCNOTTY, 0);
152 msyslog(LOG_ERR, "fork: %m");
153 perror("adjtimed: fork");
162 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
163 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
166 msyslog(LOG_INFO, "started");
167 if (verbose) printf("adjtimed: started\n");
169 if (InitClockRate() == -1)
172 (void)signal(SIGHUP, SIG_IGN);
173 (void)signal(SIGINT, SIG_IGN);
174 (void)signal(SIGQUIT, SIG_IGN);
175 (void)signal(SIGTERM, Cleanup);
177 vec.sv_handler = ResetClockRate;
180 sigvector(SIGALRM, &vec, (struct sigvec *)0);
182 if (msgget(KEY, IPC_CREAT|IPC_EXCL) == -1) {
183 if (errno == EEXIST) {
184 msyslog(LOG_ERR, "message queue already exists, use -r to remove it");
185 fputs("adjtimed: message queue already exists, use -r to remove it\n",
190 msyslog(LOG_ERR, "create message queue: %m");
191 perror("adjtimed: create message queue");
195 if ((mqid = msgget(KEY, 0)) == -1) {
196 msyslog(LOG_ERR, "get message queue id: %m");
197 perror("adjtimed: get message queue id");
201 /* Lock process in memory to improve response time */
202 if (plock(PROCLOCK)) {
203 msyslog(LOG_ERR, "plock: %m");
204 perror("adjtimed: plock");
208 /* Also raise process priority.
209 * If we do not get run when we want, this leads to bad timekeeping
210 * and "Previous time adjustment didn't complete" gripes from xntpd.
212 if (nice(-10) == -1) {
213 msyslog(LOG_ERR, "nice: %m");
214 perror("adjtimed: nice");
219 if (msgrcv(mqid, &msg.msgp, MSGSIZE, CLIENT, 0) == -1) {
220 if (errno == EINTR) continue;
221 msyslog(LOG_ERR, "read message: %m");
222 perror("adjtimed: read message");
226 switch (msg.msgb.code) {
229 AdjustClockRate(&msg.msgb.tv, &remains);
231 if (msg.msgb.code == DELTA2) {
232 msg.msgb.tv = remains;
233 msg.msgb.mtype = SERVER;
235 while (msgsnd(mqid, &msg.msgp, MSGSIZE, 0) == -1) {
236 if (errno == EINTR) continue;
237 msyslog(LOG_ERR, "send message: %m");
238 perror("adjtimed: send message");
243 if (remains.tv_sec + remains.tv_usec != 0L) {
245 printf("adjtimed: previous correction remaining %.6fs\n",
249 msyslog(LOG_INFO, "previous correction remaining %.6fs",
256 fprintf(stderr, "adjtimed: unknown message code %d\n", msg.msgb.code);
257 msyslog(LOG_ERR, "unknown message code %d", msg.msgb.code);
263 * Default clock rate (old_tick).
265 #define DEFAULT_RATE (MILLION / HZ)
266 #define UNKNOWN_RATE 0L
267 #define TICK_ADJ 5 /* standard adjustment rate, microsec/tick */
269 static long default_rate = DEFAULT_RATE;
270 static long tick_rate = HZ; /* ticks per sec */
271 static long slew_rate = TICK_ADJ * HZ; /* in microsec/sec */
275 register struct timeval *delta,
276 register struct timeval *olddelta
279 register long rate, dt, leftover;
280 struct itimerval period, remains;
282 dt = (delta->tv_sec * MILLION) + delta->tv_usec;
285 printf("adjtimed: new correction %.6fs\n", (double)dt / (double)MILLION);
287 msyslog(LOG_INFO, "new correction %.6fs", (double)dt / (double)MILLION);
288 if (verbose > 2) printf("adjtimed: leftover %ldus\n", leftover);
289 if (sysdebug > 2) msyslog(LOG_INFO, "leftover %ldus", leftover);
293 * Apply a slew rate of slew_rate over a period of dt/slew_rate seconds.
301 period.it_value.tv_sec = dt / slew_rate;
302 period.it_value.tv_usec = (dt % slew_rate) * (MILLION / slew_rate);
304 * Note: we assume the kernel will convert the specified period into ticks
305 * using the modified clock rate rather than an assumed nominal clock rate,
306 * and therefore will generate the timer interrupt after the specified
307 * number of true seconds, not skewed seconds.
311 printf("adjtimed: will be complete in %lds %ldus\n",
312 period.it_value.tv_sec, period.it_value.tv_usec);
314 msyslog(LOG_INFO, "will be complete in %lds %ldus",
315 period.it_value.tv_sec, period.it_value.tv_usec);
317 * adjust the clock rate
320 if (SetClockRate((rate / tick_rate) + default_rate) == -1) {
321 msyslog(LOG_ERR, "set clock rate: %m");
322 perror("adjtimed: set clock rate");
327 * (do this after changing the rate because the period has been rounded down)
329 period.it_interval.tv_sec = period.it_interval.tv_usec = 0L;
330 setitimer(ITIMER_REAL, &period, &remains);
335 dt = ((remains.it_value.tv_sec * MILLION) + remains.it_value.tv_usec) *
337 olddelta->tv_sec = dt / MILLION;
338 olddelta->tv_usec = dt - (olddelta->tv_sec * MILLION);
341 oldrate = (double)rate / (double)MILLION;
343 } /* AdjustClockRate */
345 static struct nlist nl[] = {
361 * The return value is the clock rate in old_tick units or -1 if error.
368 if (lseek(kmem, (off_t)nl[0].n_value, 0) == -1L)
371 mask = sigblock(sigmask(SIGALRM));
373 if (read(kmem, (caddr_t)&rate, sizeof(rate)) != sizeof(rate))
381 * The argument is the new rate in old_tick units.
390 if (lseek(kmem, (off_t)nl[0].n_value, 0) == -1L)
393 mask = sigblock(sigmask(SIGALRM));
395 if (write(kmem, (caddr_t)&rate, sizeof(rate)) != sizeof(rate)) {
402 if (rate != default_rate) {
404 printf("adjtimed: clock rate (%lu) %ldus/s\n", rate,
405 (rate - default_rate) * tick_rate);
408 msyslog(LOG_INFO, "clock rate (%lu) %ldus/s", rate,
409 (rate - default_rate) * tick_rate);
419 if ((kmem = open("/dev/kmem", O_RDWR)) == -1) {
420 msyslog(LOG_ERR, "open(/dev/kmem): %m");
421 perror("adjtimed: open(/dev/kmem)");
427 if (nl[0].n_type == 0) {
428 fputs("adjtimed: /hp-ux has no symbol table\n", stderr);
429 msyslog(LOG_ERR, "/hp-ux has no symbol table");
433 * Set the default to the system's original value
435 default_rate = GetClockRate();
436 if (default_rate == UNKNOWN_RATE) default_rate = DEFAULT_RATE;
437 tick_rate = (MILLION / default_rate);
438 slew_rate = TICK_ADJ * tick_rate;
439 fprintf(stderr,"default_rate=%ld, tick_rate=%ld, slew_rate=%ld\n",default_rate,tick_rate,slew_rate);
442 } /* InitClockRate */
445 * Reset the clock rate to the default value.
452 it.it_value.tv_sec = it.it_value.tv_usec = 0L;
453 setitimer(ITIMER_REAL, &it, (struct itimerval *)0);
455 if (verbose > 2) puts("adjtimed: resetting the clock");
456 if (sysdebug > 2) msyslog(LOG_INFO, "resetting the clock");
458 if (GetClockRate() != default_rate) {
459 if (SetClockRate(default_rate) == -1) {
460 msyslog(LOG_ERR, "set clock rate: %m");
461 perror("adjtimed: set clock rate");
466 } /* ResetClockRate */
473 if (msgctl(mqid, IPC_RMID, (struct msqid_ds *)0) == -1) {
474 if (errno != EINVAL) {
475 msyslog(LOG_ERR, "remove message queue: %m");
476 perror("adjtimed: remove message queue");
487 msyslog(LOG_ERR, "terminated");
489 if (kmem != -1) close(kmem);