1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 ProFUSION embedded systems
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.
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.
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/>.
30 #define TIMEOUT_USEC (5 * USEC_PER_SEC)
32 static bool ignore_proc(pid_t pid) {
40 /* We are PID 1, let's not commit suicide */
44 r = get_process_uid(pid, &uid);
46 return true; /* not really, but better safe than sorry */
48 /* Non-root processes otherwise are always subject to be killed */
52 snprintf(buf, sizeof(buf), "/proc/%lu/cmdline", (unsigned long) pid);
57 return true; /* not really, but has the desired effect */
59 count = fread(&c, 1, 1, f);
62 /* Kernel threads have an empty cmdline */
66 /* Processes with argv[0][0] = '@' we ignore from the killing
69 * http://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons */
70 if (count == 1 && c == '@')
76 static void wait_for_children(int n_processes, sigset_t *mask) {
81 until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
88 pid_t pid = waitpid(-1, NULL, WNOHANG);
93 if (pid < 0 && errno == ECHILD)
97 if (--n_processes == 0)
101 n = now(CLOCK_MONOTONIC);
105 timespec_store(&ts, until - n);
107 if ((k = sigtimedwait(mask, NULL, &ts)) != SIGCHLD) {
109 if (k < 0 && errno != EAGAIN) {
110 log_error("sigtimedwait() failed: %m");
115 log_warning("sigtimedwait() returned unexpected signal.");
120 static int killall(int sig) {
123 unsigned int n_processes = 0;
125 dir = opendir("/proc");
129 while ((d = readdir(dir))) {
132 if (d->d_type != DT_DIR &&
133 d->d_type != DT_UNKNOWN)
136 if (parse_pid(d->d_name, &pid) < 0)
139 if (ignore_proc(pid))
142 if (kill(pid, sig) >= 0)
144 else if (errno != ENOENT)
145 log_warning("Could not kill %d: %m", pid);
153 void broadcast_signal(int sig, bool wait_for_exit) {
154 sigset_t mask, oldmask;
157 assert_se(sigemptyset(&mask) == 0);
158 assert_se(sigaddset(&mask, SIGCHLD) == 0);
159 assert_se(sigprocmask(SIG_BLOCK, &mask, &oldmask) == 0);
161 if (kill(-1, SIGSTOP) < 0 && errno != ESRCH)
162 log_warning("kill(-1, SIGSTOP) failed: %m");
164 n_processes = killall(sig);
166 if (kill(-1, SIGCONT) < 0 && errno != ESRCH)
167 log_warning("kill(-1, SIGCONT) failed: %m");
169 if (n_processes <= 0)
173 wait_for_children(n_processes, &mask);
176 sigprocmask(SIG_SETMASK, &oldmask, NULL);