Skip to content

Commit

Permalink
package/finit: backport upstream v4.9-pre patches
Browse files Browse the repository at this point in the history
Backport support for saving and restoring system time from a file on
systems with broken RTC that reset to the future.

With this support a system can be sure time only ever moves forward.

Fix #794

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
  • Loading branch information
troglobit committed Nov 4, 2024
1 parent c23b9c2 commit 5a38e87
Show file tree
Hide file tree
Showing 8 changed files with 621 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
From 46ffa81f5c88ce95db011369d8bfb802313e4217 Mon Sep 17 00:00:00 2001
From: Joachim Wiberg <troglobit@gmail.com>
Date: Thu, 17 Oct 2024 14:23:24 +0200
Subject: [PATCH 1/2] Only mark rdeps dirty if main service is nohup
Subject: [PATCH 1/6] Only mark rdeps dirty if main service is nohup
Organization: Addiva Elektronik

This patch changes a behavior that's been default since Finit 4.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
From 119e66a7e9c95283918639b51dd03a3d666955f8 Mon Sep 17 00:00:00 2001
From: Joachim Wiberg <troglobit@gmail.com>
Date: Mon, 28 Oct 2024 10:58:04 +0100
Subject: [PATCH 2/2] Reset color attributes and clear screen when starting up
Subject: [PATCH 2/6] Reset color attributes and clear screen when starting up
Organization: Addiva Elektronik

Some boot loaders, like GRUB, leave background color artifacts from
Expand Down
196 changes: 196 additions & 0 deletions package/finit/0003-plugins-refactor-rtc.so.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
From 0c0e880f3fdd38f7bbde618408378dc0a19ff005 Mon Sep 17 00:00:00 2001
From: Joachim Wiberg <troglobit@gmail.com>
Date: Sun, 3 Nov 2024 09:39:46 +0100
Subject: [PATCH 3/6] plugins: refactor rtc.so
Organization: Addiva Elektronik

Factor out time_set() and time_get() for readability and reuse.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
---
plugins/rtc.c | 116 +++++++++++++++++++++++++++++---------------------
1 file changed, 68 insertions(+), 48 deletions(-)

diff --git a/plugins/rtc.c b/plugins/rtc.c
index 238791f..9520c7d 100644
--- a/plugins/rtc.c
+++ b/plugins/rtc.c
@@ -68,6 +68,60 @@ static void tz_restore(char *tz)
tzset();
}

