3
0

adjtimex.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 tarball for details.
  12. */
  13. #include "libbb.h"
  14. #include <sys/timex.h>
  15. static const uint16_t statlist_bit[] = {
  16. STA_PLL,
  17. STA_PPSFREQ,
  18. STA_PPSTIME,
  19. STA_FLL,
  20. STA_INS,
  21. STA_DEL,
  22. STA_UNSYNC,
  23. STA_FREQHOLD,
  24. STA_PPSSIGNAL,
  25. STA_PPSJITTER,
  26. STA_PPSWANDER,
  27. STA_PPSERROR,
  28. STA_CLOCKERR,
  29. 0
  30. };
  31. static const char statlist_name[] =
  32. "PLL" "\0"
  33. "PPSFREQ" "\0"
  34. "PPSTIME" "\0"
  35. "FFL" "\0"
  36. "INS" "\0"
  37. "DEL" "\0"
  38. "UNSYNC" "\0"
  39. "FREQHOLD" "\0"
  40. "PPSSIGNAL" "\0"
  41. "PPSJITTER" "\0"
  42. "PPSWANDER" "\0"
  43. "PPSERROR" "\0"
  44. "CLOCKERR"
  45. ;
  46. static const char ret_code_descript[] =
  47. "clock synchronized" "\0"
  48. "insert leap second" "\0"
  49. "delete leap second" "\0"
  50. "leap second in progress" "\0"
  51. "leap second has occurred" "\0"
  52. "clock not synchronized"
  53. ;
  54. int adjtimex_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  55. int adjtimex_main(int argc, char **argv)
  56. {
  57. enum {
  58. OPT_quiet = 0x1
  59. };
  60. unsigned opt;
  61. char *opt_o, *opt_f, *opt_p, *opt_t;
  62. struct timex txc;
  63. int i, ret;
  64. const char *descript;
  65. txc.modes=0;
  66. opt = getopt32(argv, "qo:f:p:t:",
  67. &opt_o, &opt_f, &opt_p, &opt_t);
  68. //if (opt & 0x1) // -q
  69. if (opt & 0x2) { // -o
  70. txc.offset = xatol(opt_o);
  71. txc.modes |= ADJ_OFFSET_SINGLESHOT;
  72. }
  73. if (opt & 0x4) { // -f
  74. txc.freq = xatol(opt_f);
  75. txc.modes |= ADJ_FREQUENCY;
  76. }
  77. if (opt & 0x8) { // -p
  78. txc.constant = xatol(opt_p);
  79. txc.modes |= ADJ_TIMECONST;
  80. }
  81. if (opt & 0x10) { // -t
  82. txc.tick = xatol(opt_t);
  83. txc.modes |= ADJ_TICK;
  84. }
  85. if (argc != optind) { /* no valid non-option parameters */
  86. bb_show_usage();
  87. }
  88. ret = adjtimex(&txc);
  89. if (ret < 0) {
  90. bb_perror_nomsg_and_die();
  91. }
  92. if (!(opt & OPT_quiet)) {
  93. int sep;
  94. const char *name;
  95. printf(
  96. " mode: %d\n"
  97. "-o offset: %ld\n"
  98. "-f frequency: %ld\n"
  99. " maxerror: %ld\n"
  100. " esterror: %ld\n"
  101. " status: %d (",
  102. txc.modes, txc.offset, txc.freq, txc.maxerror,
  103. txc.esterror, txc.status);
  104. /* representative output of next code fragment:
  105. "PLL | PPSTIME" */
  106. name = statlist_name;
  107. sep = 0;
  108. for (i = 0; statlist_bit[i]; i++) {
  109. if (txc.status & statlist_bit[i]) {
  110. if (sep)
  111. fputs(" | ", stdout);
  112. fputs(name, stdout);
  113. sep = 1;
  114. }
  115. name += strlen(name) + 1;
  116. }
  117. descript = "error";
  118. if (ret <= 5)
  119. descript = nth_string(ret_code_descript, ret);
  120. printf(")\n"
  121. "-p timeconstant: %ld\n"
  122. " precision: %ld\n"
  123. " tolerance: %ld\n"
  124. "-t tick: %ld\n"
  125. " time.tv_sec: %ld\n"
  126. " time.tv_usec: %ld\n"
  127. " return value: %d (%s)\n",
  128. txc.constant,
  129. txc.precision, txc.tolerance, txc.tick,
  130. (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
  131. }
  132. return 0;
  133. }