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