blob: 723b09589395c9ea882146cf2686792018cba440 [file] [log] [blame]
Robert Griebl1cd04452002-07-21 16:50:49 +00001/* vi: set sw=4 ts=4: */
2/*
Robert Griebl6859d762002-08-05 02:57:12 +00003 * Mini hwclock implementation for busybox
4 *
Robert Griebl1cd04452002-07-21 16:50:49 +00005 * Copyright (C) 2002 Robert Griebl <griebl@gmx.de>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenkodd898c92016-11-23 11:46:32 +01008 */
9//config:config HWCLOCK
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "hwclock (5.8 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011//config: default y
Denys Vlasenkodd898c92016-11-23 11:46:32 +010012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//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 Vlasenkodd898c92016-11-23 11:46:32 +010017//config:
Denys Vlasenkodd898c92016-11-23 11:46:32 +010018//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 Vlasenko72089cf2017-07-21 09:50:55 +020023//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 Vlasenkodd898c92016-11-23 11:46:32 +010027//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020028//config: pathname.com/fhs/pub/fhs-2.3.html#VARLIBHWCLOCKSTATEDIRECTORYFORHWCLO
Denys Vlasenkodd898c92016-11-23 11:46:32 +010029
30//applet:IF_HWCLOCK(APPLET(hwclock, BB_DIR_SBIN, BB_SUID_DROP))
31
32//kbuild:lib-$(CONFIG_HWCLOCK) += hwclock.o
Robert Griebl1cd04452002-07-21 16:50:49 +000033
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020035/* After libbb.h, since it needs sys/types.h on some systems */
36#include <sys/utsname.h>
Mike Frysinger6b160e42008-02-15 02:27:19 +000037#include "rtc_.h"
Eric Andersen8882ea52002-12-11 03:41:28 +000038
Denys Vlasenko9e262f12020-12-16 13:49:10 +010039
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 Vlasenko6959f6b2010-01-07 08:31:46 +010052/* 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 Vlasenko0c58cc72010-01-07 10:36:41 +010056 * 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 Vlasenko6959f6b2010-01-07 08:31:46 +010062#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
68static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc)
Denis Vlasenko92258542006-11-01 10:25:35 +000069{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010070 struct tm tm_time;
Mike Frysinger6b160e42008-02-15 02:27:19 +000071 int fd;
Robert Griebl1cd04452002-07-21 16:50:49 +000072
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010073 fd = rtc_xopen(pp_rtcname, O_RDONLY);
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010074
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010075 rtc_read_tm(&tm_time, fd);
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010076
77#if SHOW_HWCLOCK_DIFF
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010078 {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010079 int before = tm_time.tm_sec;
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010080 while (1) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010081 rtc_read_tm(&tm_time, fd);
Denys Vlasenko3c13da32020-12-30 23:48:01 +010082 xgettimeofday(sys_tv);
Denys Vlasenkod778e6c2012-04-17 19:25:13 +020083 if (before != (int)tm_time.tm_sec)
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010084 break;
85 }
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010086 }
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010087#endif
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010088
Denys Vlasenko695fa512010-01-06 18:16:39 +010089 if (ENABLE_FEATURE_CLEAN_UP)
90 close(fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000091
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010092 return rtc_tm2time(&tm_time, utc);
Robert Griebl1cd04452002-07-21 16:50:49 +000093}
94
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010095static void show_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000096{
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010097#if SHOW_HWCLOCK_DIFF
98 struct timeval sys_tv;
99#endif
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200100 time_t t = read_rtc(pp_rtcname, &sys_tv, utc);
Robert Griebl1cd04452002-07-21 16:50:49 +0000101
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200102#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 Yorston8ec1ff32015-03-12 20:18:51 +0100109 chomp(cp);
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200110#endif
111
Denys Vlasenko0c58cc72010-01-07 10:36:41 +0100112#if !SHOW_HWCLOCK_DIFF
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100113 printf("%s 0.000000 seconds\n", cp);
Denys Vlasenko0c58cc72010-01-07 10:36:41 +0100114#else
115 {
116 long diff = sys_tv.tv_sec - t;
117 if (diff < 0 /*&& tv.tv_usec != 0*/) {
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200118 /* 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 Vlasenko0c58cc72010-01-07 10:36:41 +0100123 diff++;
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200124 /* Should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
Denys Vlasenko0c58cc72010-01-07 10:36:41 +0100125 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 Vlasenko5e3b1402010-01-06 22:43:39 +0100128 }
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100129#endif
Robert Griebl1cd04452002-07-21 16:50:49 +0000130}
131
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100132static 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 Vlasenkod3c36882020-12-16 20:55:30 +0100156 * (Unlike --hctosys, it does not read the RTC).
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100157 *
158 * util-linux's code has this comment:
159 * RTC | settimeofday calls
160 * ------|-------------------------------------------------
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100161 * Local | 1st) warps system time*, sets PCIL* and kernel tz
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100162 * 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 */
167static 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 Vlasenkod3c36882020-12-16 20:55:30 +0100178//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 Vlasenko9e262f12020-12-16 13:49:10 +0100181 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 Vlasenkoeb0c2e22020-12-16 21:36:36 +0100187 if (hctosys) /* it's --hctosys: set time too */
188 xsettimeofday(hctosys);
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100189}
190
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100191static void to_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +0000192{
Denis Vlasenko459be352007-06-17 19:09:05 +0000193 struct timeval tv;
Eddie James1a5d6fc2020-08-10 09:59:02 -0500194
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100195 tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
Denis Vlasenko459be352007-06-17 19:09:05 +0000196 tv.tv_usec = 0;
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100197 return set_kernel_timezone_and_clock(utc, &tv);
Robert Griebl1cd04452002-07-21 16:50:49 +0000198}
199
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100200static void from_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +0000201{
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700202#if 1
203 struct timeval tv;
204 struct tm tm_time;
205 int rtc;
206
207 rtc = rtc_xopen(pp_rtcname, O_WRONLY);
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100208 xgettimeofday(&tv);
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700209 /* 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 Vlasenko10ad6222017-04-17 16:13:32 +0200227 * making all this a pointless exercise.
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700228 */
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 Vlasenkodc698bb2010-01-09 19:10:49 +0100247 struct tm tm_time;
Denis Vlasenko459be352007-06-17 19:09:05 +0000248 struct timeval tv;
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100249 int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
Robert Griebl1cd04452002-07-21 16:50:49 +0000250
Denys Vlasenko695fa512010-01-06 18:16:39 +0100251 /* Try to catch the moment when whole second is close */
252 while (1) {
253 unsigned rem_usec;
254 time_t t;
255
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100256 xgettimeofday(&tv);
Denys Vlasenko695fa512010-01-06 18:16:39 +0100257
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100258 t = tv.tv_sec;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100259 rem_usec = 1000000 - tv.tv_usec;
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700260 if (rem_usec < adj) {
261 /* Close enough */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100262 small_rem:
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100263 t++;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100264 }
265
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700266 /* Prepare tm_time from t */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100267 if (utc)
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100268 gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100269 else
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100270 localtime_r(&t, &tm_time); /* same */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700271
272 if (adj >= 32*1024) {
273 break; /* 32 ms diff and still no luck?? give up trying to sync */
274 }
Denys Vlasenko695fa512010-01-06 18:16:39 +0100275
276 /* gmtime/localtime took some time, re-get cur time */
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100277 xgettimeofday(&tv);
Denys Vlasenko695fa512010-01-06 18:16:39 +0100278
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700279 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 Vlasenko695fa512010-01-06 18:16:39 +0100281 ) {
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700282 break; /* good, we are in sync! */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100283 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700284
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 Vlasenko695fa512010-01-06 18:16:39 +0100289 }
290
291 /* Try to sync up by sleeping */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100292 usleep(rem_usec - adj);
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700293
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 Vlasenko695fa512010-01-06 18:16:39 +0100308 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700309 /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
Denys Vlasenko695fa512010-01-06 18:16:39 +0100310 * Look for a value which makes tv_usec close to 999999 or 0.
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700311 * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
Denys Vlasenko695fa512010-01-06 18:16:39 +0100312 */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700313 //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 Vlasenko695fa512010-01-06 18:16:39 +0100318
319 if (ENABLE_FEATURE_CLEAN_UP)
320 close(rtc);
Robert Griebl1cd04452002-07-21 16:50:49 +0000321}
322
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100323// 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 Cavalca658a4372011-01-22 18:55:32 +0100347//usage:#define hwclock_trivial_usage
Denys Vlasenko036585a2017-08-08 16:38:18 +0200348//usage: IF_LONG_OPTS(
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100349//usage: "[-swul] [--systz] [-f DEV]"
Davide Cavalca658a4372011-01-22 18:55:32 +0100350//usage: )
Denys Vlasenko036585a2017-08-08 16:38:18 +0200351//usage: IF_NOT_LONG_OPTS(
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100352//usage: "[-swult] [-f DEV]"
Davide Cavalca658a4372011-01-22 18:55:32 +0100353//usage: )
354//usage:#define hwclock_full_usage "\n\n"
Denys Vlasenkocaba1a12020-12-16 01:19:08 +0100355//usage: "Show or set hardware clock (RTC)\n"
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100356///////: "\n -r Show RTC time"
Denys Vlasenkocaba1a12020-12-16 01:19:08 +0100357///////-r is default, don't bother showing it in help
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100358//usage: "\n -s Set system time from RTC"
359//usage: "\n -w Set RTC from system time"
Denys Vlasenko036585a2017-08-08 16:38:18 +0200360//usage: IF_LONG_OPTS(
361//usage: "\n --systz Set in-kernel timezone, correct system time"
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100362//usage: "\n if RTC is kept in local time"
Denys Vlasenko036585a2017-08-08 16:38:18 +0200363//usage: )
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100364//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 Cavalca658a4372011-01-22 18:55:32 +0100368
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100369//TODO: get rid of incompatible -t alias to --systz?
Denys Vlasenko036585a2017-08-08 16:38:18 +0200370
Denis Vlasenko92258542006-11-01 10:25:35 +0000371#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 Cavalca658a4372011-01-22 18:55:32 +0100376#define HWCLOCK_OPT_SYSTZ 0x20
377#define HWCLOCK_OPT_RTCFILE 0x40
Glenn L McGrath689e4b92004-02-22 09:11:33 +0000378
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000379int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000380int hwclock_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1cd04452002-07-21 16:50:49 +0000381{
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100382 const char *rtcname = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000383 unsigned opt;
Robert Griebl6bb80872004-03-22 21:27:39 +0000384 int utc;
Denys Vlasenko036585a2017-08-08 16:38:18 +0200385#if ENABLE_LONG_OPTS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000386 static const char hwclock_longopts[] ALIGN1 =
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100387 "localtime\0" No_argument "l"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000388 "utc\0" No_argument "u"
389 "show\0" No_argument "r"
390 "hctosys\0" No_argument "s"
391 "systohc\0" No_argument "w"
Davide Cavalca658a4372011-01-22 18:55:32 +0100392 "systz\0" No_argument "t" /* short opt is non-standard */
393 "rtc\0" Required_argument "f"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000394 ;
Robert Griebl1cd04452002-07-21 16:50:49 +0000395#endif
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200396 opt = getopt32long(argv,
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100397 "^""lurswtf:v" /* -v is accepted and ignored */
398 "\0"
399 "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l",
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200400 hwclock_longopts,
401 &rtcname
402 );
Robert Griebl1cd04452002-07-21 16:50:49 +0000403
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100404 /* If -u or -l wasn't given, check if we are using utc */
Mike Frysingerb31566e2005-04-16 04:48:48 +0000405 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
Mike Frysinger6b160e42008-02-15 02:27:19 +0000406 utc = (opt & HWCLOCK_OPT_UTC);
Robert Griebl6bb80872004-03-22 21:27:39 +0000407 else
Mike Frysinger6b160e42008-02-15 02:27:19 +0000408 utc = rtc_adjtime_is_utc();
Mike Frysingerb31566e2005-04-16 04:48:48 +0000409
Mike Frysinger6b160e42008-02-15 02:27:19 +0000410 if (opt & HWCLOCK_OPT_HCTOSYS)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100411 to_sys_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000412 else if (opt & HWCLOCK_OPT_SYSTOHC)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100413 from_sys_clock(&rtcname, utc);
Davide Cavalca658a4372011-01-22 18:55:32 +0100414 else if (opt & HWCLOCK_OPT_SYSTZ)
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100415 set_kernel_timezone_and_clock(utc, NULL);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000416 else
417 /* default HWCLOCK_OPT_SHOW */
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100418 show_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000419
Denis Vlasenko459be352007-06-17 19:09:05 +0000420 return 0;
Robert Griebl1cd04452002-07-21 16:50:49 +0000421}