timesyncd.c revision 72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering This file is part of systemd.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering Copyright 2014 Kay Sievers, Lennart Poettering
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering systemd is free software; you can redistribute it and/or modify it
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering under the terms of the GNU Lesser General Public License as published by
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering (at your option) any later version.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering systemd is distributed in the hope that it will be useful, but
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering Lesser General Public License for more details.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering You should have received a copy of the GNU Lesser General Public License
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poetteringstatic int load_clock_timestamp(uid_t uid, gid_t gid) {
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* Let's try to make sure that the clock is always
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * monotonically increasing, by saving the clock whenever we
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * have a new NTP time, or when we shut down, and restoring it
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * when we start again. This is particularly helpful on
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * systems lacking a battery backed RTC. We also will adjust
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering * the time to at least the build time of systemd. */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering fd = open("/var/lib/systemd/clock", O_RDWR|O_CLOEXEC, 0644);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* check if the recorded time is later than the compiled-in one */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* Try to fix the access mode, so that we can still
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering touch the file after dropping priviliges */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* create stamp file with the compiled-in date */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering touch_file("/var/lib/systemd/clock", true, min, uid, gid, 0644);
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s",
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering format_timestamp(date, sizeof(date), min));
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_error_errno(errno, "Failed to restore system clock: %m");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering _cleanup_(manager_freep) Manager *m = NULL;
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_error("This program does not take arguments.");
90b2de37b80603168f4e9c9c81cff7eea4efa21aZbigniew Jędrzejewski-Szmek r = get_user_creds(&user, &uid, &gid, NULL, NULL);
eecd1362f7f4de432483b5d77c56726c3621a83aLennart Poettering log_error_errno(r, "Cannot resolve user name %s: %m", user);
88231eb62cafc8bb51406919c8cf6019dc1ac916Thomas Hindoe Paaboel Andersen r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME));
88231eb62cafc8bb51406919c8cf6019dc1ac916Thomas Hindoe Paaboel Andersen /* We need one process for ourselves, plus one thread for the asynchronous resolver */
88231eb62cafc8bb51406919c8cf6019dc1ac916Thomas Hindoe Paaboel Andersen if (setrlimit(RLIMIT_NPROC, &RLIMIT_MAKE_CONST(2)) < 0)
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_warning_errno(errno, "Failed to lower RLIMIT_NPROC to 2: %m");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
88231eb62cafc8bb51406919c8cf6019dc1ac916Thomas Hindoe Paaboel Andersen log_error_errno(r, "Failed to allocate manager: %m");
88231eb62cafc8bb51406919c8cf6019dc1ac916Thomas Hindoe Paaboel Andersen if (clock_is_localtime() > 0) {
88231eb62cafc8bb51406919c8cf6019dc1ac916Thomas Hindoe Paaboel Andersen log_info("The system is configured to read the RTC time in the local time zone. "
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "This mode can not be fully supported. All system time to RTC updates are disabled.");
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering log_warning_errno(r, "Failed to parse configuration file: %m");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_debug("systemd-timesyncd running as pid %lu", (unsigned long) getpid());
dacd6cee76a08331b8c8616c5f30f70ee49aa2f9Lennart Poettering "STATUS=Daemon is running");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering log_error_errno(r, "Failed to run event loop: %m");
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering /* if we got an authoritative time, store it in the file system */
f8e2fb7b14e53f5a4bcfd66d26910af1dee185c6Lennart Poettering "STATUS=Shutting down...");