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