|
| 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