]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/timesync/timesyncd.c
2 This file is part of systemd.
4 Copyright 2014 Kay Sievers, Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "sd-daemon.h"
23 #include "capability-util.h"
24 #include "clock-util.h"
28 #include "network-util.h"
29 #include "process-util.h"
30 #include "signal-util.h"
31 #include "timesyncd-conf.h"
32 #include "timesyncd-manager.h"
33 #include "user-util.h"
35 static int load_clock_timestamp(uid_t uid
, gid_t gid
) {
36 _cleanup_close_
int fd
= -1;
37 usec_t min
= TIME_EPOCH
* USEC_PER_SEC
;
41 /* Let's try to make sure that the clock is always
42 * monotonically increasing, by saving the clock whenever we
43 * have a new NTP time, or when we shut down, and restoring it
44 * when we start again. This is particularly helpful on
45 * systems lacking a battery backed RTC. We also will adjust
46 * the time to at least the build time of systemd. */
48 fd
= open("/var/lib/systemd/timesync/clock", O_RDWR
|O_CLOEXEC
, 0644);
53 /* check if the recorded time is later than the compiled-in one */
56 stamp
= timespec_load(&st
.st_mtim
);
62 /* Try to fix the access mode, so that we can still
63 touch the file after dropping priviliges */
66 return log_error_errno(errno
, "Failed to change file access mode: %m");
67 r
= fchown(fd
, uid
, gid
);
68 return log_error_errno(errno
, "Failed to change file owner: %m");
72 r
= mkdir_safe_label("/var/lib/systemd/timesync", 0755, uid
, gid
, true);
74 return log_error_errno(r
, "Failed to create state directory: %m");
76 /* create stamp file with the compiled-in date */
77 (void) touch_file("/var/lib/systemd/timesync/clock", false, min
, uid
, gid
, 0644);
80 ct
= now(CLOCK_REALTIME
);
83 char date
[FORMAT_TIMESTAMP_MAX
];
85 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
86 format_timestamp(date
, sizeof(date
), min
));
88 if (clock_settime(CLOCK_REALTIME
, timespec_store(&ts
, min
)) < 0)
89 log_error_errno(errno
, "Failed to restore system clock: %m");
95 int main(int argc
, char *argv
[]) {
96 _cleanup_(manager_freep
) Manager
*m
= NULL
;
97 const char *user
= "systemd-timesync";
102 log_set_target(LOG_TARGET_AUTO
);
103 log_set_facility(LOG_CRON
);
104 log_parse_environment();
110 log_error("This program does not take arguments.");
115 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
);
117 log_error_errno(r
, "Cannot resolve user name %s: %m", user
);
121 r
= load_clock_timestamp(uid
, gid
);
125 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all
126 * privileges are already dropped. */
127 if (geteuid() == 0) {
128 r
= drop_privileges(uid
, gid
, (1ULL << CAP_SYS_TIME
));
133 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
137 log_error_errno(r
, "Failed to allocate manager: %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 can not 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
;