1 /* 2 * pretty printing of status information 3 */ 4 #ifdef HAVE_CONFIG_H 5 #include <config.h> 6 #endif 7 #include <stdio.h> 8 #include "ntp_stdlib.h" 9 #include "ntp_fp.h" 10 #include "ntp.h" 11 #include "lib_strbuf.h" 12 #include "ntp_refclock.h" 13 #include "ntp_control.h" 14 #include "ntp_string.h" 15 #ifdef KERNEL_PLL 16 # include "ntp_syscall.h" 17 #endif 18 19 20 /* 21 * Structure for turning various constants into a readable string. 22 */ 23 struct codestring { 24 int code; 25 const char * const string; 26 }; 27 28 /* 29 * Leap status (leap) 30 */ 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" }, 36 { -1, "leap" } 37 }; 38 39 /* 40 * Clock source status (sync) 41 */ 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" }, 53 { -1, "sync" } 54 }; 55 56 /* 57 * Peer selection status (sel) 58 */ 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" }, 68 { -1, "sel" } 69 }; 70 71 /* 72 * Clock status (clk) 73 */ 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" }, 82 { -1, "clk" } 83 }; 84 85 86 #ifdef FLASH_CODES_UNUSED 87 /* 88 * Flash bits -- see ntpq.c tstflags & tstflagnames 89 */ 90 static const struct codestring flash_codes[] = { 91 { TEST1, "pkt_dup" }, 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" }, 104 { -1, "flash" } 105 }; 106 #endif 107 108 109 /* 110 * System events (sys) 111 */ 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" }, 127 { EVNT_TAI, "TAI" }, 128 { EVNT_LEAPVAL, "stale_leapsecond_values" }, 129 { -1, "" } 130 }; 131 132 /* 133 * Peer events (peer) 134 */ 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" }, 151 { -1, "" } 152 }; 153 154 /* 155 * Peer status bits 156 */ 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 */ 164 }; 165 166 /* 167 * Restriction match bits 168 */ 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 */ 174 }; 175 176 /* 177 * Restriction access bits 178 */ 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" }, 190 { RES_KOD, "kod" }, 191 { RES_FLAKE, "flake" }, 192 /* not used with getcode(), no terminating entry needed */ 193 }; 194 195 #ifdef AUTOKEY 196 /* 197 * Crypto events (cryp) 198 */ 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" }, 216 { -1, "" } 217 }; 218 #endif /* AUTOKEY */ 219 220 #ifdef KERNEL_PLL 221 /* 222 * kernel discipline status bits 223 */ 224 static const struct codestring k_st_bits[] = { 225 # ifdef STA_PLL 226 { STA_PLL, "pll" }, 227 # endif 228 # ifdef STA_PPSFREQ 229 { STA_PPSFREQ, "ppsfreq" }, 230 # endif 231 # ifdef STA_PPSTIME 232 { STA_PPSTIME, "ppstime" }, 233 # endif 234 # ifdef STA_FLL 235 { STA_FLL, "fll" }, 236 # endif 237 # ifdef STA_INS 238 { STA_INS, "ins" }, 239 # endif 240 # ifdef STA_DEL 241 { STA_DEL, "del" }, 242 # endif 243 # ifdef STA_UNSYNC 244 { STA_UNSYNC, "unsync" }, 245 # endif 246 # ifdef STA_FREQHOLD 247 { STA_FREQHOLD, "freqhold" }, 248 # endif 249 # ifdef STA_PPSSIGNAL 250 { STA_PPSSIGNAL, "ppssignal" }, 251 # endif 252 # ifdef STA_PPSJITTER 253 { STA_PPSJITTER, "ppsjitter" }, 254 # endif 255 # ifdef STA_PPSWANDER 256 { STA_PPSWANDER, "ppswander" }, 257 # endif 258 # ifdef STA_PPSERROR 259 { STA_PPSERROR, "ppserror" }, 260 # endif 261 # ifdef STA_CLOCKERR 262 { STA_CLOCKERR, "clockerr" }, 263 # endif 264 # ifdef STA_NANO 265 { STA_NANO, "nano" }, 266 # endif 267 # ifdef STA_MODE 268 { STA_MODE, "mode=fll" }, 269 # endif 270 # ifdef STA_CLK 271 { STA_CLK, "src=B" }, 272 # endif 273 /* not used with getcode(), no terminating entry needed */ 274 }; 275 #endif /* KERNEL_PLL */ 276 277 /* Forwards */ 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); 281 282 /* 283 * getcode - return string corresponding to code 284 */ 285 static const char * 286 getcode( 287 int code, 288 const struct codestring * codetab 289 ) 290 { 291 char * buf; 292 293 while (codetab->code != -1) { 294 if (codetab->code == code) 295 return codetab->string; 296 codetab++; 297 } 298 299 LIB_GETBUF(buf); 300 snprintf(buf, LIB_BUFLENGTH, "%s_%d", codetab->string, code); 301 302 return buf; 303 } 304 305 /* 306 * getevents - return a descriptive string for the event count 307 */ 308 static const char * 309 getevents( 310 int cnt 311 ) 312 { 313 char * buf; 314 315 if (cnt == 0) 316 return "no events"; 317 318 LIB_GETBUF(buf); 319 snprintf(buf, LIB_BUFLENGTH, "%d event%s", cnt, 320 (1 == cnt) 321 ? "" 322 : "s"); 323 324 return buf; 325 } 326 327 328 /* 329 * decode_bitflags() 330 * 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. 333 */ 334 static const char * 335 decode_bitflags( 336 int bits, 337 const char * sep2, 338 const struct codestring * tab, 339 size_t tab_ct 340 ) 341 { 342 const char * sep; 343 char * buf; 344 char * pch; 345 char * lim; 346 size_t b; 347 int rc; 348 int saved_errno; /* for use in DPRINTF with %m */ 349 350 saved_errno = errno; 351 LIB_GETBUF(buf); 352 pch = buf; 353 lim = buf + LIB_BUFLENGTH; 354 sep = ""; 355 356 for (b = 0; b < tab_ct; b++) { 357 if (tab[b].code & bits) { 358 size_t avail = lim - pch; 359 rc = snprintf(pch, avail, "%s%s", sep, 360 tab[b].string); 361 if ((size_t)rc >= avail) 362 goto toosmall; 363 pch += rc; 364 sep = sep2; 365 } 366 } 367 368 return buf; 369 370 toosmall: 371 snprintf(buf, LIB_BUFLENGTH, 372 "decode_bitflags(%s) can't decode 0x%x in %d bytes", 373 (tab == peer_st_bits) 374 ? "peer_st" 375 : 376 #ifdef KERNEL_PLL 377 (tab == k_st_bits) 378 ? "kern_st" 379 : 380 #endif 381 "", 382 bits, (int)LIB_BUFLENGTH); 383 errno = saved_errno; 384 385 return buf; 386 } 387 388 389 static const char * 390 peer_st_flags( 391 u_char pst 392 ) 393 { 394 return decode_bitflags(pst, ", ", peer_st_bits, 395 COUNTOF(peer_st_bits)); 396 } 397 398 399 const char * 400 res_match_flags( 401 u_short mf 402 ) 403 { 404 return decode_bitflags(mf, " ", res_match_bits, 405 COUNTOF(res_match_bits)); 406 } 407 408 409 const char * 410 res_access_flags( 411 u_short af 412 ) 413 { 414 return decode_bitflags(af, " ", res_access_bits, 415 COUNTOF(res_access_bits)); 416 } 417 418 419 #ifdef KERNEL_PLL 420 const char * 421 k_st_flags( 422 u_int32 st 423 ) 424 { 425 return decode_bitflags(st, " ", k_st_bits, COUNTOF(k_st_bits)); 426 } 427 #endif /* KERNEL_PLL */ 428 429 430 /* 431 * statustoa - return a descriptive string for a peer status 432 */ 433 char * 434 statustoa( 435 int type, 436 int st 437 ) 438 { 439 char * cb; 440 char * cc; 441 u_char pst; 442 443 LIB_GETBUF(cb); 444 445 switch (type) { 446 447 case TYPE_SYS: 448 snprintf(cb, LIB_BUFLENGTH, "%s, %s, %s, %s", 449 getcode(CTL_SYS_LI(st), leap_codes), 450 getcode(CTL_SYS_SOURCE(st), sync_codes), 451 getevents(CTL_SYS_NEVNT(st)), 452 getcode(CTL_SYS_EVENT(st), sys_codes)); 453 break; 454 455 case TYPE_PEER: 456 pst = (u_char)CTL_PEER_STATVAL(st); 457 snprintf(cb, LIB_BUFLENGTH, "%s, %s, %s", 458 peer_st_flags(pst), 459 getcode(pst & 0x7, select_codes), 460 getevents(CTL_PEER_NEVNT(st))); 461 if (CTL_PEER_EVENT(st) != EVNT_UNSPEC) { 462 cc = cb + strlen(cb); 463 snprintf(cc, LIB_BUFLENGTH - (cc - cb), ", %s", 464 getcode(CTL_PEER_EVENT(st), 465 peer_codes)); 466 } 467 break; 468 469 case TYPE_CLOCK: 470 snprintf(cb, LIB_BUFLENGTH, "%s, %s", 471 getevents(CTL_SYS_NEVNT(st)), 472 getcode((st) & 0xf, clock_codes)); 473 break; 474 } 475 476 return cb; 477 } 478 479 const char * 480 eventstr( 481 int num 482 ) 483 { 484 if (num & PEER_EVENT) 485 return (getcode(num & ~PEER_EVENT, peer_codes)); 486 #ifdef AUTOKEY 487 else if (num & CRPT_EVENT) 488 return (getcode(num & ~CRPT_EVENT, crypto_codes)); 489 #endif /* AUTOKEY */ 490 else 491 return (getcode(num, sys_codes)); 492 } 493 494 const char * 495 ceventstr( 496 int num 497 ) 498 { 499 return getcode(num, clock_codes); 500 } 501