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