aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/xntpd/xntpd/ntp_unixclock.c
blob: bc771b6b717f2f603abfea17c94903a9b3daa255 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
 * ntp_unixclock.c - routines for reading and adjusting a 4BSD-style
 *		     system clock
 */

#include <nlist.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>

#if defined(SYS_HPUX) || defined(sgi) || defined(SYS_BSDI) || defined(SYS_44BSD)
#include <sys/param.h>
#include <utmp.h>
#endif

#if defined(HAVE_GETBOOTFILE)
#include <paths.h>
#endif

#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_unixtime.h"
#include "ntp_stdlib.h"

#if defined(HAVE_LIBKVM)
#if defined(SYS_BSDI) || defined(SYS_44BSD)
#include <sys/proc.h>
#endif	/* SYS_BSDI */
#include <kvm.h>
#include <limits.h>

#ifndef _POSIX2_LINE_MAX
#define _POSIX2_LINE_MAX 2048
#endif
#endif	/* HAVE_LIBKVM */


#ifdef RS6000
#undef hz
#endif /* RS6000 */

extern int debug;
/*
 * These routines (init_systime, get_systime, step_systime, adj_systime)
 * implement an interface between the (more or less) system independent
 * bits of NTP and the peculiarities of dealing with the Unix system
 * clock.  These routines will run with good precision fairly independently
 * of your kernel's value of tickadj.  I couldn't tell the difference
 * between tickadj==40 and tickadj==5 on a microvax, though I prefer
 * to set tickadj == 500/hz when in doubt.  At your option you
 * may compile this so that your system's clock is always slewed to the
 * correct time even for large corrections.  Of course, all of this takes
 * a lot of code which wouldn't be needed with a reasonable tickadj and
 * a willingness to let the clock be stepped occasionally.  Oh well.
 */

/*
 * Clock variables.  We round calls to adjtime() to adj_precision
 * microseconds, and limit the adjustment to tvu_maxslew microseconds
 * (tsf_maxslew fractional sec) in one adjustment interval.  As we are
 * thus limited in the speed and precision with which we can adjust the
 * clock, we compensate by keeping the known "error" in the system time
 * in sys_clock_offset.  This is added to timestamps returned by get_systime().
 * We also remember the clock precision we computed from the kernel in
 * case someone asks us.
 */
extern	long adj_precision;	/* adj precision in usec (tickadj) */
extern	long tvu_maxslew;	/* maximum adjust doable in 1<<CLOCK_ADJ sec (usec) */

extern	u_long tsf_maxslew;	/* same as above, as long format */

extern	l_fp sys_clock_offset;	/* correction for current system time */

/*
 * Import sys_clock (it is updated in get_systime)
 */
extern long sys_clock;

static	void	clock_parms	P((u_long *, u_long *));

/*
 * init_systime - initialize the system clock support code, return
 *		  clock precision.
 *
 * Note that this code obtains to kernel variables related to the local
 * clock, tickadj and tick.  The code knows how the Berkeley adjtime
 * call works, and assumes these two variables are obtainable and are
 * used in the same manner.  Tick is supposed to be the number of
 * microseconds which are added to the system clock at clock interrupt
 * time when the time isn't being slewed.  Tickadj is supposed to be
 * the number of microseconds which are added or subtracted from tick when
 * the time is being slewed.
 *
 * If either of these two variables is missing, or is there but is used
 * for a purpose different than that described, you are SOL and may have
 * to do some custom kludging.
 *
 * This really shouldn't be in here.
 */
