adjtimex.c 4.2 KB

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