]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/clock-util.c
Move various files that don't need to be in basic/ to shared/
[thirdparty/systemd.git] / src / shared / clock-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
bbc98d32 2
bbc98d32 3#include <errno.h>
bbc98d32 4#include <fcntl.h>
11c3a366
TA
5#include <limits.h>
6#include <stdbool.h>
7#include <time.h>
07630cea
LP
8#include <linux/rtc.h>
9#include <stdio.h>
bbc98d32 10#include <sys/ioctl.h>
bbc98d32 11#include <sys/time.h>
bbc98d32 12
6d3db278 13#include "alloc-util.h"
3ffd4af2
LP
14#include "clock-util.h"
15#include "fd-util.h"
6d3db278 16#include "fileio.h"
bbc98d32 17#include "macro.h"
07630cea 18#include "string-util.h"
9c4615fb 19#include "util.h"
bbc98d32 20
60989612 21int clock_get_hwclock(struct tm *tm) {
8e64fd11 22 _cleanup_close_ int fd = -1;
bbc98d32
KS
23
24 assert(tm);
25
67fb4482 26 fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
bbc98d32
KS
27 if (fd < 0)
28 return -errno;
29
30 /* This leaves the timezone fields of struct tm
31 * uninitialized! */
32 if (ioctl(fd, RTC_RD_TIME, tm) < 0)
8e64fd11 33 return -errno;
bbc98d32
KS
34
35 /* We don't know daylight saving, so we reset this in order not
2f6a5907 36 * to confuse mktime(). */
bbc98d32
KS
37 tm->tm_isdst = -1;
38
8e64fd11 39 return 0;
bbc98d32
KS
40}
41
60989612 42int clock_set_hwclock(const struct tm *tm) {
8e64fd11 43 _cleanup_close_ int fd = -1;
bbc98d32
KS
44
45 assert(tm);
46
67fb4482 47 fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
bbc98d32
KS
48 if (fd < 0)
49 return -errno;
50
51 if (ioctl(fd, RTC_SET_TIME, tm) < 0)
8e64fd11 52 return -errno;
bbc98d32 53
8e64fd11 54 return 0;
bbc98d32 55}
a866073d 56
6369641d 57int clock_is_localtime(const char* adjtime_path) {
7fd1b19b 58 _cleanup_fclose_ FILE *f;
6d3db278 59 int r;
bbc98d32 60
234519ae 61 if (!adjtime_path)
6369641d
MP
62 adjtime_path = "/etc/adjtime";
63
bbc98d32
KS
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 */
6369641d 71 f = fopen(adjtime_path, "re");
bbc98d32 72 if (f) {
6d3db278
LP
73 _cleanup_free_ char *line = NULL;
74 unsigned i;
75
76 for (i = 0; i < 2; i++) { /* skip the first two lines */
77 r = read_line(f, LONG_LINE_MAX, NULL);
78 if (r < 0)
79 return r;
80 if (r == 0)
81 return false; /* less than three lines → default to UTC */
82 }
83
84 r = read_line(f, LONG_LINE_MAX, &line);
85 if (r < 0)
86 return r;
87 if (r == 0)
88 return false; /* less than three lines → default to UTC */
bbc98d32 89
8e2f9ebf 90 return streq(line, "LOCAL");
bbc98d32 91
bcb161b0 92 } else if (errno != ENOENT)
bbc98d32
KS
93 return -errno;
94
6d3db278
LP
95 /* adjtime not present → default to UTC */
96 return false;
bbc98d32
KS
97}
98
24efb112 99int clock_set_timezone(int *min) {
bbc98d32
KS
100 const struct timeval *tv_null = NULL;
101 struct timespec ts;
e0f691e1 102 struct tm tm;
19e65613 103 int minutesdelta;
bbc98d32
KS
104 struct timezone tz;
105
106 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
e0f691e1
YW
107 assert_se(localtime_r(&ts.tv_sec, &tm));
108 minutesdelta = tm.tm_gmtoff / 60;
bbc98d32 109
19e65613 110 tz.tz_minuteswest = -minutesdelta;
cc13b327 111 tz.tz_dsttime = 0; /* DST_NONE */
bbc98d32
KS
112
113 /*
c264aeab
KS
114 * If the RTC does not run in UTC but in local time, the very first
115 * call to settimeofday() will set the kernel's timezone and will warp the
116 * system clock, so that it runs in UTC instead of the local time we
117 * have read from the RTC.
bbc98d32
KS
118 */
119 if (settimeofday(tv_null, &tz) < 0)
9c4615fb
ZJS
120 return negative_errno();
121
bbc98d32 122 if (min)
19e65613 123 *min = minutesdelta;
bbc98d32
KS
124 return 0;
125}
126
c264aeab 127int clock_reset_timewarp(void) {
bbc98d32
KS
128 const struct timeval *tv_null = NULL;
129 struct timezone tz;
130
131 tz.tz_minuteswest = 0;
cc13b327 132 tz.tz_dsttime = 0; /* DST_NONE */
bbc98d32 133
72edcff5 134 /*
c264aeab
KS
135 * The very first call to settimeofday() does time warp magic. Do a
136 * dummy call here, so the time warping is sealed and all later calls
137 * behave as expected.
72edcff5 138 */
bbc98d32
KS
139 if (settimeofday(tv_null, &tz) < 0)
140 return -errno;
141
142 return 0;
143}
021dd87b
LP
144
145#define TIME_EPOCH_USEC ((usec_t) TIME_EPOCH * USEC_PER_SEC)
146
147int clock_apply_epoch(void) {
148 struct timespec ts;
149
150 if (now(CLOCK_REALTIME) >= TIME_EPOCH_USEC)
151 return 0;
152
153 if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, TIME_EPOCH_USEC)) < 0)
154 return -errno;
155
156 return 1;
157}