Skip to content

Commit 5a38e87

Browse files
committed
package/finit: backport upstream v4.9-pre patches
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>
1 parent c23b9c2 commit 5a38e87

8 files changed

+621
-7
lines changed

package/finit/0001-Only-mark-rdeps-dirty-if-main-service-is-nohup.patch

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 46ffa81f5c88ce95db011369d8bfb802313e4217 Mon Sep 17 00:00:00 2001
22
From: Joachim Wiberg <troglobit@gmail.com>
33
Date: Thu, 17 Oct 2024 14:23:24 +0200
4-
Subject: [PATCH 1/2] Only mark rdeps dirty if main service is nohup
4+
Subject: [PATCH 1/6] Only mark rdeps dirty if main service is nohup
55
Organization: Addiva Elektronik
66

77
This patch changes a behavior that's been default since Finit 4.0,

package/finit/0002-Reset-color-attributes-and-clear-screen-when-startin.patch

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 119e66a7e9c95283918639b51dd03a3d666955f8 Mon Sep 17 00:00:00 2001
22
From: Joachim Wiberg <troglobit@gmail.com>
33
Date: Mon, 28 Oct 2024 10:58:04 +0100
4-
Subject: [PATCH 2/2] Reset color attributes and clear screen when starting up
4+
Subject: [PATCH 2/6] Reset color attributes and clear screen when starting up
55
Organization: Addiva Elektronik
66

