adjtimex.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #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 UNUSED_PARAM, 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. opt_complementary = "=0"; /* no valid non-option parameters */
  66. opt = getopt32(argv, "qo:f:p:t:",
  67. &opt_o, &opt_f, &opt_p, &opt_t);
  68. txc.modes = 0;
  69. //if (opt & 0x1) // -q
  70. if (opt & 0x2) { // -o
  71. txc.offset = xatol(opt_o);
  72. txc.modes |= ADJ_OFFSET_SINGLESHOT;
  73. }
  74. if (opt & 0x4) { // -f
  75. txc.freq = xatol(opt_f);
  76. txc.modes |= ADJ_FREQUENCY;
  77. }
  78. if (opt & 0x8) { // -p
  79. txc.constant = xatol(opt_p);
  80. txc.modes |= ADJ_TIMECONST;
  81. }
  82. if (opt & 0x10) { // -t
  83. txc.tick = xatol(opt_t);
  84. txc.modes |= ADJ_TICK;
  85. }
  86. ret = adjtimex(&txc);
  87. if (ret < 0) {
  88. bb_perror_nomsg_and_die();
  89. }
  90. if (!(opt & OPT_quiet)) {
  91. int sep;
  92. const char *name;
  93. printf(
  94. " mode: %d\n"
  95. "-o offset: %ld\n"
  96. "-f frequency: %ld\n"
  97. " maxerror: %ld\n"
  98. " esterror: %ld\n"
  99. " status: %d (",
  100. txc.modes, txc.offset, txc.freq, txc.maxerror,
  101. txc.esterror, txc.status);
  102. /* representative output of next code fragment:
  103. "PLL | PPSTIME" */
  104. name = statlist_name;
  105. sep = 0;
  106. for (i = 0; statlist_bit[i]; i++) {
  107. if (txc.status & statlist_bit[i]) {
  108. if (sep)
  109. fputs(" | ", stdout);
  110. fputs(name, stdout);
  111. sep = 1;
  112. }
  113. name += strlen(name) + 1;
  114. }
  115. descript = "error";
  116. if (ret <= 5)
  117. descript = nth_string(ret_code_descript, ret);
  118. printf(")\n"
  119. "-p timeconstant: %ld\n"
  120. " precision: %ld\n"
  121. " tolerance: %ld\n"
  122. "-t tick: %ld\n"
  123. " time.tv_sec: %ld\n"
  124. " time.tv_usec: %ld\n"
  125. " return value: %d (%s)\n",
  126. txc.constant,
  127. txc.precision, txc.tolerance, txc.tick,
  128. (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
  129. }
  130. return 0;
  131. }