]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/clock-util.c
00f549c023fc9b640aabba36519f4740b398632e
[thirdparty/systemd.git] / src / basic / clock-util.c
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 <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdbool.h>
26 #include <time.h>
27 #include <linux/rtc.h>
28 #include <stdio.h>
29 #include <sys/ioctl.h>
30 #include <sys/time.h>
31
32 #include "clock-util.h"
33 #include "fd-util.h"
34 #include "macro.h"
35 #include "string-util.h"
36
37 int clock_get_hwclock(struct tm *tm) {
38 _cleanup_close_ int fd = -1;
39
40 assert(tm);
41
42 fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
43 if (fd < 0)
44 return -errno;
45
46 /* This leaves the timezone fields of struct tm
47 * uninitialized! */
48 if (ioctl(fd, RTC_RD_TIME, tm) < 0)
49 return -errno;
50
51 /* We don't know daylight saving, so we reset this in order not
52 * to confuse mktime(). */
53 tm->tm_isdst = -1;
54
55 return 0;
56 }
57
58 int clock_set_hwclock(const struct tm *tm) {
59 _cleanup_close_ int fd = -1;
60
61 assert(tm);
62
63 fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
64 if (fd < 0)
65 return -errno;
66
67 if (ioctl(fd, RTC_SET_TIME, tm) < 0)
68 return -errno;
69
70 return 0;
71 }
72
73 int clock_is_localtime(void) {
74 _cleanup_fclose_ FILE *f;
75
76 /*
77 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
78 * # /etc/adjtime
79 * 0.0 0 0
80 * 0
81 * UTC
82 */
83 f = fopen("/etc/adjtime", "re");
84 if (f) {
85 char line[LINE_MAX];
86 bool b;
87
88 b = fgets(line, sizeof(line), f) &&
89 fgets(line, sizeof(line), f) &&
90 fgets(line, sizeof(line), f);
91 if (!b)
92 return -EIO;
93
94 truncate_nl(line);
95 return streq(line, "LOCAL");
96
97 } else if (errno != ENOENT)
98 return -errno;
99
100 return 0;
101 }
102
103 int clock_set_timezone(int *min) {
104 const struct timeval *tv_null = NULL;
105 struct timespec ts;
106 struct tm *tm;
107 int minutesdelta;
108 struct timezone tz;
109
110 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
111 assert_se(tm = localtime(&ts.tv_sec));
112 minutesdelta = tm->tm_gmtoff / 60;
113
114 tz.tz_minuteswest = -minutesdelta;
115 tz.tz_dsttime = 0; /* DST_NONE */
116
117 /*
118 * If the RTC does not run in UTC but in local time, the very first
119 * call to settimeofday() will set the kernel's timezone and will warp the
120 * system clock, so that it runs in UTC instead of the local time we
121 * have read from the RTC.
122 */
123 if (settimeofday(tv_null, &tz) < 0)
124 return -errno;
125 if (min)
126 *min = minutesdelta;
127 return 0;
128 }
129
130 int clock_reset_timewarp(void) {
131 const struct timeval *tv_null = NULL;
132 struct timezone tz;
133
134 tz.tz_minuteswest = 0;
135 tz.tz_dsttime = 0; /* DST_NONE */
136
137 /*
138 * The very first call to settimeofday() does time warp magic. Do a
139 * dummy call here, so the time warping is sealed and all later calls
140 * behave as expected.
141 */
142 if (settimeofday(tv_null, &tz) < 0)
143 return -errno;
144
145 return 0;
146 }