10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52712Snn35248 * Common Development and Distribution License (the "License").
62712Snn35248 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
226618Srh87107 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/vfs.h>
340Sstevel@tonic-gate #include <sys/cred.h>
350Sstevel@tonic-gate #include <sys/vnode.h>
360Sstevel@tonic-gate #include <sys/file.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/user.h>
400Sstevel@tonic-gate #include <sys/buf.h>
410Sstevel@tonic-gate #include <sys/var.h>
420Sstevel@tonic-gate #include <sys/conf.h>
430Sstevel@tonic-gate #include <sys/debug.h>
440Sstevel@tonic-gate #include <sys/proc.h>
450Sstevel@tonic-gate #include <sys/signal.h>
460Sstevel@tonic-gate #include <sys/siginfo.h>
470Sstevel@tonic-gate #include <sys/acct.h>
480Sstevel@tonic-gate #include <sys/procset.h>
490Sstevel@tonic-gate #include <sys/cmn_err.h>
500Sstevel@tonic-gate #include <sys/fault.h>
510Sstevel@tonic-gate #include <sys/syscall.h>
520Sstevel@tonic-gate #include <sys/ucontext.h>
530Sstevel@tonic-gate #include <sys/procfs.h>
540Sstevel@tonic-gate #include <sys/session.h>
550Sstevel@tonic-gate #include <sys/task.h>
560Sstevel@tonic-gate #include <sys/project.h>
570Sstevel@tonic-gate #include <sys/pool.h>
580Sstevel@tonic-gate #include <sys/zone.h>
590Sstevel@tonic-gate #include <sys/contract/process_impl.h>
600Sstevel@tonic-gate
610Sstevel@tonic-gate id_t getmyid(idtype_t);
620Sstevel@tonic-gate int checkprocset(procset_t *);
630Sstevel@tonic-gate static kthread_t *getlwpptr(id_t);
640Sstevel@tonic-gate int procinset(proc_t *, procset_t *);
650Sstevel@tonic-gate static int lwpinset(proc_t *, procset_t *, kthread_t *, int *);
660Sstevel@tonic-gate
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate * The dotoprocs function locates the process(es) specified
694253Sjimp * by the procset structure pointed to by psp. funcp points to a
704253Sjimp * function which dotoprocs will call for each process in the
714253Sjimp * specified set. The arguments to this function will be a pointer
724253Sjimp * to the current process from the set and arg.
730Sstevel@tonic-gate * If the called function returns -1, it means that processing of the
740Sstevel@tonic-gate * procset should stop and a normal (non-error) return should be made
750Sstevel@tonic-gate * to the caller of dotoprocs.
760Sstevel@tonic-gate * If the called function returns any other non-zero value the search
770Sstevel@tonic-gate * is terminated and the function's return value is returned to
780Sstevel@tonic-gate * the caller of dotoprocs. This will normally be an error code.
790Sstevel@tonic-gate * Otherwise, dotoprocs will return zero after processing the entire
800Sstevel@tonic-gate * process set unless no processes were found in which case ESRCH will
810Sstevel@tonic-gate * be returned.
820Sstevel@tonic-gate */
830Sstevel@tonic-gate int
dotoprocs(procset_t * psp,int (* funcp)(),char * arg)840Sstevel@tonic-gate dotoprocs(procset_t *psp, int (*funcp)(), char *arg)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate proc_t *prp; /* A process from the set */
870Sstevel@tonic-gate int error;
880Sstevel@tonic-gate int nfound; /* Nbr of processes found. */
890Sstevel@tonic-gate proc_t *lastprp; /* Last proc found. */
900Sstevel@tonic-gate
914253Sjimp ASSERT(funcp != NULL);
924253Sjimp
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate * Check that the procset_t is valid.
950Sstevel@tonic-gate */
960Sstevel@tonic-gate error = checkprocset(psp);
970Sstevel@tonic-gate if (error) {
980Sstevel@tonic-gate return (error);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate * Check for the special value P_MYID in either operand
1020Sstevel@tonic-gate * and replace it with the correct value. We don't check
1030Sstevel@tonic-gate * for an error return from getmyid() because the idtypes
1040Sstevel@tonic-gate * have been validated by the checkprocset() call above.
1050Sstevel@tonic-gate */
1060Sstevel@tonic-gate mutex_enter(&pidlock);
1070Sstevel@tonic-gate if (psp->p_lid == P_MYID) {
1080Sstevel@tonic-gate psp->p_lid = getmyid(psp->p_lidtype);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate if (psp->p_rid == P_MYID) {
1110Sstevel@tonic-gate psp->p_rid = getmyid(psp->p_ridtype);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate * If psp only acts on a single proc, we can reduce pidlock hold time
1160Sstevel@tonic-gate * by avoiding a needless scan of the entire proc list. Although
1170Sstevel@tonic-gate * there are many procset_t combinations which might boil down to a
1180Sstevel@tonic-gate * single proc, the most common case is an AND operation where one
1190Sstevel@tonic-gate * side is a specific pid, and the other side is P_ALL, so that is
1200Sstevel@tonic-gate * the case for which we will provide a fast path. Other cases could
1210Sstevel@tonic-gate * be added in a similar fashion if they were to become significant
1220Sstevel@tonic-gate * pidlock bottlenecks.
1230Sstevel@tonic-gate *
1240Sstevel@tonic-gate * Perform the check symmetrically: either the left or right side may
1250Sstevel@tonic-gate * specify a pid, with the opposite side being 'all'.
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate if (psp->p_op == POP_AND) {
1280Sstevel@tonic-gate if (((psp->p_lidtype == P_PID) && (psp->p_ridtype == P_ALL)) ||
1290Sstevel@tonic-gate ((psp->p_ridtype == P_PID) && (psp->p_lidtype == P_ALL))) {
1300Sstevel@tonic-gate id_t pid;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate pid = (psp->p_lidtype == P_PID) ?
1330Sstevel@tonic-gate psp->p_lid : psp->p_rid;
1344253Sjimp if (((prp = prfind((pid_t)pid)) == NULL) ||
1354253Sjimp (prp->p_stat == SIDL || prp->p_stat == SZOMB ||
1364253Sjimp prp->p_tlist == NULL || prp->p_flag & SSYS)) {
1374253Sjimp /*
1384253Sjimp * Specified proc doesn't exist or should
1394253Sjimp * not be operated on.
1404253Sjimp * Don't need to make HASZONEACCESS check
1414253Sjimp * here since prfind() takes care of that.
1424253Sjimp */
1430Sstevel@tonic-gate mutex_exit(&pidlock);
1440Sstevel@tonic-gate return (ESRCH);
1450Sstevel@tonic-gate }
1464253Sjimp /*
1474253Sjimp * Operate only on the specified proc. It's okay
1484253Sjimp * if it's init.
1494253Sjimp */
1500Sstevel@tonic-gate error = (*funcp)(prp, arg);
1510Sstevel@tonic-gate mutex_exit(&pidlock);
1520Sstevel@tonic-gate if (error == -1)
1530Sstevel@tonic-gate error = 0;
1540Sstevel@tonic-gate return (error);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate nfound = 0;
1590Sstevel@tonic-gate error = 0;
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate for (prp = practive; prp != NULL; prp = prp->p_next) {
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate * If caller is in a non-global zone, skip processes
1640Sstevel@tonic-gate * in other zones.
1650Sstevel@tonic-gate */
1660Sstevel@tonic-gate if (!HASZONEACCESS(curproc, prp->p_zone->zone_id))
1670Sstevel@tonic-gate continue;
1686618Srh87107
1696618Srh87107 /*
1706618Srh87107 * Ignore this process if it's coming or going,
1716618Srh87107 * if it's a system process or if it's not in
1726618Srh87107 * the given procset_t.
1736618Srh87107 */
1746618Srh87107 if (prp->p_stat == SIDL || prp->p_stat == SZOMB)
1750Sstevel@tonic-gate continue;
1766618Srh87107
1776618Srh87107 mutex_enter(&prp->p_lock);
178*8067SJordan.Vaughan@Sun.com if (prp->p_flag & SSYS || prp->p_tlist == NULL ||
179*8067SJordan.Vaughan@Sun.com procinset(prp, psp) == 0) {
1806618Srh87107 mutex_exit(&prp->p_lock);
1816618Srh87107 } else {
1826618Srh87107 mutex_exit(&prp->p_lock);
1830Sstevel@tonic-gate nfound++;
1840Sstevel@tonic-gate lastprp = prp;
1854253Sjimp if (prp != proc_init) {
1860Sstevel@tonic-gate error = (*funcp)(prp, arg);
1870Sstevel@tonic-gate if (error == -1) {
1880Sstevel@tonic-gate mutex_exit(&pidlock);
1890Sstevel@tonic-gate return (0);
1900Sstevel@tonic-gate } else if (error) {
1910Sstevel@tonic-gate mutex_exit(&pidlock);
1920Sstevel@tonic-gate return (error);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate if (nfound == 0) {
1980Sstevel@tonic-gate mutex_exit(&pidlock);
1990Sstevel@tonic-gate return (ESRCH);
2000Sstevel@tonic-gate }
2014253Sjimp if (nfound == 1 && lastprp == proc_init)
2020Sstevel@tonic-gate error = (*funcp)(lastprp, arg);
2030Sstevel@tonic-gate if (error == -1)
2040Sstevel@tonic-gate error = 0;
2050Sstevel@tonic-gate mutex_exit(&pidlock);
2060Sstevel@tonic-gate return (error);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate * Check if a procset_t is valid. Return zero or an errno.
2110Sstevel@tonic-gate */
2120Sstevel@tonic-gate int
checkprocset(procset_t * psp)2130Sstevel@tonic-gate checkprocset(procset_t *psp)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate switch (psp->p_lidtype) {
2160Sstevel@tonic-gate case P_LWPID:
2170Sstevel@tonic-gate case P_PID:
2180Sstevel@tonic-gate case P_PPID:
2190Sstevel@tonic-gate case P_PGID:
2200Sstevel@tonic-gate case P_SID:
2210Sstevel@tonic-gate case P_TASKID:
2220Sstevel@tonic-gate case P_CID:
2230Sstevel@tonic-gate case P_UID:
2240Sstevel@tonic-gate case P_GID:
2250Sstevel@tonic-gate case P_PROJID:
2260Sstevel@tonic-gate case P_POOLID:
2270Sstevel@tonic-gate case P_ZONEID:
2280Sstevel@tonic-gate case P_CTID:
2290Sstevel@tonic-gate case P_ALL:
2300Sstevel@tonic-gate break;
2310Sstevel@tonic-gate default:
2320Sstevel@tonic-gate return (EINVAL);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate switch (psp->p_ridtype) {
2360Sstevel@tonic-gate case P_LWPID:
2370Sstevel@tonic-gate case P_PID:
2380Sstevel@tonic-gate case P_PPID:
2390Sstevel@tonic-gate case P_PGID:
2400Sstevel@tonic-gate case P_SID:
2410Sstevel@tonic-gate case P_TASKID:
2420Sstevel@tonic-gate case P_CID:
2430Sstevel@tonic-gate case P_UID:
2440Sstevel@tonic-gate case P_GID:
2450Sstevel@tonic-gate case P_PROJID:
2460Sstevel@tonic-gate case P_POOLID:
2470Sstevel@tonic-gate case P_ZONEID:
2480Sstevel@tonic-gate case P_CTID:
2490Sstevel@tonic-gate case P_ALL:
2500Sstevel@tonic-gate break;
2510Sstevel@tonic-gate default:
2520Sstevel@tonic-gate return (EINVAL);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate switch (psp->p_op) {
2560Sstevel@tonic-gate case POP_DIFF:
2570Sstevel@tonic-gate case POP_AND:
2580Sstevel@tonic-gate case POP_OR:
2590Sstevel@tonic-gate case POP_XOR:
2600Sstevel@tonic-gate break;
2610Sstevel@tonic-gate default:
2620Sstevel@tonic-gate return (EINVAL);
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate return (0);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate /*
2696618Srh87107 * procinset returns 1 if the process pointed to by pp is in the process
270*8067SJordan.Vaughan@Sun.com * set specified by psp, otherwise 0 is returned. If either process set operand
271*8067SJordan.Vaughan@Sun.com * has type P_CID and pp refers to a process that is exiting, by which we mean
272*8067SJordan.Vaughan@Sun.com * that its p_tlist is NULL, then procinset will return 0. pp's p_lock must be
273*8067SJordan.Vaughan@Sun.com * held across the call to this function. The caller should ensure that the
274*8067SJordan.Vaughan@Sun.com * process does not belong to the SYS scheduling class.
2750Sstevel@tonic-gate *
2760Sstevel@tonic-gate * This function expects to be called with a valid procset_t.
2770Sstevel@tonic-gate * The set should be checked using checkprocset() before calling
2780Sstevel@tonic-gate * this function.
2790Sstevel@tonic-gate */
2800Sstevel@tonic-gate int
procinset(proc_t * pp,procset_t * psp)2810Sstevel@tonic-gate procinset(proc_t *pp, procset_t *psp)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate int loperand = 0;
2840Sstevel@tonic-gate int roperand = 0;
2850Sstevel@tonic-gate int lwplinproc = 0;
2860Sstevel@tonic-gate int lwprinproc = 0;
287*8067SJordan.Vaughan@Sun.com kthread_t *tp;
2880Sstevel@tonic-gate
2896618Srh87107 ASSERT(MUTEX_HELD(&pp->p_lock));
2906618Srh87107
2910Sstevel@tonic-gate switch (psp->p_lidtype) {
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate case P_LWPID:
2940Sstevel@tonic-gate if (pp == ttoproc(curthread))
2950Sstevel@tonic-gate if (getlwpptr(psp->p_lid) != NULL)
2960Sstevel@tonic-gate lwplinproc++;
2970Sstevel@tonic-gate break;
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate case P_PID:
3000Sstevel@tonic-gate if (pp->p_pid == psp->p_lid)
3010Sstevel@tonic-gate loperand++;
3020Sstevel@tonic-gate break;
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate case P_PPID:
3050Sstevel@tonic-gate if (pp->p_ppid == psp->p_lid)
3060Sstevel@tonic-gate loperand++;
3070Sstevel@tonic-gate break;
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate case P_PGID:
3100Sstevel@tonic-gate if (pp->p_pgrp == psp->p_lid)
3110Sstevel@tonic-gate loperand++;
3120Sstevel@tonic-gate break;
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate case P_SID:
3152712Snn35248 mutex_enter(&pp->p_splock);
3160Sstevel@tonic-gate if (pp->p_sessp->s_sid == psp->p_lid)
3170Sstevel@tonic-gate loperand++;
3182712Snn35248 mutex_exit(&pp->p_splock);
3190Sstevel@tonic-gate break;
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate case P_CID:
322*8067SJordan.Vaughan@Sun.com tp = proctot(pp);
323*8067SJordan.Vaughan@Sun.com if (tp == NULL)
324*8067SJordan.Vaughan@Sun.com return (0);
3250Sstevel@tonic-gate if (tp->t_cid == psp->p_lid)
3260Sstevel@tonic-gate loperand++;
3270Sstevel@tonic-gate break;
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate case P_TASKID:
3300Sstevel@tonic-gate if (pp->p_task->tk_tkid == psp->p_lid)
3310Sstevel@tonic-gate loperand++;
3320Sstevel@tonic-gate break;
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate case P_UID:
3350Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
3360Sstevel@tonic-gate if (crgetuid(pp->p_cred) == psp->p_lid)
3370Sstevel@tonic-gate loperand++;
3380Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
3390Sstevel@tonic-gate break;
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate case P_GID:
3420Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
3430Sstevel@tonic-gate if (crgetgid(pp->p_cred) == psp->p_lid)
3440Sstevel@tonic-gate loperand++;
3450Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
3460Sstevel@tonic-gate break;
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate case P_PROJID:
3490Sstevel@tonic-gate if (pp->p_task->tk_proj->kpj_id == psp->p_lid)
3500Sstevel@tonic-gate loperand++;
3510Sstevel@tonic-gate break;
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate case P_POOLID:
3540Sstevel@tonic-gate if (pp->p_pool->pool_id == psp->p_lid)
3550Sstevel@tonic-gate loperand++;
3560Sstevel@tonic-gate break;
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate case P_ZONEID:
3590Sstevel@tonic-gate if (pp->p_zone->zone_id == psp->p_lid)
3600Sstevel@tonic-gate loperand++;
3610Sstevel@tonic-gate break;
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate case P_CTID:
3640Sstevel@tonic-gate if (PRCTID(pp) == psp->p_lid)
3650Sstevel@tonic-gate loperand++;
3660Sstevel@tonic-gate break;
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate case P_ALL:
3690Sstevel@tonic-gate loperand++;
3700Sstevel@tonic-gate break;
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate default:
3730Sstevel@tonic-gate #ifdef DEBUG
3740Sstevel@tonic-gate cmn_err(CE_WARN, "procinset called with bad set");
3750Sstevel@tonic-gate return (0);
3760Sstevel@tonic-gate #else
3770Sstevel@tonic-gate return (0);
3780Sstevel@tonic-gate #endif
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate
3810Sstevel@tonic-gate switch (psp->p_ridtype) {
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate case P_LWPID:
3840Sstevel@tonic-gate if (pp == ttoproc(curthread))
3850Sstevel@tonic-gate if (getlwpptr(psp->p_rid) != NULL)
3860Sstevel@tonic-gate lwprinproc++;
3870Sstevel@tonic-gate break;
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate case P_PID:
3900Sstevel@tonic-gate if (pp->p_pid == psp->p_rid)
3910Sstevel@tonic-gate roperand++;
3920Sstevel@tonic-gate break;
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate case P_PPID:
3950Sstevel@tonic-gate if (pp->p_ppid == psp->p_rid)
3960Sstevel@tonic-gate roperand++;
3970Sstevel@tonic-gate break;
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate case P_PGID:
4000Sstevel@tonic-gate if (pp->p_pgrp == psp->p_rid)
4010Sstevel@tonic-gate roperand++;
4020Sstevel@tonic-gate break;
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate case P_SID:
4052712Snn35248 mutex_enter(&pp->p_splock);
4060Sstevel@tonic-gate if (pp->p_sessp->s_sid == psp->p_rid)
4070Sstevel@tonic-gate roperand++;
4082712Snn35248 mutex_exit(&pp->p_splock);
4090Sstevel@tonic-gate break;
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate case P_TASKID:
4120Sstevel@tonic-gate if (pp->p_task->tk_tkid == psp->p_rid)
4130Sstevel@tonic-gate roperand++;
4140Sstevel@tonic-gate break;
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate case P_CID:
417*8067SJordan.Vaughan@Sun.com tp = proctot(pp);
418*8067SJordan.Vaughan@Sun.com if (tp == NULL)
419*8067SJordan.Vaughan@Sun.com return (0);
4200Sstevel@tonic-gate if (tp->t_cid == psp->p_rid)
4210Sstevel@tonic-gate roperand++;
4220Sstevel@tonic-gate break;
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate case P_UID:
4250Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
4260Sstevel@tonic-gate if (crgetuid(pp->p_cred) == psp->p_rid)
4270Sstevel@tonic-gate roperand++;
4280Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
4290Sstevel@tonic-gate break;
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate case P_GID:
4320Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
4330Sstevel@tonic-gate if (crgetgid(pp->p_cred) == psp->p_rid)
4340Sstevel@tonic-gate roperand++;
4350Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
4360Sstevel@tonic-gate break;
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate case P_PROJID:
4390Sstevel@tonic-gate if (pp->p_task->tk_proj->kpj_id == psp->p_rid)
4400Sstevel@tonic-gate roperand++;
4410Sstevel@tonic-gate break;
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate case P_POOLID:
4440Sstevel@tonic-gate if (pp->p_pool->pool_id == psp->p_rid)
4450Sstevel@tonic-gate roperand++;
4460Sstevel@tonic-gate break;
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate case P_ZONEID:
4490Sstevel@tonic-gate if (pp->p_zone->zone_id == psp->p_rid)
4500Sstevel@tonic-gate roperand++;
4510Sstevel@tonic-gate break;
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate case P_CTID:
4540Sstevel@tonic-gate if (PRCTID(pp) == psp->p_rid)
4550Sstevel@tonic-gate roperand++;
4560Sstevel@tonic-gate break;
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate case P_ALL:
4590Sstevel@tonic-gate roperand++;
4600Sstevel@tonic-gate break;
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate default:
4630Sstevel@tonic-gate #ifdef DEBUG
4640Sstevel@tonic-gate cmn_err(CE_WARN, "procinset called with bad set");
4650Sstevel@tonic-gate return (0);
4660Sstevel@tonic-gate #else
4670Sstevel@tonic-gate return (0);
4680Sstevel@tonic-gate #endif
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate switch (psp->p_op) {
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate case POP_DIFF:
4740Sstevel@tonic-gate if (loperand && !lwprinproc && !roperand)
4750Sstevel@tonic-gate return (1);
4760Sstevel@tonic-gate else
4770Sstevel@tonic-gate return (0);
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate case POP_AND:
4800Sstevel@tonic-gate if (loperand && roperand)
4810Sstevel@tonic-gate return (1);
4820Sstevel@tonic-gate else
4830Sstevel@tonic-gate return (0);
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate case POP_OR:
4860Sstevel@tonic-gate if (loperand || roperand)
4870Sstevel@tonic-gate return (1);
4880Sstevel@tonic-gate else
4890Sstevel@tonic-gate return (0);
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate case POP_XOR:
4920Sstevel@tonic-gate if ((loperand && !lwprinproc && !roperand) ||
4930Sstevel@tonic-gate (roperand && !lwplinproc && !loperand))
4940Sstevel@tonic-gate return (1);
4950Sstevel@tonic-gate else
4960Sstevel@tonic-gate return (0);
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate default:
4990Sstevel@tonic-gate #ifdef DEBUG
5000Sstevel@tonic-gate cmn_err(CE_WARN, "procinset called with bad set");
5010Sstevel@tonic-gate return (0);
5020Sstevel@tonic-gate #else
5030Sstevel@tonic-gate return (0);
5040Sstevel@tonic-gate #endif
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate /* NOTREACHED */
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate /*
5100Sstevel@tonic-gate * lwpinset returns 1 if the thread pointed to
5110Sstevel@tonic-gate * by tp is in the process set specified by psp and is not in
5120Sstevel@tonic-gate * the sys scheduling class - otherwise 0 is returned.
5130Sstevel@tonic-gate *
5140Sstevel@tonic-gate * This function expects to be called with a valid procset_t.
5150Sstevel@tonic-gate * The set should be checked using checkprocset() before calling
5160Sstevel@tonic-gate * this function.
5170Sstevel@tonic-gate */
5180Sstevel@tonic-gate int
lwpinset(proc_t * pp,procset_t * psp,kthread_t * tp,int * done)5190Sstevel@tonic-gate lwpinset(proc_t *pp, procset_t *psp, kthread_t *tp, int *done)
5200Sstevel@tonic-gate {
5210Sstevel@tonic-gate int loperand = 0;
5220Sstevel@tonic-gate int roperand = 0;
5230Sstevel@tonic-gate int lwplinset = 0;
5240Sstevel@tonic-gate int lwprinset = 0;
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate ASSERT(ttoproc(tp) == pp);
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate /*
5290Sstevel@tonic-gate * If process is in the sys class return (0).
5300Sstevel@tonic-gate */
5310Sstevel@tonic-gate if (proctot(pp)->t_cid == 0) {
5320Sstevel@tonic-gate return (0);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate switch (psp->p_lidtype) {
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate case P_LWPID:
5380Sstevel@tonic-gate if (tp->t_tid == psp->p_lid)
5390Sstevel@tonic-gate lwplinset ++;
5400Sstevel@tonic-gate break;
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate case P_PID:
5430Sstevel@tonic-gate if (pp->p_pid == psp->p_lid)
5440Sstevel@tonic-gate loperand++;
5450Sstevel@tonic-gate break;
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate case P_PPID:
5480Sstevel@tonic-gate if (pp->p_ppid == psp->p_lid)
5490Sstevel@tonic-gate loperand++;
5500Sstevel@tonic-gate break;
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate case P_PGID:
5530Sstevel@tonic-gate if (pp->p_pgrp == psp->p_lid)
5540Sstevel@tonic-gate loperand++;
5550Sstevel@tonic-gate break;
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate case P_SID:
5582712Snn35248 mutex_enter(&pp->p_splock);
5590Sstevel@tonic-gate if (pp->p_sessp->s_sid == psp->p_lid)
5600Sstevel@tonic-gate loperand++;
5612712Snn35248 mutex_exit(&pp->p_splock);
5620Sstevel@tonic-gate break;
5630Sstevel@tonic-gate
5640Sstevel@tonic-gate case P_TASKID:
5650Sstevel@tonic-gate if (pp->p_task->tk_tkid == psp->p_lid)
5660Sstevel@tonic-gate loperand++;
5670Sstevel@tonic-gate break;
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate case P_CID:
5700Sstevel@tonic-gate if (tp->t_cid == psp->p_lid)
5710Sstevel@tonic-gate loperand++;
5720Sstevel@tonic-gate break;
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate case P_UID:
5750Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
5760Sstevel@tonic-gate if (crgetuid(pp->p_cred) == psp->p_lid)
5770Sstevel@tonic-gate loperand++;
5780Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
5790Sstevel@tonic-gate break;
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate case P_GID:
5820Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
5830Sstevel@tonic-gate if (crgetgid(pp->p_cred) == psp->p_lid)
5840Sstevel@tonic-gate loperand++;
5850Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
5860Sstevel@tonic-gate break;
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate case P_PROJID:
5890Sstevel@tonic-gate if (pp->p_task->tk_proj->kpj_id == psp->p_lid)
5900Sstevel@tonic-gate loperand++;
5910Sstevel@tonic-gate break;
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate case P_POOLID:
5940Sstevel@tonic-gate if (pp->p_pool->pool_id == psp->p_lid)
5950Sstevel@tonic-gate loperand++;
5960Sstevel@tonic-gate break;
5970Sstevel@tonic-gate
5980Sstevel@tonic-gate case P_ZONEID:
5990Sstevel@tonic-gate if (pp->p_zone->zone_id == psp->p_lid)
6000Sstevel@tonic-gate loperand++;
6010Sstevel@tonic-gate break;
6020Sstevel@tonic-gate
6030Sstevel@tonic-gate case P_CTID:
6040Sstevel@tonic-gate if (PRCTID(pp) == psp->p_lid)
6050Sstevel@tonic-gate loperand++;
6060Sstevel@tonic-gate break;
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate case P_ALL:
6090Sstevel@tonic-gate loperand++;
6100Sstevel@tonic-gate break;
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate default:
6130Sstevel@tonic-gate #ifdef DEBUG
6140Sstevel@tonic-gate cmn_err(CE_WARN, "lwpinset called with bad set");
6150Sstevel@tonic-gate return (0);
6160Sstevel@tonic-gate #else
6170Sstevel@tonic-gate return (0);
6180Sstevel@tonic-gate #endif
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate switch (psp->p_ridtype) {
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate case P_LWPID:
6240Sstevel@tonic-gate if (tp->t_tid == psp->p_rid)
6250Sstevel@tonic-gate lwprinset ++;
6260Sstevel@tonic-gate break;
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate case P_PID:
6290Sstevel@tonic-gate if (pp->p_pid == psp->p_rid)
6300Sstevel@tonic-gate roperand++;
6310Sstevel@tonic-gate break;
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate case P_PPID:
6340Sstevel@tonic-gate if (pp->p_ppid == psp->p_rid)
6350Sstevel@tonic-gate roperand++;
6360Sstevel@tonic-gate break;
6370Sstevel@tonic-gate
6380Sstevel@tonic-gate case P_PGID:
6390Sstevel@tonic-gate if (pp->p_pgrp == psp->p_rid)
6400Sstevel@tonic-gate roperand++;
6410Sstevel@tonic-gate break;
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate case P_SID:
6442712Snn35248 mutex_enter(&pp->p_splock);
6450Sstevel@tonic-gate if (pp->p_sessp->s_sid == psp->p_rid)
6460Sstevel@tonic-gate roperand++;
6472712Snn35248 mutex_exit(&pp->p_splock);
6480Sstevel@tonic-gate break;
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate case P_TASKID:
6510Sstevel@tonic-gate if (pp->p_task->tk_tkid == psp->p_rid)
6520Sstevel@tonic-gate roperand++;
6530Sstevel@tonic-gate break;
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate case P_CID:
6560Sstevel@tonic-gate if (tp->t_cid == psp->p_rid)
6570Sstevel@tonic-gate roperand++;
6580Sstevel@tonic-gate break;
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate case P_UID:
6610Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
6620Sstevel@tonic-gate if (crgetuid(pp->p_cred) == psp->p_rid)
6630Sstevel@tonic-gate roperand++;
6640Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
6650Sstevel@tonic-gate break;
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate case P_GID:
6680Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
6690Sstevel@tonic-gate if (crgetgid(pp->p_cred) == psp->p_rid)
6700Sstevel@tonic-gate roperand++;
6710Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
6720Sstevel@tonic-gate break;
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate case P_PROJID:
6750Sstevel@tonic-gate if (pp->p_task->tk_proj->kpj_id == psp->p_rid)
6760Sstevel@tonic-gate roperand++;
6770Sstevel@tonic-gate break;
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate case P_POOLID:
6800Sstevel@tonic-gate if (pp->p_pool->pool_id == psp->p_rid)
6810Sstevel@tonic-gate roperand++;
6820Sstevel@tonic-gate break;
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate case P_ZONEID:
6850Sstevel@tonic-gate if (pp->p_zone->zone_id == psp->p_rid)
6860Sstevel@tonic-gate roperand++;
6870Sstevel@tonic-gate break;
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate case P_CTID:
6900Sstevel@tonic-gate if (PRCTID(pp) == psp->p_rid)
6910Sstevel@tonic-gate roperand++;
6920Sstevel@tonic-gate break;
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate case P_ALL:
6950Sstevel@tonic-gate roperand++;
6960Sstevel@tonic-gate break;
6970Sstevel@tonic-gate
6980Sstevel@tonic-gate default:
6990Sstevel@tonic-gate #ifdef DEBUG
7000Sstevel@tonic-gate cmn_err(CE_WARN, "lwpinset called with bad set");
7010Sstevel@tonic-gate return (0);
7020Sstevel@tonic-gate #else
7030Sstevel@tonic-gate return (0);
7040Sstevel@tonic-gate #endif
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate if (lwplinset && lwprinset)
7080Sstevel@tonic-gate *done = 1;
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate switch (psp->p_op) {
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate case POP_DIFF:
7130Sstevel@tonic-gate if ((loperand || lwplinset) && !(lwprinset || roperand))
7140Sstevel@tonic-gate return (1);
7150Sstevel@tonic-gate else
7160Sstevel@tonic-gate return (0);
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate case POP_AND:
7190Sstevel@tonic-gate if ((loperand || lwplinset) && (roperand || lwprinset))
7200Sstevel@tonic-gate return (1);
7210Sstevel@tonic-gate else
7220Sstevel@tonic-gate return (0);
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate case POP_OR:
7250Sstevel@tonic-gate if (loperand || roperand || lwplinset || lwprinset)
7260Sstevel@tonic-gate return (1);
7270Sstevel@tonic-gate else
7280Sstevel@tonic-gate return (0);
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate case POP_XOR:
7310Sstevel@tonic-gate if (((loperand || lwplinset) &&
7326618Srh87107 !(lwprinset || roperand)) ||
7330Sstevel@tonic-gate ((roperand || lwprinset) &&
7346618Srh87107 !(lwplinset || loperand)))
7350Sstevel@tonic-gate return (1);
7360Sstevel@tonic-gate else
7370Sstevel@tonic-gate return (0);
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate default:
7400Sstevel@tonic-gate #ifdef DEBUG
7410Sstevel@tonic-gate cmn_err(CE_WARN, "lwpinset called with bad set");
7420Sstevel@tonic-gate return (0);
7430Sstevel@tonic-gate #else
7440Sstevel@tonic-gate return (0);
7450Sstevel@tonic-gate #endif
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate /* NOTREACHED */
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate /*
7500Sstevel@tonic-gate * Check for common cases of procsets which specify only the
7510Sstevel@tonic-gate * current process. cur_inset_only() returns B_TRUE when
7520Sstevel@tonic-gate * the current process is the only one in the set. B_FALSE
7530Sstevel@tonic-gate * is returned to indicate that this may not be the case.
7540Sstevel@tonic-gate */
7550Sstevel@tonic-gate boolean_t
cur_inset_only(procset_t * psp)7560Sstevel@tonic-gate cur_inset_only(procset_t *psp)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate if (((psp->p_lidtype == P_PID &&
7596618Srh87107 (psp->p_lid == P_MYID ||
7606618Srh87107 psp->p_lid == ttoproc(curthread)->p_pid)) ||
7616618Srh87107 ((psp->p_lidtype == P_LWPID) &&
7626618Srh87107 (psp->p_lid == P_MYID ||
7636618Srh87107 psp->p_lid == curthread->t_tid))) &&
7646618Srh87107 psp->p_op == POP_AND && psp->p_ridtype == P_ALL)
7656618Srh87107 return (B_TRUE);
7660Sstevel@tonic-gate
7670Sstevel@tonic-gate if (((psp->p_ridtype == P_PID &&
7686618Srh87107 (psp->p_rid == P_MYID ||
7696618Srh87107 psp->p_rid == ttoproc(curthread)->p_pid)) ||
7706618Srh87107 ((psp->p_ridtype == P_LWPID) &&
7716618Srh87107 (psp->p_rid == P_MYID ||
7726618Srh87107 psp->p_rid == curthread->t_tid))) &&
7736618Srh87107 psp->p_op == POP_AND && psp->p_lidtype == P_ALL)
7746618Srh87107 return (B_TRUE);
7750Sstevel@tonic-gate
7760Sstevel@tonic-gate return (B_FALSE);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate id_t
getmyid(idtype_t idtype)7800Sstevel@tonic-gate getmyid(idtype_t idtype)
7810Sstevel@tonic-gate {
7820Sstevel@tonic-gate proc_t *pp;
7830Sstevel@tonic-gate uid_t uid;
7840Sstevel@tonic-gate gid_t gid;
7852712Snn35248 pid_t sid;
7860Sstevel@tonic-gate
7870Sstevel@tonic-gate pp = ttoproc(curthread);
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate switch (idtype) {
7900Sstevel@tonic-gate case P_LWPID:
7910Sstevel@tonic-gate return (curthread->t_tid);
7920Sstevel@tonic-gate
7930Sstevel@tonic-gate case P_PID:
7940Sstevel@tonic-gate return (pp->p_pid);
7950Sstevel@tonic-gate
7960Sstevel@tonic-gate case P_PPID:
7970Sstevel@tonic-gate return (pp->p_ppid);
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate case P_PGID:
8000Sstevel@tonic-gate return (pp->p_pgrp);
8010Sstevel@tonic-gate
8020Sstevel@tonic-gate case P_SID:
8032712Snn35248 mutex_enter(&pp->p_splock);
8042712Snn35248 sid = pp->p_sessp->s_sid;
8052712Snn35248 mutex_exit(&pp->p_splock);
8062712Snn35248 return (sid);
8070Sstevel@tonic-gate
8080Sstevel@tonic-gate case P_TASKID:
8090Sstevel@tonic-gate return (pp->p_task->tk_tkid);
8100Sstevel@tonic-gate
8110Sstevel@tonic-gate case P_CID:
8120Sstevel@tonic-gate return (curthread->t_cid);
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate case P_UID:
8150Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
8160Sstevel@tonic-gate uid = crgetuid(pp->p_cred);
8170Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
8180Sstevel@tonic-gate return (uid);
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate case P_GID:
8210Sstevel@tonic-gate mutex_enter(&pp->p_crlock);
8220Sstevel@tonic-gate gid = crgetgid(pp->p_cred);
8230Sstevel@tonic-gate mutex_exit(&pp->p_crlock);
8240Sstevel@tonic-gate return (gid);
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate case P_PROJID:
8270Sstevel@tonic-gate return (pp->p_task->tk_proj->kpj_id);
8280Sstevel@tonic-gate
8290Sstevel@tonic-gate case P_POOLID:
8300Sstevel@tonic-gate return (pp->p_pool->pool_id);
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate case P_ZONEID:
8330Sstevel@tonic-gate return (pp->p_zone->zone_id);
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate case P_CTID:
8360Sstevel@tonic-gate return (PRCTID(pp));
8370Sstevel@tonic-gate
8380Sstevel@tonic-gate case P_ALL:
8390Sstevel@tonic-gate /*
8400Sstevel@tonic-gate * The value doesn't matter for P_ALL.
8410Sstevel@tonic-gate */
8420Sstevel@tonic-gate return (0);
8430Sstevel@tonic-gate
8440Sstevel@tonic-gate default:
8450Sstevel@tonic-gate return (-1);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate
8490Sstevel@tonic-gate static kthread_t *
getlwpptr(id_t id)8500Sstevel@tonic-gate getlwpptr(id_t id)
8510Sstevel@tonic-gate {
8520Sstevel@tonic-gate proc_t *p;
8530Sstevel@tonic-gate kthread_t *t;
8540Sstevel@tonic-gate
8556618Srh87107 ASSERT(MUTEX_HELD(&(ttoproc(curthread)->p_lock)));
8566618Srh87107
8570Sstevel@tonic-gate if (id == P_MYID)
8580Sstevel@tonic-gate t = curthread;
8590Sstevel@tonic-gate else {
8600Sstevel@tonic-gate p = ttoproc(curthread);
8610Sstevel@tonic-gate t = idtot(p, id);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate return (t);
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate /*
8680Sstevel@tonic-gate * The dotolwp function locates the LWP(s) specified by the procset structure
8690Sstevel@tonic-gate * pointed to by psp. If funcp is non-NULL then it points to a function
8700Sstevel@tonic-gate * which dotolwp will call for each LWP in the specified set.
8710Sstevel@tonic-gate * LWPIDs specified in the procset structure always refer to lwps in curproc.
8720Sstevel@tonic-gate * The arguments for this function must be "char *arg", and "kthread_t *tp",
8730Sstevel@tonic-gate * where tp is a pointer to the current thread from the set.
8740Sstevel@tonic-gate * Note that these arguments are passed to the function in reversed order
8750Sstevel@tonic-gate * than the order of arguments passed by dotoprocs() to its callback function.
8760Sstevel@tonic-gate * Also note that there are two separate cases where this routine returns zero.
8770Sstevel@tonic-gate * In the first case no mutex is grabbed, in the second the p_lock mutex
8780Sstevel@tonic-gate * is NOT RELEASED. The priocntl code is expecting this behaviour.
8790Sstevel@tonic-gate */
8800Sstevel@tonic-gate int
dotolwp(procset_t * psp,int (* funcp)(),char * arg)8810Sstevel@tonic-gate dotolwp(procset_t *psp, int (*funcp)(), char *arg)
8820Sstevel@tonic-gate {
8830Sstevel@tonic-gate int error = 0;
8840Sstevel@tonic-gate int nfound = 0;
8850Sstevel@tonic-gate kthread_t *tp;
8860Sstevel@tonic-gate proc_t *pp;
8870Sstevel@tonic-gate int done = 0;
8880Sstevel@tonic-gate
8890Sstevel@tonic-gate /*
8900Sstevel@tonic-gate * Check that the procset_t is valid.
8910Sstevel@tonic-gate */
8920Sstevel@tonic-gate error = checkprocset(psp);
8930Sstevel@tonic-gate if (error) {
8940Sstevel@tonic-gate return (error);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate
8970Sstevel@tonic-gate mutex_enter(&pidlock);
8980Sstevel@tonic-gate
8990Sstevel@tonic-gate /*
9000Sstevel@tonic-gate * Check for the special value P_MYID in either operand
9010Sstevel@tonic-gate * and replace it with the correct value. We don't check
9020Sstevel@tonic-gate * for an error return from getmyid() because the idtypes
9030Sstevel@tonic-gate * have been validated by the checkprocset() call above.
9040Sstevel@tonic-gate */
9050Sstevel@tonic-gate if (psp->p_lid == P_MYID) {
9060Sstevel@tonic-gate psp->p_lid = getmyid(psp->p_lidtype);
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate if (psp->p_rid == P_MYID) {
9090Sstevel@tonic-gate psp->p_rid = getmyid(psp->p_ridtype);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate pp = ttoproc(curthread);
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate mutex_enter(&pp->p_lock);
9156618Srh87107 if (procinset(pp, psp) ||
9166618Srh87107 (tp = pp->p_tlist) == NULL) {
9170Sstevel@tonic-gate mutex_exit(&pp->p_lock);
9180Sstevel@tonic-gate mutex_exit(&pidlock);
9190Sstevel@tonic-gate return (0);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate do {
9220Sstevel@tonic-gate if (lwpinset(pp, psp, tp, &done)) {
9230Sstevel@tonic-gate nfound ++;
9240Sstevel@tonic-gate error = (*funcp)(arg, tp);
9250Sstevel@tonic-gate if (error) {
9260Sstevel@tonic-gate mutex_exit(&pp->p_lock);
9270Sstevel@tonic-gate mutex_exit(&pidlock);
9280Sstevel@tonic-gate return (error);
9290Sstevel@tonic-gate }
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate } while (((tp = tp->t_forw) != pp->p_tlist) && !done);
9320Sstevel@tonic-gate
9330Sstevel@tonic-gate if (nfound == 0) {
9340Sstevel@tonic-gate mutex_exit(&pp->p_lock);
9350Sstevel@tonic-gate mutex_exit(&pidlock);
9360Sstevel@tonic-gate return (ESRCH);
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate
9390Sstevel@tonic-gate mutex_exit(&pidlock);
9400Sstevel@tonic-gate return (error);
9410Sstevel@tonic-gate }
942