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