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