static int killall(int sig, Set *pids, bool send_sighup) { _cleanup_closedir_ DIR *dir = NULL; struct dirent *d; dir = opendir("/proc"); if (!dir) return -errno; while ((d = readdir(dir))) { pid_t pid; if (d->d_type != DT_DIR && d->d_type != DT_UNKNOWN) continue; if (parse_pid(d->d_name, &pid) < 0) continue; if (ignore_proc(pid)) continue; if (sig == SIGKILL) { _cleanup_free_ char *s; get_process_comm(pid, &s); log_notice("Sending SIGKILL to PID "PID_FMT" (%s).", pid, strna(s)); } if (kill(pid, sig) >= 0) { if (pids) set_put(pids, ULONG_TO_PTR(pid)); } else if (errno != ENOENT) log_warning("Could not kill %d: %m", pid); if (send_sighup) { /* Optionally, also send a SIGHUP signal, but only if the process has a controlling tty. This is useful to allow handling of shells which ignore SIGTERM but react to SIGHUP. We do not send this to processes that have no controlling TTY since we don't want to trigger reloads of daemon processes. Also we make sure to only send this after SIGTERM so that SIGTERM is always first in the queue. */ if (get_ctty_devnr(pid, NULL) >= 0) kill(pid, SIGHUP); } } return set_size(pids); }
static int killall(int sig, Set *pids) { _cleanup_closedir_ DIR *dir = NULL; struct dirent *d; dir = opendir("/proc"); if (!dir) return -errno; while ((d = readdir(dir))) { pid_t pid; if (d->d_type != DT_DIR && d->d_type != DT_UNKNOWN) continue; if (parse_pid(d->d_name, &pid) < 0) continue; if (ignore_proc(pid)) continue; if (sig == SIGKILL) { _cleanup_free_ char *s; get_process_comm(pid, &s); log_notice("Sending SIGKILL to PID %lu (%s).", (unsigned long) pid, strna(s)); } if (kill(pid, sig) >= 0) { if (pids) set_put(pids, ULONG_TO_PTR((unsigned long) pid)); } else if (errno != ENOENT) log_warning("Could not kill %d: %m", pid); } return set_size(pids); }
static void wait_for_children(Set *pids, sigset_t *mask) { usec_t until; assert(mask); if (set_isempty(pids)) return; until = now(CLOCK_MONOTONIC) + TIMEOUT_USEC; for (;;) { struct timespec ts; int k; usec_t n; void *p; Iterator i; /* First, let the kernel inform us about killed * children. Most processes will probably be our * children, but some are not (might be our * grandchildren instead...). */ for (;;) { pid_t pid; pid = waitpid(-1, NULL, WNOHANG); if (pid == 0) break; if (pid < 0) { if (errno == ECHILD) break; log_error("waitpid() failed: %m"); return; } set_remove(pids, ULONG_TO_PTR(pid)); } /* Now explicitly check who might be remaining, who * might not be our child. */ SET_FOREACH(p, pids, i) { /* We misuse getpgid as a check whether a * process still exists. */ if (getpgid((pid_t) PTR_TO_ULONG(p)) >= 0) continue; if (errno != ESRCH) continue; set_remove(pids, p); } if (set_isempty(pids)) return; n = now(CLOCK_MONOTONIC); if (n >= until) return; timespec_store(&ts, until - n); k = sigtimedwait(mask, NULL, &ts); if (k != SIGCHLD) { if (k < 0 && errno != EAGAIN) { log_error("sigtimedwait() failed: %m"); return; } if (k >= 0) log_warning("sigtimedwait() returned unexpected signal."); } } }