1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010-2012 Lennart Poettering
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.
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.
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/>.
29 #include <sys/types.h>
32 #include <sys/ioctl.h>
35 #include <sys/prctl.h>
37 #include <linux/rtc.h>
46 static int rtc_open(int flags) {
51 * Some "chaotic platforms" have multiple RTCs and we need to
52 * find the "system RTC", which is in some setups /dev/rtc1.
54 * First, we try to find the RTC which has hctosys=1 set. If we
55 * don't find any we just take the first RTC that exists at all,
56 * then try to open /dev/rtc0.
58 d = opendir("/sys/class/rtc");
65 union dirent_storage buf;
68 r = readdir_r(d, &buf.de, &de);
75 if (ignore_file(de->d_name))
78 p = strjoin("/sys/class/rtc/", de->d_name, "/hctosys", NULL);
84 r = read_one_line_file(p, &v);
96 p = strappend("/dev/", de->d_name);
115 fd = open("/dev/rtc0", flags);
122 int hwclock_get_time(struct tm *tm) {
128 fd = rtc_open(O_RDONLY|O_CLOEXEC);
132 /* This leaves the timezone fields of struct tm
134 if (ioctl(fd, RTC_RD_TIME, tm) < 0)
137 /* We don't know daylight saving, so we reset this in order not
138 * to confused mktime(). */
141 close_nointr_nofail(fd);
146 int hwclock_set_time(const struct tm *tm) {
152 fd = rtc_open(O_RDONLY|O_CLOEXEC);
156 if (ioctl(fd, RTC_SET_TIME, tm) < 0)
159 close_nointr_nofail(fd);
164 int hwclock_is_localtime(void) {
165 _cleanup_fclose_ FILE *f;
168 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
174 f = fopen("/etc/adjtime", "re");
179 b = fgets(line, sizeof(line), f) &&
180 fgets(line, sizeof(line), f) &&
181 fgets(line, sizeof(line), f);
186 return streq(line, "LOCAL");
188 } else if (errno != ENOENT)
194 int hwclock_set_timezone(int *min) {
195 const struct timeval *tv_null = NULL;
201 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
202 assert_se(tm = localtime(&ts.tv_sec));
203 minutesdelta = tm->tm_gmtoff / 60;
205 tz.tz_minuteswest = -minutesdelta;
206 tz.tz_dsttime = 0; /* DST_NONE*/
209 * If the hardware clock does not run in UTC, but in local time:
210 * The very first time we set the kernel's timezone, it will warp
211 * the clock so that it runs in UTC instead of local time.
213 if (settimeofday(tv_null, &tz) < 0)
220 int hwclock_reset_timezone(void) {
221 const struct timeval *tv_null = NULL;
224 tz.tz_minuteswest = 0;
225 tz.tz_dsttime = 0; /* DST_NONE*/
228 * The very first time we set the kernel's timezone, it will warp
229 * the clock. Do a dummy call here, so the time warping is sealed
230 * and we set only the time zone with next call.
232 if (settimeofday(tv_null, &tz) < 0)