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>
45 static int rtc_open(int flags) {
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
54 fd = open("/dev/rtc", flags);
58 d = opendir("/sys/class/rtc");
64 struct dirent buf, *de;
67 r = readdir_r(d, &buf, &de);
74 if (ignore_file(de->d_name))
77 p = strjoin("/sys/class/rtc/", de->d_name, "/hctosys", NULL);
83 r = read_one_line_file(p, &v);
95 p = strappend("/dev/", de->d_name);
109 fd = open("/dev/rtc0", flags);
116 int hwclock_get_time(struct tm *tm) {
122 fd = rtc_open(O_RDONLY|O_CLOEXEC);
126 /* This leaves the timezone fields of struct tm
128 if (ioctl(fd, RTC_RD_TIME, tm) < 0)
131 /* We don't know daylight saving, so we reset this in order not
132 * to confused mktime(). */
135 close_nointr_nofail(fd);
140 int hwclock_set_time(const struct tm *tm) {
146 fd = rtc_open(O_RDONLY|O_CLOEXEC);
150 if (ioctl(fd, RTC_SET_TIME, tm) < 0)
153 close_nointr_nofail(fd);
158 int hwclock_is_localtime(void) {
163 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
169 f = fopen("/etc/adjtime", "re");
174 b = fgets(line, sizeof(line), f) &&
175 fgets(line, sizeof(line), f) &&
176 fgets(line, sizeof(line), f);
184 local = streq(line, "LOCAL");
186 } else if (errno != -ENOENT)
192 int hwclock_set_timezone(int *min) {
193 const struct timeval *tv_null = NULL;
199 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
200 assert_se(tm = localtime(&ts.tv_sec));
201 minuteswest = tm->tm_gmtoff / 60;
203 tz.tz_minuteswest = -minuteswest;
204 tz.tz_dsttime = 0; /* DST_NONE*/
207 * If the hardware clock does not run in UTC, but in local time:
208 * The very first time we set the kernel's timezone, it will warp
209 * the clock so that it runs in UTC instead of local time.
211 if (settimeofday(tv_null, &tz) < 0)
218 int hwclock_reset_timezone(void) {
219 const struct timeval *tv_null = NULL;
222 tz.tz_minuteswest = 0;
223 tz.tz_dsttime = 0; /* DST_NONE*/
226 * The very first time we set the kernel's timezone, it will warp
227 * the clock. Do a dummy call here, so the time warping is sealed
228 * and we set only the time zone with next call.
230 if (settimeofday(tv_null, &tz) < 0)