blob: e3c160d879c987676715cbe1d36973c09a2d8823 [file] [log] [blame]
Mark Whitley6f932772001-03-20 19:18:10 +00001/*
2 * adjtimex.c - read, and possibly modify, the Linux kernel `timex' variables.
3 *
4 * Originally written: October 1997
5 * Last hack: March 2001
6 * Copyright 1997, 2000, 2001 Larry Doolittle <LRDoolittle@lbl.gov>
7 *
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
13 * reference.
14 *
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.
19 *
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.
29 *
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.
34 *
35 * This program is too simple for a Makefile, just build with:
36 * gcc -Wall -O adjtimex.c -o adjtimex
37 *
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.
41 */
42
43#include <stdio.h>
44#include <sys/types.h>
45#include <stdlib.h>
46#include <unistd.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000047
48#if __GNU_LIBRARY__ < 5
Mark Whitley6f932772001-03-20 19:18:10 +000049#include <sys/timex.h>
Eric Andersene76c3b02001-04-05 03:14:39 +000050extern int adjtimex(struct timex *buf);
51#else
52#include <sys/timex.h>
53#endif
54
Mark Whitley6f932772001-03-20 19:18:10 +000055#ifdef BB_VER
56#include "busybox.h"
57#endif
58
59static struct {int bit; char *name;} statlist[] = {
60 { STA_PLL, "PLL" },
61 { STA_PPSFREQ, "PPSFREQ" },
62 { STA_PPSTIME, "PPSTIME" },
63 { STA_FLL, "FFL" },
64 { STA_INS, "INS" },
65 { STA_DEL, "DEL" },
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" },
73 { 0, NULL } };
74
75static char *ret_code_descript[] = {
76 "clock synchronized",
77 "insert leap second",
78 "delete leap second",
79 "leap second in progress",
80 "leap second has occurred",
81 "clock not synchronized" };
82
83#ifdef BB_VER
84#define main adjtimex_main
85#else
86void usage(char *prog)
87{
88 fprintf(stderr,
89 "Usage: %s [ -q ] [ -o offset ] [ -f frequency ] [ -p timeconstant ] [ -t tick ]\n",
90 prog);
91}
92#define show_usage() usage(argv[0])
93#endif
94
95int main(int argc, char ** argv)
96{
97 struct timex txc;
98 int quiet=0;
99 int c, i, ret, sep;
100 char *descript;
101 txc.modes=0;
102 for (;;) {
103 c = getopt( argc, argv, "qo:f:p:t:");
104 if (c == EOF) break;
105 switch (c) {
106 case 'q':
107 quiet=1;
108 break;
109 case 'o':
110 txc.offset = atoi(optarg);
111 txc.modes |= ADJ_OFFSET_SINGLESHOT;
112 break;
113 case 'f':
114 txc.freq = atoi(optarg);
115 txc.modes |= ADJ_FREQUENCY;
116 break;
117 case 'p':
118 txc.constant = atoi(optarg);
119 txc.modes |= ADJ_TIMECONST;
120 break;
121 case 't':
122 txc.tick = atoi(optarg);
123 txc.modes |= ADJ_TICK;
124 break;
125 default:
126 show_usage();
127 exit(1);
128 }
129 }
130 if (argc != optind) { /* no valid non-option parameters */
131 show_usage();
132 exit(1);
133 }
134
135 ret = adjtimex(&txc);
136
137 if (ret < 0) perror("adjtimex");
138
139 if (!quiet && ret>=0) {
140 printf(
141 " mode: %d\n"
142 "-o offset: %ld\n"
143 "-f frequency: %ld\n"
144 " maxerror: %ld\n"
145 " esterror: %ld\n"
146 " status: %d ( ",
147 txc.modes, txc.offset, txc.freq, txc.maxerror,
148 txc.esterror, txc.status);
149
150 /* representative output of next code fragment:
151 "PLL | PPSTIME" */
152 sep=0;
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);
157 sep=1;
158 }
159 }
160
161 descript = "error";
162 if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret];
163 printf(" )\n"
164 "-p timeconstant: %ld\n"
165 " precision: %ld\n"
166 " tolerance: %ld\n"
167 "-t tick: %ld\n"
168 " time.tv_sec: %ld\n"
169 " time.tv_usec: %ld\n"
170 " return value: %d (%s)\n",
171 txc.constant,
172 txc.precision, txc.tolerance, txc.tick,
Eric Andersene76c3b02001-04-05 03:14:39 +0000173 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
Mark Whitley6f932772001-03-20 19:18:10 +0000174 }
175 return (ret<0);
176}