Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Common RTC functions |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 3 | * |
| 4 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include "libbb.h" |
| 8 | #include "rtc_.h" |
| 9 | |
| 10 | #if ENABLE_FEATURE_HWCLOCK_ADJTIME_FHS |
| 11 | # define ADJTIME_PATH "/var/lib/hwclock/adjtime" |
| 12 | #else |
| 13 | # define ADJTIME_PATH "/etc/adjtime" |
| 14 | #endif |
| 15 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 16 | int FAST_FUNC rtc_adjtime_is_utc(void) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 17 | { |
| 18 | int utc = 0; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 19 | FILE *f = fopen_for_read(ADJTIME_PATH); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 20 | |
| 21 | if (f) { |
| 22 | RESERVE_CONFIG_BUFFER(buffer, 128); |
| 23 | |
| 24 | while (fgets(buffer, sizeof(buffer), f)) { |
| 25 | int len = strlen(buffer); |
| 26 | |
| 27 | while (len && isspace(buffer[len - 1])) |
| 28 | len--; |
| 29 | |
| 30 | buffer[len] = 0; |
| 31 | |
| 32 | if (strncmp(buffer, "UTC", 3) == 0) { |
| 33 | utc = 1; |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | fclose(f); |
| 38 | |
| 39 | RELEASE_CONFIG_BUFFER(buffer); |
| 40 | } |
| 41 | |
| 42 | return utc; |
| 43 | } |
| 44 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 45 | int FAST_FUNC rtc_xopen(const char **default_rtc, int flags) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 46 | { |
| 47 | int rtc; |
| 48 | |
Mike Frysinger | 977bc6a | 2008-02-15 07:19:03 +0000 | [diff] [blame] | 49 | if (!*default_rtc) { |
| 50 | *default_rtc = "/dev/rtc"; |
| 51 | rtc = open(*default_rtc, flags); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 52 | if (rtc >= 0) |
| 53 | return rtc; |
Mike Frysinger | 977bc6a | 2008-02-15 07:19:03 +0000 | [diff] [blame] | 54 | *default_rtc = "/dev/rtc0"; |
| 55 | rtc = open(*default_rtc, flags); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 56 | if (rtc >= 0) |
| 57 | return rtc; |
Mike Frysinger | 977bc6a | 2008-02-15 07:19:03 +0000 | [diff] [blame] | 58 | *default_rtc = "/dev/misc/rtc"; |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Mike Frysinger | 977bc6a | 2008-02-15 07:19:03 +0000 | [diff] [blame] | 61 | return xopen(*default_rtc, flags); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 64 | time_t FAST_FUNC rtc_read_time(int fd, int utc) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 65 | { |
| 66 | struct tm tm; |
| 67 | char *oldtz = 0; |
| 68 | time_t t = 0; |
| 69 | |
| 70 | memset(&tm, 0, sizeof(struct tm)); |
| 71 | xioctl(fd, RTC_RD_TIME, &tm); |
| 72 | tm.tm_isdst = -1; /* not known */ |
| 73 | |
| 74 | if (utc) { |
| 75 | oldtz = getenv("TZ"); |
| 76 | putenv((char*)"TZ=UTC0"); |
| 77 | tzset(); |
| 78 | } |
| 79 | |
| 80 | t = mktime(&tm); |
| 81 | |
| 82 | if (utc) { |
| 83 | unsetenv("TZ"); |
| 84 | if (oldtz) |
| 85 | putenv(oldtz - 3); |
| 86 | tzset(); |
| 87 | } |
| 88 | |
| 89 | return t; |
| 90 | } |