summaryrefslogtreecommitdiff
path: root/usr/src/cmd/ptools/prun/prun.c
blob: 3efa434c8a500fad1a27b40c174c4cbcf4eaef77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <libproc.h>

static	int	start(char *);
static	int	lwpstart(int *, const lwpstatus_t *, const lwpsinfo_t *);

static	char	*command;
static	const char *lwps;
static	struct	ps_prochandle *P;

int
main(int argc, char **argv)
{
	int	rc = 0;

	if ((command = strrchr(argv[0], '/')) != NULL)
		command++;
	else
		command = argv[0];

	if (argc <= 1) {
		(void) fprintf(stderr, "usage:\t%s pid[/lwps] ...\n", command);
		(void) fprintf(stderr, "  (set stopped processes or lwps "
		    "running)\n");
		return (2);
	}

	while (--argc > 0)
		rc += start(*++argv);

	return (rc);
}

static int
start(char *arg)
{
	int gcode;
	int rc = 0;

	if ((P = proc_arg_xgrab(arg, NULL, PR_ARG_PIDS, PGRAB_FORCE |
	    PGRAB_RETAIN | PGRAB_NOSTOP, &gcode, &lwps)) == NULL) {
		(void) fprintf(stderr, "%s: cannot control %s: %s\n",
		    command, arg, Pgrab_error(gcode));
		return (1);
	}

	/*
	 * If the victim is stopped because of a job control signal, we
	 * need to send it SIGCONT to get it moving again, otherwise
	 * the agent will not be able to run, and so will not be able to
	 * exit the process.
	 */
	(void) kill(Pstatus(P)->pr_pid, SIGCONT);

	/*
	 * if the agent already exists, Pcreate_agent will adopt the
	 * extant agent so that we can destroy it
	 */
	if (Pstatus(P)->pr_lwp.pr_flags & PR_AGENT) {
		if (Pcreate_agent(P) != 0) {
			(void) fprintf(stderr,
			    "%s: cannot remove agent from %s: %s\n",
			    command, arg, strerror(errno));

			Prelease(P, 0);
			return (1);
		}

		Pdestroy_agent(P);
	}

	if (lwps != NULL) {
		/*
		 * The user provided an lwp specification. Let's consider the
		 * lwp specification as a mask.  We iterate over all lwps in the
		 * process and set running every lwp, which matches the mask.
		 * If there is no lwp matching the mask or an error occured
		 * during the iteration, set the return code to 1 as indication
		 * of an error.  We need to unset run-on-last-close flag,
		 * otherwise *all* lwps could be set running after detaching
		 * from the process and not only lwps, which were selected.
		 */
		int lwpcount = 0;

		(void) Punsetflags(P, PR_RLC);
		(void) Plwp_iter_all(P, (proc_lwp_all_f *)lwpstart, &lwpcount);

		if (lwpcount == 0) {
			(void) fprintf(stderr, "%s: cannot control %s:"
			    " no matching LWPs found\n", command, arg);
			rc = 1;
		} else if (lwpcount == -1)
			rc = 1;
	} else {
		(void) Psetrun(P, 0, 0);	/* Set the process running. */
	}

	/*
	 * Prelease could change the tracing flags or leave the victim hung
	 * so we free the handle by hand.
	 */
	Pfree(P);
	return (rc);
}

/* ARGSUSED */
static int
lwpstart(int *lwpcount, const lwpstatus_t *status, const lwpsinfo_t *info)
{
	struct ps_lwphandle *L;
	int gcode;

	if (proc_lwp_in_set(lwps, info->pr_lwpid)) {
		/*
		 * There is a race between the callback from the iterator and
		 * grabbing of the lwp.  If the lwp has already exited, Lgrab
		 * will return the error code G_NOPROC.  It's not a real error,
		 * only if there is no lwp matching the specification.
		 */
		if ((L = Lgrab(P, info->pr_lwpid, &gcode)) != NULL) {
			(void) Lsetrun(L, 0, 0);
			Lfree(L);
			if (*lwpcount >= 0)
				(*lwpcount)++;
		} else if (gcode != G_NOPROC) {
			(void) fprintf(stderr, "%s: cannot control %d/%d: %s\n",
			    command, (int)Pstatus(P)->pr_pid,
			    (int)info->pr_lwpid, Lgrab_error(gcode));
			*lwpcount = -1;
		}
	}
	return (0);
}