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 <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2020-05-03 11:04:48 +02:00
1 parent b364492a8c
commit 1eca65fcda
2 files changed
+31 -4

No files matched your search

+30
View File
@@ -33,6 +33,29 @@
/* Kernel RTC driver validates against this date for sanity check */ /* Kernel RTC driver validates against this date for sanity check */
#define RTC_TIMESTAMP_BEGIN_2000 946684800LL /* 2000-01-01 00:00:00 */ #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) static int rtc_open(void)
{ {
char *alt[] = { char *alt[] = {
@@ -59,11 +82,13 @@ static void rtc_save(void *arg)
struct timeval tv = { 0 }; struct timeval tv = { 0 };
struct tm tm = { 0 }; struct tm tm = { 0 };
int fd, rc = 0; int fd, rc = 0;
char tz[128];
fd = rtc_open(); fd = rtc_open();
if (fd < 0) if (fd < 0)
return; return;
tz_set(tz, sizeof(tz));
rc = gettimeofday(&tv, NULL); rc = gettimeofday(&tv, NULL);
if (rc < 0 || tv.tv_sec < RTC_TIMESTAMP_BEGIN_2000) { if (rc < 0 || tv.tv_sec < RTC_TIMESTAMP_BEGIN_2000) {
print_desc(NULL, "System clock invalid, not saving to RTC"); 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"); print_desc(NULL, "Saving system time (UTC) to RTC");
gmtime_r(&tv.tv_sec, &tm); gmtime_r(&tv.tv_sec, &tm);
if (ioctl(fd, RTC_SET_TIME, &tm) < 0) { if (ioctl(fd, RTC_SET_TIME, &tm) < 0) {
if (EINVAL == errno) if (EINVAL == errno)
@@ -83,6 +109,7 @@ static void rtc_save(void *arg)
} }
out: out:
tz_restore(tz);
print(rc, NULL); print(rc, NULL);
close(fd); close(fd);
} }
@@ -92,11 +119,13 @@ static void rtc_restore(void *arg)
struct timeval tv = { 0 }; struct timeval tv = { 0 };
struct tm tm = { 0 }; struct tm tm = { 0 };
int fd, rc = 0; int fd, rc = 0;
char tz[128];
fd = rtc_open(); fd = rtc_open();
if (fd < 0) if (fd < 0)
return; return;
tz_set(tz, sizeof(tz));
if (ioctl(fd, RTC_RD_TIME, &tm) < 0) { if (ioctl(fd, RTC_RD_TIME, &tm) < 0) {
char msg[120]; char msg[120];
@@ -129,6 +158,7 @@ static void rtc_restore(void *arg)
rc = 1; rc = 1;
out: out:
tz_restore(tz);
print(rc, NULL); print(rc, NULL);
close(fd); close(fd);
} }
+1 -4
View File
@@ -321,14 +321,11 @@ int main(int argc, char *argv[])
ctx = &loop; 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("PATH", _PATH_STDPATH, 1);
setenv("SHELL", _PATH_BSHELL, 1); setenv("SHELL", _PATH_BSHELL, 1);
setenv("TZ", "UTC0", 1);
tzset();
chdir("/"); chdir("/");
umask(0); umask(0);