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 | 9e262f1 | 2020-12-16 13:49:10 +0100 | [diff] [blame] | 39 | |
| 40 | //musl has no __MUSL__ or similar define to check for, |
| 41 | //but its <sys/types.h> has these lines: |
| 42 | // #define __NEED_fsblkcnt_t |
| 43 | // #define __NEED_fsfilcnt_t |
| 44 | #if defined(__linux__) && defined(__NEED_fsblkcnt_t) && defined(__NEED_fsfilcnt_t) |
| 45 | # define LIBC_IS_MUSL 1 |
| 46 | # include <sys/syscall.h> |
| 47 | #else |
| 48 | # define LIBC_IS_MUSL 0 |
| 49 | #endif |
| 50 | |
| 51 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 52 | /* diff code is disabled: it's not sys/hw clock diff, it's some useless |
| 53 | * "time between hwclock was started and we saw CMOS tick" quantity. |
| 54 | * It's useless since hwclock is started at a random moment, |
| 55 | * thus the quantity is also random, useless. Showing 0.000000 does not |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 56 | * deprive us from any useful info. |
| 57 | * |
| 58 | * SHOW_HWCLOCK_DIFF code in this file shows the difference between system |
| 59 | * and hw clock. It is useful, but not compatible with standard hwclock. |
| 60 | * Thus disabled. |
| 61 | */ |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 62 | #define SHOW_HWCLOCK_DIFF 0 |
| 63 | |
| 64 | |
| 65 | #if !SHOW_HWCLOCK_DIFF |
| 66 | # define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc) |
| 67 | #endif |
| 68 | 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] | 69 | { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 70 | struct tm tm_time; |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 71 | int fd; |
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 | fd = rtc_xopen(pp_rtcname, O_RDONLY); |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 74 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 75 | rtc_read_tm(&tm_time, fd); |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 76 | |
| 77 | #if SHOW_HWCLOCK_DIFF |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 78 | { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 79 | int before = tm_time.tm_sec; |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 80 | while (1) { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 81 | rtc_read_tm(&tm_time, fd); |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 82 | xgettimeofday(sys_tv); |
Denys Vlasenko | d778e6c | 2012-04-17 19:25:13 +0200 | [diff] [blame] | 83 | if (before != (int)tm_time.tm_sec) |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 84 | break; |
| 85 | } |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 86 | } |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 87 | #endif |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 88 | |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 89 | if (ENABLE_FEATURE_CLEAN_UP) |
| 90 | close(fd); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 91 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 92 | return rtc_tm2time(&tm_time, utc); |
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 show_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 96 | { |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 97 | #if SHOW_HWCLOCK_DIFF |
| 98 | struct timeval sys_tv; |
| 99 | #endif |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 100 | time_t t = read_rtc(pp_rtcname, &sys_tv, utc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 101 | |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 102 | #if ENABLE_LOCALE_SUPPORT |
| 103 | /* Standard hwclock uses locale-specific output format */ |
| 104 | char cp[64]; |
| 105 | struct tm *ptm = localtime(&t); |
| 106 | strftime(cp, sizeof(cp), "%c", ptm); |
| 107 | #else |
| 108 | char *cp = ctime(&t); |
Ron Yorston | 8ec1ff3 | 2015-03-12 20:18:51 +0100 | [diff] [blame] | 109 | chomp(cp); |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 110 | #endif |
| 111 | |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 112 | #if !SHOW_HWCLOCK_DIFF |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 113 | printf("%s 0.000000 seconds\n", cp); |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 114 | #else |
| 115 | { |
| 116 | long diff = sys_tv.tv_sec - t; |
| 117 | if (diff < 0 /*&& tv.tv_usec != 0*/) { |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 118 | /* Why we need diff++? */ |
| 119 | /* diff >= 0 is ok: | diff < 0, can't just use tv.tv_usec: */ |
| 120 | /* 45.520820 | 43.520820 */ |
| 121 | /* - 44.000000 | - 45.000000 */ |
| 122 | /* = 1.520820 | = -1.479180, not -2.520820! */ |
Denys Vlasenko | 0c58cc7 | 2010-01-07 10:36:41 +0100 | [diff] [blame] | 123 | diff++; |
Denys Vlasenko | 319b8bb | 2011-07-08 06:40:25 +0200 | [diff] [blame] | 124 | /* 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] | 125 | sys_tv.tv_usec = 999999 - sys_tv.tv_usec; |
| 126 | } |
| 127 | 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] | 128 | } |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 129 | #endif |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Denys Vlasenko | 9e262f1 | 2020-12-16 13:49:10 +0100 | [diff] [blame] | 132 | static void set_kernel_tz(const struct timezone *tz) |
| 133 | { |
| 134 | #if LIBC_IS_MUSL |
| 135 | /* musl libc does not pass tz argument to syscall |
| 136 | * because "it's deprecated by POSIX, therefore it's fine |
| 137 | * if we gratuitously break stuff" :( |
| 138 | */ |
| 139 | #if !defined(SYS_settimeofday) && defined(SYS_settimeofday_time32) |
| 140 | # define SYS_settimeofday SYS_settimeofday_time32 |
| 141 | #endif |
| 142 | int ret = syscall(SYS_settimeofday, NULL, tz); |
| 143 | #else |
| 144 | int ret = settimeofday(NULL, tz); |
| 145 | #endif |
| 146 | if (ret) |
| 147 | bb_simple_perror_msg_and_die("settimeofday"); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * At system boot, kernel may set system time from RTC, |
| 152 | * but it knows nothing about timezones. If RTC is in local time, |
| 153 | * then system time is wrong - it is offset by timezone. |
| 154 | * --systz option corrects system time if RTC is in local time, |
| 155 | * and (always) sets in-kernel timezone. |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 156 | * (Unlike --hctosys, it does not read the RTC). |
Denys Vlasenko | 9e262f1 | 2020-12-16 13:49:10 +0100 | [diff] [blame] | 157 | * |
| 158 | * util-linux's code has this comment: |
| 159 | * RTC | settimeofday calls |
| 160 | * ------|------------------------------------------------- |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 161 | * Local | 1st) warps system time*, sets PCIL* and kernel tz |
Denys Vlasenko | 9e262f1 | 2020-12-16 13:49:10 +0100 | [diff] [blame] | 162 | * UTC | 1st) locks warp_clock 2nd) sets kernel tz |
| 163 | * * only on first call after boot |
| 164 | * (PCIL is "persistent_clock_is_local" kernel internal flag, |
| 165 | * it makes kernel save RTC in local time, not UTC.) |
| 166 | */ |
| 167 | static void set_kernel_timezone_and_clock(int utc, const struct timeval *hctosys) |
| 168 | { |
| 169 | time_t cur; |
| 170 | struct tm *broken; |
| 171 | struct timezone tz = { 0 }; |
| 172 | |
| 173 | /* if --utc, prevent kernel's warp_clock() with a dummy call */ |
| 174 | if (utc) |
| 175 | set_kernel_tz(&tz); |
| 176 | |
| 177 | /* Set kernel's timezone offset based on userspace one */ |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 178 | //It's tempting to call tzset() and use libc global "timezone" variable |
| 179 | //...but it does NOT include DST shift (IOW: it's WRONG, usually by one hour, |
| 180 | //if DST is in effect!) Thus this ridiculous dance: |
Denys Vlasenko | 9e262f1 | 2020-12-16 13:49:10 +0100 | [diff] [blame] | 181 | cur = time(NULL); |
| 182 | broken = localtime(&cur); |
| 183 | tz.tz_minuteswest = -broken->tm_gmtoff / 60; |
| 184 | /*tz.tz_dsttime = 0; already is */ |
| 185 | set_kernel_tz(&tz); /* MIGHT warp_clock() if 1st call since boot */ |
| 186 | |
Denys Vlasenko | eb0c2e2 | 2020-12-16 21:36:36 +0100 | [diff] [blame] | 187 | if (hctosys) /* it's --hctosys: set time too */ |
| 188 | xsettimeofday(hctosys); |
Denys Vlasenko | 9e262f1 | 2020-12-16 13:49:10 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 191 | static void to_sys_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 192 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 193 | struct timeval tv; |
Eddie James | 1a5d6fc | 2020-08-10 09:59:02 -0500 | [diff] [blame] | 194 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 195 | tv.tv_sec = read_rtc(pp_rtcname, NULL, utc); |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 196 | tv.tv_usec = 0; |
Denys Vlasenko | 9e262f1 | 2020-12-16 13:49:10 +0100 | [diff] [blame] | 197 | return set_kernel_timezone_and_clock(utc, &tv); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 200 | static void from_sys_clock(const char **pp_rtcname, int utc) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 201 | { |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 202 | #if 1 |
| 203 | struct timeval tv; |
| 204 | struct tm tm_time; |
| 205 | int rtc; |
| 206 | |
| 207 | rtc = rtc_xopen(pp_rtcname, O_WRONLY); |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 208 | xgettimeofday(&tv); |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 209 | /* Prepare tm_time */ |
| 210 | if (sizeof(time_t) == sizeof(tv.tv_sec)) { |
| 211 | if (utc) |
| 212 | gmtime_r((time_t*)&tv.tv_sec, &tm_time); |
| 213 | else |
| 214 | localtime_r((time_t*)&tv.tv_sec, &tm_time); |
| 215 | } else { |
| 216 | time_t t = tv.tv_sec; |
| 217 | if (utc) |
| 218 | gmtime_r(&t, &tm_time); |
| 219 | else |
| 220 | localtime_r(&t, &tm_time); |
| 221 | } |
| 222 | #else |
| 223 | /* Bloated code which tries to set hw clock with better precision. |
| 224 | * On x86, even though code does set hw clock within <1ms of exact |
| 225 | * whole seconds, apparently hw clock (at least on some machines) |
| 226 | * doesn't reset internal fractional seconds to 0, |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 227 | * making all this a pointless exercise. |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 228 | */ |
| 229 | /* If we see that we are N usec away from whole second, |
| 230 | * we'll sleep for N-ADJ usecs. ADJ corrects for the fact |
| 231 | * that CPU is not infinitely fast. |
| 232 | * On infinitely fast CPU, next wakeup would be |
| 233 | * on (exactly_next_whole_second - ADJ). On real CPUs, |
| 234 | * this difference between current time and whole second |
| 235 | * is less than ADJ (assuming system isn't heavily loaded). |
| 236 | */ |
| 237 | /* Small value of 256us gives very precise sync for 2+ GHz CPUs. |
| 238 | * Slower CPUs will fail to sync and will go to bigger |
| 239 | * ADJ values. qemu-emulated armv4tl with ~100 MHz |
| 240 | * performance ends up using ADJ ~= 4*1024 and it takes |
| 241 | * 2+ secs (2 tries with successively larger ADJ) |
| 242 | * to sync. Even straced one on the same qemu (very slow) |
| 243 | * takes only 4 tries. |
| 244 | */ |
| 245 | #define TWEAK_USEC 256 |
| 246 | unsigned adj = TWEAK_USEC; |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 247 | struct tm tm_time; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 248 | struct timeval tv; |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 249 | int rtc = rtc_xopen(pp_rtcname, O_WRONLY); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 250 | |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 251 | /* Try to catch the moment when whole second is close */ |
| 252 | while (1) { |
| 253 | unsigned rem_usec; |
| 254 | time_t t; |
| 255 | |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 256 | xgettimeofday(&tv); |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 257 | |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 258 | t = tv.tv_sec; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 259 | rem_usec = 1000000 - tv.tv_usec; |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 260 | if (rem_usec < adj) { |
| 261 | /* Close enough */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 262 | small_rem: |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 263 | t++; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 264 | } |
| 265 | |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 266 | /* Prepare tm_time from t */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 267 | if (utc) |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 268 | gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 269 | else |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 270 | localtime_r(&t, &tm_time); /* same */ |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 271 | |
| 272 | if (adj >= 32*1024) { |
| 273 | break; /* 32 ms diff and still no luck?? give up trying to sync */ |
| 274 | } |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 275 | |
| 276 | /* gmtime/localtime took some time, re-get cur time */ |
Denys Vlasenko | 3c13da3 | 2020-12-30 23:48:01 +0100 | [diff] [blame] | 277 | xgettimeofday(&tv); |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 278 | |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 279 | if (tv.tv_sec < t /* we are still in old second */ |
| 280 | || (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] | 281 | ) { |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 282 | break; /* good, we are in sync! */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 283 | } |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 284 | |
| 285 | rem_usec = 1000000 - tv.tv_usec; |
| 286 | if (rem_usec < adj) { |
| 287 | t = tv.tv_sec; |
| 288 | goto small_rem; /* already close to next sec, don't sleep */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* Try to sync up by sleeping */ |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 292 | usleep(rem_usec - adj); |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 293 | |
| 294 | /* Jump to 1ms diff, then increase fast (x2): EVERY loop |
| 295 | * takes ~1 sec, people won't like slowly converging code here! |
| 296 | */ |
| 297 | //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec); |
| 298 | if (adj < 512) |
| 299 | adj = 512; |
| 300 | /* ... and if last "overshoot" does not look insanely big, |
| 301 | * just use it as adj increment. This makes convergence faster. |
| 302 | */ |
| 303 | if (tv.tv_usec < adj * 8) { |
| 304 | adj += tv.tv_usec; |
| 305 | continue; |
| 306 | } |
| 307 | adj *= 2; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 308 | } |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 309 | /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync. |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 310 | * 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] | 311 | * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200 |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 312 | */ |
Denys Vlasenko | 60f659f | 2010-04-14 09:19:20 -0700 | [diff] [blame] | 313 | //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec); |
| 314 | #endif |
| 315 | |
| 316 | tm_time.tm_isdst = 0; |
| 317 | xioctl(rtc, RTC_SET_TIME, &tm_time); |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 318 | |
| 319 | if (ENABLE_FEATURE_CLEAN_UP) |
| 320 | close(rtc); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 323 | // hwclock from util-linux 2.36.1 |
| 324 | // hwclock [function] [option...] |
| 325 | //Functions: |
| 326 | // -r, --show display the RTC time |
| 327 | // --get display drift corrected RTC time |
| 328 | // --set set the RTC according to --date |
| 329 | // -s, --hctosys set the system time from the RTC |
| 330 | // -w, --systohc set the RTC from the system time |
| 331 | // --systz send timescale configurations to the kernel |
| 332 | // -a, --adjust adjust the RTC to account for systematic drift |
| 333 | // --predict predict the drifted RTC time according to --date |
| 334 | //Options: |
| 335 | // -u, --utc the RTC timescale is UTC |
| 336 | // -l, --localtime the RTC timescale is Local |
| 337 | // -f, --rtc <file> use an alternate file to /dev/rtc0 |
| 338 | // --directisa use the ISA bus instead of /dev/rtc0 access |
| 339 | // --date <time> date/time input for --set and --predict |
| 340 | // --delay <sec> delay used when set new RTC time |
| 341 | // --update-drift update the RTC drift factor |
| 342 | // --noadjfile do not use /etc/adjtime |
| 343 | // --adjfile <file> use an alternate file to /etc/adjtime |
| 344 | // --test dry run; implies --verbose |
| 345 | // -v, --verbose display more details |
| 346 | |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 347 | //usage:#define hwclock_trivial_usage |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 348 | //usage: IF_LONG_OPTS( |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 349 | //usage: "[-swul] [--systz] [-f DEV]" |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 350 | //usage: ) |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 351 | //usage: IF_NOT_LONG_OPTS( |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 352 | //usage: "[-swult] [-f DEV]" |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 353 | //usage: ) |
| 354 | //usage:#define hwclock_full_usage "\n\n" |
Denys Vlasenko | caba1a1 | 2020-12-16 01:19:08 +0100 | [diff] [blame] | 355 | //usage: "Show or set hardware clock (RTC)\n" |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 356 | ///////: "\n -r Show RTC time" |
Denys Vlasenko | caba1a1 | 2020-12-16 01:19:08 +0100 | [diff] [blame] | 357 | ///////-r is default, don't bother showing it in help |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 358 | //usage: "\n -s Set system time from RTC" |
| 359 | //usage: "\n -w Set RTC from system time" |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 360 | //usage: IF_LONG_OPTS( |
| 361 | //usage: "\n --systz Set in-kernel timezone, correct system time" |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 362 | //usage: "\n if RTC is kept in local time" |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 363 | //usage: ) |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 364 | //usage: "\n -f DEV Use specified device (e.g. /dev/rtc2)" |
| 365 | //usage: "\n -u Assume RTC is kept in UTC" |
| 366 | //usage: "\n -l Assume RTC is kept in local time" |
| 367 | //usage: "\n (if neither is given, read from "ADJTIME_PATH")" |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 368 | |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 369 | //TODO: get rid of incompatible -t alias to --systz? |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 370 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 371 | #define HWCLOCK_OPT_LOCALTIME 0x01 |
| 372 | #define HWCLOCK_OPT_UTC 0x02 |
| 373 | #define HWCLOCK_OPT_SHOW 0x04 |
| 374 | #define HWCLOCK_OPT_HCTOSYS 0x08 |
| 375 | #define HWCLOCK_OPT_SYSTOHC 0x10 |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 376 | #define HWCLOCK_OPT_SYSTZ 0x20 |
| 377 | #define HWCLOCK_OPT_RTCFILE 0x40 |
Glenn L McGrath | 689e4b9 | 2004-02-22 09:11:33 +0000 | [diff] [blame] | 378 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 379 | int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 380 | int hwclock_main(int argc UNUSED_PARAM, char **argv) |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 381 | { |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 382 | const char *rtcname = NULL; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 383 | unsigned opt; |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 384 | int utc; |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 385 | #if ENABLE_LONG_OPTS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 386 | static const char hwclock_longopts[] ALIGN1 = |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 387 | "localtime\0" No_argument "l" |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 388 | "utc\0" No_argument "u" |
| 389 | "show\0" No_argument "r" |
| 390 | "hctosys\0" No_argument "s" |
| 391 | "systohc\0" No_argument "w" |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 392 | "systz\0" No_argument "t" /* short opt is non-standard */ |
| 393 | "rtc\0" Required_argument "f" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 394 | ; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 395 | #endif |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 396 | opt = getopt32long(argv, |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 397 | "^""lurswtf:v" /* -v is accepted and ignored */ |
| 398 | "\0" |
| 399 | "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l", |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 400 | hwclock_longopts, |
| 401 | &rtcname |
| 402 | ); |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 403 | |
Denys Vlasenko | d3c3688 | 2020-12-16 20:55:30 +0100 | [diff] [blame] | 404 | /* 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] | 405 | if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME)) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 406 | utc = (opt & HWCLOCK_OPT_UTC); |
Robert Griebl | 6bb8087 | 2004-03-22 21:27:39 +0000 | [diff] [blame] | 407 | else |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 408 | utc = rtc_adjtime_is_utc(); |
Mike Frysinger | b31566e | 2005-04-16 04:48:48 +0000 | [diff] [blame] | 409 | |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 410 | if (opt & HWCLOCK_OPT_HCTOSYS) |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 411 | to_sys_clock(&rtcname, utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 412 | else if (opt & HWCLOCK_OPT_SYSTOHC) |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 413 | from_sys_clock(&rtcname, utc); |
Davide Cavalca | 658a437 | 2011-01-22 18:55:32 +0100 | [diff] [blame] | 414 | else if (opt & HWCLOCK_OPT_SYSTZ) |
Denys Vlasenko | 9e262f1 | 2020-12-16 13:49:10 +0100 | [diff] [blame] | 415 | set_kernel_timezone_and_clock(utc, NULL); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 416 | else |
| 417 | /* default HWCLOCK_OPT_SHOW */ |
Denys Vlasenko | 6959f6b | 2010-01-07 08:31:46 +0100 | [diff] [blame] | 418 | show_clock(&rtcname, utc); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 419 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 420 | return 0; |
Robert Griebl | 1cd0445 | 2002-07-21 16:50:49 +0000 | [diff] [blame] | 421 | } |