]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/timesync/timesyncd.c
Merge pull request #4512 from pfl/ndisc_exponential_backoff
[thirdparty/systemd.git] / src / timesync / timesyncd.c
CommitLineData
bcdbbd7e
KS
1/***
2 This file is part of systemd.
3
e8af6973 4 Copyright 2014 Kay Sievers, Lennart Poettering
bcdbbd7e
KS
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
687ed123 20#include "sd-daemon.h"
3ffd4af2
LP
21#include "sd-event.h"
22
430f0182 23#include "capability-util.h"
84e51726 24#include "clock-util.h"
3ffd4af2 25#include "fd-util.h"
f4f15635 26#include "fs-util.h"
84e51726 27#include "network-util.h"
24882e06 28#include "signal-util.h"
84e51726 29#include "timesyncd-conf.h"
3ffd4af2 30#include "timesyncd-manager.h"
b1d4f8e1 31#include "user-util.h"
d636d376
KS
32
33static int load_clock_timestamp(uid_t uid, gid_t gid) {
ece6e766 34 _cleanup_close_ int fd = -1;
d636d376
KS
35 usec_t min = TIME_EPOCH * USEC_PER_SEC;
36 usec_t ct;
37 int r;
ece6e766
LP
38
39 /* Let's try to make sure that the clock is always
40 * monotonically increasing, by saving the clock whenever we
41 * have a new NTP time, or when we shut down, and restoring it
42 * when we start again. This is particularly helpful on
43 * systems lacking a battery backed RTC. We also will adjust
44 * the time to at least the build time of systemd. */
45
d636d376
KS
46 fd = open("/var/lib/systemd/clock", O_RDWR|O_CLOEXEC, 0644);
47 if (fd >= 0) {
ece6e766 48 struct stat st;
d636d376
KS
49 usec_t stamp;
50
51 /* check if the recorded time is later than the compiled-in one */
52 r = fstat(fd, &st);
53 if (r >= 0) {
54 stamp = timespec_load(&st.st_mtim);
55 if (stamp > min)
56 min = stamp;
ece6e766
LP
57 }
58
d636d376
KS
59 /* Try to fix the access mode, so that we can still
60 touch the file after dropping priviliges */
ac5b0c13
LP
61 (void) fchmod(fd, 0644);
62 (void) fchown(fd, uid, gid);
d636d376
KS
63
64 } else
65 /* create stamp file with the compiled-in date */
ac5b0c13 66 (void) touch_file("/var/lib/systemd/clock", true, min, uid, gid, 0644);
ece6e766
LP
67
68 ct = now(CLOCK_REALTIME);
d636d376 69 if (ct < min) {
ece6e766 70 struct timespec ts;
d636d376 71 char date[FORMAT_TIMESTAMP_MAX];
ece6e766 72
d636d376
KS
73 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
74 format_timestamp(date, sizeof(date), min));
ece6e766 75
d636d376 76 if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0)
56f64d95 77 log_error_errno(errno, "Failed to restore system clock: %m");
ece6e766
LP
78 }
79
80 return 0;
81}
82
687ed123 83int main(int argc, char *argv[]) {
84e51726 84 _cleanup_(manager_freep) Manager *m = NULL;
cedc8c44 85 const char *user = "systemd-timesync";
ece6e766
LP
86 uid_t uid;
87 gid_t gid;
687ed123
KS
88 int r;
89
90 log_set_target(LOG_TARGET_AUTO);
e8af6973 91 log_set_facility(LOG_CRON);
687ed123
KS
92 log_parse_environment();
93 log_open();
94
e8af6973
LP
95 umask(0022);
96
84e51726
LP
97 if (argc != 1) {
98 log_error("This program does not take arguments.");
99 r = -EINVAL;
100 goto finish;
101 }
102
ece6e766
LP
103 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
104 if (r < 0) {
da927ba9 105 log_error_errno(r, "Cannot resolve user name %s: %m", user);
84e51726 106 goto finish;
ece6e766
LP
107 }
108
d636d376 109 r = load_clock_timestamp(uid, gid);
ece6e766 110 if (r < 0)
84e51726 111 goto finish;
ece6e766 112
966bff26 113 r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME));
a349eb10 114 if (r < 0)
84e51726 115 goto finish;
a349eb10 116
72c0a2c2 117 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
856a5a7d 118
687ed123 119 r = manager_new(&m);
856a5a7d 120 if (r < 0) {
da927ba9 121 log_error_errno(r, "Failed to allocate manager: %m");
84e51726 122 goto finish;
856a5a7d 123 }
687ed123 124
6369641d 125 if (clock_is_localtime(NULL) > 0) {
c264aeab
KS
126 log_info("The system is configured to read the RTC time in the local time zone. "
127 "This mode can not be fully supported. All system time to RTC updates are disabled.");
128 m->rtc_local_time = true;
129 }
130
84e51726
LP
131 r = manager_parse_config_file(m);
132 if (r < 0)
da927ba9 133 log_warning_errno(r, "Failed to parse configuration file: %m");
e0e5ce23 134
3745770a
MB
135 assert_se(manager_parse_fallback_string(m, NTP_SERVERS) >= 0);
136
aa36007c 137 log_debug("systemd-timesyncd running as pid " PID_FMT, getpid());
af4ec430
LP
138 sd_notify(false,
139 "READY=1\n"
140 "STATUS=Daemon is running");
39594d49 141
e0e5ce23
TG
142 if (network_is_online()) {
143 r = manager_connect(m);
144 if (r < 0)
84e51726 145 goto finish;
e0e5ce23 146 }
678522cf 147
687ed123 148 r = sd_event_loop(m->event);
856a5a7d 149 if (r < 0) {
da927ba9 150 log_error_errno(r, "Failed to run event loop: %m");
84e51726 151 goto finish;
856a5a7d
LP
152 }
153
d636d376
KS
154 /* if we got an authoritative time, store it in the file system */
155 if (m->sync)
ac5b0c13 156 (void) touch("/var/lib/systemd/clock");
687ed123 157
84e51726
LP
158 sd_event_get_exit_code(m->event, &r);
159
160finish:
af4ec430
LP
161 sd_notify(false,
162 "STOPPING=1\n"
163 "STATUS=Shutting down...");
e8af6973 164
687ed123 165 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
bcdbbd7e 166}