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