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