blob: c4117ba34f71902b74a4aee5e348969b8e330b38 [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
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 Vlasenkodefc1ea2008-06-27 02:52:20 +000016int FAST_FUNC rtc_adjtime_is_utc(void)
Mike Frysinger6b160e42008-02-15 02:27:19 +000017{
18 int utc = 0;
Denis Vlasenko5415c852008-07-21 23:05:26 +000019 FILE *f = fopen_for_read(ADJTIME_PATH);
Mike Frysinger6b160e42008-02-15 02:27:19 +000020
21 if (f) {
Denys Vlasenko695fa512010-01-06 18:16:39 +010022 char buffer[128];
Mike Frysinger6b160e42008-02-15 02:27:19 +000023
24 while (fgets(buffer, sizeof(buffer), f)) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010025 if (is_prefixed_with(buffer, "UTC")) {
Mike Frysinger6b160e42008-02-15 02:27:19 +000026 utc = 1;
27 break;
28 }
29 }
30 fclose(f);
Mike Frysinger6b160e42008-02-15 02:27:19 +000031 }
32
33 return utc;
34}
35
Denys Vlasenko528808f2014-05-02 09:05:03 +020036/* 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 */
41static 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 Vlasenkodefc1ea2008-06-27 02:52:20 +000063int FAST_FUNC rtc_xopen(const char **default_rtc, int flags)
Mike Frysinger6b160e42008-02-15 02:27:19 +000064{
65 int rtc;
Denys Vlasenko528808f2014-05-02 09:05:03 +020066 const char *name =
67 "/dev/rtc""\0"
68 "/dev/rtc0""\0"
69 "/dev/misc/rtc""\0";
Mike Frysinger6b160e42008-02-15 02:27:19 +000070
Denys Vlasenko528808f2014-05-02 09:05:03 +020071 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 Frysinger6b160e42008-02-15 02:27:19 +000077 if (rtc >= 0)
78 return rtc;
Denys Vlasenko528808f2014-05-02 09:05:03 +020079 if (!name[0])
80 return xopen(*default_rtc, flags);
81 try_name:
82 *default_rtc = name;
Denys Vlasenkod8290c22014-05-02 09:07:16 +020083 name += strlen(name) + 1;
Mike Frysinger6b160e42008-02-15 02:27:19 +000084 }
Mike Frysinger6b160e42008-02-15 02:27:19 +000085}
86
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010087void FAST_FUNC rtc_read_tm(struct tm *ptm, int fd)
Mike Frysinger6b160e42008-02-15 02:27:19 +000088{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010089 memset(ptm, 0, sizeof(*ptm));
90 xioctl(fd, RTC_RD_TIME, ptm);
91 ptm->tm_isdst = -1; /* "not known" */
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010092}
Mike Frysinger6b160e42008-02-15 02:27:19 +000093
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010094time_t FAST_FUNC rtc_tm2time(struct tm *ptm, int utc)
Denys Vlasenko5e3b1402010-01-06 22:43:39 +010095{
96 char *oldtz = oldtz; /* for compiler */
97 time_t t;
Mike Frysinger6b160e42008-02-15 02:27:19 +000098
99 if (utc) {
100 oldtz = getenv("TZ");
101 putenv((char*)"TZ=UTC0");
102 tzset();
103 }
104
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100105 t = mktime(ptm);
Mike Frysinger6b160e42008-02-15 02:27:19 +0000106
107 if (utc) {
108 unsetenv("TZ");
109 if (oldtz)
110 putenv(oldtz - 3);
111 tzset();
112 }
113
114 return t;
115}