blob: 9662bbea9f34bfb43cd6518cf3f3a69b6dabbe19 [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 <stdio.h>
16#include <sys/types.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <sys/timex.h>
Mark Whitley6f932772001-03-20 19:18:10 +000020
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000021static const struct {int bit; const char *name;} statlist[] = {
Mark Whitley6f932772001-03-20 19:18:10 +000022 { STA_PLL, "PLL" },
23 { STA_PPSFREQ, "PPSFREQ" },
24 { STA_PPSTIME, "PPSTIME" },
25 { STA_FLL, "FFL" },
26 { STA_INS, "INS" },
27 { STA_DEL, "DEL" },
28 { STA_UNSYNC, "UNSYNC" },
29 { STA_FREQHOLD, "FREQHOLD" },
30 { STA_PPSSIGNAL, "PPSSIGNAL" },
31 { STA_PPSJITTER, "PPSJITTER" },
32 { STA_PPSWANDER, "PPSWANDER" },
33 { STA_PPSERROR, "PPSERROR" },
34 { STA_CLOCKERR, "CLOCKERR" },
35 { 0, NULL } };
36
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +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",
43 "clock not synchronized" };
44
Rob Landley8fba99f2006-05-27 22:08:01 +000045int adjtimex_main(int argc, char **argv)
Mark Whitley6f932772001-03-20 19:18:10 +000046{
Denis Vlasenko109d21f2006-09-22 08:47:54 +000047 enum {
48 OPT_quiet = 0x1
49 };
50 unsigned long opt;
51 char *opt_o, *opt_f, *opt_p, *opt_t;
Mark Whitley6f932772001-03-20 19:18:10 +000052 struct timex txc;
Denis Vlasenko109d21f2006-09-22 08:47:54 +000053 int i, ret, sep;
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000054 const char *descript;
Mark Whitley6f932772001-03-20 19:18:10 +000055 txc.modes=0;
Denis Vlasenko109d21f2006-09-22 08:47:54 +000056
57 opt = bb_getopt_ulflags(argc, argv, "qo:f:p:t:",
58 &opt_o, &opt_f, &opt_p, &opt_t);
59 //if (opt & 0x1) // -q
60 if (opt & 0x2) { // -o
61 txc.offset = atoi(opt_o);
62 txc.modes |= ADJ_OFFSET_SINGLESHOT;
63 }
64 if (opt & 0x4) { // -f
65 txc.freq = atoi(opt_f);
66 txc.modes |= ADJ_FREQUENCY;
67 }
68 if (opt & 0x8) { // -p
69 txc.constant = atoi(opt_p);
70 txc.modes |= ADJ_TIMECONST;
71 }
72 if (opt & 0x10) { // -t
73 txc.tick = atoi(opt_t);
74 txc.modes |= ADJ_TICK;
Mark Whitley6f932772001-03-20 19:18:10 +000075 }
76 if (argc != optind) { /* no valid non-option parameters */
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 bb_show_usage();
Mark Whitley6f932772001-03-20 19:18:10 +000078 }
79
80 ret = adjtimex(&txc);
81
82 if (ret < 0) perror("adjtimex");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000083
Denis Vlasenko109d21f2006-09-22 08:47:54 +000084 if (!(opt & OPT_quiet) && ret>=0) {
Mark Whitley6f932772001-03-20 19:18:10 +000085 printf(
86 " mode: %d\n"
87 "-o offset: %ld\n"
88 "-f frequency: %ld\n"
89 " maxerror: %ld\n"
90 " esterror: %ld\n"
91 " status: %d ( ",
92 txc.modes, txc.offset, txc.freq, txc.maxerror,
93 txc.esterror, txc.status);
94
95 /* representative output of next code fragment:
96 "PLL | PPSTIME" */
97 sep=0;
98 for (i=0; statlist[i].name; i++) {
99 if (txc.status & statlist[i].bit) {
100 if (sep) fputs(" | ",stdout);
101 fputs(statlist[i].name,stdout);
102 sep=1;
103 }
104 }
105
106 descript = "error";
107 if (ret >= 0 && ret <= 5) descript = ret_code_descript[ret];
108 printf(" )\n"
109 "-p timeconstant: %ld\n"
110 " precision: %ld\n"
111 " tolerance: %ld\n"
112 "-t tick: %ld\n"
113 " time.tv_sec: %ld\n"
114 " time.tv_usec: %ld\n"
115 " return value: %d (%s)\n",
116 txc.constant,
117 txc.precision, txc.tolerance, txc.tick,
Eric Andersene76c3b02001-04-05 03:14:39 +0000118 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
Mark Whitley6f932772001-03-20 19:18:10 +0000119 }
120 return (ret<0);
121}