]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/timesync/timesyncd.c
grypt-util: drop two emacs modelines
[thirdparty/systemd.git] / src / timesync / timesyncd.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2014 Kay Sievers, Lennart Poettering
4 ***/
5
6 #include "sd-daemon.h"
7 #include "sd-event.h"
8
9 #include "capability-util.h"
10 #include "clock-util.h"
11 #include "fd-util.h"
12 #include "fs-util.h"
13 #include "mkdir.h"
14 #include "network-util.h"
15 #include "process-util.h"
16 #include "signal-util.h"
17 #include "timesyncd-bus.h"
18 #include "timesyncd-conf.h"
19 #include "timesyncd-manager.h"
20 #include "user-util.h"
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("/var/lib/systemd/timesync/clock", 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(fd, 0644);
52 if (r < 0)
53 return log_error_errno(errno, "Failed to change file access mode: %m");
54 r = fchown(fd, uid, gid);
55 if (r < 0)
56 return log_error_errno(errno, "Failed to change file owner: %m");
57 }
58
59 } else {
60 r = mkdir_safe_label("/var/lib/systemd/timesync", 0755, uid, gid,
61 MKDIR_FOLLOW_SYMLINK | MKDIR_WARN_MODE);
62 if (r < 0)
63 return log_error_errno(r, "Failed to create state directory: %m");
64
65 /* create stamp file with the compiled-in date */
66 (void) touch_file("/var/lib/systemd/timesync/clock", false, min, uid, gid, 0644);
67 }
68
69 ct = now(CLOCK_REALTIME);
70 if (ct < min) {
71 struct timespec ts;
72 char date[FORMAT_TIMESTAMP_MAX];
73
74 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
75 format_timestamp(date, sizeof(date), min));
76
77 if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0)
78 log_error_errno(errno, "Failed to restore system clock: %m");
79 }
80
81 return 0;
82 }
83
84 int main(int argc, char *argv[]) {
85 _cleanup_(manager_freep) Manager *m = NULL;
86 const char *user = "systemd-timesync";
87 uid_t uid, uid_current;
88 gid_t gid;
89 int r;
90
91 log_set_target(LOG_TARGET_AUTO);
92 log_set_facility(LOG_CRON);
93 log_parse_environment();
94 log_open();
95
96 umask(0022);
97
98 if (argc != 1) {
99 log_error("This program does not take arguments.");
100 r = -EINVAL;
101 goto finish;
102 }
103
104 uid = uid_current = geteuid();
105 gid = getegid();
106
107 if (uid_current == 0) {
108 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
109 if (r < 0) {
110 log_error_errno(r, "Cannot resolve user name %s: %m", user);
111 goto finish;
112 }
113 }
114
115 r = load_clock_timestamp(uid, gid);
116 if (r < 0)
117 goto finish;
118
119 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all
120 * privileges are already dropped. */
121 if (uid_current == 0) {
122 r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME));
123 if (r < 0)
124 goto finish;
125 }
126
127 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
128
129 r = manager_new(&m);
130 if (r < 0) {
131 log_error_errno(r, "Failed to allocate manager: %m");
132 goto finish;
133 }
134
135 r = manager_connect_bus(m);
136 if (r < 0) {
137 log_error_errno(r, "Could not connect to bus: %m");
138 goto finish;
139 }
140
141 if (clock_is_localtime(NULL) > 0) {
142 log_info("The system is configured to read the RTC time in the local time zone. "
143 "This mode cannot be fully supported. All system time to RTC updates are disabled.");
144 m->rtc_local_time = true;
145 }
146
147 r = manager_parse_config_file(m);
148 if (r < 0)
149 log_warning_errno(r, "Failed to parse configuration file: %m");
150
151 r = manager_parse_fallback_string(m, NTP_SERVERS);
152 if (r < 0) {
153 log_error_errno(r, "Failed to parse fallback server strings: %m");
154 goto finish;
155 }
156
157 log_debug("systemd-timesyncd running as pid " PID_FMT, getpid_cached());
158 sd_notify(false,
159 "READY=1\n"
160 "STATUS=Daemon is running");
161
162 if (network_is_online()) {
163 r = manager_connect(m);
164 if (r < 0)
165 goto finish;
166 }
167
168 r = sd_event_loop(m->event);
169 if (r < 0) {
170 log_error_errno(r, "Failed to run event loop: %m");
171 goto finish;
172 }
173
174 /* if we got an authoritative time, store it in the file system */
175 if (m->sync)
176 (void) touch("/var/lib/systemd/timesync/clock");
177
178 sd_event_get_exit_code(m->event, &r);
179
180 finish:
181 sd_notify(false,
182 "STOPPING=1\n"
183 "STATUS=Shutting down...");
184
185 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
186 }