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 | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 17 | static const struct { |
| 18 | int bit; |
| 19 | const char *name; |
| 20 | } statlist[] = { |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 21 | { STA_PLL, "PLL" }, |
| 22 | { STA_PPSFREQ, "PPSFREQ" }, |
| 23 | { STA_PPSTIME, "PPSTIME" }, |
| 24 | { STA_FLL, "FFL" }, |
| 25 | { STA_INS, "INS" }, |
| 26 | { STA_DEL, "DEL" }, |
| 27 | { STA_UNSYNC, "UNSYNC" }, |
| 28 | { STA_FREQHOLD, "FREQHOLD" }, |
| 29 | { STA_PPSSIGNAL, "PPSSIGNAL" }, |
| 30 | { STA_PPSJITTER, "PPSJITTER" }, |
| 31 | { STA_PPSWANDER, "PPSWANDER" }, |
| 32 | { STA_PPSERROR, "PPSERROR" }, |
| 33 | { STA_CLOCKERR, "CLOCKERR" }, |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 34 | { 0, NULL } |
| 35 | }; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 37 | static const char *const ret_code_descript[] = { |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 38 | "clock synchronized", |
| 39 | "insert leap second", |
| 40 | "delete leap second", |
| 41 | "leap second in progress", |
| 42 | "leap second has occurred", |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 43 | "clock not synchronized" |
| 44 | }; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 46 | int adjtimex_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | 8fba99f | 2006-05-27 22:08:01 +0000 | [diff] [blame] | 47 | int adjtimex_main(int argc, char **argv) |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 48 | { |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 49 | enum { |
| 50 | OPT_quiet = 0x1 |
| 51 | }; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 52 | unsigned opt; |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 53 | char *opt_o, *opt_f, *opt_p, *opt_t; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 54 | struct timex txc; |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 55 | int i, ret, sep; |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 56 | const char *descript; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 57 | txc.modes=0; |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 59 | opt = getopt32(argv, "qo:f:p:t:", |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 60 | &opt_o, &opt_f, &opt_p, &opt_t); |
| 61 | //if (opt & 0x1) // -q |
| 62 | if (opt & 0x2) { // -o |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 63 | txc.offset = xatol(opt_o); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 64 | txc.modes |= ADJ_OFFSET_SINGLESHOT; |
| 65 | } |
| 66 | if (opt & 0x4) { // -f |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 67 | txc.freq = xatol(opt_f); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 68 | txc.modes |= ADJ_FREQUENCY; |
| 69 | } |
| 70 | if (opt & 0x8) { // -p |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 71 | txc.constant = xatol(opt_p); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 72 | txc.modes |= ADJ_TIMECONST; |
| 73 | } |
| 74 | if (opt & 0x10) { // -t |
Denis Vlasenko | dd2b2f7 | 2007-03-14 23:00:26 +0000 | [diff] [blame] | 75 | txc.tick = xatol(opt_t); |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 76 | txc.modes |= ADJ_TICK; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 77 | } |
| 78 | if (argc != optind) { /* no valid non-option parameters */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 79 | bb_show_usage(); |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | ret = adjtimex(&txc); |
| 83 | |
| 84 | if (ret < 0) perror("adjtimex"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | 109d21f | 2006-09-22 08:47:54 +0000 | [diff] [blame] | 86 | if (!(opt & OPT_quiet) && ret>=0) { |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 87 | printf( |
| 88 | " mode: %d\n" |
| 89 | "-o offset: %ld\n" |
| 90 | "-f frequency: %ld\n" |
| 91 | " maxerror: %ld\n" |
| 92 | " esterror: %ld\n" |
| 93 | " status: %d ( ", |
| 94 | txc.modes, txc.offset, txc.freq, txc.maxerror, |
| 95 | txc.esterror, txc.status); |
| 96 | |
| 97 | /* representative output of next code fragment: |
| 98 | "PLL | PPSTIME" */ |
| 99 | sep=0; |
| 100 | for (i=0; statlist[i].name; i++) { |
| 101 | if (txc.status & statlist[i].bit) { |
| 102 | if (sep) fputs(" | ",stdout); |
| 103 | fputs(statlist[i].name,stdout); |
| 104 | sep=1; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | descript = "error"; |
| 109 | if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret]; |
| 110 | printf(" )\n" |
| 111 | "-p timeconstant: %ld\n" |
| 112 | " precision: %ld\n" |
| 113 | " tolerance: %ld\n" |
| 114 | "-t tick: %ld\n" |
| 115 | " time.tv_sec: %ld\n" |
| 116 | " time.tv_usec: %ld\n" |
| 117 | " return value: %d (%s)\n", |
| 118 | txc.constant, |
| 119 | txc.precision, txc.tolerance, txc.tick, |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 120 | (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript); |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 121 | } |
| 122 | return (ret<0); |
| 123 | } |