summaryrefslogtreecommitdiff
path: root/miscutils/adjtimex.c (plain)
blob: b4b54c4241269bf4c96d4972d7b31d7f4ce98010
1/* vi: set sw=4 ts=4: */
2/*
3 * adjtimex.c - read, and possibly modify, the Linux kernel `timex' variables.
4 *
5 * Originally written: October 1997
6 * Last hack: March 2001
7 * Copyright 1997, 2000, 2001 Larry Doolittle <LRDoolittle@lbl.gov>
8 *
9 * busyboxed 20 March 2001, Larry Doolittle <ldoolitt@recycle.lbl.gov>
10 *
11 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 */
13//config:config ADJTIMEX
14//config: bool "adjtimex"
15//config: default y
16//config: select PLATFORM_LINUX
17//config: help
18//config: Adjtimex reads and optionally sets adjustment parameters for
19//config: the Linux clock adjustment algorithm.
20
21//applet:IF_ADJTIMEX(APPLET(adjtimex, BB_DIR_SBIN, BB_SUID_DROP))
22
23//kbuild:lib-$(CONFIG_ADJTIMEX) += adjtimex.o
24
25//usage:#define adjtimex_trivial_usage
26//usage: "[-q] [-o OFF] [-f FREQ] [-p TCONST] [-t TICK]"
27//usage:#define adjtimex_full_usage "\n\n"
28//usage: "Read or set kernel time variables. See adjtimex(2)\n"
29//usage: "\n -q Quiet"
30//usage: "\n -o OFF Time offset, microseconds"
31//usage: "\n -f FREQ Frequency adjust, integer kernel units (65536 is 1ppm)"
32//usage: "\n -t TICK Microseconds per tick, usually 10000"
33//usage: "\n (positive -t or -f values make clock run faster)"
34//usage: "\n -p TCONST"
35
36#include "libbb.h"
37#ifdef __BIONIC__
38# include <linux/timex.h>
39extern int adjtimex (struct timex *);
40#else
41# include <sys/timex.h>
42#endif
43
44static const uint16_t statlist_bit[] ALIGN2 = {
45 STA_PLL,
46 STA_PPSFREQ,
47 STA_PPSTIME,
48 STA_FLL,
49 STA_INS,
50 STA_DEL,
51 STA_UNSYNC,
52 STA_FREQHOLD,
53 STA_PPSSIGNAL,
54 STA_PPSJITTER,
55 STA_PPSWANDER,
56 STA_PPSERROR,
57 STA_CLOCKERR,
58 0
59};
60static const char statlist_name[] ALIGN1 =
61 "PLL" "\0"
62 "PPSFREQ" "\0"
63 "PPSTIME" "\0"
64 "FFL" "\0"
65 "INS" "\0"
66 "DEL" "\0"
67 "UNSYNC" "\0"
68 "FREQHOLD" "\0"
69 "PPSSIGNAL" "\0"
70 "PPSJITTER" "\0"
71 "PPSWANDER" "\0"
72 "PPSERROR" "\0"
73 "CLOCKERR"
74;
75
76static const char ret_code_descript[] ALIGN1 =
77 "clock synchronized" "\0"
78 "insert leap second" "\0"
79 "delete leap second" "\0"
80 "leap second in progress" "\0"
81 "leap second has occurred" "\0"
82 "clock not synchronized"
83;
84
85int adjtimex_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
86int adjtimex_main(int argc UNUSED_PARAM, char **argv)
87{
88 enum {
89 OPT_quiet = 0x1
90 };
91 unsigned opt;
92 char *opt_o, *opt_f, *opt_p, *opt_t;
93 struct timex txc;
94 int i, ret;
95 const char *descript;
96
97 opt_complementary = "=0"; /* no valid non-option parameters */
98 opt = getopt32(argv, "qo:f:p:t:",
99 &opt_o, &opt_f, &opt_p, &opt_t);
100 txc.modes = 0;
101 //if (opt & 0x1) // -q
102 if (opt & 0x2) { // -o
103 txc.offset = xatol(opt_o);
104 txc.modes |= ADJ_OFFSET_SINGLESHOT;
105 }
106 if (opt & 0x4) { // -f
107 txc.freq = xatol(opt_f);
108 txc.modes |= ADJ_FREQUENCY;
109 }
110 if (opt & 0x8) { // -p
111 txc.constant = xatol(opt_p);
112 txc.modes |= ADJ_TIMECONST;
113 }
114 if (opt & 0x10) { // -t
115 txc.tick = xatol(opt_t);
116 txc.modes |= ADJ_TICK;
117 }
118
119 ret = adjtimex(&txc);
120
121 if (ret < 0) {
122 bb_perror_nomsg_and_die();
123 }
124
125 if (!(opt & OPT_quiet)) {
126 const char *sep;
127 const char *name;
128
129 printf(
130 " mode: %d\n"
131 "-o offset: %ld us\n"
132 "-f freq.adjust: %ld (65536 = 1ppm)\n"
133 " maxerror: %ld\n"
134 " esterror: %ld\n"
135 " status: %d (",
136 txc.modes, txc.offset, txc.freq, txc.maxerror,
137 txc.esterror, txc.status);
138
139 /* representative output of next code fragment:
140 * "PLL | PPSTIME"
141 */
142 name = statlist_name;
143 sep = "";
144 for (i = 0; statlist_bit[i]; i++) {
145 if (txc.status & statlist_bit[i]) {
146 printf("%s%s", sep, name);
147 sep = " | ";
148 }
149 name += strlen(name) + 1;
150 }
151
152 descript = "error";
153 if (ret <= 5)
154 descript = nth_string(ret_code_descript, ret);
155 printf(")\n"
156 "-p timeconstant: %ld\n"
157 " precision: %ld us\n"
158 " tolerance: %ld\n"
159 "-t tick: %ld us\n"
160 " time.tv_sec: %ld\n"
161 " time.tv_usec: %ld\n"
162 " return value: %d (%s)\n",
163 txc.constant,
164 txc.precision, txc.tolerance, txc.tick,
165 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
166 }
167
168 return 0;
169}
170