Commit graph

187 commits

Author SHA1 Message Date
Bastian Krause
6d8fc1cf32 hwclock: fix return value on successful --param-get
hwclock should return 0 on sucessful --param-get.

Fixes: 6097b12df ("hwclock: add --param-get option")
Signed-off-by: Bastian Krause <bst@pengutronix.de>
2022-12-21 11:50:05 +01:00
Samuel Thibault
0b7aacda9d misc: non-Linux portability fixes
Signed-off-by: Karel Zak <kzak@redhat.com>
2022-02-01 15:12:47 +01:00
Karel Zak
511a512611 hwclock: get/set param cleanup
* move all code to hwclock-rtc.c
* use ul_strtou64() to simplify code
* use base=0 for ul_strtou64() to handle 10 and 16 base
* allocate for strtok() (we keep command line options read-only)

Signed-off-by: Karel Zak <kzak@redhat.com>
2022-01-31 10:05:29 +01:00
Karel Zak
63d8183479 hwclock: cleanup hwclock_params[] use
* remove it from header file
* keep it 'static const'

Signed-off-by: Karel Zak <kzak@redhat.com>
2022-01-28 12:15:39 +01:00
Bastian Krause
b22b78b1be hwclock: add --param-set option
Implement the RTC_PARAM_SET RTC ioctl in hwclock. The ioctl interface was
introduced with [1], which went mainline in Kernel v5.16. The parameters
are independent of hardware/driver. This means we can read and set
parameters in a generic way.

The new --param-set hwclock function accepts aliases for parameters
currently existent (Kernel v5.16). They can be extended later on. As
fallback and for values, hexadecimal (if prefixed with 0x) and decimal
values, as defined in [2], are accepted.

Example:

  $ hwclock --param-set bsm=0x0

[1] https://lore.kernel.org/all/20211018151933.76865-1-alexandre.belloni@bootlin.com/
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/rtc.h

Signed-off-by: Bastian Krause <bst@pengutronix.de>
2022-01-28 11:37:47 +01:00
Bastian Krause
6097b12df7 hwclock: add --param-get option
Implement the RTC_PARAM_GET RTC ioctl in hwclock. The ioctl interface was
introduced with [1], which went mainline in Kernel v5.16. The parameters
are independent of hardware/driver. This means we can read and set
parameters in a generic way.

The new --param-get hwclock function accepts aliases for parameters
currently existent (Kernel v5.16). They can be extended later on. As
fallback, hexadecimal (if prefixed with 0x) and decimal values, as
defined in [2], are accepted.

Example:

  $ hwclock --param-get features

[1] https://lore.kernel.org/all/20211018151933.76865-1-alexandre.belloni@bootlin.com/
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/rtc.h

Signed-off-by: Bastian Krause <bst@pengutronix.de>
2022-01-28 11:37:47 +01:00
Bastian Krause
ee1e12448e hwclock: increase indent in help text
This leaves more space for a longer option in a future commit.

