|
root / base / usr / src / cmd / ptools / plgrp
plgrp Plain Text 1600 lines 34.6 KB
   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
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * The plgrp utility allows a user to display and modify the home lgroup and
 * lgroup affinities of the specified threads
 */

#include <ctype.h>
#include <errno.h>
#include <libintl.h>
#include <libproc.h>
#include <locale.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/lgrp_user.h>


/*
 * Delimiters
 */
#define	DELIMIT_AFF	'/'	/* lgroup affinity from lgroups */
#define	DELIMIT_LGRP	","	/* lgroups from each other */
#define	DELIMIT_LWP	"/"	/* thread/LWP IDs from process ID */
#define	DELIMIT_RANGE	'-'	/* range of IDs (eg. lgroup) */
#define	DELIMIT_AFF_LST ','	/* list of affinities from another list */

/*
 * Exit values other than EXIT_{SUCCESS,FAILURE}
 */
#define	EXIT_NONFATAL 2		/* non-fatal errors */

/*
 * Header and format strings
 */
#define	HDR_PLGRP_AFF_GET	"     PID/LWPID    HOME  AFFINITY\n"
#define	HDR_PLGRP_AFF_SET	"     PID/LWPID    HOME       AFFINITY\n"
#define	HDR_PLGRP_HOME_GET	"     PID/LWPID    HOME\n"
#define	HDR_PLGRP_HOME_SET	"     PID/LWPID    HOME\n"

/*
 * Part of the HDR_PLGRP_AFF_SET header used to calculate space needed to
 * represent changing home as old => new
 */
#define	HDR_PLGRP_HOME_CHANGE	"HOME       "

#define	FMT_AFF			"%d/%s"
#define	FMT_AFF_STR		"%s"
#define	FMT_HOME		"%-6d"
#define	FMT_NEWHOME		"%d => %d"
#define	FMT_THREAD		"%8d/%-8d"

/*
 * How much to allocate for lgroup bitmap array as it grows
 */
#define	LGRP_BITMAP_CHUNK 8

/*
 * Strings that can be given for lgroups
 */
#define	LGRP_ALL_STR		"all"
#define	LGRP_LEAVES_STR		"leaves"
#define	LGRP_ROOT_STR		"root"

/*
 * Strings corresponding to lgroup affinities
 */
#define	LGRP_AFF_NONE_STR	"none"
#define	LGRP_AFF_STRONG_STR	"strong"
#define	LGRP_AFF_WEAK_STR	"weak"

/*
 * Invalid value for lgroup affinity
 */
#define	LGRP_AFF_INVALID	-1

/*
 * Number of args needed for lgroup system call
 */
#define	LGRPSYS_NARGS		3

#ifndef	TEXT_DOMAIN			/* should be defined by cc -D */
#define	TEXT_DOMAIN	"SYS_TEST"	/* use this only if it wasn't */
#endif

/*
 * plgrp(1) operations
 */
typedef enum plgrp_ops {
	PLGRP_AFFINITY_GET,
	PLGRP_AFFINITY_SET,
	PLGRP_HOME_GET,
	PLGRP_HOME_SET,
	PLGRP_NO_OP
} plgrp_ops_t;

/*
 * Arguments specified to plgrp(1) and any state needed to do everything
 * that plgrp(1) does for one operation from inside Plwp_iter_all()
 */
typedef struct plgrp_args {
	struct ps_prochandle	*Ph;		/* proc handle for process */
	const char		*lwps;		/* LWPs */
	lgrp_id_t		*lgrps;		/* lgroups */
	lgrp_affinity_t		*affs;		/* lgroup affinities */
	int			nlgrps;		/* number of lgroups */
	int			nelements;	/* number of elements */
	int			index;		/* index */
	int			nthreads;	/* threads processed */
	plgrp_ops_t		op;		/* operation */
} plgrp_args_t;

/*
 * How many signals caught from terminal
 * We bail out as soon as possible when interrupt is set
 */
static int	interrupt = 0;

/*
 * How many non-fatal errors ocurred
 */
static int	nerrors = 0;

/*
 * Name of this program
 */
static char	*progname;

/*
 * Root of the lgroup hierarchy
 */
static lgrp_id_t root = LGRP_NONE;

/*
 * Bitmap of all lgroups in the system
 */
static char *lgrps_bitmap = NULL;

/*
 * Size of lgrps_bitmap array
 */
static int lgrps_bitmap_nelements = 0;

/*
 * Macro LGRP_VALID returns true when lgrp is present in the system.
 */
#define	LGRP_VALID(lgrp) (lgrps_bitmap[lgrp] != 0)


/*
 * Maximum lgroup value.
 */
static int max_lgrpid = LGRP_NONE;

/*
 * Total possible number of lgroups
 */
#define	NLGRPS (max_lgrpid + 1)


static void
usage(int rc)
{
	(void) fprintf(stderr,
	    gettext("Usage:\t%s [-h] <pid> | <core> [/lwps] ...\n"), progname);
	(void) fprintf(stderr,
	    gettext("\t%s [-F] -a <lgroup list> <pid>[/lwps] ...\n"), progname);
	(void) fprintf(stderr,
	    gettext("\t%s [-F] -A <lgroup list>/none|weak|strong[,...] "
	    " <pid>[/lwps] ...\n"), progname);
	(void) fprintf(stderr,
	    gettext("\t%s [-F] -H <lgroup list> <pid>[/lwps] ...\n"), progname);
	(void) fprintf(stderr,
	    gettext("\n\twhere <lgroup list> is a comma separated list of\n"
		"\tone or more of the following:\n\n"
		"\t  - lgroup ID\n"
		"\t  - Range of lgroup IDs specified as\n"
		"\t\t<start lgroup ID>-<end lgroup ID>\n"
		"\t  - \"all\"\n"
		"\t  - \"root\"\n"
		"\t  - \"leaves\"\n\n"));

	exit(rc);
}

