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