xref: /onnv-gate/usr/src/uts/common/disp/priocntl.c (revision 11173:87f3734e64df)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52712Snn35248  * Common Development and Distribution License (the "License").
62712Snn35248  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216247Sraf 
220Sstevel@tonic-gate /*
23*11173SJonathan.Adams@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/sysmacros.h>
330Sstevel@tonic-gate #include <sys/signal.h>
340Sstevel@tonic-gate #include <sys/pcb.h>
350Sstevel@tonic-gate #include <sys/user.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/sysinfo.h>
380Sstevel@tonic-gate #include <sys/var.h>
390Sstevel@tonic-gate #include <sys/errno.h>
400Sstevel@tonic-gate #include <sys/cred.h>
410Sstevel@tonic-gate #include <sys/proc.h>
420Sstevel@tonic-gate #include <sys/procset.h>
430Sstevel@tonic-gate #include <sys/debug.h>
440Sstevel@tonic-gate #include <sys/inline.h>
450Sstevel@tonic-gate #include <sys/priocntl.h>
460Sstevel@tonic-gate #include <sys/disp.h>
470Sstevel@tonic-gate #include <sys/class.h>
480Sstevel@tonic-gate #include <sys/modctl.h>
490Sstevel@tonic-gate #include <sys/t_lock.h>
500Sstevel@tonic-gate #include <sys/uadmin.h>
510Sstevel@tonic-gate #include <sys/cmn_err.h>
520Sstevel@tonic-gate #include <sys/policy.h>
536247Sraf #include <sys/schedctl.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Structure used to pass arguments to the proccmp() function.
570Sstevel@tonic-gate  * The arguments must be passed in a structure because proccmp()
580Sstevel@tonic-gate  * is called indirectly through the dotoprocs() function which
590Sstevel@tonic-gate  * will only pass through a single one word argument.
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate struct pcmpargs {
620Sstevel@tonic-gate 	id_t	*pcmp_cidp;
630Sstevel@tonic-gate 	int	*pcmp_cntp;
646247Sraf 	kthread_t **pcmp_retthreadp;
650Sstevel@tonic-gate };
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * Structure used to pass arguments to the setparms() function
690Sstevel@tonic-gate  * which is called indirectly through dotoprocs().
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate struct stprmargs {
720Sstevel@tonic-gate 	struct pcparms	*stp_parmsp;	/* pointer to parameters */
730Sstevel@tonic-gate 	int		stp_error;	/* some errors returned here */
740Sstevel@tonic-gate };
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * A vaparm_t is an int followed by a long long -- this packs differently
790Sstevel@tonic-gate  * between the 64-bit kernel ABI and the 32-bit user ABI.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate static int
copyin_vaparms32(caddr_t arg,pc_vaparms_t * vap,uio_seg_t seg)822712Snn35248 copyin_vaparms32(caddr_t arg, pc_vaparms_t *vap, uio_seg_t seg)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	pc_vaparms32_t vaparms32;
850Sstevel@tonic-gate 	pc_vaparm32_t *src;
860Sstevel@tonic-gate 	pc_vaparm_t *dst;
870Sstevel@tonic-gate 	uint_t cnt;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	ASSERT(get_udatamodel() == DATAMODEL_ILP32);
900Sstevel@tonic-gate 
912712Snn35248 	if ((seg == UIO_USERSPACE ? copyin : kcopy)(arg, &vaparms32,
922712Snn35248 	    sizeof (vaparms32)))
930Sstevel@tonic-gate 		return (EFAULT);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	vap->pc_vaparmscnt = vaparms32.pc_vaparmscnt;
960Sstevel@tonic-gate 	if ((cnt = vaparms32.pc_vaparmscnt) > PC_VAPARMCNT)
970Sstevel@tonic-gate 		cnt = PC_VAPARMCNT;
980Sstevel@tonic-gate 	for (src = vaparms32.pc_parms, dst = vap->pc_parms;
990Sstevel@tonic-gate 	    cnt--; src++, dst++) {
1000Sstevel@tonic-gate 		dst->pc_key = src->pc_key;
1010Sstevel@tonic-gate 		dst->pc_parm = src->pc_parm;
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	return (0);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1062712Snn35248 #define	COPYIN_VAPARMS(arg, vap, size, seg)	\
1070Sstevel@tonic-gate 	(get_udatamodel() == DATAMODEL_NATIVE ?	\
1082712Snn35248 	(*copyinfn)(arg, vap, size) : copyin_vaparms32(arg, vap, seg))
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #else
1110Sstevel@tonic-gate 
1122712Snn35248 #define	COPYIN_VAPARMS(arg, vap, size, seg)	(*copyinfn)(arg, vap, size)
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate #endif
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static int donice(procset_t *, pcnice_t *);
1176247Sraf static int doprio(procset_t *, pcprio_t *);
1180Sstevel@tonic-gate static int proccmp(proc_t *, struct pcmpargs *);
1190Sstevel@tonic-gate static int setparms(proc_t *, struct stprmargs *);
1206247Sraf extern int threadcmp(struct pcmpargs *, kthread_t *);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate  * The priocntl system call.
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate long
priocntl_common(int pc_version,procset_t * psp,int cmd,caddr_t arg,caddr_t arg2,uio_seg_t seg)1262712Snn35248 priocntl_common(int pc_version, procset_t *psp, int cmd, caddr_t arg,
1272712Snn35248     caddr_t arg2, uio_seg_t seg)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	pcinfo_t		pcinfo;
1300Sstevel@tonic-gate 	pcparms_t		pcparms;
1310Sstevel@tonic-gate 	pcnice_t		pcnice;
1326247Sraf 	pcprio_t		pcprio;
1330Sstevel@tonic-gate 	pcadmin_t		pcadmin;
1340Sstevel@tonic-gate 	pcpri_t			pcpri;
1350Sstevel@tonic-gate 	procset_t		procset;
1360Sstevel@tonic-gate 	struct stprmargs	stprmargs;
1370Sstevel@tonic-gate 	struct pcmpargs		pcmpargs;
1380Sstevel@tonic-gate 	pc_vaparms_t		vaparms;
1390Sstevel@tonic-gate 	char			clname[PC_CLNMSZ];
1403247Sgjelinek 	char			*outstr;
1410Sstevel@tonic-gate 	int			count;
1426247Sraf 	kthread_t		*retthreadp;
1430Sstevel@tonic-gate 	proc_t			*initpp;
1440Sstevel@tonic-gate 	int			clnullflag;
1450Sstevel@tonic-gate 	int			error = 0;
1460Sstevel@tonic-gate 	int			error1 = 0;
1470Sstevel@tonic-gate 	int			rv = 0;
1480Sstevel@tonic-gate 	pid_t			saved_pid;
1490Sstevel@tonic-gate 	id_t			classid;
1503247Sgjelinek 	int			size;
1512712Snn35248 	int (*copyinfn)(const void *, void *, size_t);
1522712Snn35248 	int (*copyoutfn)(const void *, void *, size_t);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/*
1550Sstevel@tonic-gate 	 * First just check the version number. Right now there is only
1560Sstevel@tonic-gate 	 * one version we know about and support.  If we get some other
1570Sstevel@tonic-gate 	 * version number from the application it may be that the
1580Sstevel@tonic-gate 	 * application was built with some future version and is trying
1590Sstevel@tonic-gate 	 * to run on an old release of the system (that's us).  In any
1600Sstevel@tonic-gate 	 * case if we don't recognize the version number all we can do is
1610Sstevel@tonic-gate 	 * return error.
1620Sstevel@tonic-gate 	 */
1630Sstevel@tonic-gate 	if (pc_version != PC_VERSION)
1640Sstevel@tonic-gate 		return (set_errno(EINVAL));
1650Sstevel@tonic-gate 
1662712Snn35248 	if (seg == UIO_USERSPACE) {
1672712Snn35248 		copyinfn = copyin;
1682712Snn35248 		copyoutfn = copyout;
1692712Snn35248 	} else {
1702712Snn35248 		copyinfn = kcopy;
1712712Snn35248 		copyoutfn = kcopy;
1722712Snn35248 	}
1732712Snn35248 
1740Sstevel@tonic-gate 	switch (cmd) {
1750Sstevel@tonic-gate 	case PC_GETCID:
1760Sstevel@tonic-gate 		/*
1770Sstevel@tonic-gate 		 * If the arg pointer is NULL, the user just wants to
1780Sstevel@tonic-gate 		 * know the number of classes. If non-NULL, the pointer
1790Sstevel@tonic-gate 		 * should point to a valid user pcinfo buffer.  In the
1800Sstevel@tonic-gate 		 * dynamic world we need to return the number of loaded
1810Sstevel@tonic-gate 		 * classes, not the max number of available classes that
1820Sstevel@tonic-gate 		 * can be loaded.
1830Sstevel@tonic-gate 		 */
1840Sstevel@tonic-gate 		if (arg == NULL) {
1850Sstevel@tonic-gate 			rv = loaded_classes;
1860Sstevel@tonic-gate 			break;
1870Sstevel@tonic-gate 		} else {
1882712Snn35248 			if ((*copyinfn)(arg, &pcinfo, sizeof (pcinfo)))
1890Sstevel@tonic-gate 				return (set_errno(EFAULT));
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		pcinfo.pc_clname[PC_CLNMSZ-1] = '\0';
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 		/*
1950Sstevel@tonic-gate 		 * Get the class ID corresponding to user supplied name.
1960Sstevel@tonic-gate 		 */
1970Sstevel@tonic-gate 		error = getcid(pcinfo.pc_clname, &pcinfo.pc_cid);
1980Sstevel@tonic-gate 		if (error)
1990Sstevel@tonic-gate 			return (set_errno(error));
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		/*
2020Sstevel@tonic-gate 		 * Can't get info about the sys class.
2030Sstevel@tonic-gate 		 */
2040Sstevel@tonic-gate 		if (pcinfo.pc_cid == 0)
2050Sstevel@tonic-gate 			return (set_errno(EINVAL));
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		/*
2080Sstevel@tonic-gate 		 * Get the class specific information.
2090Sstevel@tonic-gate 		 * we MUST make sure that the class has not already
2100Sstevel@tonic-gate 		 * been unloaded before we try the CL_GETCLINFO.
2110Sstevel@tonic-gate 		 * If it has then we need to load it.
2120Sstevel@tonic-gate 		 */
2130Sstevel@tonic-gate 		error =
2140Sstevel@tonic-gate 		    scheduler_load(pcinfo.pc_clname, &sclass[pcinfo.pc_cid]);
2150Sstevel@tonic-gate 		if (error)
2160Sstevel@tonic-gate 			return (set_errno(error));
2170Sstevel@tonic-gate 		error = CL_GETCLINFO(&sclass[pcinfo.pc_cid], pcinfo.pc_clinfo);
2180Sstevel@tonic-gate 		if (error)
2190Sstevel@tonic-gate 			return (set_errno(error));
2200Sstevel@tonic-gate 
2212712Snn35248 		if ((*copyoutfn)(&pcinfo, arg, sizeof (pcinfo)))
2220Sstevel@tonic-gate 			return (set_errno(EFAULT));
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		rv = loaded_classes;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		break;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	case PC_GETCLINFO:
2290Sstevel@tonic-gate 		/*
2300Sstevel@tonic-gate 		 * If the arg pointer is NULL, the user just wants to know
2310Sstevel@tonic-gate 		 * the number of classes. If non-NULL, the pointer should
2320Sstevel@tonic-gate 		 * point to a valid user pcinfo buffer.
2330Sstevel@tonic-gate 		 */
2340Sstevel@tonic-gate 		if (arg == NULL) {
2350Sstevel@tonic-gate 			rv = loaded_classes;
2360Sstevel@tonic-gate 			break;
2370Sstevel@tonic-gate 		} else {
2382712Snn35248 			if ((*copyinfn)(arg, &pcinfo, sizeof (pcinfo)))
2390Sstevel@tonic-gate 				return (set_errno(EFAULT));
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		if (pcinfo.pc_cid >= loaded_classes || pcinfo.pc_cid < 1)
2430Sstevel@tonic-gate 			return (set_errno(EINVAL));
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		(void) strncpy(pcinfo.pc_clname, sclass[pcinfo.pc_cid].cl_name,
2460Sstevel@tonic-gate 		    PC_CLNMSZ);
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 		/*
2490Sstevel@tonic-gate 		 * Get the class specific information.  we MUST make sure
2500Sstevel@tonic-gate 		 * that the class has not already been unloaded before we
2510Sstevel@tonic-gate 		 * try the CL_GETCLINFO.  If it has then we need to load
2520Sstevel@tonic-gate 		 * it.
2530Sstevel@tonic-gate 		 */
2540Sstevel@tonic-gate 		error =
2550Sstevel@tonic-gate 		    scheduler_load(pcinfo.pc_clname, &sclass[pcinfo.pc_cid]);
2560Sstevel@tonic-gate 		if (error)
2570Sstevel@tonic-gate 			return (set_errno(error));
2580Sstevel@tonic-gate 		error = CL_GETCLINFO(&sclass[pcinfo.pc_cid], pcinfo.pc_clinfo);
2590Sstevel@tonic-gate 		if (error)
2600Sstevel@tonic-gate 			return (set_errno(error));
2610Sstevel@tonic-gate 
2622712Snn35248 		if ((*copyoutfn)(&pcinfo, arg, sizeof (pcinfo)))
2630Sstevel@tonic-gate 			return (set_errno(EFAULT));
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 		rv = loaded_classes;
2660Sstevel@tonic-gate 		break;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	case PC_SETPARMS:
2690Sstevel@tonic-gate 	case PC_SETXPARMS:
2700Sstevel@tonic-gate 		/*
2710Sstevel@tonic-gate 		 * First check the validity of the parameters we got from
2720Sstevel@tonic-gate 		 * the user.  We don't do any permissions checking here
2730Sstevel@tonic-gate 		 * because it's done on a per thread basis by parmsset().
2740Sstevel@tonic-gate 		 */
2750Sstevel@tonic-gate 		if (cmd == PC_SETPARMS) {
2762712Snn35248 			if ((*copyinfn)(arg, &pcparms, sizeof (pcparms)))
2770Sstevel@tonic-gate 				return (set_errno(EFAULT));
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 			error = parmsin(&pcparms, NULL);
2800Sstevel@tonic-gate 		} else {
2812712Snn35248 			if ((*copyinfn)(arg, clname, PC_CLNMSZ) ||
2822712Snn35248 			    COPYIN_VAPARMS(arg2, &vaparms, sizeof (vaparms),
2832712Snn35248 			    seg))
2840Sstevel@tonic-gate 				return (set_errno(EFAULT));
2850Sstevel@tonic-gate 			clname[PC_CLNMSZ-1] = '\0';
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 			if (getcid(clname, &pcparms.pc_cid))
2880Sstevel@tonic-gate 				return (set_errno(EINVAL));
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 			error = parmsin(&pcparms, &vaparms);
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		if (error)
2940Sstevel@tonic-gate 			return (set_errno(error));
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 		/*
2970Sstevel@tonic-gate 		 * Get the procset from the user.
2980Sstevel@tonic-gate 		 */
2992712Snn35248 		if ((*copyinfn)(psp, &procset, sizeof (procset)))
3000Sstevel@tonic-gate 			return (set_errno(EFAULT));
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 		/*
3030Sstevel@tonic-gate 		 * For performance we do a quick check here to catch
3040Sstevel@tonic-gate 		 * common cases where the current thread is the only one
3050Sstevel@tonic-gate 		 * in the set.  In such cases we can call parmsset()
3060Sstevel@tonic-gate 		 * directly, avoiding the relatively lengthy path through
3070Sstevel@tonic-gate 		 * dotoprocs().  The underlying classes expect pidlock to
3080Sstevel@tonic-gate 		 * be held.
3090Sstevel@tonic-gate 		 */
3100Sstevel@tonic-gate 		if (cur_inset_only(&procset) == B_TRUE) {
3110Sstevel@tonic-gate 			/* do a single LWP */
3120Sstevel@tonic-gate 			if ((procset.p_lidtype == P_LWPID) ||
3130Sstevel@tonic-gate 			    (procset.p_ridtype == P_LWPID)) {
3140Sstevel@tonic-gate 				mutex_enter(&pidlock);
3150Sstevel@tonic-gate 				mutex_enter(&curproc->p_lock);
3160Sstevel@tonic-gate 				error = parmsset(&pcparms, curthread);
3170Sstevel@tonic-gate 				mutex_exit(&curproc->p_lock);
3180Sstevel@tonic-gate 				mutex_exit(&pidlock);
3190Sstevel@tonic-gate 			} else {
3200Sstevel@tonic-gate 				/* do the entire process otherwise */
3210Sstevel@tonic-gate 				stprmargs.stp_parmsp = &pcparms;
3220Sstevel@tonic-gate 				stprmargs.stp_error = 0;
3230Sstevel@tonic-gate 				mutex_enter(&pidlock);
3240Sstevel@tonic-gate 				error = setparms(curproc, &stprmargs);
3250Sstevel@tonic-gate 				mutex_exit(&pidlock);
3260Sstevel@tonic-gate 				if (error == 0 && stprmargs.stp_error != 0)
3270Sstevel@tonic-gate 					error = stprmargs.stp_error;
3280Sstevel@tonic-gate 			}
3290Sstevel@tonic-gate 			if (error)
3300Sstevel@tonic-gate 				return (set_errno(error));
3310Sstevel@tonic-gate 		} else {
3320Sstevel@tonic-gate 			stprmargs.stp_parmsp = &pcparms;
3330Sstevel@tonic-gate 			stprmargs.stp_error = 0;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 			error1 = error = ESRCH;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 			/*
3380Sstevel@tonic-gate 			 * The dotoprocs() call below will cause
3390Sstevel@tonic-gate 			 * setparms() to be called for each thread in the
3400Sstevel@tonic-gate 			 * specified procset. setparms() will in turn
3410Sstevel@tonic-gate 			 * call parmsset() (which does the real work).
3420Sstevel@tonic-gate 			 */
3430Sstevel@tonic-gate 			if ((procset.p_lidtype != P_LWPID) ||
3446247Sraf 			    (procset.p_ridtype != P_LWPID)) {
3450Sstevel@tonic-gate 				error1 = dotoprocs(&procset, setparms,
3460Sstevel@tonic-gate 				    (char *)&stprmargs);
3470Sstevel@tonic-gate 			}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 			/*
3500Sstevel@tonic-gate 			 * take care of the case when any of the
3510Sstevel@tonic-gate 			 * operands happen to be LWP's
3520Sstevel@tonic-gate 			 */
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 			if ((procset.p_lidtype == P_LWPID) ||
3550Sstevel@tonic-gate 			    (procset.p_ridtype == P_LWPID)) {
3560Sstevel@tonic-gate 				error = dotolwp(&procset, parmsset,
3570Sstevel@tonic-gate 				    (char *)&pcparms);
3580Sstevel@tonic-gate 				/*
3590Sstevel@tonic-gate 				 * Dotolwp() returns with p_lock held.
3600Sstevel@tonic-gate 				 * This is required for the GETPARMS case
3610Sstevel@tonic-gate 				 * below. So, here we just release the
3620Sstevel@tonic-gate 				 * p_lock.
3630Sstevel@tonic-gate 				 */
3640Sstevel@tonic-gate 				if (MUTEX_HELD(&curproc->p_lock))
3650Sstevel@tonic-gate 					mutex_exit(&curproc->p_lock);
3660Sstevel@tonic-gate 			}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 			/*
3690Sstevel@tonic-gate 			 * If setparms() encounters a permissions error
3700Sstevel@tonic-gate 			 * for one or more of the threads it returns
3710Sstevel@tonic-gate 			 * EPERM in stp_error so dotoprocs() will
3720Sstevel@tonic-gate 			 * continue through the thread set.  If
3730Sstevel@tonic-gate 			 * dotoprocs() returned an error above, it was
3740Sstevel@tonic-gate 			 * more serious than permissions and dotoprocs
3750Sstevel@tonic-gate 			 * quit when the error was encountered.  We
3760Sstevel@tonic-gate 			 * return the more serious error if there was
3770Sstevel@tonic-gate 			 * one, otherwise we return EPERM if we got that
3780Sstevel@tonic-gate 			 * back.
3790Sstevel@tonic-gate 			 */
3800Sstevel@tonic-gate 			if (error1 != ESRCH)
3810Sstevel@tonic-gate 				error = error1;
3820Sstevel@tonic-gate 			if (error == 0 && stprmargs.stp_error != 0)
3830Sstevel@tonic-gate 				error = stprmargs.stp_error;
3840Sstevel@tonic-gate 		}
3850Sstevel@tonic-gate 		break;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	case PC_GETPARMS:
3880Sstevel@tonic-gate 	case PC_GETXPARMS:
3890Sstevel@tonic-gate 		if (cmd == PC_GETPARMS) {
3902712Snn35248 			if ((*copyinfn)(arg, &pcparms, sizeof (pcparms)))
3910Sstevel@tonic-gate 				return (set_errno(EFAULT));
3920Sstevel@tonic-gate 		} else {
3930Sstevel@tonic-gate 			if (arg != NULL) {
3942712Snn35248 				if ((*copyinfn)(arg, clname, PC_CLNMSZ))
3950Sstevel@tonic-gate 					return (set_errno(EFAULT));
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 				clname[PC_CLNMSZ-1] = '\0';
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 				if (getcid(clname, &pcparms.pc_cid))
4000Sstevel@tonic-gate 					return (set_errno(EINVAL));
4010Sstevel@tonic-gate 			} else
4020Sstevel@tonic-gate 				pcparms.pc_cid = PC_CLNULL;
4032712Snn35248 
4042712Snn35248 			if (COPYIN_VAPARMS(arg2, &vaparms, sizeof (vaparms),
4052712Snn35248 			    seg))
4060Sstevel@tonic-gate 				return (set_errno(EFAULT));
4070Sstevel@tonic-gate 		}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		if (pcparms.pc_cid >= loaded_classes ||
4100Sstevel@tonic-gate 		    (pcparms.pc_cid < 1 && pcparms.pc_cid != PC_CLNULL))
4110Sstevel@tonic-gate 			return (set_errno(EINVAL));
4120Sstevel@tonic-gate 
4132712Snn35248 		if ((*copyinfn)(psp, &procset, sizeof (procset)))
4140Sstevel@tonic-gate 			return (set_errno(EFAULT));
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 		/*
4170Sstevel@tonic-gate 		 * Check to see if the current thread is the only one
4180Sstevel@tonic-gate 		 * in the set. If not we must go through the whole set
4190Sstevel@tonic-gate 		 * to select a thread.
4200Sstevel@tonic-gate 		 */
4210Sstevel@tonic-gate 		if (cur_inset_only(&procset) == B_TRUE) {
4220Sstevel@tonic-gate 			/* do a single LWP */
4230Sstevel@tonic-gate 			if ((procset.p_lidtype == P_LWPID) ||
4240Sstevel@tonic-gate 			    (procset.p_ridtype == P_LWPID)) {
4250Sstevel@tonic-gate 				if (pcparms.pc_cid != PC_CLNULL &&
4260Sstevel@tonic-gate 				    pcparms.pc_cid != curthread->t_cid) {
4270Sstevel@tonic-gate 					/*
4280Sstevel@tonic-gate 					 * Specified thread not in
4290Sstevel@tonic-gate 					 * specified class.
4300Sstevel@tonic-gate 					 */
4310Sstevel@tonic-gate 					return (set_errno(ESRCH));
4320Sstevel@tonic-gate 				} else {
4330Sstevel@tonic-gate 					mutex_enter(&curproc->p_lock);
4340Sstevel@tonic-gate 					retthreadp = curthread;
4350Sstevel@tonic-gate 				}
4360Sstevel@tonic-gate 			} else {
4370Sstevel@tonic-gate 				count = 0;
4380Sstevel@tonic-gate 				retthreadp = NULL;
4390Sstevel@tonic-gate 				pcmpargs.pcmp_cidp = &pcparms.pc_cid;
4400Sstevel@tonic-gate 				pcmpargs.pcmp_cntp = &count;
4410Sstevel@tonic-gate 				pcmpargs.pcmp_retthreadp = &retthreadp;
4420Sstevel@tonic-gate 				/*
4430Sstevel@tonic-gate 				 * Specified thread not in specified class.
4440Sstevel@tonic-gate 				 */
4450Sstevel@tonic-gate 				if (pcparms.pc_cid != PC_CLNULL &&
4460Sstevel@tonic-gate 				    pcparms.pc_cid != curthread->t_cid)
4470Sstevel@tonic-gate 					return (set_errno(ESRCH));
4480Sstevel@tonic-gate 				error = proccmp(curproc, &pcmpargs);
4490Sstevel@tonic-gate 				if (error) {
4500Sstevel@tonic-gate 					if (retthreadp != NULL)
4510Sstevel@tonic-gate 						mutex_exit(&(curproc->p_lock));
4520Sstevel@tonic-gate 					return (set_errno(error));
4530Sstevel@tonic-gate 				}
4540Sstevel@tonic-gate 			}
4550Sstevel@tonic-gate 		} else {
4560Sstevel@tonic-gate 			/*
4570Sstevel@tonic-gate 			 * get initpp early to avoid lock ordering problems
4580Sstevel@tonic-gate 			 * (we cannot get pidlock while holding any p_lock).
4590Sstevel@tonic-gate 			 */
4600Sstevel@tonic-gate 			mutex_enter(&pidlock);
4610Sstevel@tonic-gate 			initpp = prfind(P_INITPID);
4620Sstevel@tonic-gate 			mutex_exit(&pidlock);
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 			/*
4650Sstevel@tonic-gate 			 * Select the thread (from the set) whose
4660Sstevel@tonic-gate 			 * parameters we are going to return.  First we
4670Sstevel@tonic-gate 			 * set up some locations for return values, then
4680Sstevel@tonic-gate 			 * we call proccmp() indirectly through
4690Sstevel@tonic-gate 			 * dotoprocs().  proccmp() will call a class
4700Sstevel@tonic-gate 			 * specific routine which actually does the
4710Sstevel@tonic-gate 			 * selection.  To understand how this works take
4720Sstevel@tonic-gate 			 * a careful look at the code below, the
4730Sstevel@tonic-gate 			 * dotoprocs() function, the proccmp() function,
4740Sstevel@tonic-gate 			 * and the class specific cl_proccmp() functions.
4750Sstevel@tonic-gate 			 */
4760Sstevel@tonic-gate 			if (pcparms.pc_cid == PC_CLNULL)
4770Sstevel@tonic-gate 				clnullflag = 1;
4780Sstevel@tonic-gate 			else
4790Sstevel@tonic-gate 				clnullflag = 0;
4800Sstevel@tonic-gate 			count = 0;
4810Sstevel@tonic-gate 			retthreadp = NULL;
4820Sstevel@tonic-gate 			pcmpargs.pcmp_cidp = &pcparms.pc_cid;
4830Sstevel@tonic-gate 			pcmpargs.pcmp_cntp = &count;
4840Sstevel@tonic-gate 			pcmpargs.pcmp_retthreadp = &retthreadp;
4850Sstevel@tonic-gate 			error1 = error = ESRCH;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 			if ((procset.p_lidtype != P_LWPID) ||
4880Sstevel@tonic-gate 			    (procset.p_ridtype != P_LWPID)) {
4890Sstevel@tonic-gate 				error1 = dotoprocs(&procset, proccmp,
4900Sstevel@tonic-gate 				    (char *)&pcmpargs);
4910Sstevel@tonic-gate 			}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 			/*
4940Sstevel@tonic-gate 			 * take care of combination of LWP and process
4950Sstevel@tonic-gate 			 * set case in a procset
4960Sstevel@tonic-gate 			 */
4970Sstevel@tonic-gate 			if ((procset.p_lidtype == P_LWPID) ||
4980Sstevel@tonic-gate 			    (procset.p_ridtype == P_LWPID)) {
4990Sstevel@tonic-gate 				error = dotolwp(&procset, threadcmp,
5000Sstevel@tonic-gate 				    (char *)&pcmpargs);
5010Sstevel@tonic-gate 			}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 			/*
5040Sstevel@tonic-gate 			 * Both proccmp() and threadcmp() return with the
5050Sstevel@tonic-gate 			 * p_lock held for the ttoproc(retthreadp). This
5060Sstevel@tonic-gate 			 * is required to make sure that the process we
5070Sstevel@tonic-gate 			 * chose as the winner doesn't go away
5080Sstevel@tonic-gate 			 * i.e. retthreadp has to be a valid pointer.
5090Sstevel@tonic-gate 			 *
5100Sstevel@tonic-gate 			 * The case below can only happen if the thread
5110Sstevel@tonic-gate 			 * with the highest priority was not in your
5120Sstevel@tonic-gate 			 * process.  In that case, dotolwp will return
5130Sstevel@tonic-gate 			 * holding p_lock for both your process as well
5140Sstevel@tonic-gate 			 * as the process in which retthreadp is a
5150Sstevel@tonic-gate 			 * thread.
5160Sstevel@tonic-gate 			 */
5170Sstevel@tonic-gate 			if ((retthreadp != NULL) &&
5180Sstevel@tonic-gate 			    (ttoproc(retthreadp) != curproc) &&
5190Sstevel@tonic-gate 			    MUTEX_HELD(&(curproc)->p_lock))
5200Sstevel@tonic-gate 				mutex_exit(&(curproc)->p_lock);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 			ASSERT(retthreadp == NULL ||
5230Sstevel@tonic-gate 			    MUTEX_HELD(&(ttoproc(retthreadp)->p_lock)));
5240Sstevel@tonic-gate 			if (error1 != ESRCH)
5250Sstevel@tonic-gate 				error = error1;
5260Sstevel@tonic-gate 			if (error) {
5270Sstevel@tonic-gate 				if (retthreadp != NULL)
5286247Sraf 				    /* CSTYLED */
5290Sstevel@tonic-gate 				    mutex_exit(&(ttoproc(retthreadp)->p_lock));
5300Sstevel@tonic-gate 				ASSERT(MUTEX_NOT_HELD(&(curproc)->p_lock));
5310Sstevel@tonic-gate 				return (set_errno(error));
5320Sstevel@tonic-gate 			}
5330Sstevel@tonic-gate 			/*
5340Sstevel@tonic-gate 			 * dotoprocs() ignores the init process if it is
5350Sstevel@tonic-gate 			 * in the set, unless it was the only process found.
5360Sstevel@tonic-gate 			 * Since we are getting parameters here rather than
5370Sstevel@tonic-gate 			 * setting them, we want to make sure init is not
5380Sstevel@tonic-gate 			 * excluded if it is in the set.
5390Sstevel@tonic-gate 			 */
5406618Srh87107 			if (initpp != NULL && retthreadp != NULL &&
5416618Srh87107 			    ttoproc(retthreadp) != initpp) {
5426618Srh87107 				mutex_enter(&initpp->p_lock);
5436618Srh87107 				if (procinset(initpp, &procset)) {
5446618Srh87107 					mutex_exit(&initpp->p_lock);
5456618Srh87107 					(void) proccmp(initpp, &pcmpargs);
5466618Srh87107 				} else {
5476618Srh87107 					mutex_exit(&initpp->p_lock);
5486618Srh87107 				}
5496618Srh87107 			}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 			/*
5520Sstevel@tonic-gate 			 * If dotoprocs returned success it found at least
5530Sstevel@tonic-gate 			 * one thread in the set.  If proccmp() failed to
5540Sstevel@tonic-gate 			 * select a thread it is because the user specified
5550Sstevel@tonic-gate 			 * a class and none of the threads in the set
5560Sstevel@tonic-gate 			 * belonged to that class, or because the process
5570Sstevel@tonic-gate 			 * specified was in the middle of exiting and had
5580Sstevel@tonic-gate 			 * cleared its thread list.
5590Sstevel@tonic-gate 			 */
5600Sstevel@tonic-gate 			if (retthreadp == NULL) {
5610Sstevel@tonic-gate 				/*
5620Sstevel@tonic-gate 				 * Might be here and still holding p_lock
5630Sstevel@tonic-gate 				 * if we did a dotolwp on an lwp that
5640Sstevel@tonic-gate 				 * existed but was in the wrong class.
5650Sstevel@tonic-gate 				 */
5660Sstevel@tonic-gate 				if (MUTEX_HELD(&(curproc)->p_lock))
5670Sstevel@tonic-gate 					mutex_exit(&(curproc)->p_lock);
5680Sstevel@tonic-gate 				return (set_errno(ESRCH));
5690Sstevel@tonic-gate 			}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 			/*
5720Sstevel@tonic-gate 			 * User can only use PC_CLNULL with one thread in set.
5730Sstevel@tonic-gate 			 */
5740Sstevel@tonic-gate 			if (clnullflag && count > 1) {
5750Sstevel@tonic-gate 				if (retthreadp != NULL)
5760Sstevel@tonic-gate 					mutex_exit(
5770Sstevel@tonic-gate 					    &(ttoproc(retthreadp)->p_lock));
5780Sstevel@tonic-gate 				ASSERT(MUTEX_NOT_HELD(&(curproc)->p_lock));
5790Sstevel@tonic-gate 				return (set_errno(EINVAL));
5800Sstevel@tonic-gate 			}
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 		ASSERT(retthreadp == NULL ||
5840Sstevel@tonic-gate 		    MUTEX_HELD(&(ttoproc(retthreadp)->p_lock)));
5850Sstevel@tonic-gate 		/*
5860Sstevel@tonic-gate 		 * It is possible to have retthreadp == NULL. Proccmp()
5870Sstevel@tonic-gate 		 * in the rare case (p_tlist == NULL) could return without
5880Sstevel@tonic-gate 		 * setting a value for retthreadp.
5890Sstevel@tonic-gate 		 */
5900Sstevel@tonic-gate 		if (retthreadp == NULL) {
5910Sstevel@tonic-gate 			ASSERT(MUTEX_NOT_HELD(&(curproc)->p_lock));
5920Sstevel@tonic-gate 			return (set_errno(ESRCH));
5930Sstevel@tonic-gate 		}
5940Sstevel@tonic-gate 		/*
5950Sstevel@tonic-gate 		 * We've selected a thread so now get the parameters.
5960Sstevel@tonic-gate 		 */
5970Sstevel@tonic-gate 		parmsget(retthreadp, &pcparms);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 		/*
6000Sstevel@tonic-gate 		 * Prepare to return parameters to the user
6010Sstevel@tonic-gate 		 */
6020Sstevel@tonic-gate 		error = parmsout(&pcparms,
6030Sstevel@tonic-gate 		    (cmd == PC_GETPARMS ? NULL : &vaparms));
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 		/*
6060Sstevel@tonic-gate 		 * Save pid of selected thread before dropping p_lock.
6070Sstevel@tonic-gate 		 */
6080Sstevel@tonic-gate 		saved_pid = ttoproc(retthreadp)->p_pid;
6090Sstevel@tonic-gate 		mutex_exit(&(ttoproc(retthreadp)->p_lock));
6100Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 		if (error)
6130Sstevel@tonic-gate 			return (set_errno(error));
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 		if (cmd == PC_GETPARMS) {
6162712Snn35248 			if ((*copyoutfn)(&pcparms, arg, sizeof (pcparms)))
6170Sstevel@tonic-gate 				return (set_errno(EFAULT));
6182712Snn35248 		} else if ((error = vaparmsout(arg, &pcparms, &vaparms,
6192712Snn35248 		    seg)) != 0)
6200Sstevel@tonic-gate 			return (set_errno(error));
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		/*
6230Sstevel@tonic-gate 		 * And finally, return the pid of the selected thread.
6240Sstevel@tonic-gate 		 */
6250Sstevel@tonic-gate 		rv = saved_pid;
6260Sstevel@tonic-gate 		break;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	case PC_ADMIN:
6290Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
6302712Snn35248 			if ((*copyinfn)(arg, &pcadmin, sizeof (pcadmin_t)))
6310Sstevel@tonic-gate 				return (set_errno(EFAULT));
6320Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
6330Sstevel@tonic-gate 		} else {
6340Sstevel@tonic-gate 			/* pcadmin struct from ILP32 callers */
6350Sstevel@tonic-gate 			pcadmin32_t pcadmin32;
6360Sstevel@tonic-gate 
6372712Snn35248 			if ((*copyinfn)(arg, &pcadmin32, sizeof (pcadmin32_t)))
6380Sstevel@tonic-gate 				return (set_errno(EFAULT));
6390Sstevel@tonic-gate 			pcadmin.pc_cid = pcadmin32.pc_cid;
6400Sstevel@tonic-gate 			pcadmin.pc_cladmin = (caddr_t)(uintptr_t)
6410Sstevel@tonic-gate 			    pcadmin32.pc_cladmin;
6420Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
6430Sstevel@tonic-gate 		}
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 		if (pcadmin.pc_cid >= loaded_classes ||
6460Sstevel@tonic-gate 		    pcadmin.pc_cid < 1)
6470Sstevel@tonic-gate 			return (set_errno(EINVAL));
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		/*
6500Sstevel@tonic-gate 		 * Have the class do whatever the user is requesting.
6510Sstevel@tonic-gate 		 */
6520Sstevel@tonic-gate 		mutex_enter(&ualock);
6530Sstevel@tonic-gate 		error = CL_ADMIN(&sclass[pcadmin.pc_cid], pcadmin.pc_cladmin,
6546247Sraf 		    CRED());
6550Sstevel@tonic-gate 		mutex_exit(&ualock);
6560Sstevel@tonic-gate 		break;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	case PC_GETPRIRANGE:
6592712Snn35248 		if ((*copyinfn)(arg, &pcpri, sizeof (pcpri_t)))
6600Sstevel@tonic-gate 			return (set_errno(EFAULT));
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 		if (pcpri.pc_cid >= loaded_classes || pcpri.pc_cid < 0)
6630Sstevel@tonic-gate 			return (set_errno(EINVAL));
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 		error = CL_GETCLPRI(&sclass[pcpri.pc_cid], &pcpri);
6660Sstevel@tonic-gate 		if (!error) {
6672712Snn35248 			if ((*copyoutfn)(&pcpri, arg, sizeof (pcpri)))
6680Sstevel@tonic-gate 				return (set_errno(EFAULT));
6690Sstevel@tonic-gate 		}
6700Sstevel@tonic-gate 		break;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	case PC_DONICE:
6730Sstevel@tonic-gate 		/*
6740Sstevel@tonic-gate 		 * Get pcnice and procset structures from the user.
6750Sstevel@tonic-gate 		 */
6762712Snn35248 		if ((*copyinfn)(arg, &pcnice, sizeof (pcnice)) ||
6772712Snn35248 		    (*copyinfn)(psp, &procset, sizeof (procset)))
6780Sstevel@tonic-gate 			return (set_errno(EFAULT));
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		error = donice(&procset, &pcnice);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 		if (!error && (pcnice.pc_op == PC_GETNICE)) {
6832712Snn35248 			if ((*copyoutfn)(&pcnice, arg, sizeof (pcnice)))
6840Sstevel@tonic-gate 				return (set_errno(EFAULT));
6850Sstevel@tonic-gate 		}
6860Sstevel@tonic-gate 		break;
6870Sstevel@tonic-gate 
6886247Sraf 	case PC_DOPRIO:
6896247Sraf 		/*
6906247Sraf 		 * Get pcprio and procset structures from the user.
6916247Sraf 		 */
6926247Sraf 		if ((*copyinfn)(arg, &pcprio, sizeof (pcprio)) ||
6936247Sraf 		    (*copyinfn)(psp, &procset, sizeof (procset)))
6946247Sraf 			return (set_errno(EFAULT));
6956247Sraf 
6966247Sraf 		error = doprio(&procset, &pcprio);
6976247Sraf 
6986247Sraf 		if (!error && (pcprio.pc_op == PC_GETPRIO)) {
6996247Sraf 			if ((*copyoutfn)(&pcprio, arg, sizeof (pcprio)))
7006247Sraf 				return (set_errno(EFAULT));
7016247Sraf 		}
7026247Sraf 		break;
7036247Sraf 
7040Sstevel@tonic-gate 	case PC_SETDFLCL:
7050Sstevel@tonic-gate 		if (secpolicy_dispadm(CRED()) != 0)
7060Sstevel@tonic-gate 			return (set_errno(EPERM));
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 		if (copyin(arg, (caddr_t)clname, PC_CLNMSZ) != 0)
7090Sstevel@tonic-gate 			return (set_errno(EFAULT));
7100Sstevel@tonic-gate 		clname[PC_CLNMSZ-1] = '\0';
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		if (getcid(clname, &classid) != 0)
7130Sstevel@tonic-gate 			return (set_errno(EINVAL));
714*11173SJonathan.Adams@Sun.COM 		if (CLASS_KERNEL(classid))
7150Sstevel@tonic-gate 			return (set_errno(EINVAL));
7160Sstevel@tonic-gate 		defaultcid = classid;
7170Sstevel@tonic-gate 		ASSERT(defaultcid > 0 && defaultcid < loaded_classes);
7180Sstevel@tonic-gate 		break;
7190Sstevel@tonic-gate 
7203247Sgjelinek 	case PC_GETDFLCL:
7213247Sgjelinek 		mutex_enter(&class_lock);
7223247Sgjelinek 
7233247Sgjelinek 		if (defaultcid >= loaded_classes)
7243247Sgjelinek 			outstr = "";
7253247Sgjelinek 		else
7263247Sgjelinek 			outstr = sclass[defaultcid].cl_name;
7273247Sgjelinek 		size = strlen(outstr) + 1;
7283247Sgjelinek 		if (arg != NULL)
7293247Sgjelinek 			if ((*copyoutfn)(outstr, arg, size) != 0)
7303247Sgjelinek 				error = EFAULT;
7313247Sgjelinek 
7323247Sgjelinek 		mutex_exit(&class_lock);
7333247Sgjelinek 		break;
7343247Sgjelinek 
7350Sstevel@tonic-gate 	default:
7360Sstevel@tonic-gate 		error = EINVAL;
7370Sstevel@tonic-gate 		break;
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 	return (error ? (set_errno(error)) : rv);
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate 
7422712Snn35248 long
priocntlsys(int pc_version,procset_t * psp,int cmd,caddr_t arg,caddr_t arg2)7432712Snn35248 priocntlsys(int pc_version, procset_t *psp, int cmd, caddr_t arg, caddr_t arg2)
7442712Snn35248 {
7452712Snn35248 	return (priocntl_common(pc_version, psp, cmd, arg, arg2,
7462712Snn35248 	    UIO_USERSPACE));
7472712Snn35248 }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate /*
7500Sstevel@tonic-gate  * The proccmp() function is part of the implementation of the
7510Sstevel@tonic-gate  * PC_GETPARMS command of the priocntl system call.  This function works
7520Sstevel@tonic-gate  * with the system call code and with the class specific cl_globpri()
7530Sstevel@tonic-gate  * function to select one thread from a specified procset based on class
7540Sstevel@tonic-gate  * specific criteria. proccmp() is called indirectly from the priocntl
7550Sstevel@tonic-gate  * code through the dotoprocs function.  Basic strategy is dotoprocs()
7560Sstevel@tonic-gate  * calls us once for each thread in the set.  We in turn call the class
7570Sstevel@tonic-gate  * specific function to compare the current thread from dotoprocs to the
7580Sstevel@tonic-gate  * "best" (according to the class criteria) found so far.  We keep the
7590Sstevel@tonic-gate  * "best" thread in *pcmp_retthreadp.
7600Sstevel@tonic-gate  */
7610Sstevel@tonic-gate static int
proccmp(proc_t * pp,struct pcmpargs * argp)7620Sstevel@tonic-gate proccmp(proc_t *pp, struct pcmpargs *argp)
7630Sstevel@tonic-gate {
7646247Sraf 	kthread_t	*tx;
7656247Sraf 	kthread_t	*ty;
7660Sstevel@tonic-gate 	int		last_pri = -1;
7670Sstevel@tonic-gate 	int		tx_pri;
7680Sstevel@tonic-gate 	int		found = 0;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	if (pp->p_tlist == NULL) {
7730Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
7740Sstevel@tonic-gate 		return (0);
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 	(*argp->pcmp_cntp)++;	/* Increment count of procs in the set */
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	if (*argp->pcmp_cidp == PC_CLNULL) {
7790Sstevel@tonic-gate 		/*
7800Sstevel@tonic-gate 		 * If no cid is specified, then lets just pick the first one.
7810Sstevel@tonic-gate 		 * It doesn't matter because if the number of processes in the
7820Sstevel@tonic-gate 		 * set are more than 1, then we return EINVAL in priocntlsys.
7830Sstevel@tonic-gate 		 */
7840Sstevel@tonic-gate 		*argp->pcmp_cidp = pp->p_tlist->t_cid;
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 	ty = tx = pp->p_tlist;
7870Sstevel@tonic-gate 	do {
7880Sstevel@tonic-gate 		if (tx->t_cid == *argp->pcmp_cidp) {
7890Sstevel@tonic-gate 			/*
7900Sstevel@tonic-gate 			 * We found one which matches the required cid.
7910Sstevel@tonic-gate 			 */
7920Sstevel@tonic-gate 			found = 1;
7930Sstevel@tonic-gate 			if ((tx_pri = CL_GLOBPRI(tx)) > last_pri) {
7940Sstevel@tonic-gate 				last_pri = tx_pri;
7950Sstevel@tonic-gate 				ty = tx;
7960Sstevel@tonic-gate 			}
7970Sstevel@tonic-gate 		}
7980Sstevel@tonic-gate 	} while ((tx = tx->t_forw) != pp->p_tlist);
7990Sstevel@tonic-gate 	if (found) {
8000Sstevel@tonic-gate 		if (*argp->pcmp_retthreadp == NULL) {
8010Sstevel@tonic-gate 			/*
8020Sstevel@tonic-gate 			 * First time through for this set.
8030Sstevel@tonic-gate 			 * keep the mutex held. He might be the one!
8040Sstevel@tonic-gate 			 */
8050Sstevel@tonic-gate 			*argp->pcmp_retthreadp = ty;
8060Sstevel@tonic-gate 		} else {
8070Sstevel@tonic-gate 			tx = *argp->pcmp_retthreadp;
8080Sstevel@tonic-gate 			if (CL_GLOBPRI(ty) <= CL_GLOBPRI(tx)) {
8090Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
8100Sstevel@tonic-gate 			} else {
8110Sstevel@tonic-gate 				mutex_exit(&(ttoproc(tx)->p_lock));
8120Sstevel@tonic-gate 				*argp->pcmp_retthreadp = ty;
8130Sstevel@tonic-gate 			}
8140Sstevel@tonic-gate 		}
8150Sstevel@tonic-gate 	} else {
8160Sstevel@tonic-gate 		/*
8170Sstevel@tonic-gate 		 * We actually didn't find anything of the same cid in
8180Sstevel@tonic-gate 		 * this process.
8190Sstevel@tonic-gate 		 */
8200Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
8210Sstevel@tonic-gate 	}
8220Sstevel@tonic-gate 	return (0);
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate int
threadcmp(struct pcmpargs * argp,kthread_t * tp)8276247Sraf threadcmp(struct pcmpargs *argp, kthread_t *tp)
8280Sstevel@tonic-gate {
8296247Sraf 	kthread_t	*tx;
8300Sstevel@tonic-gate 	proc_t		*pp;
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(tp))->p_lock));
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	(*argp->pcmp_cntp)++;   /* Increment count of procs in the set */
8350Sstevel@tonic-gate 	if (*argp->pcmp_cidp == PC_CLNULL) {
8360Sstevel@tonic-gate 		/*
8370Sstevel@tonic-gate 		 * If no cid is specified, then lets just pick the first one.
8380Sstevel@tonic-gate 		 * It doesn't matter because if the number of threads in the
8390Sstevel@tonic-gate 		 * set are more than 1, then we return EINVAL in priocntlsys.
8400Sstevel@tonic-gate 		 */
8410Sstevel@tonic-gate 		*argp->pcmp_cidp = tp->t_cid;
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 	if (tp->t_cid == *argp->pcmp_cidp) {
8440Sstevel@tonic-gate 		if (*argp->pcmp_retthreadp == NULL) {
8450Sstevel@tonic-gate 			/*
8460Sstevel@tonic-gate 			 * First time through for this set.
8470Sstevel@tonic-gate 			 */
8480Sstevel@tonic-gate 			*argp->pcmp_retthreadp = tp;
8490Sstevel@tonic-gate 		} else {
8500Sstevel@tonic-gate 			tx = *argp->pcmp_retthreadp;
8510Sstevel@tonic-gate 			if (CL_GLOBPRI(tp) > CL_GLOBPRI(tx)) {
8520Sstevel@tonic-gate 				/*
8530Sstevel@tonic-gate 				 * Unlike proccmp(), we don't release the
8540Sstevel@tonic-gate 				 * p_lock of the ttoproc(tp) if tp's global
8550Sstevel@tonic-gate 				 * priority is less than tx's. We need to go
8560Sstevel@tonic-gate 				 * through the entire list before we can do
8570Sstevel@tonic-gate 				 * that. The p_lock is released by the caller
8580Sstevel@tonic-gate 				 * of dotolwp().
8590Sstevel@tonic-gate 				 */
8600Sstevel@tonic-gate 				pp = ttoproc(tx);
8610Sstevel@tonic-gate 				ASSERT(MUTEX_HELD(&pp->p_lock));
8620Sstevel@tonic-gate 				if (pp != curproc) {
8630Sstevel@tonic-gate 					mutex_exit(&pp->p_lock);
8640Sstevel@tonic-gate 				}
8650Sstevel@tonic-gate 				*argp->pcmp_retthreadp = tp;
8660Sstevel@tonic-gate 			}
8670Sstevel@tonic-gate 		}
8680Sstevel@tonic-gate 	}
8690Sstevel@tonic-gate 	return (0);
8700Sstevel@tonic-gate }
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate /*
8740Sstevel@tonic-gate  * The setparms() function is called indirectly by priocntlsys()
8750Sstevel@tonic-gate  * through the dotoprocs() function.  setparms() acts as an
8760Sstevel@tonic-gate  * intermediary between dotoprocs() and the parmsset() function,
8770Sstevel@tonic-gate  * calling parmsset() for each thread in the set and handling
8780Sstevel@tonic-gate  * the error returns on their way back up to dotoprocs().
8790Sstevel@tonic-gate  */
8800Sstevel@tonic-gate static int
setparms(proc_t * targpp,struct stprmargs * stprmp)8810Sstevel@tonic-gate setparms(proc_t *targpp, struct stprmargs *stprmp)
8820Sstevel@tonic-gate {
8830Sstevel@tonic-gate 	int error = 0;
8846247Sraf 	kthread_t *t;
8850Sstevel@tonic-gate 	int err;
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	mutex_enter(&targpp->p_lock);
8880Sstevel@tonic-gate 	if ((t = targpp->p_tlist) == NULL) {
8890Sstevel@tonic-gate 		mutex_exit(&targpp->p_lock);
8900Sstevel@tonic-gate 		return (0);
8910Sstevel@tonic-gate 	}
8920Sstevel@tonic-gate 	do {
8930Sstevel@tonic-gate 		err = parmsset(stprmp->stp_parmsp, t);
8940Sstevel@tonic-gate 		if (error == 0)
8950Sstevel@tonic-gate 			error = err;
8960Sstevel@tonic-gate 	} while ((t = t->t_forw) != targpp->p_tlist);
8970Sstevel@tonic-gate 	mutex_exit(&targpp->p_lock);
8980Sstevel@tonic-gate 	if (error) {
8990Sstevel@tonic-gate 		if (error == EPERM) {
9000Sstevel@tonic-gate 			stprmp->stp_error = EPERM;
9010Sstevel@tonic-gate 			return (0);
9020Sstevel@tonic-gate 		} else {
9030Sstevel@tonic-gate 			return (error);
9040Sstevel@tonic-gate 		}
9050Sstevel@tonic-gate 	} else
9060Sstevel@tonic-gate 		return (0);
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate 
9092712Snn35248 int
setthreadnice(pcnice_t * pcnice,kthread_t * tp)9100Sstevel@tonic-gate setthreadnice(pcnice_t *pcnice, kthread_t *tp)
9110Sstevel@tonic-gate {
9126247Sraf 	int error;
9130Sstevel@tonic-gate 	int nice;
9140Sstevel@tonic-gate 	int inc;
9150Sstevel@tonic-gate 	id_t rtcid;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
9180Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&(ttoproc(tp)->p_lock)));
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	/*
9210Sstevel@tonic-gate 	 * The XPG5 standard requires that any realtime process or thread
9220Sstevel@tonic-gate 	 * must be unaffected by a call to setpriority().
9230Sstevel@tonic-gate 	 */
9240Sstevel@tonic-gate 	error = getcidbyname("RT", &rtcid);
9256247Sraf 	if (error == 0 && tp->t_cid == rtcid) {
9260Sstevel@tonic-gate 		if (pcnice->pc_op == PC_SETNICE)
9276247Sraf 			return (0);
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	if ((error = CL_DONICE(tp, CRED(), 0, &nice)) != 0)
9310Sstevel@tonic-gate 		return (error);
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	if (pcnice->pc_op == PC_GETNICE) {
9340Sstevel@tonic-gate 		/*
9350Sstevel@tonic-gate 		 * If there is no change to priority, we should return the
9360Sstevel@tonic-gate 		 * highest priority (lowest numerical value) pertaining to
9370Sstevel@tonic-gate 		 * any of the specified threads.
9380Sstevel@tonic-gate 		 */
9390Sstevel@tonic-gate 		if (nice < pcnice->pc_val)
9400Sstevel@tonic-gate 			pcnice->pc_val = nice;
9410Sstevel@tonic-gate 	} else {
9420Sstevel@tonic-gate 		ASSERT(pcnice->pc_op == PC_SETNICE);
9430Sstevel@tonic-gate 		/*
9440Sstevel@tonic-gate 		 * Try to change the nice value of the thread.
9450Sstevel@tonic-gate 		 */
9460Sstevel@tonic-gate 		inc = pcnice->pc_val - nice;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 		error = CL_DONICE(tp, CRED(), inc, &inc);
9496247Sraf 		schedctl_set_cidpri(tp);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	return (error);
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate 
9552712Snn35248 int
setprocnice(proc_t * pp,pcnice_t * pcnice)9560Sstevel@tonic-gate setprocnice(proc_t *pp, pcnice_t *pcnice)
9570Sstevel@tonic-gate {
9580Sstevel@tonic-gate 	kthread_t *tp;
9590Sstevel@tonic-gate 	int retval = 0;
9606247Sraf 	int error;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
9630Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	if ((tp = pp->p_tlist) == NULL) {
9660Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
9670Sstevel@tonic-gate 		return (ESRCH);
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	/*
9710Sstevel@tonic-gate 	 * Check permissions before changing the nice value.
9720Sstevel@tonic-gate 	 */
9730Sstevel@tonic-gate 	if (pcnice->pc_op == PC_SETNICE) {
9740Sstevel@tonic-gate 		if (!prochasprocperm(pp, curproc, CRED())) {
9750Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
9760Sstevel@tonic-gate 			return (EPERM);
9770Sstevel@tonic-gate 		}
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	do {
9810Sstevel@tonic-gate 		error = setthreadnice(pcnice, tp);
9820Sstevel@tonic-gate 		if (error)
9830Sstevel@tonic-gate 			retval = error;
9840Sstevel@tonic-gate 	} while ((tp = tp->t_forw) != pp->p_tlist);
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
9870Sstevel@tonic-gate 	return (retval);
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate /*
9910Sstevel@tonic-gate  * Update the nice value of the specified LWP or set of processes.
9920Sstevel@tonic-gate  */
9930Sstevel@tonic-gate static int
donice(procset_t * procset,pcnice_t * pcnice)9940Sstevel@tonic-gate donice(procset_t *procset, pcnice_t *pcnice)
9950Sstevel@tonic-gate {
9960Sstevel@tonic-gate 	int err_proc = 0;
9970Sstevel@tonic-gate 	int err_thread = 0;
9980Sstevel@tonic-gate 	int err = 0;
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	/*
10010Sstevel@tonic-gate 	 * Sanity check.
10020Sstevel@tonic-gate 	 */
10030Sstevel@tonic-gate 	if (pcnice->pc_op != PC_GETNICE && pcnice->pc_op != PC_SETNICE)
10040Sstevel@tonic-gate 		return (EINVAL);
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	/*
10070Sstevel@tonic-gate 	 * If it is PC_GETNICE operation then set pc_val to the largest
10080Sstevel@tonic-gate 	 * possible nice value to help us find the lowest nice value
10090Sstevel@tonic-gate 	 * pertaining to any of the specified processes.
10100Sstevel@tonic-gate 	 */
10110Sstevel@tonic-gate 	if (pcnice->pc_op == PC_GETNICE)
10120Sstevel@tonic-gate 		pcnice->pc_val = NZERO;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	if (procset->p_lidtype != P_LWPID ||
10150Sstevel@tonic-gate 	    procset->p_ridtype != P_LWPID)
10160Sstevel@tonic-gate 		err_proc = dotoprocs(procset, setprocnice, (char *)pcnice);
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	if (procset->p_lidtype == P_LWPID || procset->p_ridtype == P_LWPID) {
10190Sstevel@tonic-gate 		err_thread = dotolwp(procset, setthreadnice, (char *)pcnice);
10200Sstevel@tonic-gate 		/*
10210Sstevel@tonic-gate 		 * dotolwp() can return with p_lock held.  This is required
10220Sstevel@tonic-gate 		 * for the priocntl GETPARMS case.  So, here we just release
10230Sstevel@tonic-gate 		 * the p_lock.
10240Sstevel@tonic-gate 		 */
10250Sstevel@tonic-gate 		if (MUTEX_HELD(&curproc->p_lock))
10260Sstevel@tonic-gate 			mutex_exit(&curproc->p_lock);
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 		/*
10290Sstevel@tonic-gate 		 * If we were called for a single LWP, then ignore ESRCH
10300Sstevel@tonic-gate 		 * returned by the previous dotoprocs() call.
10310Sstevel@tonic-gate 		 */
10320Sstevel@tonic-gate 		if (err_proc == ESRCH)
10330Sstevel@tonic-gate 			err_proc = 0;
10340Sstevel@tonic-gate 	}
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	/*
10370Sstevel@tonic-gate 	 * dotoprocs() ignores the init process if it is in the set, unless
10380Sstevel@tonic-gate 	 * it was the only process found. We want to make sure init is not
10390Sstevel@tonic-gate 	 * excluded if we're going PC_GETNICE operation.
10400Sstevel@tonic-gate 	 */
10410Sstevel@tonic-gate 	if (pcnice->pc_op == PC_GETNICE) {
10420Sstevel@tonic-gate 		proc_t *initpp;
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 		mutex_enter(&pidlock);
10456618Srh87107 		if ((initpp = prfind(P_INITPID)) != NULL) {
10466618Srh87107 			mutex_enter(&initpp->p_lock);
10476618Srh87107 			if (procinset(initpp, procset)) {
10486618Srh87107 				mutex_exit(&initpp->p_lock);
10496618Srh87107 				err = setprocnice(initpp, pcnice);
10506618Srh87107 			} else {
10516618Srh87107 				mutex_exit(&initpp->p_lock);
10526618Srh87107 			}
10536618Srh87107 		}
10540Sstevel@tonic-gate 		mutex_exit(&pidlock);
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	/*
10580Sstevel@tonic-gate 	 * We're returning the latest error here that we've got back from
10590Sstevel@tonic-gate 	 * the setthreadnice() or setprocnice(). That is, err_thread and/or
10600Sstevel@tonic-gate 	 * err_proc can be replaced by err.
10610Sstevel@tonic-gate 	 */
10620Sstevel@tonic-gate 	if (!err)
10630Sstevel@tonic-gate 		err = err_thread ? err_thread : err_proc;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	return (err);
10660Sstevel@tonic-gate }
10676247Sraf 
10686247Sraf int
setthreadprio(pcprio_t * pcprio,kthread_t * tp)10696247Sraf setthreadprio(pcprio_t *pcprio, kthread_t *tp)
10706247Sraf {
10716247Sraf 	int prio = 0;
10726247Sraf 	int incr;
10736247Sraf 	int error;
10746247Sraf 
10756247Sraf 	ASSERT(MUTEX_HELD(&pidlock));
10766247Sraf 	ASSERT(MUTEX_HELD(&(ttoproc(tp)->p_lock)));
10776247Sraf 
10786247Sraf 	if (pcprio->pc_op == PC_SETPRIO && pcprio->pc_cid != tp->t_cid) {
10796247Sraf 		/*
10806247Sraf 		 * Target thread must change to new class.
10816247Sraf 		 * See comments in parmsset(), from where this code was copied.
10826247Sraf 		 */
10836247Sraf 		void *bufp = NULL;
10846247Sraf 		caddr_t clprocp = (caddr_t)tp->t_cldata;
10856247Sraf 		id_t oldcid = tp->t_cid;
10866247Sraf 
10876247Sraf 		error = CL_CANEXIT(tp, NULL);
10886247Sraf 		if (error)
10896247Sraf 			return (error);
10906247Sraf 		if (CL_ALLOC(&bufp, pcprio->pc_cid, KM_NOSLEEP) != 0)
10916247Sraf 			return (ENOMEM);
10926247Sraf 		error = CL_ENTERCLASS(tp, pcprio->pc_cid, NULL, CRED(), bufp);
10936247Sraf 		if (error) {
10946247Sraf 			CL_FREE(pcprio->pc_cid, bufp);
10956247Sraf 			return (error);
10966247Sraf 		}
10976247Sraf 		CL_EXITCLASS(oldcid, clprocp);
10986247Sraf 		schedctl_set_cidpri(tp);
10996247Sraf 	}
11006247Sraf 
11016247Sraf 	if ((error = CL_DOPRIO(tp, CRED(), 0, &prio)) != 0)
11026247Sraf 		return (error);
11036247Sraf 
11046247Sraf 	if (pcprio->pc_op == PC_GETPRIO) {
11056247Sraf 		/*
11066247Sraf 		 * If we are not setting the priority, we should return the
11076247Sraf 		 * highest priority pertaining to any of the specified threads.
11086247Sraf 		 */
11096247Sraf 		if (prio > pcprio->pc_val) {
11106247Sraf 			pcprio->pc_cid = tp->t_cid;
11116247Sraf 			pcprio->pc_val = prio;
11126247Sraf 		}
11136247Sraf 	} else if (prio != pcprio->pc_val) {
11146247Sraf 		/*
11156247Sraf 		 * Try to change the priority of the thread.
11166247Sraf 		 */
11176247Sraf 		incr = pcprio->pc_val - prio;
11186247Sraf 		error = CL_DOPRIO(tp, CRED(), incr, &prio);
11196247Sraf 		schedctl_set_cidpri(tp);
11206247Sraf 	}
11216247Sraf 
11226247Sraf 	return (error);
11236247Sraf }
11246247Sraf 
11256247Sraf int
setprocprio(proc_t * pp,pcprio_t * pcprio)11266247Sraf setprocprio(proc_t *pp, pcprio_t *pcprio)
11276247Sraf {
11286247Sraf 	kthread_t *tp;
11296247Sraf 	int retval = 0;
11306247Sraf 	int error;
11316247Sraf 
11326247Sraf 	ASSERT(MUTEX_HELD(&pidlock));
11336247Sraf 	mutex_enter(&pp->p_lock);
11346247Sraf 
11356247Sraf 	if ((tp = pp->p_tlist) == NULL) {
11366247Sraf 		mutex_exit(&pp->p_lock);
11376247Sraf 		return (ESRCH);
11386247Sraf 	}
11396247Sraf 
11406247Sraf 	/*
11416247Sraf 	 * Check permissions before changing the prio value.
11426247Sraf 	 */
11436247Sraf 	if (pcprio->pc_op == PC_SETPRIO) {
11446247Sraf 		if (!prochasprocperm(pp, curproc, CRED())) {
11456247Sraf 			mutex_exit(&pp->p_lock);
11466247Sraf 			return (EPERM);
11476247Sraf 		}
11486247Sraf 	}
11496247Sraf 
11506247Sraf 	do {
11516247Sraf 		error = setthreadprio(pcprio, tp);
11526247Sraf 		if (error)
11536247Sraf 			retval = error;
11546247Sraf 	} while ((tp = tp->t_forw) != pp->p_tlist);
11556247Sraf 
11566247Sraf 	mutex_exit(&pp->p_lock);
11576247Sraf 	return (retval);
11586247Sraf }
11596247Sraf 
11606247Sraf /*
11616247Sraf  * Set the class and priority of the specified LWP or set of processes.
11626247Sraf  */
11636247Sraf static int
doprio(procset_t * procset,pcprio_t * pcprio)11646247Sraf doprio(procset_t *procset, pcprio_t *pcprio)
11656247Sraf {
11666247Sraf 	int err_proc = 0;
11676247Sraf 	int err_thread = 0;
11686247Sraf 	int err = 0;
11696247Sraf 
11706247Sraf 	/*
11716247Sraf 	 * Sanity check.
11726247Sraf 	 */
11736247Sraf 	if (pcprio->pc_op != PC_GETPRIO && pcprio->pc_op != PC_SETPRIO)
11746247Sraf 		return (EINVAL);
11756247Sraf 	if (pcprio->pc_op == PC_SETPRIO &&
11766247Sraf 	    (pcprio->pc_cid >= loaded_classes || pcprio->pc_cid < 1))
11776247Sraf 		return (EINVAL);
11786247Sraf 
11796247Sraf 	/*
11806247Sraf 	 * If it is a PC_GETPRIO operation then set pc_val to the smallest
11816247Sraf 	 * possible prio value to help us find the highest priority
11826247Sraf 	 * pertaining to any of the specified processes.
11836247Sraf 	 */
11846247Sraf 	if (pcprio->pc_op == PC_GETPRIO)
11856247Sraf 		pcprio->pc_val = SHRT_MIN;
11866247Sraf 
11876247Sraf 	if (procset->p_lidtype != P_LWPID ||
11886247Sraf 	    procset->p_ridtype != P_LWPID)
11896247Sraf 		err_proc = dotoprocs(procset, setprocprio, (char *)pcprio);
11906247Sraf 
11916247Sraf 	if (procset->p_lidtype == P_LWPID || procset->p_ridtype == P_LWPID) {
11926247Sraf 		err_thread = dotolwp(procset, setthreadprio, (char *)pcprio);
11936247Sraf 		/*
11946247Sraf 		 * dotolwp() can return with p_lock held.  This is required
11956247Sraf 		 * for the priocntl GETPARMS case.  So, here we just release
11966247Sraf 		 * the p_lock.
11976247Sraf 		 */
11986247Sraf 		if (MUTEX_HELD(&curproc->p_lock))
11996247Sraf 			mutex_exit(&curproc->p_lock);
12006247Sraf 
12016247Sraf 		/*
12026247Sraf 		 * If we were called for a single LWP, then ignore ESRCH
12036247Sraf 		 * returned by the previous dotoprocs() call.
12046247Sraf 		 */
12056247Sraf 		if (err_proc == ESRCH)
12066247Sraf 			err_proc = 0;
12076247Sraf 	}
12086247Sraf 
12096247Sraf 	/*
12106247Sraf 	 * dotoprocs() ignores the init process if it is in the set, unless
12116247Sraf 	 * it was the only process found. We want to make sure init is not
12126247Sraf 	 * excluded if we're going PC_GETPRIO operation.
12136247Sraf 	 */
12146247Sraf 	if (pcprio->pc_op == PC_GETPRIO) {
12156247Sraf 		proc_t *initpp;
12166247Sraf 
12176247Sraf 		mutex_enter(&pidlock);
12186618Srh87107 		if ((initpp = prfind(P_INITPID)) != NULL) {
12196618Srh87107 			mutex_enter(&initpp->p_lock);
12206618Srh87107 			if (procinset(initpp, procset)) {
12216618Srh87107 				mutex_exit(&initpp->p_lock);
12226618Srh87107 				err = setprocprio(initpp, pcprio);
12236618Srh87107 			} else {
12246618Srh87107 				mutex_exit(&initpp->p_lock);
12256618Srh87107 			}
12266618Srh87107 		}
12276247Sraf 		mutex_exit(&pidlock);
12286247Sraf 	}
12296247Sraf 
12306247Sraf 	/*
12316247Sraf 	 * We're returning the latest error here that we've got back from
12326247Sraf 	 * the setthreadprio() or setprocprio(). That is, err_thread and/or
12336247Sraf 	 * err_proc can be replaced by err.
12346247Sraf 	 */
12356247Sraf 	if (!err)
12366247Sraf 		err = err_thread ? err_thread : err_proc;
12376247Sraf 
12386247Sraf 	return (err);
12396247Sraf }
1240