2 * adjtimex.c - read, and possibly modify, the Linux kernel `timex' variables.
4 * Originally written: October 1997
5 * Last hack: March 2001
6 * Copyright 1997, 2000, 2001 Larry Doolittle <LRDoolittle@lbl.gov>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License (Version 2,
10 * June 1991) as published by the Free Software Foundation. At the
11 * time of writing, that license was published by the FSF with the URL
12 * http://www.gnu.org/copyleft/gpl.html, and is incorporated herein by
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * This adjtimex(1) is very similar in intent to adjtimex(8) by Steven
21 * Dick <ssd@nevets.oau.org> and Jim Van Zandt <jrv@vanzandt.mv.com>
22 * (see http://metalab.unc.edu/pub/Linux/system/admin/time/adjtimex*).
23 * That version predates this one, and is _much_ bigger and more
24 * featureful. My independently written version was very similar to
25 * Steven's from the start, because they both follow the kernel timex
26 * structure. I further tweaked this version to be equivalent to Steven's
27 * where possible, but I don't like getopt_long, so the actual usage
28 * syntax is incompatible.
30 * Amazingly enough, my Red Hat 5.2 sys/timex (and sub-includes)
31 * don't actually give a prototype for adjtimex(2), so building
32 * this code (with -Wall) gives a warning. Later versions of
33 * glibc fix this issue.
35 * This program is too simple for a Makefile, just build with:
36 * gcc -Wall -O adjtimex.c -o adjtimex
38 * busyboxed 20 March 2001, Larry Doolittle <ldoolitt@recycle.lbl.gov>
39 * It will autosense if it is built in a busybox environment, based
40 * on the BB_VER preprocessor macro.
44 #include <sys/types.h>
48 #if __GNU_LIBRARY__ < 5
49 #include <sys/timex.h>
50 extern int adjtimex(struct timex *buf);
52 #include <sys/timex.h>
57 static struct {int bit; char *name;} statlist[] = {
59 { STA_PPSFREQ, "PPSFREQ" },
60 { STA_PPSTIME, "PPSTIME" },
64 { STA_UNSYNC, "UNSYNC" },
65 { STA_FREQHOLD, "FREQHOLD" },
66 { STA_PPSSIGNAL, "PPSSIGNAL" },
67 { STA_PPSJITTER, "PPSJITTER" },
68 { STA_PPSWANDER, "PPSWANDER" },
69 { STA_PPSERROR, "PPSERROR" },
70 { STA_CLOCKERR, "CLOCKERR" },
73 static char *ret_code_descript[] = {
77 "leap second in progress",
78 "leap second has occurred",
79 "clock not synchronized" };
82 #define main adjtimex_main
84 void usage(char *prog)
87 "Usage: %s [ -q ] [ -o offset ] [ -f frequency ] [ -p timeconstant ] [ -t tick ]\n",
90 #define bb_show_usage() usage(argv[0])
93 int main(int argc, char ** argv)
101 c = getopt( argc, argv, "qo:f:p:t:");
108 txc.offset = atoi(optarg);
109 txc.modes |= ADJ_OFFSET_SINGLESHOT;
112 txc.freq = atoi(optarg);
113 txc.modes |= ADJ_FREQUENCY;
116 txc.constant = atoi(optarg);
117 txc.modes |= ADJ_TIMECONST;
120 txc.tick = atoi(optarg);
121 txc.modes |= ADJ_TICK;
128 if (argc != optind) { /* no valid non-option parameters */
133 ret = adjtimex(&txc);
135 if (ret < 0) perror("adjtimex");
137 if (!quiet && ret>=0) {
141 "-f frequency: %ld\n"
145 txc.modes, txc.offset, txc.freq, txc.maxerror,
146 txc.esterror, txc.status);
148 /* representative output of next code fragment:
151 for (i=0; statlist[i].name; i++) {
152 if (txc.status & statlist[i].bit) {
153 if (sep) fputs(" | ",stdout);
154 fputs(statlist[i].name,stdout);
160 if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret];
162 "-p timeconstant: %ld\n"
166 " time.tv_sec: %ld\n"
167 " time.tv_usec: %ld\n"
168 " return value: %d (%s)\n",
170 txc.precision, txc.tolerance, txc.tick,
171 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);