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