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