]>
Commit | Line | Data |
---|---|---|
f6144808 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
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 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <sys/socket.h> | |
23 | #include <sys/poll.h> | |
24 | #include <sys/types.h> | |
25 | #include <sys/timerfd.h> | |
26 | #include <assert.h> | |
27 | #include <string.h> | |
28 | #include <errno.h> | |
29 | #include <unistd.h> | |
30 | #include <fcntl.h> | |
31 | ||
81527be1 LP |
32 | #include <systemd/sd-daemon.h> |
33 | ||
f6144808 LP |
34 | #include "shutdownd.h" |
35 | #include "log.h" | |
36 | #include "macro.h" | |
37 | #include "util.h" | |
9be9828c | 38 | #include "utmp-wtmp.h" |
f6144808 LP |
39 | |
40 | static int read_packet(int fd, struct shutdownd_command *_c) { | |
41 | struct msghdr msghdr; | |
42 | struct iovec iovec; | |
43 | struct ucred *ucred; | |
44 | union { | |
45 | struct cmsghdr cmsghdr; | |
46 | uint8_t buf[CMSG_SPACE(sizeof(struct ucred))]; | |
47 | } control; | |
48 | struct shutdownd_command c; | |
49 | ssize_t n; | |
50 | ||
51 | assert(fd >= 0); | |
52 | assert(_c); | |
53 | ||
54 | zero(iovec); | |
55 | iovec.iov_base = &c; | |
56 | iovec.iov_len = sizeof(c); | |
57 | ||
58 | zero(control); | |
59 | zero(msghdr); | |
60 | msghdr.msg_iov = &iovec; | |
61 | msghdr.msg_iovlen = 1; | |
62 | msghdr.msg_control = &control; | |
63 | msghdr.msg_controllen = sizeof(control); | |
64 | ||
65 | if ((n = recvmsg(fd, &msghdr, MSG_DONTWAIT)) <= 0) { | |
66 | if (n >= 0) { | |
67 | log_error("Short read"); | |
68 | return -EIO; | |
69 | } | |
70 | ||
71 | if (errno == EAGAIN || errno == EINTR) | |
72 | return 0; | |
73 | ||
74 | log_error("recvmsg(): %m"); | |
75 | return -errno; | |
76 | } | |
77 | ||
78 | if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) || | |
79 | control.cmsghdr.cmsg_level != SOL_SOCKET || | |
80 | control.cmsghdr.cmsg_type != SCM_CREDENTIALS || | |
81 | control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) { | |
82 | log_warning("Received message without credentials. Ignoring."); | |
83 | return 0; | |
84 | } | |
85 | ||
86 | ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr); | |
87 | if (ucred->uid != 0) { | |
88 | log_warning("Got request from unprivileged user. Ignoring."); | |
89 | return 0; | |
90 | } | |
91 | ||
92 | if (n != sizeof(c)) { | |
6b5ad000 | 93 | log_warning("Message has invalid size. Ignoring"); |
f6144808 LP |
94 | return 0; |
95 | } | |
96 | ||
9be9828c LP |
97 | char_array_0(c.wall_message); |
98 | ||
f6144808 LP |
99 | *_c = c; |
100 | return 1; | |
101 | } | |
102 | ||
08e4b1c5 | 103 | static void warn_wall(usec_t n, struct shutdownd_command *c) { |
30923233 MS |
104 | char date[FORMAT_TIMESTAMP_MAX]; |
105 | const char *prefix; | |
106 | char *l = NULL; | |
9be9828c LP |
107 | |
108 | assert(c); | |
109 | assert(c->warn_wall); | |
110 | ||
08e4b1c5 LP |
111 | if (n >= c->elapse) |
112 | return; | |
113 | ||
30923233 MS |
114 | if (c->mode == 'H') |
115 | prefix = "The system is going down for system halt at "; | |
116 | else if (c->mode == 'P') | |
117 | prefix = "The system is going down for power-off at "; | |
118 | else if (c->mode == 'r') | |
119 | prefix = "The system is going down for reboot at "; | |
120 | else | |
121 | assert_not_reached("Unknown mode!"); | |
122 | ||
123 | if (asprintf(&l, "%s%s%s%s!", c->wall_message, c->wall_message[0] ? "\n" : "", | |
124 | prefix, format_timestamp(date, sizeof(date), c->elapse)) < 0) | |
125 | log_error("Failed to allocate wall message"); | |
9be9828c | 126 | else { |
30923233 MS |
127 | utmp_wall(l, NULL); |
128 | free(l); | |
9be9828c LP |
129 | } |
130 | } | |
131 | ||
132 | static usec_t when_wall(usec_t n, usec_t elapse) { | |
133 | ||
134 | static const struct { | |
135 | usec_t delay; | |
136 | usec_t interval; | |
137 | } table[] = { | |
138 | { 10 * USEC_PER_MINUTE, USEC_PER_MINUTE }, | |
139 | { USEC_PER_HOUR, 15 * USEC_PER_MINUTE }, | |
140 | { 3 * USEC_PER_HOUR, 30 * USEC_PER_MINUTE } | |
141 | }; | |
142 | ||
143 | usec_t left, sub; | |
144 | unsigned i; | |
145 | ||
146 | /* If the time is already passed, then don't announce */ | |
147 | if (n >= elapse) | |
148 | return 0; | |
149 | ||
150 | left = elapse - n; | |
151 | for (i = 0; i < ELEMENTSOF(table); i++) | |
08e4b1c5 | 152 | if (n + table[i].delay >= elapse) { |
9be9828c | 153 | sub = ((left / table[i].interval) * table[i].interval); |
08e4b1c5 LP |
154 | break; |
155 | } | |
9be9828c LP |
156 | |
157 | if (i >= ELEMENTSOF(table)) | |
158 | sub = ((left / USEC_PER_HOUR) * USEC_PER_HOUR); | |
159 | ||
160 | return elapse > sub ? elapse - sub : 1; | |
161 | } | |
162 | ||
163 | static usec_t when_nologin(usec_t elapse) { | |
164 | return elapse > 5*USEC_PER_MINUTE ? elapse - 5*USEC_PER_MINUTE : 1; | |
165 | } | |
166 | ||
f6144808 LP |
167 | int main(int argc, char *argv[]) { |
168 | enum { | |
169 | FD_SOCKET, | |
9be9828c | 170 | FD_WALL_TIMER, |
f6144808 | 171 | FD_NOLOGIN_TIMER, |
9be9828c | 172 | FD_SHUTDOWN_TIMER, |
f6144808 LP |
173 | _FD_MAX |
174 | }; | |
175 | ||
22f4096c | 176 | int r = EXIT_FAILURE, n_fds; |
f6144808 LP |
177 | struct shutdownd_command c; |
178 | struct pollfd pollfd[_FD_MAX]; | |
9be9828c LP |
179 | bool exec_shutdown = false, unlink_nologin = false, failed = false; |
180 | unsigned i; | |
f6144808 LP |
181 | |
182 | if (getppid() != 1) { | |
183 | log_error("This program should be invoked by init only."); | |
22f4096c | 184 | return EXIT_FAILURE; |
f6144808 LP |
185 | } |
186 | ||
187 | if (argc > 1) { | |
188 | log_error("This program does not take arguments."); | |
22f4096c | 189 | return EXIT_FAILURE; |
f6144808 LP |
190 | } |
191 | ||
192 | log_set_target(LOG_TARGET_SYSLOG_OR_KMSG); | |
193 | log_parse_environment(); | |
2396fb04 | 194 | log_open(); |
f6144808 | 195 | |
4c12626c LP |
196 | umask(0022); |
197 | ||
9be9828c | 198 | if ((n_fds = sd_listen_fds(true)) < 0) { |
f6144808 | 199 | log_error("Failed to read listening file descriptors from environment: %s", strerror(-r)); |
22f4096c | 200 | return EXIT_FAILURE; |
f6144808 LP |
201 | } |
202 | ||
9be9828c | 203 | if (n_fds != 1) { |
f6144808 | 204 | log_error("Need exactly one file descriptor."); |
22f4096c | 205 | return EXIT_FAILURE; |
f6144808 LP |
206 | } |
207 | ||
f6144808 LP |
208 | zero(c); |
209 | zero(pollfd); | |
210 | ||
211 | pollfd[FD_SOCKET].fd = SD_LISTEN_FDS_START; | |
212 | pollfd[FD_SOCKET].events = POLLIN; | |
9be9828c LP |
213 | |
214 | for (i = 0; i < _FD_MAX; i++) { | |
215 | ||
216 | if (i == FD_SOCKET) | |
217 | continue; | |
218 | ||
219 | pollfd[i].events = POLLIN; | |
220 | ||
221 | if ((pollfd[i].fd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) { | |
222 | log_error("timerfd_create(): %m"); | |
da19d5c1 | 223 | failed = true; |
9be9828c LP |
224 | } |
225 | } | |
226 | ||
227 | if (failed) | |
228 | goto finish; | |
f6144808 LP |
229 | |
230 | log_debug("systemd-shutdownd running as pid %lu", (unsigned long) getpid()); | |
231 | ||
232 | sd_notify(false, | |
233 | "READY=1\n" | |
234 | "STATUS=Processing requests..."); | |
235 | ||
236 | do { | |
237 | int k; | |
9be9828c | 238 | usec_t n; |
f6144808 | 239 | |
9be9828c | 240 | if (poll(pollfd, _FD_MAX, -1) < 0) { |
f6144808 LP |
241 | |
242 | if (errno == EAGAIN || errno == EINTR) | |
243 | continue; | |
244 | ||
245 | log_error("poll(): %m"); | |
246 | goto finish; | |
247 | } | |
248 | ||
9be9828c LP |
249 | n = now(CLOCK_REALTIME); |
250 | ||
f6144808 LP |
251 | if (pollfd[FD_SOCKET].revents) { |
252 | ||
253 | if ((k = read_packet(pollfd[FD_SOCKET].fd, &c)) < 0) | |
254 | goto finish; | |
255 | else if (k > 0 && c.elapse > 0) { | |
256 | struct itimerspec its; | |
11620592 | 257 | char date[FORMAT_TIMESTAMP_MAX]; |
f6144808 | 258 | |
9be9828c LP |
259 | if (c.warn_wall) { |
260 | /* Send wall messages every so often */ | |
261 | zero(its); | |
262 | timespec_store(&its.it_value, when_wall(n, c.elapse)); | |
263 | if (timerfd_settime(pollfd[FD_WALL_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) { | |
264 | log_error("timerfd_settime(): %m"); | |
f6144808 LP |
265 | goto finish; |
266 | } | |
267 | ||
9be9828c LP |
268 | /* Warn immediately if less than 15 minutes are left */ |
269 | if (n < c.elapse && | |
270 | n + 15*USEC_PER_MINUTE >= c.elapse) | |
08e4b1c5 | 271 | warn_wall(n, &c); |
9be9828c LP |
272 | } |
273 | ||
f6144808 LP |
274 | /* Disallow logins 5 minutes prior to shutdown */ |
275 | zero(its); | |
9be9828c | 276 | timespec_store(&its.it_value, when_nologin(c.elapse)); |
f6144808 LP |
277 | if (timerfd_settime(pollfd[FD_NOLOGIN_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) { |
278 | log_error("timerfd_settime(): %m"); | |
279 | goto finish; | |
280 | } | |
281 | ||
282 | /* Shutdown after the specified time is reached */ | |
283 | zero(its); | |
284 | timespec_store(&its.it_value, c.elapse); | |
285 | if (timerfd_settime(pollfd[FD_SHUTDOWN_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) { | |
286 | log_error("timerfd_settime(): %m"); | |
287 | goto finish; | |
288 | } | |
289 | ||
f6144808 LP |
290 | sd_notifyf(false, |
291 | "STATUS=Shutting down at %s...", | |
11620592 | 292 | format_timestamp(date, sizeof(date), c.elapse)); |
f6144808 LP |
293 | } |
294 | } | |
295 | ||
9be9828c LP |
296 | if (pollfd[FD_WALL_TIMER].revents) { |
297 | struct itimerspec its; | |
298 | ||
08e4b1c5 | 299 | warn_wall(n, &c); |
9be9828c LP |
300 | flush_fd(pollfd[FD_WALL_TIMER].fd); |
301 | ||
302 | /* Restart timer */ | |
303 | zero(its); | |
304 | timespec_store(&its.it_value, when_wall(n, c.elapse)); | |
305 | if (timerfd_settime(pollfd[FD_WALL_TIMER].fd, TFD_TIMER_ABSTIME, &its, NULL) < 0) { | |
306 | log_error("timerfd_settime(): %m"); | |
307 | goto finish; | |
308 | } | |
309 | } | |
310 | ||
311 | if (pollfd[FD_NOLOGIN_TIMER].revents) { | |
f6144808 LP |
312 | int e; |
313 | ||
db019b8d | 314 | log_info("Creating /run/nologin, blocking further logins..."); |
08e4b1c5 | 315 | |
34ca941c | 316 | if ((e = write_one_line_file_atomic("/run/nologin", "System is going down.")) < 0) |
db019b8d | 317 | log_error("Failed to create /run/nologin: %s", strerror(-e)); |
f6144808 LP |
318 | else |
319 | unlink_nologin = true; | |
320 | ||
9be9828c | 321 | flush_fd(pollfd[FD_NOLOGIN_TIMER].fd); |
f6144808 LP |
322 | } |
323 | ||
9be9828c | 324 | if (pollfd[FD_SHUTDOWN_TIMER].revents) { |
f6144808 LP |
325 | exec_shutdown = true; |
326 | goto finish; | |
327 | } | |
328 | ||
329 | } while (c.elapse > 0); | |
330 | ||
22f4096c | 331 | r = EXIT_SUCCESS; |
f6144808 LP |
332 | |
333 | log_debug("systemd-shutdownd stopped as pid %lu", (unsigned long) getpid()); | |
334 | ||
335 | finish: | |
f6144808 | 336 | |
9be9828c LP |
337 | for (i = 0; i < _FD_MAX; i++) |
338 | if (pollfd[i].fd >= 0) | |
339 | close_nointr_nofail(pollfd[i].fd); | |
f6144808 | 340 | |
16061c20 | 341 | if (unlink_nologin) |
db019b8d | 342 | unlink("/run/nologin"); |
16061c20 | 343 | |
52c00215 | 344 | if (exec_shutdown && !c.dry_run) { |
f6144808 LP |
345 | char sw[3]; |
346 | ||
347 | sw[0] = '-'; | |
348 | sw[1] = c.mode; | |
349 | sw[2] = 0; | |
350 | ||
08e4b1c5 LP |
351 | execl(SYSTEMCTL_BINARY_PATH, |
352 | "shutdown", | |
353 | sw, | |
354 | "now", | |
355 | (c.warn_wall && c.wall_message[0]) ? c.wall_message : | |
356 | (c.warn_wall ? NULL : "--no-wall"), | |
357 | NULL); | |
358 | ||
f6144808 LP |
359 | log_error("Failed to execute /sbin/shutdown: %m"); |
360 | } | |
361 | ||
f6144808 LP |
362 | sd_notify(false, |
363 | "STATUS=Exiting..."); | |
364 | ||
365 | return r; | |
366 | } |