77
Some boot loaders, like GRUB, leave background color artifacts from
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
From 0c0e880f3fdd38f7bbde618408378dc0a19ff005 Mon Sep 17 00:00:00 2001
2+
From: Joachim Wiberg <troglobit@gmail.com>
3+
Date: Sun, 3 Nov 2024 09:39:46 +0100
4+
Subject: [PATCH 3/6] plugins: refactor rtc.so
5+
Organization: Addiva Elektronik
6+
7+
Factor out time_set() and time_get() for readability and reuse.
8+
9+
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
10+
---
11+
plugins/rtc.c | 116 +++++++++++++++++++++++++++++---------------------
12+
1 file changed, 68 insertions(+), 48 deletions(-)
13+
14+
diff --git a/plugins/rtc.c b/plugins/rtc.c
15+
index 238791f..9520c7d 100644
16+
--- a/plugins/rtc.c
17+
+++ b/plugins/rtc.c
18+
@@ -68,6 +68,60 @@ static void tz_restore(char *tz)
19+
tzset();
20+
}
21+
22+
+static int time_set(struct tm *tm)
23+
+{
24+
+ struct tm fallback = { 0 };
25+
+ struct timeval tv = { 0 };
26+
+ char tz[128];
27+
+ int rc = 0;
28+
+
29+
+ tz_set(tz, sizeof(tz));
30+
+
31+
+ if (!tm) {
32+
+ logit(LOG_NOTICE, "Resetting system clock to kernel default, %s.", rtc_timestamp);
33+
+ tm = &fallback;
34+
+
35+
+ /* Attempt to set RTC to a sane value ... */
36+
+ tv.tv_sec = rtc_date_fallback;
37+
+ if (!gmtime_r(&tv.tv_sec, tm)) {
38+
+ rc = 1;
39+
+ goto out;
40+
+ }
41+
+ }
42+
+
43+
+ tm->tm_isdst = -1; /* Use tzdata to figure it out, please. */
44+
+ tv.tv_sec = mktime(tm);
45+
+ if (tv.tv_sec == (time_t)-1 || tv.tv_sec < rtc_date_fallback) {
46+
+ errno = EINVAL;
47+
+ rc = 2;
48+
+ } else {
49+
+ if (settimeofday(&tv, NULL) == -1)
50+
+ rc = 1;
51+
+ }
52+
+out:
53+
+ tz_restore(tz);
54+
+ return rc;
55+
+}
56+
+
57+
+static int time_get(struct tm *tm)
58+
+{
59+
+ struct timeval tv = { 0 };
60+
+ char tz[128];
61+
+ int rc = 0;
62+
+
63+
+ tz_set(tz, sizeof(tz));
64+
+
65+
+ rc = gettimeofday(&tv, NULL);
66+
+ if (rc < 0 || tv.tv_sec < rtc_date_fallback)
67+
+ rc = 2;
68+
+ else
69+
+ gmtime_r(&tv.tv_sec, tm);
70+
+
71+
+ tz_restore(tz);
72+
+
73+
+ return rc;
74+
+}
75+
+
76+
static int rtc_open(void)
77+
{
78+
char *alt[] = {
79+
@@ -91,10 +145,8 @@ static int rtc_open(void)
80+
81+
static void rtc_save(void *arg)
82+
{
83+
- struct timeval tv = { 0 };
84+
struct tm tm = { 0 };
85+
int fd, rc = 0;
86+
- char tz[128];
87+
88+
if (rescue) {
89+
dbg("Skipping %s plugin in rescue mode.", __FILE__);
90+
@@ -105,38 +157,26 @@ static void rtc_save(void *arg)
91+
if (fd < 0)
92+
return;
93+
94+
- tz_set(tz, sizeof(tz));
95+
- rc = gettimeofday(&tv, NULL);
96+
- if (rc < 0 || tv.tv_sec < rtc_date_fallback) {
97+
+ if ((rc = time_get(&tm))) {
98+
print_desc(NULL, "System clock invalid, not saving to RTC");
99+
- invalid:
100+
- logit(LOG_ERR, "System clock invalid, before %s, not saving to RTC", rtc_timestamp);
101+
- rc = 2;
102+
- goto out;
103+
+ } else {
104+
+ print_desc(NULL, "Saving system clock (UTC) to RTC");
105+
+ rc = ioctl(fd, RTC_SET_TIME, &tm);
106+
}
107+
108+
- print_desc(NULL, "Saving system time (UTC) to RTC");
109+
-
110+
- gmtime_r(&tv.tv_sec, &tm);
111+
- if (ioctl(fd, RTC_SET_TIME, &tm) < 0) {
112+
- if (EINVAL == errno)
113+
- goto invalid;
114+
- rc = 1;
115+
- goto out;
116+
+ if (rc && errno == EINVAL) {
117+
+ logit(LOG_ERR, "System clock invalid, before %s, not saving to RTC", rtc_timestamp);
118+
+ rc = 2;
119+
}
120+
121+
-out:
122+
- tz_restore(tz);
123+
print(rc, NULL);
124+
close(fd);
125+
}
126+
127+
static void rtc_restore(void *arg)
128+
{
129+
- struct timeval tv = { 0 };
130+
struct tm tm = { 0 };
131+
int fd, rc = 0;
132+
- char tz[128];
133+
134+
if (rescue) {
135+
dbg("Skipping %s plugin in rescue mode.", __FILE__);
136+
@@ -149,16 +189,19 @@ static void rtc_restore(void *arg)
137+
return;
138+
}
139+
140+
- tz_set(tz, sizeof(tz));
141+
- if (ioctl(fd, RTC_RD_TIME, &tm) < 0) {
142+
+ if ((rc = ioctl(fd, RTC_RD_TIME, &tm)) < 0) {
143+
char msg[120];
144+
145+
snprintf(msg, sizeof(msg), "Failed restoring system clock, %s",
146+
EINVAL == errno ? "RTC time is too old" :
147+
ENOENT == errno ? "RTC has no saved time" : "see log for details");
148+
print_desc(NULL, msg);
149+
+ } else {
150+
+ print_desc(NULL, "Restoring system clock (UTC) from RTC");
151+
+ rc = time_set(&tm);
152+
+ }
153+
154+
- invalid:
155+
+ if (rc) {
156+
logit(LOG_ERR, "Failed restoring system clock from RTC.");
157+
if (EINVAL == errno)
158+
logit(LOG_ERR, "RTC time is too old (before %s)", rtc_timestamp);
159+
@@ -167,33 +210,10 @@ static void rtc_restore(void *arg)
160+
else
161+
logit(LOG_ERR, "RTC error code %d: %s", errno, strerror(errno));
162+
163+
- /* Been here already? */
164+
- if (rc)
165+
- goto out;
166+
-
167+
- /* Attempt to set RTC to a sane value ... */
168+
- tv.tv_sec = rtc_date_fallback;
169+
- if (!gmtime_r(&tv.tv_sec, &tm))
170+
- goto out;
171+
-
172+
- logit(LOG_NOTICE, "Resetting RTC to kernel default, %s.", rtc_timestamp);
173+
+ time_set(NULL);
174+
rc = 2;
175+
}
176+
177+
- if (!rc)
178+
- print_desc(NULL, "Restoring system clock (UTC) from RTC");
179+
- tm.tm_isdst = -1; /* Use tzdata to figure it out, please. */
180+
- tv.tv_sec = mktime(&tm);
181+
- if (tv.tv_sec == (time_t)-1 || tv.tv_sec < rtc_date_fallback) {
182+
- errno = EINVAL;
183+
- goto invalid;
184+
- }
185+
-
186+
- if (settimeofday(&tv, NULL) == -1)
187+
- rc = 1;
188+
-
189+
-out:
190+
- tz_restore(tz);
191+
print(rc, NULL);
192+
close(fd);
193+
}
194+
--
195+
2.43.0
196+

0 commit comments

Comments
 (0)