blob: 416271b31561e78118d3dd4985d574e9bdb1eb23 [file] [log] [blame]
Robert Griebl1cd04452002-07-21 16:50:49 +00001/* vi: set sw=4 ts=4: */
2/*
Robert Griebl6859d762002-08-05 02:57:12 +00003 * Mini hwclock implementation for busybox
4 *
Robert Griebl1cd04452002-07-21 16:50:49 +00005 * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
6 *
Bernhard Reutner-Fischer5cf905a2006-03-31 22:36:15 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Robert Griebl6859d762002-08-05 02:57:12 +00008*/
Robert Griebl1cd04452002-07-21 16:50:49 +00009
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020011/* After libbb.h, since it needs sys/types.h on some systems */
12#include <sys/utsname.h>
Mike Frysinger6b160e42008-02-15 02:27:19 +000013#include "rtc_.h"
Eric Andersen8882ea52002-12-11 03:41:28 +000014
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000015#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
Robert Griebl1cd04452002-07-21 16:50:49 +000016# ifndef _GNU_SOURCE
17# define _GNU_SOURCE
18# endif
19#endif
20
Denis Vlasenko673d4bb2007-03-07 23:02:50 +000021
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010022/* diff code is disabled: it's not sys/hw clock diff, it's some useless
23 * "time between hwclock was started and we saw CMOS tick" quantity.
24 * It's useless since hwclock is started at a random moment,
25 * thus the quantity is also random, useless. Showing 0.000000 does not
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010026 * deprive us from any useful info.
27 *
28 * SHOW_HWCLOCK_DIFF code in this file shows the difference between system
29 * and hw clock. It is useful, but not compatible with standard hwclock.
30 * Thus disabled.
31 */
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010032#define SHOW_HWCLOCK_DIFF 0
33
34
35#if !SHOW_HWCLOCK_DIFF
36# define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc)
37#endif
38static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc)
Denis Vlasenko92258542006-11-01 10:25:35 +000039{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010040 struct tm tm_time;
Mike Frysinger6b160e42008-02-15 02:27:19 +000041 int fd;
Robert Griebl1cd04452002-07-21 16:50:49 +000042
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010043 fd = rtc_xopen(pp_rtcname, O_RDONLY);
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010044
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010045 rtc_read_tm(&tm_time, fd);
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010046
47#if SHOW_HWCLOCK_DIFF
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010048 {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010049 int before = tm_time.tm_sec;
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010050 while (1) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010051 rtc_read_tm(&tm_time, fd);
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010052 gettimeofday(sys_tv, NULL);
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010053 if (before != tm_time.tm_sec)
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010054 break;
55 }
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010056 }
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010057#endif
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010058
Denys Vlasenko695fa512010-01-06 18:16:39 +010059 if (ENABLE_FEATURE_CLEAN_UP)
60 close(fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010062 return rtc_tm2time(&tm_time, utc);
Robert Griebl1cd04452002-07-21 16:50:49 +000063}
64
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010065static void show_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000066{
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010067#if SHOW_HWCLOCK_DIFF
68 struct timeval sys_tv;
69#endif
Robert Griebl1cd04452002-07-21 16:50:49 +000070 time_t t;
Denis Vlasenko459be352007-06-17 19:09:05 +000071 char *cp;
Robert Griebl1cd04452002-07-21 16:50:49 +000072
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010073 t = read_rtc(pp_rtcname, &sys_tv, utc);
Denis Vlasenko459be352007-06-17 19:09:05 +000074 cp = ctime(&t);
Denys Vlasenko695fa512010-01-06 18:16:39 +010075 strchrnul(cp, '\n')[0] = '\0';
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010076#if !SHOW_HWCLOCK_DIFF
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010077 printf("%s 0.000000 seconds\n", cp);
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010078#else
79 {
80 long diff = sys_tv.tv_sec - t;
81 if (diff < 0 /*&& tv.tv_usec != 0*/) {
82 /* Why? */
83 /* diff >= 0 is ok: diff < 0, can't just use tv.tv_usec: */
84 /* 45.520820 43.520820 */
85 /* - 44.000000 - 45.000000 */
Denys Vlasenko053ffee2010-01-07 10:52:20 +010086 /* = 1.520820 = -1.479180, not -2.520820! */
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010087 diff++;
88 /* should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
89 sys_tv.tv_usec = 999999 - sys_tv.tv_usec;
90 }
91 printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010092 }
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010093#endif
Robert Griebl1cd04452002-07-21 16:50:49 +000094}
95
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010096static void to_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000097{
Denis Vlasenko459be352007-06-17 19:09:05 +000098 struct timeval tv;
Denys Vlasenko043b1e52009-09-06 12:47:55 +020099 struct timezone tz;
100
101 tz.tz_minuteswest = timezone/60 - 60*daylight;
102 tz.tz_dsttime = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000103
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100104 tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
Denis Vlasenko459be352007-06-17 19:09:05 +0000105 tv.tv_usec = 0;
Denis Vlasenko92258542006-11-01 10:25:35 +0000106 if (settimeofday(&tv, &tz))
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100107 bb_perror_msg_and_die("settimeofday");
Robert Griebl1cd04452002-07-21 16:50:49 +0000108}
109
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100110static void from_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +0000111{
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700112#if 1
113 struct timeval tv;
114 struct tm tm_time;
115 int rtc;
116
117 rtc = rtc_xopen(pp_rtcname, O_WRONLY);
118 gettimeofday(&tv, NULL);
119 /* Prepare tm_time */
120 if (sizeof(time_t) == sizeof(tv.tv_sec)) {
121 if (utc)
122 gmtime_r((time_t*)&tv.tv_sec, &tm_time);
123 else
124 localtime_r((time_t*)&tv.tv_sec, &tm_time);
125 } else {
126 time_t t = tv.tv_sec;
127 if (utc)
128 gmtime_r(&t, &tm_time);
129 else
130 localtime_r(&t, &tm_time);
131 }
132#else
133/* Bloated code which tries to set hw clock with better precision.
134 * On x86, even though code does set hw clock within <1ms of exact
135 * whole seconds, apparently hw clock (at least on some machines)
136 * doesn't reset internal fractional seconds to 0,
137 * making all this a pointless excercise.
138 */
139 /* If we see that we are N usec away from whole second,
140 * we'll sleep for N-ADJ usecs. ADJ corrects for the fact
141 * that CPU is not infinitely fast.
142 * On infinitely fast CPU, next wakeup would be
143 * on (exactly_next_whole_second - ADJ). On real CPUs,
144 * this difference between current time and whole second
145 * is less than ADJ (assuming system isn't heavily loaded).
146 */
147 /* Small value of 256us gives very precise sync for 2+ GHz CPUs.
148 * Slower CPUs will fail to sync and will go to bigger
149 * ADJ values. qemu-emulated armv4tl with ~100 MHz
150 * performance ends up using ADJ ~= 4*1024 and it takes
151 * 2+ secs (2 tries with successively larger ADJ)
152 * to sync. Even straced one on the same qemu (very slow)
153 * takes only 4 tries.
154 */
155#define TWEAK_USEC 256
156 unsigned adj = TWEAK_USEC;
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100157 struct tm tm_time;
Denis Vlasenko459be352007-06-17 19:09:05 +0000158 struct timeval tv;
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100159 int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
Robert Griebl1cd04452002-07-21 16:50:49 +0000160
Denys Vlasenko695fa512010-01-06 18:16:39 +0100161 /* Try to catch the moment when whole second is close */
162 while (1) {
163 unsigned rem_usec;
164 time_t t;
165
166 gettimeofday(&tv, NULL);
167
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100168 t = tv.tv_sec;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100169 rem_usec = 1000000 - tv.tv_usec;
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700170 if (rem_usec < adj) {
171 /* Close enough */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100172 small_rem:
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100173 t++;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100174 }
175
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700176 /* Prepare tm_time from t */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100177 if (utc)
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100178 gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100179 else
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100180 localtime_r(&t, &tm_time); /* same */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700181
182 if (adj >= 32*1024) {
183 break; /* 32 ms diff and still no luck?? give up trying to sync */
184 }
Denys Vlasenko695fa512010-01-06 18:16:39 +0100185
186 /* gmtime/localtime took some time, re-get cur time */
187 gettimeofday(&tv, NULL);
188
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700189 if (tv.tv_sec < t /* we are still in old second */
190 || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100191 ) {
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700192 break; /* good, we are in sync! */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100193 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700194
195 rem_usec = 1000000 - tv.tv_usec;
196 if (rem_usec < adj) {
197 t = tv.tv_sec;
198 goto small_rem; /* already close to next sec, don't sleep */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100199 }
200
201 /* Try to sync up by sleeping */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100202 usleep(rem_usec - adj);
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700203
204 /* Jump to 1ms diff, then increase fast (x2): EVERY loop
205 * takes ~1 sec, people won't like slowly converging code here!
206 */
207 //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
208 if (adj < 512)
209 adj = 512;
210 /* ... and if last "overshoot" does not look insanely big,
211 * just use it as adj increment. This makes convergence faster.
212 */
213 if (tv.tv_usec < adj * 8) {
214 adj += tv.tv_usec;
215 continue;
216 }
217 adj *= 2;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100218 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700219 /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
Denys Vlasenko695fa512010-01-06 18:16:39 +0100220 * Look for a value which makes tv_usec close to 999999 or 0.
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700221 * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
Denys Vlasenko695fa512010-01-06 18:16:39 +0100222 */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700223 //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
224#endif
225
226 tm_time.tm_isdst = 0;
227 xioctl(rtc, RTC_SET_TIME, &tm_time);
Denys Vlasenko695fa512010-01-06 18:16:39 +0100228
229 if (ENABLE_FEATURE_CLEAN_UP)
230 close(rtc);
Robert Griebl1cd04452002-07-21 16:50:49 +0000231}
232
Denis Vlasenko92258542006-11-01 10:25:35 +0000233#define HWCLOCK_OPT_LOCALTIME 0x01
234#define HWCLOCK_OPT_UTC 0x02
235#define HWCLOCK_OPT_SHOW 0x04
236#define HWCLOCK_OPT_HCTOSYS 0x08
237#define HWCLOCK_OPT_SYSTOHC 0x10
Denis Vlasenko673d4bb2007-03-07 23:02:50 +0000238#define HWCLOCK_OPT_RTCFILE 0x20
Glenn L McGrath689e4b92004-02-22 09:11:33 +0000239
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000240int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000241int hwclock_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1cd04452002-07-21 16:50:49 +0000242{
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100243 const char *rtcname = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000244 unsigned opt;
Robert Griebl6bb80872004-03-22 21:27:39 +0000245 int utc;
Robert Griebl1cd04452002-07-21 16:50:49 +0000246
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +0000247#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000248 static const char hwclock_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000249 "localtime\0" No_argument "l"
250 "utc\0" No_argument "u"
251 "show\0" No_argument "r"
252 "hctosys\0" No_argument "s"
253 "systohc\0" No_argument "w"
254 "file\0" Required_argument "f"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000255 ;
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000256 applet_long_options = hwclock_longopts;
Robert Griebl1cd04452002-07-21 16:50:49 +0000257#endif
Denis Vlasenko09196572007-07-21 13:27:44 +0000258 opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l";
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000259 opt = getopt32(argv, "lurswf:", &rtcname);
Robert Griebl1cd04452002-07-21 16:50:49 +0000260
Robert Griebl6bb80872004-03-22 21:27:39 +0000261 /* If -u or -l wasn't given check if we are using utc */
Mike Frysingerb31566e2005-04-16 04:48:48 +0000262 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
Mike Frysinger6b160e42008-02-15 02:27:19 +0000263 utc = (opt & HWCLOCK_OPT_UTC);
Robert Griebl6bb80872004-03-22 21:27:39 +0000264 else
Mike Frysinger6b160e42008-02-15 02:27:19 +0000265 utc = rtc_adjtime_is_utc();
Mike Frysingerb31566e2005-04-16 04:48:48 +0000266
Mike Frysinger6b160e42008-02-15 02:27:19 +0000267 if (opt & HWCLOCK_OPT_HCTOSYS)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100268 to_sys_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000269 else if (opt & HWCLOCK_OPT_SYSTOHC)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100270 from_sys_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000271 else
272 /* default HWCLOCK_OPT_SHOW */
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100273 show_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000274
Denis Vlasenko459be352007-06-17 19:09:05 +0000275 return 0;
Robert Griebl1cd04452002-07-21 16:50:49 +0000276}