Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * adjtimex.c - read, and possibly modify, the Linux kernel `timex' variables. |
| 3 | * |
| 4 | * Originally written: October 1997 |
| 5 | * Last hack: March 2001 |
| 6 | * Copyright 1997, 2000, 2001 Larry Doolittle <LRDoolittle@lbl.gov> |
| 7 | * |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 8 | * busyboxed 20 March 2001, Larry Doolittle <ldoolitt@recycle.lbl.gov> |
Rob Landley | 8fba99f | 2006-05-27 22:08:01 +0000 | [diff] [blame] | 9 | * |
| 10 | * 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] | 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <unistd.h> |
| 17 | #include <sys/timex.h> |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 18 | #include "busybox.h" |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 19 | |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 20 | static const struct {int bit; const char *name;} 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" }, |
| 34 | { 0, NULL } }; |
| 35 | |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 36 | static const char * const ret_code_descript[] = { |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 37 | "clock synchronized", |
| 38 | "insert leap second", |
| 39 | "delete leap second", |
| 40 | "leap second in progress", |
| 41 | "leap second has occurred", |
| 42 | "clock not synchronized" }; |
| 43 | |
Rob Landley | 8fba99f | 2006-05-27 22:08:01 +0000 | [diff] [blame] | 44 | int adjtimex_main(int argc, char **argv) |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 45 | { |
| 46 | struct timex txc; |
| 47 | int quiet=0; |
| 48 | int c, i, ret, sep; |
"Vladimir N. Oleynik" | 1f0262b | 2005-10-20 11:17:48 +0000 | [diff] [blame] | 49 | const char *descript; |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 50 | txc.modes=0; |
| 51 | for (;;) { |
| 52 | c = getopt( argc, argv, "qo:f:p:t:"); |
| 53 | if (c == EOF) break; |
| 54 | switch (c) { |
| 55 | case 'q': |
| 56 | quiet=1; |
| 57 | break; |
| 58 | case 'o': |
| 59 | txc.offset = atoi(optarg); |
| 60 | txc.modes |= ADJ_OFFSET_SINGLESHOT; |
| 61 | break; |
| 62 | case 'f': |
| 63 | txc.freq = atoi(optarg); |
| 64 | txc.modes |= ADJ_FREQUENCY; |
| 65 | break; |
| 66 | case 'p': |
| 67 | txc.constant = atoi(optarg); |
| 68 | txc.modes |= ADJ_TIMECONST; |
| 69 | break; |
| 70 | case 't': |
| 71 | txc.tick = atoi(optarg); |
| 72 | txc.modes |= ADJ_TICK; |
| 73 | break; |
| 74 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | bb_show_usage(); |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 76 | exit(1); |
| 77 | } |
| 78 | } |
| 79 | if (argc != optind) { /* no valid non-option parameters */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | bb_show_usage(); |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 81 | exit(1); |
| 82 | } |
| 83 | |
| 84 | ret = adjtimex(&txc); |
| 85 | |
| 86 | if (ret < 0) perror("adjtimex"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 87 | |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 88 | if (!quiet && ret>=0) { |
| 89 | printf( |
| 90 | " mode: %d\n" |
| 91 | "-o offset: %ld\n" |
| 92 | "-f frequency: %ld\n" |
| 93 | " maxerror: %ld\n" |
| 94 | " esterror: %ld\n" |
| 95 | " status: %d ( ", |
| 96 | txc.modes, txc.offset, txc.freq, txc.maxerror, |
| 97 | txc.esterror, txc.status); |
| 98 | |
| 99 | /* representative output of next code fragment: |
| 100 | "PLL | PPSTIME" */ |
| 101 | sep=0; |
| 102 | for (i=0; statlist[i].name; i++) { |
| 103 | if (txc.status & statlist[i].bit) { |
| 104 | if (sep) fputs(" | ",stdout); |
| 105 | fputs(statlist[i].name,stdout); |
| 106 | sep=1; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | descript = "error"; |
| 111 | if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret]; |
| 112 | printf(" )\n" |
| 113 | "-p timeconstant: %ld\n" |
| 114 | " precision: %ld\n" |
| 115 | " tolerance: %ld\n" |
| 116 | "-t tick: %ld\n" |
| 117 | " time.tv_sec: %ld\n" |
| 118 | " time.tv_usec: %ld\n" |
| 119 | " return value: %d (%s)\n", |
| 120 | txc.constant, |
| 121 | txc.precision, txc.tolerance, txc.tick, |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 122 | (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript); |
Mark Whitley | 6f93277 | 2001-03-20 19:18:10 +0000 | [diff] [blame] | 123 | } |
| 124 | return (ret<0); |
| 125 | } |