blob: 905b80f47672fbdf9d73a2e68104cbf94fe88c3d [file] [log] [blame]
Mark Whitley6f932772001-03-20 19:18:10 +00001/*
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 Whitley6f932772001-03-20 19:18:10 +00008 * busyboxed 20 March 2001, Larry Doolittle <ldoolitt@recycle.lbl.gov>
Rob Landley8fba99f2006-05-27 22:08:01 +00009 *
10 * Licensed under GPLv2 or later, see file License in this tarball for details.
Mark Whitley6f932772001-03-20 19:18:10 +000011 */
12
13#include <stdio.h>
14#include <sys/types.h>
15#include <stdlib.h>
16#include <unistd.h>
17#include <sys/timex.h>
Mark Whitley6f932772001-03-20 19:18:10 +000018#include "busybox.h"
Mark Whitley6f932772001-03-20 19:18:10 +000019
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000020static const struct {int bit; const char *name;} 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" },
34 { 0, NULL } };
35
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000036static const char * const ret_code_descript[] = {
Mark Whitley6f932772001-03-20 19:18:10 +000037 "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 Landley8fba99f2006-05-27 22:08:01 +000044int adjtimex_main(int argc, char **argv)
Mark Whitley6f932772001-03-20 19:18:10 +000045{
46 struct timex txc;
47 int quiet=0;
48 int c, i, ret, sep;
"Vladimir N. Oleynik"1f0262b2005-10-20 11:17:48 +000049 const char *descript;
Mark Whitley6f932772001-03-20 19:18:10 +000050 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 cad53642003-03-19 09:13:01 +000075 bb_show_usage();
Mark Whitley6f932772001-03-20 19:18:10 +000076 exit(1);
77 }
78 }
79 if (argc != optind) { /* no valid non-option parameters */
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 bb_show_usage();
Mark Whitley6f932772001-03-20 19:18:10 +000081 exit(1);
82 }
83
84 ret = adjtimex(&txc);
85
86 if (ret < 0) perror("adjtimex");
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087
Mark Whitley6f932772001-03-20 19:18:10 +000088 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 Andersene76c3b02001-04-05 03:14:39 +0000122 (long)txc.time.tv_sec, (long)txc.time.tv_usec, ret, descript);
Mark Whitley6f932772001-03-20 19:18:10 +0000123 }
124 return (ret<0);
125}