]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/clock-util.c
e2499099b607a76f6dcab736c286fae7210ec57f
[thirdparty/systemd.git] / src / basic / clock-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010-2012 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdbool.h>
25 #include <time.h>
26 #include <linux/rtc.h>
27 #include <stdio.h>
28 #include <sys/ioctl.h>
29 #include <sys/time.h>
30
31 #include "clock-util.h"
32 #include "fd-util.h"
33 #include "macro.h"
34 #include "string-util.h"
35 #include "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(const char* adjtime_path) {
74 _cleanup_fclose_ FILE *f;
75
76 if (!adjtime_path)
77 adjtime_path = "/etc/adjtime";
78
79 /*
80 * The third line of adjtime is "UTC" or "LOCAL" or nothing.
81 * # /etc/adjtime
82 * 0.0 0 0
83 * 0
84 * UTC
85 */
86 f = fopen(adjtime_path, "re");
87 if (f) {
88 char line[LINE_MAX];
89 bool b;
90
91 b = fgets(line, sizeof(line), f) &&
92 fgets(line, sizeof(line), f) &&
93 fgets(line, sizeof(line), f);
94 if (!b)
95 /* less than three lines -> default to UTC */
96 return 0;
97
98 truncate_nl(line);
99 return streq(line, "LOCAL");
100
101 } else if (errno != ENOENT)
102 return -errno;
103
104 /* adjtime not present -> default to UTC */
105 return 0;
106 }
107
108 int clock_set_timezone(int *min) {
109 const struct timeval *tv_null = NULL;
110 struct timespec ts;
111 struct tm *tm;
112 int minutesdelta;
113 struct timezone tz;
114
115 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
116 assert_se(tm = localtime(&ts.tv_sec));
117 minutesdelta = tm->tm_gmtoff / 60;
118
119 tz.tz_minuteswest = -minutesdelta;
120 tz.tz_dsttime = 0; /* DST_NONE */
121
122 /*
123 * If the RTC does not run in UTC but in local time, the very first
124 * call to settimeofday() will set the kernel's timezone and will warp the
125 * system clock, so that it runs in UTC instead of the local time we
126 * have read from the RTC.
127 */
128 if (settimeofday(tv_null, &tz) < 0)
129 return negative_errno();
130
131 if (min)
132 *min = minutesdelta;
133 return 0;
134 }
135
136 int clock_reset_timewarp(void) {
137 const struct timeval *tv_null = NULL;
138 struct timezone tz;
139
140 tz.tz_minuteswest = 0;
141 tz.tz_dsttime = 0; /* DST_NONE */
142
143 /*
144 * The very first call to settimeofday() does time warp magic. Do a
145 * dummy call here, so the time warping is sealed and all later calls
146 * behave as expected.
147 */
148 if (settimeofday(tv_null, &tz) < 0)
149 return -errno;
150
151 return 0;
152 }
153
154 #define TIME_EPOCH_USEC ((usec_t) TIME_EPOCH * USEC_PER_SEC)
155
156 int clock_apply_epoch(void) {
157 struct timespec ts;
158
159 if (now(CLOCK_REALTIME) >= TIME_EPOCH_USEC)
160 return 0;
161
162 if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, TIME_EPOCH_USEC)) < 0)
163 return -errno;
164
165 return 1;
166 }