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