/*
 * Handler for catching signals from terminal
 */
/* ARGSUSED */
static void
intr(int sig)
{
	interrupt++;
}


/*
 * Return string name for given lgroup affinity
 */
static char *
lgrp_affinity_string(lgrp_affinity_t aff)
{
	char *rc = "unknown";

	switch (aff) {
	case LGRP_AFF_STRONG:
		rc = "strong";
		break;
	case LGRP_AFF_WEAK:
		rc = "weak";
		break;
	case LGRP_AFF_NONE:
		rc = "none";
		break;
	default:
		break;
	}

	return (rc);
}


/*
 * Add a new lgroup into lgroup array in "arg", growing lgroup and affinity
 * arrays if necessary
 */
static void
lgrps_add_lgrp(plgrp_args_t *arg, int id)
{

	if (arg->nlgrps == arg->nelements) {
		arg->nelements += LGRP_BITMAP_CHUNK;

		arg->lgrps = realloc(arg->lgrps,
		    arg->nelements * sizeof (lgrp_id_t));
		if (arg->lgrps == NULL) {
			(void) fprintf(stderr, gettext("%s: out of memory\n"),
			    progname);
			exit(EXIT_FAILURE);
		}

		arg->affs = realloc(arg->affs,
		    arg->nelements * sizeof (lgrp_affinity_t));

		if (arg->affs == NULL) {
			(void) fprintf(stderr, gettext("%s: out of memory\n"),
			    progname);
			exit(EXIT_FAILURE);
		}
	}

	arg->lgrps[arg->nlgrps] = id;
	arg->affs[arg->nlgrps] = LGRP_AFF_INVALID;
	arg->nlgrps++;
}


/*
 * Return an array having '1' for each lgroup present in given subtree under
 * specified lgroup in lgroup hierarchy
 */
static void
lgrps_bitmap_init(lgrp_cookie_t cookie, lgrp_id_t lgrpid, char **bitmap_array,
	int *bitmap_nelements)
{
	lgrp_id_t	*children;
	int		i;
	int		nchildren;

	if (lgrpid < 0) {
		lgrpid = lgrp_root(cookie);
		if (lgrpid < 0)
			return;
	}

	/*
	 * If new lgroup cannot fit, grow the array and fill unused portion
	 * with zeroes.
	 */
	while (lgrpid >= *bitmap_nelements) {
		*bitmap_nelements += LGRP_BITMAP_CHUNK;
		*bitmap_array = realloc(*bitmap_array,
		    *bitmap_nelements * sizeof (char));
		if (*bitmap_array == NULL) {
			(void) fprintf(stderr, gettext("%s: out of memory\n"),
			    progname);
			exit(EXIT_FAILURE);
		}
		bzero(*bitmap_array + NLGRPS,
		    (*bitmap_nelements - NLGRPS) * sizeof (char));
	}

	/*
	 * Insert lgroup into bitmap and update max lgroup ID seen so far
	 */
	(*bitmap_array)[lgrpid] = 1;
	if (lgrpid > max_lgrpid)
		max_lgrpid = lgrpid;

	/*
	 * Get children of specified lgroup and insert descendants of each
	 * of them
	 */
	nchildren = lgrp_children(cookie, lgrpid, NULL, 0);
	if (nchildren > 0) {
		children = malloc(nchildren * sizeof (lgrp_id_t));
		if (children == NULL) {
			(void) fprintf(stderr, gettext("%s: out of memory\n"),
			    progname);
			exit(EXIT_FAILURE);
		}
		if (lgrp_children(cookie, lgrpid, children, nchildren) !=
		    nchildren) {
			free(children);
			return;
		}

		for (i = 0; i < nchildren; i++)
			lgrps_bitmap_init(cookie, children[i], bitmap_array,
			    bitmap_nelements);

		free(children);
	}
}


/*
 * Parse lgroup affinity from given string
 *
 * Return lgroup affinity or LGRP_AFF_INVALID if string doesn't match any
 * existing lgroup affinity and return pointer to position just after affinity
 * string.
 */
static lgrp_affinity_t
parse_lgrp_affinity(char *string, char  **next)
{
	int rc = LGRP_AFF_INVALID;

	if (string == NULL)
		return (LGRP_AFF_INVALID);

	/*
	 * Skip delimiter
	 */
	if (string[0] == DELIMIT_AFF)
		string++;

	/*
	 * Return lgroup affinity matching string
	 */
	if (strncmp(string, LGRP_AFF_NONE_STR, strlen(LGRP_AFF_NONE_STR))
	    == 0) {
		rc = LGRP_AFF_NONE;
		*next = string + strlen(LGRP_AFF_NONE_STR);
	} else if (strncmp(string,
			LGRP_AFF_WEAK_STR, strlen(LGRP_AFF_WEAK_STR)) == 0) {
		rc = LGRP_AFF_WEAK;
		*next = string + strlen(LGRP_AFF_WEAK_STR);
	} else if (strncmp(string, LGRP_AFF_STRONG_STR,
			strlen(LGRP_AFF_STRONG_STR)) == 0) {
		rc = LGRP_AFF_STRONG;
		*next = string + strlen(LGRP_AFF_STRONG_STR);
	}

	return (rc);
}


/*
 * Parse lgroups from given string
 * Returns the set containing all lgroups parsed or NULL.
 */
