]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/timesync/timesyncd.c
92c50422aac0ea7ed824fb3720b511e58de25e8e
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2014 Kay Sievers, Lennart Poettering
9 #include "capability-util.h"
10 #include "clock-util.h"
14 #include "network-util.h"
15 #include "process-util.h"
16 #include "signal-util.h"
17 #include "timesyncd-bus.h"
18 #include "timesyncd-conf.h"
19 #include "timesyncd-manager.h"
20 #include "user-util.h"
22 static int load_clock_timestamp(uid_t uid
, gid_t gid
) {
23 _cleanup_close_
int fd
= -1;
24 usec_t min
= TIME_EPOCH
* USEC_PER_SEC
;
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. */
35 fd
= open("/var/lib/systemd/timesync/clock", O_RDWR
|O_CLOEXEC
, 0644);
40 /* check if the recorded time is later than the compiled-in one */
43 stamp
= timespec_load(&st
.st_mtim
);
49 /* Try to fix the access mode, so that we can still
50 touch the file after dropping priviliges */
53 return log_error_errno(errno
, "Failed to change file access mode: %m");
54 r
= fchown(fd
, uid
, gid
);
56 return log_error_errno(errno
, "Failed to change file owner: %m");
60 r
= mkdir_safe_label("/var/lib/systemd/timesync", 0755, uid
, gid
,
61 MKDIR_FOLLOW_SYMLINK
| MKDIR_WARN_MODE
);
63 return log_error_errno(r
, "Failed to create state directory: %m");
65 /* create stamp file with the compiled-in date */
66 (void) touch_file("/var/lib/systemd/timesync/clock", false, min
, uid
, gid
, 0644);
69 ct
= now(CLOCK_REALTIME
);
72 char date
[FORMAT_TIMESTAMP_MAX
];
74 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
75 format_timestamp(date
, sizeof(date
), min
));
77 if (clock_settime(CLOCK_REALTIME
, timespec_store(&ts
, min
)) < 0)
78 log_error_errno(errno
, "Failed to restore system clock: %m");
84 int main(int argc
, char *argv
[]) {
85 _cleanup_(manager_freep
) Manager
*m
= NULL
;
86 const char *user
= "systemd-timesync";
87 uid_t uid
, uid_current
;
91 log_set_target(LOG_TARGET_AUTO
);
92 log_set_facility(LOG_CRON
);
93 log_parse_environment();
99 log_error("This program does not take arguments.");
104 uid
= uid_current
= geteuid();
107 if (uid_current
== 0) {
108 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
);
110 log_error_errno(r
, "Cannot resolve user name %s: %m", user
);
115 r
= load_clock_timestamp(uid
, gid
);
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. */
121 if (uid_current
== 0) {
122 r
= drop_privileges(uid
, gid
, (1ULL << CAP_SYS_TIME
));
127 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
131 log_error_errno(r
, "Failed to allocate manager: %m");
135 r
= manager_connect_bus(m
);
137 log_error_errno(r
, "Could not connect to bus: %m");
141 if (clock_is_localtime(NULL
) > 0) {
142 log_info("The system is configured to read the RTC time in the local time zone. "
143 "This mode cannot be fully supported. All system time to RTC updates are disabled.");
144 m
->rtc_local_time
= true;
147 r
= manager_parse_config_file(m
);
149 log_warning_errno(r
, "Failed to parse configuration file: %m");
151 r
= manager_parse_fallback_string(m
, NTP_SERVERS
);
153 log_error_errno(r
, "Failed to parse fallback server strings: %m");
157 log_debug("systemd-timesyncd running as pid " PID_FMT
, getpid_cached());
160 "STATUS=Daemon is running");
162 if (network_is_online()) {
163 r
= manager_connect(m
);
168 r
= sd_event_loop(m
->event
);
170 log_error_errno(r
, "Failed to run event loop: %m");
174 /* if we got an authoritative time, store it in the file system */
176 (void) touch("/var/lib/systemd/timesync/clock");
178 sd_event_get_exit_code(m
->event
, &r
);
183 "STATUS=Shutting down...");
185 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;