xref: /titanic_51/usr/src/uts/common/os/procset.c (revision ddc470cc019b5d52e3c2546a8b3104e189611b71)
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
59acbbeafSnn35248  * Common Development and Distribution License (the "License").
69acbbeafSnn35248  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*ddc470ccSjimp  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* from SVr4.0 1.25 */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
377c478bd9Sstevel@tonic-gate #include <sys/cred.h>
387c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
397c478bd9Sstevel@tonic-gate #include <sys/file.h>
407c478bd9Sstevel@tonic-gate #include <sys/errno.h>
417c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
427c478bd9Sstevel@tonic-gate #include <sys/user.h>
437c478bd9Sstevel@tonic-gate #include <sys/buf.h>
447c478bd9Sstevel@tonic-gate #include <sys/var.h>
457c478bd9Sstevel@tonic-gate #include <sys/conf.h>
467c478bd9Sstevel@tonic-gate #include <sys/debug.h>
477c478bd9Sstevel@tonic-gate #include <sys/proc.h>
487c478bd9Sstevel@tonic-gate #include <sys/signal.h>
497c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
507c478bd9Sstevel@tonic-gate #include <sys/acct.h>
517c478bd9Sstevel@tonic-gate #include <sys/procset.h>
527c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
537c478bd9Sstevel@tonic-gate #include <sys/fault.h>
547c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
557c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
567c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
577c478bd9Sstevel@tonic-gate #include <sys/session.h>
587c478bd9Sstevel@tonic-gate #include <sys/task.h>
597c478bd9Sstevel@tonic-gate #include <sys/project.h>
607c478bd9Sstevel@tonic-gate #include <sys/pool.h>
617c478bd9Sstevel@tonic-gate #include <sys/zone.h>
627c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate id_t	getmyid(idtype_t);
657c478bd9Sstevel@tonic-gate int	checkprocset(procset_t *);
667c478bd9Sstevel@tonic-gate static	kthread_t *getlwpptr(id_t);
677c478bd9Sstevel@tonic-gate int	procinset(proc_t *, procset_t *);
687c478bd9Sstevel@tonic-gate static	int lwpinset(proc_t *, procset_t *, kthread_t *, int *);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * The dotoprocs function locates the process(es) specified
72*ddc470ccSjimp  * by the procset structure pointed to by psp.  funcp points to a
73*ddc470ccSjimp  * function which dotoprocs will call for each process in the
74*ddc470ccSjimp  * specified set.  The arguments to this function will be a pointer
75*ddc470ccSjimp  * to the current process from the set and arg.
767c478bd9Sstevel@tonic-gate  * If the called function returns -1, it means that processing of the
777c478bd9Sstevel@tonic-gate  * procset should stop and a normal (non-error) return should be made
787c478bd9Sstevel@tonic-gate  * to the caller of dotoprocs.
797c478bd9Sstevel@tonic-gate  * If the called function returns any other non-zero value the search
807c478bd9Sstevel@tonic-gate  * is terminated and the function's return value is returned to
817c478bd9Sstevel@tonic-gate  * the caller of dotoprocs.  This will normally be an error code.
827c478bd9Sstevel@tonic-gate  * Otherwise, dotoprocs will return zero after processing the entire
837c478bd9Sstevel@tonic-gate  * process set unless no processes were found in which case ESRCH will
847c478bd9Sstevel@tonic-gate  * be returned.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate int
877c478bd9Sstevel@tonic-gate dotoprocs(procset_t *psp, int (*funcp)(), char *arg)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	proc_t	*prp;	/* A process from the set */
907c478bd9Sstevel@tonic-gate 	int	error;
917c478bd9Sstevel@tonic-gate 	int	nfound;	/* Nbr of processes found.	*/
927c478bd9Sstevel@tonic-gate 	proc_t	*lastprp;	/* Last proc found.	*/
937c478bd9Sstevel@tonic-gate 
94*ddc470ccSjimp 	ASSERT(funcp != NULL);
95*ddc470ccSjimp 
967c478bd9Sstevel@tonic-gate 	/*
977c478bd9Sstevel@tonic-gate 	 * Check that the procset_t is valid.
987c478bd9Sstevel@tonic-gate 	 */
997c478bd9Sstevel@tonic-gate 	error = checkprocset(psp);
1007c478bd9Sstevel@tonic-gate 	if (error) {
1017c478bd9Sstevel@tonic-gate 		return (error);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 	/*
1047c478bd9Sstevel@tonic-gate 	 * Check for the special value P_MYID in either operand
1057c478bd9Sstevel@tonic-gate 	 * and replace it with the correct value.  We don't check
1067c478bd9Sstevel@tonic-gate 	 * for an error return from getmyid() because the idtypes
1077c478bd9Sstevel@tonic-gate 	 * have been validated by the checkprocset() call above.
1087c478bd9Sstevel@tonic-gate 	 */
1097c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
1107c478bd9Sstevel@tonic-gate 	if (psp->p_lid == P_MYID) {
1117c478bd9Sstevel@tonic-gate 		psp->p_lid = getmyid(psp->p_lidtype);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	if (psp->p_rid == P_MYID) {
1147c478bd9Sstevel@tonic-gate 		psp->p_rid = getmyid(psp->p_ridtype);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * If psp only acts on a single proc, we can reduce pidlock hold time
1197c478bd9Sstevel@tonic-gate 	 * by avoiding a needless scan of the entire proc list.  Although
1207c478bd9Sstevel@tonic-gate 	 * there are many procset_t combinations which might boil down to a
1217c478bd9Sstevel@tonic-gate 	 * single proc, the most common case is an AND operation where one
1227c478bd9Sstevel@tonic-gate 	 * side is a specific pid, and the other side is P_ALL, so that is
1237c478bd9Sstevel@tonic-gate 	 * the case for which we will provide a fast path.  Other cases could
1247c478bd9Sstevel@tonic-gate 	 * be added in a similar fashion if they were to become significant
1257c478bd9Sstevel@tonic-gate 	 * pidlock bottlenecks.
1267c478bd9Sstevel@tonic-gate 	 *
1277c478bd9Sstevel@tonic-gate 	 * Perform the check symmetrically:  either the left or right side may
1287c478bd9Sstevel@tonic-gate 	 * specify a pid, with the opposite side being 'all'.
1297c478bd9Sstevel@tonic-gate 	 */
1307c478bd9Sstevel@tonic-gate 	if (psp->p_op == POP_AND) {
1317c478bd9Sstevel@tonic-gate 		if (((psp->p_lidtype == P_PID) && (psp->p_ridtype == P_ALL)) ||
1327c478bd9Sstevel@tonic-gate 		    ((psp->p_ridtype == P_PID) && (psp->p_lidtype == P_ALL))) {
1337c478bd9Sstevel@tonic-gate 			id_t pid;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 			pid = (psp->p_lidtype == P_PID) ?
1367c478bd9Sstevel@tonic-gate 			    psp->p_lid : psp->p_rid;
137*ddc470ccSjimp 			if (((prp = prfind((pid_t)pid)) == NULL) ||
138*ddc470ccSjimp 			    (prp->p_stat == SIDL || prp->p_stat == SZOMB ||
139*ddc470ccSjimp 			    prp->p_tlist == NULL || prp->p_flag & SSYS)) {
140*ddc470ccSjimp 				/*
141*ddc470ccSjimp 				 * Specified proc doesn't exist or should
142*ddc470ccSjimp 				 * not be operated on.
143*ddc470ccSjimp 				 * Don't need to make HASZONEACCESS check
144*ddc470ccSjimp 				 * here since prfind() takes care of that.
145*ddc470ccSjimp 				 */
1467c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
1477c478bd9Sstevel@tonic-gate 				return (ESRCH);
1487c478bd9Sstevel@tonic-gate 			}
149*ddc470ccSjimp 			/*
150*ddc470ccSjimp 			 * Operate only on the specified proc.  It's okay
151*ddc470ccSjimp 			 * if it's init.
152*ddc470ccSjimp 			 */
1537c478bd9Sstevel@tonic-gate 			error = (*funcp)(prp, arg);
1547c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1557c478bd9Sstevel@tonic-gate 			if (error == -1)
1567c478bd9Sstevel@tonic-gate 				error = 0;
1577c478bd9Sstevel@tonic-gate 			return (error);
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	nfound = 0;
1627c478bd9Sstevel@tonic-gate 	error  = 0;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	for (prp = practive; prp != NULL; prp = prp->p_next) {
1657c478bd9Sstevel@tonic-gate 		/*
1667c478bd9Sstevel@tonic-gate 		 * If caller is in a non-global zone, skip processes
1677c478bd9Sstevel@tonic-gate 		 * in other zones.
1687c478bd9Sstevel@tonic-gate 		 */
1697c478bd9Sstevel@tonic-gate 		if (!HASZONEACCESS(curproc, prp->p_zone->zone_id))
1707c478bd9Sstevel@tonic-gate 			continue;
1717c478bd9Sstevel@tonic-gate 		if (prp->p_stat == SIDL || prp->p_stat == SZOMB ||
1727c478bd9Sstevel@tonic-gate 		    prp->p_tlist == NULL || prp->p_flag & SSYS)
1737c478bd9Sstevel@tonic-gate 			continue;
1747c478bd9Sstevel@tonic-gate 		if (procinset(prp, psp)) {
1757c478bd9Sstevel@tonic-gate 			nfound++;
1767c478bd9Sstevel@tonic-gate 			lastprp = prp;
177*ddc470ccSjimp 			if (prp != proc_init) {
1787c478bd9Sstevel@tonic-gate 				error = (*funcp)(prp, arg);
1797c478bd9Sstevel@tonic-gate 				if (error == -1) {
1807c478bd9Sstevel@tonic-gate 					mutex_exit(&pidlock);
1817c478bd9Sstevel@tonic-gate 					return (0);
1827c478bd9Sstevel@tonic-gate 				} else if (error) {
1837c478bd9Sstevel@tonic-gate 					mutex_exit(&pidlock);
1847c478bd9Sstevel@tonic-gate 					return (error);
1857c478bd9Sstevel@tonic-gate 				}
1867c478bd9Sstevel@tonic-gate 			}
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	if (nfound == 0) {
1907c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
1917c478bd9Sstevel@tonic-gate 		return (ESRCH);
1927c478bd9Sstevel@tonic-gate 	}
193*ddc470ccSjimp 	if (nfound == 1 && lastprp == proc_init)
1947c478bd9Sstevel@tonic-gate 		error = (*funcp)(lastprp, arg);
1957c478bd9Sstevel@tonic-gate 	if (error == -1)
1967c478bd9Sstevel@tonic-gate 		error = 0;
1977c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
1987c478bd9Sstevel@tonic-gate 	return (error);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * Check if a procset_t is valid.  Return zero or an errno.
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate int
2057c478bd9Sstevel@tonic-gate checkprocset(procset_t *psp)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	switch (psp->p_lidtype) {
2087c478bd9Sstevel@tonic-gate 	case P_LWPID:
2097c478bd9Sstevel@tonic-gate 	case P_PID:
2107c478bd9Sstevel@tonic-gate 	case P_PPID:
2117c478bd9Sstevel@tonic-gate 	case P_PGID:
2127c478bd9Sstevel@tonic-gate 	case P_SID:
2137c478bd9Sstevel@tonic-gate 	case P_TASKID:
2147c478bd9Sstevel@tonic-gate 	case P_CID:
2157c478bd9Sstevel@tonic-gate 	case P_UID:
2167c478bd9Sstevel@tonic-gate 	case P_GID:
2177c478bd9Sstevel@tonic-gate 	case P_PROJID:
2187c478bd9Sstevel@tonic-gate 	case P_POOLID:
2197c478bd9Sstevel@tonic-gate 	case P_ZONEID:
2207c478bd9Sstevel@tonic-gate 	case P_CTID:
2217c478bd9Sstevel@tonic-gate 	case P_ALL:
2227c478bd9Sstevel@tonic-gate 		break;
2237c478bd9Sstevel@tonic-gate 	default:
2247c478bd9Sstevel@tonic-gate 		return (EINVAL);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	switch (psp->p_ridtype) {
2287c478bd9Sstevel@tonic-gate 	case P_LWPID:
2297c478bd9Sstevel@tonic-gate 	case P_PID:
2307c478bd9Sstevel@tonic-gate 	case P_PPID:
2317c478bd9Sstevel@tonic-gate 	case P_PGID:
2327c478bd9Sstevel@tonic-gate 	case P_SID:
2337c478bd9Sstevel@tonic-gate 	case P_TASKID:
2347c478bd9Sstevel@tonic-gate 	case P_CID:
2357c478bd9Sstevel@tonic-gate 	case P_UID:
2367c478bd9Sstevel@tonic-gate 	case P_GID:
2377c478bd9Sstevel@tonic-gate 	case P_PROJID:
2387c478bd9Sstevel@tonic-gate 	case P_POOLID:
2397c478bd9Sstevel@tonic-gate 	case P_ZONEID:
2407c478bd9Sstevel@tonic-gate 	case P_CTID:
2417c478bd9Sstevel@tonic-gate 	case P_ALL:
2427c478bd9Sstevel@tonic-gate 		break;
2437c478bd9Sstevel@tonic-gate 	default:
2447c478bd9Sstevel@tonic-gate 		return (EINVAL);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	switch (psp->p_op) {
2487c478bd9Sstevel@tonic-gate 	case POP_DIFF:
2497c478bd9Sstevel@tonic-gate 	case POP_AND:
2507c478bd9Sstevel@tonic-gate 	case POP_OR:
2517c478bd9Sstevel@tonic-gate 	case POP_XOR:
2527c478bd9Sstevel@tonic-gate 		break;
2537c478bd9Sstevel@tonic-gate 	default:
2547c478bd9Sstevel@tonic-gate 		return (EINVAL);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	return (0);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * procinset returns 1 if the process pointed to
2627c478bd9Sstevel@tonic-gate  * by pp is in the process set specified by psp, otherwise 0 is returned.
2637c478bd9Sstevel@tonic-gate  * The caller should check that the process is not exiting and is not
2647c478bd9Sstevel@tonic-gate  * in the SYS scheduling class.
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * This function expects to be called with a valid procset_t.
2677c478bd9Sstevel@tonic-gate  * The set should be checked using checkprocset() before calling
2687c478bd9Sstevel@tonic-gate  * this function.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate int
2717c478bd9Sstevel@tonic-gate procinset(proc_t *pp, procset_t *psp)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	int	loperand = 0;
2747c478bd9Sstevel@tonic-gate 	int	roperand = 0;
2757c478bd9Sstevel@tonic-gate 	int	lwplinproc = 0;
2767c478bd9Sstevel@tonic-gate 	int	lwprinproc = 0;
2777c478bd9Sstevel@tonic-gate 	kthread_t	*tp = proctot(pp);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	switch (psp->p_lidtype) {
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	case P_LWPID:
2827c478bd9Sstevel@tonic-gate 		if (pp == ttoproc(curthread))
2837c478bd9Sstevel@tonic-gate 			if (getlwpptr(psp->p_lid) != NULL)
2847c478bd9Sstevel@tonic-gate 				lwplinproc++;
2857c478bd9Sstevel@tonic-gate 		break;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	case P_PID:
2887c478bd9Sstevel@tonic-gate 		if (pp->p_pid == psp->p_lid)
2897c478bd9Sstevel@tonic-gate 			loperand++;
2907c478bd9Sstevel@tonic-gate 		break;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	case P_PPID:
2937c478bd9Sstevel@tonic-gate 		if (pp->p_ppid == psp->p_lid)
2947c478bd9Sstevel@tonic-gate 			loperand++;
2957c478bd9Sstevel@tonic-gate 		break;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	case P_PGID:
2987c478bd9Sstevel@tonic-gate 		if (pp->p_pgrp == psp->p_lid)
2997c478bd9Sstevel@tonic-gate 			loperand++;
3007c478bd9Sstevel@tonic-gate 		break;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	case P_SID:
3039acbbeafSnn35248 		mutex_enter(&pp->p_splock);
3047c478bd9Sstevel@tonic-gate 		if (pp->p_sessp->s_sid == psp->p_lid)
3057c478bd9Sstevel@tonic-gate 			loperand++;
3069acbbeafSnn35248 		mutex_exit(&pp->p_splock);
3077c478bd9Sstevel@tonic-gate 		break;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	case P_CID:
3107c478bd9Sstevel@tonic-gate 		ASSERT(tp != NULL);
3117c478bd9Sstevel@tonic-gate 		/* This case is broken for now. Need to be fixed XXX */
3127c478bd9Sstevel@tonic-gate 		if (tp->t_cid == psp->p_lid)
3137c478bd9Sstevel@tonic-gate 			/*
3147c478bd9Sstevel@tonic-gate 			 * if (checkcid(psp->p_lid))
3157c478bd9Sstevel@tonic-gate 			 */
3167c478bd9Sstevel@tonic-gate 			loperand++;
3177c478bd9Sstevel@tonic-gate 		break;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	case P_TASKID:
3207c478bd9Sstevel@tonic-gate 		if (pp->p_task->tk_tkid == psp->p_lid)
3217c478bd9Sstevel@tonic-gate 			loperand++;
3227c478bd9Sstevel@tonic-gate 		break;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	case P_UID:
3257c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
3267c478bd9Sstevel@tonic-gate 		if (crgetuid(pp->p_cred) == psp->p_lid)
3277c478bd9Sstevel@tonic-gate 			loperand++;
3287c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
3297c478bd9Sstevel@tonic-gate 		break;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	case P_GID:
3327c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
3337c478bd9Sstevel@tonic-gate 		if (crgetgid(pp->p_cred) == psp->p_lid)
3347c478bd9Sstevel@tonic-gate 			loperand++;
3357c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
3367c478bd9Sstevel@tonic-gate 		break;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	case P_PROJID:
3397c478bd9Sstevel@tonic-gate 		if (pp->p_task->tk_proj->kpj_id == psp->p_lid)
3407c478bd9Sstevel@tonic-gate 			loperand++;
3417c478bd9Sstevel@tonic-gate 		break;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	case P_POOLID:
3447c478bd9Sstevel@tonic-gate 		if (pp->p_pool->pool_id == psp->p_lid)
3457c478bd9Sstevel@tonic-gate 			loperand++;
3467c478bd9Sstevel@tonic-gate 		break;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	case P_ZONEID:
3497c478bd9Sstevel@tonic-gate 		if (pp->p_zone->zone_id == psp->p_lid)
3507c478bd9Sstevel@tonic-gate 			loperand++;
3517c478bd9Sstevel@tonic-gate 		break;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	case P_CTID:
3547c478bd9Sstevel@tonic-gate 		if (PRCTID(pp) == psp->p_lid)
3557c478bd9Sstevel@tonic-gate 			loperand++;
3567c478bd9Sstevel@tonic-gate 		break;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	case P_ALL:
3597c478bd9Sstevel@tonic-gate 		loperand++;
3607c478bd9Sstevel@tonic-gate 		break;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	default:
3637c478bd9Sstevel@tonic-gate #ifdef DEBUG
3647c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "procinset called with bad set");
3657c478bd9Sstevel@tonic-gate 		return (0);
3667c478bd9Sstevel@tonic-gate #else
3677c478bd9Sstevel@tonic-gate 		return (0);
3687c478bd9Sstevel@tonic-gate #endif
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	switch (psp->p_ridtype) {
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	case P_LWPID:
3747c478bd9Sstevel@tonic-gate 		if (pp == ttoproc(curthread))
3757c478bd9Sstevel@tonic-gate 			if (getlwpptr(psp->p_rid) != NULL)
3767c478bd9Sstevel@tonic-gate 				lwprinproc++;
3777c478bd9Sstevel@tonic-gate 		break;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	case P_PID:
3807c478bd9Sstevel@tonic-gate 		if (pp->p_pid == psp->p_rid)
3817c478bd9Sstevel@tonic-gate 			roperand++;
3827c478bd9Sstevel@tonic-gate 		break;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	case P_PPID:
3857c478bd9Sstevel@tonic-gate 		if (pp->p_ppid == psp->p_rid)
3867c478bd9Sstevel@tonic-gate 			roperand++;
3877c478bd9Sstevel@tonic-gate 		break;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	case P_PGID:
3907c478bd9Sstevel@tonic-gate 		if (pp->p_pgrp == psp->p_rid)
3917c478bd9Sstevel@tonic-gate 			roperand++;
3927c478bd9Sstevel@tonic-gate 		break;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	case P_SID:
3959acbbeafSnn35248 		mutex_enter(&pp->p_splock);
3967c478bd9Sstevel@tonic-gate 		if (pp->p_sessp->s_sid == psp->p_rid)
3977c478bd9Sstevel@tonic-gate 			roperand++;
3989acbbeafSnn35248 		mutex_exit(&pp->p_splock);
3997c478bd9Sstevel@tonic-gate 		break;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	case P_TASKID:
4027c478bd9Sstevel@tonic-gate 		if (pp->p_task->tk_tkid == psp->p_rid)
4037c478bd9Sstevel@tonic-gate 			roperand++;
4047c478bd9Sstevel@tonic-gate 		break;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	case P_CID:
4077c478bd9Sstevel@tonic-gate 		ASSERT(tp != NULL);
4087c478bd9Sstevel@tonic-gate 		/* This case is broken for now. Need to be fixed XXX */
4097c478bd9Sstevel@tonic-gate 		if (tp->t_cid == psp->p_rid)
4107c478bd9Sstevel@tonic-gate 			/*
4117c478bd9Sstevel@tonic-gate 			 * if (checkcid(psp->p_rid))
4127c478bd9Sstevel@tonic-gate 			 */
4137c478bd9Sstevel@tonic-gate 			roperand++;
4147c478bd9Sstevel@tonic-gate 		break;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	case P_UID:
4177c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
4187c478bd9Sstevel@tonic-gate 		if (crgetuid(pp->p_cred) == psp->p_rid)
4197c478bd9Sstevel@tonic-gate 			roperand++;
4207c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
4217c478bd9Sstevel@tonic-gate 		break;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	case P_GID:
4247c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
4257c478bd9Sstevel@tonic-gate 		if (crgetgid(pp->p_cred) == psp->p_rid)
4267c478bd9Sstevel@tonic-gate 			roperand++;
4277c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
4287c478bd9Sstevel@tonic-gate 		break;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	case P_PROJID:
4317c478bd9Sstevel@tonic-gate 		if (pp->p_task->tk_proj->kpj_id == psp->p_rid)
4327c478bd9Sstevel@tonic-gate 			roperand++;
4337c478bd9Sstevel@tonic-gate 		break;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	case P_POOLID:
4367c478bd9Sstevel@tonic-gate 		if (pp->p_pool->pool_id == psp->p_rid)
4377c478bd9Sstevel@tonic-gate 			roperand++;
4387c478bd9Sstevel@tonic-gate 		break;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	case P_ZONEID:
4417c478bd9Sstevel@tonic-gate 		if (pp->p_zone->zone_id == psp->p_rid)
4427c478bd9Sstevel@tonic-gate 			roperand++;
4437c478bd9Sstevel@tonic-gate 		break;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	case P_CTID:
4467c478bd9Sstevel@tonic-gate 		if (PRCTID(pp) == psp->p_rid)
4477c478bd9Sstevel@tonic-gate 			roperand++;
4487c478bd9Sstevel@tonic-gate 		break;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	case P_ALL:
4517c478bd9Sstevel@tonic-gate 		roperand++;
4527c478bd9Sstevel@tonic-gate 		break;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	default:
4557c478bd9Sstevel@tonic-gate #ifdef DEBUG
4567c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "procinset called with bad set");
4577c478bd9Sstevel@tonic-gate 		return (0);
4587c478bd9Sstevel@tonic-gate #else
4597c478bd9Sstevel@tonic-gate 		return (0);
4607c478bd9Sstevel@tonic-gate #endif
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	switch (psp->p_op) {
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	case POP_DIFF:
4667c478bd9Sstevel@tonic-gate 		if (loperand && !lwprinproc && !roperand)
4677c478bd9Sstevel@tonic-gate 			return (1);
4687c478bd9Sstevel@tonic-gate 		else
4697c478bd9Sstevel@tonic-gate 			return (0);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	case POP_AND:
4727c478bd9Sstevel@tonic-gate 		if (loperand && roperand)
4737c478bd9Sstevel@tonic-gate 			return (1);
4747c478bd9Sstevel@tonic-gate 		else
4757c478bd9Sstevel@tonic-gate 			return (0);
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	case POP_OR:
4787c478bd9Sstevel@tonic-gate 		if (loperand || roperand)
4797c478bd9Sstevel@tonic-gate 			return (1);
4807c478bd9Sstevel@tonic-gate 		else
4817c478bd9Sstevel@tonic-gate 			return (0);
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	case POP_XOR:
4847c478bd9Sstevel@tonic-gate 		if ((loperand && !lwprinproc && !roperand) ||
4857c478bd9Sstevel@tonic-gate 		    (roperand && !lwplinproc && !loperand))
4867c478bd9Sstevel@tonic-gate 			return (1);
4877c478bd9Sstevel@tonic-gate 		else
4887c478bd9Sstevel@tonic-gate 			return (0);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	default:
4917c478bd9Sstevel@tonic-gate #ifdef DEBUG
4927c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "procinset called with bad set");
4937c478bd9Sstevel@tonic-gate 		return (0);
4947c478bd9Sstevel@tonic-gate #else
4957c478bd9Sstevel@tonic-gate 		return (0);
4967c478bd9Sstevel@tonic-gate #endif
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate /*
5027c478bd9Sstevel@tonic-gate  * lwpinset returns 1 if the thread pointed to
5037c478bd9Sstevel@tonic-gate  * by tp is in the process set specified by psp and is not in
5047c478bd9Sstevel@tonic-gate  * the sys scheduling class - otherwise 0 is returned.
5057c478bd9Sstevel@tonic-gate  *
5067c478bd9Sstevel@tonic-gate  * This function expects to be called with a valid procset_t.
5077c478bd9Sstevel@tonic-gate  * The set should be checked using checkprocset() before calling
5087c478bd9Sstevel@tonic-gate  * this function.
5097c478bd9Sstevel@tonic-gate  */
5107c478bd9Sstevel@tonic-gate int
5117c478bd9Sstevel@tonic-gate lwpinset(proc_t *pp, procset_t *psp, kthread_t *tp, int *done)
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	int	loperand = 0;
5147c478bd9Sstevel@tonic-gate 	int	roperand = 0;
5157c478bd9Sstevel@tonic-gate 	int	lwplinset  = 0;
5167c478bd9Sstevel@tonic-gate 	int	lwprinset  = 0;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	ASSERT(ttoproc(tp) == pp);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * If process is in the sys class return (0).
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	if (proctot(pp)->t_cid == 0) {
5247c478bd9Sstevel@tonic-gate 		return (0);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	switch (psp->p_lidtype) {
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	case P_LWPID:
5307c478bd9Sstevel@tonic-gate 		if (tp->t_tid == psp->p_lid)
5317c478bd9Sstevel@tonic-gate 			lwplinset ++;
5327c478bd9Sstevel@tonic-gate 		break;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	case P_PID:
5357c478bd9Sstevel@tonic-gate 		if (pp->p_pid == psp->p_lid)
5367c478bd9Sstevel@tonic-gate 			loperand++;
5377c478bd9Sstevel@tonic-gate 		break;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	case P_PPID:
5407c478bd9Sstevel@tonic-gate 		if (pp->p_ppid == psp->p_lid)
5417c478bd9Sstevel@tonic-gate 			loperand++;
5427c478bd9Sstevel@tonic-gate 		break;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	case P_PGID:
5457c478bd9Sstevel@tonic-gate 		if (pp->p_pgrp == psp->p_lid)
5467c478bd9Sstevel@tonic-gate 			loperand++;
5477c478bd9Sstevel@tonic-gate 		break;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	case P_SID:
5509acbbeafSnn35248 		mutex_enter(&pp->p_splock);
5517c478bd9Sstevel@tonic-gate 		if (pp->p_sessp->s_sid == psp->p_lid)
5527c478bd9Sstevel@tonic-gate 			loperand++;
5539acbbeafSnn35248 		mutex_exit(&pp->p_splock);
5547c478bd9Sstevel@tonic-gate 		break;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	case P_TASKID:
5577c478bd9Sstevel@tonic-gate 		if (pp->p_task->tk_tkid == psp->p_lid)
5587c478bd9Sstevel@tonic-gate 			loperand++;
5597c478bd9Sstevel@tonic-gate 		break;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	case P_CID:
5627c478bd9Sstevel@tonic-gate 		if (tp->t_cid == psp->p_lid)
5637c478bd9Sstevel@tonic-gate 			loperand++;
5647c478bd9Sstevel@tonic-gate 		break;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	case P_UID:
5677c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
5687c478bd9Sstevel@tonic-gate 		if (crgetuid(pp->p_cred) == psp->p_lid)
5697c478bd9Sstevel@tonic-gate 			loperand++;
5707c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
5717c478bd9Sstevel@tonic-gate 		break;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	case P_GID:
5747c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
5757c478bd9Sstevel@tonic-gate 		if (crgetgid(pp->p_cred) == psp->p_lid)
5767c478bd9Sstevel@tonic-gate 			loperand++;
5777c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
5787c478bd9Sstevel@tonic-gate 		break;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	case P_PROJID:
5817c478bd9Sstevel@tonic-gate 		if (pp->p_task->tk_proj->kpj_id == psp->p_lid)
5827c478bd9Sstevel@tonic-gate 			loperand++;
5837c478bd9Sstevel@tonic-gate 		break;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	case P_POOLID:
5867c478bd9Sstevel@tonic-gate 		if (pp->p_pool->pool_id == psp->p_lid)
5877c478bd9Sstevel@tonic-gate 			loperand++;
5887c478bd9Sstevel@tonic-gate 		break;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	case P_ZONEID:
5917c478bd9Sstevel@tonic-gate 		if (pp->p_zone->zone_id == psp->p_lid)
5927c478bd9Sstevel@tonic-gate 			loperand++;
5937c478bd9Sstevel@tonic-gate 		break;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	case P_CTID:
5967c478bd9Sstevel@tonic-gate 		if (PRCTID(pp) == psp->p_lid)
5977c478bd9Sstevel@tonic-gate 			loperand++;
5987c478bd9Sstevel@tonic-gate 		break;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	case P_ALL:
6017c478bd9Sstevel@tonic-gate 		loperand++;
6027c478bd9Sstevel@tonic-gate 		break;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	default:
6057c478bd9Sstevel@tonic-gate #ifdef DEBUG
6067c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "lwpinset called with bad set");
6077c478bd9Sstevel@tonic-gate 		return (0);
6087c478bd9Sstevel@tonic-gate #else
6097c478bd9Sstevel@tonic-gate 		return (0);
6107c478bd9Sstevel@tonic-gate #endif
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	switch (psp->p_ridtype) {
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	case P_LWPID:
6167c478bd9Sstevel@tonic-gate 		if (tp->t_tid == psp->p_rid)
6177c478bd9Sstevel@tonic-gate 			lwprinset ++;
6187c478bd9Sstevel@tonic-gate 		break;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	case P_PID:
6217c478bd9Sstevel@tonic-gate 		if (pp->p_pid == psp->p_rid)
6227c478bd9Sstevel@tonic-gate 			roperand++;
6237c478bd9Sstevel@tonic-gate 		break;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	case P_PPID:
6267c478bd9Sstevel@tonic-gate 		if (pp->p_ppid == psp->p_rid)
6277c478bd9Sstevel@tonic-gate 			roperand++;
6287c478bd9Sstevel@tonic-gate 		break;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	case P_PGID:
6317c478bd9Sstevel@tonic-gate 		if (pp->p_pgrp == psp->p_rid)
6327c478bd9Sstevel@tonic-gate 			roperand++;
6337c478bd9Sstevel@tonic-gate 		break;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	case P_SID:
6369acbbeafSnn35248 		mutex_enter(&pp->p_splock);
6377c478bd9Sstevel@tonic-gate 		if (pp->p_sessp->s_sid == psp->p_rid)
6387c478bd9Sstevel@tonic-gate 			roperand++;
6399acbbeafSnn35248 		mutex_exit(&pp->p_splock);
6407c478bd9Sstevel@tonic-gate 		break;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	case P_TASKID:
6437c478bd9Sstevel@tonic-gate 		if (pp->p_task->tk_tkid == psp->p_rid)
6447c478bd9Sstevel@tonic-gate 			roperand++;
6457c478bd9Sstevel@tonic-gate 		break;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	case P_CID:
6487c478bd9Sstevel@tonic-gate 		if (tp->t_cid == psp->p_rid)
6497c478bd9Sstevel@tonic-gate 			roperand++;
6507c478bd9Sstevel@tonic-gate 		break;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	case P_UID:
6537c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
6547c478bd9Sstevel@tonic-gate 		if (crgetuid(pp->p_cred) == psp->p_rid)
6557c478bd9Sstevel@tonic-gate 			roperand++;
6567c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
6577c478bd9Sstevel@tonic-gate 		break;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	case P_GID:
6607c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
6617c478bd9Sstevel@tonic-gate 		if (crgetgid(pp->p_cred) == psp->p_rid)
6627c478bd9Sstevel@tonic-gate 			roperand++;
6637c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
6647c478bd9Sstevel@tonic-gate 		break;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	case P_PROJID:
6677c478bd9Sstevel@tonic-gate 		if (pp->p_task->tk_proj->kpj_id == psp->p_rid)
6687c478bd9Sstevel@tonic-gate 			roperand++;
6697c478bd9Sstevel@tonic-gate 		break;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	case P_POOLID:
6727c478bd9Sstevel@tonic-gate 		if (pp->p_pool->pool_id == psp->p_rid)
6737c478bd9Sstevel@tonic-gate 			roperand++;
6747c478bd9Sstevel@tonic-gate 		break;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	case P_ZONEID:
6777c478bd9Sstevel@tonic-gate 		if (pp->p_zone->zone_id == psp->p_rid)
6787c478bd9Sstevel@tonic-gate 			roperand++;
6797c478bd9Sstevel@tonic-gate 		break;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	case P_CTID:
6827c478bd9Sstevel@tonic-gate 		if (PRCTID(pp) == psp->p_rid)
6837c478bd9Sstevel@tonic-gate 			roperand++;
6847c478bd9Sstevel@tonic-gate 		break;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	case P_ALL:
6877c478bd9Sstevel@tonic-gate 		roperand++;
6887c478bd9Sstevel@tonic-gate 		break;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	default:
6917c478bd9Sstevel@tonic-gate #ifdef DEBUG
6927c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "lwpinset called with bad set");
6937c478bd9Sstevel@tonic-gate 		return (0);
6947c478bd9Sstevel@tonic-gate #else
6957c478bd9Sstevel@tonic-gate 		return (0);
6967c478bd9Sstevel@tonic-gate #endif
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (lwplinset && lwprinset)
7007c478bd9Sstevel@tonic-gate 		*done = 1;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	switch (psp->p_op) {
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	case POP_DIFF:
7057c478bd9Sstevel@tonic-gate 		if ((loperand || lwplinset) && !(lwprinset || roperand))
7067c478bd9Sstevel@tonic-gate 			return (1);
7077c478bd9Sstevel@tonic-gate 		else
7087c478bd9Sstevel@tonic-gate 			return (0);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	case POP_AND:
7117c478bd9Sstevel@tonic-gate 		if ((loperand || lwplinset) && (roperand || lwprinset))
7127c478bd9Sstevel@tonic-gate 			return (1);
7137c478bd9Sstevel@tonic-gate 		else
7147c478bd9Sstevel@tonic-gate 			return (0);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	case POP_OR:
7177c478bd9Sstevel@tonic-gate 		if (loperand || roperand || lwplinset || lwprinset)
7187c478bd9Sstevel@tonic-gate 			return (1);
7197c478bd9Sstevel@tonic-gate 		else
7207c478bd9Sstevel@tonic-gate 			return (0);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	case POP_XOR:
7237c478bd9Sstevel@tonic-gate 		if (((loperand || lwplinset) &&
7247c478bd9Sstevel@tonic-gate 					!(lwprinset || roperand)) ||
7257c478bd9Sstevel@tonic-gate 		    ((roperand || lwprinset) &&
7267c478bd9Sstevel@tonic-gate 					!(lwplinset || loperand)))
7277c478bd9Sstevel@tonic-gate 			return (1);
7287c478bd9Sstevel@tonic-gate 		else
7297c478bd9Sstevel@tonic-gate 			return (0);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	default:
7327c478bd9Sstevel@tonic-gate #ifdef DEBUG
7337c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "lwpinset called with bad set");
7347c478bd9Sstevel@tonic-gate 		return (0);
7357c478bd9Sstevel@tonic-gate #else
7367c478bd9Sstevel@tonic-gate 		return (0);
7377c478bd9Sstevel@tonic-gate #endif
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate /*
7427c478bd9Sstevel@tonic-gate  * Check for common cases of procsets which specify only the
7437c478bd9Sstevel@tonic-gate  * current process.  cur_inset_only() returns B_TRUE when
7447c478bd9Sstevel@tonic-gate  * the current process is the only one in the set.  B_FALSE
7457c478bd9Sstevel@tonic-gate  * is returned to indicate that this may not be the case.
7467c478bd9Sstevel@tonic-gate  */
7477c478bd9Sstevel@tonic-gate boolean_t
7487c478bd9Sstevel@tonic-gate cur_inset_only(procset_t *psp)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 	if (((psp->p_lidtype == P_PID &&
7517c478bd9Sstevel@tonic-gate 		(psp->p_lid == P_MYID ||
7527c478bd9Sstevel@tonic-gate 		psp->p_lid == ttoproc(curthread)->p_pid)) ||
7537c478bd9Sstevel@tonic-gate 		((psp->p_lidtype == P_LWPID) &&
7547c478bd9Sstevel@tonic-gate 		(psp->p_lid == P_MYID ||
7557c478bd9Sstevel@tonic-gate 		psp->p_lid == curthread->t_tid))) &&
7567c478bd9Sstevel@tonic-gate 		psp->p_op == POP_AND && psp->p_ridtype == P_ALL)
7577c478bd9Sstevel@tonic-gate 			return (B_TRUE);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if (((psp->p_ridtype == P_PID &&
7607c478bd9Sstevel@tonic-gate 		(psp->p_rid == P_MYID ||
7617c478bd9Sstevel@tonic-gate 		psp->p_rid == ttoproc(curthread)->p_pid)) ||
7627c478bd9Sstevel@tonic-gate 		((psp->p_ridtype == P_LWPID) &&
7637c478bd9Sstevel@tonic-gate 		(psp->p_rid == P_MYID ||
7647c478bd9Sstevel@tonic-gate 		psp->p_rid == curthread->t_tid))) &&
7657c478bd9Sstevel@tonic-gate 		psp->p_op == POP_AND && psp->p_lidtype == P_ALL)
7667c478bd9Sstevel@tonic-gate 			return (B_TRUE);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	return (B_FALSE);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate id_t
7727c478bd9Sstevel@tonic-gate getmyid(idtype_t idtype)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 	proc_t	*pp;
7757c478bd9Sstevel@tonic-gate 	uid_t uid;
7767c478bd9Sstevel@tonic-gate 	gid_t gid;
7779acbbeafSnn35248 	pid_t sid;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	pp = ttoproc(curthread);
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	switch (idtype) {
7827c478bd9Sstevel@tonic-gate 	case P_LWPID:
7837c478bd9Sstevel@tonic-gate 		return (curthread->t_tid);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	case P_PID:
7867c478bd9Sstevel@tonic-gate 		return (pp->p_pid);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	case P_PPID:
7897c478bd9Sstevel@tonic-gate 		return (pp->p_ppid);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	case P_PGID:
7927c478bd9Sstevel@tonic-gate 		return (pp->p_pgrp);
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	case P_SID:
7959acbbeafSnn35248 		mutex_enter(&pp->p_splock);
7969acbbeafSnn35248 		sid = pp->p_sessp->s_sid;
7979acbbeafSnn35248 		mutex_exit(&pp->p_splock);
7989acbbeafSnn35248 		return (sid);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	case P_TASKID:
8017c478bd9Sstevel@tonic-gate 		return (pp->p_task->tk_tkid);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	case P_CID:
8047c478bd9Sstevel@tonic-gate 		return (curthread->t_cid);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	case P_UID:
8077c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
8087c478bd9Sstevel@tonic-gate 		uid = crgetuid(pp->p_cred);
8097c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
8107c478bd9Sstevel@tonic-gate 		return (uid);
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	case P_GID:
8137c478bd9Sstevel@tonic-gate 		mutex_enter(&pp->p_crlock);
8147c478bd9Sstevel@tonic-gate 		gid = crgetgid(pp->p_cred);
8157c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_crlock);
8167c478bd9Sstevel@tonic-gate 		return (gid);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	case P_PROJID:
8197c478bd9Sstevel@tonic-gate 		return (pp->p_task->tk_proj->kpj_id);
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	case P_POOLID:
8227c478bd9Sstevel@tonic-gate 		return (pp->p_pool->pool_id);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	case P_ZONEID:
8257c478bd9Sstevel@tonic-gate 		return (pp->p_zone->zone_id);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	case P_CTID:
8287c478bd9Sstevel@tonic-gate 		return (PRCTID(pp));
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	case P_ALL:
8317c478bd9Sstevel@tonic-gate 		/*
8327c478bd9Sstevel@tonic-gate 		 * The value doesn't matter for P_ALL.
8337c478bd9Sstevel@tonic-gate 		 */
8347c478bd9Sstevel@tonic-gate 		return (0);
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	default:
8377c478bd9Sstevel@tonic-gate 		return (-1);
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate static kthread_t *
8427c478bd9Sstevel@tonic-gate getlwpptr(id_t id)
8437c478bd9Sstevel@tonic-gate {
8447c478bd9Sstevel@tonic-gate 	proc_t		*p;
8457c478bd9Sstevel@tonic-gate 	kthread_t	*t;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	if (id == P_MYID)
8487c478bd9Sstevel@tonic-gate 		t = curthread;
8497c478bd9Sstevel@tonic-gate 	else {
8507c478bd9Sstevel@tonic-gate 		p = ttoproc(curthread);
8517c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
8527c478bd9Sstevel@tonic-gate 		t = idtot(p, id);
8537c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	return (t);
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate /*
8607c478bd9Sstevel@tonic-gate  * The dotolwp function locates the LWP(s) specified by the procset structure
8617c478bd9Sstevel@tonic-gate  * pointed to by psp.  If funcp is non-NULL then it points to a function
8627c478bd9Sstevel@tonic-gate  * which dotolwp will call for each LWP in the specified set.
8637c478bd9Sstevel@tonic-gate  * LWPIDs specified in the procset structure always refer to lwps in curproc.
8647c478bd9Sstevel@tonic-gate  * The arguments for this function must be "char *arg", and "kthread_t *tp",
8657c478bd9Sstevel@tonic-gate  * where tp is a pointer to the current thread from the set.
8667c478bd9Sstevel@tonic-gate  * Note that these arguments are passed to the function in reversed order
8677c478bd9Sstevel@tonic-gate  * than the order of arguments passed by dotoprocs() to its callback function.
8687c478bd9Sstevel@tonic-gate  * Also note that there are two separate cases where this routine returns zero.
8697c478bd9Sstevel@tonic-gate  * In the first case no mutex is grabbed, in the second the p_lock mutex
8707c478bd9Sstevel@tonic-gate  * is NOT RELEASED. The priocntl code is expecting this behaviour.
8717c478bd9Sstevel@tonic-gate  */
8727c478bd9Sstevel@tonic-gate int
8737c478bd9Sstevel@tonic-gate dotolwp(procset_t *psp, int (*funcp)(), char *arg)
8747c478bd9Sstevel@tonic-gate {
8757c478bd9Sstevel@tonic-gate 	int		error = 0;
8767c478bd9Sstevel@tonic-gate 	int		nfound = 0;
8777c478bd9Sstevel@tonic-gate 	kthread_t	*tp;
8787c478bd9Sstevel@tonic-gate 	proc_t		*pp;
8797c478bd9Sstevel@tonic-gate 	int		done = 0;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	/*
8827c478bd9Sstevel@tonic-gate 	 * Check that the procset_t is valid.
8837c478bd9Sstevel@tonic-gate 	 */
8847c478bd9Sstevel@tonic-gate 	error = checkprocset(psp);
8857c478bd9Sstevel@tonic-gate 	if (error) {
8867c478bd9Sstevel@tonic-gate 		return (error);
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	/*
8927c478bd9Sstevel@tonic-gate 	 * Check for the special value P_MYID in either operand
8937c478bd9Sstevel@tonic-gate 	 * and replace it with the correct value.  We don't check
8947c478bd9Sstevel@tonic-gate 	 * for an error return from getmyid() because the idtypes
8957c478bd9Sstevel@tonic-gate 	 * have been validated by the checkprocset() call above.
8967c478bd9Sstevel@tonic-gate 	 */
8977c478bd9Sstevel@tonic-gate 	if (psp->p_lid == P_MYID) {
8987c478bd9Sstevel@tonic-gate 		psp->p_lid = getmyid(psp->p_lidtype);
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 	if (psp->p_rid == P_MYID) {
9017c478bd9Sstevel@tonic-gate 		psp->p_rid = getmyid(psp->p_ridtype);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	pp = ttoproc(curthread);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if (procinset(pp, psp)) {
9077c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
9087c478bd9Sstevel@tonic-gate 		return (0);
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
9117c478bd9Sstevel@tonic-gate 	if ((tp = pp->p_tlist) == NULL) {
9127c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
9137c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
9147c478bd9Sstevel@tonic-gate 		return (0);
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 	do {
9177c478bd9Sstevel@tonic-gate 		if (lwpinset(pp, psp, tp, &done)) {
9187c478bd9Sstevel@tonic-gate 			nfound ++;
9197c478bd9Sstevel@tonic-gate 			error = (*funcp)(arg, tp);
9207c478bd9Sstevel@tonic-gate 			if (error) {
9217c478bd9Sstevel@tonic-gate 				mutex_exit(&pp->p_lock);
9227c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
9237c478bd9Sstevel@tonic-gate 				return (error);
9247c478bd9Sstevel@tonic-gate 			}
9257c478bd9Sstevel@tonic-gate 		}
9267c478bd9Sstevel@tonic-gate 	} while (((tp = tp->t_forw) != pp->p_tlist) && !done);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if (nfound == 0) {
9297c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
9307c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
9317c478bd9Sstevel@tonic-gate 		return (ESRCH);
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
9357c478bd9Sstevel@tonic-gate 	return (error);
9367c478bd9Sstevel@tonic-gate }
937