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