]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/killall.c
2 This file is part of systemd.
4 Copyright 2010 ProFUSION embedded systems
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.
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.
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/>.
25 #include "alloc-util.h"
28 #include "format-util.h"
30 #include "parse-util.h"
31 #include "process-util.h"
33 #include "string-util.h"
34 #include "terminal-util.h"
37 static bool ignore_proc(pid_t pid
, bool warn_rootfs
) {
38 _cleanup_fclose_
FILE *f
= NULL
;
45 /* We are PID 1, let's not commit suicide */
49 r
= get_process_uid(pid
, &uid
);
51 return true; /* not really, but better safe than sorry */
53 /* Non-root processes otherwise are always subject to be killed */
57 p
= procfs_file_alloca(pid
, "cmdline");
60 return true; /* not really, but has the desired effect */
62 count
= fread(&c
, 1, 1, f
);
64 /* Kernel threads have an empty cmdline */
68 /* Processes with argv[0][0] = '@' we ignore from the killing
71 * http://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons */
72 if (c
== '@' && warn_rootfs
) {
73 _cleanup_free_
char *comm
= NULL
;
75 r
= pid_from_same_root_fs(pid
);
79 get_process_comm(pid
, &comm
);
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
));
93 static void wait_for_children(Set
*pids
, sigset_t
*mask
) {
98 if (set_isempty(pids
))
101 until
= now(CLOCK_MONOTONIC
) + DEFAULT_TIMEOUT_USEC
;
109 /* First, let the kernel inform us about killed
110 * children. Most processes will probably be our
111 * children, but some are not (might be our
112 * grandchildren instead...). */
116 pid
= waitpid(-1, NULL
, WNOHANG
);
123 log_error_errno(errno
, "waitpid() failed: %m");
127 (void) set_remove(pids
, PID_TO_PTR(pid
));
130 /* Now explicitly check who might be remaining, who
131 * might not be our child. */
132 SET_FOREACH(p
, pids
, i
) {
134 /* We misuse getpgid as a check whether a
135 * process still exists. */
136 if (getpgid(PTR_TO_PID(p
)) >= 0)
145 if (set_isempty(pids
))
148 n
= now(CLOCK_MONOTONIC
);
152 timespec_store(&ts
, until
- n
);
153 k
= sigtimedwait(mask
, NULL
, &ts
);
156 if (k
< 0 && errno
!= EAGAIN
) {
157 log_error_errno(errno
, "sigtimedwait() failed: %m");
162 log_warning("sigtimedwait() returned unexpected signal.");
167 static int killall(int sig
, Set
*pids
, bool send_sighup
) {
168 _cleanup_closedir_
DIR *dir
= NULL
;
171 dir
= opendir("/proc");
175 while ((d
= readdir(dir
))) {
179 if (d
->d_type
!= DT_DIR
&&
180 d
->d_type
!= DT_UNKNOWN
)
183 if (parse_pid(d
->d_name
, &pid
) < 0)
186 if (ignore_proc(pid
, sig
== SIGKILL
&& !in_initrd()))
189 if (sig
== SIGKILL
) {
190 _cleanup_free_
char *s
= NULL
;
192 get_process_comm(pid
, &s
);
193 log_notice("Sending SIGKILL to PID "PID_FMT
" (%s).", pid
, strna(s
));
196 if (kill(pid
, sig
) >= 0) {
198 r
= set_put(pids
, PID_TO_PTR(pid
));
202 } else if (errno
!= ENOENT
)
203 log_warning_errno(errno
, "Could not kill %d: %m", pid
);
206 /* Optionally, also send a SIGHUP signal, but
207 only if the process has a controlling
208 tty. This is useful to allow handling of
209 shells which ignore SIGTERM but react to
210 SIGHUP. We do not send this to processes that
211 have no controlling TTY since we don't want to
212 trigger reloads of daemon processes. Also we
213 make sure to only send this after SIGTERM so
214 that SIGTERM is always first in the queue. */
217 if (get_ctty_devnr(pid
, NULL
) >= 0)
222 return set_size(pids
);
225 void broadcast_signal(int sig
, bool wait_for_exit
, bool send_sighup
) {
226 sigset_t mask
, oldmask
;
227 _cleanup_set_free_ Set
*pids
= NULL
;
230 pids
= set_new(NULL
);
232 assert_se(sigemptyset(&mask
) == 0);
233 assert_se(sigaddset(&mask
, SIGCHLD
) == 0);
234 assert_se(sigprocmask(SIG_BLOCK
, &mask
, &oldmask
) == 0);
236 if (kill(-1, SIGSTOP
) < 0 && errno
!= ESRCH
)
237 log_warning_errno(errno
, "kill(-1, SIGSTOP) failed: %m");
239 killall(sig
, pids
, send_sighup
);
241 if (kill(-1, SIGCONT
) < 0 && errno
!= ESRCH
)
242 log_warning_errno(errno
, "kill(-1, SIGCONT) failed: %m");
245 wait_for_children(pids
, &mask
);
247 assert_se(sigprocmask(SIG_SETMASK
, &oldmask
, NULL
) == 0);