]> git.proxmox.com Git - systemd.git/blame - src/core/killall.c
Imported Upstream version 218
[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"
28#include "def.h"
29#include "killall.h"
30#include "set.h"
31
32#define TIMEOUT_USEC (10 * USEC_PER_SEC)
33
34static bool ignore_proc(pid_t pid) {
35 _cleanup_fclose_ FILE *f = NULL;
60f067b4
JS
36 char c;
37 const char *p;
663996b3
MS
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
14228c0d
MB
54 p = procfs_file_alloca(pid, "cmdline");
55 f = fopen(p, "re");
663996b3
MS
56 if (!f)
57 return true; /* not really, but has the desired effect */
58
59 count = fread(&c, 1, 1, f);
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
75static void wait_for_children(Set *pids, sigset_t *mask) {
76 usec_t until;
77
78 assert(mask);
79
80 if (set_isempty(pids))
81 return;
82
83 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
84 for (;;) {
85 struct timespec ts;
86 int k;
87 usec_t n;
88 void *p;
89 Iterator i;
90
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...). */
95 for (;;) {
96 pid_t pid;
97
98 pid = waitpid(-1, NULL, WNOHANG);
99 if (pid == 0)
100 break;
101 if (pid < 0) {
102 if (errno == ECHILD)
103 break;
104
f47781d8 105 log_error_errno(errno, "waitpid() failed: %m");
663996b3
MS
106 return;
107 }
108
109 set_remove(pids, ULONG_TO_PTR(pid));
110 }
111
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);
125 }
126
127 if (set_isempty(pids))
128 return;
129
130 n = now(CLOCK_MONOTONIC);
131 if (n >= until)
132 return;
133
134 timespec_store(&ts, until - n);
135 k = sigtimedwait(mask, NULL, &ts);
136 if (k != SIGCHLD) {
137
138 if (k < 0 && errno != EAGAIN) {
f47781d8 139 log_error_errno(errno, "sigtimedwait() failed: %m");
663996b3
MS
140 return;
141 }
142
143 if (k >= 0)
144 log_warning("sigtimedwait() returned unexpected signal.");
145 }
146 }
147}
148
60f067b4 149static int killall(int sig, Set *pids, bool send_sighup) {
663996b3
MS
150 _cleanup_closedir_ DIR *dir = NULL;
151 struct dirent *d;
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
170 if (sig == SIGKILL) {
e842803a 171 _cleanup_free_ char *s = NULL;
663996b3
MS
172
173 get_process_comm(pid, &s);
60f067b4 174 log_notice("Sending SIGKILL to PID "PID_FMT" (%s).", pid, strna(s));
663996b3
MS
175 }
176
177 if (kill(pid, sig) >= 0) {
178 if (pids)
60f067b4 179 set_put(pids, ULONG_TO_PTR(pid));
663996b3 180 } else if (errno != ENOENT)
f47781d8 181 log_warning_errno(errno, "Could not kill %d: %m", pid);
60f067b4
JS
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 }
663996b3
MS
198 }
199
200 return set_size(pids);
201}
202
60f067b4 203void broadcast_signal(int sig, bool wait_for_exit, bool send_sighup) {
663996b3 204 sigset_t mask, oldmask;
e842803a 205 _cleanup_set_free_ Set *pids = NULL;
663996b3
MS
206
207 if (wait_for_exit)
5eef597e 208 pids = set_new(NULL);
663996b3
MS
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)
f47781d8 215 log_warning_errno(errno, "kill(-1, SIGSTOP) failed: %m");
663996b3 216
60f067b4 217 killall(sig, pids, send_sighup);
663996b3
MS
218
219 if (kill(-1, SIGCONT) < 0 && errno != ESRCH)
f47781d8 220 log_warning_errno(errno, "kill(-1, SIGCONT) failed: %m");
663996b3
MS
221
222 if (wait_for_exit)
223 wait_for_children(pids, &mask);
224
225 assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0);
663996b3 226}