adjtimex.c 2.9 KB

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