]>
Commit | Line | Data |
---|---|---|
bbc98d32 KS |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010-2012 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <assert.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <errno.h> | |
26 | #include <stdlib.h> | |
27 | #include <signal.h> | |
28 | #include <stdio.h> | |
29 | #include <sys/types.h> | |
30 | #include <sys/stat.h> | |
31 | #include <fcntl.h> | |
32 | #include <sys/ioctl.h> | |
33 | #include <stdarg.h> | |
34 | #include <ctype.h> | |
35 | #include <sys/prctl.h> | |
36 | #include <sys/time.h> | |
37 | #include <linux/rtc.h> | |
38 | ||
39 | #include "macro.h" | |
40 | #include "util.h" | |
41 | #include "log.h" | |
42 | #include "strv.h" | |
43 | #include "hwclock.h" | |
44 | ||
45 | static int rtc_open(int flags) { | |
46 | int fd; | |
47 | DIR *d; | |
48 | ||
49 | /* First, we try to make use of the /dev/rtc symlink. If that | |
50 | * doesn't exist, we open the first RTC which has hctosys=1 | |
51 | * set. If we don't find any we just take the first RTC that | |
52 | * exists at all. */ | |
53 | ||
54 | fd = open("/dev/rtc", flags); | |
55 | if (fd >= 0) | |
56 | return fd; | |
57 | ||
58 | d = opendir("/sys/class/rtc"); | |
59 | if (!d) | |
60 | goto fallback; | |
61 | ||
62 | for (;;) { | |
63 | char *p, *v; | |
64 | struct dirent buf, *de; | |
65 | int r; | |
66 | ||
67 | r = readdir_r(d, &buf, &de); | |
68 | if (r != 0) | |
69 | goto fallback; | |
70 | ||
71 | if (!de) | |
72 | goto fallback; | |
73 | ||
74 | if (ignore_file(de->d_name)) | |
75 | continue; | |
76 | ||
77 | p = join("/sys/class/rtc/", de->d_name, "/hctosys", NULL); | |
78 | if (!p) { | |
79 | closedir(d); | |
80 | return -ENOMEM; | |
81 | } | |
82 | ||
83 | r = read_one_line_file(p, &v); | |
84 | free(p); | |
85 | ||
86 | if (r < 0) | |
87 | continue; | |
88 | ||
89 | r = parse_boolean(v); | |
90 | free(v); | |
91 | ||
92 | if (r <= 0) | |
93 | continue; | |
94 | ||
95 | p = strappend("/dev/", de->d_name); | |
96 | fd = open(p, flags); | |
97 | free(p); | |
98 | ||
99 | if (fd >= 0) { | |
100 | closedir(d); | |
101 | return fd; | |
102 | } | |
103 | } | |
104 | ||
105 | fallback: | |
106 | if (d) | |
107 | closedir(d); | |
108 | ||
109 | fd = open("/dev/rtc0", flags); | |
110 | if (fd < 0) | |
111 | return -errno; | |
112 | ||
113 | return fd; | |
114 | } | |
115 | ||
116 | int hwclock_get_time(struct tm *tm) { | |
117 | int fd; | |
118 | int err = 0; | |
119 | ||
120 | assert(tm); | |
121 | ||
122 | fd = rtc_open(O_RDONLY|O_CLOEXEC); | |
123 | if (fd < 0) | |
124 | return -errno; | |
125 | ||
126 | /* This leaves the timezone fields of struct tm | |
127 | * uninitialized! */ | |
128 | if (ioctl(fd, RTC_RD_TIME, tm) < 0) | |
129 | err = -errno; | |
130 | ||
131 | /* We don't know daylight saving, so we reset this in order not | |
132 | * to confused mktime(). */ | |
133 | tm->tm_isdst = -1; | |
134 | ||
135 | close_nointr_nofail(fd); | |
136 | ||
137 | return err; | |
138 | } | |
139 | ||
140 | int hwclock_set_time(const struct tm *tm) { | |
141 | int fd; | |
142 | int err = 0; | |
143 | ||
144 | assert(tm); | |
145 | ||
146 | fd = rtc_open(O_RDONLY|O_CLOEXEC); | |
147 | if (fd < 0) | |
148 | return -errno; | |
149 | ||
150 | if (ioctl(fd, RTC_SET_TIME, tm) < 0) | |
151 | err = -errno; | |
152 | ||
153 | close_nointr_nofail(fd); | |
154 | ||
155 | return err; | |
156 | } | |
157 | int hwclock_is_localtime(void) { | |
158 | FILE *f; | |
159 | bool local = false; | |
160 | ||
161 | /* | |
162 | * The third line of adjtime is "UTC" or "LOCAL" or nothing. | |
163 | * # /etc/adjtime | |
164 | * 0.0 0 0 | |
165 | * 0 | |
166 | * UTC | |
167 | */ | |
168 | f = fopen("/etc/adjtime", "re"); | |
169 | if (f) { | |
170 | char line[LINE_MAX]; | |
171 | bool b; | |
172 | ||
173 | b = fgets(line, sizeof(line), f) && | |
174 | fgets(line, sizeof(line), f) && | |
175 | fgets(line, sizeof(line), f); | |
176 | ||
177 | fclose(f); | |
178 | ||
179 | if (!b) | |
180 | return -EIO; | |
181 | ||
182 | truncate_nl(line); | |
183 | local = streq(line, "LOCAL"); | |
184 | ||
185 | } else if (errno != -ENOENT) | |
186 | return -errno; | |
187 | ||
188 | return local; | |
189 | } | |
190 | ||
191 | int hwclock_apply_localtime_delta(int *min) { | |
192 | const struct timeval *tv_null = NULL; | |
193 | struct timespec ts; | |
194 | struct tm *tm; | |
195 | int minuteswest; | |
196 | struct timezone tz; | |
197 | ||
198 | assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0); | |
199 | assert_se(tm = localtime(&ts.tv_sec)); | |
200 | minuteswest = tm->tm_gmtoff / 60; | |
201 | ||
202 | tz.tz_minuteswest = -minuteswest; | |
203 | tz.tz_dsttime = 0; /* DST_NONE*/ | |
204 | ||
205 | /* | |
206 | * If the hardware clock does not run in UTC, but in local time: | |
207 | * The very first time we set the kernel's timezone, it will warp | |
208 | * the clock so that it runs in UTC instead of local time. | |
209 | */ | |
210 | if (settimeofday(tv_null, &tz) < 0) | |
211 | return -errno; | |
212 | if (min) | |
213 | *min = minuteswest; | |
214 | return 0; | |
215 | } | |
216 | ||
217 | int hwclock_reset_localtime_delta(void) { | |
218 | const struct timeval *tv_null = NULL; | |
219 | struct timezone tz; | |
220 | ||
221 | tz.tz_minuteswest = 0; | |
222 | tz.tz_dsttime = 0; /* DST_NONE*/ | |
223 | ||
224 | if (settimeofday(tv_null, &tz) < 0) | |
225 | return -errno; | |
226 | ||
227 | return 0; | |
228 | } |