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 2001-2002 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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #include <stdio.h>
33*7c478bd9Sstevel@tonic-gate #include <string.h>
34*7c478bd9Sstevel@tonic-gate #include <libgen.h>
35*7c478bd9Sstevel@tonic-gate #include <unistd.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/procset.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/fxpriocntl.h>
40*7c478bd9Sstevel@tonic-gate #include <limits.h>
41*7c478bd9Sstevel@tonic-gate #include <errno.h>
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate #include "priocntl.h"
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate * This file contains the class specific code implementing
47*7c478bd9Sstevel@tonic-gate * the fixed-priority priocntl sub-command.
48*7c478bd9Sstevel@tonic-gate */
49*7c478bd9Sstevel@tonic-gate
50*7c478bd9Sstevel@tonic-gate #define ADDKEYVAL(p, k, v) { (p[0]) = (k); (p[1]) = (v); p += 2; }
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate #define FX_KEYCNT 4 /* maximal number of (key, value) pairs */
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate * control flags
56*7c478bd9Sstevel@tonic-gate */
57*7c478bd9Sstevel@tonic-gate #define FX_DOUPRILIM 0x01 /* user priority limit */
58*7c478bd9Sstevel@tonic-gate #define FX_DOUPRI 0x02 /* user priority */
59*7c478bd9Sstevel@tonic-gate #define FX_DOTQ 0x04 /* time quantum */
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate static void print_fxinfo();
64*7c478bd9Sstevel@tonic-gate static int print_fxprocs();
65*7c478bd9Sstevel@tonic-gate static int set_fxprocs(idtype_t, int, char **, uint_t, pri_t, pri_t, long,
66*7c478bd9Sstevel@tonic-gate long);
67*7c478bd9Sstevel@tonic-gate static void exec_fxcmd(char **, uint_t, pri_t, pri_t, long, long);
68*7c478bd9Sstevel@tonic-gate static int fx_priocntl(idtype_t, id_t, int, char *, uintptr_t *);
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate static char usage[] =
71*7c478bd9Sstevel@tonic-gate "usage: priocntl -l\n\
72*7c478bd9Sstevel@tonic-gate priocntl -d [-d idtype] [idlist]\n\
73*7c478bd9Sstevel@tonic-gate priocntl -s [-c FX] [-m fxuprilim] [-p fxupri] [-t tqntm [-r res]] \
74*7c478bd9Sstevel@tonic-gate [-i idtype] [idlist]\n\
75*7c478bd9Sstevel@tonic-gate priocntl -e [-c FX] [-m fxuprilim] [-p fxupri] [-t tqntm [-r res]] \
76*7c478bd9Sstevel@tonic-gate command [argument(s)]\n";
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gate static char cmdpath[MAXPATHLEN];
79*7c478bd9Sstevel@tonic-gate static char basenm[BASENMSZ];
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])82*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate extern char *optarg;
85*7c478bd9Sstevel@tonic-gate extern int optind;
86*7c478bd9Sstevel@tonic-gate
87*7c478bd9Sstevel@tonic-gate int c;
88*7c478bd9Sstevel@tonic-gate int lflag, dflag, sflag, mflag, pflag, eflag, iflag, tflag;
89*7c478bd9Sstevel@tonic-gate int rflag;
90*7c478bd9Sstevel@tonic-gate pri_t fxuprilim;
91*7c478bd9Sstevel@tonic-gate pri_t fxupri;
92*7c478bd9Sstevel@tonic-gate char *idtypnm;
93*7c478bd9Sstevel@tonic-gate idtype_t idtype;
94*7c478bd9Sstevel@tonic-gate int idargc;
95*7c478bd9Sstevel@tonic-gate long res;
96*7c478bd9Sstevel@tonic-gate long tqntm;
97*7c478bd9Sstevel@tonic-gate uint_t cflags;
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate (void) strlcpy(cmdpath, argv[0], MAXPATHLEN);
100*7c478bd9Sstevel@tonic-gate (void) strlcpy(basenm, basename(argv[0]), BASENMSZ);
101*7c478bd9Sstevel@tonic-gate lflag = dflag = sflag = mflag = pflag = eflag = iflag = tflag = 0;
102*7c478bd9Sstevel@tonic-gate rflag = 0;
103*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "ldsm:p:ec:i:t:r:")) != -1) {
104*7c478bd9Sstevel@tonic-gate switch (c) {
105*7c478bd9Sstevel@tonic-gate
106*7c478bd9Sstevel@tonic-gate case 'l':
107*7c478bd9Sstevel@tonic-gate lflag++;
108*7c478bd9Sstevel@tonic-gate break;
109*7c478bd9Sstevel@tonic-gate
110*7c478bd9Sstevel@tonic-gate case 'd':
111*7c478bd9Sstevel@tonic-gate dflag++;
112*7c478bd9Sstevel@tonic-gate break;
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate case 's':
115*7c478bd9Sstevel@tonic-gate sflag++;
116*7c478bd9Sstevel@tonic-gate break;
117*7c478bd9Sstevel@tonic-gate
118*7c478bd9Sstevel@tonic-gate case 'm':
119*7c478bd9Sstevel@tonic-gate mflag++;
120*7c478bd9Sstevel@tonic-gate fxuprilim = (pri_t)str2num(optarg, SHRT_MIN, SHRT_MAX);
121*7c478bd9Sstevel@tonic-gate if (errno)
122*7c478bd9Sstevel@tonic-gate fatalerr("%s: Specified user priority limit %s"
123*7c478bd9Sstevel@tonic-gate " out of configured range\n",
124*7c478bd9Sstevel@tonic-gate basenm, optarg);
125*7c478bd9Sstevel@tonic-gate break;
126*7c478bd9Sstevel@tonic-gate
127*7c478bd9Sstevel@tonic-gate case 'p':
128*7c478bd9Sstevel@tonic-gate pflag++;
129*7c478bd9Sstevel@tonic-gate fxupri = (pri_t)str2num(optarg, SHRT_MIN, SHRT_MAX);
130*7c478bd9Sstevel@tonic-gate if (errno)
131*7c478bd9Sstevel@tonic-gate fatalerr("%s: Specified user priority %s out of"
132*7c478bd9Sstevel@tonic-gate " configured range\n", basenm, optarg);
133*7c478bd9Sstevel@tonic-gate break;
134*7c478bd9Sstevel@tonic-gate
135*7c478bd9Sstevel@tonic-gate case 'e':
136*7c478bd9Sstevel@tonic-gate eflag++;
137*7c478bd9Sstevel@tonic-gate break;
138*7c478bd9Sstevel@tonic-gate
139*7c478bd9Sstevel@tonic-gate case 'c':
140*7c478bd9Sstevel@tonic-gate if (strcmp(optarg, "FX") != 0)
141*7c478bd9Sstevel@tonic-gate fatalerr("error: %s executed for %s class, %s"
142*7c478bd9Sstevel@tonic-gate " is actually sub-command for FX class\n",
143*7c478bd9Sstevel@tonic-gate cmdpath, optarg, cmdpath);
144*7c478bd9Sstevel@tonic-gate break;
145*7c478bd9Sstevel@tonic-gate
146*7c478bd9Sstevel@tonic-gate case 'i':
147*7c478bd9Sstevel@tonic-gate iflag++;
148*7c478bd9Sstevel@tonic-gate idtypnm = optarg;
149*7c478bd9Sstevel@tonic-gate break;
150*7c478bd9Sstevel@tonic-gate case 't':
151*7c478bd9Sstevel@tonic-gate tflag++;
152*7c478bd9Sstevel@tonic-gate tqntm = str2num(optarg, 1, INT_MAX);
153*7c478bd9Sstevel@tonic-gate if (errno)
154*7c478bd9Sstevel@tonic-gate fatalerr("%s: Invalid time quantum specified;"
155*7c478bd9Sstevel@tonic-gate " time quantum must be positive\n", basenm);
156*7c478bd9Sstevel@tonic-gate break;
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate case 'r':
159*7c478bd9Sstevel@tonic-gate rflag++;
160*7c478bd9Sstevel@tonic-gate res = str2num(optarg, 1, 1000000000);
161*7c478bd9Sstevel@tonic-gate if (errno)
162*7c478bd9Sstevel@tonic-gate fatalerr("%s: Invalid resolution specified;"
163*7c478bd9Sstevel@tonic-gate " resolution must be between"
164*7c478bd9Sstevel@tonic-gate " 1 and 1,000,000,000\n", basenm);
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate break;
167*7c478bd9Sstevel@tonic-gate
168*7c478bd9Sstevel@tonic-gate case '?':
169*7c478bd9Sstevel@tonic-gate fatalerr(usage);
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate default:
172*7c478bd9Sstevel@tonic-gate break;
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate
176*7c478bd9Sstevel@tonic-gate if (lflag) {
177*7c478bd9Sstevel@tonic-gate if (dflag || sflag || mflag || pflag || eflag || iflag ||
178*7c478bd9Sstevel@tonic-gate tflag || rflag)
179*7c478bd9Sstevel@tonic-gate fatalerr(usage);
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate print_fxinfo();
182*7c478bd9Sstevel@tonic-gate
183*7c478bd9Sstevel@tonic-gate } else if (dflag) {
184*7c478bd9Sstevel@tonic-gate if (sflag || mflag || pflag || eflag)
185*7c478bd9Sstevel@tonic-gate fatalerr(usage);
186*7c478bd9Sstevel@tonic-gate
187*7c478bd9Sstevel@tonic-gate return (print_fxprocs());
188*7c478bd9Sstevel@tonic-gate
189*7c478bd9Sstevel@tonic-gate } else if (sflag) {
190*7c478bd9Sstevel@tonic-gate if (eflag)
191*7c478bd9Sstevel@tonic-gate fatalerr(usage);
192*7c478bd9Sstevel@tonic-gate
193*7c478bd9Sstevel@tonic-gate if (iflag) {
194*7c478bd9Sstevel@tonic-gate if (str2idtyp(idtypnm, &idtype) == -1)
195*7c478bd9Sstevel@tonic-gate fatalerr("%s: Bad idtype %s\n", basenm,
196*7c478bd9Sstevel@tonic-gate idtypnm);
197*7c478bd9Sstevel@tonic-gate } else
198*7c478bd9Sstevel@tonic-gate idtype = P_PID;
199*7c478bd9Sstevel@tonic-gate
200*7c478bd9Sstevel@tonic-gate cflags = (pflag ? FX_DOUPRI : 0);
201*7c478bd9Sstevel@tonic-gate
202*7c478bd9Sstevel@tonic-gate if (mflag)
203*7c478bd9Sstevel@tonic-gate cflags |= FX_DOUPRILIM;
204*7c478bd9Sstevel@tonic-gate
205*7c478bd9Sstevel@tonic-gate if (tflag)
206*7c478bd9Sstevel@tonic-gate cflags |= FX_DOTQ;
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate if (rflag == 0)
209*7c478bd9Sstevel@tonic-gate res = 1000;
210*7c478bd9Sstevel@tonic-gate
211*7c478bd9Sstevel@tonic-gate if (optind < argc)
212*7c478bd9Sstevel@tonic-gate idargc = argc - optind;
213*7c478bd9Sstevel@tonic-gate else
214*7c478bd9Sstevel@tonic-gate idargc = 0;
215*7c478bd9Sstevel@tonic-gate
216*7c478bd9Sstevel@tonic-gate return (set_fxprocs(idtype, idargc, &argv[optind], cflags,
217*7c478bd9Sstevel@tonic-gate fxuprilim, fxupri, tqntm, res));
218*7c478bd9Sstevel@tonic-gate
219*7c478bd9Sstevel@tonic-gate } else if (eflag) {
220*7c478bd9Sstevel@tonic-gate if (iflag)
221*7c478bd9Sstevel@tonic-gate fatalerr(usage);
222*7c478bd9Sstevel@tonic-gate
223*7c478bd9Sstevel@tonic-gate cflags = (pflag ? FX_DOUPRI : 0);
224*7c478bd9Sstevel@tonic-gate
225*7c478bd9Sstevel@tonic-gate if (mflag)
226*7c478bd9Sstevel@tonic-gate cflags |= FX_DOUPRILIM;
227*7c478bd9Sstevel@tonic-gate
228*7c478bd9Sstevel@tonic-gate if (tflag)
229*7c478bd9Sstevel@tonic-gate cflags |= FX_DOTQ;
230*7c478bd9Sstevel@tonic-gate
231*7c478bd9Sstevel@tonic-gate if (rflag == 0)
232*7c478bd9Sstevel@tonic-gate res = 1000;
233*7c478bd9Sstevel@tonic-gate
234*7c478bd9Sstevel@tonic-gate exec_fxcmd(&argv[optind], cflags, fxuprilim, fxupri, tqntm,
235*7c478bd9Sstevel@tonic-gate res);
236*7c478bd9Sstevel@tonic-gate
237*7c478bd9Sstevel@tonic-gate } else {
238*7c478bd9Sstevel@tonic-gate fatalerr(usage);
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate return (0);
241*7c478bd9Sstevel@tonic-gate }
242*7c478bd9Sstevel@tonic-gate
243*7c478bd9Sstevel@tonic-gate
244*7c478bd9Sstevel@tonic-gate /*
245*7c478bd9Sstevel@tonic-gate * Print our class name and the configured user priority range.
246*7c478bd9Sstevel@tonic-gate */
247*7c478bd9Sstevel@tonic-gate static void
print_fxinfo()248*7c478bd9Sstevel@tonic-gate print_fxinfo()
249*7c478bd9Sstevel@tonic-gate {
250*7c478bd9Sstevel@tonic-gate pcinfo_t pcinfo;
251*7c478bd9Sstevel@tonic-gate
252*7c478bd9Sstevel@tonic-gate (void) strcpy(pcinfo.pc_clname, "FX");
253*7c478bd9Sstevel@tonic-gate
254*7c478bd9Sstevel@tonic-gate (void) printf("FX (Fixed priority)\n");
255*7c478bd9Sstevel@tonic-gate
256*7c478bd9Sstevel@tonic-gate if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
257*7c478bd9Sstevel@tonic-gate fatalerr("\tCan't get configured FX user priority range\n");
258*7c478bd9Sstevel@tonic-gate
259*7c478bd9Sstevel@tonic-gate (void) printf("\tConfigured FX User Priority Range: 0 through %d\n",
260*7c478bd9Sstevel@tonic-gate ((fxinfo_t *)pcinfo.pc_clinfo)->fx_maxupri);
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate
263*7c478bd9Sstevel@tonic-gate
264*7c478bd9Sstevel@tonic-gate /*
265*7c478bd9Sstevel@tonic-gate * Read a list of pids from stdin and print the user priority and user
266*7c478bd9Sstevel@tonic-gate * priority limit for each of the corresponding processes.
267*7c478bd9Sstevel@tonic-gate */
268*7c478bd9Sstevel@tonic-gate static int
print_fxprocs()269*7c478bd9Sstevel@tonic-gate print_fxprocs()
270*7c478bd9Sstevel@tonic-gate {
271*7c478bd9Sstevel@tonic-gate pid_t *pidlist;
272*7c478bd9Sstevel@tonic-gate size_t numread;
273*7c478bd9Sstevel@tonic-gate int i;
274*7c478bd9Sstevel@tonic-gate char clname[PC_CLNMSZ];
275*7c478bd9Sstevel@tonic-gate uint_t fx_tqsecs;
276*7c478bd9Sstevel@tonic-gate int fx_tqnsecs;
277*7c478bd9Sstevel@tonic-gate pri_t fx_uprilim;
278*7c478bd9Sstevel@tonic-gate pri_t fx_upri;
279*7c478bd9Sstevel@tonic-gate int error = 0;
280*7c478bd9Sstevel@tonic-gate
281*7c478bd9Sstevel@tonic-gate
282*7c478bd9Sstevel@tonic-gate /*
283*7c478bd9Sstevel@tonic-gate * Read a list of pids from stdin.
284*7c478bd9Sstevel@tonic-gate */
285*7c478bd9Sstevel@tonic-gate if ((pidlist = read_pidlist(&numread, stdin)) == NULL)
286*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't read pidlist.\n", basenm);
287*7c478bd9Sstevel@tonic-gate
288*7c478bd9Sstevel@tonic-gate (void) printf("FIXED PRIORITY PROCESSES:\n PID FXUPRILIM "
289*7c478bd9Sstevel@tonic-gate "FXUPRI FXTQNTM\n");
290*7c478bd9Sstevel@tonic-gate
291*7c478bd9Sstevel@tonic-gate if (numread == 0)
292*7c478bd9Sstevel@tonic-gate fatalerr("%s: No pids on input\n", basenm);
293*7c478bd9Sstevel@tonic-gate
294*7c478bd9Sstevel@tonic-gate
295*7c478bd9Sstevel@tonic-gate for (i = 0; i < numread; i++) {
296*7c478bd9Sstevel@tonic-gate (void) printf("%7ld", pidlist[i]);
297*7c478bd9Sstevel@tonic-gate if (priocntl(P_PID, pidlist[i], PC_GETXPARMS, "FX",
298*7c478bd9Sstevel@tonic-gate FX_KY_UPRI, &fx_upri, FX_KY_UPRILIM, &fx_uprilim,
299*7c478bd9Sstevel@tonic-gate FX_KY_TQSECS, &fx_tqsecs, FX_KY_TQNSECS,
300*7c478bd9Sstevel@tonic-gate &fx_tqnsecs, 0) != -1) {
301*7c478bd9Sstevel@tonic-gate (void) printf(" %5d %5d", fx_uprilim, fx_upri);
302*7c478bd9Sstevel@tonic-gate
303*7c478bd9Sstevel@tonic-gate if (fx_tqnsecs == FX_TQINF)
304*7c478bd9Sstevel@tonic-gate (void) printf(" FX_TQINF\n");
305*7c478bd9Sstevel@tonic-gate else
306*7c478bd9Sstevel@tonic-gate (void) printf(" %11lld\n",
307*7c478bd9Sstevel@tonic-gate (longlong_t)fx_tqsecs * 1000 +
308*7c478bd9Sstevel@tonic-gate fx_tqnsecs / 1000000);
309*7c478bd9Sstevel@tonic-gate
310*7c478bd9Sstevel@tonic-gate } else {
311*7c478bd9Sstevel@tonic-gate error = 1;
312*7c478bd9Sstevel@tonic-gate
313*7c478bd9Sstevel@tonic-gate if (priocntl(P_PID, pidlist[i], PC_GETXPARMS, NULL,
314*7c478bd9Sstevel@tonic-gate PC_KY_CLNAME, clname, 0) != -1 &&
315*7c478bd9Sstevel@tonic-gate strcmp(clname, "FX")) {
316*7c478bd9Sstevel@tonic-gate /*
317*7c478bd9Sstevel@tonic-gate * Process from some class other than fixed priority.
318*7c478bd9Sstevel@tonic-gate * It has probably changed class while priocntl
319*7c478bd9Sstevel@tonic-gate * command was executing (otherwise we wouldn't
320*7c478bd9Sstevel@tonic-gate * have been passed its pid). Print the little
321*7c478bd9Sstevel@tonic-gate * we know about it.
322*7c478bd9Sstevel@tonic-gate */
323*7c478bd9Sstevel@tonic-gate (void) printf("\tChanged to class %s while priocntl"
324*7c478bd9Sstevel@tonic-gate " command executing\n", clname);
325*7c478bd9Sstevel@tonic-gate } else {
326*7c478bd9Sstevel@tonic-gate (void) printf("\tCan't get FX user priority\n");
327*7c478bd9Sstevel@tonic-gate }
328*7c478bd9Sstevel@tonic-gate }
329*7c478bd9Sstevel@tonic-gate }
330*7c478bd9Sstevel@tonic-gate
331*7c478bd9Sstevel@tonic-gate free_pidlist(pidlist);
332*7c478bd9Sstevel@tonic-gate return (error);
333*7c478bd9Sstevel@tonic-gate }
334*7c478bd9Sstevel@tonic-gate
335*7c478bd9Sstevel@tonic-gate /*
336*7c478bd9Sstevel@tonic-gate * Call priocntl() with command codes PC_SETXPARMS or PC_GETXPARMS.
337*7c478bd9Sstevel@tonic-gate * The first parameter behind the command code is always the class name.
338*7c478bd9Sstevel@tonic-gate * Each parameter is headed by a key, which determines the meaning of the
339*7c478bd9Sstevel@tonic-gate * following value. There are maximal FX_KEYCNT = 4 (key, value) pairs.
340*7c478bd9Sstevel@tonic-gate */
341*7c478bd9Sstevel@tonic-gate static int
fx_priocntl(idtype_t idtype,id_t id,int cmd,char * clname,uintptr_t * argsp)342*7c478bd9Sstevel@tonic-gate fx_priocntl(idtype_t idtype, id_t id, int cmd, char *clname, uintptr_t *argsp)
343*7c478bd9Sstevel@tonic-gate {
344*7c478bd9Sstevel@tonic-gate return (priocntl(idtype, id, cmd, clname, argsp[0], argsp[1],
345*7c478bd9Sstevel@tonic-gate argsp[2], argsp[3], argsp[4], argsp[5], argsp[6], argsp[7], 0));
346*7c478bd9Sstevel@tonic-gate }
347*7c478bd9Sstevel@tonic-gate
348*7c478bd9Sstevel@tonic-gate /*
349*7c478bd9Sstevel@tonic-gate * Set all processes in the set specified by idtype/idargv to fixed-priority
350*7c478bd9Sstevel@tonic-gate * (if they aren't already fixed-priority) and set their user priority limit
351*7c478bd9Sstevel@tonic-gate * and user priority to those specified by fxuprilim and fxupri.
352*7c478bd9Sstevel@tonic-gate */
353*7c478bd9Sstevel@tonic-gate static int
set_fxprocs(idtype_t idtype,int idargc,char ** idargv,uint_t cflags,pri_t fxuprilim,pri_t fxupri,long tqntm,long res)354*7c478bd9Sstevel@tonic-gate set_fxprocs(idtype_t idtype, int idargc, char **idargv, uint_t cflags,
355*7c478bd9Sstevel@tonic-gate pri_t fxuprilim, pri_t fxupri, long tqntm, long res)
356*7c478bd9Sstevel@tonic-gate {
357*7c478bd9Sstevel@tonic-gate pcinfo_t pcinfo;
358*7c478bd9Sstevel@tonic-gate uintptr_t args[2*FX_KEYCNT+1];
359*7c478bd9Sstevel@tonic-gate uintptr_t *argsp = &args[0];
360*7c478bd9Sstevel@tonic-gate pri_t maxupri;
361*7c478bd9Sstevel@tonic-gate char idtypnm[PC_IDTYPNMSZ];
362*7c478bd9Sstevel@tonic-gate int i;
363*7c478bd9Sstevel@tonic-gate id_t id;
364*7c478bd9Sstevel@tonic-gate hrtimer_t hrtime;
365*7c478bd9Sstevel@tonic-gate int error = 0;
366*7c478bd9Sstevel@tonic-gate
367*7c478bd9Sstevel@tonic-gate /*
368*7c478bd9Sstevel@tonic-gate * Get the fixed priority class ID and max configured user priority.
369*7c478bd9Sstevel@tonic-gate */
370*7c478bd9Sstevel@tonic-gate (void) strcpy(pcinfo.pc_clname, "FX");
371*7c478bd9Sstevel@tonic-gate if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
372*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't get FX class ID, priocntl system call"
373*7c478bd9Sstevel@tonic-gate " failed with errno %d\n", basenm, errno);
374*7c478bd9Sstevel@tonic-gate maxupri = ((fxinfo_t *)pcinfo.pc_clinfo)->fx_maxupri;
375*7c478bd9Sstevel@tonic-gate
376*7c478bd9Sstevel@tonic-gate /*
377*7c478bd9Sstevel@tonic-gate * Validate the fxuprilim and fxupri arguments.
378*7c478bd9Sstevel@tonic-gate */
379*7c478bd9Sstevel@tonic-gate if ((cflags & FX_DOUPRILIM) != 0) {
380*7c478bd9Sstevel@tonic-gate if (fxuprilim > maxupri || fxuprilim < 0) {
381*7c478bd9Sstevel@tonic-gate fatalerr("%s: Specified user priority limit %d out of"
382*7c478bd9Sstevel@tonic-gate " configured range\n", basenm, fxuprilim);
383*7c478bd9Sstevel@tonic-gate }
384*7c478bd9Sstevel@tonic-gate ADDKEYVAL(argsp, FX_KY_UPRILIM, fxuprilim);
385*7c478bd9Sstevel@tonic-gate }
386*7c478bd9Sstevel@tonic-gate
387*7c478bd9Sstevel@tonic-gate if ((cflags & FX_DOUPRI) != 0) {
388*7c478bd9Sstevel@tonic-gate if (fxupri > maxupri || fxupri < 0)
389*7c478bd9Sstevel@tonic-gate fatalerr("%s: Specified user priority %d out of "
390*7c478bd9Sstevel@tonic-gate "configured range\n", basenm, fxupri);
391*7c478bd9Sstevel@tonic-gate ADDKEYVAL(argsp, FX_KY_UPRI, fxupri);
392*7c478bd9Sstevel@tonic-gate
393*7c478bd9Sstevel@tonic-gate }
394*7c478bd9Sstevel@tonic-gate
395*7c478bd9Sstevel@tonic-gate if (cflags & FX_DOTQ) {
396*7c478bd9Sstevel@tonic-gate hrtime.hrt_secs = 0;
397*7c478bd9Sstevel@tonic-gate hrtime.hrt_rem = tqntm;
398*7c478bd9Sstevel@tonic-gate hrtime.hrt_res = res;
399*7c478bd9Sstevel@tonic-gate if (_hrtnewres(&hrtime, NANOSEC, HRT_RNDUP) == -1)
400*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't convert resolution.\n", basenm);
401*7c478bd9Sstevel@tonic-gate ADDKEYVAL(argsp, FX_KY_TQSECS, hrtime.hrt_secs);
402*7c478bd9Sstevel@tonic-gate ADDKEYVAL(argsp, FX_KY_TQNSECS, hrtime.hrt_rem);
403*7c478bd9Sstevel@tonic-gate }
404*7c478bd9Sstevel@tonic-gate
405*7c478bd9Sstevel@tonic-gate *argsp = 0;
406*7c478bd9Sstevel@tonic-gate
407*7c478bd9Sstevel@tonic-gate if (idtype == P_ALL) {
408*7c478bd9Sstevel@tonic-gate if (fx_priocntl(P_ALL, 0, PC_SETXPARMS, "FX", args) == -1) {
409*7c478bd9Sstevel@tonic-gate if (errno == EPERM) {
410*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
411*7c478bd9Sstevel@tonic-gate "Permissions error encountered "
412*7c478bd9Sstevel@tonic-gate "on one or more processes.\n");
413*7c478bd9Sstevel@tonic-gate error = 1;
414*7c478bd9Sstevel@tonic-gate } else {
415*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't reset fixed priority"
416*7c478bd9Sstevel@tonic-gate " parameters\npriocntl system call failed "
417*7c478bd9Sstevel@tonic-gate " with errno %d\n", basenm, errno);
418*7c478bd9Sstevel@tonic-gate }
419*7c478bd9Sstevel@tonic-gate } else if ((cflags & (FX_DOUPRILIM|FX_DOUPRI)) == FX_DOUPRI) {
420*7c478bd9Sstevel@tonic-gate (void) verifyupri(idtype, 0, "FX", FX_KY_UPRILIM,
421*7c478bd9Sstevel@tonic-gate fxupri, basenm);
422*7c478bd9Sstevel@tonic-gate }
423*7c478bd9Sstevel@tonic-gate } else if (idargc == 0) {
424*7c478bd9Sstevel@tonic-gate if (fx_priocntl(idtype, P_MYID, PC_SETXPARMS, "FX",
425*7c478bd9Sstevel@tonic-gate args) == -1) {
426*7c478bd9Sstevel@tonic-gate if (errno == EPERM) {
427*7c478bd9Sstevel@tonic-gate (void) idtyp2str(idtype, idtypnm);
428*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Permissions error"
429*7c478bd9Sstevel@tonic-gate " encountered on current %s.\n", idtypnm);
430*7c478bd9Sstevel@tonic-gate error = 1;
431*7c478bd9Sstevel@tonic-gate } else {
432*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't reset fixed priority"
433*7c478bd9Sstevel@tonic-gate " parameters\npriocntl system call failed"
434*7c478bd9Sstevel@tonic-gate " with errno %d\n", basenm, errno);
435*7c478bd9Sstevel@tonic-gate }
436*7c478bd9Sstevel@tonic-gate } else if ((cflags & (FX_DOUPRILIM|FX_DOUPRI)) == FX_DOUPRI &&
437*7c478bd9Sstevel@tonic-gate getmyid(idtype, &id) != -1) {
438*7c478bd9Sstevel@tonic-gate (void) verifyupri(idtype, id, "FX", FX_KY_UPRILIM,
439*7c478bd9Sstevel@tonic-gate fxupri, basenm);
440*7c478bd9Sstevel@tonic-gate }
441*7c478bd9Sstevel@tonic-gate } else {
442*7c478bd9Sstevel@tonic-gate (void) idtyp2str(idtype, idtypnm);
443*7c478bd9Sstevel@tonic-gate for (i = 0; i < idargc; i++) {
444*7c478bd9Sstevel@tonic-gate if (idtype == P_CID) {
445*7c478bd9Sstevel@tonic-gate (void) strcpy(pcinfo.pc_clname, idargv[i]);
446*7c478bd9Sstevel@tonic-gate if (priocntl(0, 0, PC_GETCID,
447*7c478bd9Sstevel@tonic-gate (caddr_t)&pcinfo) == -1)
448*7c478bd9Sstevel@tonic-gate fatalerr("%s: Invalid or unconfigured"
449*7c478bd9Sstevel@tonic-gate " class %s, priocntl system call"
450*7c478bd9Sstevel@tonic-gate " failed with errno %d\n",
451*7c478bd9Sstevel@tonic-gate basenm, pcinfo.pc_clname, errno);
452*7c478bd9Sstevel@tonic-gate id = pcinfo.pc_cid;
453*7c478bd9Sstevel@tonic-gate } else {
454*7c478bd9Sstevel@tonic-gate id = (id_t)str2num(idargv[i], INT_MIN, INT_MAX);
455*7c478bd9Sstevel@tonic-gate }
456*7c478bd9Sstevel@tonic-gate
457*7c478bd9Sstevel@tonic-gate if (fx_priocntl(idtype, id, PC_SETXPARMS, "FX", args)
458*7c478bd9Sstevel@tonic-gate == -1) {
459*7c478bd9Sstevel@tonic-gate if (errno == EPERM) {
460*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
461*7c478bd9Sstevel@tonic-gate "Permissions error encountered on"
462*7c478bd9Sstevel@tonic-gate " %s %s.\n", idtypnm, idargv[i]);
463*7c478bd9Sstevel@tonic-gate error = 1;
464*7c478bd9Sstevel@tonic-gate } else {
465*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't reset fixed "
466*7c478bd9Sstevel@tonic-gate "priority"
467*7c478bd9Sstevel@tonic-gate " parameters\npriocntl system call"
468*7c478bd9Sstevel@tonic-gate " failed with errno %d\n",
469*7c478bd9Sstevel@tonic-gate basenm, errno);
470*7c478bd9Sstevel@tonic-gate }
471*7c478bd9Sstevel@tonic-gate } else if ((cflags & (FX_DOUPRILIM|FX_DOUPRI)) ==
472*7c478bd9Sstevel@tonic-gate FX_DOUPRI) {
473*7c478bd9Sstevel@tonic-gate (void) verifyupri(idtype, id, "FX",
474*7c478bd9Sstevel@tonic-gate FX_KY_UPRILIM, fxupri, basenm);
475*7c478bd9Sstevel@tonic-gate }
476*7c478bd9Sstevel@tonic-gate }
477*7c478bd9Sstevel@tonic-gate }
478*7c478bd9Sstevel@tonic-gate return (error);
479*7c478bd9Sstevel@tonic-gate
480*7c478bd9Sstevel@tonic-gate }
481*7c478bd9Sstevel@tonic-gate
482*7c478bd9Sstevel@tonic-gate
483*7c478bd9Sstevel@tonic-gate /*
484*7c478bd9Sstevel@tonic-gate * Execute the command pointed to by cmdargv as a fixed-priority process
485*7c478bd9Sstevel@tonic-gate * with the user priority limit given by fxuprilim and user priority fxupri.
486*7c478bd9Sstevel@tonic-gate */
487*7c478bd9Sstevel@tonic-gate static void
exec_fxcmd(char ** cmdargv,uint_t cflags,pri_t fxuprilim,pri_t fxupri,long tqntm,long res)488*7c478bd9Sstevel@tonic-gate exec_fxcmd(char **cmdargv, uint_t cflags, pri_t fxuprilim, pri_t fxupri,
489*7c478bd9Sstevel@tonic-gate long tqntm, long res)
490*7c478bd9Sstevel@tonic-gate {
491*7c478bd9Sstevel@tonic-gate pcinfo_t pcinfo;
492*7c478bd9Sstevel@tonic-gate uintptr_t args[2*FX_KEYCNT+1];
493*7c478bd9Sstevel@tonic-gate uintptr_t *argsp = &args[0];
494*7c478bd9Sstevel@tonic-gate
495*7c478bd9Sstevel@tonic-gate pri_t maxupri;
496*7c478bd9Sstevel@tonic-gate pri_t uprilim;
497*7c478bd9Sstevel@tonic-gate hrtimer_t hrtime;
498*7c478bd9Sstevel@tonic-gate
499*7c478bd9Sstevel@tonic-gate /*
500*7c478bd9Sstevel@tonic-gate * Get the fixed priority class ID and max configured user priority.
501*7c478bd9Sstevel@tonic-gate */
502*7c478bd9Sstevel@tonic-gate (void) strcpy(pcinfo.pc_clname, "FX");
503*7c478bd9Sstevel@tonic-gate if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
504*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't get FX class ID, priocntl system call"
505*7c478bd9Sstevel@tonic-gate " failed with errno %d\n", basenm, errno);
506*7c478bd9Sstevel@tonic-gate maxupri = ((fxinfo_t *)pcinfo.pc_clinfo)->fx_maxupri;
507*7c478bd9Sstevel@tonic-gate
508*7c478bd9Sstevel@tonic-gate if ((cflags & FX_DOUPRILIM) != 0) {
509*7c478bd9Sstevel@tonic-gate if (fxuprilim > maxupri || fxuprilim < 0)
510*7c478bd9Sstevel@tonic-gate fatalerr("%s: Specified user priority limit %d out of"
511*7c478bd9Sstevel@tonic-gate " configured range\n", basenm, fxuprilim);
512*7c478bd9Sstevel@tonic-gate ADDKEYVAL(argsp, FX_KY_UPRILIM, fxuprilim);
513*7c478bd9Sstevel@tonic-gate }
514*7c478bd9Sstevel@tonic-gate
515*7c478bd9Sstevel@tonic-gate if ((cflags & FX_DOUPRI) != 0) {
516*7c478bd9Sstevel@tonic-gate if (fxupri > maxupri || fxupri < 0)
517*7c478bd9Sstevel@tonic-gate fatalerr("%s: Specified user priority %d out of"
518*7c478bd9Sstevel@tonic-gate " configured range\n", basenm, fxupri);
519*7c478bd9Sstevel@tonic-gate ADDKEYVAL(argsp, FX_KY_UPRI, fxupri);
520*7c478bd9Sstevel@tonic-gate }
521*7c478bd9Sstevel@tonic-gate
522*7c478bd9Sstevel@tonic-gate if ((cflags & FX_DOTQ) != 0) {
523*7c478bd9Sstevel@tonic-gate hrtime.hrt_secs = 0;
524*7c478bd9Sstevel@tonic-gate hrtime.hrt_rem = tqntm;
525*7c478bd9Sstevel@tonic-gate hrtime.hrt_res = res;
526*7c478bd9Sstevel@tonic-gate if (_hrtnewres(&hrtime, NANOSEC, HRT_RNDUP) == -1)
527*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't convert resolution.\n", basenm);
528*7c478bd9Sstevel@tonic-gate ADDKEYVAL(argsp, FX_KY_TQSECS, hrtime.hrt_secs);
529*7c478bd9Sstevel@tonic-gate ADDKEYVAL(argsp, FX_KY_TQNSECS, hrtime.hrt_rem);
530*7c478bd9Sstevel@tonic-gate
531*7c478bd9Sstevel@tonic-gate }
532*7c478bd9Sstevel@tonic-gate *argsp = 0;
533*7c478bd9Sstevel@tonic-gate if (fx_priocntl(P_PID, P_MYID, PC_SETXPARMS, "FX", args) == -1)
534*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't reset fixed priority parameters\n"
535*7c478bd9Sstevel@tonic-gate " priocntl system call failed with errno %d\n",
536*7c478bd9Sstevel@tonic-gate basenm, errno);
537*7c478bd9Sstevel@tonic-gate
538*7c478bd9Sstevel@tonic-gate if ((cflags & (FX_DOUPRILIM|FX_DOUPRI)) == FX_DOUPRI) {
539*7c478bd9Sstevel@tonic-gate if (priocntl(P_PID, P_MYID, PC_GETXPARMS, "FX",
540*7c478bd9Sstevel@tonic-gate FX_KY_UPRILIM, &uprilim, 0) != -1 && fxupri > uprilim)
541*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
542*7c478bd9Sstevel@tonic-gate "%s: Specified user priority %d exceeds"
543*7c478bd9Sstevel@tonic-gate " limit %d; set to %d (pid %d)\n",
544*7c478bd9Sstevel@tonic-gate basenm, fxupri, uprilim, uprilim, (int)getpid());
545*7c478bd9Sstevel@tonic-gate }
546*7c478bd9Sstevel@tonic-gate
547*7c478bd9Sstevel@tonic-gate (void) execvp(cmdargv[0], cmdargv);
548*7c478bd9Sstevel@tonic-gate fatalerr("%s: Can't execute %s, exec failed with errno %d\n",
549*7c478bd9Sstevel@tonic-gate basenm, cmdargv[0], errno);
550*7c478bd9Sstevel@tonic-gate }
551