blob: 54b52f23a3c687e3dd8df3e71f4a3b3cfb609269 [file] [log] [blame]
Mike Frysinger6b160e42008-02-15 02:27:19 +00001/*
2 * Common RTC functions
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00003 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2, see file LICENSE in this source tree.
Mike Frysinger6b160e42008-02-15 02:27:19 +00005 */
6
7#include "libbb.h"
8#include "rtc_.h"
9
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000010int FAST_FUNC rtc_adjtime_is_utc(void)
Mike Frysinger6b160e42008-02-15 02:27:19 +000011{
12 int utc = 0;
Denis Vlasenko5415c852008-07-21 23:05:26 +000013 FILE *f = fopen_for_read(ADJTIME_PATH);
Mike Frysinger6b160e42008-02-15 02:27:19 +000014
15 if (f) {
Denys Vlasenko695fa512010-01-06 18:16:39 +010016 char buffer[128];
Mike Frysinger6b160e42008-02-15 02:27:19 +000017
18 while (fgets(buffer, sizeof(buffer), f)) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010019 if (is_prefixed_with(buffer, "UTC")) {
Mike Frysinger6b160e42008-02-15 02:27:19 +000020 utc = 1;
21 break;
22 }
23 }
24 fclose(f);
Mike Frysinger6b160e42008-02-15 02:27:19 +000025 }
26
27 return utc;
28}
29
Denys Vlasenko528808f2014-05-02 09:05:03 +020030/* 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 */
35static 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 Vlasenkodefc1ea2008-06-27 02:52:20 +000057int FAST_FUNC rtc_xopen(const char **default_rtc, int flags)
Mike Frysinger6b160e42008-02-15 02:27:19 +000058{
59 int rtc;
Denys Vlasenko528808f2014-05-02 09:05:03 +020060 const char *name =
61 "/dev/rtc""\0"
62 "/dev/rtc0""\0"
63 "/dev/misc/rtc""\0";
Mike Frysinger6b160e42008-02-15 02:27:19 +000064
Denys Vlasenko528808f2014-05-02 09:05:03 +020065 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 Frysinger6b160e42008-02-15 02:27:19 +000071 if (rtc >= 0)
72 return rtc;
Denys Vlasenko528808f2014-05-02 09:05:03 +020073 if (!name[0])
74 return xopen(*default_rtc, flags);
75 try_name:
76 *default_rtc = name;
Denys Vlasenkod8290c22014-05-02 09:07:16 +020077 name += strlen(name) + 1;
Mike Frysinger6b160e42008-02-15 02:27:19 +000078 }
Mike Frysinger6b160e42008-02-15 02:27:19 +000079}
80
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010081void FAST_FUNC rtc_read_tm(struct tm *ptm, int fd)
Mike Frysinger6b160e42008-02-15 02:27:19 +000082{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010083 memset(ptm, 0, sizeof(*ptm));
84 xioctl(fd, RTC_RD_TIME, ptm);
85 ptm->tm_isdst = -1; /* "not known" */
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010086}
Mike Frysinger6b160e42008-02-15 02:27:19 +000087
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010088time_t FAST_FUNC rtc_tm2time(struct tm *ptm, int utc)
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010089{
90 char *oldtz = oldtz; /* for compiler */
91 time_t t;
Mike Frysinger6b160e42008-02-15 02:27:19 +000092
93 if (utc) {
94 oldtz = getenv("TZ");
95 putenv((char*)"TZ=UTC0");
96 tzset();
97 }
98
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010099 t = mktime(ptm);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000100
101 if (utc) {
102 unsetenv("TZ");
103 if (oldtz)
104 putenv(oldtz - 3);
105 tzset();
106 }
107
108 return t;
109}