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