]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/timesync/timesyncd.c
Add SPDX license identifiers to source files under the LGPL
[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 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include "sd-daemon.h"
22 #include "sd-event.h"
23
24 #include "capability-util.h"
25 #include "clock-util.h"
26 #include "fd-util.h"
27 #include "fs-util.h"
28 #include "mkdir.h"
29 #include "network-util.h"
30 #include "process-util.h"
31 #include "signal-util.h"
32 #include "timesyncd-conf.h"
33 #include "timesyncd-manager.h"
34 #include "user-util.h"
35
36 static int load_clock_timestamp(uid_t uid, gid_t gid) {
37 _cleanup_close_ int fd = -1;
38 usec_t min = TIME_EPOCH * USEC_PER_SEC;
39 usec_t ct;
40 int r;
41
42 /* Let's try to make sure that the clock is always
43 * monotonically increasing, by saving the clock whenever we
44 * have a new NTP time, or when we shut down, and restoring it
45 * when we start again. This is particularly helpful on
46 * systems lacking a battery backed RTC. We also will adjust
47 * the time to at least the build time of systemd. */
48
49 fd = open("/var/lib/systemd/timesync/clock", O_RDWR|O_CLOEXEC, 0644);
50 if (fd >= 0) {
51 struct stat st;
52 usec_t stamp;
53
54 /* check if the recorded time is later than the compiled-in one */
55 r = fstat(fd, &st);
56 if (r >= 0) {
57 stamp = timespec_load(&st.st_mtim);
58 if (stamp > min)
59 min = stamp;
60 }
61
62 if (geteuid() == 0) {
63 /* Try to fix the access mode, so that we can still
64 touch the file after dropping priviliges */
65 r = fchmod(fd, 0644);
66 if (r < 0)
67 return log_error_errno(errno, "Failed to change file access mode: %m");
68 r = fchown(fd, uid, gid);
69 return log_error_errno(errno, "Failed to change file owner: %m");
70 }
71
72 } else {
73 r = mkdir_safe_label("/var/lib/systemd/timesync", 0755, uid, gid, true);
74 if (r < 0)
75 return log_error_errno(r, "Failed to create state directory: %m");
76
77 /* create stamp file with the compiled-in date */
78 (void) touch_file("/var/lib/systemd/timesync/clock", false, min, uid, gid, 0644);
79 }
80
81 ct = now(CLOCK_REALTIME);
82 if (ct < min) {
83 struct timespec ts;
84 char date[FORMAT_TIMESTAMP_MAX];
85
86 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
87 format_timestamp(date, sizeof(date), min));
88
89 if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0)
90 log_error_errno(errno, "Failed to restore system clock: %m");
91 }
92
93 return 0;
94 }
95
96 int main(int argc, char *argv[]) {
97 _cleanup_(manager_freep) Manager *m = NULL;
98 const char *user = "systemd-timesync";
99 uid_t uid;
100 gid_t gid;
101 int r;
102
103 log_set_target(LOG_TARGET_AUTO);
104 log_set_facility(LOG_CRON);
105 log_parse_environment();
106 log_open();
107
108 umask(0022);
109
110 if (argc != 1) {
111 log_error("This program does not take arguments.");
112 r = -EINVAL;
113 goto finish;
114 }
115
116 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
117 if (r < 0) {
118 log_error_errno(r, "Cannot resolve user name %s: %m", user);
119 goto finish;
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 (geteuid() == 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 if (clock_is_localtime(NULL) > 0) {
143 log_info("The system is configured to read the RTC time in the local time zone. "
144 "This mode can not be fully supported. All system time to RTC updates are disabled.");
145 m->rtc_local_time = true;
146 }
147
148 r = manager_parse_config_file(m);
149 if (r < 0)
150 log_warning_errno(r, "Failed to parse configuration file: %m");
151
152 r = manager_parse_fallback_string(m, NTP_SERVERS);
153 if (r < 0) {
154 log_error_errno(r, "Failed to parse fallback server strings: %m");
155 goto finish;
156 }
157
158 log_debug("systemd-timesyncd running as pid " PID_FMT, getpid_cached());
159 sd_notify(false,
160 "READY=1\n"
161 "STATUS=Daemon is running");
162
163 if (network_is_online()) {
164 r = manager_connect(m);
165 if (r < 0)
166 goto finish;
167 }
168
169 r = sd_event_loop(m->event);
170 if (r < 0) {
171 log_error_errno(r, "Failed to run event loop: %m");
172 goto finish;
173 }
174
175 /* if we got an authoritative time, store it in the file system */
176 if (m->sync)
177 (void) touch("/var/lib/systemd/timesync/clock");
178
179 sd_event_get_exit_code(m->event, &r);
180
181 finish:
182 sd_notify(false,
183 "STOPPING=1\n"
184 "STATUS=Shutting down...");
185
186 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
187 }