]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/killall.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2010 ProFUSION embedded systems
11 #include "alloc-util.h"
13 #include "dirent-util.h"
15 #include "format-util.h"
17 #include "parse-util.h"
18 #include "process-util.h"
20 #include "string-util.h"
21 #include "terminal-util.h"
24 static bool ignore_proc(pid_t pid
, bool warn_rootfs
) {
25 _cleanup_fclose_
FILE *f
= NULL
;
31 /* We are PID 1, let's not commit suicide */
35 /* Ignore kernel threads */
36 r
= is_kernel_thread(pid
);
38 return true; /* also ignore processes where we can't determine this */
40 r
= get_process_uid(pid
, &uid
);
42 return true; /* not really, but better safe than sorry */
44 /* Non-root processes otherwise are always subject to be killed */
48 p
= procfs_file_alloca(pid
, "cmdline");
51 return true; /* not really, but has the desired effect */
53 /* Try to read the first character of the command line. If the cmdline is empty (which might be the case for
54 * kernel threads but potentially also other stuff), this line won't do anything, but we don't care much, as
55 * actual kernel threads are already filtered out above. */
56 (void) fread(&c
, 1, 1, f
);
58 /* Processes with argv[0][0] = '@' we ignore from the killing spree.
60 * http://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons */
65 pid_from_same_root_fs(pid
) == 0) {
67 _cleanup_free_
char *comm
= NULL
;
69 (void) get_process_comm(pid
, &comm
);
71 log_notice("Process " PID_FMT
" (%s) has been marked to be excluded from killing. It is "
72 "running from the root file system, and thus likely to block re-mounting of the "
73 "root file system to read-only. Please consider moving it into an initrd file "
74 "system instead.", pid
, strna(comm
));
80 static void wait_for_children(Set
*pids
, sigset_t
*mask
, usec_t timeout
) {
85 if (set_isempty(pids
))
88 until
= now(CLOCK_MONOTONIC
) + timeout
;
96 /* First, let the kernel inform us about killed
97 * children. Most processes will probably be our
98 * children, but some are not (might be our
99 * grandchildren instead...). */
103 pid
= waitpid(-1, NULL
, WNOHANG
);
110 log_error_errno(errno
, "waitpid() failed: %m");
114 (void) set_remove(pids
, PID_TO_PTR(pid
));
117 /* Now explicitly check who might be remaining, who
118 * might not be our child. */
119 SET_FOREACH(p
, pids
, i
) {
121 /* kill(pid, 0) sends no signal, but it tells
122 * us whether the process still exists. */
123 if (kill(PTR_TO_PID(p
), 0) == 0)
132 if (set_isempty(pids
))
135 n
= now(CLOCK_MONOTONIC
);
139 timespec_store(&ts
, until
- n
);
140 k
= sigtimedwait(mask
, NULL
, &ts
);
143 if (k
< 0 && errno
!= EAGAIN
) {
144 log_error_errno(errno
, "sigtimedwait() failed: %m");
149 log_warning("sigtimedwait() returned unexpected signal.");
154 static int killall(int sig
, Set
*pids
, bool send_sighup
) {
155 _cleanup_closedir_
DIR *dir
= NULL
;
158 dir
= opendir("/proc");
162 FOREACH_DIRENT_ALL(d
, dir
, break) {
166 if (!IN_SET(d
->d_type
, DT_DIR
, DT_UNKNOWN
))
169 if (parse_pid(d
->d_name
, &pid
) < 0)
172 if (ignore_proc(pid
, sig
== SIGKILL
&& !in_initrd()))
175 if (sig
== SIGKILL
) {
176 _cleanup_free_
char *s
= NULL
;
178 get_process_comm(pid
, &s
);
179 log_notice("Sending SIGKILL to PID "PID_FMT
" (%s).", pid
, strna(s
));
182 if (kill(pid
, sig
) >= 0) {
184 r
= set_put(pids
, PID_TO_PTR(pid
));
188 } else if (errno
!= ENOENT
)
189 log_warning_errno(errno
, "Could not kill %d: %m", pid
);
192 /* Optionally, also send a SIGHUP signal, but
193 only if the process has a controlling
194 tty. This is useful to allow handling of
195 shells which ignore SIGTERM but react to
196 SIGHUP. We do not send this to processes that
197 have no controlling TTY since we don't want to
198 trigger reloads of daemon processes. Also we
199 make sure to only send this after SIGTERM so
200 that SIGTERM is always first in the queue. */
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
);
208 return set_size(pids
);
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
;
216 pids
= set_new(NULL
);
218 assert_se(sigemptyset(&mask
) == 0);
219 assert_se(sigaddset(&mask
, SIGCHLD
) == 0);
220 assert_se(sigprocmask(SIG_BLOCK
, &mask
, &oldmask
) == 0);
222 if (kill(-1, SIGSTOP
) < 0 && errno
!= ESRCH
)
223 log_warning_errno(errno
, "kill(-1, SIGSTOP) failed: %m");
225 killall(sig
, pids
, send_sighup
);
227 if (kill(-1, SIGCONT
) < 0 && errno
!= ESRCH
)
228 log_warning_errno(errno
, "kill(-1, SIGCONT) failed: %m");
231 wait_for_children(pids
, &mask
, timeout
);
233 assert_se(sigprocmask(SIG_SETMASK
, &oldmask
, NULL
) == 0);