void
init_systime()
{
	u_long tickadj;
	u_long tick;
	u_long hz;

	/*
	 * Obtain the values
	 */
	clock_parms(&tickadj, &tick);
#ifdef	DEBUG
	if (debug)
		printf("kernel vars: tickadj = %ld, tick = %ld\n", tickadj, tick);
#endif

	/*
	 * If tickadj or hz wasn't found, we're doomed.  If hz is
	 * unreasonably small, forget it.
	 */
	if (tickadj == 0 || tick == 0) {
		syslog(LOG_ERR, "tickadj or tick unknown, exiting");
		exit(3);
	}
	if (tick > 65535) {
		syslog(LOG_ERR, "tick value of %lu is unreasonably large",
		    tick);
		exit(3);
	}

	/*
	 * Estimate hz from tick
	 */
	hz = 1000000L / tick;

	/*
	 * Set adj_precision and the maximum slew based on this.  Note
	 * that maxslew is set slightly shorter than it needs to be as
	 * insurance that all slews requested will complete in 1<<CLOCK_ADJ
	 * seconds.
	 */
#ifdef ADJTIME_IS_ACCURATE
	adj_precision = 1;
#else
	adj_precision = tickadj;
#endif /* ADJTIME_IS_ACCURATE */
#if defined(SLEWALWAYS) && !defined(ADJTIME_IS_ACCURATE)
	/*
	 * give us more time if we are always slewing... just in case
	 */
	tvu_maxslew = tickadj * (hz-3) * (1<<CLOCK_ADJ);
#else
	tvu_maxslew = tickadj * (hz-1) * (1<<CLOCK_ADJ);
#endif /* SLEWALWAYS */
	if (tvu_maxslew > 999990) {
		/*
		 * Don't let the maximum slew exceed 1 second in 4.  This
		 * simplifies calculations a lot since we can then deal
		 * with less-than-one-second fractions.
		 */
		tvu_maxslew = (999990/adj_precision) * adj_precision;
	}
	TVUTOTSF(tvu_maxslew, tsf_maxslew);
	syslog(LOG_NOTICE, "tickadj = %d, tick = %d, tvu_maxslew = %d",
	    tickadj, tick, tvu_maxslew);
#ifdef DEBUG
	if (debug)
		printf(
	"adj_precision = %ld, tvu_maxslew = %ld, tsf_maxslew = 0.%08lx\n",
		    adj_precision, tvu_maxslew, tsf_maxslew);
#endif

	/*
	 * Set the current offset to 0
	 */
	L_CLR(&sys_clock_offset);
}

#ifdef HAVE_LIBKVM
/*
 * clock_parms - return the local clock tickadj and tick parameters
 *
 * This version uses the SunOS libkvm (or the bsd compatability version).
 */
static void
clock_parms(tickadj, tick)
	u_long *tickadj;
	u_long *tick;
{
	static struct nlist nl[] = {
#define N_TICKADJ 0
		{ "_tickadj" },
#define N_TICK 1
		{ "_tick" },
		{ "" },
	};
#if	__convex__ /* { */
	if (K_open((char *)0,O_RDONLY,"/vmunix")!=0) {
		syslog(LOG_ERR, "K_open failed");
		exit(3);
	}
	kusenlist(1);
	if (knlist(nl)!=0
	||  nl[N_TICKADJ].n_value==0
	||  nl[N_TICK].n_value==0) {
		syslog(LOG_ERR, "knlist failed");
		exit(3);
	}
	if (K_read(tickadj,sizeof(*tickadj),nl[N_TICKADJ].n_value) !=
	    sizeof(*tickadj)) {
		syslog(LOG_ERR, "K_read tickadj failed");
		exit(3);
	}
	if (K_read(tick,sizeof(*tick),nl[N_TICK].n_value) !=
	    sizeof(*tick)) {
		syslog(LOG_ERR, "K_read tick failed");
		exit(3);
	}
	(void)K_close();
#else	/* }__convex__{ */
	register kvm_t *kd;
	if ((kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL)) == NULL) {
		syslog(LOG_ERR, "kvm_open failed");
		exit(3);
	}
	if (kvm_nlist(kd, nl) != 0) {
		syslog(LOG_ERR, "kvm_nlist failed");
		exit(3);
	}
	if (kvm_read(kd, nl[N_TICKADJ].n_value, (char *)tickadj, sizeof(*tickadj)) !=
	    sizeof(*tickadj)) {
		syslog(LOG_ERR, "kvm_read tickadj failed");
		exit(3);
	}
	if (kvm_read(kd, nl[N_TICK].n_value, (char *)tick, sizeof(*tick)) !=
	    sizeof(*tick)) {
		syslog(LOG_ERR, "kvm_read tick failed");
		exit(3);
	}
	if (kvm_close(kd) < 0) {
		syslog(LOG_ERR, "kvm_close failed");
		exit(3);
	}
#endif	/*}convex*/
#undef N_TICKADJ
#undef N_TICK
}
#endif /* HAVE_LIBKVM */


#ifdef HAVE_READKMEM
/*
 * clock_parms - return the local clock tickadj and tick parameters
 *
 * Note that this version grovels about in /dev/kmem to determine
 * these values.  This probably should be elsewhere.
 */
#if defined(SYS_UNIXWARE1)
/*
 * clock_parms - return the local clock tickadj and tick parameters
 *
 * The values set here were determined experimentally on a 486 system
 * I'm not confident in them. - RAS
 *
 */
static void
clock_parms(tickadj, tick)
	u_long *tickadj;
	u_long *tick;
{
	*tick = 10000;		/* microseconds */
	*tickadj = 80;		/* microseconds */
}
#else /* SYS_UNIXWARE1 */

#if defined(SYS_AUX3) || defined(SYS_AUX2) || defined(SYS_SVR4) || defined(SYS_PTX)
#define K_TICKADJ_NAME	"tickadj"
#define K_TICK_NAME		"tick"
#endif

