]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/timesync/timesyncd.c
Merge pull request #7256 from keszybz/add-cii-badge
[thirdparty/systemd.git] / src / timesync / timesyncd.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Kay Sievers, Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "sd-daemon.h"
21 #include "sd-event.h"
22
23 #include "capability-util.h"
24 #include "clock-util.h"
25 #include "fd-util.h"
26 #include "fs-util.h"
27 #include "mkdir.h"
28 #include "network-util.h"
29 #include "process-util.h"
30 #include "signal-util.h"
31 #include "timesyncd-conf.h"
32 #include "timesyncd-manager.h"
33 #include "user-util.h"
34
35 static int load_clock_timestamp(uid_t uid, gid_t gid) {
36 _cleanup_close_ int fd = -1;
37 usec_t min = TIME_EPOCH * USEC_PER_SEC;
38 usec_t ct;
39 int r;
40
41 /* Let's try to make sure that the clock is always
42 * monotonically increasing, by saving the clock whenever we
43 * have a new NTP time, or when we shut down, and restoring it
44 * when we start again. This is particularly helpful on
45 * systems lacking a battery backed RTC. We also will adjust
46 * the time to at least the build time of systemd. */
47
48 fd = open("/var/lib/systemd/timesync/clock", O_RDWR|O_CLOEXEC, 0644);
49 if (fd >= 0) {
50 struct stat st;
51 usec_t stamp;
52
53 /* check if the recorded time is later than the compiled-in one */
54 r = fstat(fd, &st);
55 if (r >= 0) {
56 stamp = timespec_load(&st.st_mtim);
57 if (stamp > min)
58 min = stamp;
59 }
60
61 if (geteuid() == 0) {
62 /* Try to fix the access mode, so that we can still
63 touch the file after dropping priviliges */
64 r = fchmod(fd, 0644);
65 if (r < 0)
66 return log_error_errno(errno, "Failed to change file access mode: %m");
67 r = fchown(fd, uid, gid);
68 return log_error_errno(errno, "Failed to change file owner: %m");
69 }
70
71 } else {
72 r = mkdir_safe_label("/var/lib/systemd/timesync", 0755, uid, gid, true);
73 if (r < 0)
74 return log_error_errno(r, "Failed to create state directory: %m");
75
76 /* create stamp file with the compiled-in date */
77 (void) touch_file("/var/lib/systemd/timesync/clock", false, min, uid, gid, 0644);
78 }
79
80 ct = now(CLOCK_REALTIME);
81 if (ct < min) {
82 struct timespec ts;
83 char date[FORMAT_TIMESTAMP_MAX];
84
85 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
86 format_timestamp(date, sizeof(date), min));
87
88 if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0)
89 log_error_errno(errno, "Failed to restore system clock: %m");
90 }
91
92 return 0;
93 }
94
95 int main(int argc, char *argv[]) {
96 _cleanup_(manager_freep) Manager *m = NULL;
97 const char *user = "systemd-timesync";
98 uid_t uid;
99 gid_t gid;
100 int r;
101
102 log_set_target(LOG_TARGET_AUTO);
103 log_set_facility(LOG_CRON);
104 log_parse_environment();
105 log_open();
106
107 umask(0022);
108
109 if (argc != 1) {
110 log_error("This program does not take arguments.");
111 r = -EINVAL;
112 goto finish;
113 }
114
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 r = load_clock_timestamp(uid, gid);
122 if (r < 0)
123 goto finish;
124
125 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all
126 * privileges are already dropped. */
127 if (geteuid() == 0) {
128 r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME));
129 if (r < 0)
130 goto finish;
131 }
132
133 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
134
135 r = manager_new(&m);
136 if (r < 0) {
137 log_error_errno(r, "Failed to allocate manager: %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 can not 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 }