summaryrefslogtreecommitdiff
path: root/miscutils/adjtimex.c (plain)
blob: 1bc05b5acee92a0bd7962a45169cb2ce01d03826
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
14//usage:#define adjtimex_trivial_usage
15//usage: "[-q] [-o OFF] [-f FREQ] [-p TCONST] [-t TICK]"
16//usage:#define adjtimex_full_usage "\n\n"
17//usage: "Read and optionally set system timebase parameters. See adjtimex(2)\n"
18//usage: "\n -q Quiet"
19//usage: "\n -o OFF Time offset, microseconds"
20//usage: "\n -f FREQ Frequency adjust, integer kernel units (65536 is 1ppm)"
21//usage: "\n (positive values make clock run faster)"
22//usage: "\n -t TICK Microseconds per tick, usually 10000"
23//usage: "\n -p TCONST"
24
25#include "libbb.h"
26#ifdef __BIONIC__
27# include <linux/timex.h>
28extern int adjtimex (struct timex *);
29#else
30# include <sys/timex.h>
31#endif
32
33static const uint16_t statlist_bit[] = {
34 STA_PLL,
35 STA_PPSFREQ,
36 STA_PPSTIME,
37 STA_FLL,
38 STA_INS,
39 STA_DEL,
40 STA_UNSYNC,
41 STA_FREQHOLD,
42 STA_PPSSIGNAL,
43 STA_PPSJITTER,
44 STA_PPSWANDER,
45 STA_PPSERROR,
46 STA_CLOCKERR,
47 0
48};
49static const char statlist_name[] =
50 "PLL" "\0"
51 "PPSFREQ" "\0"
52 "PPSTIME" "\0"
53 "FFL" "\0"
54 "INS" "\0"
55 "DEL" "\0"
56 "UNSYNC" "\0"
57 "FREQHOLD" "\0"
58 "PPSSIGNAL" "\0"
59 "PPSJITTER" "\0"
60 "PPSWANDER" "\0"
61 "PPSERROR" "\0"
62 "CLOCKERR"
63;
64
65static const char ret_code_descript[] =
66 "clock synchronized" "\0"
67 "insert leap second" "\0"
68 "delete leap second" "\0"
69 "leap second in progress" "\0"
70 "leap second has occurred" "\0"
71 "clock not synchronized"
72;
73
74int adjtimex_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
75int adjtimex_main(int argc UNUSED_PARAM, char **argv)
76{
77 enum {
78 OPT_quiet = 0x1
79 };
80 unsigned opt;
81 char *opt_o, *opt_f, *opt_p, *opt_t;
82 struct timex txc;
83 int i, ret;
84 const char *descript;
85
86 opt_complementary = "=0"; /* no valid non-option parameters */
87 opt = getopt32(argv, "qo:f:p:t:",
88 &opt_o, &opt_f, &opt_p, &opt_t);
89 txc.modes = 0;
90 //if (opt & 0x1) // -q
91 if (opt & 0x2) { // -o
92 txc.offset = xatol(opt_o);
93 txc.modes |= ADJ_OFFSET_SINGLESHOT;
94 }
95 if (opt & 0x4) { // -f
96 txc.freq = xatol(opt_f);
97 txc.modes |= ADJ_FREQUENCY;
98 }
99 if (opt & 0x8) { // -p
100 txc.constant = xatol(opt_p);
101 txc.modes |= ADJ_TIMECONST;
102 }
103 if (opt & 0x10) { // -t
104 txc.tick = xatol(opt_t);
105 txc.modes |= ADJ_TICK;
106 }
107
108 ret = adjtimex(&txc);
109
110 if (ret < 0) {
111 bb_perror_nomsg_and_die();
112 }
113
114 if (!(opt & OPT_quiet)) {
115 int sep;
116 const char *name;
117
118 printf(
119 " mode: %d\n"
120 "-o offset: %ld\n"
121 "-f frequency: %ld\n"
122 " maxerror: %ld\n"
123 " esterror: %ld\n"
124 " status: %d (",
125 txc.modes, txc.offset, txc.freq, txc.maxerror,
126 txc.esterror, txc.status);
127
128 /* representative output of next code fragment:
129 "PLL | PPSTIME" */
130 name = statlist_name;
131 sep = 0;
132 for (i = 0; statlist_bit[i]; i++) {
133 if (txc.status & statlist_bit[i]) {
134 if (sep)
135 fputs(" | ", stdout);
136 fputs(name, stdout);
137 sep = 1;
138 }
139 name += strlen(name) + 1;
140 }
141
142 descript = "error";
143 if (ret <= 5)
144 descript = nth_string(ret_code_descript, ret);
145 printf(")\n"
146 "-p timeconstant: %ld\n"
147 " precision: %ld\n"
148 " tolerance: %ld\n"
149 "-t tick: %ld\n"
150 " time.tv_sec: %ld\n"
151 " time.tv_usec: %ld\n"
152 " return value: %d (%s)\n",
153 txc.constant,
154 txc.precision, txc.tolerance, txc.tick,
155 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
156 }
157
158 return 0;
159}
160