2 * tickadj - read, and possibly modify, the kernel `tick' and
3 * `tickadj' variables, as well as `dosynctodr'. Note that
4 * this operates on the running kernel only. I'd like to be
5 * able to read and write the binary as well, but haven't
8 * HMS: The #includes here are different from those in xntpd/ntp_unixclock.c
16 #include "ntp_types.h"
22 #endif /* HAVE_UNISTD_H */
24 #ifdef HAVE___ADJTIMEX /* Linux */
26 #include <sys/timex.h>
40 extern int ntp_optind;
41 extern char *ntp_optarg;
44 if (argc==2 && argv[1][0] != '-') { /* old Linux format, for compatability */
45 if ((i = atoi(argv[1])) > 0) {
47 txc.modes = ADJ_TIMETICK;
49 fprintf(stderr, "Silly value for tick: %s\n", argv[1]);
53 while ((c = ntp_getopt(argc, argv, "a:qt:")) != EOF) {
56 if ((i=atoi(ntp_optarg)) > 0) {
58 txc.modes |= ADJ_TICKADJ;
60 (void) fprintf(stderr,
61 "%s: unlikely value for tickadj: %s\n",
62 progname, ntp_optarg);
72 if ((i=atoi(ntp_optarg)) > 0) {
74 txc.modes |= ADJ_TIMETICK;
76 (void) fprintf(stderr,
77 "%s: unlikely value for tick: %s\n",
78 progname, ntp_optarg);
85 "Usage: %s [tick_value]\n-or- %s [ -q ] [ -t tick ] [ -a tickadj ]\n",
94 if (__adjtimex(&txc) < 0)
97 printf("tick = %ld\ntick_adj = %d\n",
98 txc.time_tick, txc.tickadj);
101 exit(errflg ? 1 : 0);
112 fprintf(stderr, "Usage: %s [tick_value]\n", argv[0]);
118 if ( (txc.time_tick = atoi(argv[1])) < 1 )
120 if ( (txc.tick = atoi(argv[1])) < 1 )
123 fprintf(stderr, "Silly value for tick: %s\n", argv[1]);
127 txc.modes = ADJ_TIMETICK;
130 txc.modes = ADJ_TICK;
149 if (__adjtimex(&txc) < 0)
156 printf("tick = %ld\ntick_adj = %ld\n", txc.time_tick, txc.tickadj);
158 printf("tick = %ld\n", txc.tick);
166 #else /* not Linux... kmem tweaking: */
168 #ifdef HAVE_SYS_FILE_H
169 # include <sys/file.h>
171 #include <sys/stat.h>
173 #ifdef HAVE_SYS_PARAM_H
174 # include <sys/param.h>
179 #else /* not NLIST_STRUCT */ /* was defined(SYS_AUX3) || defined(SYS_AUX2) */
180 # include <sys/resource.h>
181 # include <sys/file.h>
183 # include <sys/var.h>
187 #include "ntp_stdlib.h"
189 #ifdef hz /* Was: RS6000 */
198 /* vxWorks needs mode flag -casey*/
199 #define open(name, flags) open(name, flags, 0777)
202 #ifndef L_SET /* Was: defined(SYS_PTX) || defined(SYS_IX86OSF1) */
203 # define L_SET SEEK_SET
207 # define HZ DEFAULT_HZ
210 #define KMEM "/dev/kmem"
211 #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
217 int writetickadj = 0;
218 int writeopttickadj = 0;
224 const char *kmem = KMEM;
225 const char *file = NULL;
228 static void getoffsets P((off_t *, off_t *, off_t *, off_t *));
229 static int openfile P((const char *, int));
230 static void writevar P((int, off_t, int));
231 static void readvar P((int, off_t, int *));
234 * main - parse arguments and handle options
244 off_t tickadj_offset;
247 off_t noprintf_offset;
248 int tickadj, ktickadj; /* HMS: Why isn't this u_long? */
249 int tick, ktick; /* HMS: Why isn't this u_long? */
253 int hz_int, hz_hundredths;
254 int recommend_tickadj;
258 while ((c = ntp_getopt(argc, argv, "a:Adkpqst:")) != EOF)
263 writetickadj = atoi(ntp_optarg);
264 if (writetickadj <= 0)
266 (void) fprintf(stderr,
267 "%s: unlikely value for tickadj: %s\n",
268 progname, ntp_optarg);
272 #if defined SCO5_CLOCK
273 if (writetickadj % HZ)
275 writetickadj = (writetickadj / HZ) * HZ;
276 (void) fprintf(stderr,
277 "tickadj truncated to: %d\n", writetickadj);
279 #endif /* SCO5_CLOCK */
301 writetick = atoi(ntp_optarg);
304 (void) fprintf(stderr,
305 "%s: unlikely value for tick: %s\n",
306 progname, ntp_optarg);
315 if (errflg || ntp_optind != argc)
317 (void) fprintf(stderr,
318 "usage: %s [-Adkpqs] [-a newadj] [-t newtick]\n", progname);
322 getoffsets(&tick_offset, &tickadj_offset, &dosync_offset, &noprintf_offset);
326 (void) printf("tick offset = %lu\n", (unsigned long)tick_offset);
327 (void) printf("tickadj offset = %lu\n", (unsigned long)tickadj_offset);
328 (void) printf("dosynctodr offset = %lu\n", (unsigned long)dosync_offset);
329 (void) printf("noprintf offset = %lu\n", (unsigned long)noprintf_offset);
332 if (writetick && (tick_offset == 0))
334 (void) fprintf(stderr,
335 "No tick kernel variable\n");
339 if (writeopttickadj && (tickadj_offset == 0))
341 (void) fprintf(stderr,
342 "No tickadj kernel variable\n");
346 if (unsetdosync && (dosync_offset == 0))
348 (void) fprintf(stderr,
349 "No dosynctodr kernel variable\n");
353 if (setnoprintf && (noprintf_offset == 0))
355 (void) fprintf(stderr,
356 "No noprintf kernel variable\n");
360 if (tick_offset != 0)
362 readvar(fd, tick_offset, &tick);
363 #if defined(TICK_NANO) && defined(K_TICK_NAME)
365 (void) printf("KERNEL %s = %d nsec\n", K_TICK_NAME, tick);
366 #endif /* TICK_NANO && K_TICK_NAME */
377 if (tickadj_offset != 0)
379 readvar(fd, tickadj_offset, &tickadj);
382 /* scale from nsec/sec to usec/tick */
383 tickadj /= (1000L * HZ);
384 #endif /*SCO5_CLOCK */
386 #if defined(TICKADJ_NANO) && defined(K_TICKADJ_NAME)
388 (void) printf("KERNEL %s = %d nsec\n", K_TICKADJ_NAME, tickadj);
389 #endif /* TICKADJ_NANO && K_TICKADJ_NAME */
401 if (dosync_offset != 0)
403 readvar(fd, dosync_offset, &dosynctodr);
406 if (noprintf_offset != 0)
408 readvar(fd, noprintf_offset, &noprintf);
413 if (unsetdosync && dosync_offset == 0)
415 (void) fprintf(stderr,
416 "%s: can't find %s in namelist\n",
418 #ifdef K_DOSYNCTODR_NAME
420 #else /* not K_DOSYNCTODR_NAME */
422 #endif /* not K_DOSYNCTODR_NAME */
428 #if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
429 hz = (int) sysconf (_SC_CLK_TCK);
430 #endif /* not HAVE_SYSCONF && _SC_CLK_TCK */
437 #endif /* PRESET_TICK */
444 #ifdef PRESET_TICKADJ
445 tickadj = (PRESET_TICKADJ) ? PRESET_TICKADJ : 1;
446 #endif /* PRESET_TICKADJ */
450 if (tick_offset != 0)
452 (void) printf("KERNEL tick = %d usec (from %s kernel variable)\n",
457 "<this can't happen>"
462 (void) printf("PRESET tick = %d usec\n", tick);
463 #endif /* PRESET_TICK */
464 if (tickadj_offset != 0)
466 (void) printf("KERNEL tickadj = %d usec (from %s kernel variable)\n",
468 #ifdef K_TICKADJ_NAME
471 "<this can't happen>"
475 #ifdef PRESET_TICKADJ
476 (void) printf("PRESET tickadj = %d usec\n", tickadj);
477 #endif /* PRESET_TICKADJ */
478 if (dosync_offset != 0)
480 (void) printf("dosynctodr is %s\n", dosynctodr ? "on" : "off");
482 if (noprintf_offset != 0)
484 (void) printf("kernel level printf's: %s\n",
485 noprintf ? "off" : "on");
491 (void) fprintf(stderr, "%s: the value of tick is silly!\n",
496 hz_int = (int)(1000000L / (long)tick);
497 hz_hundredths = (int)((100000000L / (long)tick) - ((long)hz_int * 100L));
500 (void) printf("KERNEL hz = %d\n", hz);
501 (void) printf("calculated hz = %d.%02d Hz\n", hz_int,
505 #if defined SCO5_CLOCK
506 recommend_tickadj = 100;
507 #else /* SCO5_CLOCK */
508 tmp = (long) tick * 500L;
509 recommend_tickadj = (int)(tmp / 1000000L);
510 if (tmp % 1000000L > 0)
515 #ifdef MIN_REC_TICKADJ
516 if (recommend_tickadj < MIN_REC_TICKADJ)
518 recommend_tickadj = MIN_REC_TICKADJ;
520 #endif /* MIN_REC_TICKADJ */
521 #endif /* SCO5_CLOCK */
524 if ((!quiet) && (tickadj_offset != 0))
526 (void) printf("recommended value of tickadj = %d us\n",
530 if ( writetickadj == 0
536 exit(errflg ? 1 : 0);
539 if (writetickadj == 0 && writeopttickadj)
541 writetickadj = recommend_tickadj;
544 fd = openfile(file, O_WRONLY);
546 if (setnoprintf && (noprintf_offset != 0))
550 (void) fprintf(stderr, "setting noprintf: ");
551 (void) fflush(stderr);
553 writevar(fd, noprintf_offset, 1);
556 (void) fprintf(stderr, "done!\n");
560 if ((writetick > 0) && (tick_offset != 0))
564 (void) fprintf(stderr, "writing tick, value %d: ",
566 (void) fflush(stderr);
568 writevar(fd, tick_offset, writetick);
571 (void) fprintf(stderr, "done!\n");
575 if ((writetickadj > 0) && (tickadj_offset != 0))
579 (void) fprintf(stderr, "writing tickadj, value %d: ",
581 (void) fflush(stderr);
585 /* scale from usec/tick to nsec/sec */
586 writetickadj *= (1000L * HZ);
587 #endif /* SCO5_CLOCK */
589 writevar(fd, tickadj_offset, writetickadj);
592 (void) fprintf(stderr, "done!\n");
596 if (unsetdosync && (dosync_offset != 0))
600 (void) fprintf(stderr, "zeroing dosynctodr: ");
601 (void) fflush(stderr);
603 writevar(fd, dosync_offset, 0);
606 (void) fprintf(stderr, "done!\n");
610 return(errflg ? 1 : 0);
614 * getoffsets - read the magic offsets from the specified file
626 # ifndef HAVE_KVM_OPEN
632 # ifdef NLIST_NAME_UNION
641 #define K_FILLER_NAME "DavidLetterman"
643 #ifdef NLIST_EXTRA_INDIRECTION
648 static struct nlist nl[] =
651 #ifdef K_TICKADJ_NAME
667 #ifdef K_DOSYNCTODR_NAME
675 #ifdef K_NOPRINTF_NAME
685 #ifndef HAVE_KVM_OPEN
686 static const char *kernels[] =
688 #ifdef HAVE_GETBOOTFILE
689 NULL, /* *** SEE BELOW! *** */
703 #endif /* not HAVE_KVM_OPEN */
707 * Solaris > 2.5 doesn't have a kernel file. Use the kvm_* interface
708 * to read the kernel name list. -- stolcke 3/4/96
710 kvm_t *kvm_handle = kvm_open(NULL, NULL, NULL, O_RDONLY, progname);
712 if (kvm_handle == NULL)
714 (void) fprintf(stderr,
715 "%s: kvm_open failed\n",
719 if (kvm_nlist(kvm_handle, nl) == -1)
721 (void) fprintf(stderr,
722 "%s: kvm_nlist failed\n",
726 kvm_close(kvm_handle);
727 #else /* not HAVE_KVM_OPEN */
728 #ifdef HAVE_GETBOOTFILE /* *** SEE HERE! *** */
729 if (kernels[0] == NULL)
731 char * cp = (char *)getbootfile();
739 kernels[0] = "/Placeholder";
742 #endif /* HAVE_GETBOOTFILE */
743 for (kname = kernels; *kname != NULL; kname++)
747 if (stat(*kname, &stbuf) == -1)
751 if (nlist(*kname, nl) >= 0)
757 (void) fprintf(stderr,
758 "%s: nlist didn't find needed symbols from <%s>: %s\n",
759 progname, *kname, strerror(errno));
764 (void) fprintf(stderr,
765 "%s: Couldn't find the kernel\n",
769 #endif /* HAVE_KVM_OPEN */
775 fd = openfile(file, O_RDONLY);
776 #ifdef NLIST_EXTRA_INDIRECTION
778 * Go one more round of indirection.
780 for (i = 0; i < (sizeof(nl) / sizeof(struct nlist)); i++)
782 if ((nl[i].n_value) && (nl[i].n_sclass == 0x6b))
784 readvar(fd, nl[i].n_value, &nl[i].n_value);
787 #endif /* NLIST_EXTRA_INDIRECTION */
789 #endif /* not NOKMEM */
796 #if defined(N_TICKADJ)
797 *tickadj_off = nl[N_TICKADJ].n_value;
801 *tick_off = nl[N_TICK].n_value;
804 #if defined(N_DOSYNC)
805 *dosync_off = nl[N_DOSYNC].n_value;
808 #if defined(N_NOPRINTF)
809 *noprintf_off = nl[N_NOPRINTF].n_value;
821 * openfile - open the file, check for errors
831 ifd = open(name, mode);
834 (void) fprintf(stderr, "%s: open %s: ", progname, name);
843 * writevar - write a variable into the file
853 if (lseek(ofd, off, L_SET) == -1)
855 (void) fprintf(stderr, "%s: lseek fails: ", progname);
859 if (write(ofd, (char *)&var, sizeof(int)) != sizeof(int))
861 (void) fprintf(stderr, "%s: write fails: ", progname);
870 * readvar - read a variable from the file
881 if (lseek(ifd, off, L_SET) == -1)
883 (void) fprintf(stderr, "%s: lseek fails: ", progname);
887 i = read(ifd, (char *)var, sizeof(int));
890 (void) fprintf(stderr, "%s: read fails: ", progname);
894 if (i != sizeof(int))
896 (void) fprintf(stderr, "%s: read expected %d, got %d\n",
897 progname, (int)sizeof(int), i);
902 #endif /* not Linux */