2 * Copyright 2006-2007 Free Software Foundation, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Reap any leftover children possibly holding file descriptors.
19 * Children are identified by the open file descriptor.
20 * PGID or SID may be set by the children on their own.
21 * If we find a child by its file descriptor, we kill it will all its process
22 * tree (grandchildren).
23 * The child process is run with `2>&1' redirection (due to forkpty(3)).
24 * 2007-07-10 Jan Kratochvil <jan.kratochvil@redhat.com>
27 #define _XOPEN_SOURCE 1
28 #define _XOPEN_SOURCE_EXTENDED 1
33 #include <sys/types.h>
45 static const char *progname;
47 static int signal_child_hit = 0;
49 static void signal_child (int signo)
54 static char childptyname[LINE_MAX];
56 static int spawn (char **argv)
58 pid_t child, child_got;
59 int status, amaster, i;
64 /* We do not use signal(2) to be sure we have SA_RESTART unset. */
65 memset (&act, 0, sizeof (act));
66 act.sa_handler = signal_child;
67 i = sigemptyset (&act.sa_mask);
70 perror ("sigemotyset(3)");
74 i = sigaction (SIGCHLD, &act, NULL);
77 perror ("sigaction(2)");
81 child = forkpty (&amaster, childptyname, NULL, NULL);
85 perror ("forkpty(3)");
88 execvp (argv[1], argv + 1);
96 buf_got = read (amaster, buf, sizeof buf);
99 assert (signal_child_hit != 0);
100 assert (errno == EINTR || errno == EIO);
104 if (write (STDOUT_FILENO, buf, buf_got) != buf_got)
110 child_got = waitpid (child, &status, 0);
111 if (child != child_got)
113 fprintf (stderr, "waitpid (%d) = %d: %m\n", (int) child, (int) child_got);
116 if (!WIFEXITED (status))
118 fprintf (stderr, "waitpid (%d): !WIFEXITED (%d)\n", (int) child, status);
121 /* Do not close the master FD as the child would have `/dev/pts/23 (deleted)'
122 entries which are not expected (and expecting ` (deleted)' would be
128 perror ("close (forkpty ()'s amaster)");
133 return WEXITSTATUS (status);
136 /* Detected commandline may look weird due to a race:
138 ./orphanripper sh -c 'sleep 1&' &
141 ./orphanripper: Killed -9 orphan PID 29612 (PGID 29611): sleep 1
142 Raced output (sh(1) child still did not update its argv[]):
144 ./orphanripper: Killed -9 orphan PID 29615 (PGID 29614): sh -c sleep 1&
145 We could delay a bit before ripping the children. */
146 static const char *read_cmdline (pid_t pid)
148 char cmdline_fname[32];
149 static char cmdline[LINE_MAX];
154 if (snprintf (cmdline_fname, sizeof cmdline_fname, "/proc/%d/cmdline",
157 fd = open (cmdline_fname, O_RDONLY);
160 /* It may have already exited - ENOENT. */
162 fprintf (stderr, "%s: open (\"%s\"): %m\n", progname, cmdline_fname);
166 got = read (fd, cmdline, sizeof (cmdline) - 1);
168 fprintf (stderr, "%s: read (\"%s\"): %m\n", progname,
171 fprintf (stderr, "%s: close (\"%s\"): %m\n", progname,
175 /* Convert '\0' argument delimiters to spaces. */
176 for (s = cmdline; s < cmdline + got; s++)
179 /* Trim the trailing spaces (typically single '\0'->' '). */
180 while (s > cmdline && isspace (s[-1]))
186 static int fd_fs_scan (pid_t pid, int (*func) (pid_t pid, const char *link))
189 struct dirent *dirent;
193 if (snprintf (dirname, sizeof dirname, "/proc/%d/fd", (int) pid) < 0)
195 perror ("snprintf(3)");
198 dir = opendir (dirname);
203 fprintf (stderr, "%s: opendir (\"%s\"): %m\n", progname, dirname);
206 while ((errno = 0, dirent = readdir (dir)))
208 char linkname[LINE_MAX], buf[LINE_MAX];
212 /* FIXME: POSIX portability. */
213 if ((dirent->d_type != DT_DIR && dirent->d_type != DT_LNK)
214 || (dirent->d_type == DT_DIR && strcmp (dirent->d_name, ".") != 0
215 && strcmp (dirent->d_name, "..") != 0)
216 || (dirent->d_type == DT_LNK && strspn (dirent->d_name, "0123456789")
217 != strlen (dirent->d_name)))
219 fprintf (stderr, "Unexpected entry \"%s\" on readdir (\"%s\"): %m\n",
220 dirent->d_name, dirname);
223 if (dirent->d_type == DT_DIR)
225 linkname_len = snprintf (linkname, sizeof linkname, "%s/%s", dirname,
227 if (linkname_len <= 0 || linkname_len >= sizeof linkname)
229 fprintf (stderr, "Link content too long: `%s' / `%s'\n",
230 dirent->d_name, dirent->d_name);
233 buf_len = readlink (linkname, buf, sizeof buf - 1);
234 if (buf_len <= 0 || buf_len >= sizeof buf - 1)
236 fprintf (stderr, "Error reading link \"%s\": %m\n",
241 rc = (*func) (pid, buf);
250 fprintf (stderr, "%s: readdir (\"%s\"): %m\n", progname, dirname);
253 if (closedir (dir) != 0)
255 fprintf (stderr, "%s: closedir (\"%s\"): %m\n", progname, dirname);
261 static void pid_fs_scan (void (*func) (pid_t pid, void *data), void *data)
264 struct dirent *dirent;
266 dir = opendir ("/proc");
269 perror ("opendir (\"/proc\")");
272 while ((errno = 0, dirent = readdir (dir)))
274 /* FIXME: POSIX portability. */
275 if (dirent->d_type != DT_DIR
276 || strspn (dirent->d_name, "0123456789") != strlen (dirent->d_name))
278 (*func) (atoi (dirent->d_name), data);
282 perror ("readdir (\"/proc\")");
285 if (closedir (dir) != 0)
287 perror ("closedir (\"/proc\")");
292 static int rip_check (pid_t pid, const char *link)
294 assert (pid != getpid ());
296 return strcmp (link, childptyname) == 0;
304 static struct pid *pid_list;
306 static int pid_found (pid_t pid)
310 for (entry = pid_list; entry != NULL; entry = entry->next)
311 if (entry->pid == pid)
316 static void pid_record (pid_t pid)
323 entry = malloc (sizeof (*entry));
326 fprintf (stderr, "%s: malloc: %m\n", progname);
330 entry->next = pid_list;
334 static void pid_forall (void (*func) (pid_t pid))
338 for (entry = pid_list; entry != NULL; entry = entry->next)
339 (*func) (entry->pid);
342 /* Returns 0 on failure. */
343 static pid_t pid_get_parent (pid_t pid)
350 if (snprintf (fname, sizeof fname, "/proc/%d/status", (int) pid) < 0)
352 perror ("snprintf(3)");
355 f = fopen (fname, "r");
360 while (errno = 0, fgets (line, sizeof line, f) == line)
362 if (strncmp (line, "PPid:\t", sizeof "PPid:\t" - 1) != 0)
364 retval = atoi (line + sizeof "PPid:\t" - 1);
370 fprintf (stderr, "%s: fgets (\"%s\"): %m\n", progname, fname);
375 fprintf (stderr, "%s: fclose (\"%s\"): %m\n", progname, fname);
381 static void killtree (pid_t pid);
383 static void killtree_pid_fs_scan (pid_t pid, void *data)
385 pid_t parent_pid = *(pid_t *) data;
391 if (pid_get_parent (pid) != parent_pid)
397 static void killtree (pid_t pid)
400 pid_fs_scan (killtree_pid_fs_scan, &pid);
403 static void rip_pid_fs_scan (pid_t pid, void *data)
405 /* Shouldn't happen. */
406 if (pid == getpid ())
409 if (fd_fs_scan (pid, rip_check) != 0)
413 static void killproc (pid_t pid)
417 cmdline = read_cmdline (pid);
418 if (kill (pid, 0) != 0 && errno == ESRCH)
422 fprintf (stderr, "%s: Killed -9 orphan PID %d: %s\n", progname, (int) pid, cmdline);
423 if (kill (pid, SIGKILL)) {
424 fprintf (stderr, "%s: kill (%d, SIGKILL): %m\n", progname,
428 /* Do not waitpid(2) as it cannot be our direct descendant and it gets
429 cleaned up by init(8). */
432 pid_got = waitpid (pid, NULL, 0);
435 fprintf (stderr, "%s: waitpid (%d) != %d: %m\n", progname,
436 (int) pid, (int) pid_got);
444 pid_fs_scan (rip_pid_fs_scan, NULL);
445 pid_forall (killproc);
448 int main (int argc, char **argv)
452 if (argc < 2 || strcmp (argv[1], "-h") == 0
453 || strcmp (argv[1], "--help") == 0)
455 fputs ("Syntax: orphanripper <execvp(3) commandline>\n", stdout);