]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/hwclock.c
login/sd-login.c: make use of _cleanup_free_ and friends
[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 165int hwclock_is_localtime(void) {
8e2f9ebf 166 FILE _cleanup_fclose_ *f;
bbc98d32
KS
167
168 /*
169 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
170 * # /etc/adjtime
171 * 0.0 0 0
172 * 0
173 * UTC
174 */
175 f = fopen("/etc/adjtime", "re");
176 if (f) {
177 char line[LINE_MAX];
178 bool b;
179
180 b = fgets(line, sizeof(line), f) &&
181 fgets(line, sizeof(line), f) &&
182 fgets(line, sizeof(line), f);
bbc98d32
KS
183 if (!b)
184 return -EIO;
185
186 truncate_nl(line);
8e2f9ebf 187 return streq(line, "LOCAL");
bbc98d32 188
bcb161b0 189 } else if (errno != ENOENT)
bbc98d32
KS
190 return -errno;
191
8e2f9ebf 192 return 0;
bbc98d32
KS
193}
194
72edcff5 195int hwclock_set_timezone(int *min) {
bbc98d32
KS
196 const struct timeval *tv_null = NULL;
197 struct timespec ts;
198 struct tm *tm;
19e65613 199 int minutesdelta;
bbc98d32
KS
200 struct timezone tz;
201
202 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
203 assert_se(tm = localtime(&ts.tv_sec));
19e65613 204 minutesdelta = tm->tm_gmtoff / 60;
bbc98d32 205
19e65613 206 tz.tz_minuteswest = -minutesdelta;
bbc98d32
KS
207 tz.tz_dsttime = 0; /* DST_NONE*/
208
209 /*
210 * If the hardware clock does not run in UTC, but in local time:
211 * The very first time we set the kernel's timezone, it will warp
212 * the clock so that it runs in UTC instead of local time.
213 */
214 if (settimeofday(tv_null, &tz) < 0)
215 return -errno;
216 if (min)
19e65613 217 *min = minutesdelta;
bbc98d32
KS
218 return 0;
219}
220
72edcff5 221int hwclock_reset_timezone(void) {
bbc98d32
KS
222 const struct timeval *tv_null = NULL;
223 struct timezone tz;
224
225 tz.tz_minuteswest = 0;
226 tz.tz_dsttime = 0; /* DST_NONE*/
227
72edcff5
KS
228 /*
229 * The very first time we set the kernel's timezone, it will warp
230 * the clock. Do a dummy call here, so the time warping is sealed
231 * and we set only the time zone with next call.
232 */
bbc98d32
KS
233 if (settimeofday(tv_null, &tz) < 0)
234 return -errno;
235
236 return 0;
237}