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