xref: /illumos-gate/usr/src/cmd/ptools/pstop/pstop.c (revision 0dd92943508086ca1800de461a7c66109737bcd5)
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 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <signal.h>
34 #include <libproc.h>
35 
36 static	int	stop(char *);
37 static	int	lwpstop(int *, const lwpstatus_t *, const lwpsinfo_t *);
38 
39 static	char	*command;
40 static	const char *lwps;
41 static	struct	ps_prochandle *P;
42 
43 int
44 main(int argc, char **argv)
45 {
46 	int rc = 0;
47 
48 	if ((command = strrchr(argv[0], '/')) != NULL)
49 		command++;
50 	else
51 		command = argv[0];
52 
53 	if (argc <= 1) {
54 		(void) fprintf(stderr, "usage:\t%s pid[/lwps] ...\n", command);
55 		(void) fprintf(stderr,
56 		    "  (stop processes or lwps with /proc request)\n");
57 		return (2);
58 	}
59 
60 	while (--argc > 0)
61 		rc += stop(*++argv);
62 
63 	return (rc);
64 }
65 
66 static int
67 stop(char *arg)
68 {
69 	int gcode;
70 	int rc = 0;
71 
72 	if ((P = proc_arg_xgrab(arg, NULL, PR_ARG_PIDS, PGRAB_RETAIN |
73 	    PGRAB_NOSTOP | PGRAB_FORCE, &gcode, &lwps)) == NULL) {
74 		(void) fprintf(stderr, "%s: cannot control %s: %s\n",
75 		    command, arg, Pgrab_error(gcode));
76 		return (1);
77 	} else if (lwps != NULL) {
78 		/*
79 		 * The user has provided an lwp specification.  Let's consider
80 		 * the lwp specification as a mask.  We iterate over all lwps in
81 		 * the process and stop every lwp, which matches the mask.  If
82 		 * there is no lwp matching the mask or an error occured during
83 		 * the iteration, set the return code to 1 as indication of an
84 		 * error.
85 		 */
86 		int lwpcount = 0;
87 
88 		(void) Plwp_iter_all(P, (proc_lwp_all_f *)lwpstop, &lwpcount);
89 		if (lwpcount == 0) {
90 			(void) fprintf(stderr, "%s: cannot control %s:"
91 			    " no matching LWPs found\n", command, arg);
92 			rc = 1;
93 		} else if (lwpcount == -1)
94 			rc = 1;
95 	} else {
96 		(void) Pdstop(P);	/* Stop the process. */
97 	}
98 
99 	/*
100 	 * Prelease could change the tracing flags, use Pfree and unset
101 	 * run-on-last-close flag to prevent the process being set running
102 	 * after detaching from it.
103 	 */
104 	(void) Punsetflags(P, PR_RLC);
105 	Pfree(P);
106 	return (rc);
107 }
108 
109 /* ARGSUSED */
110 static int
111 lwpstop(int *lwpcount, const lwpstatus_t *status, const lwpsinfo_t *info)
112 {
113 	struct ps_lwphandle *L;
114 	int gcode;
115 
116 	if (proc_lwp_in_set(lwps, info->pr_lwpid)) {
117 		/*
118 		 * There is a race between the callback from the iterator and
119 		 * grabbing of the lwp.  If the lwp has already exited, Lgrab
120 		 * will return the error code G_NOPROC.  It's not a real error,
121 		 * only if there is no lwp matching the specification.
122 		 */
123 		if ((L = Lgrab(P, info->pr_lwpid, &gcode)) != NULL) {
124 			(void) Ldstop(L);
125 			Lfree(L);
126 			if (*lwpcount >= 0)
127 				(*lwpcount)++;
128 		} else if (gcode != G_NOPROC) {
129 			(void) fprintf(stderr, "%s: cannot control %d/%d: %s\n",
130 			    command, (int)Pstatus(P)->pr_pid,
131 			    (int)info->pr_lwpid, Lgrab_error(gcode));
132 			*lwpcount = -1;
133 		}
134 	}
135 	return (0);
136 }
137