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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 11 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 14 | //usage:#define adjtimex_trivial_usage |
| 15 | //usage: "[-q] [-o OFF] [-f FREQ] [-p TCONST] [-t TICK]" |
| 16 | //usage:#define adjtimex_full_usage "\n\n" |
Denys Vlasenko | e152cb8 | 2014-04-17 11:29:34 +0200 | [diff] [blame] | 17 | //usage: "Read or set kernel time variables. See adjtimex(2)\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 18 | //usage: "\n -q Quiet" |
| 19 | //usage: "\n -o OFF Time offset, microseconds" |
| 20 | //usage: "\n -f FREQ Frequency adjust, integer kernel units (65536 is 1ppm)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 21 | //usage: "\n -t TICK Microseconds per tick, usually 10000" |
Denys Vlasenko | e152cb8 | 2014-04-17 11:29:34 +0200 | [diff] [blame] | 22 | //usage: "\n (positive -t or -f values make clock run faster)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | //usage: "\n -p TCONST" |
| 24 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 25 | #include "libbb.h" |
Denys Vlasenko | 8f6ce09 | 2011-07-08 08:34:28 +0200 | [diff] [blame] | 26 | #ifdef __BIONIC__ |
| 27 | # include <linux/timex.h> |
| 28 | #else |
| 29 | # include <sys/timex.h> |
| 30 | #endif |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 32 | static const uint16_t statlist_bit[] = { |
| 33 | STA_PLL, |
| 34 | STA_PPSFREQ, |
| 35 | STA_PPSTIME, |
| 36 | STA_FLL, |
| 37 | STA_INS, |
| 38 | STA_DEL, |
| 39 | STA_UNSYNC, |
| 40 | STA_FREQHOLD, |
| 41 | STA_PPSSIGNAL, |
| 42 | STA_PPSJITTER, |
| 43 | STA_PPSWANDER, |
| 44 | STA_PPSERROR, |
| 45 | STA_CLOCKERR, |
| 46 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 47 | }; |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 48 | static const char statlist_name[] = |
| 49 | "PLL" "\0" |
| 50 | "PPSFREQ" "\0" |
| 51 | "PPSTIME" "\0" |
| 52 | "FFL" "\0" |
| 53 | "INS" "\0" |
| 54 | "DEL" "\0" |
| 55 | "UNSYNC" "\0" |
| 56 | "FREQHOLD" "\0" |
| 57 | "PPSSIGNAL" "\0" |
| 58 | "PPSJITTER" "\0" |
| 59 | "PPSWANDER" "\0" |
| 60 | "PPSERROR" "\0" |
| 61 | "CLOCKERR" |
| 62 | ; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 64 | static const char ret_code_descript[] = |
| 65 | "clock synchronized" "\0" |
| 66 | "insert leap second" "\0" |
| 67 | "delete leap second" "\0" |
| 68 | "leap second in progress" "\0" |
| 69 | "leap second has occurred" "\0" |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 70 | "clock not synchronized" |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 71 | ; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 73 | int adjtimex_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 74 | int adjtimex_main(int argc UNUSED_PARAM, char **argv) |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 75 | { |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 76 | enum { |
| 77 | OPT_quiet = 0x1 |
| 78 | }; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 79 | unsigned opt; |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 80 | char *opt_o, *opt_f, *opt_p, *opt_t; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 81 | struct timex txc; |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 82 | int i, ret; |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 83 | const char *descript; |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 84 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 85 | opt_complementary = "=0"; /* no valid non-option parameters */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 86 | opt = getopt32(argv, "qo:f:p:t:", |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 87 | &opt_o, &opt_f, &opt_p, &opt_t); |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 88 | txc.modes = 0; |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 89 | //if (opt & 0x1) // -q |
| 90 | if (opt & 0x2) { // -o |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 91 | txc.offset = xatol(opt_o); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 92 | txc.modes |= ADJ_OFFSET_SINGLESHOT; |
| 93 | } |
| 94 | if (opt & 0x4) { // -f |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 95 | txc.freq = xatol(opt_f); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 96 | txc.modes |= ADJ_FREQUENCY; |
| 97 | } |
| 98 | if (opt & 0x8) { // -p |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 99 | txc.constant = xatol(opt_p); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 100 | txc.modes |= ADJ_TIMECONST; |
| 101 | } |
| 102 | if (opt & 0x10) { // -t |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 103 | txc.tick = xatol(opt_t); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 104 | txc.modes |= ADJ_TICK; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 105 | } |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 106 | |
| 107 | ret = adjtimex(&txc); |
| 108 | |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 109 | if (ret < 0) { |
| 110 | bb_perror_nomsg_and_die(); |
| 111 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 113 | if (!(opt & OPT_quiet)) { |
Denys Vlasenko | e152cb8 | 2014-04-17 11:29:34 +0200 | [diff] [blame] | 114 | const char *sep; |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 115 | const char *name; |
| 116 | |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 117 | printf( |
| 118 | " mode: %d\n" |
Denys Vlasenko | e152cb8 | 2014-04-17 11:29:34 +0200 | [diff] [blame] | 119 | "-o offset: %ld us\n" |
| 120 | "-f freq.adjust: %ld (65536 = 1ppm)\n" |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 121 | " maxerror: %ld\n" |
| 122 | " esterror: %ld\n" |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 123 | " status: %d (", |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 124 | txc.modes, txc.offset, txc.freq, txc.maxerror, |
| 125 | txc.esterror, txc.status); |
| 126 | |
| 127 | /* representative output of next code fragment: |
Denys Vlasenko | e152cb8 | 2014-04-17 11:29:34 +0200 | [diff] [blame] | 128 | * "PLL | PPSTIME" |
| 129 | */ |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 130 | name = statlist_name; |
Denys Vlasenko | e152cb8 | 2014-04-17 11:29:34 +0200 | [diff] [blame] | 131 | sep = ""; |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 132 | for (i = 0; statlist_bit[i]; i++) { |
| 133 | if (txc.status & statlist_bit[i]) { |
Denys Vlasenko | e152cb8 | 2014-04-17 11:29:34 +0200 | [diff] [blame] | 134 | printf("%s%s", sep, name); |
| 135 | sep = " | "; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 136 | } |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 137 | name += strlen(name) + 1; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | descript = "error"; |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 141 | if (ret <= 5) |
| 142 | descript = nth_string(ret_code_descript, ret); |
| 143 | printf(")\n" |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 144 | "-p timeconstant: %ld\n" |
Denys Vlasenko | e152cb8 | 2014-04-17 11:29:34 +0200 | [diff] [blame] | 145 | " precision: %ld us\n" |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 146 | " tolerance: %ld\n" |
Denys Vlasenko | e152cb8 | 2014-04-17 11:29:34 +0200 | [diff] [blame] | 147 | "-t tick: %ld us\n" |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 148 | " time.tv_sec: %ld\n" |
| 149 | " time.tv_usec: %ld\n" |
| 150 | " return value: %d (%s)\n", |
| 151 | txc.constant, |
| 152 | txc.precision, txc.tolerance, txc.tick, |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 153 | (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript); |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 154 | } |
Denis Vlasenko | bfc3d82 | 2007-11-04 04:10:17 +0000 | [diff] [blame] | 155 | |
| 156 | return 0; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 157 | } |