Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 3 | * Mini hwclock implementation for busybox |
| 4 | * |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 5 | * Copyright (C) 2002 Robert Griebl <griebl@gmx.de> |
| 6 | * |
Bernhard Reutner-Fischer | 5cf905a | 2006-03-31 22:36:15 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 8 | */ |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 9 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 11 | /* After libbb.h, since it needs sys/types.h on some systems */ |
| 12 | #include <sys/utsname.h> |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 13 | #include "rtc_.h" |
Eric Andersen | 8882ea5 | 2002-12-11 03:41:28 +0000 | [diff] [blame] | 14 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 15 | #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 16 | # ifndef _GNU_SOURCE |
| 17 | # define _GNU_SOURCE |
| 18 | # endif |
| 19 | #endif |
| 20 | |
Denis Vlasenko | 673d4bb | 2007-03-07 23:02:50 +0000 | [diff] [blame] | 21 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 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 |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame^] | 26 | * 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 Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 32 | #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 |
| 38 | static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc) |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 39 | { |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 40 | struct tm tm; |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 41 | int fd; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 42 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 43 | fd = rtc_xopen(pp_rtcname, O_RDONLY); |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 44 | |
| 45 | rtc_read_tm(&tm, fd); |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 46 | |
| 47 | #if SHOW_HWCLOCK_DIFF |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame^] | 48 | { |
| 49 | int before = tm.tm_sec; |
| 50 | while (1) { |
| 51 | rtc_read_tm(&tm, fd); |
| 52 | gettimeofday(sys_tv, NULL); |
| 53 | if (before != tm.tm_sec) |
| 54 | break; |
| 55 | } |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 56 | } |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 57 | #endif |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 58 | |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 59 | if (ENABLE_FEATURE_CLEAN_UP) |
| 60 | close(fd); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 61 | |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 62 | return rtc_tm2time(&tm, utc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 65 | static void show_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 66 | { |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame^] | 67 | #if SHOW_HWCLOCK_DIFF |
| 68 | struct timeval sys_tv; |
| 69 | #endif |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 70 | time_t t; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 71 | char *cp; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 72 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 73 | t = read_rtc(pp_rtcname, &sys_tv, utc); |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 74 | cp = ctime(&t); |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 75 | strchrnul(cp, '\n')[0] = '\0'; |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame^] | 76 | #if !SHOW_HWCLOCK_DIFF |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 77 | printf("%s 0.000000 seconds\n", cp); |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame^] | 78 | #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 */ |
| 86 | /* = 0.520820 = -1.479180, not -2.520820! */ |
| 87 | 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 Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 92 | } |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 93 | #endif |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 96 | static void to_sys_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 97 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 98 | struct timeval tv; |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 99 | struct timezone tz; |
| 100 | |
| 101 | tz.tz_minuteswest = timezone/60 - 60*daylight; |
| 102 | tz.tz_dsttime = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 103 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 104 | tv.tv_sec = read_rtc(pp_rtcname, NULL, utc); |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 105 | tv.tv_usec = 0; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 106 | if (settimeofday(&tv, &tz)) |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 107 | bb_perror_msg_and_die("settimeofday"); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 110 | static void from_sys_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 111 | { |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 112 | #define TWEAK_USEC 200 |
| 113 | struct tm tm; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 114 | struct timeval tv; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 115 | unsigned adj = TWEAK_USEC; |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 116 | int rtc = rtc_xopen(pp_rtcname, O_WRONLY); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 117 | |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 118 | /* Try to catch the moment when whole second is close */ |
| 119 | while (1) { |
| 120 | unsigned rem_usec; |
| 121 | time_t t; |
| 122 | |
| 123 | gettimeofday(&tv, NULL); |
| 124 | |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 125 | t = tv.tv_sec; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 126 | rem_usec = 1000000 - tv.tv_usec; |
| 127 | if (rem_usec < 1024) { |
| 128 | /* Less than 1ms to next second. Good enough */ |
| 129 | small_rem: |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 130 | t++; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* Prepare tm */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 134 | if (utc) |
| 135 | gmtime_r(&t, &tm); /* may read /etc/xxx (it takes time) */ |
| 136 | else |
| 137 | localtime_r(&t, &tm); /* same */ |
| 138 | tm.tm_isdst = 0; |
| 139 | |
| 140 | /* gmtime/localtime took some time, re-get cur time */ |
| 141 | gettimeofday(&tv, NULL); |
| 142 | |
| 143 | if (tv.tv_sec < t /* may happen if rem_usec was < 1024 */ |
| 144 | || (tv.tv_sec == t && tv.tv_usec < 1024) |
| 145 | ) { |
| 146 | /* We are not too far into next second. Good. */ |
| 147 | break; |
| 148 | } |
| 149 | adj += 32; /* 2^(10-5) = 2^5 = 32 iterations max */ |
| 150 | if (adj >= 1024) { |
| 151 | /* Give up trying to sync */ |
| 152 | break; |
| 153 | } |
| 154 | |
| 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 */ |
| 159 | } |
| 160 | /* Need to 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. |
| 165 | */ |
| 166 | usleep(rem_usec - adj); |
| 167 | } |
| 168 | |
| 169 | xioctl(rtc, RTC_SET_TIME, &tm); |
| 170 | |
| 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 |
| 174 | */ |
| 175 | //bb_error_msg("tv.tv_usec:%d adj:%d", (int)tv.tv_usec, adj); |
| 176 | |
| 177 | if (ENABLE_FEATURE_CLEAN_UP) |
| 178 | close(rtc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | 673d4bb | 2007-03-07 23:02:50 +0000 | [diff] [blame] | 186 | #define HWCLOCK_OPT_RTCFILE 0x20 |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 187 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 188 | int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 189 | int hwclock_main(int argc UNUSED_PARAM, char **argv) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 190 | { |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 191 | const char *rtcname = NULL; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 192 | unsigned opt; |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 193 | int utc; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 194 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 195 | #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 196 | static const char hwclock_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 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" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 203 | ; |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 204 | applet_long_options = hwclock_longopts; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 205 | #endif |
Denis Vlasenko | 0919657 | 2007-07-21 13:27:44 +0000 | [diff] [blame] | 206 | opt_complementary = "r--ws:w--rs:s--wr:l--u:u--l"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 207 | opt = getopt32(argv, "lurswf:", &rtcname); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 208 | |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 209 | /* If -u or -l wasn't given check if we are using utc */ |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 210 | if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME)) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 211 | utc = (opt & HWCLOCK_OPT_UTC); |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 212 | else |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 213 | utc = rtc_adjtime_is_utc(); |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 214 | |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 215 | if (opt & HWCLOCK_OPT_HCTOSYS) |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 216 | to_sys_clock(&rtcname, utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 217 | else if (opt & HWCLOCK_OPT_SYSTOHC) |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 218 | from_sys_clock(&rtcname, utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 219 | else |
| 220 | /* default HWCLOCK_OPT_SHOW */ |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 221 | show_clock(&rtcname, utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 222 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 223 | return 0; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 224 | } |