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