static int
parse_lgrps(lgrp_cookie_t cookie, plgrp_args_t *arg, char *s)
{
	lgrp_id_t	i;
	char		*token;

	if (cookie == LGRP_COOKIE_NONE || s == NULL || NLGRPS <= 0)
		return (0);

	/*
	 * Parse first lgroup (if any)
	 */
	token = strtok(s, DELIMIT_LGRP);
	if (token == NULL)
		return (-1);

	do {
		/*
		 * Parse lgroups
		 */
		if (isdigit(*token)) {
			lgrp_id_t	first;
			lgrp_id_t	last;
			char		*p;

			/*
			 * lgroup ID(s)
			 *
			 * Can be <lgroup ID>[-<lgroup ID>]
			 */
			p = strchr(token, DELIMIT_RANGE);
			first = atoi(token);
			if (p == NULL)
				last = first;
			else
				last = atoi(++p);

			for (i = first; i <= last; i++) {
				/*
				 * Add valid lgroups to lgroup array
				 */
				if ((i >= 0) && (i < NLGRPS) && LGRP_VALID(i))
					lgrps_add_lgrp(arg, i);
				else  {
					(void) fprintf(stderr,
					    gettext("%s: bad lgroup %d\n"),
					    progname, i);
					nerrors++;
				}
			}
		} else if (strncmp(token, LGRP_ALL_STR,
				strlen(LGRP_ALL_STR)) == 0) {
			/*
			 * Add "all" lgroups to lgroups array
			 */
			for (i = 0; i < NLGRPS; i++) {
				if (LGRP_VALID(i))
					lgrps_add_lgrp(arg, i);
			}
		} else if (strncmp(token, LGRP_ROOT_STR,
				strlen(LGRP_ROOT_STR)) == 0) {
			if (root < 0)
				root = lgrp_root(cookie);
			lgrps_add_lgrp(arg, root);
		} else if (strncmp(token, LGRP_LEAVES_STR,
		    strlen(LGRP_LEAVES_STR)) == 0) {
			/*
			 * Add leaf lgroups to lgroups array
			 */
			for (i = 0; i < NLGRPS; i++) {
				if (LGRP_VALID(i) &&
				    lgrp_children(cookie, i, NULL, 0) == 0)
					lgrps_add_lgrp(arg, i);
			}
		} else {
			return (-1);
		}
	} while (token = strtok(NULL, DELIMIT_LGRP));

	return (0);
}

/*
 * Print array of lgroup IDs, collapsing any consecutive runs of IDs into a
 * range (eg. 2,3,4 into 2-4)
 */
static void
print_lgrps(lgrp_id_t *lgrps, int nlgrps)
{
	lgrp_id_t	start;
	lgrp_id_t	end;
	int		i;

	/*
	 * Initial range consists of the first element
	 */
	start = end = lgrps[0];

	for (i = 1; i < nlgrps; i++) {
		lgrp_id_t	lgrpid;

		lgrpid = lgrps[i];
		if (lgrpid == end + 1) {
			/*
			 * Got consecutive lgroup ID, so extend end of range
			 * without printing anything since the range may extend
			 * further
			 */
			end = lgrpid;
		} else {
			/*
			 * Next lgroup ID is not consecutive, so print lgroup
			 * IDs gotten so far.
			 */
			if (end == start) {		/* same value */
				(void) printf("%d,", (int)start);
			} else if (end > start + 1) {	/* range */
				(void) printf("%d-%d,", (int)start, (int)end);
			} else {			/* different values */
				(void) printf("%d,%d,", (int)start, (int)end);
			}

			/*
			 * Try finding consecutive range starting from this
			 * lgroup ID
			 */
			start = end = lgrpid;
		}
	}

	/*
	 * Print last lgroup ID(s)
	 */
	if (end == start) {
		(void) printf("%d", (int)start);
	} else if (end > start + 1) {
		(void) printf("%d-%d", (int)start, (int)end);
	} else {
		(void) printf("%d,%d", (int)start, (int)end);
	}
}

/*
 * Print lgroup affinities given array of lgroups, corresponding array of
 * affinities, and number of elements.
 * Skip any lgroups set to LGRP_NONE or having invalid affinity.
 */
static void
print_affinities(lgrp_id_t *lgrps, lgrp_affinity_t *affs, int nelements)
{
	int		i;
	lgrp_id_t	*lgrps_none;
	lgrp_id_t	*lgrps_strong;
	lgrp_id_t	*lgrps_weak;
	int		nlgrps_none;
	int		nlgrps_strong;
	int		nlgrps_weak;

	nlgrps_strong = nlgrps_weak = nlgrps_none = 0;

	lgrps_strong = malloc(nelements * sizeof (lgrp_id_t));
	lgrps_weak = malloc(nelements * sizeof (lgrp_id_t));
	lgrps_none = malloc(nelements * sizeof (lgrp_id_t));

	if (lgrps_strong == NULL || lgrps_weak == NULL || lgrps_none == NULL) {
		(void) fprintf(stderr, gettext("%s: out of memory\n"),
		    progname);
		interrupt = 1;
		return;
	}

	/*
	 * Group lgroups by affinity
	 */
	for (i = 0; i < nelements; i++) {
		lgrp_id_t lgrpid = lgrps[i];

		/*
		 * Skip any lgroups set to LGRP_NONE
		 */
		if (lgrpid == LGRP_NONE)
			continue;

		switch (affs[i]) {
		case LGRP_AFF_STRONG:
			lgrps_strong[nlgrps_strong++] = lgrpid;
			break;
		case LGRP_AFF_WEAK:
			lgrps_weak[nlgrps_weak++] = lgrpid;
			break;
		case LGRP_AFF_NONE:
			lgrps_none[nlgrps_none++] = lgrpid;
			break;
		default:
			/*
			 * Skip any lgroups with invalid affinity.
			 */
			break;
		}
	}

	/*
	 * Print all lgroups with same affinity together
	 */
	if (nlgrps_strong) {
		print_lgrps(lgrps_strong, nlgrps_strong);
		(void) printf("/%s", lgrp_affinity_string(LGRP_AFF_STRONG));
		if (nlgrps_weak || nlgrps_none)
			(void) printf("%c", DELIMIT_AFF_LST);
	}

	if (nlgrps_weak) {
		print_lgrps(lgrps_weak, nlgrps_weak);
		(void) printf("/%s", lgrp_affinity_string(LGRP_AFF_WEAK));
		if (nlgrps_none)
			(void) printf("%c", DELIMIT_AFF_LST);
	}

	if (nlgrps_none) {
		print_lgrps(lgrps_none, nlgrps_none);
		(void) printf("/%s", lgrp_affinity_string(LGRP_AFF_NONE));
	}

	free(lgrps_strong);
	free(lgrps_weak);
	free(lgrps_none);
}


