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