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