/*
 * Print heading for specified operation
 */
static void
print_heading(plgrp_ops_t op)
{

	switch (op) {
	case PLGRP_AFFINITY_GET:
		(void) printf(HDR_PLGRP_AFF_GET);
		break;

	case PLGRP_AFFINITY_SET:
		(void) printf(HDR_PLGRP_AFF_SET);
		break;

	case PLGRP_HOME_GET:
		(void) printf(HDR_PLGRP_HOME_GET);
		break;

	case PLGRP_HOME_SET:
		(void) printf(HDR_PLGRP_HOME_SET);
		break;

	default:
		break;
	}
}

/*
 * Use /proc to call lgrp_affinity_get() in another process
 */
static lgrp_affinity_t
Plgrp_affinity_get(struct ps_prochandle *Ph, idtype_t idtype, id_t id,
    lgrp_id_t lgrp)
{
	lgrp_affinity_args_t	args;
	argdes_t		Pargd[3];
	argdes_t		*Pargdp;
	int			Pnargs;
	int			Pretval;
	sysret_t		retval;
	int			syscall;

	/*
	 * Fill in arguments needed for syscall(SYS_lgrpsys,
	 * LGRP_SYS_AFFINITY_GET, 0, &args)
	 */
	syscall = SYS_lgrpsys;

	args.idtype = idtype;
	args.id = id;
	args.lgrp = lgrp;
	args.aff = LGRP_AFF_INVALID;

	/*
	 * Fill out /proc argument descriptors for syscall(SYS_lgrpsys,
	 * LGRP_SYS_AFFINITY_GET, idtype, id)
	 */
	Pnargs = LGRPSYS_NARGS;
	Pargdp = &Pargd[0];
	Pargdp->arg_value = LGRP_SYS_AFFINITY_GET;
	Pargdp->arg_object = NULL;
	Pargdp->arg_type = AT_BYVAL;
	Pargdp->arg_inout = AI_INPUT;
	Pargdp->arg_size = 0;
	Pargdp++;

	Pargdp->arg_value = 0;
	Pargdp->arg_object = NULL;
	Pargdp->arg_type = AT_BYVAL;
	Pargdp->arg_inout = AI_INPUT;
	Pargdp->arg_size = 0;
	Pargdp++;

	Pargdp->arg_value = 0;
	Pargdp->arg_object = &args;
	Pargdp->arg_type = AT_BYREF;
	Pargdp->arg_inout = AI_INPUT;
	Pargdp->arg_size = sizeof (lgrp_affinity_args_t);
	Pargdp++;

	/*
	 * Have agent LWP call syscall with appropriate arguments in target
	 * process
	 */
	Pretval = Psyscall(Ph, &retval, syscall, Pnargs, &Pargd[0]);
	if (Pretval) {
		errno = (Pretval < 0) ? ENOSYS : Pretval;
		return (LGRP_AFF_INVALID);
	}

	return (retval.sys_rval1);
}


/*
 * Use /proc to call lgrp_affinity_set() in another process
 */
static int
Plgrp_affinity_set(struct ps_prochandle *Ph, idtype_t idtype, id_t id,
    lgrp_id_t lgrp, lgrp_affinity_t aff)
{
	lgrp_affinity_args_t	args;
	argdes_t		Pargd[3];
	argdes_t		*Pargdp;
	int			Pnargs;
	int			Pretval;
	sysret_t		retval;
	int			syscall;

	/*
	 * Fill in arguments needed for syscall(SYS_lgrpsys,
	 * LGRP_SYS_AFFINITY_SET, 0, &args)
	 */
	syscall = SYS_lgrpsys;

	args.idtype = idtype;
	args.id = id;
	args.lgrp = lgrp;
	args.aff = aff;

	/*
	 * Fill out /proc argument descriptors for syscall(SYS_lgrpsys,
	 * LGRP_SYS_AFFINITY_SET, idtype, id)
	 */
	Pnargs = LGRPSYS_NARGS;
	Pargdp = &Pargd[0];
	Pargdp->arg_value = LGRP_SYS_AFFINITY_SET;
	Pargdp->arg_object = NULL;
	Pargdp->arg_type = AT_BYVAL;
	Pargdp->arg_inout = AI_INPUT;
	Pargdp->arg_size = 0;
	Pargdp++;

	Pargdp->arg_value = 0;
	Pargdp->arg_object = NULL;
	Pargdp->arg_type = AT_BYVAL;
	Pargdp->arg_inout = AI_INPUT;
	Pargdp->arg_size = 0;
	Pargdp++;

	Pargdp->arg_value = 0;
	Pargdp->arg_object = &args;
	Pargdp->arg_type = AT_BYREF;
	Pargdp->arg_inout = AI_INPUT;
	Pargdp->arg_size = sizeof (lgrp_affinity_args_t);
	Pargdp++;

	/*
	 * Have agent LWP call syscall with appropriate arguments in
	 * target process
	 */
	Pretval = Psyscall(Ph, &retval, syscall, Pnargs, &Pargd[0]);
	if (Pretval) {
		errno = (Pretval < 0) ? ENOSYS : Pretval;
		return (-1);
	}

	return (retval.sys_rval1);
}

/*
 * Use /proc to call lgrp_home() in another process
 */
