adjtimex.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. //config:config ADJTIMEX
  14. //config: bool "adjtimex (4.9 kb)"
  15. //config: default y
  16. //config: help
  17. //config: Adjtimex reads and optionally sets adjustment parameters for
  18. //config: the Linux clock adjustment algorithm.
  19. //applet:IF_ADJTIMEX(APPLET_NOFORK(adjtimex, adjtimex, BB_DIR_SBIN, BB_SUID_DROP, adjtimex))
  20. //kbuild:lib-$(CONFIG_ADJTIMEX) += adjtimex.o
  21. //usage:#define adjtimex_trivial_usage
  22. //usage: "[-q] [-o OFS] [-f FREQ] [-p TCONST] [-t TICK]"
  23. //usage:#define adjtimex_full_usage "\n\n"
  24. //usage: "Read or set kernel time variables. See adjtimex(2)\n"
  25. //usage: "\n -q Quiet"
  26. //usage: "\n -o OFF Time offset, microseconds"
  27. //usage: "\n -f FREQ Frequency adjust, integer kernel units (65536 is 1ppm)"
  28. //usage: "\n -t TICK Microseconds per tick, usually 10000"
  29. //usage: "\n (positive -t or -f values make clock run faster)"
  30. //usage: "\n -p TCONST"
  31. #include "libbb.h"
  32. #ifdef __BIONIC__
  33. # include <linux/timex.h>
  34. #else
  35. # include <sys/timex.h>
  36. #endif
  37. static const uint16_t statlist_bit[] ALIGN2 = {
  38. STA_PLL,
  39. STA_PPSFREQ,
  40. STA_PPSTIME,
  41. STA_FLL,
  42. STA_INS,
  43. STA_DEL,
  44. STA_UNSYNC,
  45. STA_FREQHOLD,
  46. STA_PPSSIGNAL,
  47. STA_PPSJITTER,
  48. STA_PPSWANDER,
  49. STA_PPSERROR,
  50. STA_CLOCKERR,
  51. 0
  52. };
  53. static const char statlist_name[] ALIGN1 =
  54. "PLL" "\0"
  55. "PPSFREQ" "\0"
  56. "PPSTIME" "\0"
  57. "FFL" "\0"
  58. "INS" "\0"
  59. "DEL" "\0"
  60. "UNSYNC" "\0"
  61. "FREQHOLD" "\0"
  62. "PPSSIGNAL" "\0"
  63. "PPSJITTER" "\0"
  64. "PPSWANDER" "\0"
  65. "PPSERROR" "\0"
  66. "CLOCKERR"
  67. ;
  68. static const char ret_code_descript[] ALIGN1 =
  69. "clock synchronized" "\0"
  70. "insert leap second" "\0"
  71. "delete leap second" "\0"
  72. "leap second in progress" "\0"
  73. "leap second has occurred" "\0"
  74. "clock not synchronized"
  75. ;
  76. int adjtimex_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  77. int adjtimex_main(int argc UNUSED_PARAM, char **argv)
  78. {
  79. enum {
  80. OPT_quiet = 0x1
  81. };
  82. unsigned opt;
  83. char *opt_o, *opt_f, *opt_p, *opt_t;
  84. struct timex txc;
  85. int ret;
  86. const char *descript;
  87. memset(&txc, 0, sizeof(txc));
  88. opt = getopt32(argv, "^" "qo:f:p:t:"
  89. "\0" "=0"/*no valid non-option args*/,
  90. &opt_o, &opt_f, &opt_p, &opt_t
  91. );
  92. //if (opt & 0x1) // -q
  93. if (opt & 0x2) { // -o
  94. txc.offset = xatol(opt_o);
  95. txc.modes |= ADJ_OFFSET_SINGLESHOT;
  96. }
  97. if (opt & 0x4) { // -f
  98. txc.freq = xatol(opt_f);
  99. txc.modes |= ADJ_FREQUENCY;
  100. }
  101. if (opt & 0x8) { // -p
  102. txc.constant = xatol(opt_p);
  103. txc.modes |= ADJ_TIMECONST;
  104. }
  105. if (opt & 0x10) { // -t
  106. txc.tick = xatol(opt_t);
  107. txc.modes |= ADJ_TICK;
  108. }
  109. /* It's NOFORK applet because the code is very simple:
  110. * just some printf. No opens, no allocs.
  111. * If you need to make it more complex, feel free to downgrade to NOEXEC
  112. */
  113. ret = adjtimex(&txc);
  114. if (ret < 0)
  115. bb_perror_nomsg_and_die();
  116. if (!(opt & OPT_quiet)) {
  117. const char *sep;
  118. const char *name;
  119. int i;
  120. printf(
  121. " mode: %d\n"
  122. "-o offset: %ld us\n"
  123. "-f freq.adjust: %ld (65536 = 1ppm)\n"
  124. " maxerror: %ld\n"
  125. " esterror: %ld\n"
  126. " status: %d (",
  127. txc.modes, txc.offset, txc.freq, txc.maxerror,
  128. txc.esterror, txc.status
  129. );
  130. /* representative output of next code fragment:
  131. * "PLL | PPSTIME"
  132. */
  133. name = statlist_name;
  134. sep = "";
  135. for (i = 0; statlist_bit[i]; i++) {
  136. if (txc.status & statlist_bit[i]) {
  137. printf("%s%s", sep, name);
  138. sep = " | ";
  139. }
  140. name += strlen(name) + 1;
  141. }
  142. descript = "error";
  143. if (ret <= 5)
  144. descript = nth_string(ret_code_descript, ret);
  145. printf(")\n"
  146. "-p timeconstant: %ld\n"
  147. " precision: %ld us\n"
  148. " tolerance: %ld\n"
  149. "-t tick: %ld us\n"
  150. " time.tv_sec: %ld\n"
  151. " time.tv_usec: %ld\n"
  152. " return value: %d (%s)\n",
  153. txc.constant,
  154. txc.precision, txc.tolerance, txc.tick,
  155. (long)txc.time.tv_sec, (long)txc.time.tv_usec,
  156. ret, descript
  157. );
  158. }
  159. return 0;
  160. }