adjtimex.c 3.6 KB

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