1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate * pbind - bind a process to a processor (non-exclusively)
31*7c478bd9Sstevel@tonic-gate */
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/processor.h>
36*7c478bd9Sstevel@tonic-gate #include <stdio.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <procfs.h>
40*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
41*7c478bd9Sstevel@tonic-gate #include <errno.h>
42*7c478bd9Sstevel@tonic-gate #include <dirent.h>
43*7c478bd9Sstevel@tonic-gate #include <locale.h>
44*7c478bd9Sstevel@tonic-gate #include <libproc.h>
45*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */
48*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
49*7c478bd9Sstevel@tonic-gate #endif
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate #define ERR_OK 0 /* exit status for success */
52*7c478bd9Sstevel@tonic-gate #define ERR_FAIL 1 /* exit status for errors */
53*7c478bd9Sstevel@tonic-gate #define ERR_USAGE 2 /* exit status for usage errors */
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate static char *progname;
56*7c478bd9Sstevel@tonic-gate static char bflag;
57*7c478bd9Sstevel@tonic-gate static char qflag;
58*7c478bd9Sstevel@tonic-gate static char Qflag;
59*7c478bd9Sstevel@tonic-gate static char uflag;
60*7c478bd9Sstevel@tonic-gate static char Uflag;
61*7c478bd9Sstevel@tonic-gate static int errors;
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate #define MAX_PROCFS_PATH 80
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
66*7c478bd9Sstevel@tonic-gate static void
warn(char * format,...)67*7c478bd9Sstevel@tonic-gate warn(char *format, ...)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate int err = errno;
70*7c478bd9Sstevel@tonic-gate va_list alist;
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", progname);
73*7c478bd9Sstevel@tonic-gate va_start(alist, format);
74*7c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist);
75*7c478bd9Sstevel@tonic-gate va_end(alist);
76*7c478bd9Sstevel@tonic-gate if (strchr(format, '\n') == NULL)
77*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", strerror(err));
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
81*7c478bd9Sstevel@tonic-gate static void
die(char * format,...)82*7c478bd9Sstevel@tonic-gate die(char *format, ...)
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate int err = errno;
85*7c478bd9Sstevel@tonic-gate va_list alist;
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", progname);
88*7c478bd9Sstevel@tonic-gate va_start(alist, format);
89*7c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist);
90*7c478bd9Sstevel@tonic-gate va_end(alist);
91*7c478bd9Sstevel@tonic-gate if (strchr(format, '\n') == NULL)
92*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", strerror(err));
93*7c478bd9Sstevel@tonic-gate exit(ERR_FAIL);
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate /*
97*7c478bd9Sstevel@tonic-gate * Output for query.
98*7c478bd9Sstevel@tonic-gate */
99*7c478bd9Sstevel@tonic-gate static void
query_out(id_t pid,id_t lwpid,processorid_t cpu)100*7c478bd9Sstevel@tonic-gate query_out(id_t pid, id_t lwpid, processorid_t cpu)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate char *proclwp;
103*7c478bd9Sstevel@tonic-gate char pidstr[21];
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate if (lwpid == -1) {
106*7c478bd9Sstevel@tonic-gate (void) snprintf(pidstr, 20, "%d", (int)pid);
107*7c478bd9Sstevel@tonic-gate proclwp = "process";
108*7c478bd9Sstevel@tonic-gate } else {
109*7c478bd9Sstevel@tonic-gate (void) snprintf(pidstr, 20, "%d/%d", (int)pid, (int)lwpid);
110*7c478bd9Sstevel@tonic-gate proclwp = "lwp";
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate if (cpu == PBIND_NONE)
114*7c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: not bound\n"),
115*7c478bd9Sstevel@tonic-gate proclwp, pidstr);
116*7c478bd9Sstevel@tonic-gate else
117*7c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: %d\n"),
118*7c478bd9Sstevel@tonic-gate proclwp, pidstr, cpu);
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate * Binding error.
123*7c478bd9Sstevel@tonic-gate */
124*7c478bd9Sstevel@tonic-gate static void
bind_err(processorid_t cpu,id_t pid,id_t lwpid,int err)125*7c478bd9Sstevel@tonic-gate bind_err(processorid_t cpu, id_t pid, id_t lwpid, int err)
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate char *msg;
128*7c478bd9Sstevel@tonic-gate
129*7c478bd9Sstevel@tonic-gate switch (cpu) {
130*7c478bd9Sstevel@tonic-gate case PBIND_NONE:
131*7c478bd9Sstevel@tonic-gate msg = gettext("unbind");
132*7c478bd9Sstevel@tonic-gate break;
133*7c478bd9Sstevel@tonic-gate case PBIND_QUERY:
134*7c478bd9Sstevel@tonic-gate msg = gettext("query");
135*7c478bd9Sstevel@tonic-gate break;
136*7c478bd9Sstevel@tonic-gate default:
137*7c478bd9Sstevel@tonic-gate msg = gettext("bind");
138*7c478bd9Sstevel@tonic-gate break;
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate if (lwpid == -1)
141*7c478bd9Sstevel@tonic-gate warn(gettext("cannot %s pid %d: %s\n"), msg,
142*7c478bd9Sstevel@tonic-gate (int)pid, strerror(err));
143*7c478bd9Sstevel@tonic-gate else
144*7c478bd9Sstevel@tonic-gate warn(gettext("cannot %s lwpid %d/%d: %s\n"), msg,
145*7c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid, strerror(err));
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate /*
149*7c478bd9Sstevel@tonic-gate * Output for bind.
150*7c478bd9Sstevel@tonic-gate */
151*7c478bd9Sstevel@tonic-gate static void
bind_out(id_t pid,id_t lwpid,processorid_t old,processorid_t new)152*7c478bd9Sstevel@tonic-gate bind_out(id_t pid, id_t lwpid, processorid_t old, processorid_t new)
153*7c478bd9Sstevel@tonic-gate {
154*7c478bd9Sstevel@tonic-gate char *proclwp;
155*7c478bd9Sstevel@tonic-gate char pidstr[21];
156*7c478bd9Sstevel@tonic-gate
157*7c478bd9Sstevel@tonic-gate if (lwpid == -1) {
158*7c478bd9Sstevel@tonic-gate (void) snprintf(pidstr, 20, "%d", (int)pid);
159*7c478bd9Sstevel@tonic-gate proclwp = "process";
160*7c478bd9Sstevel@tonic-gate } else {
161*7c478bd9Sstevel@tonic-gate (void) snprintf(pidstr, 20, "%d/%d", (int)pid, (int)lwpid);
162*7c478bd9Sstevel@tonic-gate proclwp = "lwp";
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate if (old == PBIND_NONE) {
166*7c478bd9Sstevel@tonic-gate if (new == PBIND_NONE)
167*7c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: was not bound, "
168*7c478bd9Sstevel@tonic-gate "now not bound\n"), proclwp, pidstr);
169*7c478bd9Sstevel@tonic-gate else
170*7c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: was not bound, "
171*7c478bd9Sstevel@tonic-gate "now %d\n"), proclwp, pidstr, new);
172*7c478bd9Sstevel@tonic-gate } else {
173*7c478bd9Sstevel@tonic-gate if (new == PBIND_NONE)
174*7c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: was %d, "
175*7c478bd9Sstevel@tonic-gate "now not bound\n"), proclwp, pidstr, old);
176*7c478bd9Sstevel@tonic-gate else
177*7c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: was %d, "
178*7c478bd9Sstevel@tonic-gate "now %d\n"), proclwp, pidstr, old, new);
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate
182*7c478bd9Sstevel@tonic-gate static struct ps_prochandle *
grab_proc(id_t pid)183*7c478bd9Sstevel@tonic-gate grab_proc(id_t pid)
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate int ret;
186*7c478bd9Sstevel@tonic-gate struct ps_prochandle *Pr;
187*7c478bd9Sstevel@tonic-gate
188*7c478bd9Sstevel@tonic-gate if ((Pr = Pgrab(pid, 0, &ret)) == NULL) {
189*7c478bd9Sstevel@tonic-gate warn(gettext("cannot control process %d: %s\n"),
190*7c478bd9Sstevel@tonic-gate (int)pid, Pgrab_error(ret));
191*7c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
192*7c478bd9Sstevel@tonic-gate return (NULL);
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate
195*7c478bd9Sstevel@tonic-gate /*
196*7c478bd9Sstevel@tonic-gate * Set run-on-last-close flag so the controlled process
197*7c478bd9Sstevel@tonic-gate * runs even if we die on a signal, and create an agent LWP.
198*7c478bd9Sstevel@tonic-gate */
199*7c478bd9Sstevel@tonic-gate if (Psetflags(Pr, PR_RLC) != 0 || Pcreate_agent(Pr) != 0) {
200*7c478bd9Sstevel@tonic-gate warn(gettext("cannot control process %d\n"), (int)pid);
201*7c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
202*7c478bd9Sstevel@tonic-gate Prelease(Pr, 0);
203*7c478bd9Sstevel@tonic-gate return (NULL);
204*7c478bd9Sstevel@tonic-gate }
205*7c478bd9Sstevel@tonic-gate return (Pr);
206*7c478bd9Sstevel@tonic-gate }
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate static void
rele_proc(struct ps_prochandle * Pr)209*7c478bd9Sstevel@tonic-gate rele_proc(struct ps_prochandle *Pr)
210*7c478bd9Sstevel@tonic-gate {
211*7c478bd9Sstevel@tonic-gate if (Pr == NULL)
212*7c478bd9Sstevel@tonic-gate return;
213*7c478bd9Sstevel@tonic-gate Pdestroy_agent(Pr);
214*7c478bd9Sstevel@tonic-gate Prelease(Pr, 0);
215*7c478bd9Sstevel@tonic-gate }
216*7c478bd9Sstevel@tonic-gate
217*7c478bd9Sstevel@tonic-gate static void
bind_lwp(struct ps_prochandle * Pr,id_t pid,id_t lwpid,processorid_t cpu)218*7c478bd9Sstevel@tonic-gate bind_lwp(struct ps_prochandle *Pr, id_t pid, id_t lwpid, processorid_t cpu)
219*7c478bd9Sstevel@tonic-gate {
220*7c478bd9Sstevel@tonic-gate processorid_t old_cpu;
221*7c478bd9Sstevel@tonic-gate
222*7c478bd9Sstevel@tonic-gate if (pr_processor_bind(Pr, P_LWPID, lwpid, cpu, &old_cpu) < 0) {
223*7c478bd9Sstevel@tonic-gate bind_err(cpu, pid, lwpid, errno);
224*7c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
225*7c478bd9Sstevel@tonic-gate } else {
226*7c478bd9Sstevel@tonic-gate if (qflag)
227*7c478bd9Sstevel@tonic-gate query_out(pid, lwpid, old_cpu);
228*7c478bd9Sstevel@tonic-gate else
229*7c478bd9Sstevel@tonic-gate bind_out(pid, lwpid, old_cpu, cpu);
230*7c478bd9Sstevel@tonic-gate }
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate
233*7c478bd9Sstevel@tonic-gate /*
234*7c478bd9Sstevel@tonic-gate * Query, set, or clear bindings for the range of LWPs in the given process.
235*7c478bd9Sstevel@tonic-gate */
236*7c478bd9Sstevel@tonic-gate static int
do_lwps(id_t pid,const char * range,processorid_t cpu)237*7c478bd9Sstevel@tonic-gate do_lwps(id_t pid, const char *range, processorid_t cpu)
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate char procfile[MAX_PROCFS_PATH];
240*7c478bd9Sstevel@tonic-gate struct ps_prochandle *Pr;
241*7c478bd9Sstevel@tonic-gate struct prheader header;
242*7c478bd9Sstevel@tonic-gate processorid_t binding;
243*7c478bd9Sstevel@tonic-gate struct lwpsinfo *lwp;
244*7c478bd9Sstevel@tonic-gate char *lpsinfo, *ptr;
245*7c478bd9Sstevel@tonic-gate int nent, size;
246*7c478bd9Sstevel@tonic-gate int i, fd, found;
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate /*
249*7c478bd9Sstevel@tonic-gate * Report bindings for LWPs in process 'pid'.
250*7c478bd9Sstevel@tonic-gate */
251*7c478bd9Sstevel@tonic-gate (void) snprintf(procfile, MAX_PROCFS_PATH,
252*7c478bd9Sstevel@tonic-gate "/proc/%d/lpsinfo", (int)pid);
253*7c478bd9Sstevel@tonic-gate if ((fd = open(procfile, O_RDONLY)) < 0) {
254*7c478bd9Sstevel@tonic-gate if (errno == ENOENT)
255*7c478bd9Sstevel@tonic-gate errno = ESRCH;
256*7c478bd9Sstevel@tonic-gate bind_err(cpu, pid, -1, errno);
257*7c478bd9Sstevel@tonic-gate return (ERR_FAIL);
258*7c478bd9Sstevel@tonic-gate }
259*7c478bd9Sstevel@tonic-gate if (pread(fd, &header, sizeof (header), 0) != sizeof (header)) {
260*7c478bd9Sstevel@tonic-gate (void) close(fd);
261*7c478bd9Sstevel@tonic-gate bind_err(cpu, pid, -1, errno);
262*7c478bd9Sstevel@tonic-gate return (ERR_FAIL);
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate nent = header.pr_nent;
265*7c478bd9Sstevel@tonic-gate size = header.pr_entsize * nent;
266*7c478bd9Sstevel@tonic-gate ptr = lpsinfo = malloc(size);
267*7c478bd9Sstevel@tonic-gate if (lpsinfo == NULL) {
268*7c478bd9Sstevel@tonic-gate bind_err(cpu, pid, -1, errno);
269*7c478bd9Sstevel@tonic-gate return (ERR_FAIL);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate if (pread(fd, lpsinfo, size, sizeof (header)) != size) {
272*7c478bd9Sstevel@tonic-gate bind_err(cpu, pid, -1, errno);
273*7c478bd9Sstevel@tonic-gate free(lpsinfo);
274*7c478bd9Sstevel@tonic-gate (void) close(fd);
275*7c478bd9Sstevel@tonic-gate return (ERR_FAIL);
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate
278*7c478bd9Sstevel@tonic-gate if ((bflag || uflag) && (Pr = grab_proc(pid)) == NULL) {
279*7c478bd9Sstevel@tonic-gate free(lpsinfo);
280*7c478bd9Sstevel@tonic-gate (void) close(fd);
281*7c478bd9Sstevel@tonic-gate return (ERR_FAIL);
282*7c478bd9Sstevel@tonic-gate }
283*7c478bd9Sstevel@tonic-gate found = 0;
284*7c478bd9Sstevel@tonic-gate for (i = 0; i < nent; i++, ptr += header.pr_entsize) {
285*7c478bd9Sstevel@tonic-gate /*LINTED ALIGNMENT*/
286*7c478bd9Sstevel@tonic-gate lwp = (lwpsinfo_t *)ptr;
287*7c478bd9Sstevel@tonic-gate binding = lwp->pr_bindpro;
288*7c478bd9Sstevel@tonic-gate if (!proc_lwp_in_set(range, lwp->pr_lwpid))
289*7c478bd9Sstevel@tonic-gate continue;
290*7c478bd9Sstevel@tonic-gate found++;
291*7c478bd9Sstevel@tonic-gate if (bflag || uflag)
292*7c478bd9Sstevel@tonic-gate bind_lwp(Pr, pid, lwp->pr_lwpid, cpu);
293*7c478bd9Sstevel@tonic-gate else if (binding != PBIND_NONE)
294*7c478bd9Sstevel@tonic-gate query_out(pid, lwp->pr_lwpid, binding);
295*7c478bd9Sstevel@tonic-gate }
296*7c478bd9Sstevel@tonic-gate if (bflag || uflag)
297*7c478bd9Sstevel@tonic-gate rele_proc(Pr);
298*7c478bd9Sstevel@tonic-gate free(lpsinfo);
299*7c478bd9Sstevel@tonic-gate (void) close(fd);
300*7c478bd9Sstevel@tonic-gate if (found == 0) {
301*7c478bd9Sstevel@tonic-gate warn(gettext("cannot %s lwpid %d/%s: "
302*7c478bd9Sstevel@tonic-gate "No matching LWPs found\n"),
303*7c478bd9Sstevel@tonic-gate bflag ? "bind" : "query", pid, range);
304*7c478bd9Sstevel@tonic-gate return (ERR_FAIL);
305*7c478bd9Sstevel@tonic-gate }
306*7c478bd9Sstevel@tonic-gate return (ERR_OK);
307*7c478bd9Sstevel@tonic-gate }
308*7c478bd9Sstevel@tonic-gate
309*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
310*7c478bd9Sstevel@tonic-gate static int
query_all_proc(psinfo_t * psinfo,lwpsinfo_t * lwpsinfo,void * arg)311*7c478bd9Sstevel@tonic-gate query_all_proc(psinfo_t *psinfo, lwpsinfo_t *lwpsinfo, void *arg)
312*7c478bd9Sstevel@tonic-gate {
313*7c478bd9Sstevel@tonic-gate id_t pid = psinfo->pr_pid;
314*7c478bd9Sstevel@tonic-gate processorid_t binding;
315*7c478bd9Sstevel@tonic-gate
316*7c478bd9Sstevel@tonic-gate if (processor_bind(P_PID, pid, PBIND_QUERY, &binding) < 0) {
317*7c478bd9Sstevel@tonic-gate /*
318*7c478bd9Sstevel@tonic-gate * Ignore search errors. The process may have exited
319*7c478bd9Sstevel@tonic-gate * since we read the directory.
320*7c478bd9Sstevel@tonic-gate */
321*7c478bd9Sstevel@tonic-gate if (errno == ESRCH)
322*7c478bd9Sstevel@tonic-gate return (0);
323*7c478bd9Sstevel@tonic-gate bind_err(PBIND_QUERY, pid, -1, errno);
324*7c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
325*7c478bd9Sstevel@tonic-gate return (0);
326*7c478bd9Sstevel@tonic-gate }
327*7c478bd9Sstevel@tonic-gate if (binding != PBIND_NONE)
328*7c478bd9Sstevel@tonic-gate query_out(pid, -1, binding);
329*7c478bd9Sstevel@tonic-gate return (0);
330*7c478bd9Sstevel@tonic-gate }
331*7c478bd9Sstevel@tonic-gate
332*7c478bd9Sstevel@tonic-gate static int
query_all_lwp(psinfo_t * psinfo,lwpsinfo_t * lwpsinfo,void * arg)333*7c478bd9Sstevel@tonic-gate query_all_lwp(psinfo_t *psinfo, lwpsinfo_t *lwpsinfo, void *arg)
334*7c478bd9Sstevel@tonic-gate {
335*7c478bd9Sstevel@tonic-gate id_t pid = psinfo->pr_pid;
336*7c478bd9Sstevel@tonic-gate id_t lwpid = lwpsinfo->pr_lwpid;
337*7c478bd9Sstevel@tonic-gate processorid_t *cpuid = arg;
338*7c478bd9Sstevel@tonic-gate processorid_t binding = lwpsinfo->pr_bindpro;
339*7c478bd9Sstevel@tonic-gate
340*7c478bd9Sstevel@tonic-gate if (psinfo->pr_nlwp == 1)
341*7c478bd9Sstevel@tonic-gate lwpid = -1; /* report process bindings if only 1 lwp */
342*7c478bd9Sstevel@tonic-gate if ((cpuid != NULL && *cpuid == binding) ||
343*7c478bd9Sstevel@tonic-gate (cpuid == NULL && binding != PBIND_NONE))
344*7c478bd9Sstevel@tonic-gate query_out(pid, lwpid, binding);
345*7c478bd9Sstevel@tonic-gate return (0);
346*7c478bd9Sstevel@tonic-gate }
347*7c478bd9Sstevel@tonic-gate
348*7c478bd9Sstevel@tonic-gate static int
usage(void)349*7c478bd9Sstevel@tonic-gate usage(void)
350*7c478bd9Sstevel@tonic-gate {
351*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
352*7c478bd9Sstevel@tonic-gate gettext("usage: \n\t%1$s -b processor_id pid[/lwpids] ...\n"
353*7c478bd9Sstevel@tonic-gate "\t%1$s -U [processor_id] ...\n"
354*7c478bd9Sstevel@tonic-gate "\t%1$s -Q [processor_id] ...\n"
355*7c478bd9Sstevel@tonic-gate "\t%1$s -u pid[/lwpids] ...\n"
356*7c478bd9Sstevel@tonic-gate "\t%1$s [-q] [pid[/lwpids] ...]\n"),
357*7c478bd9Sstevel@tonic-gate progname);
358*7c478bd9Sstevel@tonic-gate return (ERR_USAGE);
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate
361*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])362*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
363*7c478bd9Sstevel@tonic-gate {
364*7c478bd9Sstevel@tonic-gate int c;
365*7c478bd9Sstevel@tonic-gate int ret;
366*7c478bd9Sstevel@tonic-gate id_t pid;
367*7c478bd9Sstevel@tonic-gate processorid_t cpu, old_cpu;
368*7c478bd9Sstevel@tonic-gate char *endstr;
369*7c478bd9Sstevel@tonic-gate
370*7c478bd9Sstevel@tonic-gate progname = argv[0]; /* put actual command name in messages */
371*7c478bd9Sstevel@tonic-gate
372*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); /* setup localization */
373*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
374*7c478bd9Sstevel@tonic-gate
375*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "b:qQuU")) != EOF) {
376*7c478bd9Sstevel@tonic-gate switch (c) {
377*7c478bd9Sstevel@tonic-gate
378*7c478bd9Sstevel@tonic-gate case 'b':
379*7c478bd9Sstevel@tonic-gate bflag = 1;
380*7c478bd9Sstevel@tonic-gate cpu = strtol(optarg, &endstr, 10);
381*7c478bd9Sstevel@tonic-gate if (endstr != NULL && *endstr != '\0' || cpu < 0)
382*7c478bd9Sstevel@tonic-gate die(gettext("invalid processor ID %s\n"),
383*7c478bd9Sstevel@tonic-gate optarg);
384*7c478bd9Sstevel@tonic-gate break;
385*7c478bd9Sstevel@tonic-gate
386*7c478bd9Sstevel@tonic-gate case 'q':
387*7c478bd9Sstevel@tonic-gate qflag = 1;
388*7c478bd9Sstevel@tonic-gate cpu = PBIND_QUERY;
389*7c478bd9Sstevel@tonic-gate break;
390*7c478bd9Sstevel@tonic-gate
391*7c478bd9Sstevel@tonic-gate case 'Q':
392*7c478bd9Sstevel@tonic-gate Qflag = 1;
393*7c478bd9Sstevel@tonic-gate cpu = PBIND_QUERY;
394*7c478bd9Sstevel@tonic-gate break;
395*7c478bd9Sstevel@tonic-gate
396*7c478bd9Sstevel@tonic-gate case 'u':
397*7c478bd9Sstevel@tonic-gate uflag = 1;
398*7c478bd9Sstevel@tonic-gate cpu = PBIND_NONE;
399*7c478bd9Sstevel@tonic-gate break;
400*7c478bd9Sstevel@tonic-gate
401*7c478bd9Sstevel@tonic-gate case 'U':
402*7c478bd9Sstevel@tonic-gate Uflag = 1;
403*7c478bd9Sstevel@tonic-gate break;
404*7c478bd9Sstevel@tonic-gate
405*7c478bd9Sstevel@tonic-gate default:
406*7c478bd9Sstevel@tonic-gate return (usage());
407*7c478bd9Sstevel@tonic-gate }
408*7c478bd9Sstevel@tonic-gate }
409*7c478bd9Sstevel@tonic-gate
410*7c478bd9Sstevel@tonic-gate
411*7c478bd9Sstevel@tonic-gate /*
412*7c478bd9Sstevel@tonic-gate * Make sure that at most one of the options b, q, Q, u, or U
413*7c478bd9Sstevel@tonic-gate * was specified.
414*7c478bd9Sstevel@tonic-gate */
415*7c478bd9Sstevel@tonic-gate c = bflag + qflag + Qflag + uflag + Uflag;
416*7c478bd9Sstevel@tonic-gate if (c < 1) { /* nothing specified */
417*7c478bd9Sstevel@tonic-gate qflag = 1; /* default to query */
418*7c478bd9Sstevel@tonic-gate cpu = PBIND_QUERY;
419*7c478bd9Sstevel@tonic-gate } else if (c > 1) {
420*7c478bd9Sstevel@tonic-gate warn(gettext("options -b, -q, -Q, -u and -U "
421*7c478bd9Sstevel@tonic-gate "are mutually exclusive\n"));
422*7c478bd9Sstevel@tonic-gate return (usage());
423*7c478bd9Sstevel@tonic-gate }
424*7c478bd9Sstevel@tonic-gate
425*7c478bd9Sstevel@tonic-gate errors = 0;
426*7c478bd9Sstevel@tonic-gate argc -= optind;
427*7c478bd9Sstevel@tonic-gate argv += optind;
428*7c478bd9Sstevel@tonic-gate
429*7c478bd9Sstevel@tonic-gate /*
430*7c478bd9Sstevel@tonic-gate * Handle query of all processes.
431*7c478bd9Sstevel@tonic-gate */
432*7c478bd9Sstevel@tonic-gate if (argc == 0) {
433*7c478bd9Sstevel@tonic-gate if (bflag || uflag) {
434*7c478bd9Sstevel@tonic-gate warn(gettext("must specify at least one pid\n"));
435*7c478bd9Sstevel@tonic-gate return (usage());
436*7c478bd9Sstevel@tonic-gate }
437*7c478bd9Sstevel@tonic-gate if (Uflag) {
438*7c478bd9Sstevel@tonic-gate if (processor_bind(P_ALL, 0, PBIND_NONE, &old_cpu) != 0)
439*7c478bd9Sstevel@tonic-gate die(gettext("failed to unbind some LWPs"));
440*7c478bd9Sstevel@tonic-gate }
441*7c478bd9Sstevel@tonic-gate if (Qflag) {
442*7c478bd9Sstevel@tonic-gate (void) proc_walk(query_all_lwp, NULL, PR_WALK_LWP);
443*7c478bd9Sstevel@tonic-gate return (errors);
444*7c478bd9Sstevel@tonic-gate } else {
445*7c478bd9Sstevel@tonic-gate (void) proc_walk(query_all_proc, NULL, PR_WALK_PROC);
446*7c478bd9Sstevel@tonic-gate return (errors);
447*7c478bd9Sstevel@tonic-gate }
448*7c478bd9Sstevel@tonic-gate }
449*7c478bd9Sstevel@tonic-gate
450*7c478bd9Sstevel@tonic-gate if (Qflag || Uflag) {
451*7c478bd9Sstevel@tonic-gate /*
452*7c478bd9Sstevel@tonic-gate * Go through listed processor IDs.
453*7c478bd9Sstevel@tonic-gate */
454*7c478bd9Sstevel@tonic-gate for (; argc > 0; argv++, argc--) {
455*7c478bd9Sstevel@tonic-gate errno = 0;
456*7c478bd9Sstevel@tonic-gate cpu = (id_t)strtol(*argv, &endstr, 10);
457*7c478bd9Sstevel@tonic-gate if (errno != 0 || (endstr != NULL && *endstr != '\0') ||
458*7c478bd9Sstevel@tonic-gate p_online(cpu, P_STATUS) == -1) {
459*7c478bd9Sstevel@tonic-gate warn(gettext("invalid processor ID\n"));
460*7c478bd9Sstevel@tonic-gate continue;
461*7c478bd9Sstevel@tonic-gate }
462*7c478bd9Sstevel@tonic-gate if (Qflag) {
463*7c478bd9Sstevel@tonic-gate (void) proc_walk(query_all_lwp,
464*7c478bd9Sstevel@tonic-gate &cpu, PR_WALK_LWP);
465*7c478bd9Sstevel@tonic-gate continue;
466*7c478bd9Sstevel@tonic-gate }
467*7c478bd9Sstevel@tonic-gate if (Uflag) {
468*7c478bd9Sstevel@tonic-gate if (processor_bind(P_CPUID, cpu,
469*7c478bd9Sstevel@tonic-gate PBIND_NONE, &old_cpu) != 0) {
470*7c478bd9Sstevel@tonic-gate warn(gettext("failed to unbind from "
471*7c478bd9Sstevel@tonic-gate "processor %d"), (int)cpu);
472*7c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
473*7c478bd9Sstevel@tonic-gate }
474*7c478bd9Sstevel@tonic-gate continue;
475*7c478bd9Sstevel@tonic-gate }
476*7c478bd9Sstevel@tonic-gate }
477*7c478bd9Sstevel@tonic-gate return (errors);
478*7c478bd9Sstevel@tonic-gate }
479*7c478bd9Sstevel@tonic-gate
480*7c478bd9Sstevel@tonic-gate /*
481*7c478bd9Sstevel@tonic-gate * Go through listed process[/lwp_ranges].
482*7c478bd9Sstevel@tonic-gate */
483*7c478bd9Sstevel@tonic-gate for (; argc > 0; argv++, argc--) {
484*7c478bd9Sstevel@tonic-gate errno = 0;
485*7c478bd9Sstevel@tonic-gate pid = (id_t)strtol(*argv, &endstr, 10);
486*7c478bd9Sstevel@tonic-gate if (errno != 0 ||
487*7c478bd9Sstevel@tonic-gate (endstr != NULL && *endstr != '\0' && *endstr != '/')) {
488*7c478bd9Sstevel@tonic-gate warn(gettext("invalid process ID: %s\n"), *argv);
489*7c478bd9Sstevel@tonic-gate continue;
490*7c478bd9Sstevel@tonic-gate }
491*7c478bd9Sstevel@tonic-gate if (endstr != NULL && *endstr == '/') {
492*7c478bd9Sstevel@tonic-gate /*
493*7c478bd9Sstevel@tonic-gate * Handle lwp range case
494*7c478bd9Sstevel@tonic-gate */
495*7c478bd9Sstevel@tonic-gate const char *lwps = (const char *)(++endstr);
496*7c478bd9Sstevel@tonic-gate if (*lwps == '\0' ||
497*7c478bd9Sstevel@tonic-gate proc_lwp_range_valid(lwps) != 0) {
498*7c478bd9Sstevel@tonic-gate warn(gettext("invalid lwp range "
499*7c478bd9Sstevel@tonic-gate "for pid %d\n"), (int)pid);
500*7c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
501*7c478bd9Sstevel@tonic-gate continue;
502*7c478bd9Sstevel@tonic-gate }
503*7c478bd9Sstevel@tonic-gate if (!qflag)
504*7c478bd9Sstevel@tonic-gate (void) proc_initstdio();
505*7c478bd9Sstevel@tonic-gate ret = do_lwps(pid, lwps, qflag ? PBIND_QUERY : cpu);
506*7c478bd9Sstevel@tonic-gate if (!qflag)
507*7c478bd9Sstevel@tonic-gate (void) proc_finistdio();
508*7c478bd9Sstevel@tonic-gate if (ret != ERR_OK)
509*7c478bd9Sstevel@tonic-gate errors = ret;
510*7c478bd9Sstevel@tonic-gate } else {
511*7c478bd9Sstevel@tonic-gate /*
512*7c478bd9Sstevel@tonic-gate * Handle whole process case.
513*7c478bd9Sstevel@tonic-gate */
514*7c478bd9Sstevel@tonic-gate if (processor_bind(P_PID, pid, cpu, &old_cpu) < 0) {
515*7c478bd9Sstevel@tonic-gate bind_err(cpu, pid, -1, errno);
516*7c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
517*7c478bd9Sstevel@tonic-gate continue;
518*7c478bd9Sstevel@tonic-gate }
519*7c478bd9Sstevel@tonic-gate if (qflag)
520*7c478bd9Sstevel@tonic-gate query_out(pid, -1, old_cpu);
521*7c478bd9Sstevel@tonic-gate else
522*7c478bd9Sstevel@tonic-gate bind_out(pid, -1, old_cpu, cpu);
523*7c478bd9Sstevel@tonic-gate }
524*7c478bd9Sstevel@tonic-gate }
525*7c478bd9Sstevel@tonic-gate return (errors);
526*7c478bd9Sstevel@tonic-gate }
527