]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
bcdbbd7e | 2 | /*** |
0c697941 | 3 | Copyright 2014 Kay Sievers |
bcdbbd7e KS |
4 | ***/ |
5 | ||
687ed123 | 6 | #include "sd-daemon.h" |
3ffd4af2 LP |
7 | #include "sd-event.h" |
8 | ||
430f0182 | 9 | #include "capability-util.h" |
84e51726 | 10 | #include "clock-util.h" |
3ffd4af2 | 11 | #include "fd-util.h" |
f4f15635 | 12 | #include "fs-util.h" |
53d133ea | 13 | #include "mkdir.h" |
84e51726 | 14 | #include "network-util.h" |
df0ff127 | 15 | #include "process-util.h" |
24882e06 | 16 | #include "signal-util.h" |
e7dd3947 | 17 | #include "timesyncd-bus.h" |
84e51726 | 18 | #include "timesyncd-conf.h" |
3ffd4af2 | 19 | #include "timesyncd-manager.h" |
b1d4f8e1 | 20 | #include "user-util.h" |
d636d376 KS |
21 | |
22 | static int load_clock_timestamp(uid_t uid, gid_t gid) { | |
ece6e766 | 23 | _cleanup_close_ int fd = -1; |
d636d376 KS |
24 | usec_t min = TIME_EPOCH * USEC_PER_SEC; |
25 | usec_t ct; | |
26 | int r; | |
ece6e766 LP |
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 | ||
53d133ea | 35 | fd = open("/var/lib/systemd/timesync/clock", O_RDWR|O_CLOEXEC, 0644); |
d636d376 | 36 | if (fd >= 0) { |
ece6e766 | 37 | struct stat st; |
d636d376 KS |
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; | |
ece6e766 LP |
46 | } |
47 | ||
53d133ea YW |
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); | |
d1c2774b | 55 | if (r < 0) |
53d133ea YW |
56 | return log_error_errno(errno, "Failed to change file owner: %m"); |
57 | } | |
58 | ||
59 | } else { | |
37c1d5e9 ZJS |
60 | r = mkdir_safe_label("/var/lib/systemd/timesync", 0755, uid, gid, |
61 | MKDIR_FOLLOW_SYMLINK | MKDIR_WARN_MODE); | |
53d133ea YW |
62 | if (r < 0) |
63 | return log_error_errno(r, "Failed to create state directory: %m"); | |
d636d376 | 64 | |
d636d376 | 65 | /* create stamp file with the compiled-in date */ |
53d133ea YW |
66 | (void) touch_file("/var/lib/systemd/timesync/clock", false, min, uid, gid, 0644); |
67 | } | |
ece6e766 LP |
68 | |
69 | ct = now(CLOCK_REALTIME); | |
d636d376 | 70 | if (ct < min) { |
ece6e766 | 71 | struct timespec ts; |
d636d376 | 72 | char date[FORMAT_TIMESTAMP_MAX]; |
ece6e766 | 73 | |
d636d376 KS |
74 | log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s", |
75 | format_timestamp(date, sizeof(date), min)); | |
ece6e766 | 76 | |
d636d376 | 77 | if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0) |
56f64d95 | 78 | log_error_errno(errno, "Failed to restore system clock: %m"); |
ece6e766 LP |
79 | } |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
687ed123 | 84 | int main(int argc, char *argv[]) { |
84e51726 | 85 | _cleanup_(manager_freep) Manager *m = NULL; |
cedc8c44 | 86 | const char *user = "systemd-timesync"; |
444c1915 | 87 | uid_t uid, uid_current; |
ece6e766 | 88 | gid_t gid; |
687ed123 KS |
89 | int r; |
90 | ||
91 | log_set_target(LOG_TARGET_AUTO); | |
e8af6973 | 92 | log_set_facility(LOG_CRON); |
687ed123 KS |
93 | log_parse_environment(); |
94 | log_open(); | |
95 | ||
e8af6973 LP |
96 | umask(0022); |
97 | ||
84e51726 LP |
98 | if (argc != 1) { |
99 | log_error("This program does not take arguments."); | |
100 | r = -EINVAL; | |
101 | goto finish; | |
102 | } | |
103 | ||
444c1915 YW |
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 | } | |
ece6e766 LP |
113 | } |
114 | ||
d636d376 | 115 | r = load_clock_timestamp(uid, gid); |
ece6e766 | 116 | if (r < 0) |
84e51726 | 117 | goto finish; |
ece6e766 | 118 | |
87a85e25 YW |
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. */ | |
444c1915 | 121 | if (uid_current == 0) { |
87a85e25 YW |
122 | r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME)); |
123 | if (r < 0) | |
124 | goto finish; | |
125 | } | |
a349eb10 | 126 | |
72c0a2c2 | 127 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0); |
856a5a7d | 128 | |
687ed123 | 129 | r = manager_new(&m); |
856a5a7d | 130 | if (r < 0) { |
da927ba9 | 131 | log_error_errno(r, "Failed to allocate manager: %m"); |
84e51726 | 132 | goto finish; |
856a5a7d | 133 | } |
687ed123 | 134 | |
e7dd3947 YW |
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 | ||
6369641d | 141 | if (clock_is_localtime(NULL) > 0) { |
c264aeab | 142 | log_info("The system is configured to read the RTC time in the local time zone. " |
87ac8d99 | 143 | "This mode cannot be fully supported. All system time to RTC updates are disabled."); |
c264aeab KS |
144 | m->rtc_local_time = true; |
145 | } | |
146 | ||
84e51726 LP |
147 | r = manager_parse_config_file(m); |
148 | if (r < 0) | |
da927ba9 | 149 | log_warning_errno(r, "Failed to parse configuration file: %m"); |
e0e5ce23 | 150 | |
c4c06912 LP |
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 | } | |
3745770a | 156 | |
df0ff127 | 157 | log_debug("systemd-timesyncd running as pid " PID_FMT, getpid_cached()); |
af4ec430 LP |
158 | sd_notify(false, |
159 | "READY=1\n" | |
160 | "STATUS=Daemon is running"); | |
39594d49 | 161 | |
e0e5ce23 TG |
162 | if (network_is_online()) { |
163 | r = manager_connect(m); | |
164 | if (r < 0) | |
84e51726 | 165 | goto finish; |
e0e5ce23 | 166 | } |
678522cf | 167 | |
687ed123 | 168 | r = sd_event_loop(m->event); |
856a5a7d | 169 | if (r < 0) { |
da927ba9 | 170 | log_error_errno(r, "Failed to run event loop: %m"); |
84e51726 | 171 | goto finish; |
856a5a7d LP |
172 | } |
173 | ||
d636d376 KS |
174 | /* if we got an authoritative time, store it in the file system */ |
175 | if (m->sync) | |
53d133ea | 176 | (void) touch("/var/lib/systemd/timesync/clock"); |
687ed123 | 177 | |
84e51726 LP |
178 | sd_event_get_exit_code(m->event, &r); |
179 | ||
180 | finish: | |
af4ec430 LP |
181 | sd_notify(false, |
182 | "STOPPING=1\n" | |
183 | "STATUS=Shutting down..."); | |
e8af6973 | 184 | |
687ed123 | 185 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
bcdbbd7e | 186 | } |