]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/hwclock.c
errno is positive
[thirdparty/systemd.git] / src / shared / hwclock.c
CommitLineData
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"
a5c32cff 44#include "fileio.h"
bbc98d32
KS
45
46static int rtc_open(int flags) {
47 int fd;
48 DIR *d;
49
50 /* First, we try to make use of the /dev/rtc symlink. If that
51 * doesn't exist, we open the first RTC which has hctosys=1
52 * set. If we don't find any we just take the first RTC that
53 * exists at all. */
54
55 fd = open("/dev/rtc", flags);
56 if (fd >= 0)
57 return fd;
58
59 d = opendir("/sys/class/rtc");
60 if (!d)
61 goto fallback;
62
63 for (;;) {
64 char *p, *v;
7d5e9c0f
LP
65 struct dirent *de;
66 union dirent_storage buf;
bbc98d32
KS
67 int r;
68
7d5e9c0f 69 r = readdir_r(d, &buf.de, &de);
bbc98d32
KS
70 if (r != 0)
71 goto fallback;
72
73 if (!de)
74 goto fallback;
75
76 if (ignore_file(de->d_name))
77 continue;
78
b7def684 79 p = strjoin("/sys/class/rtc/", de->d_name, "/hctosys", NULL);
bbc98d32
KS
80 if (!p) {
81 closedir(d);
82 return -ENOMEM;
83 }
84
85 r = read_one_line_file(p, &v);
86 free(p);
87
88 if (r < 0)
89 continue;
90
91 r = parse_boolean(v);
92 free(v);
93
94 if (r <= 0)
95 continue;
96
97 p = strappend("/dev/", de->d_name);
4eeebf70
LP
98 if (!p) {
99 closedir(d);
100 return -ENOMEM;
101 }
102
bbc98d32
KS
103 fd = open(p, flags);
104 free(p);
105
106 if (fd >= 0) {
107 closedir(d);
108 return fd;
109 }
110 }
111
112fallback:
113 if (d)
114 closedir(d);
115
116 fd = open("/dev/rtc0", flags);
117 if (fd < 0)
118 return -errno;
119
120 return fd;
121}
122
123int hwclock_get_time(struct tm *tm) {
124 int fd;
125 int err = 0;
126
127 assert(tm);
128
129 fd = rtc_open(O_RDONLY|O_CLOEXEC);
130 if (fd < 0)
131 return -errno;
132
133 /* This leaves the timezone fields of struct tm
134 * uninitialized! */
135 if (ioctl(fd, RTC_RD_TIME, tm) < 0)
136 err = -errno;
137
138 /* We don't know daylight saving, so we reset this in order not
139 * to confused mktime(). */
140 tm->tm_isdst = -1;
141
142 close_nointr_nofail(fd);
143
144 return err;
145}
146
147int hwclock_set_time(const struct tm *tm) {
148 int fd;
149 int err = 0;
150
151 assert(tm);
152
153 fd = rtc_open(O_RDONLY|O_CLOEXEC);
154 if (fd < 0)
155 return -errno;
156
157 if (ioctl(fd, RTC_SET_TIME, tm) < 0)
158 err = -errno;
159
160 close_nointr_nofail(fd);
161
162 return err;
163}
a866073d 164
bbc98d32
KS
165int hwclock_is_localtime(void) {
166 FILE *f;
167 bool local = false;
168
169 /*
170 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
171 * # /etc/adjtime
172 * 0.0 0 0
173 * 0
174 * UTC
175 */
176 f = fopen("/etc/adjtime", "re");
177 if (f) {
178 char line[LINE_MAX];
179 bool b;
180
181 b = fgets(line, sizeof(line), f) &&
182 fgets(line, sizeof(line), f) &&
183 fgets(line, sizeof(line), f);
184
185 fclose(f);
186
187 if (!b)
188 return -EIO;
189
190 truncate_nl(line);
191 local = streq(line, "LOCAL");
192
bcb161b0 193 } else if (errno != ENOENT)
bbc98d32
KS
194 return -errno;
195
196 return local;
197}
198
72edcff5 199int hwclock_set_timezone(int *min) {
bbc98d32
KS
200 const struct timeval *tv_null = NULL;
201 struct timespec ts;
202 struct tm *tm;
19e65613 203 int minutesdelta;
bbc98d32
KS
204 struct timezone tz;
205
206 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
207 assert_se(tm = localtime(&ts.tv_sec));
19e65613 208 minutesdelta = tm->tm_gmtoff / 60;
bbc98d32 209
19e65613 210 tz.tz_minuteswest = -minutesdelta;
bbc98d32
KS
211 tz.tz_dsttime = 0; /* DST_NONE*/
212
213 /*
214 * If the hardware clock does not run in UTC, but in local time:
215 * The very first time we set the kernel's timezone, it will warp
216 * the clock so that it runs in UTC instead of local time.
217 */
218 if (settimeofday(tv_null, &tz) < 0)
219 return -errno;
220 if (min)
19e65613 221 *min = minutesdelta;
bbc98d32
KS
222 return 0;
223}
224
72edcff5 225int hwclock_reset_timezone(void) {
bbc98d32
KS
226 const struct timeval *tv_null = NULL;
227 struct timezone tz;
228
229 tz.tz_minuteswest = 0;
230 tz.tz_dsttime = 0; /* DST_NONE*/
231
72edcff5
KS
232 /*
233 * The very first time we set the kernel's timezone, it will warp
234 * the clock. Do a dummy call here, so the time warping is sealed
235 * and we set only the time zone with next call.
236 */
bbc98d32
KS
237 if (settimeofday(tv_null, &tz) < 0)
238 return -errno;
239
240 return 0;
241}