1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Kay Sievers, Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "sd-daemon.h"
25 #include "capability.h"
26 #include "clock-util.h"
27 #include "network-util.h"
29 #include "timesyncd-manager.h"
30 #include "timesyncd-conf.h"
32 static int load_clock_timestamp(uid_t uid, gid_t gid) {
33 _cleanup_close_ int fd = -1;
34 usec_t min = TIME_EPOCH * USEC_PER_SEC;
38 /* Let's try to make sure that the clock is always
39 * monotonically increasing, by saving the clock whenever we
40 * have a new NTP time, or when we shut down, and restoring it
41 * when we start again. This is particularly helpful on
42 * systems lacking a battery backed RTC. We also will adjust
43 * the time to at least the build time of systemd. */
45 fd = open("/var/lib/systemd/clock", O_RDWR|O_CLOEXEC, 0644);
50 /* check if the recorded time is later than the compiled-in one */
53 stamp = timespec_load(&st.st_mtim);
58 /* Try to fix the access mode, so that we can still
59 touch the file after dropping priviliges */
64 /* create stamp file with the compiled-in date */
65 touch_file("/var/lib/systemd/clock", true, min, uid, gid, 0644);
67 ct = now(CLOCK_REALTIME);
70 char date[FORMAT_TIMESTAMP_MAX];
72 log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
73 format_timestamp(date, sizeof(date), min));
75 if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0)
76 log_error("Failed to restore system clock: %m");
82 int main(int argc, char *argv[]) {
83 _cleanup_(manager_freep) Manager *m = NULL;
84 const char *user = "systemd-timesync";
89 log_set_target(LOG_TARGET_AUTO);
90 log_set_facility(LOG_CRON);
91 log_parse_environment();
97 log_error("This program does not take arguments.");
102 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
104 log_error("Cannot resolve user name %s: %s", user, strerror(-r));
108 r = load_clock_timestamp(uid, gid);
112 r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME));
116 assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
120 log_error("Failed to allocate manager: %s", strerror(-r));
124 if (clock_is_localtime() > 0) {
125 log_info("The system is configured to read the RTC time in the local time zone. "
126 "This mode can not be fully supported. All system time to RTC updates are disabled.");
127 m->rtc_local_time = true;
130 r = manager_parse_config_file(m);
132 log_warning("Failed to parse configuration file: %s", strerror(-r));
134 log_debug("systemd-timesyncd running as pid %lu", (unsigned long) getpid());
135 sd_notify(false, "READY=1");
137 if (network_is_online()) {
138 r = manager_connect(m);
143 r = sd_event_loop(m->event);
145 log_error("Failed to run event loop: %s", strerror(-r));
149 /* if we got an authoritative time, store it in the file system */
151 touch("/var/lib/systemd/clock");
153 sd_event_get_exit_code(m->event, &r);
156 sd_notify(false, "STATUS=Shutting down...");
158 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;