summaryrefslogtreecommitdiff
path: root/util/tickadj.c
blob: d54e9e6844ab0ee4b7204bd48cc7cd0d6f6cd3a4 (plain)
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/*
 * tickadj - read, and possibly modify, the kernel `tick' and
 *	     `tickadj' variables, as well as `dosynctodr'.  Note that
 *	     this operates on the running kernel only.  I'd like to be
 *	     able to read and write the binary as well, but haven't
 *	     mastered this yet.
 *
 * HMS: The #includes here are different from those in xntpd/ntp_unixclock.c
 *      These seem "worse".
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "ntp_types.h"
#include "l_stdlib.h"

#include <stdio.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif /* HAVE_UNISTD_H */

#ifdef HAVE_SYS_TIMEX_H
# include <sys/timex.h>
#endif

#ifdef HAVE_ADJTIMEX	/* Linux */

struct timex txc;

#if 0
int
main(
	int argc,
	char *argv[]
	)
{
	int     c, i;
	int     quiet = 0;
	int     errflg = 0;
	char    *progname;
	extern int ntp_optind;
	extern char *ntp_optarg;

	progname = argv[0];
	if (argc==2 && argv[1][0] != '-') { /* old Linux format, for compatability */
	    if ((i = atoi(argv[1])) > 0) {
		    txc.time_tick = i;
		    txc.modes = ADJ_TIMETICK;
	    } else {
		    fprintf(stderr, "Silly value for tick: %s\n", argv[1]);
		    errflg++;
	    }
	} else {
	    while ((c = ntp_getopt(argc, argv, "a:qt:")) != EOF) {
		switch (c) {
		    case 'a':
			if ((i=atoi(ntp_optarg)) > 0) {
				txc.tickadj = i;
				txc.modes |= ADJ_TICKADJ;
			} else {
				fprintf(stderr,
					"%s: unlikely value for tickadj: %s\n",
					progname, ntp_optarg);
				errflg++;
			}
			break;

		    case 'q':
			quiet = 1;
			break;

		    case 't':
			if ((i=atoi(ntp_optarg)) > 0) {
				txc.time_tick = i;
				txc.modes |= ADJ_TIMETICK;
			} else {
				(void) fprintf(stderr,
				       "%s: unlikely value for tick: %s\n",
				       progname, ntp_optarg);
				errflg++;
			}
			break;

		    default:
			fprintf(stderr,
			    "Usage: %s [tick_value]\n-or-   %s [ -q ] [ -t tick ] [ -a tickadj ]\n",
			    progname, progname);
			errflg++;
			break;
		}
	    }
	}

	if (!errflg) {
		if (adjtimex(&txc) < 0)
			perror("adjtimex");
		else if (!quiet)
			printf("tick     = %ld\ntick_adj = %d\n",
			    txc.time_tick, txc.tickadj);
	}

	exit(errflg ? 1 : 0);
}
#else
int
main(
	int argc,
	char *argv[]
	)
{
	if (argc > 2)
	{
		fprintf(stderr, "Usage: %s [tick_value]\n", argv[0]);
		exit(-1);
	}
	else if (argc == 2)
	{
#ifdef ADJ_TIMETICK
		if ( (txc.time_tick = atoi(argv[1])) < 1 )
#else
		if ( (txc.tick = atoi(argv[1])) < 1 )
#endif
		{
			fprintf(stderr, "Silly value for tick: %s\n", argv[1]);
			exit(-1);
		}
#ifdef ADJ_TIMETICK
		txc.modes = ADJ_TIMETICK;
#else
#ifdef MOD_OFFSET
		txc.modes = ADJ_TICK;
#else
		txc.mode = ADJ_TICK;
#endif
#endif
	}
	else
	{
#ifdef ADJ_TIMETICK
		txc.modes = 0;
#else
#ifdef MOD_OFFSET
		txc.modes = 0;
#else
		txc.mode = 0;
#endif
#endif
	}

	if (adjtimex(&txc) < 0)
	{
		perror("adjtimex");
	}
	else
	{
#ifdef ADJ_TIMETICK
		printf("tick     = %ld\ntick_adj = %ld\n", txc.time_tick, txc.tickadj);
#else
		printf("tick = %ld\n", txc.tick);
#endif
	}

	exit(0);
}
#endif

