xref: /titanic_51/usr/src/cmd/ptools/pstop/pstop.c (revision 672986541be54a7a471bb088e60780c37e371d7e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <signal.h>
36 #include <libproc.h>
37 
38 static	int	stop(char *);
39 static	int	lwpstop(int *, const lwpstatus_t *, const lwpsinfo_t *);
40 
41 static	char	*command;
42 static	const char *lwps;
43 static	struct	ps_prochandle *P;
44 
45 int
46 main(int argc, char **argv)
47 {
48 	int rc = 0;
49 
50 	if ((command = strrchr(argv[0], '/')) != NULL)
51 		command++;
52 	else
53 		command = argv[0];
54 
55 	if (argc <= 1) {
56 		(void) fprintf(stderr, "usage:\t%s pid[/lwps] ...\n", command);
57 		(void) fprintf(stderr,
58 		    "  (stop processes or lwps with /proc request)\n");
59 		return (2);
60 	}
61 
62 	while (--argc > 0)
63 		rc += stop(*++argv);
64 
65 	return (rc);
66 }
67 
68 static int
69 stop(char *arg)
70 {
71 	int gcode;
72 	int rc = 0;
73 
74 	if ((P = proc_arg_xgrab(arg, NULL, PR_ARG_PIDS, PGRAB_RETAIN |
75 	    PGRAB_NOSTOP | PGRAB_FORCE, &gcode, &lwps)) == NULL) {
76 		(void) fprintf(stderr, "%s: cannot control %s: %s\n",
77 		    command, arg, Pgrab_error(gcode));
78 		return (1);
79 	} else if (lwps != NULL) {
80 		/*
81 		 * The user has provided an lwp specification.  Let's consider
82 		 * the lwp specification as a mask.  We iterate over all lwps in
83 		 * the process and stop every lwp, which matches the mask.  If
84 		 * there is no lwp matching the mask or an error occured during
85 		 * the iteration, set the return code to 1 as indication of an
86 		 * error.
87 		 */
88 		int lwpcount = 0;
89 
90 		(void) Plwp_iter_all(P, (proc_lwp_all_f *)lwpstop, &lwpcount);
91 		if (lwpcount == 0) {
92 			(void) fprintf(stderr, "%s: cannot control %s:"
93 			    " no matching LWPs found\n", command, arg);
94 			rc = 1;
95 		} else if (lwpcount == -1)
96 			rc = 1;
97 	} else {
98 		(void) Pdstop(P);	/* Stop the process. */
99 	}
100 
101 	/*
102 	 * Prelease could change the tracing flags, use Pfree and unset
103 	 * run-on-last-close flag to prevent the process being set running
104 	 * after detaching from it.
105 	 */
106 	(void) Punsetflags(P, PR_RLC);
107 	Pfree(P);
108 	return (rc);
109 }
110 
111 /* ARGSUSED */
112 static int
113 lwpstop(int *lwpcount, const lwpstatus_t *status, const lwpsinfo_t *info)
114 {
115 	struct ps_lwphandle *L;
116 	int gcode;
117 
118 	if (proc_lwp_in_set(lwps, info->pr_lwpid)) {
119 		/*
120 		 * There is a race between the callback from the iterator and
121 		 * grabbing of the lwp.  If the lwp has already exited, Lgrab
122 		 * will return the error code G_NOPROC.  It's not a real error,
123 		 * only if there is no lwp matching the specification.
124 		 */
125 		if ((L = Lgrab(P, info->pr_lwpid, &gcode)) != NULL) {
126 			(void) Ldstop(L);
127 			Lfree(L);
128 			if (*lwpcount >= 0)
129 				(*lwpcount)++;
130 		} else if (gcode != G_NOPROC) {
131 			(void) fprintf(stderr, "%s: cannot control %d/%d: %s\n",
132 			    command, (int)Pstatus(P)->pr_pid,
133 			    (int)info->pr_lwpid, Lgrab_error(gcode));
134 			*lwpcount = -1;
135 		}
136 	}
137 	return (0);
138 }
139