Blame SOURCES/gdb-orphanripper.c

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