/contrib/ntp/util/ntptime.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 440 lines · 380 code · 27 blank · 33 comment · 63 complexity · 00aabbc29b6f880b2c53e65b9ff15783 MD5 · raw file

  1. /*
  2. * NTP test program
  3. *
  4. * This program tests to see if the NTP user interface routines
  5. * ntp_gettime() and ntp_adjtime() have been implemented in the kernel.
  6. * If so, each of these routines is called to display current timekeeping
  7. * data.
  8. *
  9. * For more information, see the README.kern file in the doc directory
  10. * of the xntp3 distribution.
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. # include <config.h>
  14. #endif /* HAVE_CONFIG_H */
  15. #include "ntp_fp.h"
  16. #include "ntp_unixtime.h"
  17. #include "ntp_syscall.h"
  18. #include "ntp_stdlib.h"
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include <signal.h>
  22. #include <setjmp.h>
  23. #ifdef NTP_SYSCALLS_STD
  24. # ifndef SYS_DECOSF1
  25. # define BADCALL -1 /* this is supposed to be a bad syscall */
  26. # endif /* SYS_DECOSF1 */
  27. #endif
  28. #ifdef HAVE_STRUCT_NTPTIMEVAL_TIME_TV_NSEC
  29. #define tv_frac_sec tv_nsec
  30. #else
  31. #define tv_frac_sec tv_usec
  32. #endif
  33. #define TIMEX_MOD_BITS \
  34. "\20\1OFFSET\2FREQUENCY\3MAXERROR\4ESTERROR\5STATUS\6TIMECONST\
  35. \13PLL\14FLL\15MICRO\16NANO\17CLKB\20CLKA"
  36. #define TIMEX_STA_BITS \
  37. "\20\1PLL\2PPSFREQ\3PPSTIME\4FLL\5INS\6DEL\7UNSYNC\10FREQHOLD\
  38. \11PPSSIGNAL\12PPSJITTER\13PPSWANDER\14PPSERROR\15CLOCKERR\
  39. \16NANO\17MODE\20CLK"
  40. #define SCALE_FREQ 65536 /* frequency scale */
  41. /*
  42. * Function prototypes
  43. */
  44. char *sprintb P((u_int, const char *));
  45. const char *timex_state P((int));
  46. #ifdef SIGSYS
  47. void pll_trap P((int));
  48. static struct sigaction newsigsys; /* new sigaction status */
  49. static struct sigaction sigsys; /* current sigaction status */
  50. static sigjmp_buf env; /* environment var. for pll_trap() */
  51. #endif
  52. static volatile int pll_control; /* (0) daemon, (1) kernel loop */
  53. static volatile int status; /* most recent status bits */
  54. static volatile int flash; /* most recent ntp_adjtime() bits */
  55. char* progname;
  56. static char optargs[] = "MNT:cde:f:hm:o:rs:t:";
  57. int
  58. main(
  59. int argc,
  60. char *argv[]
  61. )
  62. {
  63. extern int ntp_optind;
  64. extern char *ntp_optarg;
  65. #ifdef SUBST_ADJTIMEX
  66. struct timex ntv;
  67. #else
  68. struct ntptimeval ntv;
  69. #endif
  70. struct timeval tv;
  71. struct timex ntx, _ntx;
  72. int times[20];
  73. double ftemp, gtemp, htemp;
  74. long time_frac; /* ntv.time.tv_frac_sec (us/ns) */
  75. l_fp ts;
  76. volatile unsigned ts_mask = TS_MASK; /* defaults to 20 bits (us) */
  77. volatile unsigned ts_roundbit = TS_ROUNDBIT; /* defaults to 20 bits (us) */
  78. volatile int fdigits = 6; /* fractional digits for us */
  79. int c;
  80. int errflg = 0;
  81. int cost = 0;
  82. volatile int rawtime = 0;
  83. memset((char *)&ntx, 0, sizeof(ntx));
  84. progname = argv[0];
  85. while ((c = ntp_getopt(argc, argv, optargs)) != EOF) switch (c) {
  86. #ifdef MOD_MICRO
  87. case 'M':
  88. ntx.modes |= MOD_MICRO;
  89. break;
  90. #endif
  91. #ifdef MOD_NANO
  92. case 'N':
  93. ntx.modes |= MOD_NANO;
  94. break;
  95. #endif
  96. #ifdef NTP_API
  97. # if NTP_API > 3
  98. case 'T':
  99. ntx.modes = MOD_TAI;
  100. ntx.constant = atoi(ntp_optarg);
  101. break;
  102. # endif
  103. #endif
  104. case 'c':
  105. cost++;
  106. break;
  107. case 'e':
  108. ntx.modes |= MOD_ESTERROR;
  109. ntx.esterror = atoi(ntp_optarg);
  110. break;
  111. case 'f':
  112. ntx.modes |= MOD_FREQUENCY;
  113. ntx.freq = (long)(atof(ntp_optarg) * SCALE_FREQ);
  114. break;
  115. case 'm':
  116. ntx.modes |= MOD_MAXERROR;
  117. ntx.maxerror = atoi(ntp_optarg);
  118. break;
  119. case 'o':
  120. ntx.modes |= MOD_OFFSET;
  121. ntx.offset = atoi(ntp_optarg);
  122. break;
  123. case 'r':
  124. rawtime++;
  125. break;
  126. case 's':
  127. ntx.modes |= MOD_STATUS;
  128. ntx.status = atoi(ntp_optarg);
  129. if (ntx.status < 0 || ntx.status >= 0x100) errflg++;
  130. break;
  131. case 't':
  132. ntx.modes |= MOD_TIMECONST;
  133. ntx.constant = atoi(ntp_optarg);
  134. break;
  135. default:
  136. errflg++;
  137. }
  138. if (errflg || (ntp_optind != argc)) {
  139. (void) fprintf(stderr,
  140. "usage: %s [-%s]\n\n\
  141. %s%s%s\
  142. -c display the time taken to call ntp_gettime (us)\n\
  143. -e esterror estimate of the error (us)\n\
  144. -f frequency Frequency error (-500 .. 500) (ppm)\n\
  145. -h display this help info\n\
  146. -m maxerror max possible error (us)\n\
  147. -o offset current offset (ms)\n\
  148. -r print the unix and NTP time raw\n\
  149. -s status Set the status bits\n\
  150. -t timeconstant log2 of PLL time constant (0 .. %d)\n",
  151. progname, optargs,
  152. #ifdef MOD_MICRO
  153. "-M switch to microsecond mode\n",
  154. #else
  155. "",
  156. #endif
  157. #ifdef MOD_NANO
  158. "-N switch to nanosecond mode\n",
  159. #else
  160. "",
  161. #endif
  162. #ifdef NTP_API
  163. # if NTP_API > 3
  164. "-T tai_offset set TAI offset\n",
  165. # else
  166. "",
  167. # endif
  168. #else
  169. "",
  170. #endif
  171. MAXTC);
  172. exit(2);
  173. }
  174. #ifdef SIGSYS
  175. /*
  176. * Test to make sure the sigaction() works in case of invalid
  177. * syscall codes.
  178. */
  179. newsigsys.sa_handler = pll_trap;
  180. newsigsys.sa_flags = 0;
  181. if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
  182. perror("sigaction() fails to save SIGSYS trap");
  183. exit(1);
  184. }
  185. #endif /* SIGSYS */
  186. #ifdef BADCALL
  187. /*
  188. * Make sure the trapcatcher works.
  189. */
  190. pll_control = 1;
  191. #ifdef SIGSYS
  192. if (sigsetjmp(env, 1) == 0)
  193. {
  194. #endif
  195. status = syscall(BADCALL, &ntv); /* dummy parameter */
  196. if ((status < 0) && (errno == ENOSYS))
  197. --pll_control;
  198. #ifdef SIGSYS
  199. }
  200. #endif
  201. if (pll_control)
  202. printf("sigaction() failed to catch an invalid syscall\n");
  203. #endif /* BADCALL */
  204. if (cost) {
  205. #ifdef SIGSYS
  206. if (sigsetjmp(env, 1) == 0) {
  207. #endif
  208. for (c = 0; c < sizeof times / sizeof times[0]; c++) {
  209. status = ntp_gettime(&ntv);
  210. if ((status < 0) && (errno == ENOSYS))
  211. --pll_control;
  212. if (pll_control < 0)
  213. break;
  214. times[c] = ntv.time.tv_frac_sec;
  215. }
  216. #ifdef SIGSYS
  217. }
  218. #endif
  219. if (pll_control >= 0) {
  220. printf("[ us %06d:", times[0]);
  221. for (c = 1; c < sizeof times / sizeof times[0]; c++)
  222. printf(" %d", times[c] - times[c - 1]);
  223. printf(" ]\n");
  224. }
  225. }
  226. #ifdef SIGSYS
  227. if (sigsetjmp(env, 1) == 0) {
  228. #endif
  229. status = ntp_gettime(&ntv);
  230. if ((status < 0) && (errno == ENOSYS))
  231. --pll_control;
  232. #ifdef SIGSYS
  233. }
  234. #endif
  235. _ntx.modes = 0; /* Ensure nothing is set */
  236. #ifdef SIGSYS
  237. if (sigsetjmp(env, 1) == 0) {
  238. #endif
  239. status = ntp_adjtime(&_ntx);
  240. if ((status < 0) && (errno == ENOSYS))
  241. --pll_control;
  242. flash = _ntx.status;
  243. #ifdef SIGSYS
  244. }
  245. #endif
  246. if (pll_control < 0) {
  247. printf("NTP user interface routines are not configured in this kernel.\n");
  248. goto lexit;
  249. }
  250. /*
  251. * Fetch timekeeping data and display.
  252. */
  253. status = ntp_gettime(&ntv);
  254. if (status < 0)
  255. perror("ntp_gettime() call fails");
  256. else {
  257. printf("ntp_gettime() returns code %d (%s)\n",
  258. status, timex_state(status));
  259. time_frac = ntv.time.tv_frac_sec;
  260. #ifdef STA_NANO
  261. if (flash & STA_NANO) {
  262. ntv.time.tv_frac_sec /= 1000;
  263. ts_mask = 0xfffffffc; /* 1/2^30 */
  264. ts_roundbit = 0x00000002;
  265. fdigits = 9;
  266. }
  267. #endif
  268. tv.tv_sec = ntv.time.tv_sec;
  269. tv.tv_usec = ntv.time.tv_frac_sec;
  270. TVTOTS(&tv, &ts);
  271. ts.l_ui += JAN_1970;
  272. ts.l_uf += ts_roundbit;
  273. ts.l_uf &= ts_mask;
  274. printf(" time %s, (.%0*d),\n",
  275. prettydate(&ts), fdigits, (int) time_frac);
  276. printf(" maximum error %lu us, estimated error %lu us",
  277. (u_long)ntv.maxerror, (u_long)ntv.esterror);
  278. if (rawtime)
  279. printf(" ntptime=%x.%x unixtime=%x.%0*d %s",
  280. (unsigned int) ts.l_ui, (unsigned int) ts.l_uf,
  281. (int) ntv.time.tv_sec, fdigits, (int) time_frac,
  282. ctime((const time_t *) &ntv.time.tv_sec));
  283. #if NTP_API > 3
  284. printf(", TAI offset %ld\n", (long)ntv.tai);
  285. #else
  286. printf("\n");
  287. #endif /* NTP_API */
  288. }
  289. status = ntp_adjtime(&ntx);
  290. if (status < 0)
  291. perror((errno == EPERM) ?
  292. "Must be root to set kernel values\nntp_adjtime() call fails" :
  293. "ntp_adjtime() call fails");
  294. else {
  295. flash = ntx.status;
  296. printf("ntp_adjtime() returns code %d (%s)\n",
  297. status, timex_state(status));
  298. printf(" modes %s,\n", sprintb(ntx.modes, TIMEX_MOD_BITS));
  299. ftemp = (double)ntx.offset;
  300. #ifdef STA_NANO
  301. if (flash & STA_NANO)
  302. ftemp /= 1000.0;
  303. #endif
  304. printf(" offset %.3f", ftemp);
  305. ftemp = (double)ntx.freq / SCALE_FREQ;
  306. printf(" us, frequency %.3f ppm, interval %d s,\n",
  307. ftemp, 1 << ntx.shift);
  308. printf(" maximum error %lu us, estimated error %lu us,\n",
  309. (u_long)ntx.maxerror, (u_long)ntx.esterror);
  310. printf(" status %s,\n", sprintb((u_int)ntx.status, TIMEX_STA_BITS));
  311. ftemp = (double)ntx.tolerance / SCALE_FREQ;
  312. gtemp = (double)ntx.precision;
  313. #ifdef STA_NANO
  314. if (flash & STA_NANO)
  315. gtemp /= 1000.0;
  316. #endif
  317. printf(
  318. " time constant %lu, precision %.3f us, tolerance %.0f ppm,\n",
  319. (u_long)ntx.constant, gtemp, ftemp);
  320. if (ntx.shift == 0)
  321. exit (0);
  322. ftemp = (double)ntx.ppsfreq / SCALE_FREQ;
  323. gtemp = (double)ntx.stabil / SCALE_FREQ;
  324. htemp = (double)ntx.jitter;
  325. #ifdef STA_NANO
  326. if (flash & STA_NANO)
  327. htemp /= 1000.0;
  328. #endif
  329. printf(
  330. " pps frequency %.3f ppm, stability %.3f ppm, jitter %.3f us,\n",
  331. ftemp, gtemp, htemp);
  332. printf(" intervals %lu, jitter exceeded %lu, stability exceeded %lu, errors %lu.\n",
  333. (u_long)ntx.calcnt, (u_long)ntx.jitcnt,
  334. (u_long)ntx.stbcnt, (u_long)ntx.errcnt);
  335. return (0);
  336. }
  337. /*
  338. * Put things back together the way we found them.
  339. */
  340. lexit:
  341. #ifdef SIGSYS
  342. if (sigaction(SIGSYS, &sigsys, (struct sigaction *)NULL)) {
  343. perror("sigaction() fails to restore SIGSYS trap");
  344. exit(1);
  345. }
  346. #endif
  347. exit(0);
  348. }
  349. #ifdef SIGSYS
  350. /*
  351. * pll_trap - trap processor for undefined syscalls
  352. */
  353. void
  354. pll_trap(
  355. int arg
  356. )
  357. {
  358. pll_control--;
  359. siglongjmp(env, 1);
  360. }
  361. #endif
  362. /*
  363. * Print a value a la the %b format of the kernel's printf
  364. */
  365. char *
  366. sprintb(
  367. register u_int v,
  368. register const char *bits
  369. )
  370. {
  371. register char *cp;
  372. register int i, any = 0;
  373. register char c;
  374. static char buf[132];
  375. if (bits && *bits == 8)
  376. (void)sprintf(buf, "0%o", v);
  377. else
  378. (void)sprintf(buf, "0x%x", v);
  379. cp = buf + strlen(buf);
  380. if (bits) {
  381. bits++;
  382. *cp++ = ' ';
  383. *cp++ = '(';
  384. while ((i = *bits++) != 0) {
  385. if (v & (1 << (i-1))) {
  386. if (any)
  387. *cp++ = ',';
  388. any = 1;
  389. for (; (c = *bits) > 32; bits++)
  390. *cp++ = c;
  391. } else
  392. for (; *bits > 32; bits++)
  393. continue;
  394. }
  395. *cp++ = ')';
  396. }
  397. *cp = '\0';
  398. return (buf);
  399. }
  400. const char *timex_states[] = {
  401. "OK", "INS", "DEL", "OOP", "WAIT", "ERROR"
  402. };
  403. const char *
  404. timex_state(
  405. register int s
  406. )
  407. {
  408. static char buf[32];
  409. if (s >= 0 && s < sizeof(timex_states) / sizeof(timex_states[0]))
  410. return (timex_states[s]);
  411. sprintf(buf, "TIME-#%d", s);
  412. return (buf);
  413. }