chiark / gitweb /
1eb3766f777a81d3dd566794df15541dd49d1bfe
[elogind.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 <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 (sig == SIGKILL) {
143                         _cleanup_free_ char *s;
144
145                         get_process_comm(pid, &s);
146                         log_notice("Sending SIGKILL to PID %lu (%s)", (unsigned long) pid, strna(s));
147                 }
148
149                 if (kill(pid, sig) >= 0)
150                         n_processes++;
151                 else if (errno != ENOENT)
152                         log_warning("Could not kill %d: %m", pid);
153         }
154
155         closedir(dir);
156
157         return n_processes;
158 }
159
160 void broadcast_signal(int sig, bool wait_for_exit) {
161         sigset_t mask, oldmask;
162         int n_processes;
163
164         assert_se(sigemptyset(&mask) == 0);
165         assert_se(sigaddset(&mask, SIGCHLD) == 0);
166         assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
167
168         if (kill(-1, SIGSTOP) < 0 && errno != ESRCH)
169                 log_warning("kill(-1, SIGSTOP) failed: %m");
170
171         n_processes = killall(sig);
172
173         if (kill(-1, SIGCONT) < 0 && errno != ESRCH)
174                 log_warning("kill(-1, SIGCONT) failed: %m");
175
176         if (n_processes <= 0)
177                 goto finish;
178
179         if (wait_for_exit)
180                 wait_for_children(n_processes, &mask);
181
182 finish:
183         sigprocmask(SIG_SETMASK, &oldmask, NULL);
184 }