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
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23*f285096aSRyan Zezeski * Copyright 2015 Ryan Zezeski
247c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * pbind - bind a process to a processor (non-exclusively)
307c478bd9Sstevel@tonic-gate */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/procset.h>
347c478bd9Sstevel@tonic-gate #include <sys/processor.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <procfs.h>
397c478bd9Sstevel@tonic-gate #include <fcntl.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <dirent.h>
427c478bd9Sstevel@tonic-gate #include <locale.h>
437c478bd9Sstevel@tonic-gate #include <libproc.h>
447c478bd9Sstevel@tonic-gate #include <stdarg.h>
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */
477c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate #define ERR_OK 0 /* exit status for success */
517c478bd9Sstevel@tonic-gate #define ERR_FAIL 1 /* exit status for errors */
527c478bd9Sstevel@tonic-gate #define ERR_USAGE 2 /* exit status for usage errors */
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate static char *progname;
557c478bd9Sstevel@tonic-gate static char bflag;
56*f285096aSRyan Zezeski static char eflag;
577c478bd9Sstevel@tonic-gate static char qflag;
587c478bd9Sstevel@tonic-gate static char Qflag;
597c478bd9Sstevel@tonic-gate static char uflag;
607c478bd9Sstevel@tonic-gate static char Uflag;
617c478bd9Sstevel@tonic-gate static int errors;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate #define MAX_PROCFS_PATH 80
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
667c478bd9Sstevel@tonic-gate static void
warn(char * format,...)677c478bd9Sstevel@tonic-gate warn(char *format, ...)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate int err = errno;
707c478bd9Sstevel@tonic-gate va_list alist;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", progname);
737c478bd9Sstevel@tonic-gate va_start(alist, format);
747c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist);
757c478bd9Sstevel@tonic-gate va_end(alist);
767c478bd9Sstevel@tonic-gate if (strchr(format, '\n') == NULL)
777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", strerror(err));
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
817c478bd9Sstevel@tonic-gate static void
die(char * format,...)827c478bd9Sstevel@tonic-gate die(char *format, ...)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate int err = errno;
857c478bd9Sstevel@tonic-gate va_list alist;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", progname);
887c478bd9Sstevel@tonic-gate va_start(alist, format);
897c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist);
907c478bd9Sstevel@tonic-gate va_end(alist);
917c478bd9Sstevel@tonic-gate if (strchr(format, '\n') == NULL)
927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %s\n", strerror(err));
937c478bd9Sstevel@tonic-gate exit(ERR_FAIL);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate * Output for query.
987c478bd9Sstevel@tonic-gate */
997c478bd9Sstevel@tonic-gate static void
query_out(id_t pid,id_t lwpid,processorid_t cpu)1007c478bd9Sstevel@tonic-gate query_out(id_t pid, id_t lwpid, processorid_t cpu)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate char *proclwp;
1037c478bd9Sstevel@tonic-gate char pidstr[21];
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate if (lwpid == -1) {
1067c478bd9Sstevel@tonic-gate (void) snprintf(pidstr, 20, "%d", (int)pid);
1077c478bd9Sstevel@tonic-gate proclwp = "process";
1087c478bd9Sstevel@tonic-gate } else {
1097c478bd9Sstevel@tonic-gate (void) snprintf(pidstr, 20, "%d/%d", (int)pid, (int)lwpid);
1107c478bd9Sstevel@tonic-gate proclwp = "lwp";
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate if (cpu == PBIND_NONE)
1147c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: not bound\n"),
1157c478bd9Sstevel@tonic-gate proclwp, pidstr);
1167c478bd9Sstevel@tonic-gate else
1177c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: %d\n"),
1187c478bd9Sstevel@tonic-gate proclwp, pidstr, cpu);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate * Binding error.
1237c478bd9Sstevel@tonic-gate */
1247c478bd9Sstevel@tonic-gate static void
bind_err(processorid_t cpu,id_t pid,id_t lwpid,int err)1257c478bd9Sstevel@tonic-gate bind_err(processorid_t cpu, id_t pid, id_t lwpid, int err)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate char *msg;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate switch (cpu) {
1307c478bd9Sstevel@tonic-gate case PBIND_NONE:
1317c478bd9Sstevel@tonic-gate msg = gettext("unbind");
1327c478bd9Sstevel@tonic-gate break;
1337c478bd9Sstevel@tonic-gate case PBIND_QUERY:
1347c478bd9Sstevel@tonic-gate msg = gettext("query");
1357c478bd9Sstevel@tonic-gate break;
1367c478bd9Sstevel@tonic-gate default:
1377c478bd9Sstevel@tonic-gate msg = gettext("bind");
1387c478bd9Sstevel@tonic-gate break;
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate if (lwpid == -1)
1417c478bd9Sstevel@tonic-gate warn(gettext("cannot %s pid %d: %s\n"), msg,
1427c478bd9Sstevel@tonic-gate (int)pid, strerror(err));
1437c478bd9Sstevel@tonic-gate else
1447c478bd9Sstevel@tonic-gate warn(gettext("cannot %s lwpid %d/%d: %s\n"), msg,
1457c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid, strerror(err));
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate * Output for bind.
1507c478bd9Sstevel@tonic-gate */
1517c478bd9Sstevel@tonic-gate static void
bind_out(id_t pid,id_t lwpid,processorid_t old,processorid_t new)1527c478bd9Sstevel@tonic-gate bind_out(id_t pid, id_t lwpid, processorid_t old, processorid_t new)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate char *proclwp;
1557c478bd9Sstevel@tonic-gate char pidstr[21];
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate if (lwpid == -1) {
1587c478bd9Sstevel@tonic-gate (void) snprintf(pidstr, 20, "%d", (int)pid);
1597c478bd9Sstevel@tonic-gate proclwp = "process";
1607c478bd9Sstevel@tonic-gate } else {
1617c478bd9Sstevel@tonic-gate (void) snprintf(pidstr, 20, "%d/%d", (int)pid, (int)lwpid);
1627c478bd9Sstevel@tonic-gate proclwp = "lwp";
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate if (old == PBIND_NONE) {
1667c478bd9Sstevel@tonic-gate if (new == PBIND_NONE)
1677c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: was not bound, "
1687c478bd9Sstevel@tonic-gate "now not bound\n"), proclwp, pidstr);
1697c478bd9Sstevel@tonic-gate else
1707c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: was not bound, "
1717c478bd9Sstevel@tonic-gate "now %d\n"), proclwp, pidstr, new);
1727c478bd9Sstevel@tonic-gate } else {
1737c478bd9Sstevel@tonic-gate if (new == PBIND_NONE)
1747c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: was %d, "
1757c478bd9Sstevel@tonic-gate "now not bound\n"), proclwp, pidstr, old);
1767c478bd9Sstevel@tonic-gate else
1777c478bd9Sstevel@tonic-gate (void) printf(gettext("%s id %s: was %d, "
1787c478bd9Sstevel@tonic-gate "now %d\n"), proclwp, pidstr, old, new);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate static struct ps_prochandle *
grab_proc(id_t pid)1837c478bd9Sstevel@tonic-gate grab_proc(id_t pid)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate int ret;
1867c478bd9Sstevel@tonic-gate struct ps_prochandle *Pr;
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate if ((Pr = Pgrab(pid, 0, &ret)) == NULL) {
1897c478bd9Sstevel@tonic-gate warn(gettext("cannot control process %d: %s\n"),
1907c478bd9Sstevel@tonic-gate (int)pid, Pgrab_error(ret));
1917c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
1927c478bd9Sstevel@tonic-gate return (NULL);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate * Set run-on-last-close flag so the controlled process
1977c478bd9Sstevel@tonic-gate * runs even if we die on a signal, and create an agent LWP.
1987c478bd9Sstevel@tonic-gate */
1997c478bd9Sstevel@tonic-gate if (Psetflags(Pr, PR_RLC) != 0 || Pcreate_agent(Pr) != 0) {
2007c478bd9Sstevel@tonic-gate warn(gettext("cannot control process %d\n"), (int)pid);
2017c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
2027c478bd9Sstevel@tonic-gate Prelease(Pr, 0);
2037c478bd9Sstevel@tonic-gate return (NULL);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate return (Pr);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate static void
rele_proc(struct ps_prochandle * Pr)2097c478bd9Sstevel@tonic-gate rele_proc(struct ps_prochandle *Pr)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate if (Pr == NULL)
2127c478bd9Sstevel@tonic-gate return;
2137c478bd9Sstevel@tonic-gate Pdestroy_agent(Pr);
2147c478bd9Sstevel@tonic-gate Prelease(Pr, 0);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate static void
bind_lwp(struct ps_prochandle * Pr,id_t pid,id_t lwpid,processorid_t cpu)2187c478bd9Sstevel@tonic-gate bind_lwp(struct ps_prochandle *Pr, id_t pid, id_t lwpid, processorid_t cpu)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate processorid_t old_cpu;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate if (pr_processor_bind(Pr, P_LWPID, lwpid, cpu, &old_cpu) < 0) {
2237c478bd9Sstevel@tonic-gate bind_err(cpu, pid, lwpid, errno);
2247c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
2257c478bd9Sstevel@tonic-gate } else {
2267c478bd9Sstevel@tonic-gate if (qflag)
2277c478bd9Sstevel@tonic-gate query_out(pid, lwpid, old_cpu);
2287c478bd9Sstevel@tonic-gate else
2297c478bd9Sstevel@tonic-gate bind_out(pid, lwpid, old_cpu, cpu);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate * Query, set, or clear bindings for the range of LWPs in the given process.
2357c478bd9Sstevel@tonic-gate */
2367c478bd9Sstevel@tonic-gate static int
do_lwps(id_t pid,const char * range,processorid_t cpu)2377c478bd9Sstevel@tonic-gate do_lwps(id_t pid, const char *range, processorid_t cpu)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate char procfile[MAX_PROCFS_PATH];
2407c478bd9Sstevel@tonic-gate struct ps_prochandle *Pr;
2417c478bd9Sstevel@tonic-gate struct prheader header;
2427c478bd9Sstevel@tonic-gate processorid_t binding;
2437c478bd9Sstevel@tonic-gate struct lwpsinfo *lwp;
2447c478bd9Sstevel@tonic-gate char *lpsinfo, *ptr;
2457c478bd9Sstevel@tonic-gate int nent, size;
2467c478bd9Sstevel@tonic-gate int i, fd, found;
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate * Report bindings for LWPs in process 'pid'.
2507c478bd9Sstevel@tonic-gate */
2517c478bd9Sstevel@tonic-gate (void) snprintf(procfile, MAX_PROCFS_PATH,
2527c478bd9Sstevel@tonic-gate "/proc/%d/lpsinfo", (int)pid);
2537c478bd9Sstevel@tonic-gate if ((fd = open(procfile, O_RDONLY)) < 0) {
2547c478bd9Sstevel@tonic-gate if (errno == ENOENT)
2557c478bd9Sstevel@tonic-gate errno = ESRCH;
2567c478bd9Sstevel@tonic-gate bind_err(cpu, pid, -1, errno);
2577c478bd9Sstevel@tonic-gate return (ERR_FAIL);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate if (pread(fd, &header, sizeof (header), 0) != sizeof (header)) {
2607c478bd9Sstevel@tonic-gate (void) close(fd);
2617c478bd9Sstevel@tonic-gate bind_err(cpu, pid, -1, errno);
2627c478bd9Sstevel@tonic-gate return (ERR_FAIL);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate nent = header.pr_nent;
2657c478bd9Sstevel@tonic-gate size = header.pr_entsize * nent;
2667c478bd9Sstevel@tonic-gate ptr = lpsinfo = malloc(size);
2677c478bd9Sstevel@tonic-gate if (lpsinfo == NULL) {
2687c478bd9Sstevel@tonic-gate bind_err(cpu, pid, -1, errno);
2697c478bd9Sstevel@tonic-gate return (ERR_FAIL);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate if (pread(fd, lpsinfo, size, sizeof (header)) != size) {
2727c478bd9Sstevel@tonic-gate bind_err(cpu, pid, -1, errno);
2737c478bd9Sstevel@tonic-gate free(lpsinfo);
2747c478bd9Sstevel@tonic-gate (void) close(fd);
2757c478bd9Sstevel@tonic-gate return (ERR_FAIL);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate if ((bflag || uflag) && (Pr = grab_proc(pid)) == NULL) {
2797c478bd9Sstevel@tonic-gate free(lpsinfo);
2807c478bd9Sstevel@tonic-gate (void) close(fd);
2817c478bd9Sstevel@tonic-gate return (ERR_FAIL);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate found = 0;
2847c478bd9Sstevel@tonic-gate for (i = 0; i < nent; i++, ptr += header.pr_entsize) {
2857c478bd9Sstevel@tonic-gate /*LINTED ALIGNMENT*/
2867c478bd9Sstevel@tonic-gate lwp = (lwpsinfo_t *)ptr;
2877c478bd9Sstevel@tonic-gate binding = lwp->pr_bindpro;
2887c478bd9Sstevel@tonic-gate if (!proc_lwp_in_set(range, lwp->pr_lwpid))
2897c478bd9Sstevel@tonic-gate continue;
2907c478bd9Sstevel@tonic-gate found++;
2917c478bd9Sstevel@tonic-gate if (bflag || uflag)
2927c478bd9Sstevel@tonic-gate bind_lwp(Pr, pid, lwp->pr_lwpid, cpu);
2937c478bd9Sstevel@tonic-gate else if (binding != PBIND_NONE)
2947c478bd9Sstevel@tonic-gate query_out(pid, lwp->pr_lwpid, binding);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate if (bflag || uflag)
2977c478bd9Sstevel@tonic-gate rele_proc(Pr);
2987c478bd9Sstevel@tonic-gate free(lpsinfo);
2997c478bd9Sstevel@tonic-gate (void) close(fd);
3007c478bd9Sstevel@tonic-gate if (found == 0) {
3017c478bd9Sstevel@tonic-gate warn(gettext("cannot %s lwpid %d/%s: "
3027c478bd9Sstevel@tonic-gate "No matching LWPs found\n"),
3037c478bd9Sstevel@tonic-gate bflag ? "bind" : "query", pid, range);
3047c478bd9Sstevel@tonic-gate return (ERR_FAIL);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate return (ERR_OK);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3107c478bd9Sstevel@tonic-gate static int
query_all_proc(psinfo_t * psinfo,lwpsinfo_t * lwpsinfo,void * arg)3117c478bd9Sstevel@tonic-gate query_all_proc(psinfo_t *psinfo, lwpsinfo_t *lwpsinfo, void *arg)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate id_t pid = psinfo->pr_pid;
3147c478bd9Sstevel@tonic-gate processorid_t binding;
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate if (processor_bind(P_PID, pid, PBIND_QUERY, &binding) < 0) {
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate * Ignore search errors. The process may have exited
3197c478bd9Sstevel@tonic-gate * since we read the directory.
3207c478bd9Sstevel@tonic-gate */
3217c478bd9Sstevel@tonic-gate if (errno == ESRCH)
3227c478bd9Sstevel@tonic-gate return (0);
3237c478bd9Sstevel@tonic-gate bind_err(PBIND_QUERY, pid, -1, errno);
3247c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
3257c478bd9Sstevel@tonic-gate return (0);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate if (binding != PBIND_NONE)
3287c478bd9Sstevel@tonic-gate query_out(pid, -1, binding);
3297c478bd9Sstevel@tonic-gate return (0);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate static int
query_all_lwp(psinfo_t * psinfo,lwpsinfo_t * lwpsinfo,void * arg)3337c478bd9Sstevel@tonic-gate query_all_lwp(psinfo_t *psinfo, lwpsinfo_t *lwpsinfo, void *arg)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate id_t pid = psinfo->pr_pid;
3367c478bd9Sstevel@tonic-gate id_t lwpid = lwpsinfo->pr_lwpid;
3377c478bd9Sstevel@tonic-gate processorid_t *cpuid = arg;
3387c478bd9Sstevel@tonic-gate processorid_t binding = lwpsinfo->pr_bindpro;
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate if (psinfo->pr_nlwp == 1)
3417c478bd9Sstevel@tonic-gate lwpid = -1; /* report process bindings if only 1 lwp */
3427c478bd9Sstevel@tonic-gate if ((cpuid != NULL && *cpuid == binding) ||
3437c478bd9Sstevel@tonic-gate (cpuid == NULL && binding != PBIND_NONE))
3447c478bd9Sstevel@tonic-gate query_out(pid, lwpid, binding);
3457c478bd9Sstevel@tonic-gate return (0);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate
348*f285096aSRyan Zezeski /*
349*f285096aSRyan Zezeski * Execute the cmd with args while bound to cpu. Does not return:
350*f285096aSRyan Zezeski * either executes cmd successfully or dies trying.
351*f285096aSRyan Zezeski */
352*f285096aSRyan Zezeski static void
exec_cmd(processorid_t cpu,char * cmd,char ** args)353*f285096aSRyan Zezeski exec_cmd(processorid_t cpu, char *cmd, char **args)
354*f285096aSRyan Zezeski {
355*f285096aSRyan Zezeski if (processor_bind(P_PID, P_MYID, cpu, NULL) == -1) {
356*f285096aSRyan Zezeski bind_err(cpu, getpid(), -1, errno);
357*f285096aSRyan Zezeski exit(ERR_FAIL);
358*f285096aSRyan Zezeski }
359*f285096aSRyan Zezeski
360*f285096aSRyan Zezeski if (execvp(cmd, args) == -1)
361*f285096aSRyan Zezeski die(gettext("failed to exec %s\n"), cmd);
362*f285096aSRyan Zezeski }
363*f285096aSRyan Zezeski
364*f285096aSRyan Zezeski /*
365*f285096aSRyan Zezeski * Attempt to parse str as a CPU identifier. Return the identifier or
366*f285096aSRyan Zezeski * die.
367*f285096aSRyan Zezeski */
368*f285096aSRyan Zezeski static processorid_t
parse_cpu(char * str)369*f285096aSRyan Zezeski parse_cpu(char *str)
370*f285096aSRyan Zezeski {
371*f285096aSRyan Zezeski processorid_t cpu;
372*f285096aSRyan Zezeski char *endstr;
373*f285096aSRyan Zezeski
374*f285096aSRyan Zezeski cpu = strtol(str, &endstr, 10);
375*f285096aSRyan Zezeski if (endstr != NULL && *endstr != '\0' || cpu < 0)
376*f285096aSRyan Zezeski die(gettext("invalid processor ID %s\n"), optarg);
377*f285096aSRyan Zezeski
378*f285096aSRyan Zezeski return (cpu);
379*f285096aSRyan Zezeski }
380*f285096aSRyan Zezeski
3817c478bd9Sstevel@tonic-gate static int
usage(void)3827c478bd9Sstevel@tonic-gate usage(void)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
3857c478bd9Sstevel@tonic-gate gettext("usage: \n\t%1$s -b processor_id pid[/lwpids] ...\n"
386*f285096aSRyan Zezeski "\t%1$s -e processor_id cmd [args...]\n"
3877c478bd9Sstevel@tonic-gate "\t%1$s -U [processor_id] ...\n"
3887c478bd9Sstevel@tonic-gate "\t%1$s -Q [processor_id] ...\n"
3897c478bd9Sstevel@tonic-gate "\t%1$s -u pid[/lwpids] ...\n"
3907c478bd9Sstevel@tonic-gate "\t%1$s [-q] [pid[/lwpids] ...]\n"),
3917c478bd9Sstevel@tonic-gate progname);
3927c478bd9Sstevel@tonic-gate return (ERR_USAGE);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate
3957c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])3967c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate int c;
3997c478bd9Sstevel@tonic-gate int ret;
4007c478bd9Sstevel@tonic-gate id_t pid;
4017c478bd9Sstevel@tonic-gate processorid_t cpu, old_cpu;
4027c478bd9Sstevel@tonic-gate char *endstr;
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate progname = argv[0]; /* put actual command name in messages */
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); /* setup localization */
4077c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
4087c478bd9Sstevel@tonic-gate
409*f285096aSRyan Zezeski while ((c = getopt(argc, argv, "b:e:qQuU")) != EOF) {
4107c478bd9Sstevel@tonic-gate switch (c) {
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate case 'b':
4137c478bd9Sstevel@tonic-gate bflag = 1;
414*f285096aSRyan Zezeski cpu = parse_cpu(optarg);
415*f285096aSRyan Zezeski break;
416*f285096aSRyan Zezeski
417*f285096aSRyan Zezeski case 'e':
418*f285096aSRyan Zezeski eflag = 1;
419*f285096aSRyan Zezeski cpu = parse_cpu(optarg);
4207c478bd9Sstevel@tonic-gate break;
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate case 'q':
4237c478bd9Sstevel@tonic-gate qflag = 1;
4247c478bd9Sstevel@tonic-gate cpu = PBIND_QUERY;
4257c478bd9Sstevel@tonic-gate break;
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate case 'Q':
4287c478bd9Sstevel@tonic-gate Qflag = 1;
4297c478bd9Sstevel@tonic-gate cpu = PBIND_QUERY;
4307c478bd9Sstevel@tonic-gate break;
4317c478bd9Sstevel@tonic-gate
4327c478bd9Sstevel@tonic-gate case 'u':
4337c478bd9Sstevel@tonic-gate uflag = 1;
4347c478bd9Sstevel@tonic-gate cpu = PBIND_NONE;
4357c478bd9Sstevel@tonic-gate break;
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate case 'U':
4387c478bd9Sstevel@tonic-gate Uflag = 1;
4397c478bd9Sstevel@tonic-gate break;
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate default:
4427c478bd9Sstevel@tonic-gate return (usage());
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate /*
448*f285096aSRyan Zezeski * Make sure that at most one of the options b, e, q, Q, u, or
449*f285096aSRyan Zezeski * U was specified.
4507c478bd9Sstevel@tonic-gate */
451*f285096aSRyan Zezeski c = bflag + eflag + qflag + Qflag + uflag + Uflag;
4527c478bd9Sstevel@tonic-gate if (c < 1) { /* nothing specified */
4537c478bd9Sstevel@tonic-gate qflag = 1; /* default to query */
4547c478bd9Sstevel@tonic-gate cpu = PBIND_QUERY;
4557c478bd9Sstevel@tonic-gate } else if (c > 1) {
456*f285096aSRyan Zezeski warn(gettext("options -b, -e, -q, -Q, -u and -U "
4577c478bd9Sstevel@tonic-gate "are mutually exclusive\n"));
4587c478bd9Sstevel@tonic-gate return (usage());
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate errors = 0;
4627c478bd9Sstevel@tonic-gate argc -= optind;
4637c478bd9Sstevel@tonic-gate argv += optind;
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate * Handle query of all processes.
4677c478bd9Sstevel@tonic-gate */
4687c478bd9Sstevel@tonic-gate if (argc == 0) {
4697c478bd9Sstevel@tonic-gate if (bflag || uflag) {
4707c478bd9Sstevel@tonic-gate warn(gettext("must specify at least one pid\n"));
4717c478bd9Sstevel@tonic-gate return (usage());
4727c478bd9Sstevel@tonic-gate }
473*f285096aSRyan Zezeski if (eflag) {
474*f285096aSRyan Zezeski warn(gettext("must specify command\n"));
475*f285096aSRyan Zezeski return (usage());
476*f285096aSRyan Zezeski }
4777c478bd9Sstevel@tonic-gate if (Uflag) {
4787c478bd9Sstevel@tonic-gate if (processor_bind(P_ALL, 0, PBIND_NONE, &old_cpu) != 0)
4797c478bd9Sstevel@tonic-gate die(gettext("failed to unbind some LWPs"));
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate if (Qflag) {
4827c478bd9Sstevel@tonic-gate (void) proc_walk(query_all_lwp, NULL, PR_WALK_LWP);
4837c478bd9Sstevel@tonic-gate return (errors);
4847c478bd9Sstevel@tonic-gate } else {
4857c478bd9Sstevel@tonic-gate (void) proc_walk(query_all_proc, NULL, PR_WALK_PROC);
4867c478bd9Sstevel@tonic-gate return (errors);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate
490*f285096aSRyan Zezeski if (eflag)
491*f285096aSRyan Zezeski exec_cmd(cpu, argv[0], argv);
492*f285096aSRyan Zezeski
4937c478bd9Sstevel@tonic-gate if (Qflag || Uflag) {
4947c478bd9Sstevel@tonic-gate /*
4957c478bd9Sstevel@tonic-gate * Go through listed processor IDs.
4967c478bd9Sstevel@tonic-gate */
4977c478bd9Sstevel@tonic-gate for (; argc > 0; argv++, argc--) {
4987c478bd9Sstevel@tonic-gate errno = 0;
4997c478bd9Sstevel@tonic-gate cpu = (id_t)strtol(*argv, &endstr, 10);
5007c478bd9Sstevel@tonic-gate if (errno != 0 || (endstr != NULL && *endstr != '\0') ||
5017c478bd9Sstevel@tonic-gate p_online(cpu, P_STATUS) == -1) {
5027c478bd9Sstevel@tonic-gate warn(gettext("invalid processor ID\n"));
5037c478bd9Sstevel@tonic-gate continue;
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate if (Qflag) {
5067c478bd9Sstevel@tonic-gate (void) proc_walk(query_all_lwp,
5077c478bd9Sstevel@tonic-gate &cpu, PR_WALK_LWP);
5087c478bd9Sstevel@tonic-gate continue;
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate if (Uflag) {
5117c478bd9Sstevel@tonic-gate if (processor_bind(P_CPUID, cpu,
5127c478bd9Sstevel@tonic-gate PBIND_NONE, &old_cpu) != 0) {
5137c478bd9Sstevel@tonic-gate warn(gettext("failed to unbind from "
5147c478bd9Sstevel@tonic-gate "processor %d"), (int)cpu);
5157c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate continue;
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate return (errors);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate * Go through listed process[/lwp_ranges].
5257c478bd9Sstevel@tonic-gate */
5267c478bd9Sstevel@tonic-gate for (; argc > 0; argv++, argc--) {
5277c478bd9Sstevel@tonic-gate errno = 0;
5287c478bd9Sstevel@tonic-gate pid = (id_t)strtol(*argv, &endstr, 10);
5297c478bd9Sstevel@tonic-gate if (errno != 0 ||
5307c478bd9Sstevel@tonic-gate (endstr != NULL && *endstr != '\0' && *endstr != '/')) {
5317c478bd9Sstevel@tonic-gate warn(gettext("invalid process ID: %s\n"), *argv);
5327c478bd9Sstevel@tonic-gate continue;
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate if (endstr != NULL && *endstr == '/') {
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate * Handle lwp range case
5377c478bd9Sstevel@tonic-gate */
5387c478bd9Sstevel@tonic-gate const char *lwps = (const char *)(++endstr);
5397c478bd9Sstevel@tonic-gate if (*lwps == '\0' ||
5407c478bd9Sstevel@tonic-gate proc_lwp_range_valid(lwps) != 0) {
5417c478bd9Sstevel@tonic-gate warn(gettext("invalid lwp range "
5427c478bd9Sstevel@tonic-gate "for pid %d\n"), (int)pid);
5437c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
5447c478bd9Sstevel@tonic-gate continue;
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate if (!qflag)
5477c478bd9Sstevel@tonic-gate (void) proc_initstdio();
5487c478bd9Sstevel@tonic-gate ret = do_lwps(pid, lwps, qflag ? PBIND_QUERY : cpu);
5497c478bd9Sstevel@tonic-gate if (!qflag)
5507c478bd9Sstevel@tonic-gate (void) proc_finistdio();
5517c478bd9Sstevel@tonic-gate if (ret != ERR_OK)
5527c478bd9Sstevel@tonic-gate errors = ret;
5537c478bd9Sstevel@tonic-gate } else {
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate * Handle whole process case.
5567c478bd9Sstevel@tonic-gate */
5577c478bd9Sstevel@tonic-gate if (processor_bind(P_PID, pid, cpu, &old_cpu) < 0) {
5587c478bd9Sstevel@tonic-gate bind_err(cpu, pid, -1, errno);
5597c478bd9Sstevel@tonic-gate errors = ERR_FAIL;
5607c478bd9Sstevel@tonic-gate continue;
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate if (qflag)
5637c478bd9Sstevel@tonic-gate query_out(pid, -1, old_cpu);
5647c478bd9Sstevel@tonic-gate else
5657c478bd9Sstevel@tonic-gate bind_out(pid, -1, old_cpu, cpu);
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate return (errors);
5697c478bd9Sstevel@tonic-gate }
570