]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/timesync/timesyncd.c
units: make use of !! ExecStart= prefix in systemd-timesyncd.service
[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 "network-util.h"
28 #include "process-util.h"
29 #include "signal-util.h"
30 #include "timesyncd-conf.h"
31 #include "timesyncd-manager.h"
32 #include "user-util.h"
33
34 static int load_clock_timestamp(uid_t uid, gid_t gid) {
35 _cleanup_close_ int fd = -1;
36 usec_t min = TIME_EPOCH * USEC_PER_SEC;
37 usec_t ct;
38 int r;
39
40 /* Let's try to make sure that the clock is always
41 * monotonically increasing, by saving the clock whenever we
42 * have a new NTP time, or when we shut down, and restoring it
43 * when we start again. This is particularly helpful on
44 * systems lacking a battery backed RTC. We also will adjust
45 * the time to at least the build time of systemd. */
46
47 fd = open("/var/lib/systemd/clock", O_RDWR|O_CLOEXEC, 0644);
48 if (fd >= 0) {
49 struct stat st;
50 usec_t stamp;
51
52 /* check if the recorded time is later than the compiled-in one */
53 r = fstat(fd, &st);
54 if (r >= 0) {
55 stamp = timespec_load(&st.st_mtim);
56 if (stamp > min)
57 min = stamp;
58 }
59
60 /* Try to fix the access mode, so that we can still
61 touch the file after dropping priviliges */
62 (void) fchmod(fd, 0644);
63 (void) fchown(fd, uid, gid);
64
65 } else
66 /* create stamp file with the compiled-in date */
67 (void) touch_file("/var/lib/systemd/clock", true, min, uid, gid, 0644);
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;
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 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
105 if (r < 0) {
106 log_error_errno(r, "Cannot resolve user name %s: %m", user);
107 goto finish;
108 }
109
110 r = load_clock_timestamp(uid, gid);
111 if (r < 0)
112 goto finish;
113
114 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all
115 * privileges are already dropped. */
116 if (geteuid() == 0) {
117 r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME));
118 if (r < 0)
119 goto finish;
120 }
121
122 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
123
124 r = manager_new(&m);
125 if (r < 0) {
126 log_error_errno(r, "Failed to allocate manager: %m");
127 goto finish;
128 }
129
130 if (clock_is_localtime(NULL) > 0) {
131 log_info("The system is configured to read the RTC time in the local time zone. "
132 "This mode can not be fully supported. All system time to RTC updates are disabled.");
133 m->rtc_local_time = true;
134 }
135
136 r = manager_parse_config_file(m);
137 if (r < 0)
138 log_warning_errno(r, "Failed to parse configuration file: %m");
139
140 r = manager_parse_fallback_string(m, NTP_SERVERS);
141 if (r < 0) {
142 log_error_errno(r, "Failed to parse fallback server strings: %m");
143 goto finish;
144 }
145
146 log_debug("systemd-timesyncd running as pid " PID_FMT, getpid_cached());
147 sd_notify(false,
148 "READY=1\n"
149 "STATUS=Daemon is running");
150
151 if (network_is_online()) {
152 r = manager_connect(m);
153 if (r < 0)
154 goto finish;
155 }
156
157 r = sd_event_loop(m->event);
158 if (r < 0) {
159 log_error_errno(r, "Failed to run event loop: %m");
160 goto finish;
161 }
162
163 /* if we got an authoritative time, store it in the file system */
164 if (m->sync)
165 (void) touch("/var/lib/systemd/clock");
166
167 sd_event_get_exit_code(m->event, &r);
168
169 finish:
170 sd_notify(false,
171 "STOPPING=1\n"
172 "STATUS=Shutting down...");
173
174 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
175 }