#else	/* not Linux... kmem tweaking: */

#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
#endif
#include <sys/stat.h>

#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif

#ifdef NLIST_STRUCT
# include <nlist.h>
#else /* not NLIST_STRUCT */ /* was defined(SYS_AUX3) || defined(SYS_AUX2) */
# include <sys/resource.h>
# include <sys/file.h>
# include <a.out.h>
# ifdef HAVE_SYS_VAR_H
#  include <sys/var.h>
# endif
#endif

#include "ntp_stdlib.h"
#include "ntp_io.h"

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

#ifdef HAVE_KVM_OPEN
# include <kvm.h>
#endif

#ifdef SYS_VXWORKS
/* vxWorks needs mode flag -casey*/
#define open(name, flags)   open(name, flags, 0777)
#endif

#ifndef L_SET	/* Was: defined(SYS_PTX) || defined(SYS_IX86OSF1) */
# define L_SET SEEK_SET
#endif

#ifndef HZ
# define HZ	DEFAULT_HZ
#endif

#define	KMEM	"/dev/kmem"
#define	STREQ(a, b)	(*(a) == *(b) && strcmp((a), (b)) == 0)

char *progname;

int dokmem = 1;
int writetickadj = 0;
int writeopttickadj = 0;
int unsetdosync = 0;
int writetick = 0;
int quiet = 0;
int setnoprintf = 0;

const char *kmem = KMEM;
const char *file = NULL;
int   fd  = -1;

static	void	getoffsets	(off_t *, off_t *, off_t *, off_t *);
static	int	openfile	(const char *, int);
static	void	writevar	(int, off_t, int);
static	void	readvar		(int, off_t, int *);

/*
 * main - parse arguments and handle options
 */
