8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / ptools / prun / prun.c
blob3efa434c8a500fad1a27b40c174c4cbcf4eaef77
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <signal.h>
36 #include <libproc.h>
38 static int start(char *);
39 static int lwpstart(int *, const lwpstatus_t *, const lwpsinfo_t *);
41 static char *command;
42 static const char *lwps;
43 static struct ps_prochandle *P;
45 int
46 main(int argc, char **argv)
48 int rc = 0;
50 if ((command = strrchr(argv[0], '/')) != NULL)
51 command++;
52 else
53 command = argv[0];
55 if (argc <= 1) {
56 (void) fprintf(stderr, "usage:\t%s pid[/lwps] ...\n", command);
57 (void) fprintf(stderr, " (set stopped processes or lwps "
58 "running)\n");
59 return (2);
62 while (--argc > 0)
63 rc += start(*++argv);
65 return (rc);
68 static int
69 start(char *arg)
71 int gcode;
72 int rc = 0;
74 if ((P = proc_arg_xgrab(arg, NULL, PR_ARG_PIDS, PGRAB_FORCE |
75 PGRAB_RETAIN | PGRAB_NOSTOP, &gcode, &lwps)) == NULL) {
76 (void) fprintf(stderr, "%s: cannot control %s: %s\n",
77 command, arg, Pgrab_error(gcode));
78 return (1);
82 * If the victim is stopped because of a job control signal, we
83 * need to send it SIGCONT to get it moving again, otherwise
84 * the agent will not be able to run, and so will not be able to
85 * exit the process.
87 (void) kill(Pstatus(P)->pr_pid, SIGCONT);
90 * if the agent already exists, Pcreate_agent will adopt the
91 * extant agent so that we can destroy it
93 if (Pstatus(P)->pr_lwp.pr_flags & PR_AGENT) {
94 if (Pcreate_agent(P) != 0) {
95 (void) fprintf(stderr,
96 "%s: cannot remove agent from %s: %s\n",
97 command, arg, strerror(errno));
99 Prelease(P, 0);
100 return (1);
103 Pdestroy_agent(P);
106 if (lwps != NULL) {
108 * The user provided an lwp specification. Let's consider the
109 * lwp specification as a mask. We iterate over all lwps in the
110 * process and set running every lwp, which matches the mask.
111 * If there is no lwp matching the mask or an error occured
112 * during the iteration, set the return code to 1 as indication
113 * of an error. We need to unset run-on-last-close flag,
114 * otherwise *all* lwps could be set running after detaching
115 * from the process and not only lwps, which were selected.
117 int lwpcount = 0;
119 (void) Punsetflags(P, PR_RLC);
120 (void) Plwp_iter_all(P, (proc_lwp_all_f *)lwpstart, &lwpcount);
122 if (lwpcount == 0) {
123 (void) fprintf(stderr, "%s: cannot control %s:"
124 " no matching LWPs found\n", command, arg);
125 rc = 1;
126 } else if (lwpcount == -1)
127 rc = 1;
128 } else {
129 (void) Psetrun(P, 0, 0); /* Set the process running. */
133 * Prelease could change the tracing flags or leave the victim hung
134 * so we free the handle by hand.
136 Pfree(P);
137 return (rc);
140 /* ARGSUSED */
141 static int
142 lwpstart(int *lwpcount, const lwpstatus_t *status, const lwpsinfo_t *info)
144 struct ps_lwphandle *L;
145 int gcode;
147 if (proc_lwp_in_set(lwps, info->pr_lwpid)) {
149 * There is a race between the callback from the iterator and
150 * grabbing of the lwp. If the lwp has already exited, Lgrab
151 * will return the error code G_NOPROC. It's not a real error,
152 * only if there is no lwp matching the specification.
154 if ((L = Lgrab(P, info->pr_lwpid, &gcode)) != NULL) {
155 (void) Lsetrun(L, 0, 0);
156 Lfree(L);
157 if (*lwpcount >= 0)
158 (*lwpcount)++;
159 } else if (gcode != G_NOPROC) {
160 (void) fprintf(stderr, "%s: cannot control %d/%d: %s\n",
161 command, (int)Pstatus(P)->pr_pid,
162 (int)info->pr_lwpid, Lgrab_error(gcode));
163 *lwpcount = -1;
166 return (0);