xref: /illumos-gate/usr/src/cmd/priocntl/rtpriocntl.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*d4204c85Sraf  * Common Development and Distribution License (the "License").
6*d4204c85Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*d4204c85Sraf 
227c478bd9Sstevel@tonic-gate /*
23*d4204c85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include	<stdio.h>
327c478bd9Sstevel@tonic-gate #include	<string.h>
337c478bd9Sstevel@tonic-gate #include	<stdlib.h>
347c478bd9Sstevel@tonic-gate #include	<unistd.h>
357c478bd9Sstevel@tonic-gate #include	<sys/types.h>
367c478bd9Sstevel@tonic-gate #include	<sys/time.h>
377c478bd9Sstevel@tonic-gate #include	<sys/procset.h>
387c478bd9Sstevel@tonic-gate #include	<sys/priocntl.h>
397c478bd9Sstevel@tonic-gate #include	<sys/rtpriocntl.h>
407c478bd9Sstevel@tonic-gate #include	<sys/param.h>
417c478bd9Sstevel@tonic-gate #include	<signal.h>
427c478bd9Sstevel@tonic-gate #include	<libgen.h>
437c478bd9Sstevel@tonic-gate #include	<limits.h>
447c478bd9Sstevel@tonic-gate #include	<errno.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include	"priocntl.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * This file contains the class specific code implementing
507c478bd9Sstevel@tonic-gate  * the real-time priocntl sub-command.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	ADDKEYVAL(p, k, v) { (p[0]) = (k); (p[1]) = (v); p += 2; }
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	RT_KEYCNT	4	/* maximal number of (key, value) pairs */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * control flags
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate #define	RT_DOPRI	0x01	/* change priority */
617c478bd9Sstevel@tonic-gate #define	RT_DOTQ		0x02	/* change RT time quantum */
627c478bd9Sstevel@tonic-gate #define	RT_DOSIG	0x10	/* change RT time quantum signal */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static void	print_rtinfo(void);
667c478bd9Sstevel@tonic-gate static int	print_rtprocs(void);
677c478bd9Sstevel@tonic-gate static int	rt_priocntl(idtype_t, id_t, int, char *, uintptr_t *);
687c478bd9Sstevel@tonic-gate static int	set_rtprocs(idtype_t, int, char **, uint_t, pri_t, long,
697c478bd9Sstevel@tonic-gate 			long, int);
707c478bd9Sstevel@tonic-gate static void	exec_rtcmd(char **, uint_t, pri_t, long, long, int);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static char usage[] =
747c478bd9Sstevel@tonic-gate "usage: priocntl -l\n"
757c478bd9Sstevel@tonic-gate "       priocntl -d [-i idtype] [idlist]\n"
767c478bd9Sstevel@tonic-gate "       priocntl -s [-c RT] [-p rtpri] [-t tqntm [-r res]] [-q tqsig]\n"
777c478bd9Sstevel@tonic-gate "                   [-i idtype] [idlist]\n"
787c478bd9Sstevel@tonic-gate "       priocntl -e [-c RT] [-p rtpri] [-t tqntm [-r res]] [-q tqsig]\n"
797c478bd9Sstevel@tonic-gate "                   command [argument(s)]\n";
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static char	cmdpath[MAXPATHLEN];
827c478bd9Sstevel@tonic-gate static char	basenm[BASENMSZ];
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])867c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	int		c;
89*d4204c85Sraf 	int		lflag, dflag, sflag, pflag;
90*d4204c85Sraf 	int		tflag, rflag, eflag, iflag, qflag;
917c478bd9Sstevel@tonic-gate 	pri_t		rtpri;
927c478bd9Sstevel@tonic-gate 	long		tqntm;
937c478bd9Sstevel@tonic-gate 	long		res;
947c478bd9Sstevel@tonic-gate 	int		tqsig;
957c478bd9Sstevel@tonic-gate 	char		*idtypnm;
967c478bd9Sstevel@tonic-gate 	idtype_t	idtype;
977c478bd9Sstevel@tonic-gate 	int		idargc;
987c478bd9Sstevel@tonic-gate 	uint_t		cflags;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	(void) strlcpy(cmdpath, argv[0], MAXPATHLEN);
1017c478bd9Sstevel@tonic-gate 	(void) strlcpy(basenm, basename(argv[0]), BASENMSZ);
102*d4204c85Sraf 	lflag = dflag = sflag = pflag = 0;
103*d4204c85Sraf 	tflag = rflag = eflag = iflag = qflag = 0;
1047c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ldsp:t:r:q:ec:i:")) != -1) {
1057c478bd9Sstevel@tonic-gate 		switch (c) {
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 		case 'l':
1087c478bd9Sstevel@tonic-gate 			lflag++;
1097c478bd9Sstevel@tonic-gate 			break;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		case 'd':
1127c478bd9Sstevel@tonic-gate 			dflag++;
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		case 's':
1167c478bd9Sstevel@tonic-gate 			sflag++;
1177c478bd9Sstevel@tonic-gate 			break;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		case 'p':
1207c478bd9Sstevel@tonic-gate 			pflag++;
1217c478bd9Sstevel@tonic-gate 			rtpri = (pri_t)str2num(optarg, SHRT_MIN, SHRT_MAX);
1227c478bd9Sstevel@tonic-gate 			if (errno)
1237c478bd9Sstevel@tonic-gate 				fatalerr("%s: Specified real time priority %s"
1247c478bd9Sstevel@tonic-gate 				    " out of configured range\n",
1257c478bd9Sstevel@tonic-gate 				    basenm, optarg);
1267c478bd9Sstevel@tonic-gate 			break;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		case 't':
1297c478bd9Sstevel@tonic-gate 			tflag++;
1307c478bd9Sstevel@tonic-gate 			tqntm = str2num(optarg, 1, INT_MAX);
1317c478bd9Sstevel@tonic-gate 			if (errno)
1327c478bd9Sstevel@tonic-gate 				fatalerr("%s: Invalid time quantum specified;"
1337c478bd9Sstevel@tonic-gate 				    " time quantum must be positive\n", basenm);
1347c478bd9Sstevel@tonic-gate 			break;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		case 'r':
1377c478bd9Sstevel@tonic-gate 			rflag++;
1387c478bd9Sstevel@tonic-gate 			res = str2num(optarg, 1, 1000000000);
1397c478bd9Sstevel@tonic-gate 			if (errno)
1407c478bd9Sstevel@tonic-gate 				fatalerr("%s: Invalid resolution specified;"
1417c478bd9Sstevel@tonic-gate 				    " resolution must be between"
1427c478bd9Sstevel@tonic-gate 				    " 1 and 1,000,000,000\n", basenm);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		case 'q':
1477c478bd9Sstevel@tonic-gate 			qflag++;
1487c478bd9Sstevel@tonic-gate 			if (str2sig(optarg, &tqsig) != 0)
1497c478bd9Sstevel@tonic-gate 				fatalerr("%s: Invalid real time quantum signal"
1507c478bd9Sstevel@tonic-gate 				    " specified\n", basenm);
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		case 'e':
1547c478bd9Sstevel@tonic-gate 			eflag++;
1557c478bd9Sstevel@tonic-gate 			break;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		case 'c':
1587c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "RT") != 0)
1597c478bd9Sstevel@tonic-gate 				fatalerr("error: %s executed for %s class, %s"
1607c478bd9Sstevel@tonic-gate 				    " is actually sub-command for RT class\n",
1617c478bd9Sstevel@tonic-gate 				    cmdpath, optarg, cmdpath);
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		case 'i':
1657c478bd9Sstevel@tonic-gate 			iflag++;
1667c478bd9Sstevel@tonic-gate 			idtypnm = optarg;
1677c478bd9Sstevel@tonic-gate 			break;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		case '?':
1707c478bd9Sstevel@tonic-gate 			fatalerr(usage);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		default:
1737c478bd9Sstevel@tonic-gate 			break;
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (lflag) {
1787c478bd9Sstevel@tonic-gate 		if (dflag || sflag || pflag || tflag || rflag || eflag ||
1797c478bd9Sstevel@tonic-gate 		    iflag || qflag)
1807c478bd9Sstevel@tonic-gate 			fatalerr(usage);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		print_rtinfo();
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	} else if (dflag) {
1857c478bd9Sstevel@tonic-gate 		if (lflag || sflag || pflag || tflag || rflag || eflag || qflag)
1867c478bd9Sstevel@tonic-gate 			fatalerr(usage);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		return (print_rtprocs());
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	} else if (sflag) {
1917c478bd9Sstevel@tonic-gate 		if (lflag || dflag || eflag)
1927c478bd9Sstevel@tonic-gate 			fatalerr(usage);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		if (iflag) {
1957c478bd9Sstevel@tonic-gate 			if (str2idtyp(idtypnm, &idtype) == -1)
1967c478bd9Sstevel@tonic-gate 				fatalerr("%s: Bad idtype %s\n", basenm,
1977c478bd9Sstevel@tonic-gate 				    idtypnm);
1987c478bd9Sstevel@tonic-gate 		} else {
1997c478bd9Sstevel@tonic-gate 			idtype = P_PID;
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		cflags = (pflag ? RT_DOPRI : 0);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		if (tflag)
2057c478bd9Sstevel@tonic-gate 			cflags |= RT_DOTQ;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		if (rflag == 0)
2087c478bd9Sstevel@tonic-gate 			res = 1000;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		if (optind < argc)
2117c478bd9Sstevel@tonic-gate 			idargc = argc - optind;
2127c478bd9Sstevel@tonic-gate 		else
2137c478bd9Sstevel@tonic-gate 			idargc = 0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		if (qflag)
2167c478bd9Sstevel@tonic-gate 			cflags |= RT_DOSIG;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		return (set_rtprocs(idtype, idargc, &argv[optind], cflags,
2197c478bd9Sstevel@tonic-gate 		    rtpri, tqntm, res, tqsig));
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	} else if (eflag) {
2227c478bd9Sstevel@tonic-gate 		if (lflag || dflag || sflag || iflag)
2237c478bd9Sstevel@tonic-gate 			fatalerr(usage);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		cflags = (pflag ? RT_DOPRI : 0);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		if (tflag)
2287c478bd9Sstevel@tonic-gate 			cflags |= RT_DOTQ;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		if (rflag == 0)
2317c478bd9Sstevel@tonic-gate 			res = 1000;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		if (qflag)
2347c478bd9Sstevel@tonic-gate 			cflags |= RT_DOSIG;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		exec_rtcmd(&argv[optind], cflags, rtpri, tqntm, res, tqsig);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	} else {
2397c478bd9Sstevel@tonic-gate 		fatalerr(usage);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	return (0);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
247*d4204c85Sraf  * Print our class name and the configured user priority range.
2487c478bd9Sstevel@tonic-gate  */
2497c478bd9Sstevel@tonic-gate static void
print_rtinfo(void)2507c478bd9Sstevel@tonic-gate print_rtinfo(void)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	pcinfo_t	pcinfo;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	(void) strcpy(pcinfo.pc_clname, "RT");
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	(void) printf("RT (Real Time)\n");
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
2597c478bd9Sstevel@tonic-gate 		fatalerr("\tCan't get maximum configured RT priority\n");
2607c478bd9Sstevel@tonic-gate 
261*d4204c85Sraf 	(void) printf("\tConfigured RT User Priority Range: 0 through %d\n",
2627c478bd9Sstevel@tonic-gate 	    ((rtinfo_t *)pcinfo.pc_clinfo)->rt_maxpri);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Read a list of pids from stdin and print the real-time priority and time
2687c478bd9Sstevel@tonic-gate  * quantum (in millisecond resolution) for each of the corresponding processes.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate static int
print_rtprocs(void)2717c478bd9Sstevel@tonic-gate print_rtprocs(void)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	pid_t		*pidlist;
2747c478bd9Sstevel@tonic-gate 	size_t		numread;
2757c478bd9Sstevel@tonic-gate 	int		i;
2767c478bd9Sstevel@tonic-gate 	char		clname[PC_CLNMSZ];
2777c478bd9Sstevel@tonic-gate 	pri_t		rt_pri;
2787c478bd9Sstevel@tonic-gate 	uint_t		rt_tqsecs;
2797c478bd9Sstevel@tonic-gate 	int		rt_tqnsecs;
2807c478bd9Sstevel@tonic-gate 	int		rt_tqsig;
2817c478bd9Sstevel@tonic-gate 	int		error = 0;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	/*
2847c478bd9Sstevel@tonic-gate 	 * Read a list of pids from stdin.
2857c478bd9Sstevel@tonic-gate 	 */
2867c478bd9Sstevel@tonic-gate 	if ((pidlist = read_pidlist(&numread, stdin)) == NULL)
2877c478bd9Sstevel@tonic-gate 		fatalerr("%s: Can't read pidlist.\n", basenm);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	(void) printf("REAL TIME PROCESSES:\n"
2907c478bd9Sstevel@tonic-gate 	    "    PID   RTPRI       TQNTM    TQSIG\n");
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if (numread == 0)
2937c478bd9Sstevel@tonic-gate 		fatalerr("%s: No pids on input\n", basenm);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	for (i = 0; i < numread; i++) {
2967c478bd9Sstevel@tonic-gate 		(void) printf("%7ld", pidlist[i]);
2977c478bd9Sstevel@tonic-gate 		if (priocntl(P_PID, pidlist[i], PC_GETXPARMS, "RT",
2987c478bd9Sstevel@tonic-gate 		    RT_KY_TQSECS, &rt_tqsecs, RT_KY_TQNSECS, &rt_tqnsecs,
2997c478bd9Sstevel@tonic-gate 		    RT_KY_PRI, &rt_pri, RT_KY_TQSIG, &rt_tqsig, 0) != -1) {
3007c478bd9Sstevel@tonic-gate 			(void) printf("   %5d", rt_pri);
3017c478bd9Sstevel@tonic-gate 			if (rt_tqnsecs == RT_TQINF)
3027c478bd9Sstevel@tonic-gate 				(void) printf("    RT_TQINF");
3037c478bd9Sstevel@tonic-gate 			else
3047c478bd9Sstevel@tonic-gate 				(void) printf(" %11lld",
3057c478bd9Sstevel@tonic-gate 				    (longlong_t)rt_tqsecs * 1000 +
3067c478bd9Sstevel@tonic-gate 				    rt_tqnsecs / 1000000);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 			(void) printf("      %3d\n", rt_tqsig);
3097c478bd9Sstevel@tonic-gate 		} else {
3107c478bd9Sstevel@tonic-gate 			error = 1;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 			if (priocntl(P_PID, pidlist[i], PC_GETXPARMS, NULL,
3137c478bd9Sstevel@tonic-gate 			    PC_KY_CLNAME, clname, 0) != -1 &&
3147c478bd9Sstevel@tonic-gate 			    strcmp(clname, "RT"))
3157c478bd9Sstevel@tonic-gate 				/*
3167c478bd9Sstevel@tonic-gate 				 * Process from some class other than real time.
3177c478bd9Sstevel@tonic-gate 				 * It has probably changed class while priocntl
3187c478bd9Sstevel@tonic-gate 				 * command was executing (otherwise we wouldn't
3197c478bd9Sstevel@tonic-gate 				 * have been passed its pid).  Print the little
3207c478bd9Sstevel@tonic-gate 				 * we know about it.
3217c478bd9Sstevel@tonic-gate 				 */
3227c478bd9Sstevel@tonic-gate 				(void) printf("\tChanged to class %s while"
3237c478bd9Sstevel@tonic-gate 				    " priocntl command executing\n", clname);
3247c478bd9Sstevel@tonic-gate 			else
3257c478bd9Sstevel@tonic-gate 				(void) printf("\tCan't get real time"
3267c478bd9Sstevel@tonic-gate 				    " parameters\n");
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	free_pidlist(pidlist);
3317c478bd9Sstevel@tonic-gate 	return (error);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*
3367c478bd9Sstevel@tonic-gate  * Call priocntl() with command codes PC_SETXPARMS or PC_GETXPARMS.
3377c478bd9Sstevel@tonic-gate  * The first parameter behind the command code is always the class name.
3387c478bd9Sstevel@tonic-gate  * Each parameter is headed by a key, which determines the meaning of the
3397c478bd9Sstevel@tonic-gate  * following value. There are maximal RT_KEYCNT = 4 (key, value) pairs.
3407c478bd9Sstevel@tonic-gate  */
3417c478bd9Sstevel@tonic-gate static int
rt_priocntl(idtype_t idtype,id_t id,int cmd,char * clname,uintptr_t * argsp)3427c478bd9Sstevel@tonic-gate rt_priocntl(idtype_t idtype, id_t id, int cmd, char *clname, uintptr_t *argsp)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	return (priocntl(idtype, id, cmd, clname, argsp[0], argsp[1],
3457c478bd9Sstevel@tonic-gate 	    argsp[2], argsp[3], argsp[4], argsp[5], argsp[6], argsp[7], 0));
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * Set all processes in the set specified by idtype/idargv to real time
3517c478bd9Sstevel@tonic-gate  * (if they aren't already real time) and set their real-time priority,
3527c478bd9Sstevel@tonic-gate  * real-time quantum and real-time quantum signal to those specified by
3537c478bd9Sstevel@tonic-gate  * rtpri, tqntm/res and rtqsig.
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate static int
set_rtprocs(idtype_t idtype,int idargc,char ** idargv,uint_t cflags,pri_t rtpri,long tqntm,long res,int rtqsig)3567c478bd9Sstevel@tonic-gate set_rtprocs(idtype_t idtype, int idargc, char **idargv, uint_t cflags,
3577c478bd9Sstevel@tonic-gate 	pri_t rtpri, long tqntm, long res, int rtqsig)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	pcinfo_t	pcinfo;
3607c478bd9Sstevel@tonic-gate 	uintptr_t	args[2*RT_KEYCNT+1];
3617c478bd9Sstevel@tonic-gate 	uintptr_t	*argsp = &args[0];
3627c478bd9Sstevel@tonic-gate 	pri_t		maxrtpri;
3637c478bd9Sstevel@tonic-gate 	hrtimer_t	hrtime;
3647c478bd9Sstevel@tonic-gate 	char		idtypnm[PC_IDTYPNMSZ];
3657c478bd9Sstevel@tonic-gate 	int		i;
3667c478bd9Sstevel@tonic-gate 	id_t		id;
3677c478bd9Sstevel@tonic-gate 	int		error = 0;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * Get the real time class ID and max configured RT priority.
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 	(void) strcpy(pcinfo.pc_clname, "RT");
3747c478bd9Sstevel@tonic-gate 	if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
3757c478bd9Sstevel@tonic-gate 		fatalerr("%s: Can't get RT class ID, priocntl system call"
3767c478bd9Sstevel@tonic-gate 		    " failed with errno %d\n", basenm, errno);
3777c478bd9Sstevel@tonic-gate 	maxrtpri = ((rtinfo_t *)pcinfo.pc_clinfo)->rt_maxpri;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * Validate the rtpri and res arguments.
3817c478bd9Sstevel@tonic-gate 	 */
3827c478bd9Sstevel@tonic-gate 	if ((cflags & RT_DOPRI) != 0) {
3837c478bd9Sstevel@tonic-gate 		if (rtpri > maxrtpri || rtpri < 0)
3847c478bd9Sstevel@tonic-gate 			fatalerr("%s: Specified real time priority %d out of"
3857c478bd9Sstevel@tonic-gate 			    " configured range\n", basenm, rtpri);
3867c478bd9Sstevel@tonic-gate 		ADDKEYVAL(argsp, RT_KY_PRI, rtpri);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if ((cflags & RT_DOTQ) != 0) {
3907c478bd9Sstevel@tonic-gate 		hrtime.hrt_secs = 0;
3917c478bd9Sstevel@tonic-gate 		hrtime.hrt_rem = tqntm;
3927c478bd9Sstevel@tonic-gate 		hrtime.hrt_res = res;
3937c478bd9Sstevel@tonic-gate 		if (_hrtnewres(&hrtime, NANOSEC, HRT_RNDUP) == -1)
3947c478bd9Sstevel@tonic-gate 			fatalerr("%s: Can't convert resolution.\n", basenm);
3957c478bd9Sstevel@tonic-gate 		ADDKEYVAL(argsp, RT_KY_TQSECS, hrtime.hrt_secs);
3967c478bd9Sstevel@tonic-gate 		ADDKEYVAL(argsp, RT_KY_TQNSECS, hrtime.hrt_rem);
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	if ((cflags & RT_DOSIG) != 0)
4007c478bd9Sstevel@tonic-gate 		ADDKEYVAL(argsp, RT_KY_TQSIG, rtqsig);
4017c478bd9Sstevel@tonic-gate 	*argsp = 0;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (idtype == P_ALL) {
4047c478bd9Sstevel@tonic-gate 		if (rt_priocntl(P_ALL, 0, PC_SETXPARMS, "RT", args) == -1) {
4057c478bd9Sstevel@tonic-gate 			if (errno == EPERM) {
4067c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4077c478bd9Sstevel@tonic-gate 				    "Permissions error encountered"
4087c478bd9Sstevel@tonic-gate 				    " on one or more processes.\n");
4097c478bd9Sstevel@tonic-gate 				error = 1;
4107c478bd9Sstevel@tonic-gate 			} else {
4117c478bd9Sstevel@tonic-gate 				fatalerr("%s: Can't reset real time parameters"
4127c478bd9Sstevel@tonic-gate 				    "\npriocntl system call failed with"
4137c478bd9Sstevel@tonic-gate 				    " errno %d\n", basenm, errno);
4147c478bd9Sstevel@tonic-gate 			}
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 	} else if (idargc == 0) {
4177c478bd9Sstevel@tonic-gate 		if (rt_priocntl(idtype, P_MYID, PC_SETXPARMS, "RT",
4187c478bd9Sstevel@tonic-gate 		    args) == -1) {
4197c478bd9Sstevel@tonic-gate 			if (errno == EPERM) {
4207c478bd9Sstevel@tonic-gate 				(void) idtyp2str(idtype, idtypnm);
4217c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "Permissions error"
4227c478bd9Sstevel@tonic-gate 				    " encountered on current %s.\n", idtypnm);
4237c478bd9Sstevel@tonic-gate 				error = 1;
4247c478bd9Sstevel@tonic-gate 			} else {
4257c478bd9Sstevel@tonic-gate 				fatalerr("%s: Can't reset real time parameters"
4267c478bd9Sstevel@tonic-gate 				    "\npriocntl system call failed with"
4277c478bd9Sstevel@tonic-gate 				    " errno %d\n", basenm, errno);
4287c478bd9Sstevel@tonic-gate 			}
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 	} else {
4317c478bd9Sstevel@tonic-gate 		(void) idtyp2str(idtype, idtypnm);
4327c478bd9Sstevel@tonic-gate 		for (i = 0; i < idargc; i++) {
4337c478bd9Sstevel@tonic-gate 			if (idtype == P_CID) {
4347c478bd9Sstevel@tonic-gate 				(void) strcpy(pcinfo.pc_clname, idargv[i]);
4357c478bd9Sstevel@tonic-gate 				if (priocntl(0, 0, PC_GETCID,
4367c478bd9Sstevel@tonic-gate 				    (caddr_t)&pcinfo) == -1)
4377c478bd9Sstevel@tonic-gate 					fatalerr("%s: Invalid or unconfigured"
4387c478bd9Sstevel@tonic-gate 					    " class %s, priocntl system call"
4397c478bd9Sstevel@tonic-gate 					    " failed with errno %d\n",
4407c478bd9Sstevel@tonic-gate 					    basenm, pcinfo.pc_clname, errno);
4417c478bd9Sstevel@tonic-gate 				id = pcinfo.pc_cid;
4427c478bd9Sstevel@tonic-gate 			} else {
4437c478bd9Sstevel@tonic-gate 				id = (id_t)str2num(idargv[i], INT_MIN, INT_MAX);
4447c478bd9Sstevel@tonic-gate 				if (errno)
4457c478bd9Sstevel@tonic-gate 					fatalerr("%s: Invalid id \"%s\"\n",
4467c478bd9Sstevel@tonic-gate 					    basenm, idargv[i]);
4477c478bd9Sstevel@tonic-gate 			}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 			if (rt_priocntl(idtype, id, PC_SETXPARMS, "RT",
4507c478bd9Sstevel@tonic-gate 			    args) == -1) {
4517c478bd9Sstevel@tonic-gate 				if (errno == EPERM) {
4527c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
4537c478bd9Sstevel@tonic-gate 					    "Permissions error encountered on"
4547c478bd9Sstevel@tonic-gate 					    " %s %s.\n", idtypnm, idargv[i]);
4557c478bd9Sstevel@tonic-gate 					error = 1;
4567c478bd9Sstevel@tonic-gate 				} else {
4577c478bd9Sstevel@tonic-gate 					fatalerr("%s: Can't reset real time"
4587c478bd9Sstevel@tonic-gate 					    " parameters\npriocntl system call"
4597c478bd9Sstevel@tonic-gate 					    " failed with errno %d\n",
4607c478bd9Sstevel@tonic-gate 					    basenm, errno);
4617c478bd9Sstevel@tonic-gate 				}
4627c478bd9Sstevel@tonic-gate 			}
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	return (error);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate /*
4717c478bd9Sstevel@tonic-gate  * Execute the command pointed to by cmdargv as a real-time process
4727c478bd9Sstevel@tonic-gate  * with real time priority rtpri, quantum tqntm/res and quantum signal rtqsig.
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate static void
exec_rtcmd(char ** cmdargv,uint_t cflags,pri_t rtpri,long tqntm,long res,int rtqsig)4757c478bd9Sstevel@tonic-gate exec_rtcmd(char **cmdargv, uint_t cflags, pri_t rtpri, long tqntm, long res,
4767c478bd9Sstevel@tonic-gate 	int rtqsig)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	pcinfo_t	pcinfo;
4797c478bd9Sstevel@tonic-gate 	uintptr_t	args[2*RT_KEYCNT+1];
4807c478bd9Sstevel@tonic-gate 	uintptr_t	*argsp = &args[0];
4817c478bd9Sstevel@tonic-gate 	pri_t		maxrtpri;
4827c478bd9Sstevel@tonic-gate 	hrtimer_t	hrtime;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/*
4857c478bd9Sstevel@tonic-gate 	 * Get the real time class ID and max configured RT priority.
4867c478bd9Sstevel@tonic-gate 	 */
4877c478bd9Sstevel@tonic-gate 	(void) strcpy(pcinfo.pc_clname, "RT");
4887c478bd9Sstevel@tonic-gate 	if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1)
4897c478bd9Sstevel@tonic-gate 		fatalerr("%s: Can't get RT class ID, priocntl system call"
4907c478bd9Sstevel@tonic-gate 		    " failed with errno %d\n", basenm, errno);
4917c478bd9Sstevel@tonic-gate 	maxrtpri = ((rtinfo_t *)pcinfo.pc_clinfo)->rt_maxpri;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if ((cflags & RT_DOPRI) != 0) {
4947c478bd9Sstevel@tonic-gate 		if (rtpri > maxrtpri || rtpri < 0)
4957c478bd9Sstevel@tonic-gate 			fatalerr("%s: Specified real time priority %d out of"
4967c478bd9Sstevel@tonic-gate 			    " configured range\n", basenm, rtpri);
4977c478bd9Sstevel@tonic-gate 		ADDKEYVAL(argsp, RT_KY_PRI, rtpri);
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if ((cflags & RT_DOTQ) != 0) {
5017c478bd9Sstevel@tonic-gate 		hrtime.hrt_secs = 0;
5027c478bd9Sstevel@tonic-gate 		hrtime.hrt_rem = tqntm;
5037c478bd9Sstevel@tonic-gate 		hrtime.hrt_res = res;
5047c478bd9Sstevel@tonic-gate 		if (_hrtnewres(&hrtime, NANOSEC, HRT_RNDUP) == -1)
5057c478bd9Sstevel@tonic-gate 			fatalerr("%s: Can't convert resolution.\n", basenm);
5067c478bd9Sstevel@tonic-gate 		ADDKEYVAL(argsp, RT_KY_TQSECS, hrtime.hrt_secs);
5077c478bd9Sstevel@tonic-gate 		ADDKEYVAL(argsp, RT_KY_TQNSECS, hrtime.hrt_rem);
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if ((cflags & RT_DOSIG) != 0)
5117c478bd9Sstevel@tonic-gate 		ADDKEYVAL(argsp, RT_KY_TQSIG, rtqsig);
5127c478bd9Sstevel@tonic-gate 	*argsp = 0;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (rt_priocntl(P_PID, P_MYID, PC_SETXPARMS, "RT", args) == -1)
5157c478bd9Sstevel@tonic-gate 		fatalerr("%s: Can't reset real time parameters\n"
5167c478bd9Sstevel@tonic-gate 		    "priocntl system call failed with errno %d\n",
5177c478bd9Sstevel@tonic-gate 		    basenm, errno);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	(void) execvp(cmdargv[0], cmdargv);
5207c478bd9Sstevel@tonic-gate 	fatalerr("%s: Can't execute %s, exec failed with errno %d\n",
5217c478bd9Sstevel@tonic-gate 	    basenm, cmdargv[0], errno);
5227c478bd9Sstevel@tonic-gate }
523