int
main(
	int argc,
	char *argv[]
	)
{
	int c;
	int errflg = 0;
	off_t tickadj_offset;
	off_t tick_offset;
	off_t dosync_offset;
	off_t noprintf_offset;
	int tickadj, ktickadj;	/* HMS: Why isn't this u_long? */
	int tick, ktick;	/* HMS: Why isn't this u_long? */
	int dosynctodr;
	int noprintf;
	int hz;
	int hz_int, hz_hundredths;
	int recommend_tickadj;
	long tmp;

	init_lib();

	progname = argv[0];
	while ((c = ntp_getopt(argc, argv, "a:Adkpqst:")) != EOF)
	{
		switch (c)
		{
		    case 'a':
			writetickadj = atoi(ntp_optarg);
			if (writetickadj <= 0)
			{
				(void) fprintf(stderr,
					       "%s: unlikely value for tickadj: %s\n",
					       progname, ntp_optarg);
				errflg++;
			}

#if defined SCO5_CLOCK
			if (writetickadj % HZ) 
			{
				writetickadj = (writetickadj / HZ) * HZ;
				(void) fprintf(stderr,
					       "tickadj truncated to: %d\n", writetickadj);
			}
#endif /* SCO5_CLOCK */

			break;
		    case 'A':
			writeopttickadj = 1;
			break;
		    case 'd':
			++debug;
			break;
		    case 'k':
			dokmem = 1;
			break;
		    case 'p':
			setnoprintf = 1;
			break;
		    case 'q':
			quiet = 1;
			break;
		    case 's':
			unsetdosync = 1;
			break;
		    case 't':
			writetick = atoi(ntp_optarg);
			if (writetick <= 0)
			{
				(void) fprintf(stderr,
					       "%s: unlikely value for tick: %s\n",
					       progname, ntp_optarg);
				errflg++;
			}
			break;
		    default:
			errflg++;
			break;
		}
	}
	if (errflg || ntp_optind != argc)
	{
		(void) fprintf(stderr,
			       "usage: %s [-Adkpqs] [-a newadj] [-t newtick]\n", progname);
		exit(2);
	}

	getoffsets(&tick_offset, &tickadj_offset, &dosync_offset, &noprintf_offset);

	if (debug)
	{
		(void) printf("tick offset = %lu\n", (unsigned long)tick_offset);
		(void) printf("tickadj offset = %lu\n", (unsigned long)tickadj_offset);
		(void) printf("dosynctodr offset = %lu\n", (unsigned long)dosync_offset);
		(void) printf("noprintf offset = %lu\n", (unsigned long)noprintf_offset);
	}

	if (writetick && (tick_offset == 0))
	{
		(void) fprintf(stderr, 
			       "No tick kernel variable\n");
		errflg++;
	}
	
	if (writeopttickadj && (tickadj_offset == 0))
	{
		(void) fprintf(stderr, 
			       "No tickadj kernel variable\n");
		errflg++;
	}

	if (unsetdosync && (dosync_offset == 0))
	{
		(void) fprintf(stderr, 
			       "No dosynctodr kernel variable\n");
		errflg++;
	}
	
	if (setnoprintf && (noprintf_offset == 0))
	{
		(void) fprintf(stderr, 
			       "No noprintf kernel variable\n");
		errflg++;
	}

	if (tick_offset != 0)
	{
		readvar(fd, tick_offset, &tick);
#if defined(TICK_NANO) && defined(K_TICK_NAME)
		if (!quiet)
		    (void) printf("KERNEL %s = %d nsec\n", K_TICK_NAME, tick);
#endif /* TICK_NANO && K_TICK_NAME */

#ifdef TICK_NANO
		tick /= 1000;
#endif
	}
	else
	{
		tick = 0;
	}

	if (tickadj_offset != 0)
	{
		readvar(fd, tickadj_offset, &tickadj);

#ifdef SCO5_CLOCK
		/* scale from nsec/sec to usec/tick */
		tickadj /= (1000L * HZ);
#endif /*SCO5_CLOCK */

#if defined(TICKADJ_NANO) && defined(K_TICKADJ_NAME)
		if (!quiet)
		    (void) printf("KERNEL %s = %d nsec\n", K_TICKADJ_NAME, tickadj);
#endif /* TICKADJ_NANO && K_TICKADJ_NAME */

#ifdef TICKADJ_NANO
		tickadj += 999;
		tickadj /= 1000;
#endif
	}
	else
	{
		tickadj = 0;
	}

	if (dosync_offset != 0)
	{
		readvar(fd, dosync_offset, &dosynctodr);
	}

	if (noprintf_offset != 0)
	{
		readvar(fd, noprintf_offset, &noprintf);
	}

	(void) close(fd);

	if (unsetdosync && dosync_offset == 0)
	{
		(void) fprintf(stderr,
			       "%s: can't find %s in namelist\n",
			       progname,
#ifdef K_DOSYNCTODR_NAME
			       K_DOSYNCTODR_NAME
#else /* not K_DOSYNCTODR_NAME */
			       "dosynctodr"
#endif /* not K_DOSYNCTODR_NAME */
			       );
		exit(1);
	}

	hz = HZ;
#if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
	hz = (int) sysconf (_SC_CLK_TCK);
#endif /* not HAVE_SYSCONF && _SC_CLK_TCK */
#ifdef OVERRIDE_HZ
	hz = DEFAULT_HZ;
#endif
	ktick = tick;
#ifdef PRESET_TICK
	tick = PRESET_TICK;
#endif /* PRESET_TICK */
#ifdef TICKADJ_NANO
	tickadj /= 1000;
	if (tickadj == 0)
	    tickadj = 1;
#endif
	ktickadj = tickadj;
#ifdef PRESET_TICKADJ
	tickadj = (PRESET_TICKADJ) ? PRESET_TICKADJ : 1;
#endif /* PRESET_TICKADJ */

	if (!quiet)
	{
		if (tick_offset != 0)
		{
			(void) printf("KERNEL tick = %d usec (from %s kernel variable)\n",
				      ktick,
#ifdef K_TICK_NAME
				      K_TICK_NAME
#else
				      "<this can't happen>"
#endif			
				      );
		}
#ifdef PRESET_TICK
		(void) printf("PRESET tick = %d usec\n", tick);
#endif /* PRESET_TICK */
		if (tickadj_offset != 0)
		{
			(void) printf("KERNEL tickadj = %d usec (from %s kernel variable)\n",
				      ktickadj,
#ifdef K_TICKADJ_NAME
				      K_TICKADJ_NAME
#else
				      "<this can't happen>"
#endif
				      );
		}
#ifdef PRESET_TICKADJ
		(void) printf("PRESET tickadj = %d usec\n", tickadj);
#endif /* PRESET_TICKADJ */
		if (dosync_offset != 0)
		{
			(void) printf("dosynctodr is %s\n", dosynctodr ? "on" : "off");
		}
		if (noprintf_offset != 0)
		{
			(void) printf("kernel level printf's: %s\n",
				      noprintf ? "off" : "on");
		}
	}

	if (tick <= 0)
	{
		(void) fprintf(stderr, "%s: the value of tick is silly!\n",
			       progname);
		exit(1);
	}

	hz_int = (int)(1000000L / (long)tick);
	hz_hundredths = (int)((100000000L / (long)tick) - ((long)hz_int * 100L));
	if (!quiet)
	{
		(void) printf("KERNEL hz = %d\n", hz);
		(void) printf("calculated hz = %d.%02d Hz\n", hz_int,
			      hz_hundredths);
	}

#if defined SCO5_CLOCK
	recommend_tickadj = 100;
#else /* SCO5_CLOCK */
	tmp = (long) tick * 500L;
	recommend_tickadj = (int)(tmp / 1000000L);
	if (tmp % 1000000L > 0)
	{
		recommend_tickadj++;
	}

#ifdef MIN_REC_TICKADJ
	if (recommend_tickadj < MIN_REC_TICKADJ)
	{
		recommend_tickadj = MIN_REC_TICKADJ;
	}
#endif /* MIN_REC_TICKADJ */
#endif /* SCO5_CLOCK */
  

	if ((!quiet) && (tickadj_offset != 0))
	{
		(void) printf("recommended value of tickadj = %d us\n",
			      recommend_tickadj);
	}

	if (   writetickadj == 0
	       && !writeopttickadj
	       && !unsetdosync
	       && writetick == 0
	       && !setnoprintf)
	{
		exit(errflg ? 1 : 0);
	}

	if (writetickadj == 0 && writeopttickadj)
	{
		writetickadj = recommend_tickadj;
	}

	fd = openfile(file, O_WRONLY);

	if (setnoprintf && (noprintf_offset != 0))
	{
		if (!quiet)
		{
			(void) fprintf(stderr, "setting noprintf: ");
			(void) fflush(stderr);
		}
		writevar(fd, noprintf_offset, 1);
		if (!quiet)
		{
			(void) fprintf(stderr, "done!\n");
		}
	}

	if ((writetick > 0) && (tick_offset != 0))
	{
		if (!quiet)
		{
			(void) fprintf(stderr, "writing tick, value %d: ",
				       writetick);
			(void) fflush(stderr);
		}
		writevar(fd, tick_offset, writetick);
		if (!quiet)
		{
			(void) fprintf(stderr, "done!\n");
		}
	}

	if ((writetickadj > 0) && (tickadj_offset != 0))
	{
		if (!quiet)
		{
			(void) fprintf(stderr, "writing tickadj, value %d: ",
				       writetickadj);
			(void) fflush(stderr);
		}

#ifdef SCO5_CLOCK
		/* scale from usec/tick to nsec/sec */
		writetickadj *= (1000L * HZ);
#endif /* SCO5_CLOCK */

		writevar(fd, tickadj_offset, writetickadj);
		if (!quiet)
		{
			(void) fprintf(stderr, "done!\n");
		}
	}

	if (unsetdosync && (dosync_offset != 0))
	{
		if (!quiet)
		{
			(void) fprintf(stderr, "zeroing dosynctodr: ");
			(void) fflush(stderr);
		}
		writevar(fd, dosync_offset, 0);
		if (!quiet)
		{
			(void) fprintf(stderr, "done!\n");
		}
	}
	(void) close(fd);
	return(errflg ? 1 : 0);
}

