3
0

adjtimex.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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"
  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(adjtimex, BB_DIR_SBIN, BB_SUID_DROP))
  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 i, ret;
  87. const char *descript;
  88. opt_complementary = "=0"; /* no valid non-option parameters */
  89. opt = getopt32(argv, "qo:f:p:t:",
  90. &opt_o, &opt_f, &opt_p, &opt_t);
  91. txc.modes = 0;
  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. ret = adjtimex(&txc);
  110. if (ret < 0) {
  111. bb_perror_nomsg_and_die();
  112. }
  113. if (!(opt & OPT_quiet)) {
  114. const char *sep;
  115. const char *name;
  116. printf(
  117. " mode: %d\n"
  118. "-o offset: %ld us\n"
  119. "-f freq.adjust: %ld (65536 = 1ppm)\n"
  120. " maxerror: %ld\n"
  121. " esterror: %ld\n"
  122. " status: %d (",
  123. txc.modes, txc.offset, txc.freq, txc.maxerror,
  124. txc.esterror, txc.status);
  125. /* representative output of next code fragment:
  126. * "PLL | PPSTIME"
  127. */
  128. name = statlist_name;
  129. sep = "";
  130. for (i = 0; statlist_bit[i]; i++) {
  131. if (txc.status & statlist_bit[i]) {
  132. printf("%s%s", sep, name);
  133. sep = " | ";
  134. }
  135. name += strlen(name) + 1;
  136. }
  137. descript = "error";
  138. if (ret <= 5)
  139. descript = nth_string(ret_code_descript, ret);
  140. printf(")\n"
  141. "-p timeconstant: %ld\n"
  142. " precision: %ld us\n"
  143. " tolerance: %ld\n"
  144. "-t tick: %ld us\n"
  145. " time.tv_sec: %ld\n"
  146. " time.tv_usec: %ld\n"
  147. " return value: %d (%s)\n",
  148. txc.constant,
  149. txc.precision, txc.tolerance, txc.tick,
  150. (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
  151. }
  152. return 0;
  153. }