]>
git.proxmox.com Git - systemd.git/blob - src/timesync/timesyncd.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "capability-util.h"
10 #include "clock-util.h"
11 #include "daemon-util.h"
14 #include "main-func.h"
16 #include "network-util.h"
17 #include "process-util.h"
18 #include "signal-util.h"
19 #include "timesyncd-bus.h"
20 #include "timesyncd-conf.h"
21 #include "timesyncd-manager.h"
22 #include "user-util.h"
24 #define STATE_DIR "/var/lib/systemd/timesync"
25 #define CLOCK_FILE STATE_DIR "/clock"
27 static int load_clock_timestamp(uid_t uid
, gid_t gid
) {
28 _cleanup_close_
int fd
= -1;
29 usec_t min
= TIME_EPOCH
* USEC_PER_SEC
;
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. */
40 fd
= open(CLOCK_FILE
, O_RDWR
|O_CLOEXEC
, 0644);
45 /* check if the recorded time is later than the compiled-in one */
48 stamp
= timespec_load(&st
.st_mtim
);
54 /* Try to fix the access mode, so that we can still
55 touch the file after dropping priviliges */
56 r
= fchmod_and_chown(fd
, 0644, uid
, gid
);
58 log_warning_errno(r
, "Failed to chmod or chown %s, ignoring: %m", CLOCK_FILE
);
62 r
= mkdir_safe_label(STATE_DIR
, 0755, uid
, gid
,
63 MKDIR_FOLLOW_SYMLINK
| MKDIR_WARN_MODE
);
65 log_debug_errno(r
, "Failed to create state directory, ignoring: %m");
69 /* create stamp file with the compiled-in date */
70 r
= touch_file(CLOCK_FILE
, false, min
, uid
, gid
, 0644);
72 log_debug_errno(r
, "Failed to create %s, ignoring: %m", CLOCK_FILE
);
76 ct
= now(CLOCK_REALTIME
);
79 char date
[FORMAT_TIMESTAMP_MAX
];
81 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
82 format_timestamp(date
, sizeof(date
), min
));
84 if (clock_settime(CLOCK_REALTIME
, timespec_store(&ts
, min
)) < 0)
85 log_error_errno(errno
, "Failed to restore system clock, ignoring: %m");
91 static int run(int argc
, char *argv
[]) {
92 _cleanup_(notify_on_cleanup
) const char *notify_message
= NULL
;
93 _cleanup_(manager_freep
) Manager
*m
= NULL
;
94 const char *user
= "systemd-timesync";
95 uid_t uid
, uid_current
;
99 log_set_facility(LOG_CRON
);
105 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program does not take arguments.");
107 uid
= uid_current
= geteuid();
110 if (uid_current
== 0) {
111 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
, 0);
113 return log_error_errno(r
, "Cannot resolve user name %s: %m", user
);
116 r
= load_clock_timestamp(uid
, gid
);
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. */
122 if (uid_current
== 0) {
123 r
= drop_privileges(uid
, gid
, (1ULL << CAP_SYS_TIME
));
125 return log_error_errno(r
, "Failed to drop privileges: %m");
128 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
132 return log_error_errno(r
, "Failed to allocate manager: %m");
134 r
= manager_connect_bus(m
);
136 return log_error_errno(r
, "Could not connect to bus: %m");
138 if (clock_is_localtime(NULL
) > 0) {
139 log_info("The system is configured to read the RTC time in the local time zone. "
140 "This mode cannot be fully supported. All system time to RTC updates are disabled.");
141 m
->rtc_local_time
= true;
144 r
= manager_parse_config_file(m
);
146 log_warning_errno(r
, "Failed to parse configuration file: %m");
148 r
= manager_parse_fallback_string(m
, NTP_SERVERS
);
150 return log_error_errno(r
, "Failed to parse fallback server strings: %m");
152 log_debug("systemd-timesyncd running as pid " PID_FMT
, getpid_cached());
154 notify_message
= notify_start("READY=1\n"
155 "STATUS=Daemon is running",
158 if (network_is_online()) {
159 r
= manager_connect(m
);
164 r
= sd_event_loop(m
->event
);
166 return log_error_errno(r
, "Failed to run event loop: %m");
168 /* if we got an authoritative time, store it in the file system */
170 r
= touch(CLOCK_FILE
);
172 log_debug_errno(r
, "Failed to touch %s, ignoring: %m", CLOCK_FILE
);
178 DEFINE_MAIN_FUNCTION(run
);