3 /*************************************************************************/
4 /* (c) Copyright Tai Jin, 1988. All Rights Reserved. */
5 /* Hewlett-Packard Laboratories. */
7 /* Permission is hereby granted for unlimited modification, use, and */
8 /* distribution. This software is made available with no warranty of */
9 /* any kind, express or implied. This copyright notice must remain */
10 /* intact in all versions of this software. */
12 /* The author would appreciate it if any bug fixes and enhancements were */
13 /* to be sent back to him for incorporation into future versions of this */
14 /* software. Please send changes to tai@iag.hp.com or ken@sdd.hp.com. */
15 /*************************************************************************/
18 static char RCSid
[] = "adjtimed.c,v 3.1 1993/07/06 01:04:45 jbj Exp";
23 * This daemon adjusts the rate of the system clock a la BSD's adjtime().
24 * The adjtime() routine uses SYSV messages to communicate with this daemon.
26 * Caveat: This emulation uses an undocumented kernel variable. As such, it
27 * cannot be guaranteed to work in future HP-UX releases. Fortunately,
28 * it will no longer be needed in HPUX 10.01 and later.
31 #include <sys/param.h>
32 #include <sys/types.h>
43 #include "ntp_syslog.h"
44 #include "ntp_stdlib.h"
48 double atof (const char *);
50 int InitClockRate (void);
51 int AdjustClockRate (register struct timeval
*delta
, register struct timeval
*olddelta
);
52 long GetClockRate (void);
53 int SetClockRate (long);
54 void ResetClockRate (void);
58 #define MILLION 1000000L
60 /* emacs cc-mode goes nuts if we split the next line... */
61 #define tvtod(tv) ((double)tv.tv_sec + ((double)tv.tv_usec / (double)MILLION))
63 char *progname
= NULL
;
67 static double oldrate
= 0.0;
75 struct timeval remains
;
85 openlog("adjtimed", LOG_PID
, LOG_LOCAL6
);
87 openlog("adjtimed", LOG_PID
);
90 while ((ch
= ntp_getopt(argc
, argv
, "hkrvdfp:")) != EOF
) {
94 if ((mqid
= msgget(KEY
, 0)) != -1) {
95 if (msgctl(mqid
, IPC_RMID
, (struct msqid_ds
*)0) == -1) {
96 msyslog(LOG_ERR
, "remove old message queue: %m");
97 perror("adjtimed: remove old message queue");
108 ++verbose
, nofork
= 1;
120 fputs("adjtimed: -p option ignored\n", stderr
);
124 puts("usage: adjtimed -hkrvdf");
126 puts("-k\tkill existing adjtimed, if any");
127 puts("-r\trestart (kills existing adjtimed, if any)");
128 puts("-v\tdebug output (repeat for more output)");
129 puts("-d\tsyslog output (repeat for more output)");
131 msyslog(LOG_ERR
, "usage error");
139 close(fileno(stdin
));
140 close(fileno(stdout
));
141 close(fileno(stderr
));
144 if ((fd
= open("/dev/tty")) != -1) {
145 ioctl(fd
, TIOCNOTTY
, 0);
154 msyslog(LOG_ERR
, "fork: %m");
155 perror("adjtimed: fork");
164 setvbuf(stdout
, NULL
, _IONBF
, BUFSIZ
);
165 setvbuf(stderr
, NULL
, _IONBF
, BUFSIZ
);
168 msyslog(LOG_INFO
, "started");
169 if (verbose
) printf("adjtimed: started\n");
171 if (InitClockRate() == -1)
174 (void)signal(SIGHUP
, SIG_IGN
);
175 (void)signal(SIGINT
, SIG_IGN
);
176 (void)signal(SIGQUIT
, SIG_IGN
);
177 (void)signal(SIGTERM
, Cleanup
);
179 vec
.sv_handler
= ResetClockRate
;
182 sigvector(SIGALRM
, &vec
, (struct sigvec
*)0);
184 if (msgget(KEY
, IPC_CREAT
|IPC_EXCL
) == -1) {
185 if (errno
== EEXIST
) {
186 msyslog(LOG_ERR
, "message queue already exists, use -r to remove it");
187 fputs("adjtimed: message queue already exists, use -r to remove it\n",
192 msyslog(LOG_ERR
, "create message queue: %m");
193 perror("adjtimed: create message queue");
197 if ((mqid
= msgget(KEY
, 0)) == -1) {
198 msyslog(LOG_ERR
, "get message queue id: %m");
199 perror("adjtimed: get message queue id");
203 /* Lock process in memory to improve response time */
204 if (plock(PROCLOCK
)) {
205 msyslog(LOG_ERR
, "plock: %m");
206 perror("adjtimed: plock");
210 /* Also raise process priority.
211 * If we do not get run when we want, this leads to bad timekeeping
212 * and "Previous time adjustment didn't complete" gripes from xntpd.
214 if (nice(-10) == -1) {
215 msyslog(LOG_ERR
, "nice: %m");
216 perror("adjtimed: nice");
221 if (msgrcv(mqid
, &msg
.msgp
, MSGSIZE
, CLIENT
, 0) == -1) {
222 if (errno
== EINTR
) continue;
223 msyslog(LOG_ERR
, "read message: %m");
224 perror("adjtimed: read message");
228 switch (msg
.msgb
.code
) {
231 AdjustClockRate(&msg
.msgb
.tv
, &remains
);
233 if (msg
.msgb
.code
== DELTA2
) {
234 msg
.msgb
.tv
= remains
;
235 msg
.msgb
.mtype
= SERVER
;
237 while (msgsnd(mqid
, &msg
.msgp
, MSGSIZE
, 0) == -1) {
238 if (errno
== EINTR
) continue;
239 msyslog(LOG_ERR
, "send message: %m");
240 perror("adjtimed: send message");
245 if (remains
.tv_sec
+ remains
.tv_usec
!= 0L) {
247 printf("adjtimed: previous correction remaining %.6fs\n",
251 msyslog(LOG_INFO
, "previous correction remaining %.6fs",
258 fprintf(stderr
, "adjtimed: unknown message code %d\n", msg
.msgb
.code
);
259 msyslog(LOG_ERR
, "unknown message code %d", msg
.msgb
.code
);
265 * Default clock rate (old_tick).
267 #define DEFAULT_RATE (MILLION / HZ)
268 #define UNKNOWN_RATE 0L
269 #define TICK_ADJ 5 /* standard adjustment rate, microsec/tick */
271 static long default_rate
= DEFAULT_RATE
;
272 static long tick_rate
= HZ
; /* ticks per sec */
273 static long slew_rate
= TICK_ADJ
* HZ
; /* in microsec/sec */
277 register struct timeval
*delta
,
278 register struct timeval
*olddelta
281 register long rate
, dt
, leftover
;
282 struct itimerval period
, remains
;
284 dt
= (delta
->tv_sec
* MILLION
) + delta
->tv_usec
;
287 printf("adjtimed: new correction %.6fs\n", (double)dt
/ (double)MILLION
);
289 msyslog(LOG_INFO
, "new correction %.6fs", (double)dt
/ (double)MILLION
);
290 if (verbose
> 2) printf("adjtimed: leftover %ldus\n", leftover
);
291 if (sysdebug
> 2) msyslog(LOG_INFO
, "leftover %ldus", leftover
);
295 * Apply a slew rate of slew_rate over a period of dt/slew_rate seconds.
303 period
.it_value
.tv_sec
= dt
/ slew_rate
;
304 period
.it_value
.tv_usec
= (dt
% slew_rate
) * (MILLION
/ slew_rate
);
306 * Note: we assume the kernel will convert the specified period into ticks
307 * using the modified clock rate rather than an assumed nominal clock rate,
308 * and therefore will generate the timer interrupt after the specified
309 * number of true seconds, not skewed seconds.
313 printf("adjtimed: will be complete in %lds %ldus\n",
314 period
.it_value
.tv_sec
, period
.it_value
.tv_usec
);
316 msyslog(LOG_INFO
, "will be complete in %lds %ldus",
317 period
.it_value
.tv_sec
, period
.it_value
.tv_usec
);
319 * adjust the clock rate
322 if (SetClockRate((rate
/ tick_rate
) + default_rate
) == -1) {
323 msyslog(LOG_ERR
, "set clock rate: %m");
324 perror("adjtimed: set clock rate");
329 * (do this after changing the rate because the period has been rounded down)
331 period
.it_interval
.tv_sec
= period
.it_interval
.tv_usec
= 0L;
332 setitimer(ITIMER_REAL
, &period
, &remains
);
337 dt
= ((remains
.it_value
.tv_sec
* MILLION
) + remains
.it_value
.tv_usec
) *
339 olddelta
->tv_sec
= dt
/ MILLION
;
340 olddelta
->tv_usec
= dt
- (olddelta
->tv_sec
* MILLION
);
343 oldrate
= (double)rate
/ (double)MILLION
;
345 } /* AdjustClockRate */
347 static struct nlist nl
[] = {
363 * The return value is the clock rate in old_tick units or -1 if error.
370 if (lseek(kmem
, (off_t
)nl
[0].n_value
, 0) == -1L)
373 mask
= sigblock(sigmask(SIGALRM
));
375 if (read(kmem
, (caddr_t
)&rate
, sizeof(rate
)) != sizeof(rate
))
383 * The argument is the new rate in old_tick units.
392 if (lseek(kmem
, (off_t
)nl
[0].n_value
, 0) == -1L)
395 mask
= sigblock(sigmask(SIGALRM
));
397 if (write(kmem
, (caddr_t
)&rate
, sizeof(rate
)) != sizeof(rate
)) {
404 if (rate
!= default_rate
) {
406 printf("adjtimed: clock rate (%lu) %ldus/s\n", rate
,
407 (rate
- default_rate
) * tick_rate
);
410 msyslog(LOG_INFO
, "clock rate (%lu) %ldus/s", rate
,
411 (rate
- default_rate
) * tick_rate
);
421 if ((kmem
= open("/dev/kmem", O_RDWR
)) == -1) {
422 msyslog(LOG_ERR
, "open(/dev/kmem): %m");
423 perror("adjtimed: open(/dev/kmem)");
429 if (nl
[0].n_type
== 0) {
430 fputs("adjtimed: /hp-ux has no symbol table\n", stderr
);
431 msyslog(LOG_ERR
, "/hp-ux has no symbol table");
435 * Set the default to the system's original value
437 default_rate
= GetClockRate();
438 if (default_rate
== UNKNOWN_RATE
) default_rate
= DEFAULT_RATE
;
439 tick_rate
= (MILLION
/ default_rate
);
440 slew_rate
= TICK_ADJ
* tick_rate
;
441 fprintf(stderr
,"default_rate=%ld, tick_rate=%ld, slew_rate=%ld\n",default_rate
,tick_rate
,slew_rate
);
444 } /* InitClockRate */
447 * Reset the clock rate to the default value.
454 it
.it_value
.tv_sec
= it
.it_value
.tv_usec
= 0L;
455 setitimer(ITIMER_REAL
, &it
, (struct itimerval
*)0);
457 if (verbose
> 2) puts("adjtimed: resetting the clock");
458 if (sysdebug
> 2) msyslog(LOG_INFO
, "resetting the clock");
460 if (GetClockRate() != default_rate
) {
461 if (SetClockRate(default_rate
) == -1) {
462 msyslog(LOG_ERR
, "set clock rate: %m");
463 perror("adjtimed: set clock rate");
468 } /* ResetClockRate */
475 if (msgctl(mqid
, IPC_RMID
, (struct msqid_ds
*)0) == -1) {
476 if (errno
!= EINVAL
) {
477 msyslog(LOG_ERR
, "remove message queue: %m");
478 perror("adjtimed: remove message queue");
489 msyslog(LOG_ERR
, "terminated");
491 if (kmem
!= -1) close(kmem
);