static lgrp_id_t
Plgrp_home(struct ps_prochandle *Ph, idtype_t idtype, id_t id)
{
	argdes_t		Pargd[3];
	argdes_t		*Pargdp;
	int			Pnargs;
	int			Pretval;
	sysret_t		retval;
	int			syscall;

	/*
	 * Fill in arguments needed for syscall(SYS_lgrpsys,
	 * LGRP_SYS_HOME, idtype, id)
	 */
	syscall = SYS_lgrpsys;

	/*
	 * Fill out /proc argument descriptors for syscall(SYS_lgrpsys,
	 * LGRP_SYS_HOME, idtype, id)
	 */
	Pnargs = LGRPSYS_NARGS;
	Pargdp = &Pargd[0];
	Pargdp->arg_value = LGRP_SYS_HOME;
	Pargdp->arg_object = NULL;
	Pargdp->arg_type = AT_BYVAL;
	Pargdp->arg_inout = AI_INPUT;
	Pargdp->arg_size = 0;
	Pargdp++;

	Pargdp->arg_value = idtype;
	Pargdp->arg_object = NULL;
	Pargdp->arg_type = AT_BYVAL;
	Pargdp->arg_inout = AI_INPUT;
	Pargdp->arg_size = 0;
	Pargdp++;

	Pargdp->arg_value = id;
	Pargdp->arg_object = NULL;
	Pargdp->arg_type = AT_BYVAL;
	Pargdp->arg_inout = AI_INPUT;
	Pargdp->arg_size = 0;
	Pargdp++;

	/*
	 * Have agent LWP call syscall with appropriate arguments in
	 * target process
	 */
	Pretval = Psyscall(Ph, &retval, syscall, Pnargs, &Pargd[0]);
	if (Pretval) {
		errno = (Pretval < 0) ? ENOSYS : Pretval;
		return (-1);
	}

	return (retval.sys_rval1);
}

/*
 * Use /proc to call lgrp_affinity_set(3LGRP) to set home lgroup of given
 * thread
 */
static int
Plgrp_home_set(struct ps_prochandle *Ph, idtype_t idtype, id_t id,
    lgrp_id_t lgrp)
{
	return (Plgrp_affinity_set(Ph, idtype, id, lgrp,
	    LGRP_AFF_STRONG));
}


/*
 * Do plgrp(1) operation on specified thread
 */