Signed-off-by: Bastian Krause <bst@pengutronix.de>
2022-01-28 11:37:47 +01:00
Karel Zak
c88fc0c3d0 hwclock: don't ignore sscanf() return code [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-11-29 15:30:10 +01:00
David Engraf
319563bf31 Do not include sys/syscall.h if not available
Some platforms do not provide sys/syscall.h. The configure script already
checks for the existance of the file. Include sys/syscall.h only in case
HAVE_SYS_SYSCALL_H has been set.
2021-11-18 14:08:35 +01:00
Karel Zak
365e006a36 hwclock: fix ul_path_scanf() use
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-18 16:50:58 +02:00
Karel Zak
3fdb178370 hwclock: close adjtime on write error [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-11 15:17:42 +02:00
Armin Begovic
ce3355cc54 Fixed wrongful time_t=long assumptions in hwclock.c and timeutils.c 2021-04-29 11:56:37 +00:00
Armin Begovic
56702ef2ea Changed int64_t casts to long long int casts 2021-04-23 23:28:37 +02:00
Armin Begovic
a937ef8318 Fixed format strings and type casts in hwclock to work with 64-bit time_t on 32-bit linux 2021-04-23 22:48:13 +02:00
Rafael Fontenelle
993556aaa5 Fix misspellings 2021-04-22 20:31:05 -03:00
Karel Zak
81329c8d1c hwclock: use pointer to adjtime data
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-03-04 11:27:47 +01:00
Pino Toscano
367972fae1 hwclock: do not assume __NR_settimeofday_time32
Check that __NR_settimeofday_time32 exists before trying to use it as
syscall number.

Signed-off-by: Pino Toscano <toscano.pino@tiscali.it>
2020-11-17 11:32:45 +01:00
Karel Zak
a3a0cc42af Merge branch 'master' of https://github.com/itsthem/util-linux
* 'master' of https://github.com/itsthem/util-linux:
  her -> their
  her -> their
  him -> them
  him -> them
  his -> their
  he -> they
  he -> they
  he -> they
2020-09-01 09:45:32 +02:00
Rosen Penev
fdac67ae14
hwclock: fix SYS_settimeofday fallback
turns out this is subtly broken. musl 1.2.x for 64-bit architectures defines __NR_settimeofday but not
for 32-bit ones. For 32-bit, it defines a _time32 variant.
2020-08-29 21:55:58 -07:00
leeceeksdee
d15a6c22cf
he -> they 2020-08-28 19:12:44 +02:00
Karel Zak
69e4fbfbfd hwclock: add fallback if SYS_settimeofday does not exist
It seems Musl-C removes SYS_settimeofday macro at all.

Addresses: 9c6139a720 (commitcomment-41290951)
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-08-11 11:02:31 +02:00
Karel Zak
9c6139a720 hwclock: improve use of settimeofday() portability
The different libc implements TZ deprecation in settimeofday() library
function in the different way. Let's hide these portability issues and
use directly Linux syscall to set timezone.

Addresses: https://github.com/karelzak/util-linux/issues/995
Signed-off-by: Karel Zak <kzak@redhat.com>
CC: J William Piggott <elseifthen@gmx.com>
2020-05-22 11:51:53 +02:00
Rosen Penev
ed23cbce64
[clang-tidy] use ceil
Found with bugprone-incorrect-roundings

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-20 13:21:00 -07:00
Rosen Penev
042f62dfc5
[clang-tidy] do not use else after return
Found with readability-else-after-return

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-20 13:20:59 -07:00
Karel Zak
189edf1fe5 hwclock: fix audit exit status
According to audit_log_user_message(3) result 1 is "success" and 0 is
"failed", we use standard EXIT_{SUCCESS,FAILURE} macros with reverse
status.

Addresses: https://github.com/karelzak/util-linux/issues/966
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-02-27 14:04:55 +01:00
J William Piggott
cd781c405b hwclock: make glibc 2.31 compatible
______________________________________________________
GNU C Library NEWS -- history of user-visible changes.
Version 2.31
Deprecated and removed features, and other changes affecting compatibility:

* The settimeofday function can still be used to set a system-wide time
  zone when the operating system supports it.  This is because the Linux
  kernel reused the API, on some architectures, to describe a system-wide
  time-zone-like offset between the software clock maintained by the kernel,
  and the "RTC" clock that keeps time when the system is shut down.

  However, to reduce the odds of this offset being set by accident,
  settimeofday can no longer be used to set the time and the offset
  simultaneously.  If both of its two arguments are non-null, the call
  will fail (setting errno to EINVAL).

  Callers attempting to set this offset should also be prepared for the call
  to fail and set errno to ENOSYS; this already happens on the Hurd and on
  some Linux architectures.  The Linux kernel maintainers are discussing a
  more principled replacement for the reused API.  After a replacement
  becomes available, we will change settimeofday to fail with ENOSYS on all
  platforms when its 'tzp' argument is not a null pointer.

  settimeofday itself is obsolescent according to POSIX.  Programs that set
  the system time should use clock_settime and/or the adjtime family of
  functions instead.  We may cease to make settimeofday available to newly
  linked binaries after there is a replacement for Linux's time-zone-like
  offset API.
______________________________________________________

hwclock(8) had one settimeofday(2) call where both args were set for
--hctosys when the RTC was ticking UTC. This allowed setting the system
time, timezone, and locking the warp_clock function with a single call.
That operation now takes 3 calls of settimeofday(2).

Although this common operation now takes three calls, the overall logic
for the set_system_clock() function was simplified.

Co-Author: Karel Zak <kzak@redhat.com>
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2020-02-24 16:45:32 +01:00
Karel Zak
e8c21c894e build-sys: add --disable-hwclock-gplv3
The currently used date/time parser (for hwclock --set --date <date>)
is gnulib based code with GPLv3.

This patch allows to avoid this code and replace it with minimalistic
date/time parser.

Addresses: https://github.com/karelzak/util-linux/issues/891
Reported-by: Carlos Santos <unixmania@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-01-27 16:23:21 +01:00
Karel Zak
68a2ade7ed hwclock: add SPDX-License-Identifier(s)
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-11-08 11:12:13 +01:00
Karel Zak
b3fc2a3c33 hwclock: report rtc open() errors on --verbose
Let's be more verbose and provide real open() error to make
debugging easier on --verbose.

For example:

$ hwclock --verbose
hwclock from util-linux 2.34.193-6bebea-dirty
System Time: 1570445823.701266
Trying to open: /dev/rtc0
hwclock: cannot open /dev/rtc0: Permission denied          <---
No usable clock interface found.
hwclock: Cannot access the Hardware Clock via any known method.

Addresses: https://github.com/karelzak/util-linux/issues/879
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-07 12:57:43 +02:00
Elliott Mitchell
2bb3aa36b2 cleanup: Remove some spurious spaces
Sorry detail-oriented people tend to wipe these out if they notice them.
Add in automated tools and lots of excess end-of-line spaces get wiped
out.

Addresses: https://github.com/karelzak/util-linux/pull/849
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-10-01 13:01:43 +02:00
Carlos Santos
88bc304ba8 hwclock: use CMOS clock only if available
- Add --disable-hwclock-cmos configuration argument
- Add USE_HWCLOCK_CMOS (enabled by default for i386/x86_64)
- Add define(USE_HWCLOCK_CMOS)
- Compile hwclock-cmos.c only if USE_HWCLOCK_CMOS is true
- Remove all unnecessary #ifdefs from hwclock-cmos.c
- Add #ifdef USE_HWCLOCK_CMOS around the determine_clock_access_method()
  call in hwclock.c

Signed-off-by: Carlos Santos <unixmania@gmail.com>
2019-07-15 13:56:13 +02:00
Karel Zak
226fdcf069 hwclock: don't use uninitialized value [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-23 15:28:07 +02:00
Karel Zak
2c308875a7 misc: consolidate version printing and close_stdout()
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-04-16 15:14:13 +02:00
Sami Kerola
4813a5210f various: fix 'uninitialized when used' warnings [clang]
This change fixes "warning: variable 'var' may be uninitialized when used
here [-Wconditional-uninitialized]" warnings reported in various files.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2019-02-18 13:19:24 +01:00
Karel Zak
df4f1a6647 hwclock: add --delay <seconds>
* add command line option --delay <seconds>

* read RTC type from /sys/class/rtc/rtc<N>/name

* default to 0.5 (500ms) for rtc_cmos or when RTC type is impossible
  determine; otherwise delay is 0.

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-18 13:59:15 +02:00
J William Piggott
ff4e18bd2d hwclock: add --ul-debug implementing debug.h
Undocumented at this time, because it is a skeleton
implementation.  More debugging points are to be added after
refactoring is complete, or ad hoc in the mean time.

When fully implemented, enough time may have passed that the
deprecated --debug could be used to replace --ul-debug.

[kzak@redhat.com: - use __UL_INIT_DEBUG_FROM_STRING() to initialize the mask
                  - add hwclock_init_debug()]

Coauthored-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: J William Piggott <elseifthen@gmx.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-01-22 11:10:22 +01:00
J William Piggott
de4568f757 hwclock: rename --debug option to --verbose
Warn on --debug; do not fallthrough because
the message is lost in the verbose output.

Coauthored-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2018-01-17 13:29:11 +01:00
J William Piggott
6cdc7b9c02 lib/timeutils.c: warn format_iso_time() overflow
Print a message when the format_iso_time() buffer is exceeded, because
there is more than one type of failure that returns -1.

Also remove the corresponding message from hwclock.c.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-12-09 18:43:29 -05:00
J William Piggott
af54dbd929 hwclock: fix debug regression
commit f0a0ce7 makes debug mode implicit for the --test option.

Using the previous command syntax of --test and --debug
together invokes the undocumented Level 2 debugging when
setting the RTC. This can cause many thousands of lines of
output like:
1510967983.499968 < 1510967983.500000 (-0.000032)

Fix: bump Level 2 debugging to Level 9, just before the
other undocumented Level 10. This makes it improbable for
the development debug levels to be accessed unintentionally.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-11-17 20:25:40 -05:00
J William Piggott
01d971945c hwclock: use reentrant time conversion functions
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-11-16 19:43:16 -05:00
J William Piggott
4111bb3ab5 lib/timeutils: add common ISO timestamp masks
* Start the ISO format flags at bit 0 instead of bit 1.

* Remove unnecessary _8601 from ISO format flag names to
  avoid line wrapping and to ease readability.

* ISO timestamps have date-time-timzone in common, so move
  the TIMEZONE flag to bit 2 causing all timestamp masks
  to have the first three bits set and the last four bits
  as timestamp 'options'.

* Change the 'SPACE' flag to a 'T' flag, because it makes
  the code and comments more concise.

* Add common ISO timestamp masks.

* Implement the ISO timestamp masks in all applicable code
  using the strxxx_iso() functions.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-11-10 16:34:55 -05:00
J William Piggott
9fd0a7a96c lib/timeutils: add get_gmtoff()
This new function returns the GMT offset relative to its
argument. It is used in this patch to fix two bugs:

1) On platforms that the tm struct excludes tm_gmtoff,
   hwclock assumes a one hour DST offset. This can cause
   an incorrect kernel timezone setting. For example:

 Master branch tested with tm_gmtoff illustrates the correct offset:
