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.
23*b06cdb87Svb160487 * 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 stop(char *);
39*b06cdb87Svb160487 static int lwpstop(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
main(int argc,char ** argv)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);
577c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
58*b06cdb87Svb160487 " (stop processes or lwps with /proc request)\n");
597c478bd9Sstevel@tonic-gate return (2);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate while (--argc > 0)
637c478bd9Sstevel@tonic-gate rc += stop(*++argv);
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate return (rc);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate static int
stop(char * arg)697c478bd9Sstevel@tonic-gate stop(char *arg)
707c478bd9Sstevel@tonic-gate {
71*b06cdb87Svb160487 int gcode;
72*b06cdb87Svb160487 int rc = 0;
737c478bd9Sstevel@tonic-gate
74*b06cdb87Svb160487 if ((P = proc_arg_xgrab(arg, NULL, PR_ARG_PIDS, PGRAB_RETAIN |
75*b06cdb87Svb160487 PGRAB_NOSTOP | PGRAB_FORCE, &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);
79*b06cdb87Svb160487 } else if (lwps != NULL) {
80*b06cdb87Svb160487 /*
81*b06cdb87Svb160487 * The user has provided an lwp specification. Let's consider
82*b06cdb87Svb160487 * the lwp specification as a mask. We iterate over all lwps in
83*b06cdb87Svb160487 * the process and stop every lwp, which matches the mask. If
84*b06cdb87Svb160487 * there is no lwp matching the mask or an error occured during
85*b06cdb87Svb160487 * the iteration, set the return code to 1 as indication of an
86*b06cdb87Svb160487 * error.
87*b06cdb87Svb160487 */
88*b06cdb87Svb160487 int lwpcount = 0;
89*b06cdb87Svb160487
90*b06cdb87Svb160487 (void) Plwp_iter_all(P, (proc_lwp_all_f *)lwpstop, &lwpcount);
91*b06cdb87Svb160487 if (lwpcount == 0) {
92*b06cdb87Svb160487 (void) fprintf(stderr, "%s: cannot control %s:"
93*b06cdb87Svb160487 " no matching LWPs found\n", command, arg);
94*b06cdb87Svb160487 rc = 1;
95*b06cdb87Svb160487 } else if (lwpcount == -1)
96*b06cdb87Svb160487 rc = 1;
97*b06cdb87Svb160487 } else {
98*b06cdb87Svb160487 (void) Pdstop(P); /* Stop the process. */
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate
101*b06cdb87Svb160487 /*
102*b06cdb87Svb160487 * Prelease could change the tracing flags, use Pfree and unset
103*b06cdb87Svb160487 * run-on-last-close flag to prevent the process being set running
104*b06cdb87Svb160487 * after detaching from it.
105*b06cdb87Svb160487 */
106*b06cdb87Svb160487 (void) Punsetflags(P, PR_RLC);
107*b06cdb87Svb160487 Pfree(P);
108*b06cdb87Svb160487 return (rc);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
111*b06cdb87Svb160487 /* ARGSUSED */
1127c478bd9Sstevel@tonic-gate static int
lwpstop(int * lwpcount,const lwpstatus_t * status,const lwpsinfo_t * info)113*b06cdb87Svb160487 lwpstop(int *lwpcount, const lwpstatus_t *status, const lwpsinfo_t *info)
1147c478bd9Sstevel@tonic-gate {
115*b06cdb87Svb160487 struct ps_lwphandle *L;
116*b06cdb87Svb160487 int gcode;
117*b06cdb87Svb160487
118*b06cdb87Svb160487 if (proc_lwp_in_set(lwps, info->pr_lwpid)) {
119*b06cdb87Svb160487 /*
120*b06cdb87Svb160487 * There is a race between the callback from the iterator and
121*b06cdb87Svb160487 * grabbing of the lwp. If the lwp has already exited, Lgrab
122*b06cdb87Svb160487 * will return the error code G_NOPROC. It's not a real error,
123*b06cdb87Svb160487 * only if there is no lwp matching the specification.
124*b06cdb87Svb160487 */
125*b06cdb87Svb160487 if ((L = Lgrab(P, info->pr_lwpid, &gcode)) != NULL) {
126*b06cdb87Svb160487 (void) Ldstop(L);
127*b06cdb87Svb160487 Lfree(L);
128*b06cdb87Svb160487 if (*lwpcount >= 0)
129*b06cdb87Svb160487 (*lwpcount)++;
130*b06cdb87Svb160487 } else if (gcode != G_NOPROC) {
131*b06cdb87Svb160487 (void) fprintf(stderr, "%s: cannot control %d/%d: %s\n",
132*b06cdb87Svb160487 command, (int)Pstatus(P)->pr_pid,
133*b06cdb87Svb160487 (int)info->pr_lwpid, Lgrab_error(gcode));
134*b06cdb87Svb160487 *lwpcount = -1;
135*b06cdb87Svb160487 }
136*b06cdb87Svb160487 }
1377c478bd9Sstevel@tonic-gate return (0);
1387c478bd9Sstevel@tonic-gate }
139