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