static int
do_op(plgrp_args_t *plgrp_args, id_t pid, id_t lwpid,
    const lwpsinfo_t *lwpsinfo)
{
	lgrp_affinity_t		*affs;
	lgrp_affinity_t		*cur_affs;
	lgrp_id_t		home;
	int			i;
	lgrp_affinity_t		*init_affs;
	lgrp_id_t		*lgrps;
	lgrp_id_t		*lgrps_changed;
	int			nlgrps;
	lgrp_id_t		old_home;
	lgrp_id_t		lgrpid;
	struct ps_prochandle	*Ph;
	int			nchanged;

	/*
	 * No args, so nothing to do.
	 */
	if (plgrp_args == NULL)
		return (0);

	/*
	 * Unpack plgrp(1) arguments and state needed to process this LWP
	 */
	Ph = plgrp_args->Ph;
	lgrps = plgrp_args->lgrps;
	affs = plgrp_args->affs;
	nlgrps = plgrp_args->nlgrps;

	switch (plgrp_args->op) {

	case PLGRP_HOME_GET:
		/*
		 * Get and display home lgroup for given LWP
		 */
		home = lwpsinfo->pr_lgrp;
		(void) printf(FMT_HOME"\n", (int)home);
		break;

	case PLGRP_AFFINITY_GET:
		/*
		 * Get and display this LWP's home lgroup and affinities
		 * for specified lgroups
		 */
		home = lwpsinfo->pr_lgrp;
		(void) printf(FMT_HOME, (int)home);

		/*
		 * Collect affinity values
		 */
		for (i = 0; i < nlgrps; i++) {
			affs[i] = Plgrp_affinity_get(Ph, P_LWPID, lwpid,
			    lgrps[i]);

			if (affs[i] == LGRP_AFF_INVALID) {
				nerrors++;
				(void) fprintf(stderr,
				    gettext("%s: cannot get affinity"
					" for lgroup %d for %d/%d: %s\n"),
				    progname, lgrps[i], pid, lwpid,
				    strerror(errno));
			}
		}

		/*
		 * Print affinities for each type.
		 */
		print_affinities(lgrps, affs, nlgrps);
		(void) printf("\n");

		break;

	case PLGRP_HOME_SET:
		/*
		 * Get home lgroup before and after setting it and display
		 * change.  If more than one lgroup and one LWP are specified,
		 * then home LWPs to lgroups in round robin fashion.
		 */
		old_home = lwpsinfo->pr_lgrp;

		i = plgrp_args->index;
		if (Plgrp_home_set(Ph, P_LWPID, lwpid, lgrps[i]) != 0) {
			nerrors++;
			(void) fprintf(stderr,
			    gettext("%s: cannot set home lgroup of %d/%d"
				" to lgroup %d: %s\n"),
				progname, pid, lwpid, lgrps[i],
			    strerror(errno));
			(void) printf("\n");
		} else {
			int len;
			int width = strlen(HDR_PLGRP_HOME_CHANGE);

			home = Plgrp_home(Ph, P_LWPID, lwpid);

			if (home < 0) {
				(void) fprintf(stderr,
				    gettext("%s cannot get home lgroup for"
					" %d/%d: %s\n"),
				    progname, pid, lwpid, strerror(errno));
				nerrors++;
			}

			len = printf(FMT_NEWHOME, (int)old_home, (int)home);
			if (len < width)
				(void) printf("%*c\n", (int)(width - len), ' ');
		}

		plgrp_args->index = (i + 1) % nlgrps;

		break;

	case PLGRP_AFFINITY_SET:
		/*
		 * Set affinities for specified lgroups and print old and new
		 * affinities and any resulting change in home lgroups
		 */

		/*
		 * Get initial home lgroup as it may change.
		 */
		old_home = lwpsinfo->pr_lgrp;

		/*
		 * Need to allocate arrays indexed by lgroup (ID) for
		 * affinities and lgroups because user may specify affinity
		 * for same lgroup multiple times....
		 *
		 * Keeping these arrays by lgroup (ID) eliminates any
		 * duplication and makes it easier to just print initial and
		 * final lgroup affinities (instead of trying to keep a list
		 * of lgroups specified which may include duplicates)
		 */
		init_affs = malloc(NLGRPS * sizeof (lgrp_affinity_t));
		cur_affs = malloc(NLGRPS * sizeof (lgrp_affinity_t));
		lgrps_changed = malloc(NLGRPS * sizeof (lgrp_id_t));

		if (init_affs == NULL || cur_affs == NULL ||
		    lgrps_changed == NULL) {
			(void) fprintf(stderr, gettext("%s: out of memory\n"),
			    progname);
			Prelease(Ph, PRELEASE_RETAIN);
			if (init_affs != NULL)
				free(init_affs);
			if (cur_affs != NULL)
				free(cur_affs);
			nerrors++;
			return (EXIT_NONFATAL);
		}

		/*
		 * Initialize current and initial lgroup affinities and
		 * lgroups changed
		 */
		for (lgrpid = 0; lgrpid < NLGRPS; lgrpid++) {

			if (!LGRP_VALID(lgrpid)) {
				init_affs[lgrpid] = LGRP_AFF_INVALID;
			} else {
				init_affs[lgrpid] =
				    Plgrp_affinity_get(Ph, P_LWPID,
					lwpid, lgrpid);

				if (init_affs[lgrpid] == LGRP_AFF_INVALID) {
					nerrors++;
					(void) fprintf(stderr,
					    gettext("%s: cannot get"
						" affinity for lgroup %d"
						" for %d/%d: %s\n"),
					    progname, lgrpid, pid, lwpid,
					    strerror(errno));
				}
			}

			cur_affs[lgrpid] = init_affs[lgrpid];
			lgrps_changed[lgrpid] = LGRP_NONE;
		}

		/*
		 * Change affinities.
		 */
		for (i = 0; i < nlgrps; i++) {
			lgrp_affinity_t	aff = affs[i];

			lgrpid = lgrps[i];

			/*
			 * If the suggested affinity is the same as the current
			 * one, skip this lgroup.
			 */
			if (aff == cur_affs[lgrpid])
				continue;

			/*
			 * Set affinity to the new value
			 */
			if (Plgrp_affinity_set(Ph, P_LWPID, lwpid, lgrpid,
				aff) < 0) {
				nerrors++;
				(void) fprintf(stderr,
				    gettext("%s: cannot set"
					" %s affinity for lgroup %d"
					" for %d/%d: %s\n"),
				    progname, lgrp_affinity_string(aff),
				    lgrpid, pid, lwpid,
				    strerror(errno));
				continue;
			}

			/*
			 * Get the new value and verify that it changed as
			 * expected.
			 */
			cur_affs[lgrpid] =
			    Plgrp_affinity_get(Ph, P_LWPID, lwpid, lgrpid);

			if (cur_affs[lgrpid] == LGRP_AFF_INVALID) {
				nerrors++;
				(void) fprintf(stderr,
				    gettext("%s: cannot get"
					" affinity for lgroup %d"
					" for %d/%d: %s\n"),
				    progname, lgrpid, pid, lwpid,
				    strerror(errno));
				continue;
			}

			if (aff != cur_affs[lgrpid]) {
				(void) fprintf(stderr,
				    gettext("%s: affinity for"
					" lgroup %d is set to %d instead of %d"
					" for %d/%d\n"),
				    progname, lgrpid, cur_affs[lgrpid], aff,
				    pid, lwpid);
				nerrors++;
			}
		}

		/*
		 * Compare current and initial affinities and mark lgroups with
		 * changed affinities.
		 */
		nchanged = 0;
		for (lgrpid = 0; lgrpid < NLGRPS; lgrpid++) {
			if (init_affs[lgrpid] != cur_affs[lgrpid]) {
				lgrps_changed[lgrpid] = lgrpid;
				nchanged++;
			}
		}

		if (nchanged == 0) {
			/*
			 * Nothing changed, so just print current affinities for
			 * specified lgroups.
			 */
			for (i = 0; i < nlgrps; i++) {
				lgrps_changed[lgrps[i]] = lgrps[i];
			}

			(void) printf("%-*d",
			    (int)strlen(HDR_PLGRP_HOME_CHANGE),
			    (int)old_home);

			print_affinities(lgrps_changed, cur_affs, NLGRPS);
			(void) printf("\n");
		} else {
			int width = strlen(HDR_PLGRP_HOME_CHANGE);

			/*
			 * Some lgroup affinities changed, so display old
			 * and new home lgroups for thread and its old and new
			 * affinities for affected lgroups
			 */
			home = Plgrp_home(Ph, P_LWPID, lwpid);
			if (home < 0) {
				(void) fprintf(stderr,
				    gettext("%s: cannot get home"
					" for %d/%d: %s\n"),
				    progname, pid, lwpid, strerror(errno));
				nerrors++;
			}
			if (old_home != home) {
				int len;

				/*
				 * Fit string into fixed width
				 */
				len = printf(FMT_NEWHOME,
				    (int)old_home, (int)home);
				if (len < width)
					(void) printf("%*c", width - len, ' ');
			} else {
				(void) printf("%-*d", width, (int)home);
			}

			/*
			 * Print change in affinities from old to new
			 */
			print_affinities(lgrps_changed, init_affs, NLGRPS);
			(void) printf(" => ");
			print_affinities(lgrps_changed, cur_affs, NLGRPS);
			(void) printf("\n");
		}

		free(lgrps_changed);
		free(init_affs);
		free(cur_affs);

		break;

	default:
		break;
	}

	return (0);
}


