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