/*
 * getoffsets - read the magic offsets from the specified file
 */
static void
getoffsets(
	off_t *tick_off,
	off_t *tickadj_off,
	off_t *dosync_off,
	off_t *noprintf_off
	)
{

#ifndef NOKMEM
# ifndef HAVE_KVM_OPEN
	const char **kname;
# endif
#endif

#ifndef NOKMEM
# ifdef NLIST_NAME_UNION
#  define NL_B {{
#  define NL_E }}
# else
#  define NL_B {
#  define NL_E }
# endif
#endif

#define K_FILLER_NAME "DavidLetterman"

#ifdef NLIST_EXTRA_INDIRECTION
	int i;
#endif

#ifndef NOKMEM
	static struct nlist nl[] =
	{
		NL_B
#ifdef K_TICKADJ_NAME
#define N_TICKADJ	0
		K_TICKADJ_NAME
#else
		K_FILLER_NAME
#endif
		NL_E,
		NL_B
#ifdef K_TICK_NAME
#define N_TICK		1
		K_TICK_NAME
#else
		K_FILLER_NAME
#endif
		NL_E,
		NL_B
#ifdef K_DOSYNCTODR_NAME
#define N_DOSYNC	2
		K_DOSYNCTODR_NAME
#else
		K_FILLER_NAME
#endif
		NL_E,
		NL_B
#ifdef K_NOPRINTF_NAME
#define N_NOPRINTF	3
		K_NOPRINTF_NAME
#else
		K_FILLER_NAME
#endif
		NL_E,
		NL_B "" NL_E,
	};

#ifndef HAVE_KVM_OPEN
	static const char *kernels[] =
	{
#ifdef HAVE_GETBOOTFILE
		NULL,			/* *** SEE BELOW! *** */
#endif
		"/kernel/unix",
		"/kernel",
		"/vmunix",
		"/unix",
		"/mach",
		"/hp-ux",
		"/386bsd",
		"/netbsd",
		"/stand/vmunix",
		"/bsd",
		NULL
	};
#endif /* not HAVE_KVM_OPEN */

#ifdef HAVE_KVM_OPEN
	/*
	 * Solaris > 2.5 doesn't have a kernel file.  Use the kvm_* interface
	 * to read the kernel name list. -- stolcke 3/4/96
	 */
	kvm_t *kvm_handle = kvm_open(NULL, NULL, NULL, O_RDONLY, progname);

	if (kvm_handle == NULL)
	{
		(void) fprintf(stderr,
			       "%s: kvm_open failed\n",
			       progname);
		exit(1);
	}
	if (kvm_nlist(kvm_handle, nl) == -1)
	{
		(void) fprintf(stderr,
			       "%s: kvm_nlist failed\n",
			       progname);
		exit(1);
	}
	kvm_close(kvm_handle);
#else /* not HAVE_KVM_OPEN */
#ifdef HAVE_GETBOOTFILE		/* *** SEE HERE! *** */
	if (kernels[0] == NULL)
	{
		char * cp = (char *)getbootfile();

		if (cp)
		{
			kernels[0] = cp;
		}
		else
		{
			kernels[0] = "/Placeholder";
		}
	}
#endif /* HAVE_GETBOOTFILE */
	for (kname = kernels; *kname != NULL; kname++)
	{
		struct stat stbuf;

		if (stat(*kname, &stbuf) == -1)
		{
			continue;
		}
		if (nlist(*kname, nl) >= 0)
		{
			break;
		}
		else
		{
			(void) fprintf(stderr,
				       "%s: nlist didn't find needed symbols from <%s>: %s\n",
				       progname, *kname, strerror(errno));
		}
	}
	if (*kname == NULL)
	{
		(void) fprintf(stderr,
			       "%s: Couldn't find the kernel\n",
			       progname);
		exit(1);
	}
#endif /* HAVE_KVM_OPEN */

	if (dokmem)
	{
		file = kmem;

		fd = openfile(file, O_RDONLY);
#ifdef NLIST_EXTRA_INDIRECTION
		/*
		 * Go one more round of indirection.
		 */
		for (i = 0; i < (sizeof(nl) / sizeof(struct nlist)); i++)
		{
			if ((nl[i].n_value) && (nl[i].n_sclass == 0x6b))
			{
				readvar(fd, nl[i].n_value, &nl[i].n_value);
			}
		}
#endif /* NLIST_EXTRA_INDIRECTION */
	}
#endif /* not NOKMEM */

	*tickadj_off  = 0;
	*tick_off     = 0;
	*dosync_off   = 0;
	*noprintf_off = 0;

#if defined(N_TICKADJ)
	*tickadj_off = nl[N_TICKADJ].n_value;
#endif

#if defined(N_TICK)
	*tick_off = nl[N_TICK].n_value;
#endif

#if defined(N_DOSYNC)
	*dosync_off = nl[N_DOSYNC].n_value;
#endif

#if defined(N_NOPRINTF)
	*noprintf_off = nl[N_NOPRINTF].n_value;
#endif
	return;
}