/*
 * Routine called by Plwp_iter_all() as it iterates through LWPs of another
 * process
 */
/* ARGSUSED */
static int
Plwp_iter_handler(void *arg, const lwpstatus_t *lwpstatus,
    const lwpsinfo_t *lwpsinfo)
{
	id_t			lwpid;
	struct ps_prochandle	*Ph;
	const pstatus_t		*pstatus;
	plgrp_args_t		*plgrp_args;

	/*
	 * Nothing to do if no arguments
	 */
	if (arg == NULL || interrupt)
		return (0);

	/*
	 * Unpack plgrp(1) arguments and state needed to process this LWP
	 */
	plgrp_args = arg;
	Ph = plgrp_args->Ph;

	/*
	 * Just return if no /proc handle for process
	 */
	if (Ph == NULL)
		return (0);

	pstatus = Pstatus(Ph);

	/*
	 * Skip agent LWP and any LWPs that weren't specified
	 */
	lwpid = lwpsinfo->pr_lwpid;
	if (lwpid == pstatus->pr_agentid ||
	    !proc_lwp_in_set(plgrp_args->lwps, lwpid))
		return (0);

	plgrp_args->nthreads++;

	/*
	 * Do all plgrp(1) operations specified on given thread
	 */
	(void) printf(FMT_THREAD" ", (int)pstatus->pr_pid, (int)lwpid);
	return (do_op(plgrp_args, pstatus->pr_pid, lwpid, lwpsinfo));
}

/*
 * Get target process specified in "pidstring" argument to do operation(s)
 * specified in "plgrp_todo" using /proc and agent LWP
 */
static void
do_process(char *pidstring, plgrp_args_t *plgrp_todo, int force)
{
	int			error;
	const char		*lwps;
	struct ps_prochandle	*Ph;

	/*
	 * Nothing to do, so return.
	 */
	if (plgrp_todo == NULL || interrupt)
		return;

	/*
	 * Grab target process or core and return
	 * /proc handle for process and string of LWP
	 * IDs
	 */
	Ph = proc_arg_xgrab(pidstring, NULL,
	    PR_ARG_ANY, force | PGRAB_RETAIN | PGRAB_NOSTOP, &error, &lwps);
	if (Ph == NULL) {
		(void) fprintf(stderr,
		    gettext("%s: Unable to grab process %s: %s\n"),
		    progname, pidstring, Pgrab_error(error));
		nerrors++;
		return;
	}

	/*
	 * Fill in remaining plgrp(1) arguments and state needed to do
	 * plgrp(1) operation(s) on desired LWPs in our handler
	 * called by Plwp_iter_all() as it iterates over LWPs
	 * in given process
	 */
	plgrp_todo->Ph = Ph;
	plgrp_todo->lwps = lwps;

	/*
	 * Iterate over LWPs in process and do specified
	 * operation(s) on those specified
	 */
	if (Plwp_iter_all(Ph, Plwp_iter_handler, plgrp_todo) != 0) {
		(void) fprintf(stderr,
		    gettext("%s: error iterating over threads\n"),
		    progname);
		nerrors++;
	}

	Prelease(Ph, PRELEASE_RETAIN);
}


/*
 * Parse command line and kick off any resulting actions
 *
 * plgrp(1) has the following command line syntax:
 *
 *	plgrp [-h] <pid> | <core> [/lwps] ...
 *	plgrp [-F] -a <lgroup>,... <pid>[/lwps] ...
 *	plgrp [-F] -H <lgroup>,... <pid>[/lwps] ...
 *	plgrp [-F] -A <lgroup>,... [/none|weak|strong] ... <pid>[/lwps] ...
 *
 *	where <lgroup> is an lgroup ID, "all", "root", "leaves".
 */
