blob: 07f083428c4996f05379e4a3e42a1fd355b4a288 [file] [log] [blame]
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +00001/* vi: set sw=4 ts=4: */
Mark Whitley6f932772001-03-20 19:18:10 +00002/*
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 Whitley6f932772001-03-20 19:18:10 +00009 * busyboxed 20 March 2001, Larry Doolittle <ldoolitt@recycle.lbl.gov>
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +000010 *
Rob Landley8fba99f2006-05-27 22:08:01 +000011 * Licensed under GPLv2 or later, see file License in this tarball for details.
Mark Whitley6f932772001-03-20 19:18:10 +000012 */
13
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Mark Whitley6f932772001-03-20 19:18:10 +000015#include <sys/timex.h>
Mark Whitley6f932772001-03-20 19:18:10 +000016
Denis Vlasenkobfc3d822007-11-04 04:10:17 +000017static const uint16_t statlist_bit[] = {
18 STA_PLL,
19 STA_PPSFREQ,
20 STA_PPSTIME,
21 STA_FLL,
22 STA_INS,
23 STA_DEL,
24 STA_UNSYNC,
25 STA_FREQHOLD,
26 STA_PPSSIGNAL,
27 STA_PPSJITTER,
28 STA_PPSWANDER,
29 STA_PPSERROR,
30 STA_CLOCKERR,
31 0
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000032};
Denis Vlasenkobfc3d822007-11-04 04:10:17 +000033static const char statlist_name[] =
34 "PLL" "\0"
35 "PPSFREQ" "\0"
36 "PPSTIME" "\0"
37 "FFL" "\0"
38 "INS" "\0"
39 "DEL" "\0"
40 "UNSYNC" "\0"
41 "FREQHOLD" "\0"
42 "PPSSIGNAL" "\0"
43 "PPSJITTER" "\0"
44 "PPSWANDER" "\0"
45 "PPSERROR" "\0"
46 "CLOCKERR"
47;
Mark Whitley6f932772001-03-20 19:18:10 +000048
Denis Vlasenkobfc3d822007-11-04 04:10:17 +000049static const char ret_code_descript[] =
50 "clock synchronized" "\0"
51 "insert leap second" "\0"
52 "delete leap second" "\0"
53 "leap second in progress" "\0"
54 "leap second has occurred" "\0"
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000055 "clock not synchronized"
Denis Vlasenkobfc3d822007-11-04 04:10:17 +000056;
Mark Whitley6f932772001-03-20 19:18:10 +000057
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000058int adjtimex_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landley8fba99f2006-05-27 22:08:01 +000059int adjtimex_main(int argc, char **argv)
Mark Whitley6f932772001-03-20 19:18:10 +000060{
Denis Vlasenko109d21f2006-09-22 08:47:54 +000061 enum {
62 OPT_quiet = 0x1
63 };
Denis Vlasenko67b23e62006-10-03 21:00:06 +000064 unsigned opt;
Denis Vlasenko109d21f2006-09-22 08:47:54 +000065 char *opt_o, *opt_f, *opt_p, *opt_t;
Mark Whitley6f932772001-03-20 19:18:10 +000066 struct timex txc;
Denis Vlasenkobfc3d822007-11-04 04:10:17 +000067 int i, ret;
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000068 const char *descript;
Mark Whitley6f932772001-03-20 19:18:10 +000069 txc.modes=0;
Denis Vlasenko109d21f2006-09-22 08:47:54 +000070
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000071 opt = getopt32(argv, "qo:f:p:t:",
Denis Vlasenko109d21f2006-09-22 08:47:54 +000072 &opt_o, &opt_f, &opt_p, &opt_t);
73 //if (opt & 0x1) // -q
74 if (opt & 0x2) { // -o
Denis Vlasenkodd2b2f72007-03-14 23:00:26 +000075 txc.offset = xatol(opt_o);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000076 txc.modes |= ADJ_OFFSET_SINGLESHOT;
77 }
78 if (opt & 0x4) { // -f
Denis Vlasenkodd2b2f72007-03-14 23:00:26 +000079 txc.freq = xatol(opt_f);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000080 txc.modes |= ADJ_FREQUENCY;
81 }
82 if (opt & 0x8) { // -p
Denis Vlasenkodd2b2f72007-03-14 23:00:26 +000083 txc.constant = xatol(opt_p);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000084 txc.modes |= ADJ_TIMECONST;
85 }
86 if (opt & 0x10) { // -t
Denis Vlasenkodd2b2f72007-03-14 23:00:26 +000087 txc.tick = xatol(opt_t);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000088 txc.modes |= ADJ_TICK;
Mark Whitley6f932772001-03-20 19:18:10 +000089 }
90 if (argc != optind) { /* no valid non-option parameters */
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 bb_show_usage();
Mark Whitley6f932772001-03-20 19:18:10 +000092 }
93
94 ret = adjtimex(&txc);
95
Denis Vlasenkobfc3d822007-11-04 04:10:17 +000096 if (ret < 0) {
97 bb_perror_nomsg_and_die();
98 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000099
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000100 if (!(opt & OPT_quiet)) {
101 int sep;
102 const char *name;
103
Mark Whitley6f932772001-03-20 19:18:10 +0000104 printf(
105 " mode: %d\n"
106 "-o offset: %ld\n"
107 "-f frequency: %ld\n"
108 " maxerror: %ld\n"
109 " esterror: %ld\n"
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000110 " status: %d (",
Mark Whitley6f932772001-03-20 19:18:10 +0000111 txc.modes, txc.offset, txc.freq, txc.maxerror,
112 txc.esterror, txc.status);
113
114 /* representative output of next code fragment:
115 "PLL | PPSTIME" */
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000116 name = statlist_name;
117 sep = 0;
118 for (i = 0; statlist_bit[i]; i++) {
119 if (txc.status & statlist_bit[i]) {
120 if (sep)
121 fputs(" | ", stdout);
122 fputs(name, stdout);
123 sep = 1;
Mark Whitley6f932772001-03-20 19:18:10 +0000124 }
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000125 name += strlen(name) + 1;
Mark Whitley6f932772001-03-20 19:18:10 +0000126 }
127
128 descript = "error";
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000129 if (ret <= 5)
130 descript = nth_string(ret_code_descript, ret);
131 printf(")\n"
Mark Whitley6f932772001-03-20 19:18:10 +0000132 "-p timeconstant: %ld\n"
133 " precision: %ld\n"
134 " tolerance: %ld\n"
135 "-t tick: %ld\n"
136 " time.tv_sec: %ld\n"
137 " time.tv_usec: %ld\n"
138 " return value: %d (%s)\n",
139 txc.constant,
140 txc.precision, txc.tolerance, txc.tick,
Eric Andersene76c3b02001-04-05 03:14:39 +0000141 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
Mark Whitley6f932772001-03-20 19:18:10 +0000142 }
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000143
144 return 0;
Mark Whitley6f932772001-03-20 19:18:10 +0000145}