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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 4 | * Licensed under GPLv2, see file LICENSE in this source tree. |
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) { |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 22 | char buffer[128]; |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 23 | |
| 24 | while (fgets(buffer, sizeof(buffer), f)) { |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 25 | if (strncmp(buffer, "UTC", 3) == 0) { |
| 26 | utc = 1; |
| 27 | break; |
| 28 | } |
| 29 | } |
| 30 | fclose(f); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | return utc; |
| 34 | } |
| 35 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 36 | int FAST_FUNC rtc_xopen(const char **default_rtc, int flags) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 37 | { |
| 38 | int rtc; |
| 39 | |
Mike Frysinger | 977bc6a | 2008-02-15 07:19:03 +0000 | [diff] [blame] | 40 | if (!*default_rtc) { |
| 41 | *default_rtc = "/dev/rtc"; |
| 42 | rtc = open(*default_rtc, flags); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 43 | if (rtc >= 0) |
| 44 | return rtc; |
Mike Frysinger | 977bc6a | 2008-02-15 07:19:03 +0000 | [diff] [blame] | 45 | *default_rtc = "/dev/rtc0"; |
| 46 | rtc = open(*default_rtc, flags); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 47 | if (rtc >= 0) |
| 48 | return rtc; |
Mike Frysinger | 977bc6a | 2008-02-15 07:19:03 +0000 | [diff] [blame] | 49 | *default_rtc = "/dev/misc/rtc"; |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Mike Frysinger | 977bc6a | 2008-02-15 07:19:03 +0000 | [diff] [blame] | 52 | return xopen(*default_rtc, flags); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 55 | void FAST_FUNC rtc_read_tm(struct tm *ptm, int fd) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 56 | { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 57 | memset(ptm, 0, sizeof(*ptm)); |
| 58 | xioctl(fd, RTC_RD_TIME, ptm); |
| 59 | ptm->tm_isdst = -1; /* "not known" */ |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 60 | } |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 61 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 62 | time_t FAST_FUNC rtc_tm2time(struct tm *ptm, int utc) |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 63 | { |
| 64 | char *oldtz = oldtz; /* for compiler */ |
| 65 | time_t t; |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 66 | |
| 67 | if (utc) { |
| 68 | oldtz = getenv("TZ"); |
| 69 | putenv((char*)"TZ=UTC0"); |
| 70 | tzset(); |
| 71 | } |
| 72 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 73 | t = mktime(ptm); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 74 | |
| 75 | if (utc) { |
| 76 | unsetenv("TZ"); |
| 77 | if (oldtz) |
| 78 | putenv(oldtz - 3); |
| 79 | tzset(); |
| 80 | } |
| 81 | |
| 82 | return t; |
| 83 | } |