source: MondoRescue/branches/2.2.9/mindi-busybox/util-linux/hwclock.c@ 2725

Last change on this file since 2725 was 2725, checked in by Bruno Cornec, 13 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File size: 7.0 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * Mini hwclock implementation for busybox
4 *
5 * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
6 *
[2725]7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]8*/
9
[2725]10#include "libbb.h"
11/* After libbb.h, since it needs sys/types.h on some systems */
[821]12#include <sys/utsname.h>
[2725]13#include "rtc_.h"
[821]14
[2725]15/* diff code is disabled: it's not sys/hw clock diff, it's some useless
16 * "time between hwclock was started and we saw CMOS tick" quantity.
17 * It's useless since hwclock is started at a random moment,
18 * thus the quantity is also random, useless. Showing 0.000000 does not
19 * deprive us from any useful info.
20 *
21 * SHOW_HWCLOCK_DIFF code in this file shows the difference between system
22 * and hw clock. It is useful, but not compatible with standard hwclock.
23 * Thus disabled.
24 */
25#define SHOW_HWCLOCK_DIFF 0
[821]26
27
[2725]28#if !SHOW_HWCLOCK_DIFF
29# define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc)
[821]30#endif
[2725]31static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc)
[1765]32{
[2725]33 struct tm tm_time;
34 int fd;
[1765]35
[2725]36 fd = rtc_xopen(pp_rtcname, O_RDONLY);
[1765]37
[2725]38 rtc_read_tm(&tm_time, fd);
[821]39
[2725]40#if SHOW_HWCLOCK_DIFF
41 {
42 int before = tm_time.tm_sec;
43 while (1) {
44 rtc_read_tm(&tm_time, fd);
45 gettimeofday(sys_tv, NULL);
46 if (before != tm_time.tm_sec)
47 break;
48 }
[821]49 }
[2725]50#endif
[821]51
[2725]52 if (ENABLE_FEATURE_CLEAN_UP)
53 close(fd);
[821]54
[2725]55 return rtc_tm2time(&tm_time, utc);
[821]56}
57
[2725]58static void show_clock(const char **pp_rtcname, int utc)
[821]59{
[2725]60#if SHOW_HWCLOCK_DIFF
61 struct timeval sys_tv;
62#endif
[821]63 time_t t;
[1765]64 char *cp;
[821]65
[2725]66 t = read_rtc(pp_rtcname, &sys_tv, utc);
[1765]67 cp = ctime(&t);
[2725]68 strchrnul(cp, '\n')[0] = '\0';
69#if !SHOW_HWCLOCK_DIFF
[1765]70 printf("%s 0.000000 seconds\n", cp);
[2725]71#else
72 {
73 long diff = sys_tv.tv_sec - t;
74 if (diff < 0 /*&& tv.tv_usec != 0*/) {
75 /* Why? */
76 /* diff >= 0 is ok: diff < 0, can't just use tv.tv_usec: */
77 /* 45.520820 43.520820 */
78 /* - 44.000000 - 45.000000 */
79 /* = 1.520820 = -1.479180, not -2.520820! */
80 diff++;
81 /* should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
82 sys_tv.tv_usec = 999999 - sys_tv.tv_usec;
83 }
84 printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
85 }
86#endif
[821]87}
88
[2725]89static void to_sys_clock(const char **pp_rtcname, int utc)
[821]90{
[1765]91 struct timeval tv;
[2725]92 struct timezone tz;
[821]93
[2725]94 tz.tz_minuteswest = timezone/60 - 60*daylight;
95 tz.tz_dsttime = 0;
96
97 tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
[1765]98 tv.tv_usec = 0;
99 if (settimeofday(&tv, &tz))
[2725]100 bb_perror_msg_and_die("settimeofday");
[821]101}
102
[2725]103static void from_sys_clock(const char **pp_rtcname, int utc)
[821]104{
[2725]105#if 1
[1765]106 struct timeval tv;
[2725]107 struct tm tm_time;
108 int rtc;
[821]109
[2725]110 rtc = rtc_xopen(pp_rtcname, O_WRONLY);
[1765]111 gettimeofday(&tv, NULL);
[2725]112 /* Prepare tm_time */
113 if (sizeof(time_t) == sizeof(tv.tv_sec)) {
114 if (utc)
115 gmtime_r((time_t*)&tv.tv_sec, &tm_time);
116 else
117 localtime_r((time_t*)&tv.tv_sec, &tm_time);
118 } else {
119 time_t t = tv.tv_sec;
120 if (utc)
121 gmtime_r(&t, &tm_time);
122 else
123 localtime_r(&t, &tm_time);
124 }
[821]125#else
[2725]126/* Bloated code which tries to set hw clock with better precision.
127 * On x86, even though code does set hw clock within <1ms of exact
128 * whole seconds, apparently hw clock (at least on some machines)
129 * doesn't reset internal fractional seconds to 0,
130 * making all this a pointless excercise.
131 */
132 /* If we see that we are N usec away from whole second,
133 * we'll sleep for N-ADJ usecs. ADJ corrects for the fact
134 * that CPU is not infinitely fast.
135 * On infinitely fast CPU, next wakeup would be
136 * on (exactly_next_whole_second - ADJ). On real CPUs,
137 * this difference between current time and whole second
138 * is less than ADJ (assuming system isn't heavily loaded).
139 */
140 /* Small value of 256us gives very precise sync for 2+ GHz CPUs.
141 * Slower CPUs will fail to sync and will go to bigger
142 * ADJ values. qemu-emulated armv4tl with ~100 MHz
143 * performance ends up using ADJ ~= 4*1024 and it takes
144 * 2+ secs (2 tries with successively larger ADJ)
145 * to sync. Even straced one on the same qemu (very slow)
146 * takes only 4 tries.
147 */
148#define TWEAK_USEC 256
149 unsigned adj = TWEAK_USEC;
150 struct tm tm_time;
151 struct timeval tv;
152 int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
[821]153
[2725]154 /* Try to catch the moment when whole second is close */
155 while (1) {
156 unsigned rem_usec;
157 time_t t;
[821]158
[2725]159 gettimeofday(&tv, NULL);
[821]160
[2725]161 t = tv.tv_sec;
162 rem_usec = 1000000 - tv.tv_usec;
163 if (rem_usec < adj) {
164 /* Close enough */
165 small_rem:
166 t++;
167 }
[821]168
[2725]169 /* Prepare tm_time from t */
170 if (utc)
171 gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
172 else
173 localtime_r(&t, &tm_time); /* same */
[821]174
[2725]175 if (adj >= 32*1024) {
176 break; /* 32 ms diff and still no luck?? give up trying to sync */
[821]177 }
[2725]178
179 /* gmtime/localtime took some time, re-get cur time */
180 gettimeofday(&tv, NULL);
181
182 if (tv.tv_sec < t /* we are still in old second */
183 || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
184 ) {
185 break; /* good, we are in sync! */
186 }
187
188 rem_usec = 1000000 - tv.tv_usec;
189 if (rem_usec < adj) {
190 t = tv.tv_sec;
191 goto small_rem; /* already close to next sec, don't sleep */
192 }
193
194 /* Try to sync up by sleeping */
195 usleep(rem_usec - adj);
196
197 /* Jump to 1ms diff, then increase fast (x2): EVERY loop
198 * takes ~1 sec, people won't like slowly converging code here!
199 */
200 //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
201 if (adj < 512)
202 adj = 512;
203 /* ... and if last "overshoot" does not look insanely big,
204 * just use it as adj increment. This makes convergence faster.
205 */
206 if (tv.tv_usec < adj * 8) {
207 adj += tv.tv_usec;
208 continue;
209 }
210 adj *= 2;
[821]211 }
[2725]212 /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
213 * Look for a value which makes tv_usec close to 999999 or 0.
214 * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
215 */
216 //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
217#endif
218
219 tm_time.tm_isdst = 0;
220 xioctl(rtc, RTC_SET_TIME, &tm_time);
221
222 if (ENABLE_FEATURE_CLEAN_UP)
223 close(rtc);
[821]224}
225
[1765]226#define HWCLOCK_OPT_LOCALTIME 0x01
227#define HWCLOCK_OPT_UTC 0x02
228#define HWCLOCK_OPT_SHOW 0x04
229#define HWCLOCK_OPT_HCTOSYS 0x08
230#define HWCLOCK_OPT_SYSTOHC 0x10
231#define HWCLOCK_OPT_RTCFILE 0x20
[821]232
[2725]233int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
234int hwclock_main(int argc UNUSED_PARAM, char **argv)
[821]235{
[2725]236 const char *rtcname = NULL;
[1765]237 unsigned opt;
[821]238 int utc;
239
240#if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS
[1765]241 static const char hwclock_longopts[] ALIGN1 =
242 "localtime\0" No_argument "l"
243 "utc\0" No_argument "u"
244 "show\0" No_argument "r"
245 "hctosys\0" No_argument "s"
246 "systohc\0" No_argument "w"
247 "file\0" Required_argument "f"
248 ;
249 applet_long_options = hwclock_longopts;
[821]250#endif
[1765]251 opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l";
252 opt = getopt32(argv, "lurswf:", &rtcname);
[821]253
254 /* If -u or -l wasn't given check if we are using utc */
255 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
[2725]256 utc = (opt & HWCLOCK_OPT_UTC);
[821]257 else
[2725]258 utc = rtc_adjtime_is_utc();
[821]259
[2725]260 if (opt & HWCLOCK_OPT_HCTOSYS)
261 to_sys_clock(&rtcname, utc);
262 else if (opt & HWCLOCK_OPT_SYSTOHC)
263 from_sys_clock(&rtcname, utc);
264 else
265 /* default HWCLOCK_OPT_SHOW */
266 show_clock(&rtcname, utc);
267
[1765]268 return 0;
[821]269}
Note: See TracBrowser for help on using the repository browser.