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