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>
59 static struct {int bit; char *name;} statlist[] = {
61 { STA_PPSFREQ, "PPSFREQ" },
62 { STA_PPSTIME, "PPSTIME" },
66 { STA_UNSYNC, "UNSYNC" },
67 { STA_FREQHOLD, "FREQHOLD" },
68 { STA_PPSSIGNAL, "PPSSIGNAL" },
69 { STA_PPSJITTER, "PPSJITTER" },
70 { STA_PPSWANDER, "PPSWANDER" },
71 { STA_PPSERROR, "PPSERROR" },
72 { STA_CLOCKERR, "CLOCKERR" },
75 static char *ret_code_descript[] = {
79 "leap second in progress",
80 "leap second has occurred",
81 "clock not synchronized" };
84 #define main adjtimex_main
86 void usage(char *prog)
89 "Usage: %s [ -q ] [ -o offset ] [ -f frequency ] [ -p timeconstant ] [ -t tick ]\n",
92 #define show_usage() usage(argv[0])
95 int main(int argc, char ** argv)
103 c = getopt( argc, argv, "qo:f:p:t:");
110 txc.offset = atoi(optarg);
111 txc.modes |= ADJ_OFFSET_SINGLESHOT;
114 txc.freq = atoi(optarg);
115 txc.modes |= ADJ_FREQUENCY;
118 txc.constant = atoi(optarg);
119 txc.modes |= ADJ_TIMECONST;
122 txc.tick = atoi(optarg);
123 txc.modes |= ADJ_TICK;
130 if (argc != optind) { /* no valid non-option parameters */
135 ret = adjtimex(&txc);
137 if (ret < 0) perror("adjtimex");
139 if (!quiet && ret>=0) {
143 "-f frequency: %ld\n"
147 txc.modes, txc.offset, txc.freq, txc.maxerror,
148 txc.esterror, txc.status);
150 /* representative output of next code fragment:
153 for (i=0; statlist[i].name; i++) {
154 if (txc.status & statlist[i].bit) {
155 if (sep) fputs(" | ",stdout);
156 fputs(statlist[i].name,stdout);
162 if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret];
164 "-p timeconstant: %ld\n"
168 " time.tv_sec: %ld\n"
169 " time.tv_usec: %ld\n"
170 " return value: %d (%s)\n",
172 txc.precision, txc.tolerance, txc.tick,
173 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);