]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/timesync/timesyncd.c
Merge pull request #9832 from yuwata/fix-9831
[thirdparty/systemd.git] / src / timesync / timesyncd.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "sd-daemon.h"
4 #include "sd-event.h"
5
6 #include "capability-util.h"
7 #include "clock-util.h"
8 #include "fd-util.h"
9 #include "fs-util.h"
10 #include "mkdir.h"
11 #include "network-util.h"
12 #include "process-util.h"
13 #include "signal-util.h"
14 #include "timesyncd-bus.h"
15 #include "timesyncd-conf.h"
16 #include "timesyncd-manager.h"
17 #include "user-util.h"
18
19 #define STATE_DIR "/var/lib/systemd/timesync"
20 #define CLOCK_FILE STATE_DIR "/clock"
21
22 static int load_clock_timestamp(uid_t uid, gid_t gid) {
23 _cleanup_close_ int fd = -1;
24 usec_t min = TIME_EPOCH * USEC_PER_SEC;
25 usec_t ct;
26 int r;
27
28 /* Let's try to make sure that the clock is always
29 * monotonically increasing, by saving the clock whenever we
30 * have a new NTP time, or when we shut down, and restoring it
31 * when we start again. This is particularly helpful on
32 * systems lacking a battery backed RTC. We also will adjust
33 * the time to at least the build time of systemd. */
34
35 fd = open(CLOCK_FILE, O_RDWR|O_CLOEXEC, 0644);
36 if (fd >= 0) {
37 struct stat st;
38 usec_t stamp;
39
40 /* check if the recorded time is later than the compiled-in one */
41 r = fstat(fd, &st);
42 if (r >= 0) {
43 stamp = timespec_load(&st.st_mtim);
44 if (stamp > min)
45 min = stamp;
46 }
47
48 if (geteuid() == 0) {
49 /* Try to fix the access mode, so that we can still
50 touch the file after dropping priviliges */
51 r = fchmod_and_chown(fd, 0644, uid, gid);
52 if (r < 0)
53 log_warning_errno(r, "Failed to chmod or chown %s, ignoring: %m", CLOCK_FILE);
54 }
55
56 } else {
57 r = mkdir_safe_label(STATE_DIR, 0755, uid, gid,
58 MKDIR_FOLLOW_SYMLINK | MKDIR_WARN_MODE);
59 if (r < 0) {
60 log_debug_errno(r, "Failed to create state directory, ignoring: %m");
61 goto settime;
62 }
63
64 /* create stamp file with the compiled-in date */
65 r = touch_file(CLOCK_FILE, false, min, uid, gid, 0644);
66 if (r < 0)
67 log_debug_errno(r, "Failed to create %s, ignoring: %m", CLOCK_FILE);
68 }
69
70 settime:
71 ct = now(CLOCK_REALTIME);
72 if (ct < min) {
73 struct timespec ts;
74 char date[FORMAT_TIMESTAMP_MAX];
75
76 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
77 format_timestamp(date, sizeof(date), min));
78
79 if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0)
80 log_error_errno(errno, "Failed to restore system clock, ignoring: %m");
81 }
82
83 return 0;
84 }
85
86 int main(int argc, char *argv[]) {
87 _cleanup_(manager_freep) Manager *m = NULL;
88 const char *user = "systemd-timesync";
89 uid_t uid, uid_current;
90 gid_t gid;
91 int r;
92
93 log_set_target(LOG_TARGET_AUTO);
94 log_set_facility(LOG_CRON);
95 log_parse_environment();
96 log_open();
97
98 umask(0022);
99
100 if (argc != 1) {
101 log_error("This program does not take arguments.");
102 r = -EINVAL;
103 goto finish;
104 }
105
106 uid = uid_current = geteuid();
107 gid = getegid();
108
109 if (uid_current == 0) {
110 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
111 if (r < 0) {
112 log_error_errno(r, "Cannot resolve user name %s: %m", user);
113 goto finish;
114 }
115 }
116
117 r = load_clock_timestamp(uid, gid);
118 if (r < 0)
119 goto finish;
120
121 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all
122 * privileges are already dropped. */
123 if (uid_current == 0) {
124 r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME));
125 if (r < 0)
126 goto finish;
127 }
128
129 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
130
131 r = manager_new(&m);
132 if (r < 0) {
133 log_error_errno(r, "Failed to allocate manager: %m");
134 goto finish;
135 }
136
137 r = manager_connect_bus(m);
138 if (r < 0) {
139 log_error_errno(r, "Could not connect to bus: %m");
140 goto finish;
141 }
142
143 if (clock_is_localtime(NULL) > 0) {
144 log_info("The system is configured to read the RTC time in the local time zone. "
145 "This mode cannot be fully supported. All system time to RTC updates are disabled.");
146 m->rtc_local_time = true;
147 }
148
149 r = manager_parse_config_file(m);
150 if (r < 0)
151 log_warning_errno(r, "Failed to parse configuration file: %m");
152
153 r = manager_parse_fallback_string(m, NTP_SERVERS);
154 if (r < 0) {
155 log_error_errno(r, "Failed to parse fallback server strings: %m");
156 goto finish;
157 }
158
159 log_debug("systemd-timesyncd running as pid " PID_FMT, getpid_cached());
160 sd_notify(false,
161 "READY=1\n"
162 "STATUS=Daemon is running");
163
164 if (network_is_online()) {
165 r = manager_connect(m);
166 if (r < 0)
167 goto finish;
168 }
169
170 r = sd_event_loop(m->event);
171 if (r < 0) {
172 log_error_errno(r, "Failed to run event loop: %m");
173 goto finish;
174 }
175
176 /* if we got an authoritative time, store it in the file system */
177 if (m->sync) {
178 r = touch(CLOCK_FILE);
179 if (r < 0)
180 log_debug_errno(r, "Failed to touch %s, ignoring: %m", CLOCK_FILE);
181 }
182
183 sd_event_get_exit_code(m->event, &r);
184
185 finish:
186 sd_notify(false,
187 "STOPPING=1\n"
188 "STATUS=Shutting down...");
189
190 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
191 }