]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/timesync/timesyncd.c
7a0ab18ca012908d0b992a4ee98327e00c693281
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/>.
22 #include "sd-daemon.h"
25 #include "capability.h"
26 #include "clock-util.h"
28 #include "network-util.h"
29 #include "signal-util.h"
30 #include "timesyncd-conf.h"
31 #include "timesyncd-manager.h"
32 #include "user-util.h"
34 static int load_clock_timestamp(uid_t uid
, gid_t gid
) {
35 _cleanup_close_
int fd
= -1;
36 usec_t min
= TIME_EPOCH
* USEC_PER_SEC
;
40 /* Let's try to make sure that the clock is always
41 * monotonically increasing, by saving the clock whenever we
42 * have a new NTP time, or when we shut down, and restoring it
43 * when we start again. This is particularly helpful on
44 * systems lacking a battery backed RTC. We also will adjust
45 * the time to at least the build time of systemd. */
47 fd
= open("/var/lib/systemd/clock", O_RDWR
|O_CLOEXEC
, 0644);
52 /* check if the recorded time is later than the compiled-in one */
55 stamp
= timespec_load(&st
.st_mtim
);
60 /* Try to fix the access mode, so that we can still
61 touch the file after dropping priviliges */
62 (void) fchmod(fd
, 0644);
63 (void) fchown(fd
, uid
, gid
);
66 /* create stamp file with the compiled-in date */
67 (void) touch_file("/var/lib/systemd/clock", true, 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";
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 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
);
106 log_error_errno(r
, "Cannot resolve user name %s: %m", user
);
110 r
= load_clock_timestamp(uid
, gid
);
114 r
= drop_privileges(uid
, gid
, (1ULL << CAP_SYS_TIME
));
118 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
122 log_error_errno(r
, "Failed to allocate manager: %m");
126 if (clock_is_localtime() > 0) {
127 log_info("The system is configured to read the RTC time in the local time zone. "
128 "This mode can not be fully supported. All system time to RTC updates are disabled.");
129 m
->rtc_local_time
= true;
132 r
= manager_parse_config_file(m
);
134 log_warning_errno(r
, "Failed to parse configuration file: %m");
136 log_debug("systemd-timesyncd running as pid " PID_FMT
, getpid());
139 "STATUS=Daemon is running");
141 if (network_is_online()) {
142 r
= manager_connect(m
);
147 r
= sd_event_loop(m
->event
);
149 log_error_errno(r
, "Failed to run event loop: %m");
153 /* if we got an authoritative time, store it in the file system */
155 (void) touch("/var/lib/systemd/clock");
157 sd_event_get_exit_code(m
->event
, &r
);
162 "STATUS=Shutting down...");
164 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;