1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
30*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <string.h>
36*7c478bd9Sstevel@tonic-gate #include <wait.h>
37*7c478bd9Sstevel@tonic-gate #include <search.h>
38*7c478bd9Sstevel@tonic-gate #include <unistd.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <dirent.h>
41*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
45*7c478bd9Sstevel@tonic-gate #include <procfs.h>
46*7c478bd9Sstevel@tonic-gate #include <macros.h>
47*7c478bd9Sstevel@tonic-gate #include <libgen.h>
48*7c478bd9Sstevel@tonic-gate #include <limits.h>
49*7c478bd9Sstevel@tonic-gate #include <errno.h>
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate #include "priocntl.h"
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate /*
54*7c478bd9Sstevel@tonic-gate * This file contains the code implementing the class independent part
55*7c478bd9Sstevel@tonic-gate * of the priocntl command. Most of the useful work for the priocntl
56*7c478bd9Sstevel@tonic-gate * command is done by the class specific sub-commands, the code for
57*7c478bd9Sstevel@tonic-gate * which is elsewhere. The class independent part of the command is
58*7c478bd9Sstevel@tonic-gate * responsible for executing the appropriate class specific sub-commands
59*7c478bd9Sstevel@tonic-gate * and providing any necessary input to the sub-commands.
60*7c478bd9Sstevel@tonic-gate * Code in this file should never assume any knowledge of any specific
61*7c478bd9Sstevel@tonic-gate * scheduler class (other than the SYS class).
62*7c478bd9Sstevel@tonic-gate */
63*7c478bd9Sstevel@tonic-gate
64*7c478bd9Sstevel@tonic-gate #define CLASSPATH "/usr/lib/class"
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate typedef struct classpids {
67*7c478bd9Sstevel@tonic-gate char clp_clname[PC_CLNMSZ];
68*7c478bd9Sstevel@tonic-gate pid_t *clp_pidlist;
69*7c478bd9Sstevel@tonic-gate int clp_pidlistsz;
70*7c478bd9Sstevel@tonic-gate int clp_npids;
71*7c478bd9Sstevel@tonic-gate } classpids_t;
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate static char usage[] =
74*7c478bd9Sstevel@tonic-gate "usage: priocntl -l\n\
75*7c478bd9Sstevel@tonic-gate priocntl -d [-i idtype] [idlist]\n\
76*7c478bd9Sstevel@tonic-gate priocntl -s [-c class] [c.s.o.] [-i idtype] [idlist]\n\
77*7c478bd9Sstevel@tonic-gate priocntl -e [-c class] [c.s.o.] command [argument(s)]\n";
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate static char basenm[BASENMSZ];
80*7c478bd9Sstevel@tonic-gate static char cmdpath[MAXPATHLEN];
81*7c478bd9Sstevel@tonic-gate
82*7c478bd9Sstevel@tonic-gate static char *procdir = "/proc";
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate static int print_classlist(void);
85*7c478bd9Sstevel@tonic-gate static void set_procs(char *, idtype_t, int, char **, char **);
86*7c478bd9Sstevel@tonic-gate static void exec_cmd(char *, char **);
87*7c478bd9Sstevel@tonic-gate static int print_procs(idtype_t, int, char *[]);
88*7c478bd9Sstevel@tonic-gate static void ids2pids(idtype_t, id_t *, int, classpids_t *, int);
89*7c478bd9Sstevel@tonic-gate static void add_pid_tolist(classpids_t *, int, char *, pid_t);
90*7c478bd9Sstevel@tonic-gate static void increase_pidlist(classpids_t *);
91*7c478bd9Sstevel@tonic-gate static boolean_t idmatch(char *, char *, int, char **);
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gate /*
94*7c478bd9Sstevel@tonic-gate * These variables are defined to be used in prio_getopt() below.
95*7c478bd9Sstevel@tonic-gate */
96*7c478bd9Sstevel@tonic-gate static int prio_getopt();
97*7c478bd9Sstevel@tonic-gate /* LINTED static unused */
98*7c478bd9Sstevel@tonic-gate static int prio_optopt = 0;
99*7c478bd9Sstevel@tonic-gate static char *prio_optarg = 0;
100*7c478bd9Sstevel@tonic-gate static int prio_optind = 1;
101*7c478bd9Sstevel@tonic-gate static int prio_sp = 1;
102*7c478bd9Sstevel@tonic-gate
103*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])104*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
105*7c478bd9Sstevel@tonic-gate {
106*7c478bd9Sstevel@tonic-gate int c;
107*7c478bd9Sstevel@tonic-gate int lflag, dflag, sflag, eflag, cflag, iflag, csoptsflag;
108*7c478bd9Sstevel@tonic-gate char *clname;
109*7c478bd9Sstevel@tonic-gate char *idtypnm;
110*7c478bd9Sstevel@tonic-gate idtype_t idtype;
111*7c478bd9Sstevel@tonic-gate int idargc;
112*7c478bd9Sstevel@tonic-gate char **idargv;
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate (void) strlcpy(cmdpath, argv[0], MAXPATHLEN);
115*7c478bd9Sstevel@tonic-gate (void) strlcpy(basenm, basename(argv[0]), BASENMSZ);
116*7c478bd9Sstevel@tonic-gate lflag = dflag = sflag = eflag = cflag = iflag = csoptsflag = 0;
117*7c478bd9Sstevel@tonic-gate while ((c = prio_getopt(argc, argv, "ldsec:i:")) != -1) {
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate switch (c) {
120*7c478bd9Sstevel@tonic-gate
121*7c478bd9Sstevel@tonic-gate case 'l':
122*7c478bd9Sstevel@tonic-gate lflag++;
123*7c478bd9Sstevel@tonic-gate break;
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate case 'd':
126*7c478bd9Sstevel@tonic-gate dflag++;
127*7c478bd9Sstevel@tonic-gate break;
128*7c478bd9Sstevel@tonic-gate
129*7c478bd9Sstevel@tonic-gate case 's':
130*7c478bd9Sstevel@tonic-gate sflag++;
131*7c478bd9Sstevel@tonic-gate break;
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate case 'e':
134*7c478bd9Sstevel@tonic-gate eflag++;
135*7c478bd9Sstevel@tonic-gate break;
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate case 'c':
138*7c478bd9Sstevel@tonic-gate cflag++;
139*7c478bd9Sstevel@tonic-gate clname = prio_optarg;
140*7c478bd9Sstevel@tonic-gate break;
141*7c478bd9Sstevel@tonic-gate
142*7c478bd9Sstevel@tonic-gate case 'i':
143*7c478bd9Sstevel@tonic-gate iflag++;
144*7c478bd9Sstevel@tonic-gate idtypnm = prio_optarg;
145*7c478bd9Sstevel@tonic-gate break;
146*7c478bd9Sstevel@tonic-gate
147*7c478bd9Sstevel@tonic-gate case '?':
148*7c478bd9Sstevel@tonic-gate if (strcmp(argv[prio_optind - 1], "-c") == 0 ||
149*7c478bd9Sstevel@tonic-gate strcmp(argv[prio_optind - 1], "-i") == 0) {
150*7c478bd9Sstevel@tonic-gate
151*7c478bd9Sstevel@tonic-gate /*
152*7c478bd9Sstevel@tonic-gate * getopt() will return ? if either
153*7c478bd9Sstevel@tonic-gate * of these appear without an argument.
154*7c478bd9Sstevel@tonic-gate */
155*7c478bd9Sstevel@tonic-gate fatalerr(usage);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate /*
159*7c478bd9Sstevel@tonic-gate * We assume for now that any option that
160*7c478bd9Sstevel@tonic-gate * getopt() doesn't recognize (with the
161*7c478bd9Sstevel@tonic-gate * exception of c and i) is intended for a
162*7c478bd9Sstevel@tonic-gate * class specific subcommand. For now we also
163*7c478bd9Sstevel@tonic-gate * require that all class specific options
164*7c478bd9Sstevel@tonic-gate * take an argument (until we can get smarter
165*7c478bd9Sstevel@tonic-gate * about parsing our options).
166*7c478bd9Sstevel@tonic-gate */
167*7c478bd9Sstevel@tonic-gate csoptsflag++;
168*7c478bd9Sstevel@tonic-gate prio_optind++;
169*7c478bd9Sstevel@tonic-gate prio_sp = 1;
170*7c478bd9Sstevel@tonic-gate break;
171*7c478bd9Sstevel@tonic-gate
172*7c478bd9Sstevel@tonic-gate default:
173*7c478bd9Sstevel@tonic-gate break;
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate if (lflag) {
178*7c478bd9Sstevel@tonic-gate if (dflag || sflag || eflag || cflag || iflag || csoptsflag)
179*7c478bd9Sstevel@tonic-gate fatalerr(usage);
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate return (print_classlist());
182*7c478bd9Sstevel@tonic-gate
183*7c478bd9Sstevel@tonic-gate } else if (dflag) {
184*7c478bd9Sstevel@tonic-gate if (lflag || sflag || eflag || cflag || csoptsflag)
185*7c478bd9Sstevel@tonic-gate fatalerr(usage);
186*7c478bd9Sstevel@tonic-gate if (iflag) {
187*7c478bd9Sstevel@tonic-gate if (str2idtyp(idtypnm, &idtype) == -1)
188*7c478bd9Sstevel@tonic-gate fatalerr("%s: bad idtype %s\n", cmdpath,
189*7c478bd9Sstevel@tonic-gate idtypnm);
190*7c478bd9Sstevel@tonic-gate } else {
191*7c478bd9Sstevel@tonic-gate idtype = P_PID;
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate
194*7c478bd9Sstevel@tonic-gate if (prio_optind < argc) {
195*7c478bd9Sstevel@tonic-gate idargc = argc - prio_optind;
196*7c478bd9Sstevel@tonic-gate idargv = &argv[prio_optind];
197*7c478bd9Sstevel@tonic-gate } else {
198*7c478bd9Sstevel@tonic-gate idargc = 0;
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate
201*7c478bd9Sstevel@tonic-gate return (print_procs(idtype, idargc, idargv));
202*7c478bd9Sstevel@tonic-gate
203*7c478bd9Sstevel@tonic-gate } else if (sflag) {
204*7c478bd9Sstevel@tonic-gate if (lflag || dflag || eflag)
205*7c478bd9Sstevel@tonic-gate fatalerr(usage);
206*7c478bd9Sstevel@tonic-gate if (iflag) {
207*7c478bd9Sstevel@tonic-gate if (str2idtyp(idtypnm, &idtype) == -1)
208*7c478bd9Sstevel@tonic-gate fatalerr("%s: bad idtype %s\n", cmdpath,
209*7c478bd9Sstevel@tonic-gate idtypnm);
210*7c478bd9Sstevel@tonic-gate } else {
211*7c478bd9Sstevel@tonic-gate idtype = P_PID;
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate
214*7c478bd9Sstevel@tonic-gate if (cflag == 0)
215*7c478bd9Sstevel@tonic-gate clname = NULL;
216*7c478bd9Sstevel@tonic-gate
217*7c478bd9Sstevel@tonic-gate if (prio_optind < argc) {
218*7c478bd9Sstevel@tonic-gate idargc = argc - prio_optind;
219*7c478bd9Sstevel@tonic-gate idargv = &argv[prio_optind];
220*7c478bd9Sstevel@tonic-gate } else {
221*7c478bd9Sstevel@tonic-gate idargc = 0;
222*7c478bd9Sstevel@tonic-gate }
223*7c478bd9Sstevel@tonic-gate
224*7c478bd9Sstevel@tonic-gate set_procs(clname, idtype, idargc, idargv, argv);
225*7c478bd9Sstevel@tonic-gate
226*7c478bd9Sstevel@tonic-gate } else if (eflag) {
227*7c478bd9Sstevel@tonic-gate if (lflag || dflag || sflag || iflag)
228*7c478bd9Sstevel@tonic-gate fatalerr(usage);
229*7c478bd9Sstevel@tonic-gate
230*7c478bd9Sstevel@tonic-gate if (cflag == 0)
231*7c478bd9Sstevel@tonic-gate clname = NULL;
232*7c478bd9Sstevel@tonic-gate
233*7c478bd9Sstevel@tonic-gate if (prio_optind >= argc)
234*7c478bd9Sstevel@tonic-gate fatalerr(usage);
235*7c478bd9Sstevel@tonic-gate
236*7c478bd9Sstevel@tonic-gate exec_cmd(clname, argv);
237*7c478bd9Sstevel@tonic-gate
238*7c478bd9Sstevel@tonic-gate } else {
239*7c478bd9Sstevel@tonic-gate fatalerr(usage);
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate
242*7c478bd9Sstevel@tonic-gate return (0);
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate
245*7c478bd9Sstevel@tonic-gate
246*7c478bd9Sstevel@tonic-gate /*
247*7c478bd9Sstevel@tonic-gate * Print the heading for the class list and execute the class
248*7c478bd9Sstevel@tonic-gate * specific sub-command with the -l option for each configured class.
249*7c478bd9Sstevel@tonic-gate */
250*7c478bd9Sstevel@tonic-gate static int
print_classlist(void)251*7c478bd9Sstevel@tonic-gate print_classlist(void)
252*7c478bd9Sstevel@tonic-gate {
253*7c478bd9Sstevel@tonic-gate id_t cid;
254*7c478bd9Sstevel@tonic-gate int nclass;
255*7c478bd9Sstevel@tonic-gate pcinfo_t pcinfo;
256*7c478bd9Sstevel@tonic-gate static char subcmdpath[128];
257*7c478bd9Sstevel@tonic-gate int status;
258*7c478bd9Sstevel@tonic-gate pid_t pid;
259*7c478bd9Sstevel@tonic-gate int error = 0;
260*7c478bd9Sstevel@tonic-gate
261*7c478bd9Sstevel@tonic-gate /*
262*7c478bd9Sstevel@tonic-gate * No special privileges required for this operation.
263*7c478bd9Sstevel@tonic-gate * Set the effective UID back to the real UID.
264*7c478bd9Sstevel@tonic-gate */
265*7c478bd9Sstevel@tonic-gate if (setuid(getuid()) == -1)
266*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't set effective UID back to real UID\n",
267*7c478bd9Sstevel@tonic-gate cmdpath);
268*7c478bd9Sstevel@tonic-gate
269*7c478bd9Sstevel@tonic-gate if ((nclass = priocntl(0, 0, PC_GETCLINFO, NULL)) == -1)
270*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't get number of configured classes, priocntl"
271*7c478bd9Sstevel@tonic-gate " system call failed with errno %d\n", cmdpath, errno);
272*7c478bd9Sstevel@tonic-gate
273*7c478bd9Sstevel@tonic-gate (void) printf("CONFIGURED CLASSES\n==================\n\n");
274*7c478bd9Sstevel@tonic-gate (void) printf("SYS (System Class)\n");
275*7c478bd9Sstevel@tonic-gate for (cid = 1; cid < nclass; cid++) {
276*7c478bd9Sstevel@tonic-gate (void) printf("\n");
277*7c478bd9Sstevel@tonic-gate (void) fflush(stdout);
278*7c478bd9Sstevel@tonic-gate pcinfo.pc_cid = cid;
279*7c478bd9Sstevel@tonic-gate if (priocntl(0, 0, PC_GETCLINFO, (caddr_t)&pcinfo) == -1)
280*7c478bd9Sstevel@tonic-gate fatalerr("%s: can't get class name (class ID = %ld)\n",
281*7c478bd9Sstevel@tonic-gate cmdpath, cid);
282*7c478bd9Sstevel@tonic-gate if (snprintf(subcmdpath, sizeof (subcmdpath), "%s/%s/%s%s",
283*7c478bd9Sstevel@tonic-gate CLASSPATH, pcinfo.pc_clname, pcinfo.pc_clname, basenm) >=
284*7c478bd9Sstevel@tonic-gate sizeof (subcmdpath))
285*7c478bd9Sstevel@tonic-gate fatalerr("%s: can't generate %s specific subcommand\n",
286*7c478bd9Sstevel@tonic-gate cmdpath, pcinfo.pc_clname);
287*7c478bd9Sstevel@tonic-gate if ((pid = fork()) == 0) {
288*7c478bd9Sstevel@tonic-gate (void) execl(subcmdpath, subcmdpath, "-l", (char *)0);
289*7c478bd9Sstevel@tonic-gate (void) printf("%s\n", pcinfo.pc_clname);
290*7c478bd9Sstevel@tonic-gate fatalerr("\tCan't execute %s specific subcommand\n",
291*7c478bd9Sstevel@tonic-gate pcinfo.pc_clname);
292*7c478bd9Sstevel@tonic-gate } else if (pid == (pid_t)-1) {
293*7c478bd9Sstevel@tonic-gate (void) printf("%s\n", pcinfo.pc_clname);
294*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
295*7c478bd9Sstevel@tonic-gate "Can't execute %s specific subcommand)\n",
296*7c478bd9Sstevel@tonic-gate pcinfo.pc_clname);
297*7c478bd9Sstevel@tonic-gate error = 1;
298*7c478bd9Sstevel@tonic-gate } else {
299*7c478bd9Sstevel@tonic-gate (void) wait(&status);
300*7c478bd9Sstevel@tonic-gate if (status)
301*7c478bd9Sstevel@tonic-gate error = 1;
302*7c478bd9Sstevel@tonic-gate }
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate
305*7c478bd9Sstevel@tonic-gate return (error);
306*7c478bd9Sstevel@tonic-gate }
307*7c478bd9Sstevel@tonic-gate
308*7c478bd9Sstevel@tonic-gate
309*7c478bd9Sstevel@tonic-gate /*
310*7c478bd9Sstevel@tonic-gate * For each class represented within the set of processes specified by
311*7c478bd9Sstevel@tonic-gate * idtype/idargv, print_procs() executes the class specific sub-command
312*7c478bd9Sstevel@tonic-gate * with the -d option. We pipe to each sub-command a list of pids in
313*7c478bd9Sstevel@tonic-gate * the set belonging to that class.
314*7c478bd9Sstevel@tonic-gate */
315*7c478bd9Sstevel@tonic-gate static int
print_procs(idtype_t idtype,int idargc,char * idargv[])316*7c478bd9Sstevel@tonic-gate print_procs(idtype_t idtype, int idargc, char *idargv[])
317*7c478bd9Sstevel@tonic-gate {
318*7c478bd9Sstevel@tonic-gate int i;
319*7c478bd9Sstevel@tonic-gate id_t id;
320*7c478bd9Sstevel@tonic-gate id_t idlist[NIDS];
321*7c478bd9Sstevel@tonic-gate int nids;
322*7c478bd9Sstevel@tonic-gate classpids_t *clpids;
323*7c478bd9Sstevel@tonic-gate int nclass;
324*7c478bd9Sstevel@tonic-gate id_t cid;
325*7c478bd9Sstevel@tonic-gate pcinfo_t pcinfo;
326*7c478bd9Sstevel@tonic-gate int pidexists;
327*7c478bd9Sstevel@tonic-gate FILE *pipe_to_subcmd;
328*7c478bd9Sstevel@tonic-gate char subcmd[128];
329*7c478bd9Sstevel@tonic-gate int error = 0;
330*7c478bd9Sstevel@tonic-gate
331*7c478bd9Sstevel@tonic-gate
332*7c478bd9Sstevel@tonic-gate /*
333*7c478bd9Sstevel@tonic-gate * Build a list of ids eliminating any duplicates in idargv.
334*7c478bd9Sstevel@tonic-gate */
335*7c478bd9Sstevel@tonic-gate if (idtype == P_ALL) {
336*7c478bd9Sstevel@tonic-gate /*
337*7c478bd9Sstevel@tonic-gate * No idlist should be specified. If one is specified,
338*7c478bd9Sstevel@tonic-gate * it is ignored.
339*7c478bd9Sstevel@tonic-gate */
340*7c478bd9Sstevel@tonic-gate nids = 0;
341*7c478bd9Sstevel@tonic-gate } else if (idargc == 0) {
342*7c478bd9Sstevel@tonic-gate
343*7c478bd9Sstevel@tonic-gate /*
344*7c478bd9Sstevel@tonic-gate * No ids supplied by user; use current id.
345*7c478bd9Sstevel@tonic-gate */
346*7c478bd9Sstevel@tonic-gate if (getmyid(idtype, &idlist[0]) == -1)
347*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't get ID for current process,"
348*7c478bd9Sstevel@tonic-gate " idtype = %d\n", cmdpath, idtype);
349*7c478bd9Sstevel@tonic-gate nids = 1;
350*7c478bd9Sstevel@tonic-gate } else {
351*7c478bd9Sstevel@tonic-gate nids = 0;
352*7c478bd9Sstevel@tonic-gate for (i = 0; i < idargc && nids < NIDS; i++) {
353*7c478bd9Sstevel@tonic-gate if (idtype == P_CID) {
354*7c478bd9Sstevel@tonic-gate if ((id = clname2cid(idargv[i])) == -1) {
355*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: Invalid or"
356*7c478bd9Sstevel@tonic-gate " unconfigured class %s in idlist"
357*7c478bd9Sstevel@tonic-gate " - ignored\n", cmdpath, idargv[i]);
358*7c478bd9Sstevel@tonic-gate error = 1;
359*7c478bd9Sstevel@tonic-gate }
360*7c478bd9Sstevel@tonic-gate } else {
361*7c478bd9Sstevel@tonic-gate id = (id_t)str2num(idargv[i], INT_MIN, INT_MAX);
362*7c478bd9Sstevel@tonic-gate if (errno) {
363*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
364*7c478bd9Sstevel@tonic-gate "%s: Invalid id \"%s\"\n",
365*7c478bd9Sstevel@tonic-gate cmdpath, idargv[i]);
366*7c478bd9Sstevel@tonic-gate error = 1;
367*7c478bd9Sstevel@tonic-gate id = BADPID;
368*7c478bd9Sstevel@tonic-gate }
369*7c478bd9Sstevel@tonic-gate }
370*7c478bd9Sstevel@tonic-gate
371*7c478bd9Sstevel@tonic-gate /*
372*7c478bd9Sstevel@tonic-gate * lsearch(3C) adds ids to the idlist,
373*7c478bd9Sstevel@tonic-gate * eliminating duplicates.
374*7c478bd9Sstevel@tonic-gate */
375*7c478bd9Sstevel@tonic-gate (void) lsearch((void *)&id, (void *)idlist,
376*7c478bd9Sstevel@tonic-gate (size_t *)&nids, sizeof (id), (int (*)())idcompar);
377*7c478bd9Sstevel@tonic-gate }
378*7c478bd9Sstevel@tonic-gate }
379*7c478bd9Sstevel@tonic-gate
380*7c478bd9Sstevel@tonic-gate if ((nclass = priocntl(0, 0, PC_GETCLINFO, NULL)) == -1)
381*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't get number of configured classes, priocntl"
382*7c478bd9Sstevel@tonic-gate " system call failed with errno %d\n", cmdpath, errno);
383*7c478bd9Sstevel@tonic-gate
384*7c478bd9Sstevel@tonic-gate if ((clpids = (classpids_t *)malloc(sizeof (classpids_t) * nclass)) ==
385*7c478bd9Sstevel@tonic-gate NULL)
386*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't allocate memory for clpids.\n", cmdpath);
387*7c478bd9Sstevel@tonic-gate
388*7c478bd9Sstevel@tonic-gate for (cid = 1; cid < nclass; cid++) {
389*7c478bd9Sstevel@tonic-gate pcinfo.pc_cid = cid;
390*7c478bd9Sstevel@tonic-gate if (priocntl(0, 0, PC_GETCLINFO, (caddr_t)&pcinfo) == -1)
391*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't get class name, cid = %ld\n",
392*7c478bd9Sstevel@tonic-gate cmdpath, cid);
393*7c478bd9Sstevel@tonic-gate
394*7c478bd9Sstevel@tonic-gate (void) strncpy(clpids[cid].clp_clname, pcinfo.pc_clname,
395*7c478bd9Sstevel@tonic-gate PC_CLNMSZ);
396*7c478bd9Sstevel@tonic-gate
397*7c478bd9Sstevel@tonic-gate /*
398*7c478bd9Sstevel@tonic-gate * The memory allocation for the pidlist uses realloc().
399*7c478bd9Sstevel@tonic-gate * A realloc() call is required, when "clp_npids" is
400*7c478bd9Sstevel@tonic-gate * equal to "clp_pidlistsz".
401*7c478bd9Sstevel@tonic-gate */
402*7c478bd9Sstevel@tonic-gate clpids[cid].clp_pidlist = (pid_t *)NULL;
403*7c478bd9Sstevel@tonic-gate clpids[cid].clp_pidlistsz = 0;
404*7c478bd9Sstevel@tonic-gate clpids[cid].clp_npids = 0;
405*7c478bd9Sstevel@tonic-gate }
406*7c478bd9Sstevel@tonic-gate
407*7c478bd9Sstevel@tonic-gate /*
408*7c478bd9Sstevel@tonic-gate * Build the pidlist.
409*7c478bd9Sstevel@tonic-gate */
410*7c478bd9Sstevel@tonic-gate ids2pids(idtype, idlist, nids, clpids, nclass);
411*7c478bd9Sstevel@tonic-gate
412*7c478bd9Sstevel@tonic-gate /*
413*7c478bd9Sstevel@tonic-gate * No need for special privileges any more.
414*7c478bd9Sstevel@tonic-gate * Set the effective UID back to the real UID.
415*7c478bd9Sstevel@tonic-gate */
416*7c478bd9Sstevel@tonic-gate if (setuid(getuid()) == -1)
417*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't set effective UID back to real UID\n",
418*7c478bd9Sstevel@tonic-gate cmdpath);
419*7c478bd9Sstevel@tonic-gate
420*7c478bd9Sstevel@tonic-gate pidexists = 0;
421*7c478bd9Sstevel@tonic-gate for (cid = 1; cid < nclass; cid++) {
422*7c478bd9Sstevel@tonic-gate if (clpids[cid].clp_npids == 0)
423*7c478bd9Sstevel@tonic-gate continue;
424*7c478bd9Sstevel@tonic-gate
425*7c478bd9Sstevel@tonic-gate pidexists = 1;
426*7c478bd9Sstevel@tonic-gate if (snprintf(subcmd, sizeof (subcmd), "%s/%s/%s%s -d",
427*7c478bd9Sstevel@tonic-gate CLASSPATH, clpids[cid].clp_clname, clpids[cid].clp_clname,
428*7c478bd9Sstevel@tonic-gate basenm) >= sizeof (subcmd)) {
429*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
430*7c478bd9Sstevel@tonic-gate "Can't generate %s specific subcommand\n",
431*7c478bd9Sstevel@tonic-gate clpids[cid].clp_clname);
432*7c478bd9Sstevel@tonic-gate error = 1;
433*7c478bd9Sstevel@tonic-gate free(clpids[cid].clp_pidlist);
434*7c478bd9Sstevel@tonic-gate continue;
435*7c478bd9Sstevel@tonic-gate }
436*7c478bd9Sstevel@tonic-gate if ((pipe_to_subcmd = popen(subcmd, "w")) == NULL) {
437*7c478bd9Sstevel@tonic-gate (void) printf("%s\n", clpids[cid].clp_clname);
438*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
439*7c478bd9Sstevel@tonic-gate "Can't execute %s specific subcommand\n",
440*7c478bd9Sstevel@tonic-gate clpids[cid].clp_clname);
441*7c478bd9Sstevel@tonic-gate error = 1;
442*7c478bd9Sstevel@tonic-gate free(clpids[cid].clp_pidlist);
443*7c478bd9Sstevel@tonic-gate continue;
444*7c478bd9Sstevel@tonic-gate }
445*7c478bd9Sstevel@tonic-gate (void) fwrite(clpids[cid].clp_pidlist, sizeof (pid_t),
446*7c478bd9Sstevel@tonic-gate clpids[cid].clp_npids, pipe_to_subcmd);
447*7c478bd9Sstevel@tonic-gate if (pclose(pipe_to_subcmd))
448*7c478bd9Sstevel@tonic-gate error = 1;
449*7c478bd9Sstevel@tonic-gate
450*7c478bd9Sstevel@tonic-gate free(clpids[cid].clp_pidlist);
451*7c478bd9Sstevel@tonic-gate }
452*7c478bd9Sstevel@tonic-gate
453*7c478bd9Sstevel@tonic-gate free(clpids);
454*7c478bd9Sstevel@tonic-gate
455*7c478bd9Sstevel@tonic-gate if (pidexists == 0)
456*7c478bd9Sstevel@tonic-gate fatalerr("%s: Process(es) not found.\n", cmdpath);
457*7c478bd9Sstevel@tonic-gate
458*7c478bd9Sstevel@tonic-gate return (error);
459*7c478bd9Sstevel@tonic-gate }
460*7c478bd9Sstevel@tonic-gate
461*7c478bd9Sstevel@tonic-gate
462*7c478bd9Sstevel@tonic-gate /*
463*7c478bd9Sstevel@tonic-gate * Execute the appropriate class specific sub-command with the arguments
464*7c478bd9Sstevel@tonic-gate * pointed to by subcmdargv. If the user specified a class we simply
465*7c478bd9Sstevel@tonic-gate * exec the sub-command for that class. If no class was specified we
466*7c478bd9Sstevel@tonic-gate * verify that the processes in the set specified by idtype/idargv are
467*7c478bd9Sstevel@tonic-gate * all in the same class and then execute the sub-command for that class.
468*7c478bd9Sstevel@tonic-gate */
469*7c478bd9Sstevel@tonic-gate static void
set_procs(clname,idtype,idargc,idargv,subcmdargv)470*7c478bd9Sstevel@tonic-gate set_procs(clname, idtype, idargc, idargv, subcmdargv)
471*7c478bd9Sstevel@tonic-gate char *clname;
472*7c478bd9Sstevel@tonic-gate idtype_t idtype;
473*7c478bd9Sstevel@tonic-gate int idargc;
474*7c478bd9Sstevel@tonic-gate char **idargv;
475*7c478bd9Sstevel@tonic-gate char **subcmdargv;
476*7c478bd9Sstevel@tonic-gate {
477*7c478bd9Sstevel@tonic-gate char idstr[PC_IDTYPNMSZ];
478*7c478bd9Sstevel@tonic-gate char myidstr[PC_IDTYPNMSZ];
479*7c478bd9Sstevel@tonic-gate char clnmbuf[PC_CLNMSZ];
480*7c478bd9Sstevel@tonic-gate pcinfo_t pcinfo;
481*7c478bd9Sstevel@tonic-gate static psinfo_t prinfo;
482*7c478bd9Sstevel@tonic-gate static prcred_t prcred;
483*7c478bd9Sstevel@tonic-gate DIR *dirp;
484*7c478bd9Sstevel@tonic-gate struct dirent *dentp;
485*7c478bd9Sstevel@tonic-gate static char pname[100];
486*7c478bd9Sstevel@tonic-gate char *fname;
487*7c478bd9Sstevel@tonic-gate int procfd;
488*7c478bd9Sstevel@tonic-gate int saverr;
489*7c478bd9Sstevel@tonic-gate static char subcmdpath[128];
490*7c478bd9Sstevel@tonic-gate boolean_t procinset;
491*7c478bd9Sstevel@tonic-gate id_t id;
492*7c478bd9Sstevel@tonic-gate size_t len;
493*7c478bd9Sstevel@tonic-gate
494*7c478bd9Sstevel@tonic-gate if (clname == NULL && idtype == P_PID && idargc <= 1) {
495*7c478bd9Sstevel@tonic-gate
496*7c478bd9Sstevel@tonic-gate /*
497*7c478bd9Sstevel@tonic-gate * No class specified by user but only one process
498*7c478bd9Sstevel@tonic-gate * in specified set. Get the class the easy way.
499*7c478bd9Sstevel@tonic-gate */
500*7c478bd9Sstevel@tonic-gate if (idargc == 0) {
501*7c478bd9Sstevel@tonic-gate if (priocntl(P_PID, P_MYID, PC_GETXPARMS, NULL,
502*7c478bd9Sstevel@tonic-gate PC_KY_CLNAME, clnmbuf, 0) == -1)
503*7c478bd9Sstevel@tonic-gate if (errno == ESRCH)
504*7c478bd9Sstevel@tonic-gate fatalerr("%s: Process not found.\n",
505*7c478bd9Sstevel@tonic-gate cmdpath);
506*7c478bd9Sstevel@tonic-gate else
507*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't get class of"
508*7c478bd9Sstevel@tonic-gate " current process\npriocntl"
509*7c478bd9Sstevel@tonic-gate " system call failed with"
510*7c478bd9Sstevel@tonic-gate " errno %d\n", cmdpath, errno);
511*7c478bd9Sstevel@tonic-gate } else {
512*7c478bd9Sstevel@tonic-gate /* idargc == 1 */
513*7c478bd9Sstevel@tonic-gate id = (id_t)str2num(idargv[0], INT_MIN, INT_MAX);
514*7c478bd9Sstevel@tonic-gate if (errno)
515*7c478bd9Sstevel@tonic-gate fatalerr("%s: Invalid id \"%s\"\n", cmdpath,
516*7c478bd9Sstevel@tonic-gate idargv[0]);
517*7c478bd9Sstevel@tonic-gate
518*7c478bd9Sstevel@tonic-gate if (priocntl(P_PID, id, PC_GETXPARMS,
519*7c478bd9Sstevel@tonic-gate NULL, PC_KY_CLNAME, clnmbuf, 0) == -1)
520*7c478bd9Sstevel@tonic-gate if (errno == ESRCH)
521*7c478bd9Sstevel@tonic-gate fatalerr("%s: Process not found.\n",
522*7c478bd9Sstevel@tonic-gate cmdpath);
523*7c478bd9Sstevel@tonic-gate else
524*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't get class of "
525*7c478bd9Sstevel@tonic-gate " specified process\npriocntl"
526*7c478bd9Sstevel@tonic-gate " system call failed with"
527*7c478bd9Sstevel@tonic-gate " errno %d\n", cmdpath, errno);
528*7c478bd9Sstevel@tonic-gate }
529*7c478bd9Sstevel@tonic-gate
530*7c478bd9Sstevel@tonic-gate clname = clnmbuf;
531*7c478bd9Sstevel@tonic-gate } else if (clname == NULL) {
532*7c478bd9Sstevel@tonic-gate
533*7c478bd9Sstevel@tonic-gate /*
534*7c478bd9Sstevel@tonic-gate * No class specified by user and potentially more
535*7c478bd9Sstevel@tonic-gate * than one process in specified set. Verify that
536*7c478bd9Sstevel@tonic-gate * all procs in set are in the same class.
537*7c478bd9Sstevel@tonic-gate */
538*7c478bd9Sstevel@tonic-gate if (idargc == 0 && idtype != P_ALL) {
539*7c478bd9Sstevel@tonic-gate
540*7c478bd9Sstevel@tonic-gate /*
541*7c478bd9Sstevel@tonic-gate * No ids supplied by user; use current id.
542*7c478bd9Sstevel@tonic-gate */
543*7c478bd9Sstevel@tonic-gate if (getmyidstr(idtype, myidstr) == -1)
544*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't get ID string for current"
545*7c478bd9Sstevel@tonic-gate " process, idtype = %d\n", cmdpath, idtype);
546*7c478bd9Sstevel@tonic-gate }
547*7c478bd9Sstevel@tonic-gate if ((dirp = opendir(procdir)) == NULL)
548*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't open PROC directory %s\n",
549*7c478bd9Sstevel@tonic-gate cmdpath, procdir);
550*7c478bd9Sstevel@tonic-gate
551*7c478bd9Sstevel@tonic-gate while ((dentp = readdir(dirp)) != NULL) {
552*7c478bd9Sstevel@tonic-gate if (dentp->d_name[0] == '.') /* skip . and .. */
553*7c478bd9Sstevel@tonic-gate continue;
554*7c478bd9Sstevel@tonic-gate
555*7c478bd9Sstevel@tonic-gate len = snprintf(pname, sizeof (pname), "%s/%s/",
556*7c478bd9Sstevel@tonic-gate procdir, dentp->d_name);
557*7c478bd9Sstevel@tonic-gate /* Really max(sizeof ("psinfo"), sizeof ("cred")) */
558*7c478bd9Sstevel@tonic-gate if (len + sizeof ("psinfo") > sizeof (pname)) {
559*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
560*7c478bd9Sstevel@tonic-gate "%s: skipping %s, name too long.\n",
561*7c478bd9Sstevel@tonic-gate cmdpath, dentp->d_name);
562*7c478bd9Sstevel@tonic-gate continue;
563*7c478bd9Sstevel@tonic-gate }
564*7c478bd9Sstevel@tonic-gate fname = pname + len;
565*7c478bd9Sstevel@tonic-gate retry:
566*7c478bd9Sstevel@tonic-gate (void) strcpy(fname, "psinfo");
567*7c478bd9Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) < 0)
568*7c478bd9Sstevel@tonic-gate continue;
569*7c478bd9Sstevel@tonic-gate
570*7c478bd9Sstevel@tonic-gate if (read(procfd, &prinfo, sizeof (prinfo)) !=
571*7c478bd9Sstevel@tonic-gate sizeof (prinfo)) {
572*7c478bd9Sstevel@tonic-gate saverr = errno;
573*7c478bd9Sstevel@tonic-gate (void) close(procfd);
574*7c478bd9Sstevel@tonic-gate if (saverr == EAGAIN)
575*7c478bd9Sstevel@tonic-gate goto retry;
576*7c478bd9Sstevel@tonic-gate if (saverr != ENOENT) {
577*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
578*7c478bd9Sstevel@tonic-gate "%s: Can't get process info for"
579*7c478bd9Sstevel@tonic-gate " %s\n", cmdpath, pname);
580*7c478bd9Sstevel@tonic-gate }
581*7c478bd9Sstevel@tonic-gate continue;
582*7c478bd9Sstevel@tonic-gate }
583*7c478bd9Sstevel@tonic-gate (void) close(procfd);
584*7c478bd9Sstevel@tonic-gate
585*7c478bd9Sstevel@tonic-gate if (idtype == P_UID || idtype == P_GID) {
586*7c478bd9Sstevel@tonic-gate (void) strcpy(fname, "cred");
587*7c478bd9Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) < 0 ||
588*7c478bd9Sstevel@tonic-gate read(procfd, &prcred, sizeof (prcred)) !=
589*7c478bd9Sstevel@tonic-gate sizeof (prcred)) {
590*7c478bd9Sstevel@tonic-gate saverr = errno;
591*7c478bd9Sstevel@tonic-gate if (procfd >= 0)
592*7c478bd9Sstevel@tonic-gate (void) close(procfd);
593*7c478bd9Sstevel@tonic-gate if (saverr == EAGAIN)
594*7c478bd9Sstevel@tonic-gate goto retry;
595*7c478bd9Sstevel@tonic-gate if (saverr != ENOENT) {
596*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
597*7c478bd9Sstevel@tonic-gate "%s: Can't get process"
598*7c478bd9Sstevel@tonic-gate " credentials for %s\n",
599*7c478bd9Sstevel@tonic-gate cmdpath, pname);
600*7c478bd9Sstevel@tonic-gate }
601*7c478bd9Sstevel@tonic-gate continue;
602*7c478bd9Sstevel@tonic-gate }
603*7c478bd9Sstevel@tonic-gate (void) close(procfd);
604*7c478bd9Sstevel@tonic-gate }
605*7c478bd9Sstevel@tonic-gate
606*7c478bd9Sstevel@tonic-gate if (prinfo.pr_lwp.pr_state == 0 || prinfo.pr_nlwp == 0)
607*7c478bd9Sstevel@tonic-gate continue;
608*7c478bd9Sstevel@tonic-gate
609*7c478bd9Sstevel@tonic-gate
610*7c478bd9Sstevel@tonic-gate switch (idtype) {
611*7c478bd9Sstevel@tonic-gate
612*7c478bd9Sstevel@tonic-gate case P_PID:
613*7c478bd9Sstevel@tonic-gate itoa((long)prinfo.pr_pid, idstr);
614*7c478bd9Sstevel@tonic-gate procinset = idmatch(idstr, myidstr,
615*7c478bd9Sstevel@tonic-gate idargc, idargv);
616*7c478bd9Sstevel@tonic-gate break;
617*7c478bd9Sstevel@tonic-gate
618*7c478bd9Sstevel@tonic-gate case P_PPID:
619*7c478bd9Sstevel@tonic-gate itoa((long)prinfo.pr_ppid, idstr);
620*7c478bd9Sstevel@tonic-gate procinset = idmatch(idstr, myidstr,
621*7c478bd9Sstevel@tonic-gate idargc, idargv);
622*7c478bd9Sstevel@tonic-gate break;
623*7c478bd9Sstevel@tonic-gate
624*7c478bd9Sstevel@tonic-gate case P_PGID:
625*7c478bd9Sstevel@tonic-gate itoa((long)prinfo.pr_pgid, idstr);
626*7c478bd9Sstevel@tonic-gate procinset = idmatch(idstr, myidstr,
627*7c478bd9Sstevel@tonic-gate idargc, idargv);
628*7c478bd9Sstevel@tonic-gate break;
629*7c478bd9Sstevel@tonic-gate
630*7c478bd9Sstevel@tonic-gate case P_SID:
631*7c478bd9Sstevel@tonic-gate itoa((long)prinfo.pr_sid, idstr);
632*7c478bd9Sstevel@tonic-gate procinset = idmatch(idstr, myidstr,
633*7c478bd9Sstevel@tonic-gate idargc, idargv);
634*7c478bd9Sstevel@tonic-gate break;
635*7c478bd9Sstevel@tonic-gate
636*7c478bd9Sstevel@tonic-gate case P_CID:
637*7c478bd9Sstevel@tonic-gate procinset = idmatch(prinfo.pr_lwp.pr_clname,
638*7c478bd9Sstevel@tonic-gate myidstr, idargc, idargv);
639*7c478bd9Sstevel@tonic-gate break;
640*7c478bd9Sstevel@tonic-gate
641*7c478bd9Sstevel@tonic-gate case P_UID:
642*7c478bd9Sstevel@tonic-gate itoa((long)prcred.pr_euid, idstr);
643*7c478bd9Sstevel@tonic-gate procinset = idmatch(idstr, myidstr,
644*7c478bd9Sstevel@tonic-gate idargc, idargv);
645*7c478bd9Sstevel@tonic-gate break;
646*7c478bd9Sstevel@tonic-gate
647*7c478bd9Sstevel@tonic-gate case P_GID:
648*7c478bd9Sstevel@tonic-gate itoa((long)prcred.pr_egid, idstr);
649*7c478bd9Sstevel@tonic-gate procinset = idmatch(idstr, myidstr,
650*7c478bd9Sstevel@tonic-gate idargc, idargv);
651*7c478bd9Sstevel@tonic-gate break;
652*7c478bd9Sstevel@tonic-gate
653*7c478bd9Sstevel@tonic-gate case P_PROJID:
654*7c478bd9Sstevel@tonic-gate itoa((long)prinfo.pr_projid, idstr);
655*7c478bd9Sstevel@tonic-gate procinset = idmatch(idstr, myidstr,
656*7c478bd9Sstevel@tonic-gate idargc, idargv);
657*7c478bd9Sstevel@tonic-gate break;
658*7c478bd9Sstevel@tonic-gate
659*7c478bd9Sstevel@tonic-gate case P_TASKID:
660*7c478bd9Sstevel@tonic-gate itoa((long)prinfo.pr_taskid, idstr);
661*7c478bd9Sstevel@tonic-gate procinset = idmatch(idstr, myidstr,
662*7c478bd9Sstevel@tonic-gate idargc, idargv);
663*7c478bd9Sstevel@tonic-gate break;
664*7c478bd9Sstevel@tonic-gate
665*7c478bd9Sstevel@tonic-gate case P_ZONEID:
666*7c478bd9Sstevel@tonic-gate itoa((long)prinfo.pr_zoneid, idstr);
667*7c478bd9Sstevel@tonic-gate procinset = idmatch(idstr, myidstr,
668*7c478bd9Sstevel@tonic-gate idargc, idargv);
669*7c478bd9Sstevel@tonic-gate break;
670*7c478bd9Sstevel@tonic-gate
671*7c478bd9Sstevel@tonic-gate case P_CTID:
672*7c478bd9Sstevel@tonic-gate itoa((long)prinfo.pr_contract, idstr);
673*7c478bd9Sstevel@tonic-gate procinset = idmatch(idstr, myidstr,
674*7c478bd9Sstevel@tonic-gate idargc, idargv);
675*7c478bd9Sstevel@tonic-gate break;
676*7c478bd9Sstevel@tonic-gate
677*7c478bd9Sstevel@tonic-gate case P_ALL:
678*7c478bd9Sstevel@tonic-gate procinset = B_TRUE;
679*7c478bd9Sstevel@tonic-gate break;
680*7c478bd9Sstevel@tonic-gate
681*7c478bd9Sstevel@tonic-gate default:
682*7c478bd9Sstevel@tonic-gate fatalerr("%s: Bad idtype %d in set_procs()\n",
683*7c478bd9Sstevel@tonic-gate cmdpath, idtype);
684*7c478bd9Sstevel@tonic-gate }
685*7c478bd9Sstevel@tonic-gate if (procinset == B_TRUE) {
686*7c478bd9Sstevel@tonic-gate if (clname == NULL) {
687*7c478bd9Sstevel@tonic-gate
688*7c478bd9Sstevel@tonic-gate /*
689*7c478bd9Sstevel@tonic-gate * First proc found in set.
690*7c478bd9Sstevel@tonic-gate */
691*7c478bd9Sstevel@tonic-gate (void) strcpy(clnmbuf,
692*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname);
693*7c478bd9Sstevel@tonic-gate clname = clnmbuf;
694*7c478bd9Sstevel@tonic-gate } else if (strcmp(clname,
695*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname) != 0) {
696*7c478bd9Sstevel@tonic-gate fatalerr("%s: Specified processes"
697*7c478bd9Sstevel@tonic-gate " from different classes.\n",
698*7c478bd9Sstevel@tonic-gate cmdpath);
699*7c478bd9Sstevel@tonic-gate }
700*7c478bd9Sstevel@tonic-gate }
701*7c478bd9Sstevel@tonic-gate }
702*7c478bd9Sstevel@tonic-gate (void) closedir(dirp);
703*7c478bd9Sstevel@tonic-gate if (clname == NULL)
704*7c478bd9Sstevel@tonic-gate fatalerr("%s: Process(es) not found.\n", cmdpath);
705*7c478bd9Sstevel@tonic-gate } else {
706*7c478bd9Sstevel@tonic-gate
707*7c478bd9Sstevel@tonic-gate /*
708*7c478bd9Sstevel@tonic-gate * User specified class. Check it for validity.
709*7c478bd9Sstevel@tonic-gate */
710*7c478bd9Sstevel@tonic-gate (void) strcpy(pcinfo.pc_clname, clname);
711*7c478bd9Sstevel@tonic-gate if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
712*7c478bd9Sstevel@tonic-gate fatalerr("%s: Invalid or unconfigured class %s\n",
713*7c478bd9Sstevel@tonic-gate cmdpath, clname);
714*7c478bd9Sstevel@tonic-gate }
715*7c478bd9Sstevel@tonic-gate
716*7c478bd9Sstevel@tonic-gate /*
717*7c478bd9Sstevel@tonic-gate * No need for special privileges any more.
718*7c478bd9Sstevel@tonic-gate * Set the effective UID back to the real UID.
719*7c478bd9Sstevel@tonic-gate */
720*7c478bd9Sstevel@tonic-gate if (setuid(getuid()) == -1)
721*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't set effective UID back to real UID\n",
722*7c478bd9Sstevel@tonic-gate cmdpath);
723*7c478bd9Sstevel@tonic-gate
724*7c478bd9Sstevel@tonic-gate if (snprintf(subcmdpath, sizeof (subcmdpath), "%s/%s/%s%s",
725*7c478bd9Sstevel@tonic-gate CLASSPATH, clname, clname, basenm) >= sizeof (subcmdpath))
726*7c478bd9Sstevel@tonic-gate fatalerr("%s: can't generate %s specific subcommand\n",
727*7c478bd9Sstevel@tonic-gate cmdpath, clname);
728*7c478bd9Sstevel@tonic-gate
729*7c478bd9Sstevel@tonic-gate subcmdargv[0] = subcmdpath;
730*7c478bd9Sstevel@tonic-gate (void) execv(subcmdpath, subcmdargv);
731*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't execute %s sub-command\n", cmdpath, clname);
732*7c478bd9Sstevel@tonic-gate }
733*7c478bd9Sstevel@tonic-gate
734*7c478bd9Sstevel@tonic-gate
735*7c478bd9Sstevel@tonic-gate /*
736*7c478bd9Sstevel@tonic-gate * Execute the appropriate class specific sub-command with the arguments
737*7c478bd9Sstevel@tonic-gate * pointed to by subcmdargv. If the user specified a class we simply
738*7c478bd9Sstevel@tonic-gate * exec the sub-command for that class. If no class was specified we
739*7c478bd9Sstevel@tonic-gate * execute the sub-command for our own current class.
740*7c478bd9Sstevel@tonic-gate */
741*7c478bd9Sstevel@tonic-gate static void
exec_cmd(clname,subcmdargv)742*7c478bd9Sstevel@tonic-gate exec_cmd(clname, subcmdargv)
743*7c478bd9Sstevel@tonic-gate char *clname;
744*7c478bd9Sstevel@tonic-gate char **subcmdargv;
745*7c478bd9Sstevel@tonic-gate {
746*7c478bd9Sstevel@tonic-gate pcinfo_t pcinfo;
747*7c478bd9Sstevel@tonic-gate char clnmbuf[PC_CLNMSZ];
748*7c478bd9Sstevel@tonic-gate char subcmdpath[128];
749*7c478bd9Sstevel@tonic-gate
750*7c478bd9Sstevel@tonic-gate /*
751*7c478bd9Sstevel@tonic-gate * No special privileges required for this operation.
752*7c478bd9Sstevel@tonic-gate * Set the effective UID back to the real UID.
753*7c478bd9Sstevel@tonic-gate */
754*7c478bd9Sstevel@tonic-gate if (setuid(getuid()) == -1)
755*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't set effective UID back to real UID\n",
756*7c478bd9Sstevel@tonic-gate cmdpath);
757*7c478bd9Sstevel@tonic-gate
758*7c478bd9Sstevel@tonic-gate if (clname == NULL) {
759*7c478bd9Sstevel@tonic-gate if (priocntl(P_PID, P_MYID, PC_GETXPARMS, NULL,
760*7c478bd9Sstevel@tonic-gate PC_KY_CLNAME, clnmbuf, 0) == -1)
761*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't get class name of current process\n"
762*7c478bd9Sstevel@tonic-gate "priocntl system call failed with errno %d\n",
763*7c478bd9Sstevel@tonic-gate cmdpath, errno);
764*7c478bd9Sstevel@tonic-gate
765*7c478bd9Sstevel@tonic-gate clname = clnmbuf;
766*7c478bd9Sstevel@tonic-gate } else {
767*7c478bd9Sstevel@tonic-gate
768*7c478bd9Sstevel@tonic-gate /*
769*7c478bd9Sstevel@tonic-gate * User specified class. Check it for validity.
770*7c478bd9Sstevel@tonic-gate */
771*7c478bd9Sstevel@tonic-gate (void) strcpy(pcinfo.pc_clname, clname);
772*7c478bd9Sstevel@tonic-gate if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
773*7c478bd9Sstevel@tonic-gate fatalerr("%s: Invalid or unconfigured class %s\n",
774*7c478bd9Sstevel@tonic-gate cmdpath, clname);
775*7c478bd9Sstevel@tonic-gate }
776*7c478bd9Sstevel@tonic-gate
777*7c478bd9Sstevel@tonic-gate if (snprintf(subcmdpath, sizeof (subcmdpath), "%s/%s/%s%s",
778*7c478bd9Sstevel@tonic-gate CLASSPATH, clname, clname, basenm) >= sizeof (subcmdpath))
779*7c478bd9Sstevel@tonic-gate fatalerr("%s: can't generate %s specific subcommand\n",
780*7c478bd9Sstevel@tonic-gate cmdpath, clname);
781*7c478bd9Sstevel@tonic-gate subcmdargv[0] = subcmdpath;
782*7c478bd9Sstevel@tonic-gate (void) execv(subcmdpath, subcmdargv);
783*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't execute %s sub-command\n", cmdpath, clname);
784*7c478bd9Sstevel@tonic-gate }
785*7c478bd9Sstevel@tonic-gate
786*7c478bd9Sstevel@tonic-gate
787*7c478bd9Sstevel@tonic-gate /*
788*7c478bd9Sstevel@tonic-gate * Fill in the classpids structures in the array pointed to by clpids
789*7c478bd9Sstevel@tonic-gate * with pids for the processes in the set specified by idtype/idlist.
790*7c478bd9Sstevel@tonic-gate * We read the /proc/<pid>/psinfo file to get the necessary process
791*7c478bd9Sstevel@tonic-gate * information.
792*7c478bd9Sstevel@tonic-gate */
793*7c478bd9Sstevel@tonic-gate static void
ids2pids(idtype,idlist,nids,clpids,nclass)794*7c478bd9Sstevel@tonic-gate ids2pids(idtype, idlist, nids, clpids, nclass)
795*7c478bd9Sstevel@tonic-gate idtype_t idtype;
796*7c478bd9Sstevel@tonic-gate id_t *idlist;
797*7c478bd9Sstevel@tonic-gate int nids;
798*7c478bd9Sstevel@tonic-gate classpids_t *clpids;
799*7c478bd9Sstevel@tonic-gate int nclass;
800*7c478bd9Sstevel@tonic-gate {
801*7c478bd9Sstevel@tonic-gate static psinfo_t prinfo;
802*7c478bd9Sstevel@tonic-gate static prcred_t prcred;
803*7c478bd9Sstevel@tonic-gate DIR *dirp;
804*7c478bd9Sstevel@tonic-gate struct dirent *dentp;
805*7c478bd9Sstevel@tonic-gate char pname[100];
806*7c478bd9Sstevel@tonic-gate char *fname;
807*7c478bd9Sstevel@tonic-gate int procfd;
808*7c478bd9Sstevel@tonic-gate int saverr;
809*7c478bd9Sstevel@tonic-gate int i;
810*7c478bd9Sstevel@tonic-gate char *clname;
811*7c478bd9Sstevel@tonic-gate size_t len;
812*7c478bd9Sstevel@tonic-gate
813*7c478bd9Sstevel@tonic-gate if ((dirp = opendir(procdir)) == NULL)
814*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't open PROC directory %s\n",
815*7c478bd9Sstevel@tonic-gate cmdpath, procdir);
816*7c478bd9Sstevel@tonic-gate
817*7c478bd9Sstevel@tonic-gate while ((dentp = readdir(dirp)) != NULL) {
818*7c478bd9Sstevel@tonic-gate if (dentp->d_name[0] == '.') /* skip . and .. */
819*7c478bd9Sstevel@tonic-gate continue;
820*7c478bd9Sstevel@tonic-gate
821*7c478bd9Sstevel@tonic-gate len = snprintf(pname, sizeof (pname), "%s/%s/",
822*7c478bd9Sstevel@tonic-gate procdir, dentp->d_name);
823*7c478bd9Sstevel@tonic-gate /* Really max(sizeof ("psinfo"), sizeof ("cred")) */
824*7c478bd9Sstevel@tonic-gate if (len + sizeof ("psinfo") > sizeof (pname)) {
825*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
826*7c478bd9Sstevel@tonic-gate "%s: skipping %s, name too long.\n",
827*7c478bd9Sstevel@tonic-gate cmdpath, dentp->d_name);
828*7c478bd9Sstevel@tonic-gate continue;
829*7c478bd9Sstevel@tonic-gate }
830*7c478bd9Sstevel@tonic-gate fname = pname + len;
831*7c478bd9Sstevel@tonic-gate retry:
832*7c478bd9Sstevel@tonic-gate (void) strcpy(fname, "psinfo");
833*7c478bd9Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) < 0)
834*7c478bd9Sstevel@tonic-gate continue;
835*7c478bd9Sstevel@tonic-gate if (read(procfd, &prinfo, sizeof (prinfo)) != sizeof (prinfo)) {
836*7c478bd9Sstevel@tonic-gate saverr = errno;
837*7c478bd9Sstevel@tonic-gate (void) close(procfd);
838*7c478bd9Sstevel@tonic-gate if (saverr == EAGAIN)
839*7c478bd9Sstevel@tonic-gate goto retry;
840*7c478bd9Sstevel@tonic-gate if (saverr != ENOENT) {
841*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
842*7c478bd9Sstevel@tonic-gate "%s: Can't get process info for %s\n",
843*7c478bd9Sstevel@tonic-gate cmdpath, pname);
844*7c478bd9Sstevel@tonic-gate }
845*7c478bd9Sstevel@tonic-gate continue;
846*7c478bd9Sstevel@tonic-gate }
847*7c478bd9Sstevel@tonic-gate (void) close(procfd);
848*7c478bd9Sstevel@tonic-gate
849*7c478bd9Sstevel@tonic-gate if (idtype == P_UID || idtype == P_GID) {
850*7c478bd9Sstevel@tonic-gate (void) strcpy(fname, "cred");
851*7c478bd9Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) < 0 ||
852*7c478bd9Sstevel@tonic-gate read(procfd, &prcred, sizeof (prcred)) !=
853*7c478bd9Sstevel@tonic-gate sizeof (prcred)) {
854*7c478bd9Sstevel@tonic-gate saverr = errno;
855*7c478bd9Sstevel@tonic-gate (void) close(procfd);
856*7c478bd9Sstevel@tonic-gate if (saverr == EAGAIN)
857*7c478bd9Sstevel@tonic-gate goto retry;
858*7c478bd9Sstevel@tonic-gate if (saverr != ENOENT) {
859*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
860*7c478bd9Sstevel@tonic-gate "%s: Can't get process credentials"
861*7c478bd9Sstevel@tonic-gate " for %s\n",
862*7c478bd9Sstevel@tonic-gate cmdpath, pname);
863*7c478bd9Sstevel@tonic-gate }
864*7c478bd9Sstevel@tonic-gate continue;
865*7c478bd9Sstevel@tonic-gate }
866*7c478bd9Sstevel@tonic-gate (void) close(procfd);
867*7c478bd9Sstevel@tonic-gate }
868*7c478bd9Sstevel@tonic-gate
869*7c478bd9Sstevel@tonic-gate if (prinfo.pr_lwp.pr_state == 0 || prinfo.pr_nlwp == 0)
870*7c478bd9Sstevel@tonic-gate continue;
871*7c478bd9Sstevel@tonic-gate
872*7c478bd9Sstevel@tonic-gate switch (idtype) {
873*7c478bd9Sstevel@tonic-gate
874*7c478bd9Sstevel@tonic-gate case P_PID:
875*7c478bd9Sstevel@tonic-gate for (i = 0; i < nids; i++) {
876*7c478bd9Sstevel@tonic-gate if (idlist[i] == (id_t)prinfo.pr_pid)
877*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass,
878*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname,
879*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
880*7c478bd9Sstevel@tonic-gate }
881*7c478bd9Sstevel@tonic-gate break;
882*7c478bd9Sstevel@tonic-gate
883*7c478bd9Sstevel@tonic-gate case P_PPID:
884*7c478bd9Sstevel@tonic-gate for (i = 0; i < nids; i++) {
885*7c478bd9Sstevel@tonic-gate if (idlist[i] == (id_t)prinfo.pr_ppid)
886*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass,
887*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname,
888*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
889*7c478bd9Sstevel@tonic-gate }
890*7c478bd9Sstevel@tonic-gate break;
891*7c478bd9Sstevel@tonic-gate
892*7c478bd9Sstevel@tonic-gate case P_PGID:
893*7c478bd9Sstevel@tonic-gate for (i = 0; i < nids; i++) {
894*7c478bd9Sstevel@tonic-gate if (idlist[i] == (id_t)prinfo.pr_pgid)
895*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass,
896*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname,
897*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
898*7c478bd9Sstevel@tonic-gate }
899*7c478bd9Sstevel@tonic-gate break;
900*7c478bd9Sstevel@tonic-gate
901*7c478bd9Sstevel@tonic-gate case P_SID:
902*7c478bd9Sstevel@tonic-gate for (i = 0; i < nids; i++) {
903*7c478bd9Sstevel@tonic-gate if (idlist[i] == (id_t)prinfo.pr_sid)
904*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass,
905*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname,
906*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
907*7c478bd9Sstevel@tonic-gate }
908*7c478bd9Sstevel@tonic-gate break;
909*7c478bd9Sstevel@tonic-gate
910*7c478bd9Sstevel@tonic-gate case P_CID:
911*7c478bd9Sstevel@tonic-gate for (i = 0; i < nids; i++) {
912*7c478bd9Sstevel@tonic-gate clname = clpids[idlist[i]].clp_clname;
913*7c478bd9Sstevel@tonic-gate if (strcmp(clname,
914*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname) == 0)
915*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass,
916*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname,
917*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
918*7c478bd9Sstevel@tonic-gate }
919*7c478bd9Sstevel@tonic-gate break;
920*7c478bd9Sstevel@tonic-gate
921*7c478bd9Sstevel@tonic-gate case P_UID:
922*7c478bd9Sstevel@tonic-gate for (i = 0; i < nids; i++) {
923*7c478bd9Sstevel@tonic-gate if (idlist[i] == (id_t)prcred.pr_euid)
924*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass,
925*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname,
926*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
927*7c478bd9Sstevel@tonic-gate }
928*7c478bd9Sstevel@tonic-gate break;
929*7c478bd9Sstevel@tonic-gate
930*7c478bd9Sstevel@tonic-gate case P_GID:
931*7c478bd9Sstevel@tonic-gate for (i = 0; i < nids; i++) {
932*7c478bd9Sstevel@tonic-gate if (idlist[i] == (id_t)prcred.pr_egid)
933*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass,
934*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname,
935*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
936*7c478bd9Sstevel@tonic-gate }
937*7c478bd9Sstevel@tonic-gate break;
938*7c478bd9Sstevel@tonic-gate
939*7c478bd9Sstevel@tonic-gate case P_PROJID:
940*7c478bd9Sstevel@tonic-gate for (i = 0; i < nids; i++) {
941*7c478bd9Sstevel@tonic-gate if (idlist[i] == (id_t)prinfo.pr_projid)
942*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass,
943*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname,
944*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
945*7c478bd9Sstevel@tonic-gate }
946*7c478bd9Sstevel@tonic-gate break;
947*7c478bd9Sstevel@tonic-gate
948*7c478bd9Sstevel@tonic-gate case P_TASKID:
949*7c478bd9Sstevel@tonic-gate for (i = 0; i < nids; i++) {
950*7c478bd9Sstevel@tonic-gate if (idlist[i] == (id_t)prinfo.pr_taskid)
951*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass,
952*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname,
953*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
954*7c478bd9Sstevel@tonic-gate }
955*7c478bd9Sstevel@tonic-gate break;
956*7c478bd9Sstevel@tonic-gate
957*7c478bd9Sstevel@tonic-gate case P_ZONEID:
958*7c478bd9Sstevel@tonic-gate for (i = 0; i < nids; i++) {
959*7c478bd9Sstevel@tonic-gate if (idlist[i] == (id_t)prinfo.pr_zoneid)
960*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass,
961*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname,
962*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
963*7c478bd9Sstevel@tonic-gate }
964*7c478bd9Sstevel@tonic-gate break;
965*7c478bd9Sstevel@tonic-gate
966*7c478bd9Sstevel@tonic-gate case P_CTID:
967*7c478bd9Sstevel@tonic-gate for (i = 0; i < nids; i++) {
968*7c478bd9Sstevel@tonic-gate if (idlist[i] == (id_t)prinfo.pr_contract)
969*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass,
970*7c478bd9Sstevel@tonic-gate prinfo.pr_lwp.pr_clname,
971*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
972*7c478bd9Sstevel@tonic-gate }
973*7c478bd9Sstevel@tonic-gate break;
974*7c478bd9Sstevel@tonic-gate
975*7c478bd9Sstevel@tonic-gate case P_ALL:
976*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass, prinfo.pr_lwp.pr_clname,
977*7c478bd9Sstevel@tonic-gate prinfo.pr_pid);
978*7c478bd9Sstevel@tonic-gate break;
979*7c478bd9Sstevel@tonic-gate
980*7c478bd9Sstevel@tonic-gate default:
981*7c478bd9Sstevel@tonic-gate fatalerr("%s: Bad idtype %d in ids2pids()\n",
982*7c478bd9Sstevel@tonic-gate cmdpath, idtype);
983*7c478bd9Sstevel@tonic-gate }
984*7c478bd9Sstevel@tonic-gate }
985*7c478bd9Sstevel@tonic-gate (void) closedir(dirp);
986*7c478bd9Sstevel@tonic-gate }
987*7c478bd9Sstevel@tonic-gate
988*7c478bd9Sstevel@tonic-gate
989*7c478bd9Sstevel@tonic-gate /*
990*7c478bd9Sstevel@tonic-gate * Search the array pointed to by clpids for the classpids
991*7c478bd9Sstevel@tonic-gate * structure corresponding to clname and add pid to its
992*7c478bd9Sstevel@tonic-gate * pidlist.
993*7c478bd9Sstevel@tonic-gate */
994*7c478bd9Sstevel@tonic-gate static void
add_pid_tolist(clpids,nclass,clname,pid)995*7c478bd9Sstevel@tonic-gate add_pid_tolist(clpids, nclass, clname, pid)
996*7c478bd9Sstevel@tonic-gate classpids_t *clpids;
997*7c478bd9Sstevel@tonic-gate int nclass;
998*7c478bd9Sstevel@tonic-gate char *clname;
999*7c478bd9Sstevel@tonic-gate pid_t pid;
1000*7c478bd9Sstevel@tonic-gate {
1001*7c478bd9Sstevel@tonic-gate classpids_t *clp;
1002*7c478bd9Sstevel@tonic-gate
1003*7c478bd9Sstevel@tonic-gate for (clp = clpids; clp != &clpids[nclass]; clp++) {
1004*7c478bd9Sstevel@tonic-gate if (strcmp(clp->clp_clname, clname) == 0) {
1005*7c478bd9Sstevel@tonic-gate if (clp->clp_npids == clp->clp_pidlistsz)
1006*7c478bd9Sstevel@tonic-gate increase_pidlist(clp);
1007*7c478bd9Sstevel@tonic-gate
1008*7c478bd9Sstevel@tonic-gate (clp->clp_pidlist)[clp->clp_npids] = pid;
1009*7c478bd9Sstevel@tonic-gate clp->clp_npids++;
1010*7c478bd9Sstevel@tonic-gate return;
1011*7c478bd9Sstevel@tonic-gate }
1012*7c478bd9Sstevel@tonic-gate }
1013*7c478bd9Sstevel@tonic-gate }
1014*7c478bd9Sstevel@tonic-gate
1015*7c478bd9Sstevel@tonic-gate
1016*7c478bd9Sstevel@tonic-gate static void
increase_pidlist(classpids_t * clp)1017*7c478bd9Sstevel@tonic-gate increase_pidlist(classpids_t *clp)
1018*7c478bd9Sstevel@tonic-gate {
1019*7c478bd9Sstevel@tonic-gate if ((clp->clp_pidlist = realloc(clp->clp_pidlist,
1020*7c478bd9Sstevel@tonic-gate (clp->clp_pidlistsz + NPIDS) * sizeof (pid_t))) == NULL)
1021*7c478bd9Sstevel@tonic-gate /*
1022*7c478bd9Sstevel@tonic-gate * The pidlist is filled up and we cannot increase the size.
1023*7c478bd9Sstevel@tonic-gate */
1024*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't allocate memory for pidlist.\n", cmdpath);
1025*7c478bd9Sstevel@tonic-gate
1026*7c478bd9Sstevel@tonic-gate clp->clp_pidlistsz += NPIDS;
1027*7c478bd9Sstevel@tonic-gate }
1028*7c478bd9Sstevel@tonic-gate
1029*7c478bd9Sstevel@tonic-gate
1030*7c478bd9Sstevel@tonic-gate /*
1031*7c478bd9Sstevel@tonic-gate * Compare id strings for equality. If idargv contains ids
1032*7c478bd9Sstevel@tonic-gate * (idargc > 0) compare idstr to each id in idargv, otherwise
1033*7c478bd9Sstevel@tonic-gate * just compare to curidstr.
1034*7c478bd9Sstevel@tonic-gate */
1035*7c478bd9Sstevel@tonic-gate static boolean_t
idmatch(idstr,curidstr,idargc,idargv)1036*7c478bd9Sstevel@tonic-gate idmatch(idstr, curidstr, idargc, idargv)
1037*7c478bd9Sstevel@tonic-gate char *idstr;
1038*7c478bd9Sstevel@tonic-gate char *curidstr;
1039*7c478bd9Sstevel@tonic-gate int idargc;
1040*7c478bd9Sstevel@tonic-gate char **idargv;
1041*7c478bd9Sstevel@tonic-gate {
1042*7c478bd9Sstevel@tonic-gate int i;
1043*7c478bd9Sstevel@tonic-gate
1044*7c478bd9Sstevel@tonic-gate if (idargc == 0) {
1045*7c478bd9Sstevel@tonic-gate if (strcmp(curidstr, idstr) == 0)
1046*7c478bd9Sstevel@tonic-gate return (B_TRUE);
1047*7c478bd9Sstevel@tonic-gate } else {
1048*7c478bd9Sstevel@tonic-gate for (i = 0; i < idargc; i++) {
1049*7c478bd9Sstevel@tonic-gate if (strcmp(idargv[i], idstr) == 0)
1050*7c478bd9Sstevel@tonic-gate return (B_TRUE);
1051*7c478bd9Sstevel@tonic-gate }
1052*7c478bd9Sstevel@tonic-gate }
1053*7c478bd9Sstevel@tonic-gate return (B_FALSE);
1054*7c478bd9Sstevel@tonic-gate }
1055*7c478bd9Sstevel@tonic-gate
1056*7c478bd9Sstevel@tonic-gate /*
1057*7c478bd9Sstevel@tonic-gate * This is a copy of the getopt() function found in libc:getopt.c. A separate
1058*7c478bd9Sstevel@tonic-gate * copy is required to fix the bug id #1114636. To fix the problem we need to
1059*7c478bd9Sstevel@tonic-gate * reset the _sp to 1. Since _sp in libc:getopt() is not exposed, a copy of
1060*7c478bd9Sstevel@tonic-gate * the getopt() is kept so that prio_sp can be reset to 1.
1061*7c478bd9Sstevel@tonic-gate */
1062*7c478bd9Sstevel@tonic-gate
1063*7c478bd9Sstevel@tonic-gate static int
prio_getopt(argc,argv,opts)1064*7c478bd9Sstevel@tonic-gate prio_getopt(argc, argv, opts)
1065*7c478bd9Sstevel@tonic-gate int argc;
1066*7c478bd9Sstevel@tonic-gate #ifdef __STDC__
1067*7c478bd9Sstevel@tonic-gate char *const *argv, *opts;
1068*7c478bd9Sstevel@tonic-gate #else
1069*7c478bd9Sstevel@tonic-gate char **argv, *opts;
1070*7c478bd9Sstevel@tonic-gate #endif
1071*7c478bd9Sstevel@tonic-gate {
1072*7c478bd9Sstevel@tonic-gate register char c;
1073*7c478bd9Sstevel@tonic-gate register char *cp;
1074*7c478bd9Sstevel@tonic-gate
1075*7c478bd9Sstevel@tonic-gate if (prio_sp == 1)
1076*7c478bd9Sstevel@tonic-gate if (prio_optind >= argc ||
1077*7c478bd9Sstevel@tonic-gate argv[prio_optind][0] != '-' || argv[prio_optind][1] == '\0')
1078*7c478bd9Sstevel@tonic-gate return (EOF);
1079*7c478bd9Sstevel@tonic-gate else if (strcmp(argv[prio_optind], "--") == NULL) {
1080*7c478bd9Sstevel@tonic-gate prio_optind++;
1081*7c478bd9Sstevel@tonic-gate return (EOF);
1082*7c478bd9Sstevel@tonic-gate }
1083*7c478bd9Sstevel@tonic-gate prio_optopt = c = (unsigned char)argv[prio_optind][prio_sp];
1084*7c478bd9Sstevel@tonic-gate if (c == ':' || (cp = strchr(opts, c)) == NULL) {
1085*7c478bd9Sstevel@tonic-gate if (argv[prio_optind][++prio_sp] == '\0') {
1086*7c478bd9Sstevel@tonic-gate prio_optind++;
1087*7c478bd9Sstevel@tonic-gate prio_sp = 1;
1088*7c478bd9Sstevel@tonic-gate }
1089*7c478bd9Sstevel@tonic-gate return ('?');
1090*7c478bd9Sstevel@tonic-gate }
1091*7c478bd9Sstevel@tonic-gate if (*++cp == ':') {
1092*7c478bd9Sstevel@tonic-gate if (argv[prio_optind][prio_sp+1] != '\0')
1093*7c478bd9Sstevel@tonic-gate prio_optarg = &argv[prio_optind++][prio_sp+1];
1094*7c478bd9Sstevel@tonic-gate else if (++prio_optind >= argc) {
1095*7c478bd9Sstevel@tonic-gate prio_sp = 1;
1096*7c478bd9Sstevel@tonic-gate return ('?');
1097*7c478bd9Sstevel@tonic-gate } else
1098*7c478bd9Sstevel@tonic-gate prio_optarg = argv[prio_optind++];
1099*7c478bd9Sstevel@tonic-gate prio_sp = 1;
1100*7c478bd9Sstevel@tonic-gate } else {
1101*7c478bd9Sstevel@tonic-gate if (argv[prio_optind][++prio_sp] == '\0') {
1102*7c478bd9Sstevel@tonic-gate prio_sp = 1;
1103*7c478bd9Sstevel@tonic-gate prio_optind++;
1104*7c478bd9Sstevel@tonic-gate }
1105*7c478bd9Sstevel@tonic-gate prio_optarg = NULL;
1106*7c478bd9Sstevel@tonic-gate }
1107*7c478bd9Sstevel@tonic-gate return (c);
1108*7c478bd9Sstevel@tonic-gate }
1109