10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*2528Svb160487 * Common Development and Distribution License (the "License"). 6*2528Svb160487 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*2528Svb160487 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <stdio.h> 290Sstevel@tonic-gate #include <stdlib.h> 300Sstevel@tonic-gate #include <unistd.h> 310Sstevel@tonic-gate #include <fcntl.h> 320Sstevel@tonic-gate #include <string.h> 330Sstevel@tonic-gate #include <errno.h> 340Sstevel@tonic-gate #include <sys/types.h> 350Sstevel@tonic-gate #include <signal.h> 360Sstevel@tonic-gate #include <libproc.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate static int start(char *); 39*2528Svb160487 static int lwpstart(int *, const lwpstatus_t *, const lwpsinfo_t *); 400Sstevel@tonic-gate 410Sstevel@tonic-gate static char *command; 42*2528Svb160487 static const char *lwps; 43*2528Svb160487 static struct ps_prochandle *P; 440Sstevel@tonic-gate 450Sstevel@tonic-gate int 460Sstevel@tonic-gate main(int argc, char **argv) 470Sstevel@tonic-gate { 48*2528Svb160487 int rc = 0; 490Sstevel@tonic-gate 500Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL) 510Sstevel@tonic-gate command++; 520Sstevel@tonic-gate else 530Sstevel@tonic-gate command = argv[0]; 540Sstevel@tonic-gate 550Sstevel@tonic-gate if (argc <= 1) { 56*2528Svb160487 (void) fprintf(stderr, "usage:\t%s pid[/lwps] ...\n", command); 57*2528Svb160487 (void) fprintf(stderr, " (set stopped processes or lwps " 58*2528Svb160487 "running)\n"); 590Sstevel@tonic-gate return (2); 600Sstevel@tonic-gate } 610Sstevel@tonic-gate 620Sstevel@tonic-gate while (--argc > 0) 630Sstevel@tonic-gate rc += start(*++argv); 640Sstevel@tonic-gate 650Sstevel@tonic-gate return (rc); 660Sstevel@tonic-gate } 670Sstevel@tonic-gate 680Sstevel@tonic-gate static int 690Sstevel@tonic-gate start(char *arg) 700Sstevel@tonic-gate { 710Sstevel@tonic-gate int gcode; 72*2528Svb160487 int rc = 0; 730Sstevel@tonic-gate 74*2528Svb160487 if ((P = proc_arg_xgrab(arg, NULL, PR_ARG_PIDS, PGRAB_FORCE | 75*2528Svb160487 PGRAB_RETAIN | PGRAB_NOSTOP, &gcode, &lwps)) == NULL) { 760Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot control %s: %s\n", 77*2528Svb160487 command, arg, Pgrab_error(gcode)); 780Sstevel@tonic-gate return (1); 790Sstevel@tonic-gate } 800Sstevel@tonic-gate 810Sstevel@tonic-gate /* 820Sstevel@tonic-gate * If the victim is stopped because of a job control signal, we 830Sstevel@tonic-gate * need to send it SIGCONT to get it moving again, otherwise 840Sstevel@tonic-gate * the agent will not be able to run, and so will not be able to 850Sstevel@tonic-gate * exit the process. 860Sstevel@tonic-gate */ 870Sstevel@tonic-gate (void) kill(Pstatus(P)->pr_pid, SIGCONT); 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * if the agent already exists, Pcreate_agent will adopt the 910Sstevel@tonic-gate * extant agent so that we can destroy it 920Sstevel@tonic-gate */ 930Sstevel@tonic-gate if (Pstatus(P)->pr_lwp.pr_flags & PR_AGENT) { 940Sstevel@tonic-gate if (Pcreate_agent(P) != 0) { 950Sstevel@tonic-gate (void) fprintf(stderr, 960Sstevel@tonic-gate "%s: cannot remove agent from %s: %s\n", 970Sstevel@tonic-gate command, arg, strerror(errno)); 980Sstevel@tonic-gate 990Sstevel@tonic-gate Prelease(P, 0); 1000Sstevel@tonic-gate return (1); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate Pdestroy_agent(P); 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate 106*2528Svb160487 if (lwps != NULL) { 107*2528Svb160487 /* 108*2528Svb160487 * The user provided an lwp specification. Let's consider the 109*2528Svb160487 * lwp specification as a mask. We iterate over all lwps in the 110*2528Svb160487 * process and set running every lwp, which matches the mask. 111*2528Svb160487 * If there is no lwp matching the mask or an error occured 112*2528Svb160487 * during the iteration, set the return code to 1 as indication 113*2528Svb160487 * of an error. We need to unset run-on-last-close flag, 114*2528Svb160487 * otherwise *all* lwps could be set running after detaching 115*2528Svb160487 * from the process and not only lwps, which were selected. 116*2528Svb160487 */ 117*2528Svb160487 int lwpcount = 0; 118*2528Svb160487 119*2528Svb160487 (void) Punsetflags(P, PR_RLC); 120*2528Svb160487 (void) Plwp_iter_all(P, (proc_lwp_all_f *)lwpstart, &lwpcount); 121*2528Svb160487 122*2528Svb160487 if (lwpcount == 0) { 123*2528Svb160487 (void) fprintf(stderr, "%s: cannot control %s:" 124*2528Svb160487 " no matching LWPs found\n", command, arg); 125*2528Svb160487 rc = 1; 126*2528Svb160487 } else if (lwpcount == -1) 127*2528Svb160487 rc = 1; 128*2528Svb160487 } else { 129*2528Svb160487 (void) Psetrun(P, 0, 0); /* Set the process running. */ 130*2528Svb160487 } 131*2528Svb160487 1320Sstevel@tonic-gate /* 1330Sstevel@tonic-gate * Prelease could change the tracing flags or leave the victim hung 134*2528Svb160487 * so we free the handle by hand. 1350Sstevel@tonic-gate */ 1360Sstevel@tonic-gate Pfree(P); 137*2528Svb160487 return (rc); 138*2528Svb160487 } 1390Sstevel@tonic-gate 140*2528Svb160487 /* ARGSUSED */ 141*2528Svb160487 static int 142*2528Svb160487 lwpstart(int *lwpcount, const lwpstatus_t *status, const lwpsinfo_t *info) 143*2528Svb160487 { 144*2528Svb160487 struct ps_lwphandle *L; 145*2528Svb160487 int gcode; 146*2528Svb160487 147*2528Svb160487 if (proc_lwp_in_set(lwps, info->pr_lwpid)) { 148*2528Svb160487 /* 149*2528Svb160487 * There is a race between the callback from the iterator and 150*2528Svb160487 * grabbing of the lwp. If the lwp has already exited, Lgrab 151*2528Svb160487 * will return the error code G_NOPROC. It's not a real error, 152*2528Svb160487 * only if there is no lwp matching the specification. 153*2528Svb160487 */ 154*2528Svb160487 if ((L = Lgrab(P, info->pr_lwpid, &gcode)) != NULL) { 155*2528Svb160487 (void) Lsetrun(L, 0, 0); 156*2528Svb160487 Lfree(L); 157*2528Svb160487 if (*lwpcount >= 0) 158*2528Svb160487 (*lwpcount)++; 159*2528Svb160487 } else if (gcode != G_NOPROC) { 160*2528Svb160487 (void) fprintf(stderr, "%s: cannot control %d/%d: %s\n", 161*2528Svb160487 command, (int)Pstatus(P)->pr_pid, 162*2528Svb160487 (int)info->pr_lwpid, Lgrab_error(gcode)); 163*2528Svb160487 *lwpcount = -1; 164*2528Svb160487 } 165*2528Svb160487 } 1660Sstevel@tonic-gate return (0); 1670Sstevel@tonic-gate } 168