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