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