]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shutdown.c
systemctl: always null-terminate the password
[thirdparty/systemd.git] / src / shutdown.c
CommitLineData
b1b2a107
FF
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 ProFUSION embedded systems
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/mman.h>
23#include <sys/types.h>
24#include <sys/reboot.h>
25#include <linux/reboot.h>
26#include <sys/wait.h>
27#include <dirent.h>
28#include <errno.h>
29#include <unistd.h>
30#include <signal.h>
31#include <stdbool.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include "log.h"
36#include "umount.h"
37#include "util.h"
38
567ea02a 39#define TIMEOUT_USEC (5 * USEC_PER_SEC)
b1b2a107 40#define FINALIZE_ATTEMPTS 50
b1b2a107 41
b1b2a107
FF
42static bool ignore_proc(pid_t pid) {
43 if (pid == 1)
44 return true;
45
46 /* TODO: add more ignore rules here: device-mapper, etc */
47
48 return false;
49}
50
51static bool is_kernel_thread(pid_t pid)
52{
53 char buf[PATH_MAX];
54 FILE *f;
55 char c;
56 size_t count;
57
58 snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long)pid);
59 f = fopen(buf, "re");
60 if (!f)
61 return true; /* not really, but has the desired effect */
62
63 count = fread(&c, 1, 1, f);
64 fclose(f);
65 return count != 1;
66}
67
68static int killall(int sign) {
69 DIR *dir;
70 struct dirent *d;
12aad1d0 71 unsigned int n_processes = 0;
b1b2a107
FF
72
73 if ((dir = opendir("/proc")) == NULL)
74 return -errno;
75
76 while ((d = readdir(dir))) {
77 pid_t pid;
78
79 if (parse_pid(d->d_name, &pid) < 0)
80 continue;
81
82 if (is_kernel_thread(pid))
83 continue;
84
85 if (ignore_proc(pid))
86 continue;
87
88 if (kill(pid, sign) == 0)
12aad1d0 89 n_processes++;
b1b2a107
FF
90 else
91 log_warning("Could not kill %d: %m", pid);
92 }
93
94 closedir(dir);
95
12aad1d0 96 return n_processes;
b1b2a107
FF
97}
98
99static int send_signal(int sign) {
100 sigset_t mask, oldmask;
101 usec_t until;
12aad1d0 102 int n_processes;
b1b2a107
FF
103 struct timespec ts;
104
105 assert_se(sigemptyset(&mask) == 0);
106 assert_se(sigaddset(&mask, SIGCHLD) == 0);
107 if (sigprocmask(SIG_BLOCK, &mask, &oldmask) != 0)
108 return -errno;
109
110 if (kill(-1, SIGSTOP) < 0)
ab58e291 111 log_warning("kill(-1, SIGSTOP) failed: %m");
b1b2a107 112
12aad1d0 113 n_processes = killall(sign);
b1b2a107
FF
114
115 if (kill(-1, SIGCONT) < 0)
ab58e291 116 log_warning("kill(-1, SIGCONT) failed: %m");
b1b2a107 117
12aad1d0 118 if (n_processes <= 0)
b1b2a107
FF
119 goto finish;
120
121 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
122 for (;;) {
ab58e291 123 int k;
b1b2a107 124 usec_t n = now(CLOCK_MONOTONIC);
ab58e291 125
b1b2a107
FF
126 for (;;) {
127 pid_t pid = waitpid(-1, NULL, WNOHANG);
12aad1d0 128
b1b2a107
FF
129 if (pid == 0)
130 break;
131 else if (pid < 0 && errno == ECHILD) {
12aad1d0 132 n_processes = 0;
b1b2a107
FF
133 goto finish;
134 }
135
12aad1d0 136 if (--n_processes == 0)
b1b2a107
FF
137 goto finish;
138 }
139
140 if (n >= until)
141 goto finish;
142
143 timespec_store(&ts, until - n);
ab58e291
LP
144 if ((k = sigtimedwait(&mask, NULL, &ts)) != SIGCHLD) {
145 if (k >= 0)
146 log_warning("sigtimedwait() returned unexpected signal.");
147 if (k < 0 && errno != EAGAIN)
148 log_warning("sigtimedwait() failed: %m");
149 }
b1b2a107
FF
150 }
151
152finish:
153 sigprocmask(SIG_SETMASK, &oldmask, NULL);
154
12aad1d0 155 return n_processes;
b1b2a107
FF
156}
157
158static int rescue_send_signal(int sign) {
159 sigset_t mask, oldmask;
160 usec_t until;
161 struct timespec ts;
162 int r;
163
164 sigemptyset(&mask);
165 sigaddset(&mask, SIGCHLD);
166 if (sigprocmask(SIG_BLOCK, &mask, &oldmask) != 0)
167 return -errno;
168
169 if (kill(-1, SIGSTOP) < 0)
ab58e291 170 log_warning("kill(-1, SIGSTOP) failed: %m");
b1b2a107
FF
171
172 r = kill(-1, sign);
173 if (r < 0)
ab58e291 174 log_warning("kill(-1, %d) failed: %m", sign);
b1b2a107
FF
175
176 if (kill(-1, SIGCONT) < 0)
ab58e291 177 log_warning("kill(-1, SIGCONT) failed: %m");
b1b2a107
FF
178
179 if (r < 0)
180 goto finish;
181
182 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
183 for (;;) {
ab58e291 184 int k;
b1b2a107 185 usec_t n = now(CLOCK_MONOTONIC);
ab58e291 186
b1b2a107
FF
187 for (;;) {
188 pid_t pid = waitpid(-1, NULL, WNOHANG);
189 if (pid == 0)
190 break;
191 else if (pid < 0 && errno == ECHILD)
192 goto finish;
193 }
194
195 if (n >= until)
196 goto finish;
197
198 timespec_store(&ts, until - n);
ab58e291
LP
199 if ((k = sigtimedwait(&mask, NULL, &ts)) != SIGCHLD) {
200 if (k >= 0)
201 log_warning("sigtimedwait() returned unexpected signal.");
202 if (k < 0 && errno != EAGAIN)
203 log_warning("sigtimedwait() failed: %m");
204 }
b1b2a107
FF
205 }
206
207finish:
208 sigprocmask(SIG_SETMASK, &oldmask, NULL);
209
210 return r;
211}
212
b1b2a107 213int main(int argc, char *argv[]) {
12aad1d0
LP
214 int cmd, r;
215 unsigned retries;
d48141ba 216 bool need_umount = true, need_swapoff = true, need_loop_detach = true, need_dm_detach = true;
12aad1d0 217 bool killed_everbody = false;
b1b2a107
FF
218
219 log_parse_environment();
2ca1b422 220 log_set_target(LOG_TARGET_CONSOLE); /* syslog will die if not gone yet */
b1b2a107
FF
221 log_open();
222
223 if (getpid() != 1) {
567ea02a 224 log_error("Not executed by init (pid 1).");
b1b2a107
FF
225 r = -EPERM;
226 goto error;
227 }
228
229 if (argc != 2) {
230 log_error("Invalid number of arguments.");
231 r = -EINVAL;
232 goto error;
233 }
234
235 if (streq(argv[1], "reboot"))
236 cmd = RB_AUTOBOOT;
237 else if (streq(argv[1], "poweroff"))
238 cmd = RB_POWER_OFF;
239 else if (streq(argv[1], "halt"))
240 cmd = RB_HALT_SYSTEM;
241 else if (streq(argv[1], "kexec"))
242 cmd = LINUX_REBOOT_CMD_KEXEC;
243 else {
244 log_error("Unknown action '%s'.", argv[1]);
245 r = -EINVAL;
246 goto error;
247 }
248
249 /* lock us into memory */
250 if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0)
251 log_warning("Cannot lock process memory: %m");
252
ab58e291 253 log_info("Sending SIGTERM to remaining processes...");
b1b2a107
FF
254 r = send_signal(SIGTERM);
255 if (r < 0)
ab58e291 256 log_warning("Failed to send SIGTERM to remaining processes: %s", strerror(r));
b1b2a107 257
ab58e291 258 log_info("Sending SIGKILL to remaining processes...");
b1b2a107
FF
259 r = send_signal(SIGKILL);
260 if (r < 0)
ab58e291 261 log_warning("Failed to send SIGKILL to remaining processes: %s", strerror(r));
b1b2a107 262
567ea02a 263 /* Unmount all mountpoints, swaps, and loopback devices */
12aad1d0
LP
264 for (retries = 0; retries < FINALIZE_ATTEMPTS; retries++) {
265 bool changed = false;
266
b1b2a107 267 if (need_umount) {
ab58e291 268 log_info("Unmounting file systems.");
12aad1d0 269 r = umount_all(&changed);
b1b2a107
FF
270 if (r == 0)
271 need_umount = false;
272 else if (r > 0)
ab58e291 273 log_info("Not all file systems unmounted, %d left.", r);
b1b2a107 274 else
ab58e291 275 log_error("Failed to unmount file systems: %s", strerror(-r));
b1b2a107
FF
276 }
277
278 if (need_swapoff) {
279 log_info("Disabling swaps.");
12aad1d0 280 r = swapoff_all(&changed);
b1b2a107
FF
281 if (r == 0)
282 need_swapoff = false;
283 else if (r > 0)
ab58e291 284 log_info("Not all swaps are turned off, %d left.", r);
b1b2a107 285 else
ab58e291 286 log_error("Failed to turn off swaps: %s", strerror(-r));
b1b2a107
FF
287 }
288
289 if (need_loop_detach) {
290 log_info("Detaching loop devices.");
12aad1d0 291 r = loopback_detach_all(&changed);
b1b2a107
FF
292 if (r == 0)
293 need_loop_detach = false;
294 else if (r > 0)
ab58e291 295 log_info("Not all loop devices detached, %d left.", r);
b1b2a107 296 else
ab58e291 297 log_error("Failed to detach loop devices: %s", strerror(-r));
d48141ba 298 }
b1b2a107 299
d48141ba
LP
300 if (need_dm_detach) {
301 log_info("Detaching DM devices.");
12aad1d0 302 r = dm_detach_all(&changed);
d48141ba
LP
303 if (r == 0)
304 need_dm_detach = false;
305 else if (r > 0)
ab58e291 306 log_warning("Not all DM devices detached, %d left.", r);
d48141ba 307 else
ab58e291 308 log_error("Failed to detach DM devices: %s", strerror(-r));
b1b2a107
FF
309 }
310
12aad1d0
LP
311 if (!need_umount && !need_swapoff && !need_loop_detach && !need_dm_detach)
312 /* Yay, done */
313 break;
b1b2a107 314
12aad1d0
LP
315 /* If in this iteration we didn't manage to
316 * unmount/deactivate anything, we either kill more
317 * processes, or simply give up */
318 if (!changed) {
b1b2a107 319
12aad1d0
LP
320 if (killed_everbody) {
321 /* Hmm, we already killed everybody,
322 * let's just give up */
ab58e291 323 log_error("Cannot finalize remaining file systems and devices, giving up.");
d37fb98b 324 break;
b1b2a107 325 }
12aad1d0 326
ab58e291 327 log_warning("Cannot finalize remaining file systems and devices, trying to kill remaining processes.");
12aad1d0
LP
328 rescue_send_signal(SIGTERM);
329 rescue_send_signal(SIGKILL);
330 killed_everbody = true;
331 }
332
ab58e291 333 log_debug("Couldn't finalize remaining file systems and devices after %u retries, trying again.", retries+1);
b1b2a107
FF
334 }
335
12aad1d0
LP
336 if (retries >= FINALIZE_ATTEMPTS)
337 log_error("Too many interations, giving up.");
338
83cc030f
LP
339 execute_directory(SYSTEM_SHUTDOWN_PATH, NULL, NULL);
340
b1b2a107
FF
341 sync();
342
343 if (cmd == LINUX_REBOOT_CMD_KEXEC) {
12aad1d0 344 /* We cheat and exec kexec to avoid doing all its work */
b1b2a107 345 pid_t pid = fork();
12aad1d0 346
e61cd186
LP
347 if (pid < 0)
348 log_error("Could not fork: %m. Falling back to normal reboot.");
349 else if (pid > 0) {
350 wait_for_terminate_and_warn("kexec", pid);
351 log_warning("kexec failed. Falling back to normal reboot.");
b1b2a107 352 } else {
e61cd186 353 /* Child */
f8d0ceb4 354 const char *args[3] = { "/sbin/kexec", "-e", NULL };
b1b2a107
FF
355 execv(args[0], (char * const *) args);
356 return EXIT_FAILURE;
357 }
e61cd186
LP
358
359 cmd = RB_AUTOBOOT;
b1b2a107
FF
360 }
361
362 reboot(cmd);
e61cd186
LP
363 log_error("Failed to invoke reboot(): %m");
364 r = -errno;
b1b2a107
FF
365
366 error:
e61cd186
LP
367 log_error("Critical error while doing system shutdown: %s", strerror(-r));
368
b1b2a107 369 freeze();
3c14d26c 370 return EXIT_FAILURE;
b1b2a107 371}