]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/timesync/timesyncd.c
timesyncd,resolved,machinectl: drop calls to sd_event_get_exit_code()
[thirdparty/systemd.git] / src / timesync / timesyncd.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
bcdbbd7e 2
687ed123 3#include "sd-daemon.h"
3ffd4af2
LP
4#include "sd-event.h"
5
430f0182 6#include "capability-util.h"
84e51726 7#include "clock-util.h"
82310c79 8#include "daemon-util.h"
3ffd4af2 9#include "fd-util.h"
f4f15635 10#include "fs-util.h"
82310c79 11#include "main-func.h"
53d133ea 12#include "mkdir.h"
84e51726 13#include "network-util.h"
df0ff127 14#include "process-util.h"
24882e06 15#include "signal-util.h"
e7dd3947 16#include "timesyncd-bus.h"
84e51726 17#include "timesyncd-conf.h"
3ffd4af2 18#include "timesyncd-manager.h"
b1d4f8e1 19#include "user-util.h"
d636d376 20
86aaccb0
YW
21#define STATE_DIR "/var/lib/systemd/timesync"
22#define CLOCK_FILE STATE_DIR "/clock"
23
d636d376 24static 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
86aaccb0 37 fd = open(CLOCK_FILE, 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 */
86aaccb0 53 r = fchmod_and_chown(fd, 0644, uid, gid);
d1c2774b 54 if (r < 0)
86aaccb0 55 log_warning_errno(r, "Failed to chmod or chown %s, ignoring: %m", CLOCK_FILE);
53d133ea
YW
56 }
57
58 } else {
86aaccb0 59 r = mkdir_safe_label(STATE_DIR, 0755, uid, gid,
37c1d5e9 60 MKDIR_FOLLOW_SYMLINK | MKDIR_WARN_MODE);
86aaccb0
YW
61 if (r < 0) {
62 log_debug_errno(r, "Failed to create state directory, ignoring: %m");
63 goto settime;
64 }
d636d376 65
d636d376 66 /* create stamp file with the compiled-in date */
86aaccb0
YW
67 r = touch_file(CLOCK_FILE, false, min, uid, gid, 0644);
68 if (r < 0)
69 log_debug_errno(r, "Failed to create %s, ignoring: %m", CLOCK_FILE);
53d133ea 70 }
ece6e766 71
86aaccb0 72settime:
ece6e766 73 ct = now(CLOCK_REALTIME);
d636d376 74 if (ct < min) {
ece6e766 75 struct timespec ts;
d636d376 76 char date[FORMAT_TIMESTAMP_MAX];
ece6e766 77
d636d376
KS
78 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
79 format_timestamp(date, sizeof(date), min));
ece6e766 80
d636d376 81 if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0)
86aaccb0 82 log_error_errno(errno, "Failed to restore system clock, ignoring: %m");
ece6e766
LP
83 }
84
85 return 0;
86}
87
82310c79
YW
88static int run(int argc, char *argv[]) {
89 _cleanup_(notify_on_cleanup) const char *notify_message = NULL;
84e51726 90 _cleanup_(manager_freep) Manager *m = NULL;
cedc8c44 91 const char *user = "systemd-timesync";
444c1915 92 uid_t uid, uid_current;
ece6e766 93 gid_t gid;
687ed123
KS
94 int r;
95
e8af6973 96 log_set_facility(LOG_CRON);
6bf3c61c 97 log_setup_service();
687ed123 98
e8af6973
LP
99 umask(0022);
100
82310c79
YW
101 if (argc != 1)
102 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program does not take arguments.");
84e51726 103
444c1915
YW
104 uid = uid_current = geteuid();
105 gid = getegid();
106
107 if (uid_current == 0) {
fafff8f1 108 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
82310c79
YW
109 if (r < 0)
110 return log_error_errno(r, "Cannot resolve user name %s: %m", user);
ece6e766
LP
111 }
112
d636d376 113 r = load_clock_timestamp(uid, gid);
ece6e766 114 if (r < 0)
82310c79 115 return r;
ece6e766 116
87a85e25
YW
117 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all
118 * privileges are already dropped. */
444c1915 119 if (uid_current == 0) {
87a85e25
YW
120 r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME));
121 if (r < 0)
82310c79 122 return log_error_errno(r, "Failed to drop privileges: %m");
87a85e25 123 }
a349eb10 124
72c0a2c2 125 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
856a5a7d 126
687ed123 127 r = manager_new(&m);
82310c79
YW
128 if (r < 0)
129 return log_error_errno(r, "Failed to allocate manager: %m");
687ed123 130
e7dd3947 131 r = manager_connect_bus(m);
82310c79
YW
132 if (r < 0)
133 return log_error_errno(r, "Could not connect to bus: %m");
e7dd3947 134
6369641d 135 if (clock_is_localtime(NULL) > 0) {
c264aeab 136 log_info("The system is configured to read the RTC time in the local time zone. "
87ac8d99 137 "This mode cannot be fully supported. All system time to RTC updates are disabled.");
c264aeab
KS
138 m->rtc_local_time = true;
139 }
140
84e51726
LP
141 r = manager_parse_config_file(m);
142 if (r < 0)
da927ba9 143 log_warning_errno(r, "Failed to parse configuration file: %m");
e0e5ce23 144
c4c06912 145 r = manager_parse_fallback_string(m, NTP_SERVERS);
82310c79
YW
146 if (r < 0)
147 return log_error_errno(r, "Failed to parse fallback server strings: %m");
3745770a 148
df0ff127 149 log_debug("systemd-timesyncd running as pid " PID_FMT, getpid_cached());
82310c79
YW
150
151 notify_message = notify_start("READY=1\n"
152 "STATUS=Daemon is running",
153 NOTIFY_STOPPING);
39594d49 154
e0e5ce23
TG
155 if (network_is_online()) {
156 r = manager_connect(m);
157 if (r < 0)
82310c79 158 return r;
e0e5ce23 159 }
678522cf 160
687ed123 161 r = sd_event_loop(m->event);
82310c79
YW
162 if (r < 0)
163 return log_error_errno(r, "Failed to run event loop: %m");
856a5a7d 164
d636d376 165 /* if we got an authoritative time, store it in the file system */
86aaccb0
YW
166 if (m->sync) {
167 r = touch(CLOCK_FILE);
168 if (r < 0)
169 log_debug_errno(r, "Failed to touch %s, ignoring: %m", CLOCK_FILE);
170 }
687ed123 171
690f02f4 172 return 0;
bcdbbd7e 173}
82310c79
YW
174
175DEFINE_MAIN_FUNCTION(run);