]>
Commit | Line | Data |
---|---|---|
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> | |
7cb1094a HH |
27 | #include <sys/types.h> |
28 | #include <sys/stat.h> | |
29 | #include <sys/mount.h> | |
30 | #include <sys/syscall.h> | |
31 | #include <fcntl.h> | |
b1b2a107 FF |
32 | #include <dirent.h> |
33 | #include <errno.h> | |
34 | #include <unistd.h> | |
35 | #include <signal.h> | |
36 | #include <stdbool.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | ||
7cb1094a | 40 | #include "missing.h" |
b1b2a107 FF |
41 | #include "log.h" |
42 | #include "umount.h" | |
43 | #include "util.h" | |
44 | ||
567ea02a | 45 | #define TIMEOUT_USEC (5 * USEC_PER_SEC) |
b1b2a107 | 46 | #define FINALIZE_ATTEMPTS 50 |
b1b2a107 | 47 | |
b1b2a107 FF |
48 | static bool ignore_proc(pid_t pid) { |
49 | if (pid == 1) | |
50 | return true; | |
51 | ||
52 | /* TODO: add more ignore rules here: device-mapper, etc */ | |
53 | ||
54 | return false; | |
55 | } | |
56 | ||
57 | static bool is_kernel_thread(pid_t pid) | |
58 | { | |
59 | char buf[PATH_MAX]; | |
60 | FILE *f; | |
61 | char c; | |
62 | size_t count; | |
63 | ||
64 | snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long)pid); | |
65 | f = fopen(buf, "re"); | |
66 | if (!f) | |
67 | return true; /* not really, but has the desired effect */ | |
68 | ||
69 | count = fread(&c, 1, 1, f); | |
70 | fclose(f); | |
71 | return count != 1; | |
72 | } | |
73 | ||
74 | static int killall(int sign) { | |
75 | DIR *dir; | |
76 | struct dirent *d; | |
12aad1d0 | 77 | unsigned int n_processes = 0; |
b1b2a107 FF |
78 | |
79 | if ((dir = opendir("/proc")) == NULL) | |
80 | return -errno; | |
81 | ||
82 | while ((d = readdir(dir))) { | |
83 | pid_t pid; | |
84 | ||
85 | if (parse_pid(d->d_name, &pid) < 0) | |
86 | continue; | |
87 | ||
88 | if (is_kernel_thread(pid)) | |
89 | continue; | |
90 | ||
91 | if (ignore_proc(pid)) | |
92 | continue; | |
93 | ||
94 | if (kill(pid, sign) == 0) | |
12aad1d0 | 95 | n_processes++; |
b1b2a107 FF |
96 | else |
97 | log_warning("Could not kill %d: %m", pid); | |
98 | } | |
99 | ||
100 | closedir(dir); | |
101 | ||
12aad1d0 | 102 | return n_processes; |
b1b2a107 FF |
103 | } |
104 | ||
40e85d00 | 105 | static void wait_for_children(int n_processes, sigset_t *mask) { |
b1b2a107 | 106 | usec_t until; |
b1b2a107 | 107 | |
40e85d00 | 108 | assert(mask); |
b1b2a107 FF |
109 | |
110 | until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC; | |
111 | for (;;) { | |
40e85d00 | 112 | struct timespec ts; |
ab58e291 | 113 | int k; |
40e85d00 | 114 | usec_t n; |
ab58e291 | 115 | |
b1b2a107 FF |
116 | for (;;) { |
117 | pid_t pid = waitpid(-1, NULL, WNOHANG); | |
12aad1d0 | 118 | |
b1b2a107 FF |
119 | if (pid == 0) |
120 | break; | |
b1b2a107 | 121 | |
40e85d00 LP |
122 | if (pid < 0 && errno == ECHILD) |
123 | return; | |
124 | ||
125 | if (n_processes > 0) | |
126 | if (--n_processes == 0) | |
127 | return; | |
b1b2a107 FF |
128 | } |
129 | ||
40e85d00 | 130 | n = now(CLOCK_MONOTONIC); |
b1b2a107 | 131 | if (n >= until) |
40e85d00 | 132 | return; |
b1b2a107 FF |
133 | |
134 | timespec_store(&ts, until - n); | |
40e85d00 LP |
135 | |
136 | if ((k = sigtimedwait(mask, NULL, &ts)) != SIGCHLD) { | |
137 | ||
138 | if (k < 0 && errno != EAGAIN) { | |
139 | log_error("sigtimedwait() failed: %m"); | |
140 | return; | |
141 | } | |
142 | ||
ab58e291 LP |
143 | if (k >= 0) |
144 | log_warning("sigtimedwait() returned unexpected signal."); | |
ab58e291 | 145 | } |
b1b2a107 | 146 | } |
40e85d00 LP |
147 | } |
148 | ||
149 | static void send_signal(int sign) { | |
150 | sigset_t mask, oldmask; | |
151 | int n_processes; | |
152 | ||
153 | assert_se(sigemptyset(&mask) == 0); | |
154 | assert_se(sigaddset(&mask, SIGCHLD) == 0); | |
155 | assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0); | |
156 | ||
157 | if (kill(-1, SIGSTOP) < 0 && errno != ESRCH) | |
158 | log_warning("kill(-1, SIGSTOP) failed: %m"); | |
159 | ||
160 | n_processes = killall(sign); | |
161 | ||
162 | if (kill(-1, SIGCONT) < 0 && errno != ESRCH) | |
163 | log_warning("kill(-1, SIGCONT) failed: %m"); | |
164 | ||
165 | if (n_processes <= 0) | |
166 | goto finish; | |
167 | ||
168 | wait_for_children(n_processes, &mask); | |
b1b2a107 FF |
169 | |
170 | finish: | |
171 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | |
b1b2a107 FF |
172 | } |
173 | ||
40e85d00 | 174 | static void ultimate_send_signal(int sign) { |
b1b2a107 | 175 | sigset_t mask, oldmask; |
b1b2a107 FF |
176 | int r; |
177 | ||
40e85d00 LP |
178 | assert_se(sigemptyset(&mask) == 0); |
179 | assert_se(sigaddset(&mask, SIGCHLD) == 0); | |
180 | assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0); | |
b1b2a107 | 181 | |
40e85d00 | 182 | if (kill(-1, SIGSTOP) < 0 && errno != ESRCH) |
ab58e291 | 183 | log_warning("kill(-1, SIGSTOP) failed: %m"); |
b1b2a107 FF |
184 | |
185 | r = kill(-1, sign); | |
40e85d00 LP |
186 | if (r < 0 && errno != ESRCH) |
187 | log_warning("kill(-1, %s) failed: %m", signal_to_string(sign)); | |
b1b2a107 | 188 | |
40e85d00 | 189 | if (kill(-1, SIGCONT) < 0 && errno != ESRCH) |
ab58e291 | 190 | log_warning("kill(-1, SIGCONT) failed: %m"); |
b1b2a107 FF |
191 | |
192 | if (r < 0) | |
193 | goto finish; | |
194 | ||
40e85d00 | 195 | wait_for_children(0, &mask); |
b1b2a107 FF |
196 | |
197 | finish: | |
198 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | |
b1b2a107 FF |
199 | } |
200 | ||
89d471d5 LP |
201 | static int prepare_new_root(void) { |
202 | static const char dirs[] = | |
203 | "/run/initramfs/oldroot\0" | |
204 | "/run/initramfs/proc\0" | |
205 | "/run/initramfs/sys\0" | |
206 | "/run/initramfs/dev\0" | |
207 | "/run/initramfs/run\0"; | |
208 | ||
209 | const char *dir; | |
210 | ||
211 | if (mount("/run/initramfs", "/run/initramfs", NULL, MS_BIND, NULL) < 0) { | |
212 | log_error("Failed to mount bind /run/initramfs on /run/initramfs: %m"); | |
213 | return -errno; | |
214 | } | |
215 | ||
216 | if (mount(NULL, "/run/initramfs", NULL, MS_PRIVATE, NULL) < 0) { | |
217 | log_error("Failed to make /run/initramfs private mount: %m"); | |
218 | return -errno; | |
219 | } | |
220 | ||
221 | NULSTR_FOREACH(dir, dirs) | |
222 | if (mkdir_p(dir, 0755) < 0 && errno != EEXIST) { | |
223 | log_error("Failed to mkdir %s: %m", dir); | |
224 | return -errno; | |
7cb1094a | 225 | } |
89d471d5 LP |
226 | |
227 | if (mount("/sys", "/run/initramfs/sys", NULL, MS_BIND, NULL) < 0) { | |
228 | log_error("Failed to mount bind /sys on /run/initramfs/sys: %m"); | |
229 | return -errno; | |
230 | } | |
231 | ||
232 | if (mount("/proc", "/run/initramfs/proc", NULL, MS_BIND, NULL) < 0) { | |
233 | log_error("Failed to mount bind /proc on /run/initramfs/proc: %m"); | |
234 | return -errno; | |
235 | } | |
236 | ||
237 | if (mount("/dev", "/run/initramfs/dev", NULL, MS_BIND, NULL) < 0) { | |
238 | log_error("Failed to mount bind /dev on /run/initramfs/dev: %m"); | |
239 | return -errno; | |
7cb1094a HH |
240 | } |
241 | ||
89d471d5 LP |
242 | if (mount("/run", "/run/initramfs/run", NULL, MS_BIND, NULL) < 0) { |
243 | log_error("Failed to mount bind /run on /run/initramfs/run: %m"); | |
244 | return -errno; | |
245 | } | |
246 | ||
247 | return 0; | |
7cb1094a HH |
248 | } |
249 | ||
89d471d5 | 250 | static int pivot_to_new_root(void) { |
7cb1094a | 251 | int fd; |
89d471d5 | 252 | |
7cb1094a HH |
253 | chdir("/run/initramfs"); |
254 | ||
255 | /* | |
256 | In case some evil process made "/" MS_SHARED | |
257 | It works for pivot_root, but the ref count for the root device | |
258 | is not decreasing :-/ | |
259 | */ | |
89d471d5 LP |
260 | if (mount(NULL, "/", NULL, MS_PRIVATE, NULL) < 0) { |
261 | log_error("Failed to make \"/\" private mount %m"); | |
262 | return -errno; | |
7cb1094a HH |
263 | } |
264 | ||
89d471d5 | 265 | if (pivot_root(".", "oldroot") < 0) { |
7cb1094a | 266 | log_error("pivot failed: %m"); |
89d471d5 LP |
267 | /* only chroot if pivot root succeded */ |
268 | return -errno; | |
7cb1094a | 269 | } |
89d471d5 | 270 | |
7cb1094a | 271 | chroot("."); |
89d471d5 LP |
272 | log_info("Successfully changed into root pivot."); |
273 | ||
274 | fd = open("/dev/console", O_RDWR); | |
275 | if (fd < 0) | |
276 | log_error("Failed to open /dev/console: %m"); | |
277 | else { | |
278 | make_stdio(fd); | |
bccc1d88 LP |
279 | |
280 | /* Initialize the controlling terminal */ | |
281 | setsid(); | |
282 | ioctl(STDIN_FILENO, TIOCSCTTY, NULL); | |
89d471d5 LP |
283 | } |
284 | ||
285 | return 0; | |
7cb1094a HH |
286 | } |
287 | ||
b1b2a107 | 288 | int main(int argc, char *argv[]) { |
12aad1d0 LP |
289 | int cmd, r; |
290 | unsigned retries; | |
d48141ba | 291 | bool need_umount = true, need_swapoff = true, need_loop_detach = true, need_dm_detach = true; |
40e85d00 | 292 | bool killed_everbody = false, in_container; |
b1b2a107 FF |
293 | |
294 | log_parse_environment(); | |
2ca1b422 | 295 | log_set_target(LOG_TARGET_CONSOLE); /* syslog will die if not gone yet */ |
b1b2a107 FF |
296 | log_open(); |
297 | ||
298 | if (getpid() != 1) { | |
567ea02a | 299 | log_error("Not executed by init (pid 1)."); |
b1b2a107 FF |
300 | r = -EPERM; |
301 | goto error; | |
302 | } | |
303 | ||
304 | if (argc != 2) { | |
305 | log_error("Invalid number of arguments."); | |
306 | r = -EINVAL; | |
307 | goto error; | |
308 | } | |
309 | ||
40e85d00 LP |
310 | in_container = detect_container(NULL) > 0; |
311 | ||
b1b2a107 FF |
312 | if (streq(argv[1], "reboot")) |
313 | cmd = RB_AUTOBOOT; | |
314 | else if (streq(argv[1], "poweroff")) | |
315 | cmd = RB_POWER_OFF; | |
316 | else if (streq(argv[1], "halt")) | |
317 | cmd = RB_HALT_SYSTEM; | |
318 | else if (streq(argv[1], "kexec")) | |
319 | cmd = LINUX_REBOOT_CMD_KEXEC; | |
320 | else { | |
321 | log_error("Unknown action '%s'.", argv[1]); | |
322 | r = -EINVAL; | |
323 | goto error; | |
324 | } | |
325 | ||
326 | /* lock us into memory */ | |
327 | if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0) | |
328 | log_warning("Cannot lock process memory: %m"); | |
329 | ||
ab58e291 | 330 | log_info("Sending SIGTERM to remaining processes..."); |
40e85d00 | 331 | send_signal(SIGTERM); |
b1b2a107 | 332 | |
ab58e291 | 333 | log_info("Sending SIGKILL to remaining processes..."); |
40e85d00 LP |
334 | send_signal(SIGKILL); |
335 | ||
336 | if (in_container) | |
337 | need_swapoff = false; | |
b1b2a107 | 338 | |
567ea02a | 339 | /* Unmount all mountpoints, swaps, and loopback devices */ |
12aad1d0 LP |
340 | for (retries = 0; retries < FINALIZE_ATTEMPTS; retries++) { |
341 | bool changed = false; | |
342 | ||
b1b2a107 | 343 | if (need_umount) { |
ab58e291 | 344 | log_info("Unmounting file systems."); |
12aad1d0 | 345 | r = umount_all(&changed); |
b1b2a107 FF |
346 | if (r == 0) |
347 | need_umount = false; | |
348 | else if (r > 0) | |
ab58e291 | 349 | log_info("Not all file systems unmounted, %d left.", r); |
b1b2a107 | 350 | else |
ab58e291 | 351 | log_error("Failed to unmount file systems: %s", strerror(-r)); |
b1b2a107 FF |
352 | } |
353 | ||
354 | if (need_swapoff) { | |
355 | log_info("Disabling swaps."); | |
12aad1d0 | 356 | r = swapoff_all(&changed); |
b1b2a107 FF |
357 | if (r == 0) |
358 | need_swapoff = false; | |
359 | else if (r > 0) | |
ab58e291 | 360 | log_info("Not all swaps are turned off, %d left.", r); |
b1b2a107 | 361 | else |
ab58e291 | 362 | log_error("Failed to turn off swaps: %s", strerror(-r)); |
b1b2a107 FF |
363 | } |
364 | ||
365 | if (need_loop_detach) { | |
366 | log_info("Detaching loop devices."); | |
12aad1d0 | 367 | r = loopback_detach_all(&changed); |
b1b2a107 FF |
368 | if (r == 0) |
369 | need_loop_detach = false; | |
370 | else if (r > 0) | |
ab58e291 | 371 | log_info("Not all loop devices detached, %d left.", r); |
b1b2a107 | 372 | else |
ab58e291 | 373 | log_error("Failed to detach loop devices: %s", strerror(-r)); |
d48141ba | 374 | } |
b1b2a107 | 375 | |
d48141ba LP |
376 | if (need_dm_detach) { |
377 | log_info("Detaching DM devices."); | |
12aad1d0 | 378 | r = dm_detach_all(&changed); |
d48141ba LP |
379 | if (r == 0) |
380 | need_dm_detach = false; | |
381 | else if (r > 0) | |
ab58e291 | 382 | log_warning("Not all DM devices detached, %d left.", r); |
d48141ba | 383 | else |
ab58e291 | 384 | log_error("Failed to detach DM devices: %s", strerror(-r)); |
b1b2a107 FF |
385 | } |
386 | ||
12aad1d0 LP |
387 | if (!need_umount && !need_swapoff && !need_loop_detach && !need_dm_detach) |
388 | /* Yay, done */ | |
389 | break; | |
b1b2a107 | 390 | |
12aad1d0 LP |
391 | /* If in this iteration we didn't manage to |
392 | * unmount/deactivate anything, we either kill more | |
393 | * processes, or simply give up */ | |
394 | if (!changed) { | |
b1b2a107 | 395 | |
12aad1d0 LP |
396 | if (killed_everbody) { |
397 | /* Hmm, we already killed everybody, | |
398 | * let's just give up */ | |
ab58e291 | 399 | log_error("Cannot finalize remaining file systems and devices, giving up."); |
d37fb98b | 400 | break; |
b1b2a107 | 401 | } |
12aad1d0 | 402 | |
ab58e291 | 403 | log_warning("Cannot finalize remaining file systems and devices, trying to kill remaining processes."); |
40e85d00 LP |
404 | ultimate_send_signal(SIGTERM); |
405 | ultimate_send_signal(SIGKILL); | |
12aad1d0 LP |
406 | killed_everbody = true; |
407 | } | |
408 | ||
ab58e291 | 409 | log_debug("Couldn't finalize remaining file systems and devices after %u retries, trying again.", retries+1); |
b1b2a107 FF |
410 | } |
411 | ||
12aad1d0 | 412 | if (retries >= FINALIZE_ATTEMPTS) |
35b8ca3a | 413 | log_error("Too many iterations, giving up."); |
12aad1d0 | 414 | |
83cc030f LP |
415 | execute_directory(SYSTEM_SHUTDOWN_PATH, NULL, NULL); |
416 | ||
40e85d00 LP |
417 | /* If we are in a container, just exit, this will kill our |
418 | * container for good. */ | |
f41de959 LP |
419 | if (in_container) { |
420 | log_error("Exiting container."); | |
40e85d00 | 421 | exit(0); |
f41de959 | 422 | } |
40e85d00 | 423 | |
7cb1094a | 424 | if (access("/run/initramfs/shutdown", X_OK) == 0) { |
89d471d5 LP |
425 | |
426 | if (prepare_new_root() >= 0 && | |
427 | pivot_to_new_root() >= 0) { | |
428 | execv("/shutdown", argv); | |
7cb1094a HH |
429 | log_error("Failed to execute shutdown binary: %m"); |
430 | } | |
431 | } | |
432 | ||
89d471d5 LP |
433 | sync(); |
434 | ||
b1b2a107 | 435 | if (cmd == LINUX_REBOOT_CMD_KEXEC) { |
12aad1d0 | 436 | /* We cheat and exec kexec to avoid doing all its work */ |
b1b2a107 | 437 | pid_t pid = fork(); |
12aad1d0 | 438 | |
e61cd186 LP |
439 | if (pid < 0) |
440 | log_error("Could not fork: %m. Falling back to normal reboot."); | |
441 | else if (pid > 0) { | |
442 | wait_for_terminate_and_warn("kexec", pid); | |
443 | log_warning("kexec failed. Falling back to normal reboot."); | |
b1b2a107 | 444 | } else { |
e61cd186 | 445 | /* Child */ |
f8d0ceb4 | 446 | const char *args[3] = { "/sbin/kexec", "-e", NULL }; |
b1b2a107 FF |
447 | execv(args[0], (char * const *) args); |
448 | return EXIT_FAILURE; | |
449 | } | |
e61cd186 LP |
450 | |
451 | cmd = RB_AUTOBOOT; | |
b1b2a107 FF |
452 | } |
453 | ||
454 | reboot(cmd); | |
e61cd186 LP |
455 | log_error("Failed to invoke reboot(): %m"); |
456 | r = -errno; | |
b1b2a107 FF |
457 | |
458 | error: | |
e61cd186 LP |
459 | log_error("Critical error while doing system shutdown: %s", strerror(-r)); |
460 | ||
b1b2a107 | 461 | freeze(); |
3c14d26c | 462 | return EXIT_FAILURE; |
b1b2a107 | 463 | } |