blob: b35538a84114ff72b9e5953c7287995755cf5ece [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
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +000014#include "busybox.h"
Mark Whitley6f932772001-03-20 19:18:10 +000015#include <sys/timex.h>
Mark Whitley6f932772001-03-20 19:18:10 +000016
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000017static const struct {int bit; const char *name;} statlist[] = {
Mark Whitley6f932772001-03-20 19:18:10 +000018 { 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"1f0262b2005-10-20 11:17:48 +000033static const char * const ret_code_descript[] = {
Mark Whitley6f932772001-03-20 19:18:10 +000034 "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
Rob Landley8fba99f2006-05-27 22:08:01 +000041int adjtimex_main(int argc, char **argv)
Mark Whitley6f932772001-03-20 19:18:10 +000042{
Denis Vlasenko109d21f2006-09-22 08:47:54 +000043 enum {
44 OPT_quiet = 0x1
45 };
Denis Vlasenko67b23e62006-10-03 21:00:06 +000046 unsigned opt;
Denis Vlasenko109d21f2006-09-22 08:47:54 +000047 char *opt_o, *opt_f, *opt_p, *opt_t;
Mark Whitley6f932772001-03-20 19:18:10 +000048 struct timex txc;
Denis Vlasenko109d21f2006-09-22 08:47:54 +000049 int i, ret, sep;
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000050 const char *descript;
Mark Whitley6f932772001-03-20 19:18:10 +000051 txc.modes=0;
Denis Vlasenko109d21f2006-09-22 08:47:54 +000052
Denis Vlasenko67b23e62006-10-03 21:00:06 +000053 opt = getopt32(argc, argv, "qo:f:p:t:",
Denis Vlasenko109d21f2006-09-22 08:47:54 +000054 &opt_o, &opt_f, &opt_p, &opt_t);
55 //if (opt & 0x1) // -q
56 if (opt & 0x2) { // -o
Denis Vlasenko13858992006-10-08 12:49:22 +000057 txc.offset = xatoi(opt_o);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000058 txc.modes |= ADJ_OFFSET_SINGLESHOT;
59 }
60 if (opt & 0x4) { // -f
Denis Vlasenko13858992006-10-08 12:49:22 +000061 txc.freq = xatou(opt_f);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000062 txc.modes |= ADJ_FREQUENCY;
63 }
64 if (opt & 0x8) { // -p
Denis Vlasenko13858992006-10-08 12:49:22 +000065 txc.constant = xatoi(opt_p);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000066 txc.modes |= ADJ_TIMECONST;
67 }
68 if (opt & 0x10) { // -t
Denis Vlasenko13858992006-10-08 12:49:22 +000069 txc.tick = xatoi(opt_t);
Denis Vlasenko109d21f2006-09-22 08:47:54 +000070 txc.modes |= ADJ_TICK;
Mark Whitley6f932772001-03-20 19:18:10 +000071 }
72 if (argc != optind) { /* no valid non-option parameters */
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 bb_show_usage();
Mark Whitley6f932772001-03-20 19:18:10 +000074 }
75
76 ret = adjtimex(&txc);
77
78 if (ret < 0) perror("adjtimex");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000079
Denis Vlasenko109d21f2006-09-22 08:47:54 +000080 if (!(opt & OPT_quiet) && ret>=0) {
Mark Whitley6f932772001-03-20 19:18:10 +000081 printf(
82 " mode: %d\n"
83 "-o offset: %ld\n"
84 "-f frequency: %ld\n"
85 " maxerror: %ld\n"
86 " esterror: %ld\n"
87 " status: %d ( ",
88 txc.modes, txc.offset, txc.freq, txc.maxerror,
89 txc.esterror, txc.status);
90
91 /* representative output of next code fragment:
92 "PLL | PPSTIME" */
93 sep=0;
94 for (i=0; statlist[i].name; i++) {
95 if (txc.status & statlist[i].bit) {
96 if (sep) fputs(" | ",stdout);
97 fputs(statlist[i].name,stdout);
98 sep=1;
99 }
100 }
101
102 descript = "error";
103 if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret];
104 printf(" )\n"
105 "-p timeconstant: %ld\n"
106 " precision: %ld\n"
107 " tolerance: %ld\n"
108 "-t tick: %ld\n"
109 " time.tv_sec: %ld\n"
110 " time.tv_usec: %ld\n"
111 " return value: %d (%s)\n",
112 txc.constant,
113 txc.precision, txc.tolerance, txc.tick,
Eric Andersene76c3b02001-04-05 03:14:39 +0000114 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
Mark Whitley6f932772001-03-20 19:18:10 +0000115 }
116 return (ret<0);
117}