$ TZ="Australia/Lord_Howe" hwclock --hctosys --test | grep settimeofday
Calling settimeofday(1507494204.192398, -660)

 Master branch tested without tm_gmtoff has an incorrect offset:
$ TZ="Australia/Lord_Howe" hwclock --hctosys --test | grep settimeofday
Calling settimeofday(1507494249.193852, -690)

 Patched tested without tm_gmtoff has the correct offset:
$ TZ="Australia/Lord_Howe" hwclock --hctosys --test | grep settimeofday
Calling settimeofday(1507494260.194208, -660)

2) ISO 8601 'extended' format requires all time elements
   to use a colon (:).

Current invalid ISO 8601:
$ hwclock
2017-10-08 16:25:17.895462-0400

Patched:
$ hwclock
2017-10-08 16:25:34.141895-04:00

Also required by this change:
login-utils/last.c: increase ISO out_len and in_len by one to
                    accommodate the addition of the timezone colon.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-11-10 16:14:04 -05:00
J William Piggott
10191da63a hwclock: add iso-8601 overflow check
hwclock wasn't testing for strtimeval_iso() truncation:

/sbin/hwclock --utc --noadjfile --predict --date '7982 years'; echo $?
9999-09-25 19:33:01.000000-0400
0

/sbin/hwclock --utc --noadjfile --predict --date '7983 years'; echo $?
10000-09-25 19:33:10.000000-
0

