xref: /illumos-gate/usr/src/cmd/ptools/prun/prun.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.
237c478bd9Sstevel@tonic-gate  * 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	start(char *);
37*b06cdb87Svb static	int	lwpstart(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 {
46*b06cdb87Svb 	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);
55*b06cdb87Svb 		(void) fprintf(stderr, "  (set stopped processes or lwps "
56*b06cdb87Svb 		    "running)\n");
577c478bd9Sstevel@tonic-gate 		return (2);
587c478bd9Sstevel@tonic-gate 	}
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	while (--argc > 0)
617c478bd9Sstevel@tonic-gate 		rc += start(*++argv);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	return (rc);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static int
start(char * arg)677c478bd9Sstevel@tonic-gate start(char *arg)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	int gcode;
70*b06cdb87Svb 	int rc = 0;
717c478bd9Sstevel@tonic-gate 
72*b06cdb87Svb 	if ((P = proc_arg_xgrab(arg, NULL, PR_ARG_PIDS, PGRAB_FORCE |
73*b06cdb87Svb 	    PGRAB_RETAIN | PGRAB_NOSTOP, &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);
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * If the victim is stopped because of a job control signal, we
817c478bd9Sstevel@tonic-gate 	 * need to send it SIGCONT to get it moving again, otherwise
827c478bd9Sstevel@tonic-gate 	 * the agent will not be able to run, and so will not be able to
837c478bd9Sstevel@tonic-gate 	 * exit the process.
847c478bd9Sstevel@tonic-gate 	 */
857c478bd9Sstevel@tonic-gate 	(void) kill(Pstatus(P)->pr_pid, SIGCONT);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	/*
887c478bd9Sstevel@tonic-gate 	 * if the agent already exists, Pcreate_agent will adopt the
897c478bd9Sstevel@tonic-gate 	 * extant agent so that we can destroy it
907c478bd9Sstevel@tonic-gate 	 */
917c478bd9Sstevel@tonic-gate 	if (Pstatus(P)->pr_lwp.pr_flags & PR_AGENT) {
927c478bd9Sstevel@tonic-gate 		if (Pcreate_agent(P) != 0) {
937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
947c478bd9Sstevel@tonic-gate 			    "%s: cannot remove agent from %s: %s\n",
957c478bd9Sstevel@tonic-gate 			    command, arg, strerror(errno));
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 			Prelease(P, 0);
987c478bd9Sstevel@tonic-gate 			return (1);
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 		Pdestroy_agent(P);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
104*b06cdb87Svb 	if (lwps != NULL) {
105*b06cdb87Svb 		/*
106*b06cdb87Svb 		 * The user provided an lwp specification. Let's consider the
107*b06cdb87Svb 		 * lwp specification as a mask.  We iterate over all lwps in the
108*b06cdb87Svb 		 * process and set running every lwp, which matches the mask.
109*b06cdb87Svb 		 * If there is no lwp matching the mask or an error occured
110*b06cdb87Svb 		 * during the iteration, set the return code to 1 as indication
111*b06cdb87Svb 		 * of an error.  We need to unset run-on-last-close flag,
112*b06cdb87Svb 		 * otherwise *all* lwps could be set running after detaching
113*b06cdb87Svb 		 * from the process and not only lwps, which were selected.
114*b06cdb87Svb 		 */
115*b06cdb87Svb 		int lwpcount = 0;
116*b06cdb87Svb 
117*b06cdb87Svb 		(void) Punsetflags(P, PR_RLC);
118*b06cdb87Svb 		(void) Plwp_iter_all(P, (proc_lwp_all_f *)lwpstart, &lwpcount);
119*b06cdb87Svb 
120*b06cdb87Svb 		if (lwpcount == 0) {
121*b06cdb87Svb 			(void) fprintf(stderr, "%s: cannot control %s:"
122*b06cdb87Svb 			    " no matching LWPs found\n", command, arg);
123*b06cdb87Svb 			rc = 1;
124*b06cdb87Svb 		} else if (lwpcount == -1)
125*b06cdb87Svb 			rc = 1;
126*b06cdb87Svb 	} else {
127*b06cdb87Svb 		(void) Psetrun(P, 0, 0);	/* Set the process running. */
128*b06cdb87Svb 	}
129*b06cdb87Svb 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * Prelease could change the tracing flags or leave the victim hung
132*b06cdb87Svb 	 * so we free the handle by hand.
1337c478bd9Sstevel@tonic-gate 	 */
1347c478bd9Sstevel@tonic-gate 	Pfree(P);
135*b06cdb87Svb 	return (rc);
136*b06cdb87Svb }
1377c478bd9Sstevel@tonic-gate 
138*b06cdb87Svb /* ARGSUSED */
139*b06cdb87Svb static int
lwpstart(int * lwpcount,const lwpstatus_t * status,const lwpsinfo_t * info)140*b06cdb87Svb lwpstart(int *lwpcount, const lwpstatus_t *status, const lwpsinfo_t *info)
141*b06cdb87Svb {
142*b06cdb87Svb 	struct ps_lwphandle *L;
143*b06cdb87Svb 	int gcode;
144*b06cdb87Svb 
145*b06cdb87Svb 	if (proc_lwp_in_set(lwps, info->pr_lwpid)) {
146*b06cdb87Svb 		/*
147*b06cdb87Svb 		 * There is a race between the callback from the iterator and
148*b06cdb87Svb 		 * grabbing of the lwp.  If the lwp has already exited, Lgrab
149*b06cdb87Svb 		 * will return the error code G_NOPROC.  It's not a real error,
150*b06cdb87Svb 		 * only if there is no lwp matching the specification.
151*b06cdb87Svb 		 */
152*b06cdb87Svb 		if ((L = Lgrab(P, info->pr_lwpid, &gcode)) != NULL) {
153*b06cdb87Svb 			(void) Lsetrun(L, 0, 0);
154*b06cdb87Svb 			Lfree(L);
155*b06cdb87Svb 			if (*lwpcount >= 0)
156*b06cdb87Svb 				(*lwpcount)++;
157*b06cdb87Svb 		} else if (gcode != G_NOPROC) {
158*b06cdb87Svb 			(void) fprintf(stderr, "%s: cannot control %d/%d: %s\n",
159*b06cdb87Svb 			    command, (int)Pstatus(P)->pr_pid,
160*b06cdb87Svb 			    (int)info->pr_lwpid, Lgrab_error(gcode));
161*b06cdb87Svb 			*lwpcount = -1;
162*b06cdb87Svb 		}
163*b06cdb87Svb 	}
1647c478bd9Sstevel@tonic-gate 	return (0);
1657c478bd9Sstevel@tonic-gate }
166