+static int time_set(struct tm *tm)
+{
+ struct tm fallback = { 0 };
+ struct timeval tv = { 0 };
+ char tz[128];
+ int rc = 0;
+
+ tz_set(tz, sizeof(tz));
+
+ if (!tm) {
+ logit(LOG_NOTICE, "Resetting system clock to kernel default, %s.", rtc_timestamp);
+ tm = &fallback;
+
+ /* Attempt to set RTC to a sane value ... */
+ tv.tv_sec = rtc_date_fallback;
+ if (!gmtime_r(&tv.tv_sec, tm)) {
+ rc = 1;
+ goto out;
+ }
+ }
+
+ tm->tm_isdst = -1; /* Use tzdata to figure it out, please. */
+ tv.tv_sec = mktime(tm);
+ if (tv.tv_sec == (time_t)-1 || tv.tv_sec < rtc_date_fallback) {
+ errno = EINVAL;
+ rc = 2;
+ } else {
+ if (settimeofday(&tv, NULL) == -1)
+ rc = 1;
+ }
+out:
+ tz_restore(tz);
+ return rc;
+}
+
+static int time_get(struct tm *tm)
+{
+ struct timeval tv = { 0 };
+ char tz[128];
+ int rc = 0;
+
+ tz_set(tz, sizeof(tz));
+
+ rc = gettimeofday(&tv, NULL);
+ if (rc < 0 || tv.tv_sec < rtc_date_fallback)
+ rc = 2;
+ else
+ gmtime_r(&tv.tv_sec, tm);
+
+ tz_restore(tz);
+
+ return rc;
+}
+
static int rtc_open(void)
{
char *alt[] = {
@@ -91,10 +145,8 @@ static int rtc_open(void)

static void rtc_save(void *arg)
{
- struct timeval tv = { 0 };
struct tm tm = { 0 };
int fd, rc = 0;
- char tz[128];

if (rescue) {
dbg("Skipping %s plugin in rescue mode.", __FILE__);
@@ -105,38 +157,26 @@ static void rtc_save(void *arg)
if (fd < 0)
return;

- tz_set(tz, sizeof(tz));
- rc = gettimeofday(&tv, NULL);
- if (rc < 0 || tv.tv_sec < rtc_date_fallback) {
+ if ((rc = time_get(&tm))) {
print_desc(NULL, "System clock invalid, not saving to RTC");
- invalid:
- logit(LOG_ERR, "System clock invalid, before %s, not saving to RTC", rtc_timestamp);
- rc = 2;
- goto out;
+ } else {
+ print_desc(NULL, "Saving system clock (UTC) to RTC");
+ rc = ioctl(fd, RTC_SET_TIME, &tm);
}

- print_desc(NULL, "Saving system time (UTC) to RTC");
-
- gmtime_r(&tv.tv_sec, &tm);
- if (ioctl(fd, RTC_SET_TIME, &tm) < 0) {
- if (EINVAL == errno)
- goto invalid;
- rc = 1;
- goto out;
+ if (rc && errno == EINVAL) {
+ logit(LOG_ERR, "System clock invalid, before %s, not saving to RTC", rtc_timestamp);
+ rc = 2;
}

-out:
- tz_restore(tz);
print(rc, NULL);
close(fd);
}

static void rtc_restore(void *arg)
{
- struct timeval tv = { 0 };
struct tm tm = { 0 };
int fd, rc = 0;
- char tz[128];

if (rescue) {
dbg("Skipping %s plugin in rescue mode.", __FILE__);
@@ -149,16 +189,19 @@ static void rtc_restore(void *arg)
return;
}

- tz_set(tz, sizeof(tz));
- if (ioctl(fd, RTC_RD_TIME, &tm) < 0) {
+ if ((rc = ioctl(fd, RTC_RD_TIME, &tm)) < 0) {
char msg[120];

snprintf(msg, sizeof(msg), "Failed restoring system clock, %s",
EINVAL == errno ? "RTC time is too old" :
ENOENT == errno ? "RTC has no saved time" : "see log for details");
print_desc(NULL, msg);
+ } else {
+ print_desc(NULL, "Restoring system clock (UTC) from RTC");
+ rc = time_set(&tm);
+ }

- invalid:
+ if (rc) {
logit(LOG_ERR, "Failed restoring system clock from RTC.");
if (EINVAL == errno)
logit(LOG_ERR, "RTC time is too old (before %s)", rtc_timestamp);
@@ -167,33 +210,10 @@ static void rtc_restore(void *arg)
else
logit(LOG_ERR, "RTC error code %d: %s", errno, strerror(errno));

- /* Been here already? */
- if (rc)
- goto out;
-
- /* Attempt to set RTC to a sane value ... */
- tv.tv_sec = rtc_date_fallback;
- if (!gmtime_r(&tv.tv_sec, &tm))
- goto out;
-
- logit(LOG_NOTICE, "Resetting RTC to kernel default, %s.", rtc_timestamp);
+ time_set(NULL);
rc = 2;
}

- if (!rc)
- print_desc(NULL, "Restoring system clock (UTC) from RTC");
- tm.tm_isdst = -1; /* Use tzdata to figure it out, please. */
- tv.tv_sec = mktime(&tm);
- if (tv.tv_sec == (time_t)-1 || tv.tv_sec < rtc_date_fallback) {
- errno = EINVAL;
- goto invalid;
- }
-
- if (settimeofday(&tv, NULL) == -1)
- rc = 1;
-
-out:
- tz_restore(tz);
print(rc, NULL);
close(fd);
}
--
2.43.0

Loading

0 comments on commit 5a38e87

Please sign in to comment.