1 /* vi: set sw=4 ts=4: */
3 * Mini hwclock implementation for busybox
5 * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 /* After libbb.h, since it needs sys/types.h on some systems */
12 #include <sys/utsname.h>
15 #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
22 /* 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
26 * deprive us from any useful info.
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.
32 #define SHOW_HWCLOCK_DIFF 0
35 #if !SHOW_HWCLOCK_DIFF
36 # define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc)
38 static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc)
43 fd = rtc_xopen(pp_rtcname, O_RDONLY);
49 int before = tm.tm_sec;
52 gettimeofday(sys_tv, NULL);
53 if (before != tm.tm_sec)
59 if (ENABLE_FEATURE_CLEAN_UP)
62 return rtc_tm2time(&tm, utc);
65 static void show_clock(const char **pp_rtcname, int utc)
68 struct timeval sys_tv;
73 t = read_rtc(pp_rtcname, &sys_tv, utc);
75 strchrnul(cp, '\n')[0] = '\0';
76 #if !SHOW_HWCLOCK_DIFF
77 printf("%s 0.000000 seconds\n", cp);
80 long diff = sys_tv.tv_sec - t;
81 if (diff < 0 /*&& tv.tv_usec != 0*/) {
83 /* diff >= 0 is ok: diff < 0, can't just use tv.tv_usec: */
84 /* 45.520820 43.520820 */
85 /* - 44.000000 - 45.000000 */
86 /* = 0.520820 = -1.479180, not -2.520820! */
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;
91 printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
96 static void to_sys_clock(const char **pp_rtcname, int utc)
101 tz.tz_minuteswest = timezone/60 - 60*daylight;
104 tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
106 if (settimeofday(&tv, &tz))
107 bb_perror_msg_and_die("settimeofday");
110 static void from_sys_clock(const char **pp_rtcname, int utc)
112 #define TWEAK_USEC 200
115 unsigned adj = TWEAK_USEC;
116 int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
118 /* Try to catch the moment when whole second is close */
123 gettimeofday(&tv, NULL);
126 rem_usec = 1000000 - tv.tv_usec;
127 if (rem_usec < 1024) {
128 /* Less than 1ms to next second. Good enough */
135 gmtime_r(&t, &tm); /* may read /etc/xxx (it takes time) */
137 localtime_r(&t, &tm); /* same */
140 /* gmtime/localtime took some time, re-get cur time */
141 gettimeofday(&tv, NULL);
143 if (tv.tv_sec < t /* may happen if rem_usec was < 1024 */
144 || (tv.tv_sec == t && tv.tv_usec < 1024)
146 /* We are not too far into next second. Good. */
149 adj += 32; /* 2^(10-5) = 2^5 = 32 iterations max */
151 /* Give up trying to sync */
155 /* Try to sync up by sleeping */
156 rem_usec = 1000000 - tv.tv_usec;
157 if (rem_usec < 1024) {
158 goto small_rem; /* already close, don't sleep */
161 * Note that small adj on slow processors can make us
162 * to always overshoot tv.tv_usec < 1024 check on next
163 * iteration. That's why adj is increased on each iteration.
164 * This also allows it to be reused as a loop limiter.
166 usleep(rem_usec - adj);
169 xioctl(rtc, RTC_SET_TIME, &tm);
171 /* Debug aid to find "good" TWEAK_USEC.
172 * Look for a value which makes tv_usec close to 999999 or 0.
173 * for 2.20GHz Intel Core 2: TWEAK_USEC ~= 200
175 //bb_error_msg("tv.tv_usec:%d adj:%d", (int)tv.tv_usec, adj);
177 if (ENABLE_FEATURE_CLEAN_UP)
181 #define HWCLOCK_OPT_LOCALTIME 0x01
182 #define HWCLOCK_OPT_UTC 0x02
183 #define HWCLOCK_OPT_SHOW 0x04
184 #define HWCLOCK_OPT_HCTOSYS 0x08
185 #define HWCLOCK_OPT_SYSTOHC 0x10
186 #define HWCLOCK_OPT_RTCFILE 0x20
188 int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
189 int hwclock_main(int argc UNUSED_PARAM, char **argv)
191 const char *rtcname = NULL;
195 #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
196 static const char hwclock_longopts[] ALIGN1 =
197 "localtime\0" No_argument "l"
198 "utc\0" No_argument "u"
199 "show\0" No_argument "r"
200 "hctosys\0" No_argument "s"
201 "systohc\0" No_argument "w"
202 "file\0" Required_argument "f"
204 applet_long_options = hwclock_longopts;
206 opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l";
207 opt = getopt32(argv, "lurswf:", &rtcname);
209 /* If -u or -l wasn't given check if we are using utc */
210 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
211 utc = (opt & HWCLOCK_OPT_UTC);
213 utc = rtc_adjtime_is_utc();
215 if (opt & HWCLOCK_OPT_HCTOSYS)
216 to_sys_clock(&rtcname, utc);
217 else if (opt & HWCLOCK_OPT_SYSTOHC)
218 from_sys_clock(&rtcname, utc);
220 /* default HWCLOCK_OPT_SHOW */
221 show_clock(&rtcname, utc);