blob: 67dd0a93beca6a3bf20b0a4efcf41a3f584d08e0 [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 Vlasenko6ca409e2007-08-12 20:58:27 +000017static const struct {
18 int bit;
19 const char *name;
20} statlist[] = {
Mark Whitley6f932772001-03-20 19:18:10 +000021 { 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 Vlasenko6ca409e2007-08-12 20:58:27 +000034 { 0, NULL }
35};
Mark Whitley6f932772001-03-20 19:18:10 +000036
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000037static const char *const ret_code_descript[] = {
Mark Whitley6f932772001-03-20 19:18:10 +000038 "clock synchronized",
39 "insert leap second",
40 "delete leap second",
41 "leap second in progress",
42 "leap second has occurred",
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000043 "clock not synchronized"
44};
Mark Whitley6f932772001-03-20 19:18:10 +000045
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000046int adjtimex_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landley8fba99f2006-05-27 22:08:01 +000047int adjtimex_main(int argc, char **argv)
Mark Whitley6f932772001-03-20 19:18:10 +000048{
Denis Vlasenko109d21f2006-09-22 08:47:54 +000049 enum {
50 OPT_quiet = 0x1
51 };
Denis Vlasenko67b23e62006-10-03 21:00:06 +000052 unsigned opt;
Denis Vlasenko109d21f2006-09-22 08:47:54 +000053 char *opt_o, *opt_f, *opt_p, *opt_t;
Mark Whitley6f932772001-03-20 19:18:10 +000054 struct timex txc;
Denis Vlasenko109d21f2006-09-22 08:47:54 +000055 int i, ret, sep;
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000056 const char *descript;
Mark Whitley6f932772001-03-20 19:18:10 +000057 txc.modes=0;
Denis Vlasenko109d21f2006-09-22 08:47:54 +000058
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000059 opt = getopt32(argv, "qo:f:p:t:",
Denis Vlasenko109d21f2006-09-22 08:47:54 +000060 &opt_o, &opt_f, &opt_p, &opt_t);
61 //if (opt & 0x1) // -q
62 if (opt & 0x2) { // -o
Denis Vlasenkodd2b2f72007-03-14 23:00:26 +000063 txc.offset = xatol(opt_o);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000064 txc.modes |= ADJ_OFFSET_SINGLESHOT;
65 }
66 if (opt & 0x4) { // -f
Denis Vlasenkodd2b2f72007-03-14 23:00:26 +000067 txc.freq = xatol(opt_f);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000068 txc.modes |= ADJ_FREQUENCY;
69 }
70 if (opt & 0x8) { // -p
Denis Vlasenkodd2b2f72007-03-14 23:00:26 +000071 txc.constant = xatol(opt_p);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000072 txc.modes |= ADJ_TIMECONST;
73 }
74 if (opt & 0x10) { // -t
Denis Vlasenkodd2b2f72007-03-14 23:00:26 +000075 txc.tick = xatol(opt_t);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000076 txc.modes |= ADJ_TICK;
Mark Whitley6f932772001-03-20 19:18:10 +000077 }
78 if (argc != optind) { /* no valid non-option parameters */
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 bb_show_usage();
Mark Whitley6f932772001-03-20 19:18:10 +000080 }
81
82 ret = adjtimex(&txc);
83
84 if (ret < 0) perror("adjtimex");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000085
Denis Vlasenko109d21f2006-09-22 08:47:54 +000086 if (!(opt & OPT_quiet) && ret>=0) {
Mark Whitley6f932772001-03-20 19:18:10 +000087 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 Andersene76c3b02001-04-05 03:14:39 +0000120 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
Mark Whitley6f932772001-03-20 19:18:10 +0000121 }
122 return (ret<0);
123}