2 * Copyright 2006 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 * 2006-11-30 Jan Kratochvil <jan.kratochvil@redhat.com>
23 #define _XOPEN_SOURCE 1
24 #define _XOPEN_SOURCE_EXTENDED 1
29 #include <sys/types.h>
40 static const char *progname;
42 static int spawn (char **argv, pid_t *child_pointer)
44 pid_t child, child_got;
47 switch (child = fork ()) {
52 /* Do not setpgrp(2) in the parent process as the process-group
53 is shared for the whole sh(1) pipeline we could be a part
54 of. The process-group is set according to PID of the first
55 command in the pipeline.
56 We would rip even vi(1) in the case of:
57 ./orphanripper sh -c 'sleep 1&' | vi -
63 execvp (argv[1], argv + 1);
69 if (child != (child_got = waitpid (child, &status, 0))) {
70 fprintf (stderr, "waitpid(%d)=%d: %m\n", (int) child,
74 if (!WIFEXITED (status)) {
75 fprintf (stderr, "waitpid(2): !WIFEXITED(%d)\n", status);
80 *child_pointer = child;
81 return WEXITSTATUS (status);
84 /* Detected commandline may look weird due to a race:
86 ./orphanripper sh -c 'sleep 1&' &
89 ./orphanripper: Killed -9 orphan PID 29612 (PGID 29611): sleep 1
90 Raced output (sh(1) child still did not update its argv[]):
92 ./orphanripper: Killed -9 orphan PID 29615 (PGID 29614): sh -c sleep 1&
93 We could delay a bit before ripping the children. */
94 static const char *read_cmdline (pid_t pid)
96 char cmdline_fname[32];
97 static char cmdline[LINE_MAX];
102 if (snprintf (cmdline_fname, sizeof (cmdline_fname), "/proc/%d/cmdline",
105 fd = open (cmdline_fname, O_RDONLY);
107 fprintf (stderr, "%s: open (\"%s\"): %m\n", progname,
111 got = read (fd, cmdline, sizeof (cmdline) - 1);
113 fprintf (stderr, "%s: read (\"%s\"): %m\n", progname,
116 fprintf (stderr, "%s: close (\"%s\"): %m\n", progname,
120 /* Convert '\0' argument delimiters to spaces. */
121 for (s = cmdline; s < cmdline + got; s++)
124 /* Trim the trailing spaces (typically single '\0'->' '). */
125 while (s > cmdline && isspace (s[-1]))
131 static void rip_pid (pid_t pid, pid_t pgid_child)
136 /* Don't shoot ourselves. */
139 pgid_pids = getpgid (pid);
140 /* Ignore errors (permissions? untested). */
143 /* Not a process of ours. */
144 if (pgid_pids != pgid_child)
147 cmdline = read_cmdline (pid);
150 fprintf (stderr, "%s: Killed -9 orphan PID %d (PGID %d): %s\n",
151 progname, (int) pid, (int) pgid_pids, cmdline);
152 if (kill (pid, SIGKILL)) {
153 fprintf (stderr, "%s: kill (%d, SIGKILL): %m\n", progname,
157 /* Do not waitpid(2) as it cannot be our direct descendant and it gets
158 cleaned up by init(8). */
161 if (pid != (pid_got = waitpid (pid, NULL, 0))) {
162 fprintf (stderr, "%s: waitpid (%d) != %d: %m\n", progname,
163 (int) pid, (int) pid_got);
169 static void rip (pid_t pgid_child)
172 struct dirent *dirent;
174 dir = opendir ("/proc");
176 perror ("opendir (\"/proc\")");
179 while ((errno = 0, dirent = readdir (dir))) {
182 /* FIXME: POSIX portability. */
183 if (dirent->d_type != DT_DIR)
186 for (cs = dirent->d_name; *cs; cs++)
189 if (cs == dirent->d_name || *cs)
191 rip_pid (atoi (dirent->d_name), pgid_child);
194 perror ("readdir (\"/proc\")");
197 if (closedir (dir)) {
198 perror ("closedir (\"/proc\")");
204 int main (int argc, char **argv)
210 || !strcmp (argv[1], "-h")
211 || !strcmp (argv[1], "--help")) {
212 fputs("Syntax: orphanripper <execvp(3) commandline>\n", stdout);
216 rc = spawn (argv, &child);