4a242cfb753c55d14530272324bcc2ad0da57397
[oweals/busybox.git] / miscutils / adjtimex.c
1 /*
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>
47 #include <sys/time.h>
48 #include <sys/timex.h>
49 #include "busybox.h"
50
51 #if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
52 #define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
53 #endif
54 #if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
55 #define ADJ_FREQUENCY MOD_FREQUENCY
56 #endif
57 #if !defined ADJ_TIMECONST && defined MOD_TIMECONST
58 #define ADJ_TIMECONST MOD_TIMECONST
59 #endif
60 #if !defined ADJ_TICK && defined MOD_CLKB
61 #define ADJ_TICK MOD_CLKB
62 #endif
63
64 static const struct {int bit; const char *name;} statlist[] = {
65         { STA_PLL,       "PLL"       },
66         { STA_PPSFREQ,   "PPSFREQ"   },
67         { STA_PPSTIME,   "PPSTIME"   },
68         { STA_FLL,       "FFL"       },
69         { STA_INS,       "INS"       },
70         { STA_DEL,       "DEL"       },
71         { STA_UNSYNC,    "UNSYNC"    },
72         { STA_FREQHOLD,  "FREQHOLD"  },
73         { STA_PPSSIGNAL, "PPSSIGNAL" },
74         { STA_PPSJITTER, "PPSJITTER" },
75         { STA_PPSWANDER, "PPSWANDER" },
76         { STA_PPSERROR,  "PPSERROR"  },
77         { STA_CLOCKERR,  "CLOCKERR"  },
78         { 0, NULL } };
79
80 static const char * const ret_code_descript[] = {
81         "clock synchronized",
82         "insert leap second",
83         "delete leap second",
84         "leap second in progress",
85         "leap second has occurred",
86         "clock not synchronized" };
87
88 #ifdef BB_VER
89 #define main adjtimex_main
90 #else
91 void usage(char *prog)
92 {
93         fprintf(stderr,
94                 "Usage: %s [ -q ] [ -o offset ] [ -f frequency ] [ -p timeconstant ] [ -t tick ]\n",
95                 prog);
96 }
97 #define bb_show_usage() usage(argv[0])
98 #endif
99
100 int main(int argc, char ** argv)
101 {
102         struct timex txc;
103         int quiet=0;
104         int c, i, ret, sep;
105         const char *descript;
106         txc.modes=0;
107         for (;;) {
108                 c = getopt( argc, argv, "qo:f:p:t:");
109                 if (c == EOF) break;
110                 switch (c) {
111                         case 'q':
112                                 quiet=1;
113                                 break;
114                         case 'o':
115                                 txc.offset = atoi(optarg);
116                                 txc.modes |= ADJ_OFFSET_SINGLESHOT;
117                                 break;
118                         case 'f':
119                                 txc.freq = atoi(optarg);
120                                 txc.modes |= ADJ_FREQUENCY;
121                                 break;
122                         case 'p':
123                                 txc.constant = atoi(optarg);
124                                 txc.modes |= ADJ_TIMECONST;
125                                 break;
126                         case 't':
127                                 txc.tick = atoi(optarg);
128                                 txc.modes |= ADJ_TICK;
129                                 break;
130                         default:
131                                 bb_show_usage();
132                                 exit(1);
133                 }
134         }
135         if (argc != optind) { /* no valid non-option parameters */
136                 bb_show_usage();
137                 exit(1);
138         }
139
140         ret = adjtimex(&txc);
141
142         if (ret < 0) perror("adjtimex");
143
144         if (!quiet && ret>=0) {
145                 printf(
146                         "    mode:         %d\n"
147                         "-o  offset:       %ld\n"
148                         "-f  frequency:    %ld\n"
149                         "    maxerror:     %ld\n"
150                         "    esterror:     %ld\n"
151                         "    status:       %d ( ",
152                 txc.modes, txc.offset, txc.freq, txc.maxerror,
153                 txc.esterror, txc.status);
154
155                 /* representative output of next code fragment:
156                    "PLL | PPSTIME" */
157                 sep=0;
158                 for (i=0; statlist[i].name; i++) {
159                         if (txc.status & statlist[i].bit) {
160                                 if (sep) fputs(" | ",stdout);
161                                 fputs(statlist[i].name,stdout);
162                                 sep=1;
163                         }
164                 }
165
166                 descript = "error";
167                 if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret];
168                 printf(" )\n"
169                         "-p  timeconstant: %ld\n"
170                         "    precision:    %ld\n"
171                         "    tolerance:    %ld\n"
172                         "-t  tick:         %ld\n"
173                         "    time.tv_sec:  %ld\n"
174                         "    time.tv_usec: %ld\n"
175                         "    return value: %d (%s)\n",
176                 txc.constant,
177                 txc.precision, txc.tolerance, txc.tick,
178                 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
179         }
180         return (ret<0);
181 }