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