+ int callback (struct dirent *dirent, const char *pathname)
+ {
+ char buf[LINE_MAX];
+ ssize_t buf_len;
+
+ if ((dirent->d_type != DT_DIR && dirent->d_type != DT_LNK)
+ || (dirent->d_type == DT_DIR && strcmp (dirent->d_name, ".") != 0
+ && strcmp (dirent->d_name, "..") != 0)
+ || (dirent->d_type == DT_LNK && strspn (dirent->d_name, "0123456789")
+ != strlen (dirent->d_name)))
+ {
+ fprintf (stderr, "Unexpected entry \"%s\" (d_type %u)"
+ " on readdir (\"%s\"): %m\n",
+ dirent->d_name, (unsigned) dirent->d_type, dirname);
+ return 0;
+ }
+ if (dirent->d_type == DT_DIR)
+ return 0;
+ buf_len = readlink (pathname, buf, sizeof buf - 1);
+ if (buf_len <= 0 || buf_len >= (ssize_t) sizeof buf - 1)
+ {
+ if (errno != ENOENT && errno != EACCES)
+ fprintf (stderr, "Error reading link \"%s\": %m\n", pathname);
+ return 0;
+ }
+ buf[buf_len] = 0;
+ return (*func) (pid, buf);
+ }
+
+ return dir_scan (dirname, callback);
+}
+
+static void pid_fs_scan (void (*func) (pid_t pid, void *data), void *data)
+{
+ int callback (struct dirent *dirent, const char *pathname)
+ {
+ if (dirent->d_type != DT_DIR
+ || strspn (dirent->d_name, "0123456789") != strlen (dirent->d_name))
+ return 0;
+ (*func) (atoi (dirent->d_name), data);
+ return 0;
+ }
+
+ dir_scan ("/proc", callback);
+}
+
+static int rip_check_ptyname (pid_t pid, const char *link)
+{
+ assert (pid != getpid ());
+
+ return strcmp (link, childptyname) == 0;
+}
+
+struct pid
+ {
+ struct pid *next;
+ pid_t pid;
+ };
+static struct pid *pid_list;
+
+static int pid_found (pid_t pid)
+{
+ struct pid *entry;
+
+ for (entry = pid_list; entry != NULL; entry = entry->next)
+ if (entry->pid == pid)
+ return 1;
+ return 0;
+}
+
+/* Single pass is not enough, a (multithreaded) process was seen to survive.
+ Repeated killing of the same process is not enough, zombies can be killed.
+ */
+static int cleanup_acted;
+
+static void pid_record (pid_t pid)
+{
+ struct pid *entry;
+
+ if (pid_found (pid))
+ return;
+ cleanup_acted = 1;
+
+ entry = malloc (sizeof (*entry));
+ if (entry == NULL)
+ {
+ fprintf (stderr, "%s: malloc: %m\n", progname);
+ exit (EXIT_FAILURE);
+ }
+ entry->pid = pid;
+ entry->next = pid_list;
+ pid_list = entry;
+}
+
+static void pid_forall (void (*func) (pid_t pid))
+{
+ struct pid *entry;
+
+ for (entry = pid_list; entry != NULL; entry = entry->next)
+ (*func) (entry->pid);
+}
+
+/* Returns 0 on failure. */
+static pid_t pid_get_parent (pid_t pid)
+{
+ char fname[64];
+ FILE *f;
+ char line[LINE_MAX];
+ pid_t retval = 0;
+
+ if (snprintf (fname, sizeof fname, "/proc/%d/status", (int) pid) < 0)
+ {
+ perror ("snprintf(3)");
+ exit (EXIT_FAILURE);
+ }
+ f = fopen (fname, "r");
+ if (f == NULL)
+ {
+ return 0;
+ }
+ while (errno = 0, fgets (line, sizeof line, f) == line)
+ {
+ if (strncmp (line, "PPid:\t", sizeof "PPid:\t" - 1) != 0)
+ continue;
+ retval = atoi (line + sizeof "PPid:\t" - 1);
+ errno = 0;
+ break;
+ }
+ if (errno != 0)
+ {
+ fprintf (stderr, "%s: fgets (\"%s\"): %m\n", progname, fname);
+ exit (EXIT_FAILURE);
+ }
+ if (fclose (f) != 0)
+ {
+ fprintf (stderr, "%s: fclose (\"%s\"): %m\n", progname, fname);
+ exit (EXIT_FAILURE);
+ }
+ return retval;
+}
+
+static void killtree (pid_t pid);
+
+static void killtree_pid_fs_scan (pid_t pid, void *data)
+{
+ pid_t parent_pid = *(pid_t *) data;
+
+ /* Do not optimize it as we could miss some newly spawned processes.
+ Always traverse all the leaves. */
+#if 0
+ /* Optimization. */
+ if (pid_found (pid))
+ return;
+#endif
+
+ if (pid_get_parent (pid) != parent_pid)
+ return;
+
+ killtree (pid);
+}
+
+static void killtree (pid_t pid)
+{
+ pid_record (pid);
+ pid_fs_scan (killtree_pid_fs_scan, &pid);
+}
+
+static void rip_pid_fs_scan (pid_t pid, void *data)
+{
+ pid_t pgid;
+
+ /* Shouldn't happen. */
+ if (pid == getpid ())
+ return;
+
+ /* Check both PGID and the stale file descriptors. */
+ pgid = getpgid (pid);
+ if (pgid == child
+ || fd_fs_scan (pid, rip_check_ptyname) != 0)
+ killtree (pid);
+}
+
+static void killproc (pid_t pid)
+{
+ const char *cmdline;
+
+ cmdline = read_cmdline (pid);
+ /* Avoid printing the message for already gone processes. */
+ if (kill (pid, 0) != 0 && errno == ESRCH)
+ return;
+ if (cmdline == NULL)
+ cmdline = "<error>";
+ fprintf (stderr, "%s: Killed -9 orphan PID %d: %s\n", progname, (int) pid, cmdline);
+ if (kill (pid, SIGKILL) == 0)
+ cleanup_acted = 1;
+ else if (errno != ESRCH)
+ fprintf (stderr, "%s: kill (%d, SIGKILL): %m\n", progname, (int) pid);
+ /* RHEL-3 kernels cannot SIGKILL a `T (stopped)' process. */
+ kill (pid, SIGCONT);
+ /* Do not waitpid(2) as it cannot be our direct descendant and it gets
+ cleaned up by init(8). */
+#if 0
+ pid_t pid_got;
+ pid_got = waitpid (pid, NULL, 0);
+ if (pid != pid_got)
+ {
+ fprintf (stderr, "%s: waitpid (%d) != %d: %m\n", progname,
+ (int) pid, (int) pid_got);
+ return;
+ }
+#endif
+}
+
+static void rip (void)
+{
+ cleanup_acted = 0;
+ do
+ {
+ if (cleanup_acted)
+ usleep (1000000 / 10);
+ cleanup_acted = 0;
+ pid_fs_scan (rip_pid_fs_scan, NULL);
+ pid_forall (killproc);
+ }
+ while (cleanup_acted);