]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <fcntl.h> | |
5 | #include <limits.h> | |
6 | #include <stdbool.h> | |
7 | #include <time.h> | |
8 | #include <linux/rtc.h> | |
9 | #include <stdio.h> | |
10 | #include <sys/ioctl.h> | |
11 | #include <sys/time.h> | |
12 | ||
13 | #include "clock-util.h" | |
14 | #include "fd-util.h" | |
15 | #include "macro.h" | |
16 | #include "string-util.h" | |
17 | #include "util.h" | |
18 | ||
19 | int clock_get_hwclock(struct tm *tm) { | |
20 | _cleanup_close_ int fd = -1; | |
21 | ||
22 | assert(tm); | |
23 | ||
24 | fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC); | |
25 | if (fd < 0) | |
26 | return -errno; | |
27 | ||
28 | /* This leaves the timezone fields of struct tm | |
29 | * uninitialized! */ | |
30 | if (ioctl(fd, RTC_RD_TIME, tm) < 0) | |
31 | return -errno; | |
32 | ||
33 | /* We don't know daylight saving, so we reset this in order not | |
34 | * to confuse mktime(). */ | |
35 | tm->tm_isdst = -1; | |
36 | ||
37 | return 0; | |
38 | } | |
39 | ||
40 | int clock_set_hwclock(const struct tm *tm) { | |
41 | _cleanup_close_ int fd = -1; | |
42 | ||
43 | assert(tm); | |
44 | ||
45 | fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC); | |
46 | if (fd < 0) | |
47 | return -errno; | |
48 | ||
49 | if (ioctl(fd, RTC_SET_TIME, tm) < 0) | |
50 | return -errno; | |
51 | ||
52 | return 0; | |
53 | } | |
54 | ||
55 | int clock_is_localtime(const char* adjtime_path) { | |
56 | _cleanup_fclose_ FILE *f; | |
57 | ||
58 | if (!adjtime_path) | |
59 | adjtime_path = "/etc/adjtime"; | |
60 | ||
61 | /* | |
62 | * The third line of adjtime is "UTC" or "LOCAL" or nothing. | |
63 | * # /etc/adjtime | |
64 | * 0.0 0 0 | |
65 | * 0 | |
66 | * UTC | |
67 | */ | |
68 | f = fopen(adjtime_path, "re"); | |
69 | if (f) { | |
70 | char line[LINE_MAX]; | |
71 | bool b; | |
72 | ||
73 | b = fgets(line, sizeof(line), f) && | |
74 | fgets(line, sizeof(line), f) && | |
75 | fgets(line, sizeof(line), f); | |
76 | if (!b) | |
77 | /* less than three lines -> default to UTC */ | |
78 | return 0; | |
79 | ||
80 | truncate_nl(line); | |
81 | return streq(line, "LOCAL"); | |
82 | ||
83 | } else if (errno != ENOENT) | |
84 | return -errno; | |
85 | ||
86 | /* adjtime not present -> default to UTC */ | |
87 | return 0; | |
88 | } | |
89 | ||
90 | int clock_set_timezone(int *min) { | |
91 | const struct timeval *tv_null = NULL; | |
92 | struct timespec ts; | |
93 | struct tm tm; | |
94 | int minutesdelta; | |
95 | struct timezone tz; | |
96 | ||
97 | assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0); | |
98 | assert_se(localtime_r(&ts.tv_sec, &tm)); | |
99 | minutesdelta = tm.tm_gmtoff / 60; | |
100 | ||
101 | tz.tz_minuteswest = -minutesdelta; | |
102 | tz.tz_dsttime = 0; /* DST_NONE */ | |
103 | ||
104 | /* | |
105 | * If the RTC does not run in UTC but in local time, the very first | |
106 | * call to settimeofday() will set the kernel's timezone and will warp the | |
107 | * system clock, so that it runs in UTC instead of the local time we | |
108 | * have read from the RTC. | |
109 | */ | |
110 | if (settimeofday(tv_null, &tz) < 0) | |
111 | return negative_errno(); | |
112 | ||
113 | if (min) | |
114 | *min = minutesdelta; | |
115 | return 0; | |
116 | } | |
117 | ||
118 | int clock_reset_timewarp(void) { | |
119 | const struct timeval *tv_null = NULL; | |
120 | struct timezone tz; | |
121 | ||
122 | tz.tz_minuteswest = 0; | |
123 | tz.tz_dsttime = 0; /* DST_NONE */ | |
124 | ||
125 | /* | |
126 | * The very first call to settimeofday() does time warp magic. Do a | |
127 | * dummy call here, so the time warping is sealed and all later calls | |
128 | * behave as expected. | |
129 | */ | |
130 | if (settimeofday(tv_null, &tz) < 0) | |
131 | return -errno; | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
136 | #define TIME_EPOCH_USEC ((usec_t) TIME_EPOCH * USEC_PER_SEC) | |
137 | ||
138 | int clock_apply_epoch(void) { | |
139 | struct timespec ts; | |
140 | ||
141 | if (now(CLOCK_REALTIME) >= TIME_EPOCH_USEC) | |
142 | return 0; | |
143 | ||
144 | if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, TIME_EPOCH_USEC)) < 0) | |
145 | return -errno; | |
146 | ||
147 | return 1; | |
148 | } |