3
0

adjtimex.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "busybox.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. {
  39. enum {
  40. OPT_quiet = 0x1
  41. };
  42. unsigned opt;
  43. char *opt_o, *opt_f, *opt_p, *opt_t;
  44. struct timex txc;
  45. int i, ret, sep;
  46. const char *descript;
  47. txc.modes=0;
  48. opt = getopt32(argc, argv, "qo:f:p:t:",
  49. &opt_o, &opt_f, &opt_p, &opt_t);
  50. //if (opt & 0x1) // -q
  51. if (opt & 0x2) { // -o
  52. txc.offset = xatoi(opt_o);
  53. txc.modes |= ADJ_OFFSET_SINGLESHOT;
  54. }
  55. if (opt & 0x4) { // -f
  56. txc.freq = xatou(opt_f);
  57. txc.modes |= ADJ_FREQUENCY;
  58. }
  59. if (opt & 0x8) { // -p
  60. txc.constant = xatoi(opt_p);
  61. txc.modes |= ADJ_TIMECONST;
  62. }
  63. if (opt & 0x10) { // -t
  64. txc.tick = xatoi(opt_t);
  65. txc.modes |= ADJ_TICK;
  66. }
  67. if (argc != optind) { /* no valid non-option parameters */
  68. bb_show_usage();
  69. }
  70. ret = adjtimex(&txc);
  71. if (ret < 0) perror("adjtimex");
  72. if (!(opt & OPT_quiet) && ret>=0) {
  73. printf(
  74. " mode: %d\n"
  75. "-o offset: %ld\n"
  76. "-f frequency: %ld\n"
  77. " maxerror: %ld\n"
  78. " esterror: %ld\n"
  79. " status: %d ( ",
  80. txc.modes, txc.offset, txc.freq, txc.maxerror,
  81. txc.esterror, txc.status);
  82. /* representative output of next code fragment:
  83. "PLL | PPSTIME" */
  84. sep=0;
  85. for (i=0; statlist[i].name; i++) {
  86. if (txc.status & statlist[i].bit) {
  87. if (sep) fputs(" | ",stdout);
  88. fputs(statlist[i].name,stdout);
  89. sep=1;
  90. }
  91. }
  92. descript = "error";
  93. if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret];
  94. printf(" )\n"
  95. "-p timeconstant: %ld\n"
  96. " precision: %ld\n"
  97. " tolerance: %ld\n"
  98. "-t tick: %ld\n"
  99. " time.tv_sec: %ld\n"
  100. " time.tv_usec: %ld\n"
  101. " return value: %d (%s)\n",
  102. txc.constant,
  103. txc.precision, txc.tolerance, txc.tick,
  104. (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
  105. }
  106. return (ret<0);
  107. }