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