chiark / gitweb /
shutdown: correctly wait for processes we killed in the killall spree
[elogind.git] / src / core / killall.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 Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/wait.h>
23 #include <signal.h>
24 #include <errno.h>
25 #include <unistd.h>
26
27 #include "util.h"
28 #include "def.h"
29 #include "killall.h"
30 #include "set.h"
31
32 #define TIMEOUT_USEC (10 * USEC_PER_SEC)
33
34 static bool ignore_proc(pid_t pid) {
35         char buf[PATH_MAX];
36         FILE *f;
37         char c;
38         size_t count;
39         uid_t uid;
40         int r;
41
42         /* We are PID 1, let's not commit suicide */
43         if (pid == 1)
44                 return true;
45
46         r = get_process_uid(pid, &uid);
47         if (r < 0)
48                 return true; /* not really, but better safe than sorry */
49
50         /* Non-root processes otherwise are always subject to be killed */
51         if (uid != 0)
52                 return false;
53
54         snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long) pid);
55         char_array_0(buf);
56
57         f = fopen(buf, "re");
58         if (!f)
59                 return true; /* not really, but has the desired effect */
60
61         count = fread(&c, 1, 1, f);
62         fclose(f);
63
64         /* Kernel threads have an empty cmdline */
65         if (count <= 0)
66                 return true;
67
68         /* Processes with argv[0][0] = '@' we ignore from the killing
69          * spree.
70          *
71          * http://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons */
72         if (count == 1 && c == '@')
73                 return true;
74
75         return false;
76 }
77
78 static void wait_for_children(Set *pids, sigset_t *mask) {
79         usec_t until;
80
81         assert(mask);
82
83         if (set_isempty(pids))
84                 return;
85
86         until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
87         for (;;) {
88                 struct timespec ts;
89                 int k;
90                 usec_t n;
91                 void *p;
92                 Iterator i;
93
94                 /* First, let the kernel inform us about killed
95                  * children. Most processes will probably be our
96                  * children, but some are not (might be our
97                  * grandchildren instead...). */
98                 for (;;) {
99                         pid_t pid;
100
101                         pid = waitpid(-1, NULL, WNOHANG);
102                         if (pid == 0)
103                                 break;
104                         if (pid < 0) {
105                                 if (errno == ECHILD)
106                                         break;
107
108                                 log_error("waitpid() failed: %m");
109                                 return;
110                         }
111
112                         set_remove(pids, ULONG_TO_PTR(pid));
113                 }
114
115                 /* Now explicitly check who might be remaining, who
116                  * might not be our child. */
117                 SET_FOREACH(p, pids, i) {
118
119                         /* We misuse getpgid as a check whether a
120                          * process still exists. */
121                         if (getpgid((pid_t) PTR_TO_ULONG(p)) >= 0)
122                                 continue;
123
124                         if (errno != ESRCH)
125                                 continue;
126
127                         set_remove(pids, p);
128                 }
129
130                 if (set_isempty(pids))
131                         return;
132
133                 n = now(CLOCK_MONOTONIC);
134                 if (n >= until)
135                         return;
136
137                 timespec_store(&ts, until - n);
138                 k = sigtimedwait(mask, NULL, &ts);
139                 if (k != SIGCHLD) {
140
141                         if (k < 0 && errno != EAGAIN) {
142                                 log_error("sigtimedwait() failed: %m");
143                                 return;
144                         }
145
146                         if (k >= 0)
147                                 log_warning("sigtimedwait() returned unexpected signal.");
148                 }
149         }
150 }
151
152 static int killall(int sig, Set *pids) {
153         _cleanup_closedir_ DIR *dir = NULL;
154         struct dirent *d;
155
156         dir = opendir("/proc");
157         if (!dir)
158                 return -errno;
159
160         while ((d = readdir(dir))) {
161                 pid_t pid;
162
163                 if (d->d_type != DT_DIR &&
164                     d->d_type != DT_UNKNOWN)
165                         continue;
166
167                 if (parse_pid(d->d_name, &pid) < 0)
168                         continue;
169
170                 if (ignore_proc(pid))
171                         continue;
172
173                 if (sig == SIGKILL) {
174                         _cleanup_free_ char *s;
175
176                         get_process_comm(pid, &s);
177                         log_notice("Sending SIGKILL to PID %lu (%s).", (unsigned long) pid, strna(s));
178                 }
179
180                 if (kill(pid, sig) >= 0) {
181                         if (pids)
182                                 set_put(pids, ULONG_TO_PTR((unsigned long) pid));
183                 } else if (errno != ENOENT)
184                         log_warning("Could not kill %d: %m", pid);
185         }
186
187         return set_size(pids);
188 }
189
190 void broadcast_signal(int sig, bool wait_for_exit) {
191         sigset_t mask, oldmask;
192         Set *pids;
193
194         if (wait_for_exit)
195                 pids = set_new(trivial_hash_func, trivial_compare_func);
196
197         assert_se(sigemptyset(&mask) == 0);
198         assert_se(sigaddset(&mask, SIGCHLD) == 0);
199         assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
200
201         if (kill(-1, SIGSTOP) < 0 && errno != ESRCH)
202                 log_warning("kill(-1, SIGSTOP) failed: %m");
203
204         killall(sig, pids);
205
206         if (kill(-1, SIGCONT) < 0 && errno != ESRCH)
207                 log_warning("kill(-1, SIGCONT) failed: %m");
208
209         if (wait_for_exit)
210                 wait_for_children(pids, &mask);
211
212         assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
213
214         set_free(pids);
215 }