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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 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 |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 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 | */ |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 25 | #define SHOW_HWCLOCK_DIFF 0 |
| 26 | |
| 27 | |
| 28 | #if !SHOW_HWCLOCK_DIFF |
| 29 | # define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc) |
| 30 | #endif |
| 31 | 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] | 32 | { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 33 | struct tm tm_time; |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 34 | int fd; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 35 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 36 | fd = rtc_xopen(pp_rtcname, O_RDONLY); |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 37 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 38 | rtc_read_tm(&tm_time, fd); |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 39 | |
| 40 | #if SHOW_HWCLOCK_DIFF |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 41 | { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 42 | int before = tm_time.tm_sec; |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 43 | while (1) { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 44 | rtc_read_tm(&tm_time, fd); |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 45 | gettimeofday(sys_tv, NULL); |
Denys Vlasenko | d778e6c | 2012-04-17 19:25:13 +0200 | [diff] [blame] | 46 | if (before != (int)tm_time.tm_sec) |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 47 | break; |
| 48 | } |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 49 | } |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 50 | #endif |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 51 | |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 52 | if (ENABLE_FEATURE_CLEAN_UP) |
| 53 | close(fd); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 54 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 55 | return rtc_tm2time(&tm_time, utc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 58 | static void show_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 59 | { |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 60 | #if SHOW_HWCLOCK_DIFF |
| 61 | struct timeval sys_tv; |
| 62 | #endif |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 63 | time_t t = read_rtc(pp_rtcname, &sys_tv, utc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 64 | |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 65 | #if ENABLE_LOCALE_SUPPORT |
| 66 | /* Standard hwclock uses locale-specific output format */ |
| 67 | char cp[64]; |
| 68 | struct tm *ptm = localtime(&t); |
| 69 | strftime(cp, sizeof(cp), "%c", ptm); |
| 70 | #else |
| 71 | char *cp = ctime(&t); |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 72 | strchrnul(cp, '\n')[0] = '\0'; |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 73 | #endif |
| 74 | |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 75 | #if !SHOW_HWCLOCK_DIFF |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 76 | printf("%s 0.000000 seconds\n", cp); |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 77 | #else |
| 78 | { |
| 79 | long diff = sys_tv.tv_sec - t; |
| 80 | if (diff < 0 /*&& tv.tv_usec != 0*/) { |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 81 | /* Why we need diff++? */ |
| 82 | /* diff >= 0 is ok: | diff < 0, can't just use tv.tv_usec: */ |
| 83 | /* 45.520820 | 43.520820 */ |
| 84 | /* - 44.000000 | - 45.000000 */ |
| 85 | /* = 1.520820 | = -1.479180, not -2.520820! */ |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 86 | diff++; |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 87 | /* Should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */ |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 88 | sys_tv.tv_usec = 999999 - sys_tv.tv_usec; |
| 89 | } |
| 90 | 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] | 91 | } |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 92 | #endif |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 95 | static void to_sys_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 96 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 97 | struct timeval tv; |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 98 | struct timezone tz; |
| 99 | |
| 100 | tz.tz_minuteswest = timezone/60 - 60*daylight; |
| 101 | tz.tz_dsttime = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 102 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 103 | tv.tv_sec = read_rtc(pp_rtcname, NULL, utc); |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 104 | tv.tv_usec = 0; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 105 | if (settimeofday(&tv, &tz)) |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 106 | bb_perror_msg_and_die("settimeofday"); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 109 | static void from_sys_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 110 | { |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 111 | #if 1 |
| 112 | struct timeval tv; |
| 113 | struct tm tm_time; |
| 114 | int rtc; |
| 115 | |
| 116 | rtc = rtc_xopen(pp_rtcname, O_WRONLY); |
| 117 | gettimeofday(&tv, NULL); |
| 118 | /* Prepare tm_time */ |
| 119 | if (sizeof(time_t) == sizeof(tv.tv_sec)) { |
| 120 | if (utc) |
| 121 | gmtime_r((time_t*)&tv.tv_sec, &tm_time); |
| 122 | else |
| 123 | localtime_r((time_t*)&tv.tv_sec, &tm_time); |
| 124 | } else { |
| 125 | time_t t = tv.tv_sec; |
| 126 | if (utc) |
| 127 | gmtime_r(&t, &tm_time); |
| 128 | else |
| 129 | localtime_r(&t, &tm_time); |
| 130 | } |
| 131 | #else |
| 132 | /* Bloated code which tries to set hw clock with better precision. |
| 133 | * On x86, even though code does set hw clock within <1ms of exact |
| 134 | * whole seconds, apparently hw clock (at least on some machines) |
| 135 | * doesn't reset internal fractional seconds to 0, |
| 136 | * making all this a pointless excercise. |
| 137 | */ |
| 138 | /* If we see that we are N usec away from whole second, |
| 139 | * we'll sleep for N-ADJ usecs. ADJ corrects for the fact |
| 140 | * that CPU is not infinitely fast. |
| 141 | * On infinitely fast CPU, next wakeup would be |
| 142 | * on (exactly_next_whole_second - ADJ). On real CPUs, |
| 143 | * this difference between current time and whole second |
| 144 | * is less than ADJ (assuming system isn't heavily loaded). |
| 145 | */ |
| 146 | /* Small value of 256us gives very precise sync for 2+ GHz CPUs. |
| 147 | * Slower CPUs will fail to sync and will go to bigger |
| 148 | * ADJ values. qemu-emulated armv4tl with ~100 MHz |
| 149 | * performance ends up using ADJ ~= 4*1024 and it takes |
| 150 | * 2+ secs (2 tries with successively larger ADJ) |
| 151 | * to sync. Even straced one on the same qemu (very slow) |
| 152 | * takes only 4 tries. |
| 153 | */ |
| 154 | #define TWEAK_USEC 256 |
| 155 | unsigned adj = TWEAK_USEC; |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 156 | struct tm tm_time; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 157 | struct timeval tv; |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 158 | int rtc = rtc_xopen(pp_rtcname, O_WRONLY); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 159 | |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 160 | /* Try to catch the moment when whole second is close */ |
| 161 | while (1) { |
| 162 | unsigned rem_usec; |
| 163 | time_t t; |
| 164 | |
| 165 | gettimeofday(&tv, NULL); |
| 166 | |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 167 | t = tv.tv_sec; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 168 | rem_usec = 1000000 - tv.tv_usec; |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 169 | if (rem_usec < adj) { |
| 170 | /* Close enough */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 171 | small_rem: |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 172 | t++; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 175 | /* Prepare tm_time from t */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 176 | if (utc) |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 177 | gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 178 | else |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 179 | localtime_r(&t, &tm_time); /* same */ |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 180 | |
| 181 | if (adj >= 32*1024) { |
| 182 | break; /* 32 ms diff and still no luck?? give up trying to sync */ |
| 183 | } |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 184 | |
| 185 | /* gmtime/localtime took some time, re-get cur time */ |
| 186 | gettimeofday(&tv, NULL); |
| 187 | |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 188 | if (tv.tv_sec < t /* we are still in old second */ |
| 189 | || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 190 | ) { |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 191 | break; /* good, we are in sync! */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 192 | } |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 193 | |
| 194 | rem_usec = 1000000 - tv.tv_usec; |
| 195 | if (rem_usec < adj) { |
| 196 | t = tv.tv_sec; |
| 197 | goto small_rem; /* already close to next sec, don't sleep */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* Try to sync up by sleeping */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 201 | usleep(rem_usec - adj); |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 202 | |
| 203 | /* Jump to 1ms diff, then increase fast (x2): EVERY loop |
| 204 | * takes ~1 sec, people won't like slowly converging code here! |
| 205 | */ |
| 206 | //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec); |
| 207 | if (adj < 512) |
| 208 | adj = 512; |
| 209 | /* ... and if last "overshoot" does not look insanely big, |
| 210 | * just use it as adj increment. This makes convergence faster. |
| 211 | */ |
| 212 | if (tv.tv_usec < adj * 8) { |
| 213 | adj += tv.tv_usec; |
| 214 | continue; |
| 215 | } |
| 216 | adj *= 2; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 217 | } |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 218 | /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync. |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 219 | * Look for a value which makes tv_usec close to 999999 or 0. |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 220 | * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200 |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 221 | */ |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 222 | //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec); |
| 223 | #endif |
| 224 | |
| 225 | tm_time.tm_isdst = 0; |
| 226 | xioctl(rtc, RTC_SET_TIME, &tm_time); |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 227 | |
| 228 | if (ENABLE_FEATURE_CLEAN_UP) |
| 229 | close(rtc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 232 | /* |
| 233 | * At system boot, kernel may set system time from RTC, |
| 234 | * but it knows nothing about timezones. If RTC is in local time, |
| 235 | * then system time is wrong - it is offset by timezone. |
| 236 | * This option corrects system time if RTC is in local time, |
| 237 | * and (always) sets in-kernel timezone. |
| 238 | * |
| 239 | * This is an alternate option to --hctosys that does not read the |
| 240 | * hardware clock. |
| 241 | */ |
| 242 | static void set_system_clock_timezone(int utc) |
| 243 | { |
| 244 | struct timeval tv; |
| 245 | struct tm *broken; |
| 246 | struct timezone tz; |
| 247 | |
| 248 | gettimeofday(&tv, NULL); |
| 249 | broken = localtime(&tv.tv_sec); |
| 250 | tz.tz_minuteswest = timezone / 60; |
| 251 | if (broken->tm_isdst) |
| 252 | tz.tz_minuteswest -= 60; |
| 253 | tz.tz_dsttime = 0; |
| 254 | gettimeofday(&tv, NULL); |
| 255 | if (!utc) |
| 256 | tv.tv_sec += tz.tz_minuteswest * 60; |
| 257 | if (settimeofday(&tv, &tz)) |
| 258 | bb_perror_msg_and_die("settimeofday"); |
| 259 | } |
| 260 | |
| 261 | //usage:#define hwclock_trivial_usage |
| 262 | //usage: IF_FEATURE_HWCLOCK_LONG_OPTIONS( |
| 263 | //usage: "[-r|--show] [-s|--hctosys] [-w|--systohc] [-t|--systz]" |
| 264 | //usage: " [-l|--localtime] [-u|--utc]" |
| 265 | //usage: " [-f|--rtc FILE]" |
| 266 | //usage: ) |
| 267 | //usage: IF_NOT_FEATURE_HWCLOCK_LONG_OPTIONS( |
| 268 | //usage: "[-r] [-s] [-w] [-t] [-l] [-u] [-f FILE]" |
| 269 | //usage: ) |
| 270 | //usage:#define hwclock_full_usage "\n\n" |
| 271 | //usage: "Query and set hardware clock (RTC)\n" |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 272 | //usage: "\n -r Show hardware clock time" |
| 273 | //usage: "\n -s Set system time from hardware clock" |
| 274 | //usage: "\n -w Set hardware clock from system time" |
| 275 | //usage: "\n -t Set in-kernel timezone, correct system time" |
| 276 | //usage: "\n if hardware clock is in local time" |
Denys Vlasenko | 46465ec | 2011-07-04 04:34:57 +0200 | [diff] [blame] | 277 | //usage: "\n -u Assume hardware clock is kept in UTC" |
| 278 | //usage: "\n -l Assume hardware clock is kept in local time" |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 279 | //usage: "\n -f FILE Use specified device (e.g. /dev/rtc2)" |
| 280 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 281 | #define HWCLOCK_OPT_LOCALTIME 0x01 |
| 282 | #define HWCLOCK_OPT_UTC 0x02 |
| 283 | #define HWCLOCK_OPT_SHOW 0x04 |
| 284 | #define HWCLOCK_OPT_HCTOSYS 0x08 |
| 285 | #define HWCLOCK_OPT_SYSTOHC 0x10 |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 286 | #define HWCLOCK_OPT_SYSTZ 0x20 |
| 287 | #define HWCLOCK_OPT_RTCFILE 0x40 |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 288 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 289 | int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 290 | int hwclock_main(int argc UNUSED_PARAM, char **argv) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 291 | { |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 292 | const char *rtcname = NULL; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 293 | unsigned opt; |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 294 | int utc; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 295 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 296 | #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 297 | static const char hwclock_longopts[] ALIGN1 = |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 298 | "localtime\0" No_argument "l" /* short opt is non-standard */ |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 299 | "utc\0" No_argument "u" |
| 300 | "show\0" No_argument "r" |
| 301 | "hctosys\0" No_argument "s" |
| 302 | "systohc\0" No_argument "w" |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 303 | "systz\0" No_argument "t" /* short opt is non-standard */ |
| 304 | "rtc\0" Required_argument "f" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 305 | ; |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 306 | applet_long_options = hwclock_longopts; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 307 | #endif |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 308 | opt_complementary = "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l"; |
| 309 | opt = getopt32(argv, "lurswtf:", &rtcname); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 310 | |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 311 | /* 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] | 312 | if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME)) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 313 | utc = (opt & HWCLOCK_OPT_UTC); |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 314 | else |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 315 | utc = rtc_adjtime_is_utc(); |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 316 | |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 317 | if (opt & HWCLOCK_OPT_HCTOSYS) |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 318 | to_sys_clock(&rtcname, utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 319 | else if (opt & HWCLOCK_OPT_SYSTOHC) |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 320 | from_sys_clock(&rtcname, utc); |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 321 | else if (opt & HWCLOCK_OPT_SYSTZ) |
| 322 | set_system_clock_timezone(utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 323 | else |
| 324 | /* default HWCLOCK_OPT_SHOW */ |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 325 | show_clock(&rtcname, utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 326 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 327 | return 0; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 328 | } |