1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <unistd.h>
32*0Sstevel@tonic-gate #include <fcntl.h>
33*0Sstevel@tonic-gate #include <ctype.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <signal.h>
36*0Sstevel@tonic-gate #include <errno.h>
37*0Sstevel@tonic-gate #include <sys/types.h>
38*0Sstevel@tonic-gate #include <sys/wait.h>
39*0Sstevel@tonic-gate #include <libproc.h>
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #define NOREAP_TIME 60 /* wait 60 seconds before allow a reap */
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate static volatile int interrupt;
44*0Sstevel@tonic-gate static int Fflag;
45*0Sstevel@tonic-gate static char *command;
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate static void
intr(int sig)48*0Sstevel@tonic-gate intr(int sig)
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate interrupt = sig;
51*0Sstevel@tonic-gate }
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate static int
open_usage(pid_t pid,int * perr)54*0Sstevel@tonic-gate open_usage(pid_t pid, int *perr)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate char path[64];
57*0Sstevel@tonic-gate struct stat64 st;
58*0Sstevel@tonic-gate int fd;
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "/proc/%d/usage", (int)pid);
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate * Attempt to open the usage file, and return the fd if we can
64*0Sstevel@tonic-gate * confirm this is a regular file provided by /proc.
65*0Sstevel@tonic-gate */
66*0Sstevel@tonic-gate if ((fd = open64(path, O_RDONLY)) >= 0) {
67*0Sstevel@tonic-gate if (fstat64(fd, &st) != 0 || !S_ISREG(st.st_mode) ||
68*0Sstevel@tonic-gate strcmp(st.st_fstype, "proc") != 0) {
69*0Sstevel@tonic-gate (void) close(fd);
70*0Sstevel@tonic-gate fd = -1;
71*0Sstevel@tonic-gate }
72*0Sstevel@tonic-gate } else if (errno == EACCES || errno == EPERM)
73*0Sstevel@tonic-gate *perr = G_PERM;
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate return (fd);
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate static int
proc_usage(pid_t pid,prusage_t * pup,int * perr)79*0Sstevel@tonic-gate proc_usage(pid_t pid, prusage_t *pup, int *perr)
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate int fd;
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gate *perr = G_NOPROC;
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate if ((fd = open_usage(pid, perr)) != -1) {
86*0Sstevel@tonic-gate if (read(fd, pup, sizeof (prusage_t)) == sizeof (prusage_t)) {
87*0Sstevel@tonic-gate *perr = 0;
88*0Sstevel@tonic-gate (void) close(fd);
89*0Sstevel@tonic-gate return (0);
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate /*
93*0Sstevel@tonic-gate * If the read failed, the process may have gone away.
94*0Sstevel@tonic-gate */
95*0Sstevel@tonic-gate (void) close(fd);
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate return (-1);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate /*
101*0Sstevel@tonic-gate * Force the parent process (ppid) to wait for its child process (pid).
102*0Sstevel@tonic-gate */
103*0Sstevel@tonic-gate static int
reap(char * arg,pid_t * reap_pid,int * exit_status)104*0Sstevel@tonic-gate reap(char *arg, pid_t *reap_pid, int *exit_status)
105*0Sstevel@tonic-gate {
106*0Sstevel@tonic-gate struct ps_prochandle *Pr;
107*0Sstevel@tonic-gate siginfo_t siginfo;
108*0Sstevel@tonic-gate psinfo_t psinfo;
109*0Sstevel@tonic-gate prusage_t usage;
110*0Sstevel@tonic-gate pid_t pid, ppid;
111*0Sstevel@tonic-gate time_t elapsed;
112*0Sstevel@tonic-gate int gret;
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate * get the specified pid and the psinfo struct
116*0Sstevel@tonic-gate */
117*0Sstevel@tonic-gate if ((pid = proc_arg_psinfo(arg, PR_ARG_PIDS, &psinfo, &gret)) == -1) {
118*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
119*0Sstevel@tonic-gate command, arg, Pgrab_error(gret));
120*0Sstevel@tonic-gate return (1);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate if (psinfo.pr_nlwp != 0) {
124*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: process not defunct: %d\n",
125*0Sstevel@tonic-gate command, (int)pid);
126*0Sstevel@tonic-gate return (1);
127*0Sstevel@tonic-gate }
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate *exit_status = psinfo.pr_wstat;
130*0Sstevel@tonic-gate *reap_pid = psinfo.pr_pid;
131*0Sstevel@tonic-gate ppid = psinfo.pr_ppid;
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate if (ppid == 1) {
134*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: Failed to reap %d: the only "
135*0Sstevel@tonic-gate "non-defunct ancestor is 'init'\n", command,
136*0Sstevel@tonic-gate (int)pid);
137*0Sstevel@tonic-gate return (1);
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate if (proc_usage(pid, &usage, &gret) == 0) {
141*0Sstevel@tonic-gate elapsed = usage.pr_tstamp.tv_sec - usage.pr_term.tv_sec;
142*0Sstevel@tonic-gate } else {
143*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %d: %s\n",
144*0Sstevel@tonic-gate command, (int)pid, Pgrab_error(gret));
145*0Sstevel@tonic-gate return (1);
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate if ((Fflag == 0) && (elapsed < NOREAP_TIME)) {
149*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: unsafe to reap %d; it has been "
150*0Sstevel@tonic-gate "defunct less than %d seconds\n", command, (int)pid,
151*0Sstevel@tonic-gate NOREAP_TIME);
152*0Sstevel@tonic-gate return (1);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate if ((Pr = Pgrab(ppid, Fflag | PGRAB_NOSTOP, &gret)) == NULL) {
156*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot examine %d: %s\n", command,
157*0Sstevel@tonic-gate (int)ppid, Pgrab_error(gret));
158*0Sstevel@tonic-gate return (1);
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate if ((Fflag == 0) && (Pstate(Pr) == PS_STOP)) {
162*0Sstevel@tonic-gate Prelease(Pr, 0);
163*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: unsafe to reap %d; parent is "
164*0Sstevel@tonic-gate "stopped and may reap status upon restart\n", command,
165*0Sstevel@tonic-gate (int)pid);
166*0Sstevel@tonic-gate return (1);
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate /*
170*0Sstevel@tonic-gate * Pstop() will fail if the process to be stopped has become a zombie.
171*0Sstevel@tonic-gate * This means that we can say with certainty that the child of this
172*0Sstevel@tonic-gate * process has not changed parents (i.e. been reparented to init) once
173*0Sstevel@tonic-gate * the Pstop() succeeds.
174*0Sstevel@tonic-gate */
175*0Sstevel@tonic-gate if (Pstop(Pr, 1000) != 0) {
176*0Sstevel@tonic-gate Prelease(Pr, 0);
177*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: failed to stop %d: %s", command,
178*0Sstevel@tonic-gate (int)ppid, strerror(errno));
179*0Sstevel@tonic-gate return (1);
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate if (pr_waitid(Pr, P_PID, pid, &siginfo, WEXITED|WNOHANG) != 0) {
183*0Sstevel@tonic-gate Prelease(Pr, 0);
184*0Sstevel@tonic-gate (void) fprintf(stderr, "%s: waitid() in process %d failed: %s",
185*0Sstevel@tonic-gate command, (int)ppid, strerror(errno));
186*0Sstevel@tonic-gate return (1);
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gate Prelease(Pr, 0);
190*0Sstevel@tonic-gate return (0);
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate static void
print_exit_status(pid_t pid,int wstat)194*0Sstevel@tonic-gate print_exit_status(pid_t pid, int wstat)
195*0Sstevel@tonic-gate {
196*0Sstevel@tonic-gate (void) printf("%d: ", (int)pid);
197*0Sstevel@tonic-gate if (WIFSIGNALED(wstat)) {
198*0Sstevel@tonic-gate char buf[SIG2STR_MAX];
199*0Sstevel@tonic-gate int sig = WTERMSIG(wstat);
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate if (sig2str(sig, buf) == 0)
202*0Sstevel@tonic-gate (void) printf("killed by signal %s", buf);
203*0Sstevel@tonic-gate else
204*0Sstevel@tonic-gate (void) printf("killed by signal %d", sig);
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate if (WCOREDUMP(wstat))
207*0Sstevel@tonic-gate (void) printf(" (core dumped)");
208*0Sstevel@tonic-gate } else {
209*0Sstevel@tonic-gate (void) printf("exited with status %d", WEXITSTATUS(wstat));
210*0Sstevel@tonic-gate }
211*0Sstevel@tonic-gate (void) printf("\n");
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gate int
main(int argc,char * argv[])215*0Sstevel@tonic-gate main(int argc, char *argv[])
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate int retc = 0;
218*0Sstevel@tonic-gate int opt;
219*0Sstevel@tonic-gate int errflg = 0;
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL)
222*0Sstevel@tonic-gate command++;
223*0Sstevel@tonic-gate else
224*0Sstevel@tonic-gate command = argv[0];
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate while ((opt = getopt(argc, argv, "F")) != EOF) {
227*0Sstevel@tonic-gate switch (opt) {
228*0Sstevel@tonic-gate case 'F': /* force grabbing (no O_EXCL) */
229*0Sstevel@tonic-gate Fflag = PGRAB_FORCE;
230*0Sstevel@tonic-gate break;
231*0Sstevel@tonic-gate default:
232*0Sstevel@tonic-gate errflg = 1;
233*0Sstevel@tonic-gate break;
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate }
236*0Sstevel@tonic-gate
237*0Sstevel@tonic-gate argc -= optind;
238*0Sstevel@tonic-gate argv += optind;
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gate if (errflg || argc <= 0) {
241*0Sstevel@tonic-gate (void) fprintf(stderr, "usage: %s pid ...\n", command);
242*0Sstevel@tonic-gate (void) fprintf(stderr, " (Reap a defunct process by forcing "
243*0Sstevel@tonic-gate "its parent to wait(2) for it)\n");
244*0Sstevel@tonic-gate exit(2);
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate /* catch signals from terminal */
248*0Sstevel@tonic-gate if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
249*0Sstevel@tonic-gate (void) sigset(SIGHUP, intr);
250*0Sstevel@tonic-gate if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
251*0Sstevel@tonic-gate (void) sigset(SIGINT, intr);
252*0Sstevel@tonic-gate if (sigset(SIGPIPE, SIG_IGN) == SIG_DFL)
253*0Sstevel@tonic-gate (void) sigset(SIGPIPE, intr);
254*0Sstevel@tonic-gate if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
255*0Sstevel@tonic-gate (void) sigset(SIGQUIT, intr);
256*0Sstevel@tonic-gate (void) sigset(SIGTERM, intr);
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gate while (--argc >= 0 && !interrupt) {
259*0Sstevel@tonic-gate pid_t pid;
260*0Sstevel@tonic-gate int wstat, r;
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate retc += r = reap(*argv++, &pid, &wstat);
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate if (r == 0)
265*0Sstevel@tonic-gate print_exit_status(pid, wstat);
266*0Sstevel@tonic-gate }
267*0Sstevel@tonic-gate
268*0Sstevel@tonic-gate if (interrupt && retc == 0)
269*0Sstevel@tonic-gate retc++;
270*0Sstevel@tonic-gate return (retc == 0 ? 0 : 1);
271*0Sstevel@tonic-gate }
272