From 1eca65fcda0d0930ec30bbe6fc90a9c1631d5d84 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sun, 3 May 2020 11:04:48 +0200 Subject: [PATCH] plugins: rtc: Move TZ set/unset to rtc plugin from Finit main This patch makes the RTC plugin a bit more stand-alone, it is now also back in the hands of the user to set a default timezone. Signed-off-by: Joachim Nilsson --- plugins/rtc.c | 30 ++++++++++++++++++++++++++++++ src/finit.c | 5 +---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/plugins/rtc.c b/plugins/rtc.c index f4c57077..17f0d397 100644 --- a/plugins/rtc.c +++ b/plugins/rtc.c @@ -33,6 +33,29 @@ /* Kernel RTC driver validates against this date for sanity check */ #define RTC_TIMESTAMP_BEGIN_2000 946684800LL /* 2000-01-01 00:00:00 */ +static void tz_set(char *tz, size_t len) +{ + char *ptr; + + ptr = getenv("TZ"); + if (!ptr) + memset(tz, 0, len); + else + strlcpy(tz, ptr, len); + + setenv("TZ", "UTC0", 1); + tzset(); +} + +static void tz_restore(char *tz) +{ + if (!tz || !tz[0]) + unsetenv("TZ"); + else + setenv("TZ", tz, 1); + tzset(); +} + static int rtc_open(void) { char *alt[] = { @@ -59,11 +82,13 @@ static void rtc_save(void *arg) struct timeval tv = { 0 }; struct tm tm = { 0 }; int fd, rc = 0; + char tz[128]; fd = rtc_open(); if (fd < 0) return; + tz_set(tz, sizeof(tz)); rc = gettimeofday(&tv, NULL); if (rc < 0 || tv.tv_sec < RTC_TIMESTAMP_BEGIN_2000) { print_desc(NULL, "System clock invalid, not saving to RTC"); @@ -74,6 +99,7 @@ static void rtc_save(void *arg) } 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) @@ -83,6 +109,7 @@ static void rtc_save(void *arg) } out: + tz_restore(tz); print(rc, NULL); close(fd); } @@ -92,11 +119,13 @@ static void rtc_restore(void *arg) struct timeval tv = { 0 }; struct tm tm = { 0 }; int fd, rc = 0; + char tz[128]; fd = rtc_open(); if (fd < 0) return; + tz_set(tz, sizeof(tz)); if (ioctl(fd, RTC_RD_TIME, &tm) < 0) { char msg[120]; @@ -129,6 +158,7 @@ static void rtc_restore(void *arg) rc = 1; out: + tz_restore(tz); print(rc, NULL); close(fd); } diff --git a/src/finit.c b/src/finit.c index 3f2087d5..4d2bc9f6 100644 --- a/src/finit.c +++ b/src/finit.c @@ -321,14 +321,11 @@ int main(int argc, char *argv[]) ctx = &loop; /* - * Set PATH, SHELL, PWD, TZ and umask early to something sane + * Set PATH, SHELL, PWD, and umask early to something sane */ setenv("PATH", _PATH_STDPATH, 1); setenv("SHELL", _PATH_BSHELL, 1); - setenv("TZ", "UTC0", 1); - tzset(); - chdir("/"); umask(0);