adjtimex.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 tarball for details.
  12. */
  13. #include "libbb.h"
  14. #include <sys/timex.h>
  15. static const struct {
  16. int bit;
  17. const char *name;
  18. } statlist[] = {
  19. { STA_PLL, "PLL" },
  20. { STA_PPSFREQ, "PPSFREQ" },
  21. { STA_PPSTIME, "PPSTIME" },
  22. { STA_FLL, "FFL" },
  23. { STA_INS, "INS" },
  24. { STA_DEL, "DEL" },
  25. { STA_UNSYNC, "UNSYNC" },
  26. { STA_FREQHOLD, "FREQHOLD" },
  27. { STA_PPSSIGNAL, "PPSSIGNAL" },
  28. { STA_PPSJITTER, "PPSJITTER" },
  29. { STA_PPSWANDER, "PPSWANDER" },
  30. { STA_PPSERROR, "PPSERROR" },
  31. { STA_CLOCKERR, "CLOCKERR" },
  32. { 0, NULL }
  33. };
  34. static const char *const ret_code_descript[] = {
  35. "clock synchronized",
  36. "insert leap second",
  37. "delete leap second",
  38. "leap second in progress",
  39. "leap second has occurred",
  40. "clock not synchronized"
  41. };
  42. int adjtimex_main(int argc, char **argv);
  43. int adjtimex_main(int argc, char **argv)
  44. {
  45. enum {
  46. OPT_quiet = 0x1
  47. };
  48. unsigned opt;
  49. char *opt_o, *opt_f, *opt_p, *opt_t;
  50. struct timex txc;
  51. int i, ret, sep;
  52. const char *descript;
  53. txc.modes=0;
  54. opt = getopt32(argv, "qo:f:p:t:",
  55. &opt_o, &opt_f, &opt_p, &opt_t);
  56. //if (opt & 0x1) // -q
  57. if (opt & 0x2) { // -o
  58. txc.offset = xatol(opt_o);
  59. txc.modes |= ADJ_OFFSET_SINGLESHOT;
  60. }
  61. if (opt & 0x4) { // -f
  62. txc.freq = xatol(opt_f);
  63. txc.modes |= ADJ_FREQUENCY;
  64. }
  65. if (opt & 0x8) { // -p
  66. txc.constant = xatol(opt_p);
  67. txc.modes |= ADJ_TIMECONST;
  68. }
  69. if (opt & 0x10) { // -t
  70. txc.tick = xatol(opt_t);
  71. txc.modes |= ADJ_TICK;
  72. }
  73. if (argc != optind) { /* no valid non-option parameters */
  74. bb_show_usage();
  75. }
  76. ret = adjtimex(&txc);
  77. if (ret < 0) perror("adjtimex");
  78. if (!(opt & OPT_quiet) && ret>=0) {
  79. printf(
  80. " mode: %d\n"
  81. "-o offset: %ld\n"
  82. "-f frequency: %ld\n"
  83. " maxerror: %ld\n"
  84. " esterror: %ld\n"
  85. " status: %d ( ",
  86. txc.modes, txc.offset, txc.freq, txc.maxerror,
  87. txc.esterror, txc.status);
  88. /* representative output of next code fragment:
  89. "PLL | PPSTIME" */
  90. sep=0;
  91. for (i=0; statlist[i].name; i++) {
  92. if (txc.status & statlist[i].bit) {
  93. if (sep) fputs(" | ",stdout);
  94. fputs(statlist[i].name,stdout);
  95. sep=1;
  96. }
  97. }
  98. descript = "error";
  99. if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret];
  100. printf(" )\n"
  101. "-p timeconstant: %ld\n"
  102. " precision: %ld\n"
  103. " tolerance: %ld\n"
  104. "-t tick: %ld\n"
  105. " time.tv_sec: %ld\n"
  106. " time.tv_usec: %ld\n"
  107. " return value: %d (%s)\n",
  108. txc.constant,
  109. txc.precision, txc.tolerance, txc.tick,
  110. (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
  111. }
  112. return (ret<0);
  113. }