[LTP] [PATCH 2/3] rtc02: reset backward 1 hour to RTC time

Li Wang
2022-04-28T13:27:38Z

If there's a limit on how much backward time can be set to RTC,
rtc02 easily get an EINVAL error as well. (Note: that limitation
may enforced by the host or by EFI firmware)

rtc02.c:70: TFAIL: ioctl() RTC_SET_TIME: EINVAL (22)
tst_rtctime.c:116: TWARN: open(/dev/rtc,0,21042104211) failed: EBUSY (16)
tst_wallclock.c:117: TWARN: tst_rtc_settime() realtime failed: EBADF (9)

To show the problem by manually performing `hwclock -w`.

# date -s "2020-10-09 13:23:30"
Fri Oct 9 13:23:30 EDT 2020
# hwclock -w
hwclock: ioctl(RTC_SET_TIME) to /dev/rtc0 to set the time failed: Invalid argument

If just go with a few days backwards things work:

# date -s "2022-03-12 13:23:30"
Sat Mar 12 13:23:30 EST 2022
# hwclock -w
# echo $?
0

To be on the safe side, here reset backward +/-1 hour (in case of zero clock).

Signed-off-by: Li Wang <liwang@redhat.com>
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: Eirik Fuller <efuller@redhat.com>
---
testcases/kernel/device-drivers/rtc/rtc02.c | 27 ++++++++++++---------
1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/testcases/kernel/device-drivers/rtc/rtc02.c b/testcases/kernel/device-drivers/rtc/rtc02.c
index 8484a0074..6198a5d5d 100644
--- a/testcases/kernel/device-drivers/rtc/rtc02.c
+++ b/testcases/kernel/device-drivers/rtc/rtc02.c
@@ -51,19 +51,23 @@ static int rtc_tm_cmp(struct rtc_time *set_tm, struct rtc_time *read_tm)

static void set_rtc_test(void)
{
- struct rtc_time read_tm;
+ struct rtc_time read_tm, set_tm;
int ret;

- struct rtc_time set_tm = {
- .tm_sec = 30,
- .tm_min = 23,
- .tm_hour = 13,
- .tm_mday = 9,
- .tm_mon = 9,
- .tm_year = 120,
- };
+ /* Read current RTC Time */
+ ret = tst_rtc_gettime(rtc_dev, &read_tm);
+ if (ret != 0) {
+ tst_res(TFAIL | TERRNO, "ioctl() RTC_RD_TIME");
+ return;
+ }
+
+ /* set rtc to +/-1 hour */
+ set_tm = read_tm;
+ if (set_tm.tm_hour == 0)
+ set_tm.tm_hour += 1;
+ else
+ set_tm.tm_hour -= 1;

- /* set rtc to 2020.10.9 13:23:30 */
tst_res(TINFO, "To set RTC date/time is: %s", rtctime_to_str(&set_tm));

ret = tst_rtc_settime(rtc_dev, &set_tm);
@@ -76,7 +80,7 @@ static void set_rtc_test(void)
return;
}

- /* Read current RTC Time */
+ /* Read new RTC Time */
ret = tst_rtc_gettime(rtc_dev, &read_tm);
if (ret != 0) {
tst_res(TFAIL | TERRNO, "ioctl() RTC_RD_TIME");
@@ -89,7 +93,6 @@ static void set_rtc_test(void)
return;
}
tst_res(TPASS, "The read RTC time is consistent with set time");
-
}

static void rtc_setup(void)
--
2.35.1