#undef N_TICKADJ
#undef N_TICK
#undef N_DOSYNC
#undef N_NOPRINTF


/*
 * openfile - open the file, check for errors
 */
static int
openfile(
	const char *name,
	int mode
	)
{
	int ifd;

	ifd = open(name, mode);
	if (ifd < 0)
	{
		(void) fprintf(stderr, "%s: open %s: ", progname, name);
		perror("");
		exit(1);
	}
	return ifd;
}


/*
 * writevar - write a variable into the file
 */
static void
writevar(
	int ofd,
	off_t off,
	int var
	)
{
	
	if (lseek(ofd, off, L_SET) == -1)
	{
		(void) fprintf(stderr, "%s: lseek fails: ", progname);
		perror("");
		exit(1);
	}
	if (write(ofd, (char *)&var, sizeof(int)) != sizeof(int))
	{
		(void) fprintf(stderr, "%s: write fails: ", progname);
		perror("");
		exit(1);
	}
	return;
}


/*
 * readvar - read a variable from the file
 */
static void
readvar(
	int ifd,
	off_t off,
	int *var
	)
{
	int i;
	
	if (lseek(ifd, off, L_SET) == -1)
	{
		(void) fprintf(stderr, "%s: lseek fails: ", progname);
		perror("");
		exit(1);
	}
	i = read(ifd, (char *)var, sizeof(int));
	if (i < 0)
	{
		(void) fprintf(stderr, "%s: read fails: ", progname);
		perror("");
		exit(1);
	}
	if (i != sizeof(int))
	{
		(void) fprintf(stderr, "%s: read expected %d, got %d\n",
			       progname, (int)sizeof(int), i);
		exit(1);
	}
	return;
}
#endif /* not Linux */