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. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 8 | */ |
| 9 | //config:config HWCLOCK |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "hwclock (5.8 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 11 | //config: default y |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: The hwclock utility is used to read and set the hardware clock |
| 14 | //config: on a system. This is primarily used to set the current time on |
| 15 | //config: shutdown in the hardware clock, so the hardware will keep the |
| 16 | //config: correct time when Linux is _not_ running. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 17 | //config: |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 18 | //config:config FEATURE_HWCLOCK_ADJTIME_FHS |
| 19 | //config: bool "Use FHS /var/lib/hwclock/adjtime" |
| 20 | //config: default n # util-linux-ng in Fedora 13 still uses /etc/adjtime |
| 21 | //config: depends on HWCLOCK |
| 22 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 23 | //config: Starting with FHS 2.3, the adjtime state file is supposed to exist |
| 24 | //config: at /var/lib/hwclock/adjtime instead of /etc/adjtime. If you wish |
| 25 | //config: to use the FHS behavior, answer Y here, otherwise answer N for the |
| 26 | //config: classic /etc/adjtime path. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 27 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 28 | //config: pathname.com/fhs/pub/fhs-2.3.html#VARLIBHWCLOCKSTATEDIRECTORYFORHWCLO |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 29 | |
| 30 | //applet:IF_HWCLOCK(APPLET(hwclock, BB_DIR_SBIN, BB_SUID_DROP)) |
| 31 | |
| 32 | //kbuild:lib-$(CONFIG_HWCLOCK) += hwclock.o |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 33 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 34 | #include "libbb.h" |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 35 | /* After libbb.h, since it needs sys/types.h on some systems */ |
| 36 | #include <sys/utsname.h> |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 37 | #include "rtc_.h" |
Eric Andersen | 8882ea5 | 2002-12-11 03:41:28 +0000 | [diff] [blame] | 38 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 39 | /* diff code is disabled: it's not sys/hw clock diff, it's some useless |
| 40 | * "time between hwclock was started and we saw CMOS tick" quantity. |
| 41 | * It's useless since hwclock is started at a random moment, |
| 42 | * thus the quantity is also random, useless. Showing 0.000000 does not |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 43 | * deprive us from any useful info. |
| 44 | * |
| 45 | * SHOW_HWCLOCK_DIFF code in this file shows the difference between system |
| 46 | * and hw clock. It is useful, but not compatible with standard hwclock. |
| 47 | * Thus disabled. |
| 48 | */ |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 49 | #define SHOW_HWCLOCK_DIFF 0 |
| 50 | |
| 51 | |
| 52 | #if !SHOW_HWCLOCK_DIFF |
| 53 | # define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc) |
| 54 | #endif |
| 55 | 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] | 56 | { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 57 | struct tm tm_time; |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 58 | int fd; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 59 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 60 | fd = rtc_xopen(pp_rtcname, O_RDONLY); |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 61 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 62 | rtc_read_tm(&tm_time, fd); |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 63 | |
| 64 | #if SHOW_HWCLOCK_DIFF |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 65 | { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 66 | int before = tm_time.tm_sec; |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 67 | while (1) { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 68 | rtc_read_tm(&tm_time, fd); |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 69 | gettimeofday(sys_tv, NULL); |
Denys Vlasenko | d778e6c | 2012-04-17 19:25:13 +0200 | [diff] [blame] | 70 | if (before != (int)tm_time.tm_sec) |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 71 | break; |
| 72 | } |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 73 | } |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 74 | #endif |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 75 | |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 76 | if (ENABLE_FEATURE_CLEAN_UP) |
| 77 | close(fd); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 78 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 79 | return rtc_tm2time(&tm_time, utc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 82 | static void show_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 83 | { |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 84 | #if SHOW_HWCLOCK_DIFF |
| 85 | struct timeval sys_tv; |
| 86 | #endif |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 87 | time_t t = read_rtc(pp_rtcname, &sys_tv, utc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 88 | |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 89 | #if ENABLE_LOCALE_SUPPORT |
| 90 | /* Standard hwclock uses locale-specific output format */ |
| 91 | char cp[64]; |
| 92 | struct tm *ptm = localtime(&t); |
| 93 | strftime(cp, sizeof(cp), "%c", ptm); |
| 94 | #else |
| 95 | char *cp = ctime(&t); |
Ron Yorston | 8ec1ff3 | 2015-03-12 20:18:51 +0100 | [diff] [blame] | 96 | chomp(cp); |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 97 | #endif |
| 98 | |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 99 | #if !SHOW_HWCLOCK_DIFF |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 100 | printf("%s 0.000000 seconds\n", cp); |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 101 | #else |
| 102 | { |
| 103 | long diff = sys_tv.tv_sec - t; |
| 104 | if (diff < 0 /*&& tv.tv_usec != 0*/) { |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 105 | /* Why we need diff++? */ |
| 106 | /* diff >= 0 is ok: | diff < 0, can't just use tv.tv_usec: */ |
| 107 | /* 45.520820 | 43.520820 */ |
| 108 | /* - 44.000000 | - 45.000000 */ |
| 109 | /* = 1.520820 | = -1.479180, not -2.520820! */ |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 110 | diff++; |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 111 | /* 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] | 112 | sys_tv.tv_usec = 999999 - sys_tv.tv_usec; |
| 113 | } |
| 114 | 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] | 115 | } |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 116 | #endif |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 119 | static void to_sys_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 120 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 121 | struct timeval tv; |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 122 | struct timezone tz; |
| 123 | |
Eddie James | 1a5d6fc | 2020-08-10 09:59:02 -0500 | [diff] [blame] | 124 | tz.tz_minuteswest = timezone / 60; |
Denys Vlasenko | 589051b | 2014-02-25 17:52:10 +0100 | [diff] [blame] | 125 | /* ^^^ used to also subtract 60*daylight, but it's wrong: |
| 126 | * daylight!=0 means "this timezone has some DST |
| 127 | * during the year", not "DST is in effect now". |
| 128 | */ |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 129 | tz.tz_dsttime = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 130 | |
Eddie James | 1a5d6fc | 2020-08-10 09:59:02 -0500 | [diff] [blame] | 131 | /* glibc v2.31+ returns an error if both args are non-NULL */ |
| 132 | if (settimeofday(NULL, &tz)) |
| 133 | bb_simple_perror_msg_and_die("settimeofday"); |
| 134 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 135 | tv.tv_sec = read_rtc(pp_rtcname, NULL, utc); |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 136 | tv.tv_usec = 0; |
Eddie James | 1a5d6fc | 2020-08-10 09:59:02 -0500 | [diff] [blame] | 137 | if (settimeofday(&tv, NULL)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 138 | bb_simple_perror_msg_and_die("settimeofday"); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 141 | static void from_sys_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 142 | { |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 143 | #if 1 |
| 144 | struct timeval tv; |
| 145 | struct tm tm_time; |
| 146 | int rtc; |
| 147 | |
| 148 | rtc = rtc_xopen(pp_rtcname, O_WRONLY); |
| 149 | gettimeofday(&tv, NULL); |
| 150 | /* Prepare tm_time */ |
| 151 | if (sizeof(time_t) == sizeof(tv.tv_sec)) { |
| 152 | if (utc) |
| 153 | gmtime_r((time_t*)&tv.tv_sec, &tm_time); |
| 154 | else |
| 155 | localtime_r((time_t*)&tv.tv_sec, &tm_time); |
| 156 | } else { |
| 157 | time_t t = tv.tv_sec; |
| 158 | if (utc) |
| 159 | gmtime_r(&t, &tm_time); |
| 160 | else |
| 161 | localtime_r(&t, &tm_time); |
| 162 | } |
| 163 | #else |
| 164 | /* Bloated code which tries to set hw clock with better precision. |
| 165 | * On x86, even though code does set hw clock within <1ms of exact |
| 166 | * whole seconds, apparently hw clock (at least on some machines) |
| 167 | * doesn't reset internal fractional seconds to 0, |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 168 | * making all this a pointless exercise. |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 169 | */ |
| 170 | /* If we see that we are N usec away from whole second, |
| 171 | * we'll sleep for N-ADJ usecs. ADJ corrects for the fact |
| 172 | * that CPU is not infinitely fast. |
| 173 | * On infinitely fast CPU, next wakeup would be |
| 174 | * on (exactly_next_whole_second - ADJ). On real CPUs, |
| 175 | * this difference between current time and whole second |
| 176 | * is less than ADJ (assuming system isn't heavily loaded). |
| 177 | */ |
| 178 | /* Small value of 256us gives very precise sync for 2+ GHz CPUs. |
| 179 | * Slower CPUs will fail to sync and will go to bigger |
| 180 | * ADJ values. qemu-emulated armv4tl with ~100 MHz |
| 181 | * performance ends up using ADJ ~= 4*1024 and it takes |
| 182 | * 2+ secs (2 tries with successively larger ADJ) |
| 183 | * to sync. Even straced one on the same qemu (very slow) |
| 184 | * takes only 4 tries. |
| 185 | */ |
| 186 | #define TWEAK_USEC 256 |
| 187 | unsigned adj = TWEAK_USEC; |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 188 | struct tm tm_time; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 189 | struct timeval tv; |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 190 | int rtc = rtc_xopen(pp_rtcname, O_WRONLY); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 191 | |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 192 | /* Try to catch the moment when whole second is close */ |
| 193 | while (1) { |
| 194 | unsigned rem_usec; |
| 195 | time_t t; |
| 196 | |
| 197 | gettimeofday(&tv, NULL); |
| 198 | |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 199 | t = tv.tv_sec; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 200 | rem_usec = 1000000 - tv.tv_usec; |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 201 | if (rem_usec < adj) { |
| 202 | /* Close enough */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 203 | small_rem: |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 204 | t++; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 207 | /* Prepare tm_time from t */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 208 | if (utc) |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 209 | gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 210 | else |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 211 | localtime_r(&t, &tm_time); /* same */ |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 212 | |
| 213 | if (adj >= 32*1024) { |
| 214 | break; /* 32 ms diff and still no luck?? give up trying to sync */ |
| 215 | } |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 216 | |
| 217 | /* gmtime/localtime took some time, re-get cur time */ |
| 218 | gettimeofday(&tv, NULL); |
| 219 | |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 220 | if (tv.tv_sec < t /* we are still in old second */ |
| 221 | || (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] | 222 | ) { |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 223 | break; /* good, we are in sync! */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 224 | } |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 225 | |
| 226 | rem_usec = 1000000 - tv.tv_usec; |
| 227 | if (rem_usec < adj) { |
| 228 | t = tv.tv_sec; |
| 229 | goto small_rem; /* already close to next sec, don't sleep */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* Try to sync up by sleeping */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 233 | usleep(rem_usec - adj); |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 234 | |
| 235 | /* Jump to 1ms diff, then increase fast (x2): EVERY loop |
| 236 | * takes ~1 sec, people won't like slowly converging code here! |
| 237 | */ |
| 238 | //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec); |
| 239 | if (adj < 512) |
| 240 | adj = 512; |
| 241 | /* ... and if last "overshoot" does not look insanely big, |
| 242 | * just use it as adj increment. This makes convergence faster. |
| 243 | */ |
| 244 | if (tv.tv_usec < adj * 8) { |
| 245 | adj += tv.tv_usec; |
| 246 | continue; |
| 247 | } |
| 248 | adj *= 2; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 249 | } |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 250 | /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync. |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 251 | * 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] | 252 | * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200 |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 253 | */ |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 254 | //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec); |
| 255 | #endif |
| 256 | |
| 257 | tm_time.tm_isdst = 0; |
| 258 | xioctl(rtc, RTC_SET_TIME, &tm_time); |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 259 | |
| 260 | if (ENABLE_FEATURE_CLEAN_UP) |
| 261 | close(rtc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 264 | /* |
| 265 | * At system boot, kernel may set system time from RTC, |
| 266 | * but it knows nothing about timezones. If RTC is in local time, |
| 267 | * then system time is wrong - it is offset by timezone. |
| 268 | * This option corrects system time if RTC is in local time, |
| 269 | * and (always) sets in-kernel timezone. |
| 270 | * |
| 271 | * This is an alternate option to --hctosys that does not read the |
| 272 | * hardware clock. |
| 273 | */ |
| 274 | static void set_system_clock_timezone(int utc) |
| 275 | { |
| 276 | struct timeval tv; |
| 277 | struct tm *broken; |
| 278 | struct timezone tz; |
| 279 | |
| 280 | gettimeofday(&tv, NULL); |
| 281 | broken = localtime(&tv.tv_sec); |
| 282 | tz.tz_minuteswest = timezone / 60; |
Denys Vlasenko | 589051b | 2014-02-25 17:52:10 +0100 | [diff] [blame] | 283 | if (broken->tm_isdst > 0) |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 284 | tz.tz_minuteswest -= 60; |
| 285 | tz.tz_dsttime = 0; |
| 286 | gettimeofday(&tv, NULL); |
| 287 | if (!utc) |
| 288 | tv.tv_sec += tz.tz_minuteswest * 60; |
Eddie James | 1a5d6fc | 2020-08-10 09:59:02 -0500 | [diff] [blame] | 289 | |
| 290 | /* glibc v2.31+ returns an error if both args are non-NULL */ |
| 291 | if (settimeofday(NULL, &tz)) |
| 292 | bb_simple_perror_msg_and_die("settimeofday"); |
| 293 | if (settimeofday(&tv, NULL)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 294 | bb_simple_perror_msg_and_die("settimeofday"); |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | //usage:#define hwclock_trivial_usage |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 298 | //usage: IF_LONG_OPTS( |
Denys Vlasenko | caba1a1 | 2020-12-16 01:19:08 +0100 | [diff] [blame^] | 299 | //usage: "[-swu] [--systz] [--localtime] [-f FILE]" |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 300 | //usage: ) |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 301 | //usage: IF_NOT_LONG_OPTS( |
Denys Vlasenko | caba1a1 | 2020-12-16 01:19:08 +0100 | [diff] [blame^] | 302 | //usage: "[-swtlu] [-f FILE]" |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 303 | //usage: ) |
| 304 | //usage:#define hwclock_full_usage "\n\n" |
Denys Vlasenko | caba1a1 | 2020-12-16 01:19:08 +0100 | [diff] [blame^] | 305 | //usage: "Show or set hardware clock (RTC)\n" |
| 306 | ///////: "\n -r Show hardware clock time" |
| 307 | ///////-r is default, don't bother showing it in help |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 308 | //usage: "\n -s Set system time from hardware clock" |
| 309 | //usage: "\n -w Set hardware clock from system time" |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 310 | //usage: IF_LONG_OPTS( |
| 311 | //usage: "\n --systz Set in-kernel timezone, correct system time" |
| 312 | //usage: ) |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 313 | //usage: "\n if hardware clock is in local time" |
Denys Vlasenko | 46465ec | 2011-07-04 04:34:57 +0200 | [diff] [blame] | 314 | //usage: "\n -u Assume hardware clock is kept in UTC" |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 315 | //usage: IF_LONG_OPTS( |
| 316 | //usage: "\n --localtime Assume hardware clock is kept in local time" |
| 317 | //usage: ) |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 318 | //usage: "\n -f FILE Use specified device (e.g. /dev/rtc2)" |
| 319 | |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 320 | //TODO: get rid of incompatible -t and -l aliases to --systz and --localtime |
| 321 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 322 | #define HWCLOCK_OPT_LOCALTIME 0x01 |
| 323 | #define HWCLOCK_OPT_UTC 0x02 |
| 324 | #define HWCLOCK_OPT_SHOW 0x04 |
| 325 | #define HWCLOCK_OPT_HCTOSYS 0x08 |
| 326 | #define HWCLOCK_OPT_SYSTOHC 0x10 |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 327 | #define HWCLOCK_OPT_SYSTZ 0x20 |
| 328 | #define HWCLOCK_OPT_RTCFILE 0x40 |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 329 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 330 | int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 331 | int hwclock_main(int argc UNUSED_PARAM, char **argv) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 332 | { |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 333 | const char *rtcname = NULL; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 334 | unsigned opt; |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 335 | int utc; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 336 | |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 337 | #if ENABLE_LONG_OPTS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 338 | static const char hwclock_longopts[] ALIGN1 = |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 339 | "localtime\0" No_argument "l" /* short opt is non-standard */ |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 340 | "utc\0" No_argument "u" |
| 341 | "show\0" No_argument "r" |
| 342 | "hctosys\0" No_argument "s" |
| 343 | "systohc\0" No_argument "w" |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 344 | "systz\0" No_argument "t" /* short opt is non-standard */ |
| 345 | "rtc\0" Required_argument "f" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 346 | ; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 347 | #endif |
Denys Vlasenko | 589051b | 2014-02-25 17:52:10 +0100 | [diff] [blame] | 348 | |
| 349 | /* Initialize "timezone" (libc global variable) */ |
| 350 | tzset(); |
| 351 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 352 | opt = getopt32long(argv, |
| 353 | "^lurswtf:" "\0" "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l", |
| 354 | hwclock_longopts, |
| 355 | &rtcname |
| 356 | ); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 357 | |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 358 | /* 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] | 359 | if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME)) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 360 | utc = (opt & HWCLOCK_OPT_UTC); |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 361 | else |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 362 | utc = rtc_adjtime_is_utc(); |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 363 | |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 364 | if (opt & HWCLOCK_OPT_HCTOSYS) |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 365 | to_sys_clock(&rtcname, utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 366 | else if (opt & HWCLOCK_OPT_SYSTOHC) |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 367 | from_sys_clock(&rtcname, utc); |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 368 | else if (opt & HWCLOCK_OPT_SYSTZ) |
| 369 | set_system_clock_timezone(utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 370 | else |
| 371 | /* default HWCLOCK_OPT_SHOW */ |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 372 | show_clock(&rtcname, utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 373 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 374 | return 0; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 375 | } |