chiark / gitweb /
swap: add only swaps listed in /etc/fstab automatically to swap.target, others should...
[elogind.git] / src / shutdown.c
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
39 #define TIMEOUT_USEC (5 * USEC_PER_SEC)
40 #define FINALIZE_ATTEMPTS 50
41
42 static 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
51 static 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
68 static int killall(int sign) {
69         DIR *dir;
70         struct dirent *d;
71         unsigned int n_processes = 0;
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)
89                         n_processes++;
90                 else
91                         log_warning("Could not kill %d: %m", pid);
92         }
93
94         closedir(dir);
95
96         return n_processes;
97 }
98
99 static int send_signal(int sign) {
100         sigset_t mask, oldmask;
101         usec_t until;
102         int n_processes;
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)
111                 log_warning("Failed kill(-1, SIGSTOP): %m");
112
113         n_processes = killall(sign);
114
115         if (kill(-1, SIGCONT) < 0)
116                 log_warning("Failed kill(-1, SIGCONT): %m");
117
118         if (n_processes <= 0)
119                 goto finish;
120
121         until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
122         for (;;) {
123                 usec_t n = now(CLOCK_MONOTONIC);
124                 for (;;) {
125                         pid_t pid = waitpid(-1, NULL, WNOHANG);
126
127                         if (pid == 0)
128                                 break;
129                         else if (pid < 0 && errno == ECHILD) {
130                                 n_processes = 0;
131                                 goto finish;
132                         }
133
134                         if (--n_processes == 0)
135                                 goto finish;
136                 }
137
138                 if (n >= until)
139                         goto finish;
140
141                 timespec_store(&ts, until - n);
142                 if (sigtimedwait(&mask, NULL, &ts) != SIGCHLD)
143                         if (errno != EAGAIN)
144                                 log_warning("Failed: sigtimedwait did not return SIGCHLD: %m");
145         }
146
147 finish:
148         sigprocmask(SIG_SETMASK, &oldmask, NULL);
149
150         return n_processes;
151 }
152
153 static int rescue_send_signal(int sign) {
154         sigset_t mask, oldmask;
155         usec_t until;
156         struct timespec ts;
157         int r;
158
159         sigemptyset(&mask);
160         sigaddset(&mask, SIGCHLD);
161         if (sigprocmask(SIG_BLOCK, &mask, &oldmask) != 0)
162                 return -errno;
163
164         if (kill(-1, SIGSTOP) < 0)
165                 log_warning("Failed kill(-1, SIGSTOP): %m");
166
167         r = kill(-1, sign);
168         if (r < 0)
169                 log_warning("Failed kill(-1, %d): %m", sign);
170
171         if (kill(-1, SIGCONT) < 0)
172                 log_warning("Failed kill(-1, SIGCONT): %m");
173
174         if (r < 0)
175                 goto finish;
176
177         until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
178         for (;;) {
179                 usec_t n = now(CLOCK_MONOTONIC);
180                 for (;;) {
181                         pid_t pid = waitpid(-1, NULL, WNOHANG);
182                         if (pid == 0)
183                                 break;
184                         else if (pid < 0 && errno == ECHILD)
185                                 goto finish;
186                 }
187
188                 if (n >= until)
189                         goto finish;
190
191                 timespec_store(&ts, until - n);
192                 if (sigtimedwait(&mask, NULL, &ts) != SIGCHLD)
193                         if (errno != EAGAIN)
194                                 log_warning("Failed: sigtimedwait did not return SIGCHLD: %m");
195         }
196
197 finish:
198         sigprocmask(SIG_SETMASK, &oldmask, NULL);
199
200         return r;
201 }
202
203 int main(int argc, char *argv[]) {
204         int cmd, r;
205         unsigned retries;
206         bool need_umount = true, need_swapoff = true, need_loop_detach = true, need_dm_detach = true;
207         bool killed_everbody = false;
208
209         log_parse_environment();
210         log_set_target(LOG_TARGET_CONSOLE); /* syslog will die if not gone yet */
211         log_open();
212
213         if (getpid() != 1) {
214                 log_error("Not executed by init (pid 1).");
215                 r = -EPERM;
216                 goto error;
217         }
218
219         if (argc != 2) {
220                 log_error("Invalid number of arguments.");
221                 r = -EINVAL;
222                 goto error;
223         }
224
225         if (streq(argv[1], "reboot"))
226                 cmd = RB_AUTOBOOT;
227         else if (streq(argv[1], "poweroff"))
228                 cmd = RB_POWER_OFF;
229         else if (streq(argv[1], "halt"))
230                 cmd = RB_HALT_SYSTEM;
231         else if (streq(argv[1], "kexec"))
232                 cmd = LINUX_REBOOT_CMD_KEXEC;
233         else {
234                 log_error("Unknown action '%s'.", argv[1]);
235                 r = -EINVAL;
236                 goto error;
237         }
238
239         /* lock us into memory */
240         if (mlockall(MCL_CURRENT|MCL_FUTURE) != 0)
241                 log_warning("Cannot lock process memory: %m");
242
243         log_info("Sending SIGTERM to processes");
244         r = send_signal(SIGTERM);
245         if (r < 0)
246                 log_warning("Cannot send SIGTERM to all process: %s", strerror(r));
247
248         log_info("Sending SIGKILL to processes");
249         r = send_signal(SIGKILL);
250         if (r < 0)
251                 log_warning("Cannot send SIGKILL to all process: %s", strerror(r));
252
253         /* Unmount all mountpoints, swaps, and loopback devices */
254         for (retries = 0; retries < FINALIZE_ATTEMPTS; retries++) {
255                 bool changed = false;
256
257                 if (need_umount) {
258                         log_info("Unmounting filesystems.");
259                         r = umount_all(&changed);
260                         if (r == 0)
261                                 need_umount = false;
262                         else if (r > 0)
263                                 log_warning("Not all filesystems unmounted, %d left.", r);
264                         else
265                                 log_error("Error unmounting filesystems: %s", strerror(-r));
266                 }
267
268                 if (need_swapoff) {
269                         log_info("Disabling swaps.");
270                         r = swapoff_all(&changed);
271                         if (r == 0)
272                                 need_swapoff = false;
273                         else if (r > 0)
274                                 log_warning("Not all swaps are off, %d left.", r);
275                         else
276                                 log_error("Error turning off swaps: %s", strerror(-r));
277                 }
278
279                 if (need_loop_detach) {
280                         log_info("Detaching loop devices.");
281                         r = loopback_detach_all(&changed);
282                         if (r == 0)
283                                 need_loop_detach = false;
284                         else if (r > 0)
285                                 log_warning("Not all loop devices detached, %d left.", r);
286                         else
287                                 log_error("Error detaching loop devices: %s", strerror(-r));
288                 }
289
290                 if (need_dm_detach) {
291                         log_info("Detaching DM devices.");
292                         r = dm_detach_all(&changed);
293                         if (r == 0)
294                                 need_dm_detach = false;
295                         else if (r > 0)
296                                 log_warning("Not all dm devices detached, %d left.", r);
297                         else
298                                 log_error("Error detaching dm devices: %s", strerror(-r));
299                 }
300
301                 if (!need_umount && !need_swapoff && !need_loop_detach && !need_dm_detach)
302                         /* Yay, done */
303                         break;
304
305                 /* If in this iteration we didn't manage to
306                  * unmount/deactivate anything, we either kill more
307                  * processes, or simply give up */
308                 if (!changed) {
309
310                         if (killed_everbody) {
311                                 /* Hmm, we already killed everybody,
312                                  * let's just give up */
313                                 log_error("Cannot finalize all filesystems and devices, giving up.");
314                                 break;
315                         }
316
317                         log_warning("Cannot finalize filesystems and devices, trying to kill remaining processes.");
318                         rescue_send_signal(SIGTERM);
319                         rescue_send_signal(SIGKILL);
320                         killed_everbody = true;
321                 }
322
323                 log_debug("Couldn't finalize filesystems and devices after %u retries, trying again.", retries+1);
324         }
325
326         if (retries >= FINALIZE_ATTEMPTS)
327                 log_error("Too many interations, giving up.");
328
329         sync();
330
331         if (cmd == LINUX_REBOOT_CMD_KEXEC) {
332                 /* We cheat and exec kexec to avoid doing all its work */
333                 pid_t pid = fork();
334
335                 if (pid < 0)
336                         log_error("Could not fork: %m. Falling back to normal reboot.");
337                 else if (pid > 0) {
338                         wait_for_terminate_and_warn("kexec", pid);
339                         log_warning("kexec failed. Falling back to normal reboot.");
340                 } else {
341                         /* Child */
342                         const char *args[5] = { KEXEC_BINARY_PATH, "-e", "-f", "-x", NULL };
343                         execv(args[0], (char * const *) args);
344                         return EXIT_FAILURE;
345                 }
346
347                 cmd = RB_AUTOBOOT;
348         }
349
350         reboot(cmd);
351         log_error("Failed to invoke reboot(): %m");
352         r = -errno;
353
354   error:
355         sync();
356         log_error("Critical error while doing system shutdown: %s", strerror(-r));
357
358         freeze();
359         return EXIT_FAILURE;
360 }