]>
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> | |
25 | ||
26 | #include "util.h" | |
27 | #include "def.h" | |
28 | #include "killall.h" | |
29 | ||
30 | #define TIMEOUT_USEC (5 * USEC_PER_SEC) | |
31 | ||
32 | static bool ignore_proc(pid_t pid) { | |
33 | char buf[PATH_MAX]; | |
34 | FILE *f; | |
35 | char c; | |
36 | size_t count; | |
37 | uid_t uid; | |
38 | int r; | |
39 | ||
40 | /* We are PID 1, let's not commit suicide */ | |
41 | if (pid == 1) | |
42 | return true; | |
43 | ||
44 | r = get_process_uid(pid, &uid); | |
45 | if (r < 0) | |
46 | return true; /* not really, but better safe than sorry */ | |
47 | ||
48 | /* Non-root processes otherwise are always subject to be killed */ | |
49 | if (uid != 0) | |
50 | return false; | |
51 | ||
52 | snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long) pid); | |
53 | char_array_0(buf); | |
54 | ||
55 | f = fopen(buf, "re"); | |
56 | if (!f) | |
57 | return true; /* not really, but has the desired effect */ | |
58 | ||
59 | count = fread(&c, 1, 1, f); | |
60 | fclose(f); | |
61 | ||
62 | /* Kernel threads have an empty cmdline */ | |
63 | if (count <= 0) | |
64 | return true; | |
65 | ||
66 | /* Processes with argv[0][0] = '@' we ignore from the killing | |
67 | * spree. | |
68 | * | |
69 | * http://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons */ | |
70 | if (count == 1 && c == '@') | |
71 | return true; | |
72 | ||
73 | return false; | |
74 | } | |
75 | ||
76 | static void wait_for_children(int n_processes, sigset_t *mask) { | |
77 | usec_t until; | |
78 | ||
79 | assert(mask); | |
80 | ||
81 | until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC; | |
82 | for (;;) { | |
83 | struct timespec ts; | |
84 | int k; | |
85 | usec_t n; | |
86 | ||
87 | for (;;) { | |
88 | pid_t pid = waitpid(-1, NULL, WNOHANG); | |
89 | ||
90 | if (pid == 0) | |
91 | break; | |
92 | ||
93 | if (pid < 0 && errno == ECHILD) | |
94 | return; | |
95 | ||
96 | if (n_processes > 0) | |
97 | if (--n_processes == 0) | |
98 | return; | |
99 | } | |
100 | ||
101 | n = now(CLOCK_MONOTONIC); | |
102 | if (n >= until) | |
103 | return; | |
104 | ||
105 | timespec_store(&ts, until - n); | |
106 | ||
107 | if ((k = sigtimedwait(mask, NULL, &ts)) != SIGCHLD) { | |
108 | ||
109 | if (k < 0 && errno != EAGAIN) { | |
110 | log_error("sigtimedwait() failed: %m"); | |
111 | return; | |
112 | } | |
113 | ||
114 | if (k >= 0) | |
115 | log_warning("sigtimedwait() returned unexpected signal."); | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | static int killall(int sig) { | |
121 | DIR *dir; | |
122 | struct dirent *d; | |
123 | unsigned int n_processes = 0; | |
124 | ||
125 | dir = opendir("/proc"); | |
126 | if (!dir) | |
127 | return -errno; | |
128 | ||
129 | while ((d = readdir(dir))) { | |
130 | pid_t pid; | |
131 | ||
132 | if (d->d_type != DT_DIR && | |
133 | d->d_type != DT_UNKNOWN) | |
134 | continue; | |
135 | ||
136 | if (parse_pid(d->d_name, &pid) < 0) | |
137 | continue; | |
138 | ||
139 | if (ignore_proc(pid)) | |
140 | continue; | |
141 | ||
142 | if (kill(pid, sig) >= 0) | |
143 | n_processes++; | |
144 | else if (errno != ENOENT) | |
145 | log_warning("Could not kill %d: %m", pid); | |
146 | } | |
147 | ||
148 | closedir(dir); | |
149 | ||
150 | return n_processes; | |
151 | } | |
152 | ||
cee530bb | 153 | void broadcast_signal(int sig, bool wait) { |
bd3fa1d2 LP |
154 | sigset_t mask, oldmask; |
155 | int n_processes; | |
156 | ||
157 | assert_se(sigemptyset(&mask) == 0); | |
158 | assert_se(sigaddset(&mask, SIGCHLD) == 0); | |
159 | assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0); | |
160 | ||
161 | if (kill(-1, SIGSTOP) < 0 && errno != ESRCH) | |
162 | log_warning("kill(-1, SIGSTOP) failed: %m"); | |
163 | ||
164 | n_processes = killall(sig); | |
165 | ||
166 | if (kill(-1, SIGCONT) < 0 && errno != ESRCH) | |
167 | log_warning("kill(-1, SIGCONT) failed: %m"); | |
168 | ||
169 | if (n_processes <= 0) | |
170 | goto finish; | |
171 | ||
cee530bb LP |
172 | if (wait) |
173 | wait_for_children(n_processes, &mask); | |
bd3fa1d2 LP |
174 | |
175 | finish: | |
176 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | |
177 | } |