]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/killall.c
treewide: use log_*_errno whenever %m is in the format string
[thirdparty/systemd.git] / src / core / killall.c
CommitLineData
bd3fa1d2
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 ProFUSION embedded systems
7
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.
12
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.
17
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/>.
20***/
21
22#include <sys/wait.h>
23#include <signal.h>
24#include <errno.h>
aaf7eb81 25#include <unistd.h>
bd3fa1d2
LP
26
27#include "util.h"
28#include "def.h"
29#include "killall.h"
aaf7eb81 30#include "set.h"
bd3fa1d2 31
aaf7eb81 32#define TIMEOUT_USEC (10 * USEC_PER_SEC)
bd3fa1d2
LP
33
34static bool ignore_proc(pid_t pid) {
31885cd5 35 _cleanup_fclose_ FILE *f = NULL;
b68fa010
SP
36 char c;
37 const char *p;
bd3fa1d2
LP
38 size_t count;
39 uid_t uid;
40 int r;
41
42 /* We are PID 1, let's not commit suicide */
43 if (pid == 1)
44 return true;
45
46 r = get_process_uid(pid, &uid);
47 if (r < 0)
48 return true; /* not really, but better safe than sorry */
49
50 /* Non-root processes otherwise are always subject to be killed */
51 if (uid != 0)
52 return false;
53
5841bd80
ZJS
54 p = procfs_file_alloca(pid, "cmdline");
55 f = fopen(p, "re");
bd3fa1d2
LP
56 if (!f)
57 return true; /* not really, but has the desired effect */
58
59 count = fread(&c, 1, 1, f);
bd3fa1d2
LP
60
61 /* Kernel threads have an empty cmdline */
62 if (count <= 0)
63 return true;
64
65 /* Processes with argv[0][0] = '@' we ignore from the killing
66 * spree.
67 *
68 * http://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons */
69 if (count == 1 && c == '@')
70 return true;
71
72 return false;
73}
74
aaf7eb81 75static void wait_for_children(Set *pids, sigset_t *mask) {
bd3fa1d2
LP
76 usec_t until;
77
78 assert(mask);
79
aaf7eb81
LP
80 if (set_isempty(pids))
81 return;
82
bd3fa1d2
LP
83 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
84 for (;;) {
85 struct timespec ts;
86 int k;
87 usec_t n;
aaf7eb81
LP
88 void *p;
89 Iterator i;
bd3fa1d2 90
aaf7eb81
LP
91 /* First, let the kernel inform us about killed
92 * children. Most processes will probably be our
93 * children, but some are not (might be our
94 * grandchildren instead...). */
bd3fa1d2 95 for (;;) {
aaf7eb81 96 pid_t pid;
bd3fa1d2 97
aaf7eb81 98 pid = waitpid(-1, NULL, WNOHANG);
bd3fa1d2
LP
99 if (pid == 0)
100 break;
aaf7eb81
LP
101 if (pid < 0) {
102 if (errno == ECHILD)
103 break;
bd3fa1d2 104
56f64d95 105 log_error_errno(errno, "waitpid() failed: %m");
bd3fa1d2 106 return;
aaf7eb81
LP
107 }
108
109 set_remove(pids, ULONG_TO_PTR(pid));
110 }
bd3fa1d2 111
aaf7eb81
LP
112 /* Now explicitly check who might be remaining, who
113 * might not be our child. */
114 SET_FOREACH(p, pids, i) {
115
116 /* We misuse getpgid as a check whether a
117 * process still exists. */
118 if (getpgid((pid_t) PTR_TO_ULONG(p)) >= 0)
119 continue;
120
121 if (errno != ESRCH)
122 continue;
123
124 set_remove(pids, p);
bd3fa1d2
LP
125 }
126
aaf7eb81
LP
127 if (set_isempty(pids))
128 return;
129
bd3fa1d2
LP
130 n = now(CLOCK_MONOTONIC);
131 if (n >= until)
132 return;
133
134 timespec_store(&ts, until - n);
aaf7eb81
LP
135 k = sigtimedwait(mask, NULL, &ts);
136 if (k != SIGCHLD) {
bd3fa1d2
LP
137
138 if (k < 0 && errno != EAGAIN) {
56f64d95 139 log_error_errno(errno, "sigtimedwait() failed: %m");
bd3fa1d2
LP
140 return;
141 }
142
143 if (k >= 0)
144 log_warning("sigtimedwait() returned unexpected signal.");
145 }
146 }
147}
148
0bee65f0 149static int killall(int sig, Set *pids, bool send_sighup) {
aaf7eb81 150 _cleanup_closedir_ DIR *dir = NULL;
bd3fa1d2 151 struct dirent *d;
bd3fa1d2
LP
152
153 dir = opendir("/proc");
154 if (!dir)
155 return -errno;
156
157 while ((d = readdir(dir))) {
158 pid_t pid;
159
160 if (d->d_type != DT_DIR &&
161 d->d_type != DT_UNKNOWN)
162 continue;
163
164 if (parse_pid(d->d_name, &pid) < 0)
165 continue;
166
167 if (ignore_proc(pid))
168 continue;
169
df758e98 170 if (sig == SIGKILL) {
3e09eb5c 171 _cleanup_free_ char *s = NULL;
df758e98
KS
172
173 get_process_comm(pid, &s);
ccd06097 174 log_notice("Sending SIGKILL to PID "PID_FMT" (%s).", pid, strna(s));
df758e98
KS
175 }
176
aaf7eb81
LP
177 if (kill(pid, sig) >= 0) {
178 if (pids)
ccd06097 179 set_put(pids, ULONG_TO_PTR(pid));
aaf7eb81 180 } else if (errno != ENOENT)
56f64d95 181 log_warning_errno(errno, "Could not kill %d: %m", pid);
0bee65f0
LP
182
183 if (send_sighup) {
184 /* Optionally, also send a SIGHUP signal, but
185 only if the process has a controlling
186 tty. This is useful to allow handling of
187 shells which ignore SIGTERM but react to
188 SIGHUP. We do not send this to processes that
189 have no controlling TTY since we don't want to
190 trigger reloads of daemon processes. Also we
191 make sure to only send this after SIGTERM so
192 that SIGTERM is always first in the queue. */
193
194
195 if (get_ctty_devnr(pid, NULL) >= 0)
196 kill(pid, SIGHUP);
197 }
bd3fa1d2
LP
198 }
199
aaf7eb81 200 return set_size(pids);
bd3fa1d2
LP
201}
202
6301a98c 203void broadcast_signal(int sig, bool wait_for_exit, bool send_sighup) {
bd3fa1d2 204 sigset_t mask, oldmask;
e1d75803 205 _cleanup_set_free_ Set *pids = NULL;
aaf7eb81
LP
206
207 if (wait_for_exit)
d5099efc 208 pids = set_new(NULL);
bd3fa1d2
LP
209
210 assert_se(sigemptyset(&mask) == 0);
211 assert_se(sigaddset(&mask, SIGCHLD) == 0);
212 assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
213
214 if (kill(-1, SIGSTOP) < 0 && errno != ESRCH)
56f64d95 215 log_warning_errno(errno, "kill(-1, SIGSTOP) failed: %m");
bd3fa1d2 216
0bee65f0 217 killall(sig, pids, send_sighup);
bd3fa1d2
LP
218
219 if (kill(-1, SIGCONT) < 0 && errno != ESRCH)
56f64d95 220 log_warning_errno(errno, "kill(-1, SIGCONT) failed: %m");
bd3fa1d2 221
3d141780 222 if (wait_for_exit)
aaf7eb81
LP
223 wait_for_children(pids, &mask);
224
225 assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
bd3fa1d2 226}