summaryrefslogtreecommitdiff
path: root/usr/src/cmd/psrset/psrset.c
blob: 35585499a940e47e8afa377ae9d3997048ab608b (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * psrset - create and manage processor sets
 */

#include <sys/types.h>
#include <sys/procset.h>
#include <sys/processor.h>
#include <sys/pset.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <dirent.h>
#include <locale.h>
#include <string.h>
#include <limits.h>
#include <procfs.h>
#include <libproc.h>
#include <stdarg.h>

#if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
#define	TEXT_DOMAIN 	"SYS_TEST"	/* Use this only if it wasn't */
#endif

#define	MAX_PROCFS_PATH	80

#define	ERR_OK		0		/* exit status for success */
#define	ERR_FAIL	1		/* exit status for errors */
#define	ERR_USAGE	2		/* exit status for usage errors */

static char *progname;
static int errors;
static char cflag;
static char dflag;
static char aflag;
static char rflag;
static char iflag;
static char bflag;
static char uflag;
static char Uflag;
static char qflag;
static char Qflag;
static char pflag;
static char nflag;
static char fflag;
static char Fflag;
static char eflag;

extern int pset_assign_forced(psetid_t, processorid_t, psetid_t *);

/*PRINTFLIKE1*/
static void
warn(char *format, ...)
{
	int err = errno;
	va_list alist;

	(void) fprintf(stderr, "%s: ", progname);
	va_start(alist, format);
	(void) vfprintf(stderr, format, alist);
	va_end(alist);
	if (strchr(format, '\n') == NULL)
		(void) fprintf(stderr, ": %s\n", strerror(err));
}

/*PRINTFLIKE1*/
static void
die(char *format, ...)
{
	int err = errno;
	va_list alist;

	(void) fprintf(stderr, "%s: ", progname);
	va_start(alist, format);
	(void) vfprintf(stderr, format, alist);
	va_end(alist);
	if (strchr(format, '\n') == NULL)
		(void) fprintf(stderr, ": %s\n", strerror(err));
	exit(ERR_FAIL);
}

static struct ps_prochandle *
grab_proc(id_t pid)
{
	int ret;
	struct ps_prochandle *Pr;

	if ((Pr = Pgrab(pid, 0, &ret)) == NULL) {
		warn(gettext("cannot control process %d: %s\n"),
		    (int)pid, Pgrab_error(ret));
		errors = ERR_FAIL;
		return (NULL);
	}

	return (Pr);
}

static void
rele_proc(struct ps_prochandle *Pr)
{
	if (Pr == NULL)
		return;
	Prelease(Pr, 0);
}

static void
bind_err(psetid_t pset, id_t pid, id_t lwpid, int err)
{
	char    *msg;

	switch (pset) {
	case PS_NONE:
		msg = gettext("unbind");
		break;
	case PS_QUERY:
		msg = gettext("query");
		break;
	default:
		msg = gettext("bind");
		break;
	}

	errno = err;
	if (lwpid == -1)
		warn(gettext("cannot %s pid %d"), msg, pid);
	else
		warn(gettext("cannot %s lwpid %d/%d"), msg, pid, lwpid);
}

/*
 * Output for create.
 */
static void
create_out(psetid_t pset)
{
	(void) printf("%s %d\n", gettext("created processor set"), pset);
}

/*
 * Output for assign.
 */
static void
assign_out(processorid_t cpu, psetid_t old, psetid_t new)
{
	if (old == PS_NONE) {
		if (new == PS_NONE)
			(void) printf(gettext("processor %d: was not assigned,"
			    " now not assigned\n"), cpu);
		else
			(void) printf(gettext("processor %d: was not assigned,"
			    " now %d\n"), cpu, new);
	} else {
		if (new == PS_NONE)
			(void) printf(gettext("processor %d: was %d, "
			    "now not assigned\n"), cpu, old);
		else
			(void) printf(gettext("processor %d: was %d, "
			    "now %d\n"), cpu, old, new);
	}
}

/*
 * Output for query.
 */
static void
query_out(id_t pid, id_t lwpid, psetid_t pset)
{
	char *proclwp;
	char pidstr[21];

	if (lwpid == -1) {
		(void) snprintf(pidstr, 20, "%d", pid);
		proclwp = "process";
	} else {
		(void) snprintf(pidstr, 20, "%d/%d", pid, lwpid);
		proclwp = "lwp";
	}

	if (pset == PS_NONE)
		(void) printf(gettext("%s id %s: not bound\n"),
		    proclwp, pidstr);
	else
		(void) printf(gettext("%s id %s: %d\n"), proclwp, pidstr, pset);
}

/*
 * Output for info.
 */
static void
info_out(psetid_t pset, int type, uint_t numcpus, processorid_t *cpus)
{
	int i;
	if (type == PS_SYSTEM)
		(void) printf(gettext("system processor set %d:"), pset);
	else
		(void) printf(gettext("user processor set %d:"), pset);
	if (numcpus == 0)
		(void) printf(gettext(" empty"));
	else if (numcpus > 1)
		(void) printf(gettext(" processors"));
	else
		(void) printf(gettext(" processor"));
	for (i = 0; i < numcpus; i++)
		(void) printf(" %d", cpus[i]);
	(void) printf("\n");
}

/*
 * Output for print.
 */
static void
print_out(processorid_t cpu, psetid_t pset)
{
	if (pset == PS_NONE)
		(void) printf(gettext("processor %d: not assigned\n"), cpu);
	else
		(void) printf(gettext("processor %d: %d\n"), cpu, pset);
}

/*
 * Output for bind.
 */
static void
bind_out(id_t pid, id_t lwpid, psetid_t old, psetid_t new)
{
	char *proclwp;
	char pidstr[21];

	if (lwpid == -1) {
		(void) snprintf(pidstr, 20, "%d", pid);
		proclwp = "process";
	} else {
		(void) snprintf(pidstr, 20, "%d/%d", pid, lwpid);
		proclwp = "lwp";
	}

	if (old == PS_NONE) {
		if (new == PS_NONE)
			(void) printf(gettext("%s id %s: was not bound, "
			    "now not bound\n"), proclwp, pidstr);
		else
			(void) printf(gettext("%s id %s: was not bound, "
			    "now %d\n"), proclwp, pidstr, new);
	} else {
		if (new == PS_NONE)
			(void) printf(gettext("%s id %s: was %d, "
			    "now not bound\n"), proclwp, pidstr, old);
		else
			(void) printf(gettext("%s id %s: was %d, "
			    "now %d\n"), proclwp, pidstr, old, new);
	}
}

static void
bind_lwp(id_t pid, id_t lwpid, psetid_t pset)
{
	psetid_t old_pset;

	if (pset_bind_lwp(pset, lwpid, pid, &old_pset) != 0) {
		bind_err(pset, pid, lwpid, errno);
		errors = ERR_FAIL;
	}
	if (errors != ERR_FAIL) {
		if (qflag)
			query_out(pid, lwpid, old_pset);
		else
			bind_out(pid, lwpid, old_pset, pset);
	}
}

static int
do_cpu(psetid_t pset, processorid_t cpu, int print, int mustexist)
{
	psetid_t old_pset;
	int err;

	if ((!Fflag && pset_assign(pset, cpu, &old_pset) != 0) ||
	    (Fflag && pset_assign_forced(pset, cpu, &old_pset) != 0)) {
		if (errno == EINVAL && !mustexist)
			return (EINVAL);
		err = errno;

		switch (pset) {
		case PS_NONE:
			warn(gettext("cannot remove processor %d"), cpu);
			break;
		case PS_QUERY:
			warn(gettext("cannot query processor %d"), cpu);
			break;
		default:
			warn(gettext("cannot assign processor %d"), cpu);
			break;
		}
		return (err);
	}
	if (print)
		print_out(cpu, old_pset);
	else
		assign_out(cpu, old_pset, pset);
	return (0);
}

static int
do_range(psetid_t pset, processorid_t first, processorid_t last, int print)
{
	processorid_t cpu;
	int error = ERR_OK;
	int err;
	int found_one = 0;

	for (cpu = first; cpu <= last; cpu++) {
		if ((err = do_cpu(pset, cpu, print, 0)) == 0)
			found_one = 1;
		else if (err != EINVAL)
			error = ERR_FAIL;
	}
	if (!found_one && error == ERR_OK) {
		warn(gettext("no processors in range %d-%d\n"), first, last);
		error = ERR_FAIL;
	}
	return (error);
}

static int
do_info(psetid_t pset)
{
	int	type;
	uint_t	numcpus;
	processorid_t	*cpus;

	numcpus = (uint_t)sysconf(_SC_NPROCESSORS_MAX);
	cpus = (processorid_t *)
	    malloc(numcpus * sizeof (processorid_t));
	if (cpus == NULL) {
		warn(gettext("memory allocation failed"));
		return (ERR_FAIL);
	}
	if (pset_info(pset, &type, &numcpus, cpus) != 0) {
		warn(gettext("cannot get info for processor set %d"), pset);
		free(cpus);
		return (ERR_FAIL);
	}
	info_out(pset, type, numcpus, cpus);
	free(cpus);
	return (ERR_OK);
}

static int
do_destroy(psetid_t pset)
{
	if (pset_destroy(pset) != 0) {
		warn(gettext("could not remove processor set %d"), pset);
		return (ERR_FAIL);
	}
	(void) printf(gettext("removed processor set %d\n"), pset);
	return (ERR_OK);
}

static int
do_intr(psetid_t pset, int flag)
{
	uint_t i, numcpus;
	processorid_t *cpus;
	int error = ERR_OK;

	numcpus = (uint_t)sysconf(_SC_NPROCESSORS_MAX);
	cpus = (processorid_t *)
	    malloc(numcpus * sizeof (processorid_t));
	if (cpus == NULL) {
		warn(gettext("memory allocation failed"));
		return (ERR_FAIL);
	}
	if (pset_info(pset, NULL, &numcpus, cpus) != 0) {
		warn(gettext(
		    "cannot set interrupt status for processor set %d"), pset);
		free(cpus);
		return (ERR_FAIL);
	}
	for (i = 0; i < numcpus; i++) {
		int status = p_online(cpus[i], P_STATUS);
		if (status != P_OFFLINE && status != P_POWEROFF &&
		    status != flag) {
			if (p_online(cpus[i], flag) == -1) {
				warn(gettext("processor %d"), cpus[i]);
				error = ERR_FAIL;
			}
		}
	}
	free(cpus);
	return (error);
}

/*
 * Query the type and CPUs for all active processor sets in the system.
 */
static int
info_all(void)
{
	psetid_t *psetlist;
	uint_t	npsets, oldnpsets;
	int	i;
	int	errors = ERR_OK;

	if (pset_list(NULL, &npsets) != 0) {
		warn(gettext("cannot get number of processor sets"));
		return (1);
	}
	for (;;) {
		psetlist = malloc(sizeof (psetid_t) * npsets);
		if (psetlist == NULL) {
			warn(gettext("memory allocation failed"));
			return (ERR_FAIL);
		}
		oldnpsets = npsets;
		if (pset_list(psetlist, &npsets) != 0) {
			warn(gettext("cannot get list of processor sets"));
			free(psetlist);
			return (ERR_FAIL);
		}
		if (npsets <= oldnpsets)
			break;
		free(psetlist);
	}

	for (i = 0; i < npsets; i++) {
		if (do_info(psetlist[i]))
			errors = ERR_FAIL;
	}
	free(psetlist);
	return (errors);
}

/*
 * Query the processor set assignments for all CPUs in the system.
 */
static int
print_all(void)
{
	psetid_t	pset;
	processorid_t cpuid, max_cpuid;
	int	errors = ERR_OK;

	max_cpuid = (processorid_t)sysconf(_SC_CPUID_MAX);
	for (cpuid = 0; cpuid <= max_cpuid; cpuid++) {
		if (pset_assign(PS_QUERY, cpuid, &pset) == 0) {
			if (pset != PS_NONE)
				print_out(cpuid, pset);
		} else if (errno != EINVAL) {
			warn(gettext("cannot query processor %d"), cpuid);
			errors = ERR_FAIL;
		}
	}
	return (errors);
}

/*ARGSUSED*/
static int
query_all_proc(psinfo_t *psinfo, lwpsinfo_t *lwpsinfo, void *arg)
{
	id_t pid = psinfo->pr_pid;
	psetid_t binding;

	if (pset_bind(PS_QUERY, P_PID, pid, &binding) < 0) {
		/*
		 * Ignore search errors.  The process may have exited
		 * since we read the directory.
		 */
		if (errno == ESRCH)
			return (0);
		bind_err(PS_QUERY, pid, -1, errno);
		errors = ERR_FAIL;
		return (0);
	}
	if (binding != PS_NONE)
		query_out(pid, -1, binding);
	return (0);
}

static int
query_all_lwp(psinfo_t *psinfo, lwpsinfo_t *lwpsinfo, void *arg)
{
	id_t pid = psinfo->pr_pid;
	id_t lwpid = lwpsinfo->pr_lwpid;
	psetid_t *cpuid = arg;
	psetid_t binding = lwpsinfo->pr_bindpset;

	if (psinfo->pr_nlwp == 1)
		lwpid = -1;	/* report process bindings if only 1 lwp */
	if ((cpuid != NULL && *cpuid == binding) ||
	    (cpuid == NULL && binding != PBIND_NONE))
		query_out(pid, lwpid, binding);
	return (0);
}

void
exec_cmd(psetid_t pset, char **argv)
{
	if (pset_bind(pset, P_PID, P_MYID, NULL) != 0) {
		warn(gettext("cannot exec in processor set %d"), pset);
		return;
	}

	(void) execvp(argv[0], argv);
	warn(gettext("cannot exec command %s"), argv[0]);
}

int
usage(void)
{
	(void) fprintf(stderr, gettext(
	    "usage: \n"
	    "\t%1$s -c [-F] [processor_id ...]\n"
	    "\t%1$s -d processor_set_id ...\n"
	    "\t%1$s -n processor_set_id\n"
	    "\t%1$s -f processor_set_id\n"
	    "\t%1$s -e processor_set_id command [argument(s)...]\n"
	    "\t%1$s -a [-F] processor_set_id processor_id ...\n"
	    "\t%1$s -r [-F] processor_id ...\n"
	    "\t%1$s -p [processorid ...]\n"
	    "\t%1$s -b processor_set_id pid[/lwpids] ...\n"
	    "\t%1$s -u pid[/lwpids] ...\n"
	    "\t%1$s -q [pid[/lwpids] ...]\n"
	    "\t%1$s -U [processor_set_id] ...\n"
	    "\t%1$s -Q [processor_set_id] ...\n"
	    "\t%1$s [-i] [processor_set_id ...]\n"),
	    progname);
	return (ERR_USAGE);
}

/*
 * Query, set, or clear bindings for the range of LWPs in the given process.
 */
static int
do_lwps(id_t pid, const char *range, psetid_t pset)
{
	char procfile[MAX_PROCFS_PATH];
	struct ps_prochandle *Pr;
	struct prheader header;
	struct lwpsinfo *lwp;
	char *lpsinfo, *ptr;
	psetid_t binding;
	int nent, size;
	int i, fd, found;

	/*
	 * Report bindings for LWPs in process 'pid'.
	 */
	(void) snprintf(procfile, MAX_PROCFS_PATH,
	    "/proc/%d/lpsinfo", (int)pid);
	if ((fd = open(procfile, O_RDONLY)) < 0) {
		if (errno == ENOENT)
			errno = ESRCH;
		bind_err(pset, pid, -1, errno);
		return (ERR_FAIL);
	}
	if (pread(fd, &header, sizeof (header), 0) != sizeof (header)) {
		(void) close(fd);
		bind_err(pset, pid, -1, errno);
		return (ERR_FAIL);
	}
	nent = header.pr_nent;
	size = header.pr_entsize * nent;
	ptr = lpsinfo = malloc(size);
	if (lpsinfo == NULL) {
		bind_err(pset, pid, -1, errno);
		return (ERR_FAIL);
	}
	if (pread(fd, lpsinfo, size, sizeof (header)) != size) {
		bind_err(pset, pid, -1, errno);
		free(lpsinfo);
		(void) close(fd);
		return (ERR_FAIL);
	}

	if ((bflag || uflag) && (Pr = grab_proc(pid)) == NULL) {
		free(lpsinfo);
		(void) close(fd);
		return (ERR_FAIL);
	}
	found = 0;
	for (i = 0; i < nent; i++, ptr += header.pr_entsize) {
		/*LINTED ALIGNMENT*/
		lwp = (lwpsinfo_t *)ptr;
		binding = lwp->pr_bindpset;
		if (!proc_lwp_in_set(range, lwp->pr_lwpid))
			continue;
		found++;
		if (bflag || uflag)
			bind_lwp(pid, lwp->pr_lwpid, pset);
		else if (binding != PBIND_NONE)
			query_out(pid, lwp->pr_lwpid, binding);
	}
	if (bflag || uflag)
		rele_proc(Pr);
	free(lpsinfo);
	(void) close(fd);
	if (found == 0) {
		warn(gettext("cannot %s lwpid %d/%s: "
		    "No matching LWPs found\n"),
		    bflag ? "bind" : "query", pid, range);
		return (ERR_FAIL);
	}
	return (ERR_OK);
}

int
main(int argc, char *argv[])
{
	extern int optind;
	int	c;
	id_t	pid;
	processorid_t	cpu;
	psetid_t	pset, old_pset;
	char	*errptr;

	progname = argv[0];	/* put actual command name in messages */

	(void) setlocale(LC_ALL, "");	/* setup localization */
	(void) textdomain(TEXT_DOMAIN);

	while ((c = getopt(argc, argv, "cdFarpibqQuUnfe")) != EOF) {
		switch (c) {
		case 'c':
			cflag = 1;
			break;
		case 'd':
			dflag = 1;
			break;
		case 'e':
			eflag = 1;
			break;
		case 'a':
			aflag = 1;
			break;
		case 'r':
			rflag = 1;
			pset = PS_NONE;
			break;
		case 'p':
			pflag = 1;
			pset = PS_QUERY;
			break;
		case 'i':
			iflag = 1;
			break;
		case 'b':
			bflag = 1;
			break;
		case 'u':
			uflag = 1;
			pset = PS_NONE;
			break;
		case 'U':
			Uflag = 1;
			break;
		case 'q':
			qflag = 1;
			pset = PS_QUERY;
			break;
		case 'Q':
			Qflag = 1;
			break;
		case 'f':
			fflag = 1;
			break;
		case 'F':
			Fflag = 1;
			break;
		case 'n':
			nflag = 1;
			break;
		default:
			return (usage());
		}
	}

	/*
	 * Make sure that at most one of the options was specified.
	 */
	c = cflag + dflag + aflag + rflag + pflag +
	    iflag + bflag + uflag + Uflag +
	    qflag + Qflag + fflag + nflag + eflag;
	if (c < 1) {				/* nothing specified */
		iflag = 1;			/* default is to get info */
	} else if (c > 1) {
		warn(gettext("options are mutually exclusive\n"));
		return (usage());
	}

	if (Fflag && (cflag + aflag + rflag == 0))
		return (usage());

	errors = 0;
	argc -= optind;
	argv += optind;

	if (argc == 0) {
		/*
		 * Handle single option cases.
		 */
		if (qflag) {
			(void) proc_walk(query_all_proc, NULL, PR_WALK_PROC);
			return (errors);
		}
		if (Qflag) {
			(void) proc_walk(query_all_lwp, NULL, PR_WALK_LWP);
			return (errors);
		}
		if (Uflag) {
			if (pset_bind(PS_NONE, P_ALL, 0, &old_pset) != 0)
				die(gettext("failed to unbind all LWPs"));
		}
		if (pflag)
			return (print_all());
		if (iflag)
			return (info_all());
	}

	/*
	 * Get processor set id.
	 */
	if (aflag || bflag || fflag || nflag || eflag) {
		if (argc < 1) {
			/* must specify processor set */
			warn(gettext("must specify processor set\n"));
			return (usage());
		}
		pset = strtol(*argv, &errptr, 10);
		if (errptr != NULL && *errptr != '\0' || pset < 0) {
			warn(gettext("invalid processor set ID %s\n"), *argv);
			return (ERR_FAIL);
		}
		argv++;
		argc--;
	}

	if (cflag) {
		if (pset_create(&pset) != 0) {
			warn(gettext("could not create processor set"));
			return (ERR_FAIL);
		} else {
			create_out(pset);
			if (argc == 0)
				return (ERR_OK);
		}
	} else if (iflag || dflag) {
		if (argc == 0) {
			warn(gettext("must specify at least one "
			    "processor set\n"));
			return (usage());
		}
		/*
		 * Go through listed processor sets.
		 */
		for (; argc > 0; argv++, argc--) {
			pset = (psetid_t)strtol(*argv, &errptr, 10);
			if (errptr != NULL && *errptr != '\0') {
				warn(gettext("invalid processor set ID %s\n"),
				    *argv);
				errors = ERR_FAIL;
				continue;
			}
			if (iflag) {
				errors = do_info(pset);
			} else {
				errors = do_destroy(pset);
			}
		}
	} else if (nflag) {
		errors = do_intr(pset, P_ONLINE);
	} else if (fflag) {
		errors = do_intr(pset, P_NOINTR);
	} else if (eflag) {
		if (argc == 0) {
			warn(gettext("must specify command\n"));
			return (usage());
		}
		exec_cmd(pset, argv);
		/* if returning, must have had an error */
		return (ERR_USAGE);
	}

	if (cflag || aflag || rflag || pflag) {
		/*
		 * Perform function for each processor specified.
		 */
		if (argc == 0) {
			warn(gettext("must specify at least one processor\n"));
			return (usage());
		}

		/*
		 * Go through listed processors.
		 */
		for (; argc > 0; argv++, argc--) {
			if (strchr(*argv, '-') == NULL) {
				/* individual processor id */
				cpu = (processorid_t)strtol(*argv, &errptr, 10);
				if (errptr != NULL && *errptr != '\0') {
					warn(gettext("invalid processor "
					    "ID %s\n"), *argv);
					errors = ERR_FAIL;
					continue;
				}
				if (do_cpu(pset, cpu, pflag, 1))
					errors = ERR_FAIL;
			} else {
				/* range of processors */
				processorid_t first, last;

				first = (processorid_t)
				    strtol(*argv, &errptr, 10);
				if (*errptr++ != '-') {
					warn(gettext(
					    "invalid processor range %s\n"),
					    *argv);
					errors = ERR_USAGE;
					continue;
				}
				last = (processorid_t)
				    strtol(errptr, &errptr, 10);
				if ((errptr != NULL && *errptr != '\0') ||
				    last < first || first < 0) {
					warn(gettext(
					    "invalid processor range %s\n"),
					    *argv);
					errors = ERR_USAGE;
					continue;
				}
				if (do_range(pset, first, last, pflag))
					errors = ERR_FAIL;
			}
		}
	} else if (bflag || uflag || qflag) {
		/*
		 * Perform function for each pid/lwpid specified.
		 */
		if (argc == 0) {
			warn(gettext("must specify at least one pid\n"));
			return (usage());
		}

		/*
		 * Go through listed processes/lwp_ranges.
		 */
		for (; argc > 0; argv++, argc--) {
			pid = (id_t)strtol(*argv, &errptr, 10);
			if (errno != 0 ||
			    (errptr != NULL && *errptr != '\0' &&
			    *errptr != '/')) {
				warn(gettext("invalid process ID: %s\n"),
				    *argv);
				continue;
			}
			if (errptr != NULL && *errptr == '/') {
				int ret;
				/*
				 * Handle lwp range case
				 */
				const char *lwps = (const char *)(++errptr);
				if (*lwps == '\0' ||
				    proc_lwp_range_valid(lwps) != 0) {
					warn(gettext("invalid lwp range "
					    "for pid %d\n"), (int)pid);
					errors = ERR_FAIL;
					continue;
				}
				if (!qflag)
					(void) proc_initstdio();
				ret = do_lwps(pid, lwps, pset);
				if (!qflag)
					(void) proc_finistdio();
				if (ret != ERR_OK)
					errors = ret;
			} else {
				/*
				 * Handle whole process case.
				 */
				if (pset_bind(pset, P_PID, pid,
				    &old_pset) < 0) {
					bind_err(pset, pid, -1, errno);
					errors = ERR_FAIL;
					continue;
				}
				if (qflag)
					query_out(pid, -1, old_pset);
				else
					bind_out(pid, -1, old_pset, pset);
			}
		}
	}

	if (Qflag || Uflag) {
		/*
		 * Go through listed processor set IDs.
		 */
		for (; argc > 0; argv++, argc--) {
			errno = 0;
			pset = (id_t)strtol(*argv, &errptr, 10);
			if (errno != 0 ||
			    (errptr != NULL && *errptr != '\0')) {
				warn(gettext("invalid processor set ID\n"));
				continue;
			}
			if (Qflag) {
				(void) proc_walk(query_all_lwp,
				    &pset, PR_WALK_LWP);
				continue;
			}
			if (Uflag) {
				if (pset_bind(PS_NONE, P_PSETID, pset,
				    &old_pset) != 0) {
					warn(gettext("failed to unbind from "
					    "processor set %d"), (int)pset);
					errors = ERR_FAIL;
				}
				continue;
			}
		}
	}

	return (errors);
}