2 * pretty printing of status information
8 #include "ntp_stdlib.h"
11 #include "lib_strbuf.h"
12 #include "ntp_refclock.h"
13 #include "ntp_control.h"
14 #include "ntp_string.h"
16 # include "ntp_syscall.h"
21 * Structure for turning various constants into a readable string.
25 const char * const string;
31 static const struct codestring leap_codes[] = {
32 { LEAP_NOWARNING, "leap_none" },
33 { LEAP_ADDSECOND, "leap_add_sec" },
34 { LEAP_DELSECOND, "leap_del_sec" },
35 { LEAP_NOTINSYNC, "leap_alarm" },
40 * Clock source status (sync)
42 static const struct codestring sync_codes[] = {
43 { CTL_SST_TS_UNSPEC, "sync_unspec" },
44 { CTL_SST_TS_ATOM, "sync_pps" },
45 { CTL_SST_TS_LF, "sync_lf_radio" },
46 { CTL_SST_TS_HF, "sync_hf_radio" },
47 { CTL_SST_TS_UHF, "sync_uhf_radio" },
48 { CTL_SST_TS_LOCAL, "sync_local" },
49 { CTL_SST_TS_NTP, "sync_ntp" },
50 { CTL_SST_TS_UDPTIME, "sync_other" },
51 { CTL_SST_TS_WRSTWTCH, "sync_wristwatch" },
52 { CTL_SST_TS_TELEPHONE, "sync_telephone" },
57 * Peer selection status (sel)
59 static const struct codestring select_codes[] = {
60 { CTL_PST_SEL_REJECT, "sel_reject" },
61 { CTL_PST_SEL_SANE, "sel_falsetick" },
62 { CTL_PST_SEL_CORRECT, "sel_excess" },
63 { CTL_PST_SEL_SELCAND, "sel_outlier" },
64 { CTL_PST_SEL_SYNCCAND, "sel_candidate" },
65 { CTL_PST_SEL_EXCESS, "sel_backup" },
66 { CTL_PST_SEL_SYSPEER, "sel_sys.peer" },
67 { CTL_PST_SEL_PPS, "sel_pps.peer" },
74 static const struct codestring clock_codes[] = {
75 { CTL_CLK_OKAY, "clk_unspec" },
76 { CTL_CLK_NOREPLY, "clk_no_reply" },
77 { CTL_CLK_BADFORMAT, "clk_bad_format" },
78 { CTL_CLK_FAULT, "clk_fault" },
79 { CTL_CLK_PROPAGATION, "clk_bad_signal" },
80 { CTL_CLK_BADDATE, "clk_bad_date" },
81 { CTL_CLK_BADTIME, "clk_bad_time" },
86 #ifdef FLASH_CODES_UNUSED
88 * Flash bits -- see ntpq.c tstflags & tstflagnames
90 static const struct codestring flash_codes[] = {
92 { TEST2, "pkt_bogus" },
93 { TEST3, "pkt_unsync" },
94 { TEST4, "pkt_denied" },
95 { TEST5, "pkt_auth" },
96 { TEST6, "pkt_stratum" },
97 { TEST7, "pkt_header" },
98 { TEST8, "pkt_autokey" },
99 { TEST9, "pkt_crypto" },
100 { TEST10, "peer_stratum" },
101 { TEST11, "peer_dist" },
102 { TEST12, "peer_loop" },
103 { TEST13, "peer_unreach" },
110 * System events (sys)
112 static const struct codestring sys_codes[] = {
113 { EVNT_UNSPEC, "unspecified" },
114 { EVNT_NSET, "freq_not_set" },
115 { EVNT_FSET, "freq_set" },
116 { EVNT_SPIK, "spike_detect" },
117 { EVNT_FREQ, "freq_mode" },
118 { EVNT_SYNC, "clock_sync" },
119 { EVNT_SYSRESTART, "restart" },
120 { EVNT_SYSFAULT, "panic_stop" },
121 { EVNT_NOPEER, "no_sys_peer" },
122 { EVNT_ARMED, "leap_armed" },
123 { EVNT_DISARMED, "leap_disarmed" },
124 { EVNT_LEAP, "leap_event" },
125 { EVNT_CLOCKRESET, "clock_step" },
126 { EVNT_KERN, "kern" },
128 { EVNT_LEAPVAL, "stale_leapsecond_values" },
135 static const struct codestring peer_codes[] = {
136 { PEVNT_MOBIL & ~PEER_EVENT, "mobilize" },
137 { PEVNT_DEMOBIL & ~PEER_EVENT, "demobilize" },
138 { PEVNT_UNREACH & ~PEER_EVENT, "unreachable" },
139 { PEVNT_REACH & ~PEER_EVENT, "reachable" },
140 { PEVNT_RESTART & ~PEER_EVENT, "restart" },
141 { PEVNT_REPLY & ~PEER_EVENT, "no_reply" },
142 { PEVNT_RATE & ~PEER_EVENT, "rate_exceeded" },
143 { PEVNT_DENY & ~PEER_EVENT, "access_denied" },
144 { PEVNT_ARMED & ~PEER_EVENT, "leap_armed" },
145 { PEVNT_NEWPEER & ~PEER_EVENT, "sys_peer" },
146 { PEVNT_CLOCK & ~PEER_EVENT, "clock_event" },
147 { PEVNT_AUTH & ~PEER_EVENT, "bad_auth" },
148 { PEVNT_POPCORN & ~PEER_EVENT, "popcorn" },
149 { PEVNT_XLEAVE & ~PEER_EVENT, "interleave_mode" },
150 { PEVNT_XERR & ~PEER_EVENT, "interleave_error" },
157 static const struct codestring peer_st_bits[] = {
158 { CTL_PST_CONFIG, "conf" },
159 { CTL_PST_AUTHENABLE, "authenb" },
160 { CTL_PST_AUTHENTIC, "auth" },
161 { CTL_PST_REACH, "reach" },
162 { CTL_PST_BCAST, "bcast" },
163 /* not used with getcode(), no terminating entry needed */
167 * Restriction match bits
169 static const struct codestring res_match_bits[] = {
170 { RESM_NTPONLY, "ntpport" },
171 { RESM_INTERFACE, "interface" },
172 { RESM_SOURCE, "source" },
173 /* not used with getcode(), no terminating entry needed */
177 * Restriction access bits
179 static const struct codestring res_access_bits[] = {
180 { RES_IGNORE, "ignore" },
181 { RES_DONTSERVE, "noserve" },
182 { RES_DONTTRUST, "notrust" },
183 { RES_NOQUERY, "noquery" },
184 { RES_NOMODIFY, "nomodify" },
185 { RES_NOPEER, "nopeer" },
186 { RES_NOTRAP, "notrap" },
187 { RES_LPTRAP, "lptrap" },
188 { RES_LIMITED, "limited" },
189 { RES_VERSION, "version" },
191 { RES_FLAKE, "flake" },
192 /* not used with getcode(), no terminating entry needed */
197 * Crypto events (cryp)
199 static const struct codestring crypto_codes[] = {
200 { XEVNT_OK & ~CRPT_EVENT, "success" },
201 { XEVNT_LEN & ~CRPT_EVENT, "bad_field_format_or_length" },
202 { XEVNT_TSP & ~CRPT_EVENT, "bad_timestamp" },
203 { XEVNT_FSP & ~CRPT_EVENT, "bad_filestamp" },
204 { XEVNT_PUB & ~CRPT_EVENT, "bad_or_missing_public_key" },
205 { XEVNT_MD & ~CRPT_EVENT, "unsupported_digest_type" },
206 { XEVNT_KEY & ~CRPT_EVENT, "unsupported_identity_type" },
207 { XEVNT_SGL & ~CRPT_EVENT, "bad_signature_length" },
208 { XEVNT_SIG & ~CRPT_EVENT, "signature_not_verified" },
209 { XEVNT_VFY & ~CRPT_EVENT, "certificate_not_verified" },
210 { XEVNT_PER & ~CRPT_EVENT, "host_certificate_expired" },
211 { XEVNT_CKY & ~CRPT_EVENT, "bad_or_missing_cookie" },
212 { XEVNT_DAT & ~CRPT_EVENT, "bad_or_missing_leapseconds" },
213 { XEVNT_CRT & ~CRPT_EVENT, "bad_or_missing_certificate" },
214 { XEVNT_ID & ~CRPT_EVENT, "bad_or_missing_group key" },
215 { XEVNT_ERR & ~CRPT_EVENT, "protocol_error" },
222 * kernel discipline status bits
224 static const struct codestring k_st_bits[] = {
229 { STA_PPSFREQ, "ppsfreq" },
232 { STA_PPSTIME, "ppstime" },
244 { STA_UNSYNC, "unsync" },
247 { STA_FREQHOLD, "freqhold" },
249 # ifdef STA_PPSSIGNAL
250 { STA_PPSSIGNAL, "ppssignal" },
252 # ifdef STA_PPSJITTER
253 { STA_PPSJITTER, "ppsjitter" },
255 # ifdef STA_PPSWANDER
256 { STA_PPSWANDER, "ppswander" },
259 { STA_PPSERROR, "ppserror" },
262 { STA_CLOCKERR, "clockerr" },
265 { STA_NANO, "nano" },
268 { STA_MODE, "mode=fll" },
271 { STA_CLK, "src=B" },
273 /* not used with getcode(), no terminating entry needed */
275 #endif /* KERNEL_PLL */
278 static const char * getcode(int, const struct codestring *);
279 static const char * getevents(int);
280 static const char * peer_st_flags(u_char pst);
283 * getcode - return string corresponding to code
288 const struct codestring * codetab
293 while (codetab->code != -1) {
294 if (codetab->code == code)
295 return codetab->string;
300 snprintf(buf, LIB_BUFLENGTH, "%s_%d", codetab->string, code);
306 * getevents - return a descriptive string for the event count
319 snprintf(buf, LIB_BUFLENGTH, "%d event%s", cnt,
331 * returns a human-readable string with a keyword from tab for each bit
332 * set in bits, separating multiple entries with text of sep2.
338 const struct codestring * tab,
348 int saved_errno; /* for use in DPRINTF with %m */
353 lim = buf + LIB_BUFLENGTH;
356 for (b = 0; b < tab_ct; b++) {
357 if (tab[b].code & bits) {
358 rc = snprintf(pch, (lim - pch), "%s%s", sep,
372 snprintf(buf, LIB_BUFLENGTH,
373 "decode_bitflags(%s) can't decode 0x%x in %d bytes",
374 (tab == peer_st_bits)
383 bits, (int)LIB_BUFLENGTH);
395 return decode_bitflags(pst, ", ", peer_st_bits,
396 COUNTOF(peer_st_bits));
405 return decode_bitflags(mf, " ", res_match_bits,
406 COUNTOF(res_match_bits));
415 return decode_bitflags(af, " ", res_access_bits,
416 COUNTOF(res_access_bits));
426 return decode_bitflags(st, " ", k_st_bits, COUNTOF(k_st_bits));
428 #endif /* KERNEL_PLL */
432 * statustoa - return a descriptive string for a peer status
449 snprintf(cb, LIB_BUFLENGTH, "%s, %s, %s, %s",
450 getcode(CTL_SYS_LI(st), leap_codes),
451 getcode(CTL_SYS_SOURCE(st), sync_codes),
452 getevents(CTL_SYS_NEVNT(st)),
453 getcode(CTL_SYS_EVENT(st), sys_codes));
457 pst = (u_char)CTL_PEER_STATVAL(st);
458 snprintf(cb, LIB_BUFLENGTH, "%s, %s, %s",
460 getcode(pst & 0x7, select_codes),
461 getevents(CTL_PEER_NEVNT(st)));
462 if (CTL_PEER_EVENT(st) != EVNT_UNSPEC) {
463 cc = cb + strlen(cb);
464 snprintf(cc, LIB_BUFLENGTH - (cc - cb), ", %s",
465 getcode(CTL_PEER_EVENT(st),
471 snprintf(cb, LIB_BUFLENGTH, "%s, %s",
472 getevents(CTL_SYS_NEVNT(st)),
473 getcode((st) & 0xf, clock_codes));
485 if (num & PEER_EVENT)
486 return (getcode(num & ~PEER_EVENT, peer_codes));
488 else if (num & CRPT_EVENT)
489 return (getcode(num & ~CRPT_EVENT, crypto_codes));
492 return (getcode(num, sys_codes));
500 return getcode(num, clock_codes);