]>
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" | |
28 | #include "def.h" | |
29 | #include "killall.h" | |
aaf7eb81 | 30 | #include "set.h" |
bd3fa1d2 | 31 | |
aaf7eb81 | 32 | #define TIMEOUT_USEC (10 * USEC_PER_SEC) |
bd3fa1d2 LP |
33 | |
34 | static bool ignore_proc(pid_t pid) { | |
35 | char buf[PATH_MAX]; | |
36 | FILE *f; | |
37 | char c; | |
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 | ||
54 | snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long) pid); | |
55 | char_array_0(buf); | |
56 | ||
57 | f = fopen(buf, "re"); | |
58 | if (!f) | |
59 | return true; /* not really, but has the desired effect */ | |
60 | ||
61 | count = fread(&c, 1, 1, f); | |
62 | fclose(f); | |
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 | 78 | static 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 | |
aaf7eb81 | 108 | log_error("waitpid() failed: %m"); |
bd3fa1d2 | 109 | return; |
aaf7eb81 LP |
110 | } |
111 | ||
112 | set_remove(pids, ULONG_TO_PTR(pid)); | |
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. */ | |
121 | if (getpgid((pid_t) PTR_TO_ULONG(p)) >= 0) | |
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) { | |
142 | log_error("sigtimedwait() failed: %m"); | |
143 | return; | |
144 | } | |
145 | ||
146 | if (k >= 0) | |
147 | log_warning("sigtimedwait() returned unexpected signal."); | |
148 | } | |
149 | } | |
150 | } | |
151 | ||
aaf7eb81 LP |
152 | static int killall(int sig, Set *pids) { |
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; | |
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 | ||
df758e98 KS |
173 | if (sig == SIGKILL) { |
174 | _cleanup_free_ char *s; | |
175 | ||
176 | get_process_comm(pid, &s); | |
aaf7eb81 | 177 | log_notice("Sending SIGKILL to PID %lu (%s).", (unsigned long) pid, strna(s)); |
df758e98 KS |
178 | } |
179 | ||
aaf7eb81 LP |
180 | if (kill(pid, sig) >= 0) { |
181 | if (pids) | |
182 | set_put(pids, ULONG_TO_PTR((unsigned long) pid)); | |
183 | } else if (errno != ENOENT) | |
bd3fa1d2 LP |
184 | log_warning("Could not kill %d: %m", pid); |
185 | } | |
186 | ||
aaf7eb81 | 187 | return set_size(pids); |
bd3fa1d2 LP |
188 | } |
189 | ||
3d141780 | 190 | void broadcast_signal(int sig, bool wait_for_exit) { |
bd3fa1d2 | 191 | sigset_t mask, oldmask; |
b6e8f1f0 | 192 | Set *pids = NULL; |
aaf7eb81 LP |
193 | |
194 | if (wait_for_exit) | |
195 | pids = set_new(trivial_hash_func, trivial_compare_func); | |
bd3fa1d2 LP |
196 | |
197 | assert_se(sigemptyset(&mask) == 0); | |
198 | assert_se(sigaddset(&mask, SIGCHLD) == 0); | |
199 | assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0); | |
200 | ||
201 | if (kill(-1, SIGSTOP) < 0 && errno != ESRCH) | |
202 | log_warning("kill(-1, SIGSTOP) failed: %m"); | |
203 | ||
aaf7eb81 | 204 | killall(sig, pids); |
bd3fa1d2 LP |
205 | |
206 | if (kill(-1, SIGCONT) < 0 && errno != ESRCH) | |
207 | log_warning("kill(-1, SIGCONT) failed: %m"); | |
208 | ||
3d141780 | 209 | if (wait_for_exit) |
aaf7eb81 LP |
210 | wait_for_children(pids, &mask); |
211 | ||
212 | assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) == 0); | |
bd3fa1d2 | 213 | |
aaf7eb81 | 214 | set_free(pids); |
bd3fa1d2 | 215 | } |