4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
37 #include <sys/types.h>
41 #define NOREAP_TIME 60 /* wait 60 seconds before allow a reap */
43 static volatile int interrupt
;
54 open_usage(pid_t pid
, int *perr
)
60 (void) snprintf(path
, sizeof (path
), "/proc/%d/usage", (int)pid
);
63 * Attempt to open the usage file, and return the fd if we can
64 * confirm this is a regular file provided by /proc.
66 if ((fd
= open64(path
, O_RDONLY
)) >= 0) {
67 if (fstat64(fd
, &st
) != 0 || !S_ISREG(st
.st_mode
) ||
68 strcmp(st
.st_fstype
, "proc") != 0) {
72 } else if (errno
== EACCES
|| errno
== EPERM
)
79 proc_usage(pid_t pid
, prusage_t
*pup
, int *perr
)
85 if ((fd
= open_usage(pid
, perr
)) != -1) {
86 if (read(fd
, pup
, sizeof (prusage_t
)) == sizeof (prusage_t
)) {
93 * If the read failed, the process may have gone away.
101 * Force the parent process (ppid) to wait for its child process (pid).
104 reap(char *arg
, pid_t
*reap_pid
, int *exit_status
)
106 struct ps_prochandle
*Pr
;
115 * get the specified pid and the psinfo struct
117 if ((pid
= proc_arg_psinfo(arg
, PR_ARG_PIDS
, &psinfo
, &gret
)) == -1) {
118 (void) fprintf(stderr
, "%s: cannot examine %s: %s\n",
119 command
, arg
, Pgrab_error(gret
));
123 if (psinfo
.pr_nlwp
!= 0) {
124 (void) fprintf(stderr
, "%s: process not defunct: %d\n",
129 *exit_status
= psinfo
.pr_wstat
;
130 *reap_pid
= psinfo
.pr_pid
;
131 ppid
= psinfo
.pr_ppid
;
134 (void) fprintf(stderr
, "%s: Failed to reap %d: the only "
135 "non-defunct ancestor is 'init'\n", command
,
140 if (proc_usage(pid
, &usage
, &gret
) == 0) {
141 elapsed
= usage
.pr_tstamp
.tv_sec
- usage
.pr_term
.tv_sec
;
143 (void) fprintf(stderr
, "%s: cannot examine %d: %s\n",
144 command
, (int)pid
, Pgrab_error(gret
));
148 if ((Fflag
== 0) && (elapsed
< NOREAP_TIME
)) {
149 (void) fprintf(stderr
, "%s: unsafe to reap %d; it has been "
150 "defunct less than %d seconds\n", command
, (int)pid
,
155 if ((Pr
= Pgrab(ppid
, Fflag
| PGRAB_NOSTOP
, &gret
)) == NULL
) {
156 (void) fprintf(stderr
, "%s: cannot examine %d: %s\n", command
,
157 (int)ppid
, Pgrab_error(gret
));
161 if ((Fflag
== 0) && (Pstate(Pr
) == PS_STOP
)) {
163 (void) fprintf(stderr
, "%s: unsafe to reap %d; parent is "
164 "stopped and may reap status upon restart\n", command
,
170 * Pstop() will fail if the process to be stopped has become a zombie.
171 * This means that we can say with certainty that the child of this
172 * process has not changed parents (i.e. been reparented to init) once
173 * the Pstop() succeeds.
175 if (Pstop(Pr
, 1000) != 0) {
177 (void) fprintf(stderr
, "%s: failed to stop %d: %s", command
,
178 (int)ppid
, strerror(errno
));
182 if (pr_waitid(Pr
, P_PID
, pid
, &siginfo
, WEXITED
|WNOHANG
) != 0) {
184 (void) fprintf(stderr
, "%s: waitid() in process %d failed: %s",
185 command
, (int)ppid
, strerror(errno
));
194 print_exit_status(pid_t pid
, int wstat
)
196 (void) printf("%d: ", (int)pid
);
197 if (WIFSIGNALED(wstat
)) {
198 char buf
[SIG2STR_MAX
];
199 int sig
= WTERMSIG(wstat
);
201 if (sig2str(sig
, buf
) == 0)
202 (void) printf("killed by signal %s", buf
);
204 (void) printf("killed by signal %d", sig
);
206 if (WCOREDUMP(wstat
))
207 (void) printf(" (core dumped)");
209 (void) printf("exited with status %d", WEXITSTATUS(wstat
));
215 main(int argc
, char *argv
[])
221 if ((command
= strrchr(argv
[0], '/')) != NULL
)
226 while ((opt
= getopt(argc
, argv
, "F")) != EOF
) {
228 case 'F': /* force grabbing (no O_EXCL) */
240 if (errflg
|| argc
<= 0) {
241 (void) fprintf(stderr
, "usage: %s pid ...\n", command
);
242 (void) fprintf(stderr
, " (Reap a defunct process by forcing "
243 "its parent to wait(2) for it)\n");
247 /* catch signals from terminal */
248 if (sigset(SIGHUP
, SIG_IGN
) == SIG_DFL
)
249 (void) sigset(SIGHUP
, intr
);
250 if (sigset(SIGINT
, SIG_IGN
) == SIG_DFL
)
251 (void) sigset(SIGINT
, intr
);
252 if (sigset(SIGPIPE
, SIG_IGN
) == SIG_DFL
)
253 (void) sigset(SIGPIPE
, intr
);
254 if (sigset(SIGQUIT
, SIG_IGN
) == SIG_DFL
)
255 (void) sigset(SIGQUIT
, intr
);
256 (void) sigset(SIGTERM
, intr
);
258 while (--argc
>= 0 && !interrupt
) {
262 retc
+= r
= reap(*argv
++, &pid
, &wstat
);
265 print_exit_status(pid
, wstat
);
268 if (interrupt
&& retc
== 0)
270 return (retc
== 0 ? 0 : 1);