Patched:
./hwclock --utc --noadjfile --predict --date '7982 years'; echo $?
9999-09-25 19:22:15.000000-0400
0

./hwclock --utc --noadjfile --predict --date '7983 years'; echo $?
hwclock: iso-8601 format truncated
1

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-11-10 15:49:45 -05:00
J William Piggott
f0a0ce7445 hwclock: make debug implicit for test mode.
Calling --test without --debug is not useful, so
make it implicit.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-09-21 14:53:43 -04:00
J William Piggott
c4b0dc3ea3 hwclock: exit bug fix and single test mode message
Bug fix: hwclock returns success when saving /etc/adjtime fails.

Remove redundant test mode messages.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-09-21 14:43:02 -04:00
Sami Kerola
40191b5f95 hwclock: add -a that is short of --adjust to manual page and usage
In same go use -V as return value of --version from getopts_long().

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2017-09-05 14:34:53 +02:00
J William Piggott
08e3c9e662 hwclock: update usage()
Improve usage strings for debug and version.

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-09-05 12:33:19 +02:00
J William Piggott
5b8e46f7e7 hwclock: close hwaudit_fd unconditionally
Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-09-03 12:34:03 -04:00
J William Piggott
652dcf5131 hwclock: don't always use hwclock_exit
Special exit handling is not wanted for usage() or bad
command syntax. For example we do not want to audit:
hwclock --set --date foo

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-09-03 12:23:44 -04:00
J William Piggott
c47a8f2a87 hwclock: remove sysexits.h
sysexits.h was introduced in v2.11t prior to util-linux-ng, with the
HISTORY entry: * hwclock: minor polishing.

So there was no specific issue solved by adding it. Its use was never
documented so it should be safe to remove.

Also, fix return values being used for the exit status that were not
magic constants (portability issue).

Signed-off-by: J William Piggott <elseifthen@gmx.com>
2017-09-03 12:03:10 -04:00