]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/killall.c
Merge pull request #2440 from poettering/journal-fix
[thirdparty/systemd.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 <errno.h>
23 #include <signal.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26
27 #include "alloc-util.h"
28 #include "fd-util.h"
29 #include "formats-util.h"
30 #include "killall.h"
31 #include "parse-util.h"
32 #include "process-util.h"
33 #include "set.h"
34 #include "string-util.h"
35 #include "terminal-util.h"
36 #include "util.h"
37
38 #define TIMEOUT_USEC (10 * USEC_PER_SEC)
39
40 static bool ignore_proc(pid_t pid, bool warn_rootfs) {
41 _cleanup_fclose_ FILE *f = NULL;
42 char c;
43 const char *p;
44 size_t count;
45 uid_t uid;
46 int r;
47
48 /* We are PID 1, let's not commit suicide */
49 if (pid == 1)
50 return true;
51
52 r = get_process_uid(pid, &uid);
53 if (r < 0)
54 return true; /* not really, but better safe than sorry */
55
56 /* Non-root processes otherwise are always subject to be killed */
57 if (uid != 0)
58 return false;
59
60 p = procfs_file_alloca(pid, "cmdline");
61 f = fopen(p, "re");
62 if (!f)
63 return true; /* not really, but has the desired effect */
64
65 count = fread(&c, 1, 1, f);
66
67 /* Kernel threads have an empty cmdline */
68 if (count <= 0)
69 return true;
70
71 /* Processes with argv[0][0] = '@' we ignore from the killing
72 * spree.
73 *
74 * http://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons */
75 if (c == '@' && warn_rootfs) {
76 _cleanup_free_ char *comm = NULL;
77
78 r = pid_from_same_root_fs(pid);
79 if (r < 0)
80 return true;
81
82 get_process_comm(pid, &comm);
83
84 if (r)
85 log_notice("Process " PID_FMT " (%s) has been been marked to be excluded from killing. It is "
86 "running from the root file system, and thus likely to block re-mounting of the "
87 "root file system to read-only. Please consider moving it into an initrd file "
88 "system instead.", pid, strna(comm));
89 return true;
90 } else if (c == '@')
91 return true;
92
93 return false;
94 }
95
96 static void wait_for_children(Set *pids, sigset_t *mask) {
97 usec_t until;
98
99 assert(mask);
100
101 if (set_isempty(pids))
102 return;
103
104 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
105 for (;;) {
106 struct timespec ts;
107 int k;
108 usec_t n;
109 void *p;
110 Iterator i;
111
112 /* First, let the kernel inform us about killed
113 * children. Most processes will probably be our
114 * children, but some are not (might be our
115 * grandchildren instead...). */
116 for (;;) {
117 pid_t pid;
118
119 pid = waitpid(-1, NULL, WNOHANG);
120 if (pid == 0)
121 break;
122 if (pid < 0) {
123 if (errno == ECHILD)
124 break;
125
126 log_error_errno(errno, "waitpid() failed: %m");
127 return;
128 }
129
130 (void) set_remove(pids, PID_TO_PTR(pid));
131 }
132
133 /* Now explicitly check who might be remaining, who
134 * might not be our child. */
135 SET_FOREACH(p, pids, i) {
136
137 /* We misuse getpgid as a check whether a
138 * process still exists. */
139 if (getpgid(PTR_TO_PID(p)) >= 0)
140 continue;
141
142 if (errno != ESRCH)
143 continue;
144
145 set_remove(pids, p);
146 }
147
148 if (set_isempty(pids))
149 return;
150
151 n = now(CLOCK_MONOTONIC);
152 if (n >= until)
153 return;
154
155 timespec_store(&ts, until - n);
156 k = sigtimedwait(mask, NULL, &ts);
157 if (k != SIGCHLD) {
158
159 if (k < 0 && errno != EAGAIN) {
160 log_error_errno(errno, "sigtimedwait() failed: %m");
161 return;
162 }
163
164 if (k >= 0)
165 log_warning("sigtimedwait() returned unexpected signal.");
166 }
167 }
168 }
169
170 static int killall(int sig, Set *pids, bool send_sighup) {
171 _cleanup_closedir_ DIR *dir = NULL;
172 struct dirent *d;
173
174 dir = opendir("/proc");
175 if (!dir)
176 return -errno;
177
178 while ((d = readdir(dir))) {
179 pid_t pid;
180 int r;
181
182 if (d->d_type != DT_DIR &&
183 d->d_type != DT_UNKNOWN)
184 continue;
185
186 if (parse_pid(d->d_name, &pid) < 0)
187 continue;
188
189 if (ignore_proc(pid, sig == SIGKILL && !in_initrd()))
190 continue;
191
192 if (sig == SIGKILL) {
193 _cleanup_free_ char *s = NULL;
194
195 get_process_comm(pid, &s);
196 log_notice("Sending SIGKILL to PID "PID_FMT" (%s).", pid, strna(s));
197 }
198
199 if (kill(pid, sig) >= 0) {
200 if (pids) {
201 r = set_put(pids, PID_TO_PTR(pid));
202 if (r < 0)
203 log_oom();
204 }
205 } else if (errno != ENOENT)
206 log_warning_errno(errno, "Could not kill %d: %m", pid);
207
208 if (send_sighup) {
209 /* Optionally, also send a SIGHUP signal, but
210 only if the process has a controlling
211 tty. This is useful to allow handling of
212 shells which ignore SIGTERM but react to
213 SIGHUP. We do not send this to processes that
214 have no controlling TTY since we don't want to
215 trigger reloads of daemon processes. Also we
216 make sure to only send this after SIGTERM so
217 that SIGTERM is always first in the queue. */
218
219
220 if (get_ctty_devnr(pid, NULL) >= 0)
221 kill(pid, SIGHUP);
222 }
223 }
224
225 return set_size(pids);
226 }
227
228 void broadcast_signal(int sig, bool wait_for_exit, bool send_sighup) {
229 sigset_t mask, oldmask;
230 _cleanup_set_free_ Set *pids = NULL;
231
232 if (wait_for_exit)
233 pids = set_new(NULL);
234
235 assert_se(sigemptyset(&mask) == 0);
236 assert_se(sigaddset(&mask, SIGCHLD) == 0);
237 assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
238
239 if (kill(-1, SIGSTOP) < 0 && errno != ESRCH)
240 log_warning_errno(errno, "kill(-1, SIGSTOP) failed: %m");
241
242 killall(sig, pids, send_sighup);
243
244 if (kill(-1, SIGCONT) < 0 && errno != ESRCH)
245 log_warning_errno(errno, "kill(-1, SIGCONT) failed: %m");
246
247 if (wait_for_exit)
248 wait_for_children(pids, &mask);
249
250 assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
251 }