Skip to content

Commit 5d444b7

Browse files
committed
plugins:rtc.c: drop some invalid condition checks
There are some invalid condition checks in rtc.c, when rtc_file is missing, it should not consider that as a error and return, otherwise, it can lead to the case that time_set wont be called at all. With this fix, when both RTC and file restore fail, it will fall back to call time_set(NULL) and restore time from rtc_timestamp, this ensures the OS has a valid time at the very first boot. Signed-off-by: Ming Liu <[email protected]>
1 parent 6f6267f commit 5d444b7

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

plugins/rtc.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,35 +170,41 @@ static void file_save(void *arg)
170170
static void file_restore(void *arg)
171171
{
172172
struct tm tm = { 0 };
173-
int rc = 1;
173+
int rc = 0;
174174
FILE *fp;
175175

176176
if (!rtc_file) {
177-
logit(LOG_NOTICE, "System has no RTC (missing driver?), skipping restore.");
177+
print_desc(NULL, "Restoring system time from timestamp");
178+
rc = time_set(NULL);
179+
print(rc, NULL);
178180
return;
179181
}
180182

181-
print_desc(NULL, "Restoring system clock from backup");
182-
183183
fp = fopen(rtc_file, "r");
184184
if (fp) {
185185
char buf[32];
186186

187187
if (fgets(buf, sizeof(buf), fp)) {
188188
chomp(buf);
189189
strptime(buf, RTC_FMT, &tm);
190-
rc = time_set(&tm);
190+
if (!strptime(buf, RTC_FMT, &tm))
191+
rc = 1;
191192
}
192193
fclose(fp);
193-
} else
194-
logit(LOG_WARNING, "Missing %s", rtc_file);
194+
}
195+
rc = 1;
195196

196197
if (rc) {
197-
time_set(NULL);
198-
rc = 2;
198+
print_desc(NULL, "Failed to restore system clock from restore file");
199+
print(2, NULL);
200+
print_desc(NULL, "Restoring system time from timestamp");
201+
rc = time_set(NULL);
202+
print(rc, NULL);
203+
} else {
204+
print_desc(NULL, "Restoring system clock from restore file");
205+
rc = time_set(&tm);
206+
print(rc, NULL);
199207
}
200-
201-
print(rc, NULL);
202208
}
203209

204210
static int rtc_open(void)
@@ -292,8 +298,7 @@ static void rtc_restore(void *arg)
292298
print(2, NULL);
293299

294300
/* Try restoring from last save game */
295-
if (rtc_file)
296-
file_restore(arg);
301+
file_restore(arg);
297302
} else
298303
print(0, NULL);
299304

0 commit comments

Comments
 (0)