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 | |
Denys Vlasenko | 528808f | 2014-05-02 09:05:03 +0200 | [diff] [blame] | 36 | /* rtc opens are exclusive. |
| 37 | * Try to run two "hwclock -w" at the same time to see it. |
| 38 | * Users wouldn't expect that to fail merely because /dev/rtc |
| 39 | * was momentarily busy, let's try a bit harder on errno == EBUSY. |
| 40 | */ |
| 41 | static int open_loop_on_busy(const char *name, int flags) |
| 42 | { |
| 43 | int rtc; |
| 44 | /* |
| 45 | * Tested with two parallel "hwclock -w" loops. |
| 46 | * With try = 10, no failures with 2x1000000 loop iterations. |
| 47 | */ |
| 48 | int try = 1000 / 20; |
| 49 | again: |
| 50 | errno = 0; |
| 51 | rtc = open(name, flags); |
| 52 | if (errno == EBUSY) { |
| 53 | usleep(20 * 1000); |
| 54 | if (--try != 0) |
| 55 | goto again; |
| 56 | /* EBUSY. Last try, exit on error instead of returning -1 */ |
| 57 | return xopen(name, flags); |
| 58 | } |
| 59 | return rtc; |
| 60 | } |
| 61 | |
| 62 | /* Never fails */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 63 | int FAST_FUNC rtc_xopen(const char **default_rtc, int flags) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 64 | { |
| 65 | int rtc; |
Denys Vlasenko | 528808f | 2014-05-02 09:05:03 +0200 | [diff] [blame] | 66 | const char *name = |
| 67 | "/dev/rtc""\0" |
| 68 | "/dev/rtc0""\0" |
| 69 | "/dev/misc/rtc""\0"; |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 70 | |
Denys Vlasenko | 528808f | 2014-05-02 09:05:03 +0200 | [diff] [blame] | 71 | if (!*default_rtc) |
| 72 | goto try_name; |
| 73 | name = ""; /*else: we have rtc name, don't try other names */ |
| 74 | |
| 75 | for (;;) { |
| 76 | rtc = open_loop_on_busy(*default_rtc, flags); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 77 | if (rtc >= 0) |
| 78 | return rtc; |
Denys Vlasenko | 528808f | 2014-05-02 09:05:03 +0200 | [diff] [blame] | 79 | if (!name[0]) |
| 80 | return xopen(*default_rtc, flags); |
| 81 | try_name: |
| 82 | *default_rtc = name; |
Denys Vlasenko | d8290c2 | 2014-05-02 09:07:16 +0200 | [diff] [blame] | 83 | name += strlen(name) + 1; |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 84 | } |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 87 | void FAST_FUNC rtc_read_tm(struct tm *ptm, int fd) |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 88 | { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 89 | memset(ptm, 0, sizeof(*ptm)); |
| 90 | xioctl(fd, RTC_RD_TIME, ptm); |
| 91 | ptm->tm_isdst = -1; /* "not known" */ |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 92 | } |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 93 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 94 | time_t FAST_FUNC rtc_tm2time(struct tm *ptm, int utc) |
Denys Vlasenko | 5e3b140 | 2010-01-06 22:43:39 +0100 | [diff] [blame] | 95 | { |
| 96 | char *oldtz = oldtz; /* for compiler */ |
| 97 | time_t t; |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 98 | |
| 99 | if (utc) { |
| 100 | oldtz = getenv("TZ"); |
| 101 | putenv((char*)"TZ=UTC0"); |
| 102 | tzset(); |
| 103 | } |
| 104 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 105 | t = mktime(ptm); |
Mike Frysinger | 6b160e4 | 2008-02-15 02:27:19 +0000 | [diff] [blame] | 106 | |
| 107 | if (utc) { |
| 108 | unsetenv("TZ"); |
| 109 | if (oldtz) |
| 110 | putenv(oldtz - 3); |
| 111 | tzset(); |
| 112 | } |
| 113 | |
| 114 | return t; |
| 115 | } |