1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include "libbb.h"
36#ifdef __BIONIC__
37# include <linux/timex.h>
38#else
39# include <sys/timex.h>
40#endif
41
42static const uint16_t statlist_bit[] ALIGN2 = {
43 STA_PLL,
44 STA_PPSFREQ,
45 STA_PPSTIME,
46 STA_FLL,
47 STA_INS,
48 STA_DEL,
49 STA_UNSYNC,
50 STA_FREQHOLD,
51 STA_PPSSIGNAL,
52 STA_PPSJITTER,
53 STA_PPSWANDER,
54 STA_PPSERROR,
55 STA_CLOCKERR,
56 0
57};
58static const char statlist_name[] ALIGN1 =
59 "PLL" "\0"
60 "PPSFREQ" "\0"
61 "PPSTIME" "\0"
62 "FFL" "\0"
63 "INS" "\0"
64 "DEL" "\0"
65 "UNSYNC" "\0"
66 "FREQHOLD" "\0"
67 "PPSSIGNAL" "\0"
68 "PPSJITTER" "\0"
69 "PPSWANDER" "\0"
70 "PPSERROR" "\0"
71 "CLOCKERR"
72;
73
74static const char ret_code_descript[] ALIGN1 =
75 "clock synchronized" "\0"
76 "insert leap second" "\0"
77 "delete leap second" "\0"
78 "leap second in progress" "\0"
79 "leap second has occurred" "\0"
80 "clock not synchronized"
81;
82
83int adjtimex_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
84int adjtimex_main(int argc UNUSED_PARAM, char **argv)
85{
86 enum {
87 OPT_quiet = 0x1
88 };
89 unsigned opt;
90 char *opt_o, *opt_f, *opt_p, *opt_t;
91 struct timex txc;
92 int ret;
93 const char *descript;
94
95 memset(&txc, 0, sizeof(txc));
96
97 opt = getopt32(argv, "^" "qo:f:p:t:"
98 "\0" "=0",
99 &opt_o, &opt_f, &opt_p, &opt_t
100 );
101
102 if (opt & 0x2) {
103 txc.offset = xatol(opt_o);
104 txc.modes |= ADJ_OFFSET_SINGLESHOT;
105 }
106 if (opt & 0x4) {
107 txc.freq = xatol(opt_f);
108 txc.modes |= ADJ_FREQUENCY;
109 }
110 if (opt & 0x8) {
111 txc.constant = xatol(opt_p);
112 txc.modes |= ADJ_TIMECONST;
113 }
114 if (opt & 0x10) {
115 txc.tick = xatol(opt_t);
116 txc.modes |= ADJ_TICK;
117 }
118
119
120
121
122
123
124 ret = adjtimex(&txc);
125 if (ret < 0)
126 bb_perror_nomsg_and_die();
127
128 if (!(opt & OPT_quiet)) {
129 const char *sep;
130 const char *name;
131 int i;
132
133 printf(
134 " mode: %d\n"
135 "-o offset: %ld us\n"
136 "-f freq.adjust: %ld (65536 = 1ppm)\n"
137 " maxerror: %ld\n"
138 " esterror: %ld\n"
139 " status: %d (",
140 txc.modes, txc.offset, txc.freq, txc.maxerror,
141 txc.esterror, txc.status
142 );
143
144
145
146
147 name = statlist_name;
148 sep = "";
149 for (i = 0; statlist_bit[i]; i++) {
150 if (txc.status & statlist_bit[i]) {
151 printf("%s%s", sep, name);
152 sep = " | ";
153 }
154 name += strlen(name) + 1;
155 }
156
157 descript = "error";
158 if (ret <= 5)
159 descript = nth_string(ret_code_descript, ret);
160 printf(")\n"
161 "-p timeconstant: %ld\n"
162 " precision: %ld us\n"
163 " tolerance: %ld\n"
164 "-t tick: %ld us\n"
165 " time.tv_sec: %ld\n"
166 " time.tv_usec: %ld\n"
167 " return value: %d (%s)\n",
168 txc.constant,
169 txc.precision, txc.tolerance, txc.tick,
170 (long)txc.time.tv_sec, (long)txc.time.tv_usec,
171 ret, descript
172 );
173 }
174
175 return 0;
176}
177