int
main(int argc, char *argv[])
{
	lgrp_affinity_t		aff;
	char			*affstring;
	int			c;
	lgrp_cookie_t		cookie;
	int			Fflag;
	int			i;
	int			opt_seen;
	plgrp_args_t		plgrp_todo;
	char			*s;

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	opt_seen = 0;

	/*
	 * Get name of program
	 */
	progname = basename(argv[0]);

	/*
	 * Not much to do when only name of program given
	 */
	if (argc == 1)
		usage(0);

	/*
	 * Catch signals from terminal, so they can be handled asynchronously
	 * when we're ready instead of when we're not (;-)
	 */
	if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
		(void) sigset(SIGHUP, intr);
	if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
		(void) sigset(SIGINT, intr);
	if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
		(void) sigset(SIGQUIT, intr);
	(void) sigset(SIGPIPE, intr);
	(void) sigset(SIGTERM, intr);

	/*
	 * Take snapshot of lgroup hierarchy
	 */
	cookie = lgrp_init(LGRP_VIEW_OS);
	if (cookie == LGRP_COOKIE_NONE) {
		(void) fprintf(stderr,
		    gettext("%s: Fatal error: cannot get lgroup"
			" information from the OS: %s\n"),
		    progname, strerror(errno));
		return (EXIT_FAILURE);
	}

	root = lgrp_root(cookie);
	lgrps_bitmap_init(cookie, root, &lgrps_bitmap, &lgrps_bitmap_nelements);

	/*
	 * Remember arguments and state needed to do plgrp(1) operation
	 * on desired LWPs
	 */
	bzero(&plgrp_todo, sizeof (plgrp_args_t));
	plgrp_todo.op = PLGRP_HOME_GET;

	/*
	 * Parse options
	 */
	opterr = 0;
	Fflag = 0;
	while (!interrupt && (c = getopt(argc, argv, "a:A:FhH:")) != -1) {
		/*
		 * Parse option and only allow one option besides -F to be
		 * specified
		 */
		switch (c) {

		case 'h':	/* Get home lgroup */
			/*
			 * Only allow one option (besides -F) to be specified
			 */
			if (opt_seen)
				usage(EXIT_FAILURE);
			opt_seen = 1;

			plgrp_todo.op = PLGRP_HOME_GET;
			break;

		case 'H':	/* Set home lgroup */

			/*
			 * Fail if already specified option (besides -F)
			 * or no more arguments
			 */
			if (opt_seen || optind >= argc) {
				usage(EXIT_FAILURE);
			}
			opt_seen = 1;

			plgrp_todo.op = PLGRP_HOME_SET;

			if (parse_lgrps(cookie, &plgrp_todo, optarg) < 0)
				usage(EXIT_FAILURE);

			/* If there are no valid lgroups exit immediately */
			if (plgrp_todo.nlgrps == 0) {
				(void) fprintf(stderr,
				    gettext("%s: no valid lgroups"
					" specified for -%c\n\n"),
				    progname, c);
				    usage(EXIT_FAILURE);
			}

			break;

		case 'a':	/* Get lgroup affinity */

			/*
			 * Fail if already specified option (besides -F)
			 * or no more arguments
			 */
			if (opt_seen || optind >= argc) {
				usage(EXIT_FAILURE);
			}
			opt_seen = 1;

			plgrp_todo.op = PLGRP_AFFINITY_GET;

			if (parse_lgrps(cookie, &plgrp_todo, optarg) < 0)
				usage(EXIT_FAILURE);

			/* If there are no valid lgroups exit immediately */
			if (plgrp_todo.nlgrps == 0) {
				(void) fprintf(stderr,
				    gettext("%s: no valid lgroups specified"
					" for -%c\n\n"),
				    progname, c);
				    usage(EXIT_FAILURE);
			}

			break;

		case 'A':	/* Set lgroup affinity */

			/*
			 * Fail if already specified option (besides -F)
			 * or no more arguments
			 */
			if (opt_seen || optind >= argc) {
				usage(EXIT_FAILURE);
			}
			opt_seen = 1;

			plgrp_todo.op = PLGRP_AFFINITY_SET;

			/*
			 * 'affstring' is the unparsed prtion of the affinity
			 * specification like 1,2/none,2/weak,0/strong
			 *
			 * 'next' is the next affinity specification to parse.
			 */
			affstring = optarg;
			while (affstring != NULL && strlen(affstring) > 0) {
				char	*next;

				/*
				 * affstring points to the first affinity
				 * specification. Split the string by
				 * DELIMIT_AFF separator and parse lgroups and
				 * affinity value separately.
				 */
				s = strchr(affstring, DELIMIT_AFF);
				if (s == NULL) {
					(void) fprintf(stderr,
					    gettext("%s: invalid "
						"syntax >%s<\n"),
					    progname, affstring);
					usage(EXIT_FAILURE);
				}

				aff = parse_lgrp_affinity(s, &next);
				if (aff == LGRP_AFF_INVALID) {
					(void) fprintf(stderr,
					    gettext("%s: invalid "
						"affinity >%s<\n"),
					    progname, affstring);
					usage(EXIT_FAILURE);
				}

				/*
				 * next should either point to the empty string
				 * or to the DELIMIT_AFF_LST separator.
				 */
				if (*next != '\0') {
					if (*next != DELIMIT_AFF_LST) {
						(void) fprintf(stderr,
						    gettext("%s: invalid "
							"syntax >%s<\n"),
						    progname, next);
						usage(EXIT_FAILURE);
					}
					*next = '\0';
					next++;
				}


				/*
				 * Now parse the list of lgroups
				 */
				if (parse_lgrps(cookie, &plgrp_todo,
					affstring) < 0) {
					usage(EXIT_FAILURE);
				}

				/*
				 * Set desired affinity for specified lgroup to
				 * the specified affinity.
				 */
				for (i = 0; i < plgrp_todo.nlgrps; i++) {
					if (plgrp_todo.affs[i] ==
					    LGRP_AFF_INVALID)
						plgrp_todo.affs[i] = aff;
				}

				/*
				 * We processed the leftmost element of the
				 * list. Advance affstr to the remaining part of
				 * the list. and repeat.
				 */
				affstring = next;
			}

			/*
			 * If there are no valid lgroups, exit immediately
			 */
			if (plgrp_todo.nlgrps == 0) {
				(void) fprintf(stderr,
				    gettext("%s: no valid lgroups specified "
				    "for -%c\n\n"), progname, c);
				    usage(EXIT_FAILURE);
			}

			break;

		case 'F':	/* Force */

			/*
			 * Only allow one occurrence
			 */
			if (Fflag != 0) {
				usage(EXIT_FAILURE);
			}

			/*
			 * Set flag to force /proc to grab process even though
			 * it's been grabbed by another process already
			 */
			Fflag = PGRAB_FORCE;
			break;

		case '?':	/* Unrecognized option */
		default:
			usage(EXIT_FAILURE);
			break;

		}
	}

	/*
	 * Should have more arguments left at least for PID or core
	 */
	if (optind >= argc)
		usage(EXIT_FAILURE);

	(void) lgrp_fini(cookie);

	/*
	 * Print heading and process each [pid | core]/lwps argument
	 */
	print_heading(plgrp_todo.op);
	(void) proc_initstdio();

	for (i = optind; i < argc && !interrupt; i++) {
		(void) proc_flushstdio();
		do_process(argv[i], &plgrp_todo, Fflag);
	}

	(void) proc_finistdio();

	if (plgrp_todo.nthreads == 0) {
		(void) fprintf(stderr, gettext("%s: no matching LWPs found\n"),
		    progname);
	}

	return ((nerrors ||interrupt) ? EXIT_NONFATAL : EXIT_SUCCESS);
}