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