xref: /illumos-gate/usr/src/cmd/ptools/pstop/pstop.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*b06cdb87Svb  * Common Development and Distribution License (the "License").
6*b06cdb87Svb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*b06cdb87Svb  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*b06cdb87Svb  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <signal.h>
347c478bd9Sstevel@tonic-gate #include <libproc.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static	int	stop(char *);
37*b06cdb87Svb static	int	lwpstop(int *, const lwpstatus_t *, const lwpsinfo_t *);
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static	char	*command;
40*b06cdb87Svb static	const char *lwps;
41*b06cdb87Svb static	struct	ps_prochandle *P;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)447c478bd9Sstevel@tonic-gate main(int argc, char **argv)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	int rc = 0;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
497c478bd9Sstevel@tonic-gate 		command++;
507c478bd9Sstevel@tonic-gate 	else
517c478bd9Sstevel@tonic-gate 		command = argv[0];
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if (argc <= 1) {
54*b06cdb87Svb 		(void) fprintf(stderr, "usage:\t%s pid[/lwps] ...\n", command);
557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
56*b06cdb87Svb 		    "  (stop processes or lwps with /proc request)\n");
577c478bd9Sstevel@tonic-gate 		return (2);
587c478bd9Sstevel@tonic-gate 	}
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	while (--argc > 0)
617c478bd9Sstevel@tonic-gate 		rc += stop(*++argv);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	return (rc);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static int
stop(char * arg)677c478bd9Sstevel@tonic-gate stop(char *arg)
687c478bd9Sstevel@tonic-gate {
69*b06cdb87Svb 	int gcode;
70*b06cdb87Svb 	int rc = 0;
717c478bd9Sstevel@tonic-gate 
72*b06cdb87Svb 	if ((P = proc_arg_xgrab(arg, NULL, PR_ARG_PIDS, PGRAB_RETAIN |
73*b06cdb87Svb 	    PGRAB_NOSTOP | PGRAB_FORCE, &gcode, &lwps)) == NULL) {
747c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot control %s: %s\n",
75*b06cdb87Svb 		    command, arg, Pgrab_error(gcode));
767c478bd9Sstevel@tonic-gate 		return (1);
77*b06cdb87Svb 	} else if (lwps != NULL) {
78*b06cdb87Svb 		/*
79*b06cdb87Svb 		 * The user has provided an lwp specification.  Let's consider
80*b06cdb87Svb 		 * the lwp specification as a mask.  We iterate over all lwps in
81*b06cdb87Svb 		 * the process and stop every lwp, which matches the mask.  If
82*b06cdb87Svb 		 * there is no lwp matching the mask or an error occured during
83*b06cdb87Svb 		 * the iteration, set the return code to 1 as indication of an
84*b06cdb87Svb 		 * error.
85*b06cdb87Svb 		 */
86*b06cdb87Svb 		int lwpcount = 0;
877c478bd9Sstevel@tonic-gate 
88*b06cdb87Svb 		(void) Plwp_iter_all(P, (proc_lwp_all_f *)lwpstop, &lwpcount);
89*b06cdb87Svb 		if (lwpcount == 0) {
90*b06cdb87Svb 			(void) fprintf(stderr, "%s: cannot control %s:"
91*b06cdb87Svb 			    " no matching LWPs found\n", command, arg);
92*b06cdb87Svb 			rc = 1;
93*b06cdb87Svb 		} else if (lwpcount == -1)
94*b06cdb87Svb 			rc = 1;
95*b06cdb87Svb 	} else {
96*b06cdb87Svb 		(void) Pdstop(P);	/* Stop the process. */
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
99*b06cdb87Svb 	/*
100*b06cdb87Svb 	 * Prelease could change the tracing flags, use Pfree and unset
101*b06cdb87Svb 	 * run-on-last-close flag to prevent the process being set running
102*b06cdb87Svb 	 * after detaching from it.
103*b06cdb87Svb 	 */
104*b06cdb87Svb 	(void) Punsetflags(P, PR_RLC);
105*b06cdb87Svb 	Pfree(P);
106*b06cdb87Svb 	return (rc);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
109*b06cdb87Svb /* ARGSUSED */
1107c478bd9Sstevel@tonic-gate static int
lwpstop(int * lwpcount,const lwpstatus_t * status,const lwpsinfo_t * info)111*b06cdb87Svb lwpstop(int *lwpcount, const lwpstatus_t *status, const lwpsinfo_t *info)
1127c478bd9Sstevel@tonic-gate {
113*b06cdb87Svb 	struct ps_lwphandle *L;
114*b06cdb87Svb 	int gcode;
115*b06cdb87Svb 
116*b06cdb87Svb 	if (proc_lwp_in_set(lwps, info->pr_lwpid)) {
117*b06cdb87Svb 		/*
118*b06cdb87Svb 		 * There is a race between the callback from the iterator and
119*b06cdb87Svb 		 * grabbing of the lwp.  If the lwp has already exited, Lgrab
120*b06cdb87Svb 		 * will return the error code G_NOPROC.  It's not a real error,
121*b06cdb87Svb 		 * only if there is no lwp matching the specification.
122*b06cdb87Svb 		 */
123*b06cdb87Svb 		if ((L = Lgrab(P, info->pr_lwpid, &gcode)) != NULL) {
124*b06cdb87Svb 			(void) Ldstop(L);
125*b06cdb87Svb 			Lfree(L);
126*b06cdb87Svb 			if (*lwpcount >= 0)
127*b06cdb87Svb 				(*lwpcount)++;
128*b06cdb87Svb 		} else if (gcode != G_NOPROC) {
129*b06cdb87Svb 			(void) fprintf(stderr, "%s: cannot control %d/%d: %s\n",
130*b06cdb87Svb 			    command, (int)Pstatus(P)->pr_pid,
131*b06cdb87Svb 			    (int)info->pr_lwpid, Lgrab_error(gcode));
132*b06cdb87Svb 			*lwpcount = -1;
133*b06cdb87Svb 		}
134*b06cdb87Svb 	}
135*b06cdb87Svb 	return (0);
1367c478bd9Sstevel@tonic-gate }
137