]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/killall.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 ProFUSION embedded systems
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.
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.
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/>.
28 #include "formats-util.h"
30 #include "process-util.h"
32 #include "string-util.h"
33 #include "terminal-util.h"
36 #define TIMEOUT_USEC (10 * USEC_PER_SEC)
38 static bool ignore_proc(pid_t pid
) {
39 _cleanup_fclose_
FILE *f
= NULL
;
46 /* We are PID 1, let's not commit suicide */
50 r
= get_process_uid(pid
, &uid
);
52 return true; /* not really, but better safe than sorry */
54 /* Non-root processes otherwise are always subject to be killed */
58 p
= procfs_file_alloca(pid
, "cmdline");
61 return true; /* not really, but has the desired effect */
63 count
= fread(&c
, 1, 1, f
);
65 /* Kernel threads have an empty cmdline */
69 /* Processes with argv[0][0] = '@' we ignore from the killing
72 * http://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons */
73 if (count
== 1 && c
== '@')
79 static void wait_for_children(Set
*pids
, sigset_t
*mask
) {
84 if (set_isempty(pids
))
87 until
= now(CLOCK_MONOTONIC
) + TIMEOUT_USEC
;
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...). */
102 pid
= waitpid(-1, NULL
, WNOHANG
);
109 log_error_errno(errno
, "waitpid() failed: %m");
113 (void) set_remove(pids
, PID_TO_PTR(pid
));
116 /* Now explicitly check who might be remaining, who
117 * might not be our child. */
118 SET_FOREACH(p
, pids
, i
) {
120 /* We misuse getpgid as a check whether a
121 * process still exists. */
122 if (getpgid(PTR_TO_PID(p
)) >= 0)
131 if (set_isempty(pids
))
134 n
= now(CLOCK_MONOTONIC
);
138 timespec_store(&ts
, until
- n
);
139 k
= sigtimedwait(mask
, NULL
, &ts
);
142 if (k
< 0 && errno
!= EAGAIN
) {
143 log_error_errno(errno
, "sigtimedwait() failed: %m");
148 log_warning("sigtimedwait() returned unexpected signal.");
153 static int killall(int sig
, Set
*pids
, bool send_sighup
) {
154 _cleanup_closedir_
DIR *dir
= NULL
;
157 dir
= opendir("/proc");
161 while ((d
= readdir(dir
))) {
165 if (d
->d_type
!= DT_DIR
&&
166 d
->d_type
!= DT_UNKNOWN
)
169 if (parse_pid(d
->d_name
, &pid
) < 0)
172 if (ignore_proc(pid
))
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. */
203 if (get_ctty_devnr(pid
, NULL
) >= 0)
208 return set_size(pids
);
211 void broadcast_signal(int sig
, bool wait_for_exit
, bool send_sighup
) {
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
);
233 assert_se(sigprocmask(SIG_SETMASK
, &oldmask
, NULL
) == 0);