Blame SOURCES/gdb-orphanripper.c

689258
/*
689258
 * Copyright 2006-2007 Free Software Foundation, Inc.
689258
 *
689258
 * This program is free software; you can redistribute it and/or modify
689258
 * it under the terms of the GNU General Public License as published by
689258
 * the Free Software Foundation; either version 2 of the License, or
689258
 * (at your option) any later version.
689258
 *
689258
 * This program is distributed in the hope that it will be useful,
689258
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
689258
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
689258
 * GNU General Public License for more details.
689258
 *
689258
 * You should have received a copy of the GNU General Public License
689258
 * along with this program; if not, write to the Free Software
689258
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
689258
 *
689258
 * Reap any leftover children possibly holding file descriptors.
689258
 * Children are identified by the stale file descriptor or PGID / SID.
689258
 * Both can be missed but only the stale file descriptors are important for us.
689258
 * PGID / SID may be set by the children on their own.
689258
 * If we fine a candidate we kill it will all its process tree (grandchildren).
689258
 * The child process is run with `2>&1' redirection (due to forkpty(3)).
689258
 * 2007-07-10  Jan Kratochvil  <jan.kratochvil@redhat.com>
689258
 */
689258
689258
/* For getpgid(2).  */
689258
#define _GNU_SOURCE 1
689258
689258
#include <stdio.h>
689258
#include <stdlib.h>
689258
#include <sys/types.h>
689258
#include <sys/wait.h>
689258
#include <dirent.h>
689258
#include <unistd.h>
689258
#include <errno.h>
689258
#include <ctype.h>
689258
#include <string.h>
689258
#include <limits.h>
689258
#include <fcntl.h>
689258
#include <assert.h>
689258
#include <pty.h>
689258
#include <poll.h>
689258
#include <sys/stat.h>
689258
689258
#define LENGTH(x) (sizeof (x) / sizeof (*(x)))
689258
689258
static const char *progname;
689258
689258
static volatile pid_t child;
689258
689258
static void signal_chld (int signo)
689258
{
689258
}
689258
689258
static volatile int signal_alrm_hit = 0;
689258
689258
static void signal_alrm (int signo)
689258
{
689258
  signal_alrm_hit = 1;
689258
}
689258
689258
static char childptyname[LINE_MAX];
689258
689258
static void print_child_error (const char *reason, char **argv)
689258
{
689258
  char **sp;
689258
689258
  fprintf (stderr, "%s: %d %s:", progname, (int) child, reason);
689258
  for (sp = argv; *sp != NULL; sp++)
689258
    {
689258
      fputc (' ', stderr);
689258
      fputs (*sp, stderr);
689258
    }
689258
  fputc ('\n', stderr);
689258
}
689258
689258
static int read_out (int amaster)
689258
{
689258
  char buf[LINE_MAX];
689258
  ssize_t buf_got;
689258
689258
  buf_got = read (amaster, buf, sizeof buf);
689258
  if (buf_got == 0)
689258
    return 0;
689258
  /* Weird but at least after POLLHUP we get EIO instead of just EOF.  */
689258
  if (buf_got == -1 && errno == EIO)
689258
    return 0;
689258
  if (buf_got == -1 && errno == EAGAIN)
689258
    return 0;
689258
  if (buf_got < 0)
689258
    {
689258
      perror ("read (amaster)");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  if (write (STDOUT_FILENO, buf, buf_got) != buf_got)
689258
    {
689258
      perror ("write(2)");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  return 1;
689258
}
689258
689258
/* kill (child, 0) == 0 sometimes even when CHILD's state is already "Z".  */
689258
689258
static int child_exited (void)
689258
{
689258
  char buf[200];
689258
  int fd, i, retval;
689258
  ssize_t got;
689258
  char state[3];
689258
689258
  snprintf (buf, sizeof (buf), "/proc/%ld/stat", (long) child);
689258
  fd = open (buf, O_RDONLY);
689258
  if (fd == -1)
689258
    {
689258
      perror ("open (/proc/CHILD/stat)");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  got = read (fd, buf, sizeof(buf));
689258
  if (got <= 0)
689258
    {
689258
      perror ("read (/proc/CHILD/stat)");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  if (close (fd) != 0)
689258
    {
689258
      perror ("close (/proc/CHILD/stat)");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  /* RHEL-5 does not support %ms.  */
689258
  i = sscanf (buf, "%*d%*s%2s", state);
689258
  if (i != 1)
689258
    {
689258
      perror ("sscanf (/proc/CHILD/stat)");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  retval = strcmp (state, "Z") == 0;
689258
  return retval;
689258
}
689258
689258
static int spawn (char **argv, int timeout)
689258
{
689258
  pid_t child_got;
689258
  int status, amaster, i, rc;
689258
  struct sigaction act;
689258
  sigset_t set;
689258
  struct termios termios;
689258
  unsigned alarm_orig;
689258
689258
  /* We do not use signal(2) to be sure we do not have SA_RESTART.  */
689258
  memset (&act, 0, sizeof (act));
689258
  act.sa_handler = signal_chld;
689258
  i = sigemptyset (&act.sa_mask);
689258
  assert (i == 0);
689258
  act.sa_flags = 0;	/* !SA_RESTART */
689258
  i = sigaction (SIGCHLD, &act, NULL);
689258
  assert (i == 0);
689258
689258
  i = sigemptyset (&set);
689258
  assert (i == 0);
689258
  i = sigaddset (&set, SIGCHLD);
689258
  assert (i == 0);
689258
  i = sigprocmask (SIG_SETMASK, &set, NULL);
689258
  assert (i == 0);
689258
689258
  /* With TERMP passed as NULL we get "\n" -> "\r\n".  */
689258
  termios.c_iflag = IGNBRK | IGNPAR;
689258
  termios.c_oflag = 0;
689258
  termios.c_cflag = CS8 | CREAD | CLOCAL | HUPCL | B9600;
689258
  termios.c_lflag = IEXTEN | NOFLSH;
689258
  memset (termios.c_cc, _POSIX_VDISABLE, sizeof (termios.c_cc));
689258
  termios.c_cc[VTIME] = 0;
689258
  termios.c_cc[VMIN ] = 1;
689258
  cfmakeraw (&termios);
689258
#ifdef FLUSHO
689258
  /* Workaround a readline deadlock bug in _get_tty_settings().  */
689258
  termios.c_lflag &= ~FLUSHO;
689258
#endif
689258
  child = forkpty (&amaster, childptyname, &termios, NULL);
689258
  switch (child)
689258
    {
689258
      case -1:
689258
	perror ("forkpty(3)");
689258
	exit (EXIT_FAILURE);
689258
      case 0:
689258
	/* Do not replace STDIN as inferiors query its termios.  */
689258
#if 0
689258
	i = close (STDIN_FILENO);
689258
	assert (i == 0);
689258
	i = open ("/dev/null", O_RDONLY);
689258
	assert (i == STDIN_FILENO);
689258
#endif
689258
689258
	i = sigemptyset (&set);
689258
	assert (i == 0);
689258
	i = sigprocmask (SIG_SETMASK, &set, NULL);
689258
	assert (i == 0);
689258
689258
	/* Do not setpgrp(2) in the parent process as the process-group
689258
	   is shared for the whole sh(1) pipeline we could be a part
689258
	   of.  The process-group is set according to PID of the first
689258
	   command in the pipeline.
689258
	   We would rip even vi(1) in the case of:
689258
		./orphanripper sh -c 'sleep 1&' | vi -
689258
	   */
689258
	/* Do not setpgrp(2) as our pty would not be ours and we would
689258
	   get `SIGSTOP' later, particularly after spawning gdb(1).
689258
	   setsid(3) was already executed by forkpty(3) and it would fail if
689258
	   executed again.  */
689258
	if (getpid() != getpgrp ())
689258
	  {
689258
	    perror ("getpgrp(2)");
689258
	    exit (EXIT_FAILURE);
689258
	  }
689258
	execvp (argv[0], argv);
689258
	perror ("execvp(2)");
689258
	exit (EXIT_FAILURE);
689258
      default:
689258
	break;
689258
    }
689258
  i = fcntl (amaster, F_SETFL, O_RDWR | O_NONBLOCK);
689258
  if (i != 0)
689258
    {
689258
      perror ("fcntl (amaster, F_SETFL, O_NONBLOCK)");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
689258
  /* We do not use signal(2) to be sure we do not have SA_RESTART.  */
689258
  act.sa_handler = signal_alrm;
689258
  i = sigaction (SIGALRM, &act, NULL);
689258
  assert (i == 0);
689258
689258
  alarm_orig = alarm (timeout);
689258
  assert (alarm_orig == 0);
689258
689258
  i = sigemptyset (&set);
689258
  assert (i == 0);
689258
689258
  while (!signal_alrm_hit)
689258
    {
689258
      struct pollfd pollfd;
689258
689258
      pollfd.fd = amaster;
689258
      pollfd.events = POLLIN;
689258
      i = ppoll (&pollfd, 1, NULL, &set);
689258
      if (i == -1 && errno == EINTR)
689258
	{
689258
	  if (child_exited ())
689258
	    break;
689258
	  /* Non-CHILD child may have exited.  */
689258
	  continue;
689258
	}
689258
      assert (i == 1);
689258
      /* Data available?  Process it first.  */
689258
      if (pollfd.revents & POLLIN)
689258
	{
689258
	  if (!read_out (amaster))
689258
	    {
689258
	      fprintf (stderr, "%s: Unexpected EOF\n", progname);
689258
	      exit (EXIT_FAILURE);
689258
	    }
689258
	}
689258
      if (pollfd.revents & POLLHUP)
689258
        break;
689258
      if ((pollfd.revents &= ~POLLIN) != 0)
689258
	{
689258
	  fprintf (stderr, "%s: ppoll(2): revents 0x%x\n", progname,
689258
		   (unsigned) pollfd.revents);
689258
	  exit (EXIT_FAILURE);
689258
	}
689258
      /* Child exited?  */
689258
      if (child_exited ())
689258
	break;
689258
    }
689258
689258
  if (signal_alrm_hit)
689258
    {
689258
      i = kill (child, SIGKILL);
689258
      assert (i == 0);
689258
    }
689258
  else
689258
    alarm (0);
689258
689258
  /* WNOHANG still could fail.  */
689258
  child_got = waitpid (child, &status, 0);
689258
  if (child != child_got)
689258
    {
689258
      fprintf (stderr, "waitpid (%d) = %d: %m\n", (int) child, (int) child_got);
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  if (signal_alrm_hit)
689258
    {
689258
      char *buf;
689258
689258
      if (asprintf (&buf, "Timed out after %d seconds", timeout) != -1)
689258
	{
689258
	  print_child_error (buf, argv);
689258
	  free (buf);
689258
	}
689258
      rc = 128 + SIGALRM;
689258
    }
689258
  else if (WIFEXITED (status))
689258
    rc = WEXITSTATUS (status);
689258
  else if (WIFSIGNALED (status))
689258
    {
689258
      print_child_error (strsignal (WTERMSIG (status)), argv);
689258
      rc = 128 + WTERMSIG (status);
689258
    }
689258
  else if (WIFSTOPPED (status))
689258
    {
689258
      fprintf (stderr, "waitpid (%d): WIFSTOPPED - WSTOPSIG is %d\n",
689258
	       (int) child, WSTOPSIG (status));
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  else
689258
    {
689258
      fprintf (stderr, "waitpid (%d): !WIFEXITED (%d)\n", (int) child, status);
689258
      exit (EXIT_FAILURE);
689258
    }
689258
689258
  /* Not used in fact.  */
689258
  i = sigprocmask (SIG_SETMASK, &set, NULL);
689258
  assert (i == 0);
689258
689258
  /* Do not unset O_NONBLOCK as a stale child (the whole purpose of this
689258
     program) having open its output pty would block us in read_out.  */
689258
#if 0
689258
  i = fcntl (amaster, F_SETFL, O_RDONLY /* !O_NONBLOCK */);
689258
  if (i != 0)
689258
    {
689258
      perror ("fcntl (amaster, F_SETFL, O_RDONLY /* !O_NONBLOCK */)");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
#endif
689258
689258
  while (read_out (amaster));
689258
689258
  /* Do not close the master FD as the child would have `/dev/pts/23 (deleted)'
689258
     entries which are not expected (and expecting ` (deleted)' would be
689258
     a race.  */
689258
#if 0
689258
  i = close (amaster);
689258
  if (i != 0)
689258
    {
689258
      perror ("close (forkpty ()'s amaster)");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
#endif
689258
689258
  return rc;
689258
}
689258
689258
/* Detected commandline may look weird due to a race:
689258
   Original command:
689258
	./orphanripper sh -c 'sleep 1&' &
689258
   Correct output:
689258
	[1] 29610
689258
	./orphanripper: Killed -9 orphan PID 29612 (PGID 29611): sleep 1
689258
   Raced output (sh(1) child still did not update its argv[]):
689258
	[1] 29613
689258
	./orphanripper: Killed -9 orphan PID 29615 (PGID 29614): sh -c sleep 1&
689258
   We could delay a bit before ripping the children.  */
689258
static const char *read_cmdline (pid_t pid)
689258
{
689258
  char cmdline_fname[32];
689258
  static char cmdline[LINE_MAX];
689258
  int fd;
689258
  ssize_t got;
689258
  char *s;
689258
689258
  if (snprintf (cmdline_fname, sizeof cmdline_fname, "/proc/%d/cmdline",
689258
      (int) pid) < 0)
689258
    return NULL;
689258
  fd = open (cmdline_fname, O_RDONLY);
689258
  if (fd == -1)
689258
    {
689258
      /* It may have already exited - ENOENT.  */
689258
#if 0
689258
      fprintf (stderr, "%s: open (\"%s\"): %m\n", progname, cmdline_fname);
689258
#endif
689258
      return NULL;
689258
    }
689258
  got = read (fd, cmdline, sizeof (cmdline) - 1);
689258
  if (got == -1)
689258
    fprintf (stderr, "%s: read (\"%s\"): %m\n", progname,
689258
       cmdline_fname);
689258
  if (close (fd) != 0)
689258
    fprintf (stderr, "%s: close (\"%s\"): %m\n", progname,
689258
       cmdline_fname);
689258
  if (got < 0)
689258
    return NULL;
689258
  /* Convert '\0' argument delimiters to spaces.  */
689258
  for (s = cmdline; s < cmdline + got; s++)
689258
    if (!*s)
689258
      *s = ' ';
689258
  /* Trim the trailing spaces (typically single '\0'->' ').  */
689258
  while (s > cmdline && isspace (s[-1]))
689258
    s--;
689258
  *s = 0;
689258
  return cmdline;
689258
}
689258
689258
static int dir_scan (const char *dirname,
689258
		  int (*callback) (struct dirent *dirent, const char *pathname))
689258
{
689258
  DIR *dir;
689258
  struct dirent *dirent;
689258
  int rc = 0;
689258
689258
  dir = opendir (dirname);
689258
  if (dir == NULL)
689258
    {
689258
      if (errno == EACCES || errno == ENOENT)
689258
	return rc;
689258
      fprintf (stderr, "%s: opendir (\"%s\"): %m\n", progname, dirname);
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  while ((errno = 0, dirent = readdir (dir)))
689258
    {
689258
      char pathname[LINE_MAX];
689258
      int pathname_len;
689258
689258
      pathname_len = snprintf (pathname, sizeof pathname, "%s/%s",
689258
				 dirname, dirent->d_name);
689258
      if (pathname_len <= 0 || pathname_len >= (int) sizeof pathname)
689258
	{
689258
	  fprintf (stderr, "entry file name too long: `%s' / `%s'\n",
689258
		   dirname, dirent->d_name);
689258
	  continue;
689258
	}
689258
      /* RHEL-4.5 on s390x never fills in D_TYPE.  */
689258
      if (dirent->d_type == DT_UNKNOWN)
689258
        {
689258
	  struct stat statbuf;
689258
	  int i;
689258
689258
	  /* We are not interested in the /proc/PID/fd/ links targets.  */
689258
	  i = lstat (pathname, &statbuf);
689258
	  if (i == -1)
689258
	    {
689258
	      if (errno == EACCES || errno == ENOENT)
689258
	        continue;
689258
	      fprintf (stderr, "%s: stat (\"%s\"): %m\n", progname, pathname);
689258
	      exit (EXIT_FAILURE);
689258
	    }
689258
	  if (S_ISDIR (statbuf.st_mode))
689258
	    dirent->d_type = DT_DIR;
689258
	  if (S_ISLNK (statbuf.st_mode))
689258
	    dirent->d_type = DT_LNK;
689258
	  /* No other D_TYPE types used in this code.  */
689258
	}
689258
      rc = (*callback) (dirent, pathname);
689258
      if (rc != 0)
689258
	{
689258
	  errno = 0;
689258
	  break;
689258
	}
689258
    }
689258
  if (errno != 0)
689258
    {
689258
      fprintf (stderr, "%s: readdir (\"%s\"): %m\n", progname, dirname);
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  if (closedir (dir) != 0)
689258
    {
689258
      fprintf (stderr, "%s: closedir (\"%s\"): %m\n", progname, dirname);
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  return rc;
689258
}
689258
689258
static int fd_fs_scan (pid_t pid, int (*func) (pid_t pid, const char *link))
689258
{
689258
  char dirname[64];
689258
689258
  if (snprintf (dirname, sizeof dirname, "/proc/%d/fd", (int) pid) < 0)
689258
    {
689258
      perror ("snprintf(3)");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
689258
  int callback (struct dirent *dirent, const char *pathname)
689258
  {
689258
    char buf[LINE_MAX];
689258
    ssize_t buf_len;
689258
689258
    if ((dirent->d_type != DT_DIR && dirent->d_type != DT_LNK)
689258
	|| (dirent->d_type == DT_DIR && strcmp (dirent->d_name, ".") != 0
689258
	    && strcmp (dirent->d_name, "..") != 0)
689258
	|| (dirent->d_type == DT_LNK && strspn (dirent->d_name, "0123456789")
689258
	    != strlen (dirent->d_name)))
689258
      {
689258
	fprintf (stderr, "Unexpected entry \"%s\" (d_type %u)"
689258
			 " on readdir (\"%s\"): %m\n",
689258
		 dirent->d_name, (unsigned) dirent->d_type, dirname);
689258
	return 0;
689258
      }
689258
    if (dirent->d_type == DT_DIR)
689258
      return 0;
689258
    buf_len = readlink (pathname, buf, sizeof buf - 1);
689258
    if (buf_len <= 0 || buf_len >= (ssize_t) sizeof buf - 1)
689258
      {
689258
	if (errno != ENOENT && errno != EACCES)
689258
	  fprintf (stderr, "Error reading link \"%s\": %m\n", pathname);
689258
	return 0;
689258
      }
689258
    buf[buf_len] = 0;
689258
    return (*func) (pid, buf);
689258
  }
689258
689258
  return dir_scan (dirname, callback);
689258
}
689258
689258
static void pid_fs_scan (void (*func) (pid_t pid, void *data), void *data)
689258
{
689258
  int callback (struct dirent *dirent, const char *pathname)
689258
  {
689258
    if (dirent->d_type != DT_DIR
689258
	|| strspn (dirent->d_name, "0123456789") != strlen (dirent->d_name))
689258
      return 0;
689258
    (*func) (atoi (dirent->d_name), data);
689258
    return 0;
689258
  }
689258
689258
  dir_scan ("/proc", callback);
689258
}
689258
689258
static int rip_check_ptyname (pid_t pid, const char *link)
689258
{
689258
  assert (pid != getpid ());
689258
689258
  return strcmp (link, childptyname) == 0;
689258
}
689258
689258
struct pid
689258
  {
689258
    struct pid *next;
689258
    pid_t pid;
689258
  };
689258
static struct pid *pid_list;
689258
689258
static int pid_found (pid_t pid)
689258
{
689258
  struct pid *entry;
689258
689258
  for (entry = pid_list; entry != NULL; entry = entry->next)
689258
    if (entry->pid == pid)
689258
      return 1;
689258
  return 0;
689258
}
689258
689258
/* Single pass is not enough, a (multithreaded) process was seen to survive.
689258
   Repeated killing of the same process is not enough, zombies can be killed.
689258
   */
689258
static int cleanup_acted;
689258
689258
static void pid_record (pid_t pid)
689258
{
689258
  struct pid *entry;
689258
689258
  if (pid_found (pid))
689258
    return;
689258
  cleanup_acted = 1;
689258
689258
  entry = malloc (sizeof (*entry));
689258
  if (entry == NULL)
689258
    {
689258
      fprintf (stderr, "%s: malloc: %m\n", progname);
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  entry->pid = pid;
689258
  entry->next = pid_list;
689258
  pid_list = entry;
689258
}
689258
689258
static void pid_forall (void (*func) (pid_t pid))
689258
{
689258
  struct pid *entry;
689258
689258
  for (entry = pid_list; entry != NULL; entry = entry->next)
689258
    (*func) (entry->pid);
689258
}
689258
689258
/* Returns 0 on failure.  */
689258
static pid_t pid_get_parent (pid_t pid)
689258
{
689258
  char fname[64];
689258
  FILE *f;
689258
  char line[LINE_MAX];
689258
  pid_t retval = 0;
689258
689258
  if (snprintf (fname, sizeof fname, "/proc/%d/status", (int) pid) < 0)
689258
    {
689258
      perror ("snprintf(3)");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  f = fopen (fname, "r");
689258
  if (f == NULL)
689258
    {
689258
      return 0;
689258
    }
689258
  while (errno = 0, fgets (line, sizeof line, f) == line)
689258
    {
689258
      if (strncmp (line, "PPid:\t", sizeof "PPid:\t" - 1) != 0)
689258
	continue;
689258
      retval = atoi (line + sizeof "PPid:\t" - 1);
689258
      errno = 0;
689258
      break;
689258
    }
689258
  if (errno != 0)
689258
    {
689258
      fprintf (stderr, "%s: fgets (\"%s\"): %m\n", progname, fname);
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  if (fclose (f) != 0)
689258
    {
689258
      fprintf (stderr, "%s: fclose (\"%s\"): %m\n", progname, fname);
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  return retval;
689258
}
689258
689258
static void killtree (pid_t pid);
689258
689258
static void killtree_pid_fs_scan (pid_t pid, void *data)
689258
{
689258
  pid_t parent_pid = *(pid_t *) data;
689258
689258
  /* Do not optimize it as we could miss some newly spawned processes.
689258
     Always traverse all the leaves.  */
689258
#if 0
689258
  /* Optimization.  */
689258
  if (pid_found (pid))
689258
    return;
689258
#endif
689258
689258
  if (pid_get_parent (pid) != parent_pid)
689258
    return;
689258
689258
  killtree (pid);
689258
}
689258
689258
static void killtree (pid_t pid)
689258
{
689258
  pid_record (pid);
689258
  pid_fs_scan (killtree_pid_fs_scan, &pid;;
689258
}
689258
689258
static void rip_pid_fs_scan (pid_t pid, void *data)
689258
{
689258
  pid_t pgid;
689258
689258
  /* Shouldn't happen.  */
689258
  if (pid == getpid ())
689258
    return;
689258
689258
  /* Check both PGID and the stale file descriptors.  */
689258
  pgid = getpgid (pid);
689258
  if (pgid == child
689258
      || fd_fs_scan (pid, rip_check_ptyname) != 0)
689258
    killtree (pid);
689258
}
689258
689258
static void killproc (pid_t pid)
689258
{
689258
  const char *cmdline;
689258
689258
  cmdline = read_cmdline (pid);
689258
  /* Avoid printing the message for already gone processes.  */
689258
  if (kill (pid, 0) != 0 && errno == ESRCH)
689258
    return;
689258
  if (cmdline == NULL)
689258
    cmdline = "<error>";
689258
  fprintf (stderr, "%s: Killed -9 orphan PID %d: %s\n", progname, (int) pid, cmdline);
689258
  if (kill (pid, SIGKILL) == 0)
689258
    cleanup_acted = 1;
689258
  else if (errno != ESRCH)
689258
    fprintf (stderr, "%s: kill (%d, SIGKILL): %m\n", progname, (int) pid);
689258
  /* RHEL-3 kernels cannot SIGKILL a `T (stopped)' process.  */
689258
  kill (pid, SIGCONT);
689258
  /* Do not waitpid(2) as it cannot be our direct descendant and it gets
689258
     cleaned up by init(8).  */
689258
#if 0
689258
  pid_t pid_got;
689258
  pid_got = waitpid (pid, NULL, 0);
689258
  if (pid != pid_got)
689258
    {
689258
      fprintf (stderr, "%s: waitpid (%d) != %d: %m\n", progname,
689258
	 (int) pid, (int) pid_got);
689258
      return;
689258
    }
689258
#endif
689258
}
689258
689258
static void rip (void)
689258
{
689258
  cleanup_acted = 0;
689258
  do
689258
    {
689258
      if (cleanup_acted)
689258
        usleep (1000000 / 10);
689258
      cleanup_acted = 0;
689258
      pid_fs_scan (rip_pid_fs_scan, NULL);
689258
      pid_forall (killproc);
689258
    }
689258
  while (cleanup_acted);
689258
}
689258
689258
int main (int argc, char **argv)
689258
{
689258
  int timeout = 0;
689258
  int rc;
689258
689258
  progname = *argv++;
689258
  argc--;
689258
689258
  if (argc < 1 || strcmp (*argv, "-h") == 0
689258
      || strcmp (*argv, "--help") == 0)
689258
    {
689258
      puts ("Syntax: orphanripper [-t <seconds>] <execvp(3) commandline>");
689258
      exit (EXIT_FAILURE);
689258
    }
689258
  if ((*argv)[0] == '-' && (*argv)[1] == 't')
689258
    {
689258
      char *timeout_s = NULL;
689258
689258
      if ((*argv)[2] == 0)
689258
	timeout_s = *++argv;
689258
      else if (isdigit ((*argv)[2]))
689258
	timeout_s = (*argv) + 2;
689258
      if (timeout_s != NULL)
689258
	{
689258
	  long l;
689258
	  char *endptr;
689258
689258
	  argv++;
689258
	  l = strtol (timeout_s, &endptr, 0);
689258
	  timeout = l;
689258
	  if ((endptr != NULL && *endptr != 0) || timeout < 0 || timeout != l)
689258
	    {
689258
	      fprintf (stderr, "%s: Invalid timeout value: %s\n", progname,
689258
		       timeout_s);
689258
	      exit (EXIT_FAILURE);
689258
	    }
689258
	}
689258
    }
689258
689258
  rc = spawn (argv, timeout);
689258
  rip ();
689258
  return rc;
689258
}