blob: 2edadfa4d8fad3b582d07636619652ad24247130 [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
Samuel Thibault77216c32022-10-16 02:04:59 +020012//config: select PLATFORM_LINUX
Denys Vlasenkodd898c92016-11-23 11:46:32 +010013//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: The hwclock utility is used to read and set the hardware clock
15//config: on a system. This is primarily used to set the current time on
16//config: shutdown in the hardware clock, so the hardware will keep the
17//config: correct time when Linux is _not_ running.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010018//config:
Denys Vlasenkodd898c92016-11-23 11:46:32 +010019//config:config FEATURE_HWCLOCK_ADJTIME_FHS
20//config: bool "Use FHS /var/lib/hwclock/adjtime"
21//config: default n # util-linux-ng in Fedora 13 still uses /etc/adjtime
22//config: depends on HWCLOCK
23//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020024//config: Starting with FHS 2.3, the adjtime state file is supposed to exist
25//config: at /var/lib/hwclock/adjtime instead of /etc/adjtime. If you wish
26//config: to use the FHS behavior, answer Y here, otherwise answer N for the
27//config: classic /etc/adjtime path.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010028//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020029//config: pathname.com/fhs/pub/fhs-2.3.html#VARLIBHWCLOCKSTATEDIRECTORYFORHWCLO
Denys Vlasenkodd898c92016-11-23 11:46:32 +010030
31//applet:IF_HWCLOCK(APPLET(hwclock, BB_DIR_SBIN, BB_SUID_DROP))
32
33//kbuild:lib-$(CONFIG_HWCLOCK) += hwclock.o
Robert Griebl1cd04452002-07-21 16:50:49 +000034
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020036/* After libbb.h, since it needs sys/types.h on some systems */
37#include <sys/utsname.h>
Mike Frysinger6b160e42008-02-15 02:27:19 +000038#include "rtc_.h"
Eric Andersen8882ea52002-12-11 03:41:28 +000039
Denys Vlasenko9e262f12020-12-16 13:49:10 +010040
41//musl has no __MUSL__ or similar define to check for,
42//but its <sys/types.h> has these lines:
43// #define __NEED_fsblkcnt_t
44// #define __NEED_fsfilcnt_t
45#if defined(__linux__) && defined(__NEED_fsblkcnt_t) && defined(__NEED_fsfilcnt_t)
46# define LIBC_IS_MUSL 1
47# include <sys/syscall.h>
48#else
49# define LIBC_IS_MUSL 0
50#endif
51
52
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010053/* diff code is disabled: it's not sys/hw clock diff, it's some useless
54 * "time between hwclock was started and we saw CMOS tick" quantity.
55 * It's useless since hwclock is started at a random moment,
56 * thus the quantity is also random, useless. Showing 0.000000 does not
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010057 * deprive us from any useful info.
58 *
59 * SHOW_HWCLOCK_DIFF code in this file shows the difference between system
60 * and hw clock. It is useful, but not compatible with standard hwclock.
61 * Thus disabled.
62 */
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010063#define SHOW_HWCLOCK_DIFF 0
64
65
66#if !SHOW_HWCLOCK_DIFF
67# define read_rtc(pp_rtcname, sys_tv, utc) read_rtc(pp_rtcname, utc)
68#endif
69static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc)
Denis Vlasenko92258542006-11-01 10:25:35 +000070{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010071 struct tm tm_time;
Mike Frysinger6b160e42008-02-15 02:27:19 +000072 int fd;
Robert Griebl1cd04452002-07-21 16:50:49 +000073
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010074 fd = rtc_xopen(pp_rtcname, O_RDONLY);
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010075
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010076 rtc_read_tm(&tm_time, fd);
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010077
78#if SHOW_HWCLOCK_DIFF
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010079 {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010080 int before = tm_time.tm_sec;
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010081 while (1) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010082 rtc_read_tm(&tm_time, fd);
Denys Vlasenko3c13da32020-12-30 23:48:01 +010083 xgettimeofday(sys_tv);
Denys Vlasenkod778e6c2012-04-17 19:25:13 +020084 if (before != (int)tm_time.tm_sec)
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010085 break;
86 }
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010087 }
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010088#endif
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010089
Denys Vlasenko695fa512010-01-06 18:16:39 +010090 if (ENABLE_FEATURE_CLEAN_UP)
91 close(fd);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000092
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010093 return rtc_tm2time(&tm_time, utc);
Robert Griebl1cd04452002-07-21 16:50:49 +000094}
95
Denys Vlasenko6959f6b2010-01-07 08:31:46 +010096static void show_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +000097{
Denys Vlasenko0c58cc72010-01-07 10:36:41 +010098#if SHOW_HWCLOCK_DIFF
99 struct timeval sys_tv;
100#endif
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200101 time_t t = read_rtc(pp_rtcname, &sys_tv, utc);
Robert Griebl1cd04452002-07-21 16:50:49 +0000102
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200103#if ENABLE_LOCALE_SUPPORT
104 /* Standard hwclock uses locale-specific output format */
105 char cp[64];
106 struct tm *ptm = localtime(&t);
107 strftime(cp, sizeof(cp), "%c", ptm);
108#else
109 char *cp = ctime(&t);
Ron Yorston8ec1ff32015-03-12 20:18:51 +0100110 chomp(cp);
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200111#endif
112
Denys Vlasenko0c58cc72010-01-07 10:36:41 +0100113#if !SHOW_HWCLOCK_DIFF
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100114 printf("%s 0.000000 seconds\n", cp);
Denys Vlasenko0c58cc72010-01-07 10:36:41 +0100115#else
116 {
117 long diff = sys_tv.tv_sec - t;
118 if (diff < 0 /*&& tv.tv_usec != 0*/) {
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200119 /* Why we need diff++? */
120 /* diff >= 0 is ok: | diff < 0, can't just use tv.tv_usec: */
121 /* 45.520820 | 43.520820 */
122 /* - 44.000000 | - 45.000000 */
123 /* = 1.520820 | = -1.479180, not -2.520820! */
Denys Vlasenko0c58cc72010-01-07 10:36:41 +0100124 diff++;
Denys Vlasenko319b8bb2011-07-08 06:40:25 +0200125 /* Should be 1000000 - tv.tv_usec, but then we must check tv.tv_usec != 0 */
Denys Vlasenko0c58cc72010-01-07 10:36:41 +0100126 sys_tv.tv_usec = 999999 - sys_tv.tv_usec;
127 }
128 printf("%s %ld.%06lu seconds\n", cp, diff, (unsigned long)sys_tv.tv_usec);
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100129 }
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100130#endif
Robert Griebl1cd04452002-07-21 16:50:49 +0000131}
132
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100133static void set_kernel_tz(const struct timezone *tz)
134{
135#if LIBC_IS_MUSL
136 /* musl libc does not pass tz argument to syscall
137 * because "it's deprecated by POSIX, therefore it's fine
138 * if we gratuitously break stuff" :(
139 */
140#if !defined(SYS_settimeofday) && defined(SYS_settimeofday_time32)
141# define SYS_settimeofday SYS_settimeofday_time32
142#endif
143 int ret = syscall(SYS_settimeofday, NULL, tz);
144#else
145 int ret = settimeofday(NULL, tz);
146#endif
147 if (ret)
148 bb_simple_perror_msg_and_die("settimeofday");
149}
150
151/*
152 * At system boot, kernel may set system time from RTC,
153 * but it knows nothing about timezones. If RTC is in local time,
154 * then system time is wrong - it is offset by timezone.
155 * --systz option corrects system time if RTC is in local time,
156 * and (always) sets in-kernel timezone.
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100157 * (Unlike --hctosys, it does not read the RTC).
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100158 *
159 * util-linux's code has this comment:
160 * RTC | settimeofday calls
161 * ------|-------------------------------------------------
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100162 * Local | 1st) warps system time*, sets PCIL* and kernel tz
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100163 * UTC | 1st) locks warp_clock 2nd) sets kernel tz
164 * * only on first call after boot
165 * (PCIL is "persistent_clock_is_local" kernel internal flag,
166 * it makes kernel save RTC in local time, not UTC.)
167 */
168static void set_kernel_timezone_and_clock(int utc, const struct timeval *hctosys)
169{
170 time_t cur;
171 struct tm *broken;
172 struct timezone tz = { 0 };
173
174 /* if --utc, prevent kernel's warp_clock() with a dummy call */
175 if (utc)
176 set_kernel_tz(&tz);
177
178 /* Set kernel's timezone offset based on userspace one */
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100179//It's tempting to call tzset() and use libc global "timezone" variable
180//...but it does NOT include DST shift (IOW: it's WRONG, usually by one hour,
181//if DST is in effect!) Thus this ridiculous dance:
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100182 cur = time(NULL);
183 broken = localtime(&cur);
184 tz.tz_minuteswest = -broken->tm_gmtoff / 60;
185 /*tz.tz_dsttime = 0; already is */
186 set_kernel_tz(&tz); /* MIGHT warp_clock() if 1st call since boot */
187
Denys Vlasenkoeb0c2e22020-12-16 21:36:36 +0100188 if (hctosys) /* it's --hctosys: set time too */
189 xsettimeofday(hctosys);
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100190}
191
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100192static void to_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +0000193{
Denis Vlasenko459be352007-06-17 19:09:05 +0000194 struct timeval tv;
Eddie James1a5d6fc2020-08-10 09:59:02 -0500195
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100196 tv.tv_sec = read_rtc(pp_rtcname, NULL, utc);
Denis Vlasenko459be352007-06-17 19:09:05 +0000197 tv.tv_usec = 0;
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100198 return set_kernel_timezone_and_clock(utc, &tv);
Robert Griebl1cd04452002-07-21 16:50:49 +0000199}
200
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100201static void from_sys_clock(const char **pp_rtcname, int utc)
Robert Griebl1cd04452002-07-21 16:50:49 +0000202{
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700203#if 1
204 struct timeval tv;
205 struct tm tm_time;
206 int rtc;
207
208 rtc = rtc_xopen(pp_rtcname, O_WRONLY);
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100209 xgettimeofday(&tv);
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700210 /* Prepare tm_time */
211 if (sizeof(time_t) == sizeof(tv.tv_sec)) {
212 if (utc)
213 gmtime_r((time_t*)&tv.tv_sec, &tm_time);
214 else
215 localtime_r((time_t*)&tv.tv_sec, &tm_time);
216 } else {
217 time_t t = tv.tv_sec;
218 if (utc)
219 gmtime_r(&t, &tm_time);
220 else
221 localtime_r(&t, &tm_time);
222 }
223#else
224/* Bloated code which tries to set hw clock with better precision.
225 * On x86, even though code does set hw clock within <1ms of exact
226 * whole seconds, apparently hw clock (at least on some machines)
227 * doesn't reset internal fractional seconds to 0,
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200228 * making all this a pointless exercise.
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700229 */
230 /* If we see that we are N usec away from whole second,
231 * we'll sleep for N-ADJ usecs. ADJ corrects for the fact
232 * that CPU is not infinitely fast.
233 * On infinitely fast CPU, next wakeup would be
234 * on (exactly_next_whole_second - ADJ). On real CPUs,
235 * this difference between current time and whole second
236 * is less than ADJ (assuming system isn't heavily loaded).
237 */
238 /* Small value of 256us gives very precise sync for 2+ GHz CPUs.
239 * Slower CPUs will fail to sync and will go to bigger
240 * ADJ values. qemu-emulated armv4tl with ~100 MHz
241 * performance ends up using ADJ ~= 4*1024 and it takes
242 * 2+ secs (2 tries with successively larger ADJ)
243 * to sync. Even straced one on the same qemu (very slow)
244 * takes only 4 tries.
245 */
246#define TWEAK_USEC 256
247 unsigned adj = TWEAK_USEC;
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100248 struct tm tm_time;
Denis Vlasenko459be352007-06-17 19:09:05 +0000249 struct timeval tv;
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100250 int rtc = rtc_xopen(pp_rtcname, O_WRONLY);
Robert Griebl1cd04452002-07-21 16:50:49 +0000251
Denys Vlasenko695fa512010-01-06 18:16:39 +0100252 /* Try to catch the moment when whole second is close */
253 while (1) {
254 unsigned rem_usec;
255 time_t t;
256
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100257 xgettimeofday(&tv);
Denys Vlasenko695fa512010-01-06 18:16:39 +0100258
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100259 t = tv.tv_sec;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100260 rem_usec = 1000000 - tv.tv_usec;
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700261 if (rem_usec < adj) {
262 /* Close enough */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100263 small_rem:
Denys Vlasenko5e3b1402010-01-06 22:43:39 +0100264 t++;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100265 }
266
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700267 /* Prepare tm_time from t */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100268 if (utc)
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100269 gmtime_r(&t, &tm_time); /* may read /etc/xxx (it takes time) */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100270 else
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100271 localtime_r(&t, &tm_time); /* same */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700272
273 if (adj >= 32*1024) {
274 break; /* 32 ms diff and still no luck?? give up trying to sync */
275 }
Denys Vlasenko695fa512010-01-06 18:16:39 +0100276
277 /* gmtime/localtime took some time, re-get cur time */
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100278 xgettimeofday(&tv);
Denys Vlasenko695fa512010-01-06 18:16:39 +0100279
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700280 if (tv.tv_sec < t /* we are still in old second */
281 || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100282 ) {
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700283 break; /* good, we are in sync! */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100284 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700285
286 rem_usec = 1000000 - tv.tv_usec;
287 if (rem_usec < adj) {
288 t = tv.tv_sec;
289 goto small_rem; /* already close to next sec, don't sleep */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100290 }
291
292 /* Try to sync up by sleeping */
Denys Vlasenko695fa512010-01-06 18:16:39 +0100293 usleep(rem_usec - adj);
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700294
295 /* Jump to 1ms diff, then increase fast (x2): EVERY loop
296 * takes ~1 sec, people won't like slowly converging code here!
297 */
298 //bb_error_msg("adj:%d tv.tv_usec:%d", adj, (int)tv.tv_usec);
299 if (adj < 512)
300 adj = 512;
301 /* ... and if last "overshoot" does not look insanely big,
302 * just use it as adj increment. This makes convergence faster.
303 */
304 if (tv.tv_usec < adj * 8) {
305 adj += tv.tv_usec;
306 continue;
307 }
308 adj *= 2;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100309 }
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700310 /* Debug aid to find "optimal" TWEAK_USEC with nearly exact sync.
Denys Vlasenko695fa512010-01-06 18:16:39 +0100311 * Look for a value which makes tv_usec close to 999999 or 0.
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700312 * For 2.20GHz Intel Core 2: optimal TWEAK_USEC ~= 200
Denys Vlasenko695fa512010-01-06 18:16:39 +0100313 */
Denys Vlasenko60f659f2010-04-14 09:19:20 -0700314 //bb_error_msg("tv.tv_usec:%d", (int)tv.tv_usec);
315#endif
316
317 tm_time.tm_isdst = 0;
318 xioctl(rtc, RTC_SET_TIME, &tm_time);
Denys Vlasenko695fa512010-01-06 18:16:39 +0100319
320 if (ENABLE_FEATURE_CLEAN_UP)
321 close(rtc);
Robert Griebl1cd04452002-07-21 16:50:49 +0000322}
323
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100324// hwclock from util-linux 2.36.1
325// hwclock [function] [option...]
326//Functions:
327// -r, --show display the RTC time
328// --get display drift corrected RTC time
329// --set set the RTC according to --date
330// -s, --hctosys set the system time from the RTC
331// -w, --systohc set the RTC from the system time
332// --systz send timescale configurations to the kernel
333// -a, --adjust adjust the RTC to account for systematic drift
334// --predict predict the drifted RTC time according to --date
335//Options:
336// -u, --utc the RTC timescale is UTC
337// -l, --localtime the RTC timescale is Local
338// -f, --rtc <file> use an alternate file to /dev/rtc0
339// --directisa use the ISA bus instead of /dev/rtc0 access
340// --date <time> date/time input for --set and --predict
341// --delay <sec> delay used when set new RTC time
342// --update-drift update the RTC drift factor
343// --noadjfile do not use /etc/adjtime
344// --adjfile <file> use an alternate file to /etc/adjtime
345// --test dry run; implies --verbose
346// -v, --verbose display more details
347
Davide Cavalca658a4372011-01-22 18:55:32 +0100348//usage:#define hwclock_trivial_usage
Denys Vlasenko036585a2017-08-08 16:38:18 +0200349//usage: IF_LONG_OPTS(
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100350//usage: "[-swul] [--systz] [-f DEV]"
Davide Cavalca658a4372011-01-22 18:55:32 +0100351//usage: )
Denys Vlasenko036585a2017-08-08 16:38:18 +0200352//usage: IF_NOT_LONG_OPTS(
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100353//usage: "[-swult] [-f DEV]"
Davide Cavalca658a4372011-01-22 18:55:32 +0100354//usage: )
355//usage:#define hwclock_full_usage "\n\n"
Denys Vlasenkocaba1a12020-12-16 01:19:08 +0100356//usage: "Show or set hardware clock (RTC)\n"
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100357///////: "\n -r Show RTC time"
Denys Vlasenkocaba1a12020-12-16 01:19:08 +0100358///////-r is default, don't bother showing it in help
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100359//usage: "\n -s Set system time from RTC"
360//usage: "\n -w Set RTC from system time"
Denys Vlasenko036585a2017-08-08 16:38:18 +0200361//usage: IF_LONG_OPTS(
362//usage: "\n --systz Set in-kernel timezone, correct system time"
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100363//usage: "\n if RTC is kept in local time"
Denys Vlasenko036585a2017-08-08 16:38:18 +0200364//usage: )
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100365//usage: "\n -f DEV Use specified device (e.g. /dev/rtc2)"
366//usage: "\n -u Assume RTC is kept in UTC"
367//usage: "\n -l Assume RTC is kept in local time"
368//usage: "\n (if neither is given, read from "ADJTIME_PATH")"
Davide Cavalca658a4372011-01-22 18:55:32 +0100369
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100370//TODO: get rid of incompatible -t alias to --systz?
Denys Vlasenko036585a2017-08-08 16:38:18 +0200371
Denis Vlasenko92258542006-11-01 10:25:35 +0000372#define HWCLOCK_OPT_LOCALTIME 0x01
373#define HWCLOCK_OPT_UTC 0x02
374#define HWCLOCK_OPT_SHOW 0x04
375#define HWCLOCK_OPT_HCTOSYS 0x08
376#define HWCLOCK_OPT_SYSTOHC 0x10
Davide Cavalca658a4372011-01-22 18:55:32 +0100377#define HWCLOCK_OPT_SYSTZ 0x20
378#define HWCLOCK_OPT_RTCFILE 0x40
Glenn L McGrath689e4b92004-02-22 09:11:33 +0000379
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000380int hwclock_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000381int hwclock_main(int argc UNUSED_PARAM, char **argv)
Robert Griebl1cd04452002-07-21 16:50:49 +0000382{
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100383 const char *rtcname = NULL;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000384 unsigned opt;
Robert Griebl6bb80872004-03-22 21:27:39 +0000385 int utc;
Denys Vlasenko036585a2017-08-08 16:38:18 +0200386#if ENABLE_LONG_OPTS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000387 static const char hwclock_longopts[] ALIGN1 =
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100388 "localtime\0" No_argument "l"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000389 "utc\0" No_argument "u"
390 "show\0" No_argument "r"
391 "hctosys\0" No_argument "s"
392 "systohc\0" No_argument "w"
Davide Cavalca658a4372011-01-22 18:55:32 +0100393 "systz\0" No_argument "t" /* short opt is non-standard */
394 "rtc\0" Required_argument "f"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000395 ;
Robert Griebl1cd04452002-07-21 16:50:49 +0000396#endif
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200397 opt = getopt32long(argv,
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100398 "^""lurswtf:v" /* -v is accepted and ignored */
399 "\0"
400 "r--wst:w--rst:s--wrt:t--rsw:l--u:u--l",
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200401 hwclock_longopts,
402 &rtcname
403 );
Robert Griebl1cd04452002-07-21 16:50:49 +0000404
Denys Vlasenkod3c36882020-12-16 20:55:30 +0100405 /* If -u or -l wasn't given, check if we are using utc */
Mike Frysingerb31566e2005-04-16 04:48:48 +0000406 if (opt & (HWCLOCK_OPT_UTC | HWCLOCK_OPT_LOCALTIME))
Mike Frysinger6b160e42008-02-15 02:27:19 +0000407 utc = (opt & HWCLOCK_OPT_UTC);
Robert Griebl6bb80872004-03-22 21:27:39 +0000408 else
Mike Frysinger6b160e42008-02-15 02:27:19 +0000409 utc = rtc_adjtime_is_utc();
Mike Frysingerb31566e2005-04-16 04:48:48 +0000410
Mike Frysinger6b160e42008-02-15 02:27:19 +0000411 if (opt & HWCLOCK_OPT_HCTOSYS)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100412 to_sys_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000413 else if (opt & HWCLOCK_OPT_SYSTOHC)
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100414 from_sys_clock(&rtcname, utc);
Davide Cavalca658a4372011-01-22 18:55:32 +0100415 else if (opt & HWCLOCK_OPT_SYSTZ)
Denys Vlasenko9e262f12020-12-16 13:49:10 +0100416 set_kernel_timezone_and_clock(utc, NULL);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000417 else
418 /* default HWCLOCK_OPT_SHOW */
Denys Vlasenko6959f6b2010-01-07 08:31:46 +0100419 show_clock(&rtcname, utc);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000420
Denis Vlasenko459be352007-06-17 19:09:05 +0000421 return 0;
Robert Griebl1cd04452002-07-21 16:50:49 +0000422}