]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
bbc98d32 | 2 | |
bbc98d32 | 3 | #include <errno.h> |
bbc98d32 | 4 | #include <fcntl.h> |
11c3a366 TA |
5 | #include <limits.h> |
6 | #include <stdbool.h> | |
7 | #include <time.h> | |
07630cea LP |
8 | #include <linux/rtc.h> |
9 | #include <stdio.h> | |
bbc98d32 | 10 | #include <sys/ioctl.h> |
bbc98d32 | 11 | #include <sys/time.h> |
bbc98d32 | 12 | |
6d3db278 | 13 | #include "alloc-util.h" |
3ffd4af2 | 14 | #include "clock-util.h" |
2b2fec7d | 15 | #include "errno-util.h" |
3ffd4af2 | 16 | #include "fd-util.h" |
6d3db278 | 17 | #include "fileio.h" |
bbc98d32 | 18 | #include "macro.h" |
07630cea | 19 | #include "string-util.h" |
bbc98d32 | 20 | |
60989612 | 21 | int clock_get_hwclock(struct tm *tm) { |
8e64fd11 | 22 | _cleanup_close_ int fd = -1; |
bbc98d32 KS |
23 | |
24 | assert(tm); | |
25 | ||
67fb4482 | 26 | fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC); |
bbc98d32 KS |
27 | if (fd < 0) |
28 | return -errno; | |
29 | ||
30 | /* This leaves the timezone fields of struct tm | |
31 | * uninitialized! */ | |
32 | if (ioctl(fd, RTC_RD_TIME, tm) < 0) | |
8e64fd11 | 33 | return -errno; |
bbc98d32 KS |
34 | |
35 | /* We don't know daylight saving, so we reset this in order not | |
2f6a5907 | 36 | * to confuse mktime(). */ |
bbc98d32 KS |
37 | tm->tm_isdst = -1; |
38 | ||
8e64fd11 | 39 | return 0; |
bbc98d32 KS |
40 | } |
41 | ||
60989612 | 42 | int clock_set_hwclock(const struct tm *tm) { |
8e64fd11 | 43 | _cleanup_close_ int fd = -1; |
bbc98d32 KS |
44 | |
45 | assert(tm); | |
46 | ||
67fb4482 | 47 | fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC); |
bbc98d32 KS |
48 | if (fd < 0) |
49 | return -errno; | |
50 | ||
7c248223 | 51 | return RET_NERRNO(ioctl(fd, RTC_SET_TIME, tm)); |
bbc98d32 | 52 | } |
a866073d | 53 | |
6369641d | 54 | int clock_is_localtime(const char* adjtime_path) { |
c2b2df60 | 55 | _cleanup_fclose_ FILE *f = NULL; |
6d3db278 | 56 | int r; |
bbc98d32 | 57 | |
234519ae | 58 | if (!adjtime_path) |
6369641d MP |
59 | adjtime_path = "/etc/adjtime"; |
60 | ||
bbc98d32 KS |
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 | */ | |
6369641d | 68 | f = fopen(adjtime_path, "re"); |
bbc98d32 | 69 | if (f) { |
6d3db278 LP |
70 | _cleanup_free_ char *line = NULL; |
71 | unsigned i; | |
72 | ||
73 | for (i = 0; i < 2; i++) { /* skip the first two lines */ | |
74 | r = read_line(f, LONG_LINE_MAX, NULL); | |
75 | if (r < 0) | |
76 | return r; | |
77 | if (r == 0) | |
78 | return false; /* less than three lines → default to UTC */ | |
79 | } | |
80 | ||
81 | r = read_line(f, LONG_LINE_MAX, &line); | |
82 | if (r < 0) | |
83 | return r; | |
84 | if (r == 0) | |
85 | return false; /* less than three lines → default to UTC */ | |
bbc98d32 | 86 | |
8e2f9ebf | 87 | return streq(line, "LOCAL"); |
bbc98d32 | 88 | |
bcb161b0 | 89 | } else if (errno != ENOENT) |
bbc98d32 KS |
90 | return -errno; |
91 | ||
6d3db278 LP |
92 | /* adjtime not present → default to UTC */ |
93 | return false; | |
bbc98d32 KS |
94 | } |
95 | ||
505061bb | 96 | int clock_set_timezone(int *ret_minutesdelta) { |
bbc98d32 | 97 | struct timespec ts; |
e0f691e1 | 98 | struct tm tm; |
19e65613 | 99 | int minutesdelta; |
bbc98d32 KS |
100 | struct timezone tz; |
101 | ||
102 | assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0); | |
e0f691e1 YW |
103 | assert_se(localtime_r(&ts.tv_sec, &tm)); |
104 | minutesdelta = tm.tm_gmtoff / 60; | |
bbc98d32 | 105 | |
505061bb LP |
106 | tz = (struct timezone) { |
107 | .tz_minuteswest = -minutesdelta, | |
108 | .tz_dsttime = 0, /* DST_NONE */ | |
109 | }; | |
bbc98d32 | 110 | |
505061bb LP |
111 | /* If the RTC does not run in UTC but in local time, the very first call to settimeofday() will set |
112 | * the kernel's timezone and will warp the system clock, so that it runs in UTC instead of the local | |
113 | * time we have read from the RTC. */ | |
114 | if (settimeofday(NULL, &tz) < 0) | |
115 | return -errno; | |
116 | ||
117 | if (ret_minutesdelta) | |
118 | *ret_minutesdelta = minutesdelta; | |
9c4615fb | 119 | |
bbc98d32 KS |
120 | return 0; |
121 | } | |
122 | ||
c264aeab | 123 | int clock_reset_timewarp(void) { |
505061bb LP |
124 | static const struct timezone tz = { |
125 | .tz_minuteswest = 0, | |
126 | .tz_dsttime = 0, /* DST_NONE */ | |
127 | }; | |
128 | ||
129 | /* The very first call to settimeofday() does time warp magic. Do a dummy call here, so the time | |
130 | * warping is sealed and all later calls behave as expected. */ | |
7c248223 | 131 | return RET_NERRNO(settimeofday(NULL, &tz)); |
bbc98d32 | 132 | } |
021dd87b | 133 | |
5170afbc | 134 | #define EPOCH_FILE "/usr/lib/clock-epoch" |
021dd87b | 135 | |
b10abe4b | 136 | int clock_apply_epoch(ClockChangeDirection *ret_attempted_change) { |
5170afbc | 137 | struct stat st; |
021dd87b | 138 | struct timespec ts; |
b10abe4b EI |
139 | usec_t epoch_usec, now_usec; |
140 | ||
141 | /* NB: we update *ret_attempted_change in *all* cases, both | |
142 | * on success and failure, to indicate what we intended to do! */ | |
143 | ||
144 | assert(ret_attempted_change); | |
021dd87b | 145 | |
5170afbc DM |
146 | if (stat(EPOCH_FILE, &st) < 0) { |
147 | if (errno != ENOENT) | |
0d18259e | 148 | log_warning_errno(errno, "Cannot stat " EPOCH_FILE ": %m"); |
5170afbc | 149 | |
0d18259e | 150 | epoch_usec = (usec_t) TIME_EPOCH * USEC_PER_SEC; |
5170afbc DM |
151 | } else |
152 | epoch_usec = timespec_load(&st.st_mtim); | |
153 | ||
b10abe4b EI |
154 | now_usec = now(CLOCK_REALTIME); |
155 | if (now_usec < epoch_usec) | |
156 | *ret_attempted_change = CLOCK_CHANGE_FORWARD; | |
157 | else if (now_usec > usec_add(epoch_usec, CLOCK_VALID_RANGE_USEC_MAX)) | |
158 | *ret_attempted_change = CLOCK_CHANGE_BACKWARD; | |
159 | else { | |
160 | *ret_attempted_change = CLOCK_CHANGE_NOOP; | |
021dd87b | 161 | return 0; |
b10abe4b | 162 | } |
021dd87b | 163 | |
5170afbc | 164 | if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, epoch_usec)) < 0) |
021dd87b LP |
165 | return -errno; |
166 | ||
167 | return 1; | |
168 | } |