]>
Commit | Line | Data |
---|---|---|
bcdbbd7e KS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
e8af6973 | 4 | Copyright 2014 Kay Sievers, Lennart Poettering |
bcdbbd7e KS |
5 | |
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. | |
10 | ||
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. | |
15 | ||
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/>. | |
18 | ***/ | |
19 | ||
687ed123 | 20 | #include "sd-daemon.h" |
3ffd4af2 LP |
21 | #include "sd-event.h" |
22 | ||
430f0182 | 23 | #include "capability-util.h" |
84e51726 | 24 | #include "clock-util.h" |
3ffd4af2 | 25 | #include "fd-util.h" |
f4f15635 | 26 | #include "fs-util.h" |
84e51726 | 27 | #include "network-util.h" |
df0ff127 | 28 | #include "process-util.h" |
24882e06 | 29 | #include "signal-util.h" |
84e51726 | 30 | #include "timesyncd-conf.h" |
3ffd4af2 | 31 | #include "timesyncd-manager.h" |
b1d4f8e1 | 32 | #include "user-util.h" |
d636d376 KS |
33 | |
34 | static int load_clock_timestamp(uid_t uid, gid_t gid) { | |
ece6e766 | 35 | _cleanup_close_ int fd = -1; |
d636d376 KS |
36 | usec_t min = TIME_EPOCH * USEC_PER_SEC; |
37 | usec_t ct; | |
38 | int r; | |
ece6e766 LP |
39 | |
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. */ | |
46 | ||
d636d376 KS |
47 | fd = open("/var/lib/systemd/clock", O_RDWR|O_CLOEXEC, 0644); |
48 | if (fd >= 0) { | |
ece6e766 | 49 | struct stat st; |
d636d376 KS |
50 | usec_t stamp; |
51 | ||
52 | /* check if the recorded time is later than the compiled-in one */ | |
53 | r = fstat(fd, &st); | |
54 | if (r >= 0) { | |
55 | stamp = timespec_load(&st.st_mtim); | |
56 | if (stamp > min) | |
57 | min = stamp; | |
ece6e766 LP |
58 | } |
59 | ||
d636d376 KS |
60 | /* Try to fix the access mode, so that we can still |
61 | touch the file after dropping priviliges */ | |
ac5b0c13 LP |
62 | (void) fchmod(fd, 0644); |
63 | (void) fchown(fd, uid, gid); | |
d636d376 KS |
64 | |
65 | } else | |
66 | /* create stamp file with the compiled-in date */ | |
ac5b0c13 | 67 | (void) touch_file("/var/lib/systemd/clock", true, min, uid, gid, 0644); |
ece6e766 LP |
68 | |
69 | ct = now(CLOCK_REALTIME); | |
d636d376 | 70 | if (ct < min) { |
ece6e766 | 71 | struct timespec ts; |
d636d376 | 72 | char date[FORMAT_TIMESTAMP_MAX]; |
ece6e766 | 73 | |
d636d376 KS |
74 | log_info("System clock time unset or jumped backwards, restoring from recorded timestamp: %s", |
75 | format_timestamp(date, sizeof(date), min)); | |
ece6e766 | 76 | |
d636d376 | 77 | if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, min)) < 0) |
56f64d95 | 78 | log_error_errno(errno, "Failed to restore system clock: %m"); |
ece6e766 LP |
79 | } |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
687ed123 | 84 | int main(int argc, char *argv[]) { |
84e51726 | 85 | _cleanup_(manager_freep) Manager *m = NULL; |
cedc8c44 | 86 | const char *user = "systemd-timesync"; |
ece6e766 LP |
87 | uid_t uid; |
88 | gid_t gid; | |
687ed123 KS |
89 | int r; |
90 | ||
91 | log_set_target(LOG_TARGET_AUTO); | |
e8af6973 | 92 | log_set_facility(LOG_CRON); |
687ed123 KS |
93 | log_parse_environment(); |
94 | log_open(); | |
95 | ||
e8af6973 LP |
96 | umask(0022); |
97 | ||
84e51726 LP |
98 | if (argc != 1) { |
99 | log_error("This program does not take arguments."); | |
100 | r = -EINVAL; | |
101 | goto finish; | |
102 | } | |
103 | ||
ece6e766 LP |
104 | r = get_user_creds(&user, &uid, &gid, NULL, NULL); |
105 | if (r < 0) { | |
da927ba9 | 106 | log_error_errno(r, "Cannot resolve user name %s: %m", user); |
84e51726 | 107 | goto finish; |
ece6e766 LP |
108 | } |
109 | ||
d636d376 | 110 | r = load_clock_timestamp(uid, gid); |
ece6e766 | 111 | if (r < 0) |
84e51726 | 112 | goto finish; |
ece6e766 | 113 | |
966bff26 | 114 | r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME)); |
a349eb10 | 115 | if (r < 0) |
84e51726 | 116 | goto finish; |
a349eb10 | 117 | |
72c0a2c2 | 118 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0); |
856a5a7d | 119 | |
687ed123 | 120 | r = manager_new(&m); |
856a5a7d | 121 | if (r < 0) { |
da927ba9 | 122 | log_error_errno(r, "Failed to allocate manager: %m"); |
84e51726 | 123 | goto finish; |
856a5a7d | 124 | } |
687ed123 | 125 | |
6369641d | 126 | if (clock_is_localtime(NULL) > 0) { |
c264aeab KS |
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; | |
130 | } | |
131 | ||
84e51726 LP |
132 | r = manager_parse_config_file(m); |
133 | if (r < 0) | |
da927ba9 | 134 | log_warning_errno(r, "Failed to parse configuration file: %m"); |
e0e5ce23 | 135 | |
c4c06912 LP |
136 | r = manager_parse_fallback_string(m, NTP_SERVERS); |
137 | if (r < 0) { | |
138 | log_error_errno(r, "Failed to parse fallback server strings: %m"); | |
139 | goto finish; | |
140 | } | |
3745770a | 141 | |
df0ff127 | 142 | log_debug("systemd-timesyncd running as pid " PID_FMT, getpid_cached()); |
af4ec430 LP |
143 | sd_notify(false, |
144 | "READY=1\n" | |
145 | "STATUS=Daemon is running"); | |
39594d49 | 146 | |
e0e5ce23 TG |
147 | if (network_is_online()) { |
148 | r = manager_connect(m); | |
149 | if (r < 0) | |
84e51726 | 150 | goto finish; |
e0e5ce23 | 151 | } |
678522cf | 152 | |
687ed123 | 153 | r = sd_event_loop(m->event); |
856a5a7d | 154 | if (r < 0) { |
da927ba9 | 155 | log_error_errno(r, "Failed to run event loop: %m"); |
84e51726 | 156 | goto finish; |
856a5a7d LP |
157 | } |
158 | ||
d636d376 KS |
159 | /* if we got an authoritative time, store it in the file system */ |
160 | if (m->sync) | |
ac5b0c13 | 161 | (void) touch("/var/lib/systemd/clock"); |
687ed123 | 162 | |
84e51726 LP |
163 | sd_event_get_exit_code(m->event, &r); |
164 | ||
165 | finish: | |
af4ec430 LP |
166 | sd_notify(false, |
167 | "STOPPING=1\n" | |
168 | "STATUS=Shutting down..."); | |
e8af6973 | 169 | |
687ed123 | 170 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
bcdbbd7e | 171 | } |