#ifdef SYS_HPUX
#if defined(hp9000s300)
#define K_TICKADJ_NAME	"_tickadj"
#define K_TICK_NAME		"_old_tick"
#else
#define K_TICKADJ_NAME	"tickadj"
#define K_TICK_NAME		"old_tick"
#endif
#endif

/* The defaults if not defined previously */
#if !defined(K_TICKADJ_NAME)
#define K_TICKADJ_NAME	"_tickadj"
#endif
#if !defined(K_TICK_NAME)
#define K_TICK_NAME		"_tick"
#endif

static void
clock_parms(tickadj, tick)
	u_long *tickadj;
	u_long *tick;
{
	register int i;
	int kmem;
#if defined(HAVE_N_UN)
#define N_NAME n_un.n_name
	static struct nlist nl[] =
	{	{{K_TICKADJ_NAME}},
		{{K_TICK_NAME}},
		{{""}},
	};
#else
#define N_NAME n_name
	static struct nlist nl[] =
	{	{K_TICKADJ_NAME},
		{K_TICK_NAME},
		{""},
	};
#endif
#ifdef HAVE_GETBOOTFILE
	const char *kernelname;
#else
	static char *kernelnames[] = {
		"/kernel",
		"/vmunix",
		"/unix",
		"/mach",
		"/hp-ux",
		"/386bsd",
		"/netbsd",
#ifdef	KERNELFILE
		KERNELFILE,
#endif
		NULL
	};
#endif
	struct stat stbuf;
	int vars[2];

#define	K_TICKADJ	0
#define	K_TICK		1

	/*
	 * Check to see what to use for the object file for names and get
	 * the locations of the necessary kernel variables.
	 */
#ifdef	HAVE_GETBOOTFILE
	kernelname = getbootfile();
	if (kernelname &&
            ((stat(kernelname, &stbuf) == -1) || (nlist(kernelname, nl) < 0))) {
#else
	for (i = 0; kernelnames[i] != NULL; i++) {
		if (stat(kernelnames[i], &stbuf) == -1)
			continue;
		if (nlist(kernelnames[i], nl) >= 0)
			break;
	}
	if (kernelnames[i] == NULL) {
#endif
		syslog(LOG_ERR,
		  "Clock init couldn't find kernel object file");
		exit(3);
	}

	/*
	 * Read clock parameters from kernel
	 */
	kmem = open("/dev/kmem", O_RDONLY);
	if (kmem < 0) {
		syslog(LOG_ERR, "Can't open /dev/kmem for reading: %m");
#ifdef	DEBUG
		if (debug)
			perror("/dev/kmem");
#endif
		exit(3);
	}

	for (i = 0; i < (sizeof(vars)/sizeof(vars[0])); i++) {
		off_t where;

		vars[i] = 0;
		if ((where = nl[i].n_value) == 0) {
			syslog(LOG_ERR, "Unknown kernal var %s",
			       nl[i].N_NAME);
			continue;
		}
		if (lseek(kmem, where, SEEK_SET) == -1) {
			syslog(LOG_ERR, "lseek for %s fails: %m",
			       nl[i].N_NAME);
			continue;
		}
		if (read(kmem, &vars[i], sizeof(int)) != sizeof(int)) {
			syslog(LOG_ERR, "read for %s fails: %m",
			       nl[i].N_NAME);
		}
	}
	close(kmem);

	*tickadj = (u_long)vars[K_TICKADJ];
	*tick = (u_long)vars[K_TICK];

#undef	K_TICKADJ
#undef	K_TICK
#undef	K_TICKADJ_NAME
#undef	K_TICK_NAME
#undef	N_NAME
}
#endif /* SYS_UNIXWARE1 */
#endif /* HAVE_READKMEM */

#if defined(SOLARIS)&&defined(ADJTIME_IS_ACCURATE)
/*
 * clock_parms for Solaris 2.2 and later, with high-res timer kernel code.
 * The clock code changed in Solaris 2.2, and tickadj went away.
 * The good news is that ADJTIME_IS_ACCURATE and tick is available through
 * sysconf().
 */
static void
clock_parms(tickadj, tick)
        u_long *tickadj;
        u_long *tick;
{
        int hz;

        hz = (int) sysconf (_SC_CLK_TCK);
        *tick = 1000000L/hz;
        *tickadj = (*tick/16); /* There is no tickadj, and it is only set here
				for tvu_maxslew calculation above. Really,
				clock_parms should return adj_precision
				and tvu_maxslew, instead of the very
				BSD-centric tickadj */

#ifdef DEBUG
        if (debug) printf ("Solaris tick = %d\n", *tick);
#endif
}
#endif /* SOLARIS_HRTIME */


#if defined(sgi)
/*
 * clock_parms - return the local clock tickadj and tick parameters
 *
 * The values set here were determined experimentally on a 4D/220 and
 * an R4000-50 server under IRIX 4.0.5.
 */
static void
clock_parms(tickadj, tick)
	u_long *tickadj;
	u_long *tick;
{
	*tick = 10000;
	*tickadj = 150;
}
#endif /* sgi */

#ifdef NOKMEM

#ifndef HZ
#define HZ	60
#endif

/*
 * clock_parms - return the local clock tickadj and tick parameters
 *
 * Note that this version uses static values!
 */
static void
clock_parms(tickadj, tick)
	u_long *tickadj;
	u_long *tick;
{
#ifdef RS6000
	*tickadj = 1000;
#else  /*RS6000*/
#if  SYS_DOMAINOS
	*tickadj = 668;
#else  /*SYS_DOMAINOS*/
	*tickadj = 500 / HZ;
#endif /*SYS_DOMAINOS*/
#endif /*RS6000*/
	*tick = 1000000L / HZ;

#ifdef	DEBUG
	if (debug)
		printf("NOTE: Using preset values for tick and tickadj !!\n");
#endif
}
#endif /*NOKMEM*/

#if ((defined(SOLARIS)&&!defined(ADJTIME_IS_ACCURATE))|| (defined(RS6000)&&!defined(NOKMEM))||defined(SYS_SINIXM) )
#ifndef _SC_CLK_TCK
#include <unistd.h>
#endif
/*
 * clock_parms - return the local clock tickadj and tick parameters
 *
 * Note that this version grovels about in /dev/kmem to determine
 * these values.  This probably should be elsewhere.
 */
static void
clock_parms(tickadj, tick)
	u_long *tickadj;
	u_long *tick;
{
	register int i;
	int kmem;
#define N_NAME n_name
	static struct nlist nl[] =
	{	{"tickadj"},
		{""},
	};
	static char *kernelnames[] = {
		"/kernel/unix",
		"/unix",
		NULL
	};
	struct stat stbuf;
	int vars[1];

#define	K_TICKADJ	0
	/*
	 * Read clock parameters from kernel
	 */
	kmem = open("/dev/kmem", O_RDONLY);
	if (kmem < 0) {
		syslog(LOG_ERR, "Can't open /dev/kmem for reading: %m");
#ifdef	DEBUG
		if (debug)
			perror("/dev/kmem");
#endif
		exit(3);
	}

	for (i = 0; kernelnames[i] != NULL; i++) {
		if (stat(kernelnames[i], &stbuf) == -1)
			continue;
		if (nlist(kernelnames[i], nl) >= 0)
			break;
	}
	if (kernelnames[i] == NULL) {
		syslog(LOG_ERR,
		  "Clock init couldn't find kernel as either /vmunix or /unix");
		exit(3);
	}

	for (i = 0; i < (sizeof(vars)/sizeof(vars[0])); i++) {
		off_t where;

		vars[i] = 0;
		if ((where = nl[i].n_value) == 0) {
			syslog(LOG_ERR, "Unknown kernal var %s",
			       nl[i].N_NAME);
			continue;
		}
		if (lseek(kmem, where, SEEK_SET) == -1) {
			syslog(LOG_ERR, "lseek for %s fails: %m",
			       nl[i].N_NAME);
			continue;
		}
		if (read(kmem, &vars[i], sizeof(int)) != sizeof(int)) {
			syslog(LOG_ERR, "read for %s fails: %m",
			       nl[i].N_NAME);
		}
#if defined(RS6000)
                /*
                 * Aix requires one more round of indirection.
                 */
                if (lseek(kmem, vars[i], SEEK_SET) == -1) {
                        syslog(LOG_ERR, "lseek for %s fails: %m",
                               nl[i].N_NAME);
                        continue;
                }
                if (read(kmem, &vars[i], sizeof(int)) != sizeof(int)) {
                        syslog(LOG_ERR, "read for %s fails: %m",
                               nl[i].N_NAME);
                }
#endif
	}
	close(kmem);

	*tickadj = (u_long)vars[K_TICKADJ];
	*tick = (u_long)(1000000/sysconf(_SC_CLK_TCK));

#undef	K_TICKADJ
#undef	N_NAME
}
#endif /* SOLARIS */

#ifdef SYS_LINUX
#include "sys/timex.h"
static void
clock_parms(tickadj, tick)
	u_long *tickadj;
	u_long *tick;
{
  struct timex txc;

  txc.mode = 0;
  __adjtimex(&txc);

  *tickadj = (u_long)1;		/* our adjtime is accurate */
  *tick    = (u_long)txc.tick;
}
#endif /* SYS_LINUX */