xref: /titanic_51/usr/src/cmd/ptools/prun/prun.c (revision b06cdb87d254343cca2e66a21fd421617c3a0b7b)
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*b06cdb87Svb160487  * Common Development and Distribution License (the "License").
6*b06cdb87Svb160487  * 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*b06cdb87Svb160487  * 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <fcntl.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <signal.h>
367c478bd9Sstevel@tonic-gate #include <libproc.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static	int	start(char *);
39*b06cdb87Svb160487 static	int	lwpstart(int *, const lwpstatus_t *, const lwpsinfo_t *);
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static	char	*command;
42*b06cdb87Svb160487 static	const char *lwps;
43*b06cdb87Svb160487 static	struct	ps_prochandle *P;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate int
467c478bd9Sstevel@tonic-gate main(int argc, char **argv)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	int	rc = 0;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	if ((command = strrchr(argv[0], '/')) != NULL)
517c478bd9Sstevel@tonic-gate 		command++;
527c478bd9Sstevel@tonic-gate 	else
537c478bd9Sstevel@tonic-gate 		command = argv[0];
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	if (argc <= 1) {
56*b06cdb87Svb160487 		(void) fprintf(stderr, "usage:\t%s pid[/lwps] ...\n", command);
57*b06cdb87Svb160487 		(void) fprintf(stderr, "  (set stopped processes or lwps "
58*b06cdb87Svb160487 		    "running)\n");
597c478bd9Sstevel@tonic-gate 		return (2);
607c478bd9Sstevel@tonic-gate 	}
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	while (--argc > 0)
637c478bd9Sstevel@tonic-gate 		rc += start(*++argv);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	return (rc);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static int
697c478bd9Sstevel@tonic-gate start(char *arg)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	int gcode;
72*b06cdb87Svb160487 	int rc = 0;
737c478bd9Sstevel@tonic-gate 
74*b06cdb87Svb160487 	if ((P = proc_arg_xgrab(arg, NULL, PR_ARG_PIDS, PGRAB_FORCE |
75*b06cdb87Svb160487 	    PGRAB_RETAIN | PGRAB_NOSTOP, &gcode, &lwps)) == NULL) {
767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot control %s: %s\n",
777c478bd9Sstevel@tonic-gate 		    command, arg, Pgrab_error(gcode));
787c478bd9Sstevel@tonic-gate 		return (1);
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	/*
827c478bd9Sstevel@tonic-gate 	 * If the victim is stopped because of a job control signal, we
837c478bd9Sstevel@tonic-gate 	 * need to send it SIGCONT to get it moving again, otherwise
847c478bd9Sstevel@tonic-gate 	 * the agent will not be able to run, and so will not be able to
857c478bd9Sstevel@tonic-gate 	 * exit the process.
867c478bd9Sstevel@tonic-gate 	 */
877c478bd9Sstevel@tonic-gate 	(void) kill(Pstatus(P)->pr_pid, SIGCONT);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	/*
907c478bd9Sstevel@tonic-gate 	 * if the agent already exists, Pcreate_agent will adopt the
917c478bd9Sstevel@tonic-gate 	 * extant agent so that we can destroy it
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	if (Pstatus(P)->pr_lwp.pr_flags & PR_AGENT) {
947c478bd9Sstevel@tonic-gate 		if (Pcreate_agent(P) != 0) {
957c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
967c478bd9Sstevel@tonic-gate 			    "%s: cannot remove agent from %s: %s\n",
977c478bd9Sstevel@tonic-gate 			    command, arg, strerror(errno));
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 			Prelease(P, 0);
1007c478bd9Sstevel@tonic-gate 			return (1);
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		Pdestroy_agent(P);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
106*b06cdb87Svb160487 	if (lwps != NULL) {
107*b06cdb87Svb160487 		/*
108*b06cdb87Svb160487 		 * The user provided an lwp specification. Let's consider the
109*b06cdb87Svb160487 		 * lwp specification as a mask.  We iterate over all lwps in the
110*b06cdb87Svb160487 		 * process and set running every lwp, which matches the mask.
111*b06cdb87Svb160487 		 * If there is no lwp matching the mask or an error occured
112*b06cdb87Svb160487 		 * during the iteration, set the return code to 1 as indication
113*b06cdb87Svb160487 		 * of an error.  We need to unset run-on-last-close flag,
114*b06cdb87Svb160487 		 * otherwise *all* lwps could be set running after detaching
115*b06cdb87Svb160487 		 * from the process and not only lwps, which were selected.
116*b06cdb87Svb160487 		 */
117*b06cdb87Svb160487 		int lwpcount = 0;
118*b06cdb87Svb160487 
119*b06cdb87Svb160487 		(void) Punsetflags(P, PR_RLC);
120*b06cdb87Svb160487 		(void) Plwp_iter_all(P, (proc_lwp_all_f *)lwpstart, &lwpcount);
121*b06cdb87Svb160487 
122*b06cdb87Svb160487 		if (lwpcount == 0) {
123*b06cdb87Svb160487 			(void) fprintf(stderr, "%s: cannot control %s:"
124*b06cdb87Svb160487 			    " no matching LWPs found\n", command, arg);
125*b06cdb87Svb160487 			rc = 1;
126*b06cdb87Svb160487 		} else if (lwpcount == -1)
127*b06cdb87Svb160487 			rc = 1;
128*b06cdb87Svb160487 	} else {
129*b06cdb87Svb160487 		(void) Psetrun(P, 0, 0);	/* Set the process running. */
130*b06cdb87Svb160487 	}
131*b06cdb87Svb160487 
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * Prelease could change the tracing flags or leave the victim hung
134*b06cdb87Svb160487 	 * so we free the handle by hand.
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 	Pfree(P);
137*b06cdb87Svb160487 	return (rc);
138*b06cdb87Svb160487 }
1397c478bd9Sstevel@tonic-gate 
140*b06cdb87Svb160487 /* ARGSUSED */
141*b06cdb87Svb160487 static int
142*b06cdb87Svb160487 lwpstart(int *lwpcount, const lwpstatus_t *status, const lwpsinfo_t *info)
143*b06cdb87Svb160487 {
144*b06cdb87Svb160487 	struct ps_lwphandle *L;
145*b06cdb87Svb160487 	int gcode;
146*b06cdb87Svb160487 
147*b06cdb87Svb160487 	if (proc_lwp_in_set(lwps, info->pr_lwpid)) {
148*b06cdb87Svb160487 		/*
149*b06cdb87Svb160487 		 * There is a race between the callback from the iterator and
150*b06cdb87Svb160487 		 * grabbing of the lwp.  If the lwp has already exited, Lgrab
151*b06cdb87Svb160487 		 * will return the error code G_NOPROC.  It's not a real error,
152*b06cdb87Svb160487 		 * only if there is no lwp matching the specification.
153*b06cdb87Svb160487 		 */
154*b06cdb87Svb160487 		if ((L = Lgrab(P, info->pr_lwpid, &gcode)) != NULL) {
155*b06cdb87Svb160487 			(void) Lsetrun(L, 0, 0);
156*b06cdb87Svb160487 			Lfree(L);
157*b06cdb87Svb160487 			if (*lwpcount >= 0)
158*b06cdb87Svb160487 				(*lwpcount)++;
159*b06cdb87Svb160487 		} else if (gcode != G_NOPROC) {
160*b06cdb87Svb160487 			(void) fprintf(stderr, "%s: cannot control %d/%d: %s\n",
161*b06cdb87Svb160487 			    command, (int)Pstatus(P)->pr_pid,
162*b06cdb87Svb160487 			    (int)info->pr_lwpid, Lgrab_error(gcode));
163*b06cdb87Svb160487 			*lwpcount = -1;
164*b06cdb87Svb160487 		}
165*b06cdb87Svb160487 	}
1667c478bd9Sstevel@tonic-gate 	return (0);
1677c478bd9Sstevel@tonic-gate }
168