Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 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 | * |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 9 | * busyboxed 20 March 2001, Larry Doolittle <ldoolitt@recycle.lbl.gov> |
Bernhard Reutner-Fischer | c89982d | 2006-06-03 19:49:21 +0000 | [diff] [blame] | 10 | * |
Rob Landley | 8fba99f | 2006-05-27 22:08:01 +0000 | [diff] [blame] | 11 | * Licensed under GPLv2 or later, see file License in this tarball for details. |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 15 | #include <sys/timex.h> |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 17 | static const uint16_t statlist_bit[] = { |
| 18 | STA_PLL, |
| 19 | STA_PPSFREQ, |
| 20 | STA_PPSTIME, |
| 21 | STA_FLL, |
| 22 | STA_INS, |
| 23 | STA_DEL, |
| 24 | STA_UNSYNC, |
| 25 | STA_FREQHOLD, |
| 26 | STA_PPSSIGNAL, |
| 27 | STA_PPSJITTER, |
| 28 | STA_PPSWANDER, |
| 29 | STA_PPSERROR, |
| 30 | STA_CLOCKERR, |
| 31 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 32 | }; |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 33 | static const char statlist_name[] = |
| 34 | "PLL" "\0" |
| 35 | "PPSFREQ" "\0" |
| 36 | "PPSTIME" "\0" |
| 37 | "FFL" "\0" |
| 38 | "INS" "\0" |
| 39 | "DEL" "\0" |
| 40 | "UNSYNC" "\0" |
| 41 | "FREQHOLD" "\0" |
| 42 | "PPSSIGNAL" "\0" |
| 43 | "PPSJITTER" "\0" |
| 44 | "PPSWANDER" "\0" |
| 45 | "PPSERROR" "\0" |
| 46 | "CLOCKERR" |
| 47 | ; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 49 | static const char ret_code_descript[] = |
| 50 | "clock synchronized" "\0" |
| 51 | "insert leap second" "\0" |
| 52 | "delete leap second" "\0" |
| 53 | "leap second in progress" "\0" |
| 54 | "leap second has occurred" "\0" |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 55 | "clock not synchronized" |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 56 | ; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 57 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 58 | int adjtimex_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 59 | int adjtimex_main(int argc UNUSED_PARAM, char **argv) |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 60 | { |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 61 | enum { |
| 62 | OPT_quiet = 0x1 |
| 63 | }; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 64 | unsigned opt; |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 65 | char *opt_o, *opt_f, *opt_p, *opt_t; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 66 | struct timex txc; |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 67 | int i, ret; |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 68 | const char *descript; |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 69 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 70 | opt_complementary = "=0"; /* no valid non-option parameters */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 71 | opt = getopt32(argv, "qo:f:p:t:", |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 72 | &opt_o, &opt_f, &opt_p, &opt_t); |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 73 | txc.modes = 0; |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 74 | //if (opt & 0x1) // -q |
| 75 | if (opt & 0x2) { // -o |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 76 | txc.offset = xatol(opt_o); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 77 | txc.modes |= ADJ_OFFSET_SINGLESHOT; |
| 78 | } |
| 79 | if (opt & 0x4) { // -f |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 80 | txc.freq = xatol(opt_f); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 81 | txc.modes |= ADJ_FREQUENCY; |
| 82 | } |
| 83 | if (opt & 0x8) { // -p |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 84 | txc.constant = xatol(opt_p); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 85 | txc.modes |= ADJ_TIMECONST; |
| 86 | } |
| 87 | if (opt & 0x10) { // -t |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 88 | txc.tick = xatol(opt_t); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 89 | txc.modes |= ADJ_TICK; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 90 | } |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 91 | |
| 92 | ret = adjtimex(&txc); |
| 93 | |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 94 | if (ret < 0) { |
| 95 | bb_perror_nomsg_and_die(); |
| 96 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 98 | if (!(opt & OPT_quiet)) { |
| 99 | int sep; |
| 100 | const char *name; |
| 101 | |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 102 | printf( |
| 103 | " mode: %d\n" |
| 104 | "-o offset: %ld\n" |
| 105 | "-f frequency: %ld\n" |
| 106 | " maxerror: %ld\n" |
| 107 | " esterror: %ld\n" |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 108 | " status: %d (", |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 109 | txc.modes, txc.offset, txc.freq, txc.maxerror, |
| 110 | txc.esterror, txc.status); |
| 111 | |
| 112 | /* representative output of next code fragment: |
| 113 | "PLL | PPSTIME" */ |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 114 | name = statlist_name; |
| 115 | sep = 0; |
| 116 | for (i = 0; statlist_bit[i]; i++) { |
| 117 | if (txc.status & statlist_bit[i]) { |
| 118 | if (sep) |
| 119 | fputs(" | ", stdout); |
| 120 | fputs(name, stdout); |
| 121 | sep = 1; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 122 | } |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 123 | name += strlen(name) + 1; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | descript = "error"; |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 127 | if (ret <= 5) |
| 128 | descript = nth_string(ret_code_descript, ret); |
| 129 | printf(")\n" |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 130 | "-p timeconstant: %ld\n" |
| 131 | " precision: %ld\n" |
| 132 | " tolerance: %ld\n" |
| 133 | "-t tick: %ld\n" |
| 134 | " time.tv_sec: %ld\n" |
| 135 | " time.tv_usec: %ld\n" |
| 136 | " return value: %d (%s)\n", |
| 137 | txc.constant, |
| 138 | txc.precision, txc.tolerance, txc.tick, |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 139 | (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript); |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 140 | } |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 141 | |
| 142 | return 0; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 143 | } |