1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1989, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)kern_proc.c 8.4 (Berkeley) 1/4/94 3487b6de2bSPoul-Henning Kamp * $Id: kern_proc.c,v 1.13 1995/12/07 12:46:47 davidg Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38df8bae1dSRodney W. Grimes #include <sys/systm.h> 39df8bae1dSRodney W. Grimes #include <sys/kernel.h> 40972f9b20SPoul-Henning Kamp #include <sys/sysctl.h> 41df8bae1dSRodney W. Grimes #include <sys/proc.h> 42df8bae1dSRodney W. Grimes #include <sys/buf.h> 43df8bae1dSRodney W. Grimes #include <sys/acct.h> 44df8bae1dSRodney W. Grimes #include <sys/wait.h> 45df8bae1dSRodney W. Grimes #include <sys/file.h> 46df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h> 47df8bae1dSRodney W. Grimes #include <sys/uio.h> 48df8bae1dSRodney W. Grimes #include <sys/malloc.h> 49df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 50df8bae1dSRodney W. Grimes #include <sys/ioctl.h> 51df8bae1dSRodney W. Grimes #include <sys/tty.h> 52bb56ec4aSPoul-Henning Kamp #include <sys/signalvar.h> 53efeaf95aSDavid Greenman #include <vm/vm.h> 54efeaf95aSDavid Greenman #include <vm/vm_param.h> 55efeaf95aSDavid Greenman #include <vm/vm_prot.h> 56efeaf95aSDavid Greenman #include <vm/lock.h> 57efeaf95aSDavid Greenman #include <vm/pmap.h> 58efeaf95aSDavid Greenman #include <vm/vm_map.h> 59efeaf95aSDavid Greenman #include <sys/user.h> 60df8bae1dSRodney W. Grimes 61e8fb0b2cSDavid Greenman struct prochd qs[NQS]; /* as good a place as any... */ 62e8fb0b2cSDavid Greenman struct prochd rtqs[NQS]; /* Space for REALTIME queues too */ 637216391eSDavid Greenman struct prochd idqs[NQS]; /* Space for IDLE queues too */ 64e8fb0b2cSDavid Greenman 65f23b4c91SGarrett Wollman volatile struct proc *allproc; /* all processes */ 66f23b4c91SGarrett Wollman struct proc *zombproc; /* just zombies */ 67f23b4c91SGarrett Wollman 6887b6de2bSPoul-Henning Kamp static void pgdelete __P((struct pgrp *)); 6926f9a767SRodney W. Grimes 70df8bae1dSRodney W. Grimes /* 71df8bae1dSRodney W. Grimes * Structure associated with user cacheing. 72df8bae1dSRodney W. Grimes */ 7387b6de2bSPoul-Henning Kamp static struct uidinfo { 74df8bae1dSRodney W. Grimes struct uidinfo *ui_next; 75df8bae1dSRodney W. Grimes struct uidinfo **ui_prev; 76df8bae1dSRodney W. Grimes uid_t ui_uid; 77df8bae1dSRodney W. Grimes long ui_proccnt; 78df8bae1dSRodney W. Grimes } **uihashtbl; 7987b6de2bSPoul-Henning Kamp static u_long uihash; /* size of hash table - 1 */ 80df8bae1dSRodney W. Grimes #define UIHASH(uid) ((uid) & uihash) 81df8bae1dSRodney W. Grimes 8298d93822SBruce Evans static void orphanpg __P((struct pgrp *pg)); 8398d93822SBruce Evans 84df8bae1dSRodney W. Grimes /* 85df8bae1dSRodney W. Grimes * Allocate a hash table. 86df8bae1dSRodney W. Grimes */ 8726f9a767SRodney W. Grimes void 88df8bae1dSRodney W. Grimes usrinfoinit() 89df8bae1dSRodney W. Grimes { 90df8bae1dSRodney W. Grimes 91df8bae1dSRodney W. Grimes uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash); 92df8bae1dSRodney W. Grimes } 93df8bae1dSRodney W. Grimes 94df8bae1dSRodney W. Grimes /* 95df8bae1dSRodney W. Grimes * Change the count associated with number of processes 96df8bae1dSRodney W. Grimes * a given user is using. 97df8bae1dSRodney W. Grimes */ 98df8bae1dSRodney W. Grimes int 99df8bae1dSRodney W. Grimes chgproccnt(uid, diff) 100df8bae1dSRodney W. Grimes uid_t uid; 101df8bae1dSRodney W. Grimes int diff; 102df8bae1dSRodney W. Grimes { 103df8bae1dSRodney W. Grimes register struct uidinfo **uipp, *uip, *uiq; 104df8bae1dSRodney W. Grimes 105df8bae1dSRodney W. Grimes uipp = &uihashtbl[UIHASH(uid)]; 106df8bae1dSRodney W. Grimes for (uip = *uipp; uip; uip = uip->ui_next) 107df8bae1dSRodney W. Grimes if (uip->ui_uid == uid) 108df8bae1dSRodney W. Grimes break; 109df8bae1dSRodney W. Grimes if (uip) { 110df8bae1dSRodney W. Grimes uip->ui_proccnt += diff; 111df8bae1dSRodney W. Grimes if (uip->ui_proccnt > 0) 112df8bae1dSRodney W. Grimes return (uip->ui_proccnt); 113df8bae1dSRodney W. Grimes if (uip->ui_proccnt < 0) 114df8bae1dSRodney W. Grimes panic("chgproccnt: procs < 0"); 115bb56ec4aSPoul-Henning Kamp if ((uiq = uip->ui_next)) 116df8bae1dSRodney W. Grimes uiq->ui_prev = uip->ui_prev; 117df8bae1dSRodney W. Grimes *uip->ui_prev = uiq; 118df8bae1dSRodney W. Grimes FREE(uip, M_PROC); 119df8bae1dSRodney W. Grimes return (0); 120df8bae1dSRodney W. Grimes } 121df8bae1dSRodney W. Grimes if (diff <= 0) { 122df8bae1dSRodney W. Grimes if (diff == 0) 123df8bae1dSRodney W. Grimes return(0); 124df8bae1dSRodney W. Grimes panic("chgproccnt: lost user"); 125df8bae1dSRodney W. Grimes } 126df8bae1dSRodney W. Grimes MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK); 127bb56ec4aSPoul-Henning Kamp if ((uiq = *uipp)) 128df8bae1dSRodney W. Grimes uiq->ui_prev = &uip->ui_next; 129df8bae1dSRodney W. Grimes uip->ui_next = uiq; 130df8bae1dSRodney W. Grimes uip->ui_prev = uipp; 131df8bae1dSRodney W. Grimes *uipp = uip; 132df8bae1dSRodney W. Grimes uip->ui_uid = uid; 133df8bae1dSRodney W. Grimes uip->ui_proccnt = diff; 134df8bae1dSRodney W. Grimes return (diff); 135df8bae1dSRodney W. Grimes } 136df8bae1dSRodney W. Grimes 137df8bae1dSRodney W. Grimes /* 138df8bae1dSRodney W. Grimes * Is p an inferior of the current process? 139df8bae1dSRodney W. Grimes */ 14026f9a767SRodney W. Grimes int 141df8bae1dSRodney W. Grimes inferior(p) 142df8bae1dSRodney W. Grimes register struct proc *p; 143df8bae1dSRodney W. Grimes { 144df8bae1dSRodney W. Grimes 145df8bae1dSRodney W. Grimes for (; p != curproc; p = p->p_pptr) 146df8bae1dSRodney W. Grimes if (p->p_pid == 0) 147df8bae1dSRodney W. Grimes return (0); 148df8bae1dSRodney W. Grimes return (1); 149df8bae1dSRodney W. Grimes } 150df8bae1dSRodney W. Grimes 151df8bae1dSRodney W. Grimes /* 152df8bae1dSRodney W. Grimes * Locate a process by number 153df8bae1dSRodney W. Grimes */ 154df8bae1dSRodney W. Grimes struct proc * 155df8bae1dSRodney W. Grimes pfind(pid) 156df8bae1dSRodney W. Grimes register pid_t pid; 157df8bae1dSRodney W. Grimes { 158df8bae1dSRodney W. Grimes register struct proc *p; 159df8bae1dSRodney W. Grimes 160df8bae1dSRodney W. Grimes for (p = pidhash[PIDHASH(pid)]; p != NULL; p = p->p_hash) 161df8bae1dSRodney W. Grimes if (p->p_pid == pid) 162df8bae1dSRodney W. Grimes return (p); 163df8bae1dSRodney W. Grimes return (NULL); 164df8bae1dSRodney W. Grimes } 165df8bae1dSRodney W. Grimes 166df8bae1dSRodney W. Grimes /* 167df8bae1dSRodney W. Grimes * Locate a process group by number 168df8bae1dSRodney W. Grimes */ 169df8bae1dSRodney W. Grimes struct pgrp * 170df8bae1dSRodney W. Grimes pgfind(pgid) 171df8bae1dSRodney W. Grimes register pid_t pgid; 172df8bae1dSRodney W. Grimes { 173df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 174df8bae1dSRodney W. Grimes 175df8bae1dSRodney W. Grimes for (pgrp = pgrphash[PIDHASH(pgid)]; 176df8bae1dSRodney W. Grimes pgrp != NULL; pgrp = pgrp->pg_hforw) 177df8bae1dSRodney W. Grimes if (pgrp->pg_id == pgid) 178df8bae1dSRodney W. Grimes return (pgrp); 179df8bae1dSRodney W. Grimes return (NULL); 180df8bae1dSRodney W. Grimes } 181df8bae1dSRodney W. Grimes 182df8bae1dSRodney W. Grimes /* 183df8bae1dSRodney W. Grimes * Move p to a new or existing process group (and session) 184df8bae1dSRodney W. Grimes */ 18526f9a767SRodney W. Grimes int 186df8bae1dSRodney W. Grimes enterpgrp(p, pgid, mksess) 187df8bae1dSRodney W. Grimes register struct proc *p; 188df8bae1dSRodney W. Grimes pid_t pgid; 189df8bae1dSRodney W. Grimes int mksess; 190df8bae1dSRodney W. Grimes { 191df8bae1dSRodney W. Grimes register struct pgrp *pgrp = pgfind(pgid); 192df8bae1dSRodney W. Grimes register struct proc **pp; 193df8bae1dSRodney W. Grimes int n; 194df8bae1dSRodney W. Grimes 195df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 196df8bae1dSRodney W. Grimes if (pgrp != NULL && mksess) /* firewalls */ 197df8bae1dSRodney W. Grimes panic("enterpgrp: setsid into non-empty pgrp"); 198df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) 199df8bae1dSRodney W. Grimes panic("enterpgrp: session leader attempted setpgrp"); 200df8bae1dSRodney W. Grimes #endif 201df8bae1dSRodney W. Grimes if (pgrp == NULL) { 202df8bae1dSRodney W. Grimes pid_t savepid = p->p_pid; 203df8bae1dSRodney W. Grimes struct proc *np; 204df8bae1dSRodney W. Grimes /* 205df8bae1dSRodney W. Grimes * new process group 206df8bae1dSRodney W. Grimes */ 207df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 208df8bae1dSRodney W. Grimes if (p->p_pid != pgid) 209df8bae1dSRodney W. Grimes panic("enterpgrp: new pgrp and pid != pgid"); 210df8bae1dSRodney W. Grimes #endif 211df8bae1dSRodney W. Grimes MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, 212df8bae1dSRodney W. Grimes M_WAITOK); 213df8bae1dSRodney W. Grimes if ((np = pfind(savepid)) == NULL || np != p) 214df8bae1dSRodney W. Grimes return (ESRCH); 215df8bae1dSRodney W. Grimes if (mksess) { 216df8bae1dSRodney W. Grimes register struct session *sess; 217df8bae1dSRodney W. Grimes 218df8bae1dSRodney W. Grimes /* 219df8bae1dSRodney W. Grimes * new session 220df8bae1dSRodney W. Grimes */ 221df8bae1dSRodney W. Grimes MALLOC(sess, struct session *, sizeof(struct session), 222df8bae1dSRodney W. Grimes M_SESSION, M_WAITOK); 223df8bae1dSRodney W. Grimes sess->s_leader = p; 224df8bae1dSRodney W. Grimes sess->s_count = 1; 225df8bae1dSRodney W. Grimes sess->s_ttyvp = NULL; 226df8bae1dSRodney W. Grimes sess->s_ttyp = NULL; 227df8bae1dSRodney W. Grimes bcopy(p->p_session->s_login, sess->s_login, 228df8bae1dSRodney W. Grimes sizeof(sess->s_login)); 229df8bae1dSRodney W. Grimes p->p_flag &= ~P_CONTROLT; 230df8bae1dSRodney W. Grimes pgrp->pg_session = sess; 231df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 232df8bae1dSRodney W. Grimes if (p != curproc) 233df8bae1dSRodney W. Grimes panic("enterpgrp: mksession and p != curproc"); 234df8bae1dSRodney W. Grimes #endif 235df8bae1dSRodney W. Grimes } else { 236df8bae1dSRodney W. Grimes pgrp->pg_session = p->p_session; 237df8bae1dSRodney W. Grimes pgrp->pg_session->s_count++; 238df8bae1dSRodney W. Grimes } 239df8bae1dSRodney W. Grimes pgrp->pg_id = pgid; 240df8bae1dSRodney W. Grimes pgrp->pg_hforw = pgrphash[n = PIDHASH(pgid)]; 241df8bae1dSRodney W. Grimes pgrphash[n] = pgrp; 242df8bae1dSRodney W. Grimes pgrp->pg_jobc = 0; 243df8bae1dSRodney W. Grimes pgrp->pg_mem = NULL; 244df8bae1dSRodney W. Grimes } else if (pgrp == p->p_pgrp) 245df8bae1dSRodney W. Grimes return (0); 246df8bae1dSRodney W. Grimes 247df8bae1dSRodney W. Grimes /* 248df8bae1dSRodney W. Grimes * Adjust eligibility of affected pgrps to participate in job control. 249df8bae1dSRodney W. Grimes * Increment eligibility counts before decrementing, otherwise we 250df8bae1dSRodney W. Grimes * could reach 0 spuriously during the first call. 251df8bae1dSRodney W. Grimes */ 252df8bae1dSRodney W. Grimes fixjobc(p, pgrp, 1); 253df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 254df8bae1dSRodney W. Grimes 255df8bae1dSRodney W. Grimes /* 256df8bae1dSRodney W. Grimes * unlink p from old process group 257df8bae1dSRodney W. Grimes */ 258df8bae1dSRodney W. Grimes for (pp = &p->p_pgrp->pg_mem; *pp; pp = &(*pp)->p_pgrpnxt) { 259df8bae1dSRodney W. Grimes if (*pp == p) { 260df8bae1dSRodney W. Grimes *pp = p->p_pgrpnxt; 261df8bae1dSRodney W. Grimes break; 262df8bae1dSRodney W. Grimes } 263df8bae1dSRodney W. Grimes } 264df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 265df8bae1dSRodney W. Grimes if (pp == NULL) 266df8bae1dSRodney W. Grimes panic("enterpgrp: can't find p on old pgrp"); 267df8bae1dSRodney W. Grimes #endif 268df8bae1dSRodney W. Grimes /* 269df8bae1dSRodney W. Grimes * delete old if empty 270df8bae1dSRodney W. Grimes */ 271df8bae1dSRodney W. Grimes if (p->p_pgrp->pg_mem == 0) 272df8bae1dSRodney W. Grimes pgdelete(p->p_pgrp); 273df8bae1dSRodney W. Grimes /* 274df8bae1dSRodney W. Grimes * link into new one 275df8bae1dSRodney W. Grimes */ 276df8bae1dSRodney W. Grimes p->p_pgrp = pgrp; 277df8bae1dSRodney W. Grimes p->p_pgrpnxt = pgrp->pg_mem; 278df8bae1dSRodney W. Grimes pgrp->pg_mem = p; 279df8bae1dSRodney W. Grimes return (0); 280df8bae1dSRodney W. Grimes } 281df8bae1dSRodney W. Grimes 282df8bae1dSRodney W. Grimes /* 283df8bae1dSRodney W. Grimes * remove process from process group 284df8bae1dSRodney W. Grimes */ 28526f9a767SRodney W. Grimes int 286df8bae1dSRodney W. Grimes leavepgrp(p) 287df8bae1dSRodney W. Grimes register struct proc *p; 288df8bae1dSRodney W. Grimes { 289df8bae1dSRodney W. Grimes register struct proc **pp = &p->p_pgrp->pg_mem; 290df8bae1dSRodney W. Grimes 291df8bae1dSRodney W. Grimes for (; *pp; pp = &(*pp)->p_pgrpnxt) { 292df8bae1dSRodney W. Grimes if (*pp == p) { 293df8bae1dSRodney W. Grimes *pp = p->p_pgrpnxt; 294df8bae1dSRodney W. Grimes break; 295df8bae1dSRodney W. Grimes } 296df8bae1dSRodney W. Grimes } 297df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 298df8bae1dSRodney W. Grimes if (pp == NULL) 299df8bae1dSRodney W. Grimes panic("leavepgrp: can't find p in pgrp"); 300df8bae1dSRodney W. Grimes #endif 301df8bae1dSRodney W. Grimes if (!p->p_pgrp->pg_mem) 302df8bae1dSRodney W. Grimes pgdelete(p->p_pgrp); 303df8bae1dSRodney W. Grimes p->p_pgrp = 0; 304df8bae1dSRodney W. Grimes return (0); 305df8bae1dSRodney W. Grimes } 306df8bae1dSRodney W. Grimes 307df8bae1dSRodney W. Grimes /* 308df8bae1dSRodney W. Grimes * delete a process group 309df8bae1dSRodney W. Grimes */ 31087b6de2bSPoul-Henning Kamp static void 311df8bae1dSRodney W. Grimes pgdelete(pgrp) 312df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 313df8bae1dSRodney W. Grimes { 314df8bae1dSRodney W. Grimes register struct pgrp **pgp = &pgrphash[PIDHASH(pgrp->pg_id)]; 315df8bae1dSRodney W. Grimes 316df8bae1dSRodney W. Grimes if (pgrp->pg_session->s_ttyp != NULL && 317df8bae1dSRodney W. Grimes pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 318df8bae1dSRodney W. Grimes pgrp->pg_session->s_ttyp->t_pgrp = NULL; 319df8bae1dSRodney W. Grimes for (; *pgp; pgp = &(*pgp)->pg_hforw) { 320df8bae1dSRodney W. Grimes if (*pgp == pgrp) { 321df8bae1dSRodney W. Grimes *pgp = pgrp->pg_hforw; 322df8bae1dSRodney W. Grimes break; 323df8bae1dSRodney W. Grimes } 324df8bae1dSRodney W. Grimes } 325df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 326df8bae1dSRodney W. Grimes if (pgp == NULL) 327df8bae1dSRodney W. Grimes panic("pgdelete: can't find pgrp on hash chain"); 328df8bae1dSRodney W. Grimes #endif 329df8bae1dSRodney W. Grimes if (--pgrp->pg_session->s_count == 0) 330df8bae1dSRodney W. Grimes FREE(pgrp->pg_session, M_SESSION); 331df8bae1dSRodney W. Grimes FREE(pgrp, M_PGRP); 332df8bae1dSRodney W. Grimes } 333df8bae1dSRodney W. Grimes 334df8bae1dSRodney W. Grimes /* 335df8bae1dSRodney W. Grimes * Adjust pgrp jobc counters when specified process changes process group. 336df8bae1dSRodney W. Grimes * We count the number of processes in each process group that "qualify" 337df8bae1dSRodney W. Grimes * the group for terminal job control (those with a parent in a different 338df8bae1dSRodney W. Grimes * process group of the same session). If that count reaches zero, the 339df8bae1dSRodney W. Grimes * process group becomes orphaned. Check both the specified process' 340df8bae1dSRodney W. Grimes * process group and that of its children. 341df8bae1dSRodney W. Grimes * entering == 0 => p is leaving specified group. 342df8bae1dSRodney W. Grimes * entering == 1 => p is entering specified group. 343df8bae1dSRodney W. Grimes */ 34426f9a767SRodney W. Grimes void 345df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering) 346df8bae1dSRodney W. Grimes register struct proc *p; 347df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 348df8bae1dSRodney W. Grimes int entering; 349df8bae1dSRodney W. Grimes { 350df8bae1dSRodney W. Grimes register struct pgrp *hispgrp; 351df8bae1dSRodney W. Grimes register struct session *mysession = pgrp->pg_session; 352df8bae1dSRodney W. Grimes 353df8bae1dSRodney W. Grimes /* 354df8bae1dSRodney W. Grimes * Check p's parent to see whether p qualifies its own process 355df8bae1dSRodney W. Grimes * group; if so, adjust count for p's process group. 356df8bae1dSRodney W. Grimes */ 357df8bae1dSRodney W. Grimes if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 358df8bae1dSRodney W. Grimes hispgrp->pg_session == mysession) 359df8bae1dSRodney W. Grimes if (entering) 360df8bae1dSRodney W. Grimes pgrp->pg_jobc++; 361df8bae1dSRodney W. Grimes else if (--pgrp->pg_jobc == 0) 362df8bae1dSRodney W. Grimes orphanpg(pgrp); 363df8bae1dSRodney W. Grimes 364df8bae1dSRodney W. Grimes /* 365df8bae1dSRodney W. Grimes * Check this process' children to see whether they qualify 366df8bae1dSRodney W. Grimes * their process groups; if so, adjust counts for children's 367df8bae1dSRodney W. Grimes * process groups. 368df8bae1dSRodney W. Grimes */ 369df8bae1dSRodney W. Grimes for (p = p->p_cptr; p; p = p->p_osptr) 370df8bae1dSRodney W. Grimes if ((hispgrp = p->p_pgrp) != pgrp && 371df8bae1dSRodney W. Grimes hispgrp->pg_session == mysession && 372df8bae1dSRodney W. Grimes p->p_stat != SZOMB) 373df8bae1dSRodney W. Grimes if (entering) 374df8bae1dSRodney W. Grimes hispgrp->pg_jobc++; 375df8bae1dSRodney W. Grimes else if (--hispgrp->pg_jobc == 0) 376df8bae1dSRodney W. Grimes orphanpg(hispgrp); 377df8bae1dSRodney W. Grimes } 378df8bae1dSRodney W. Grimes 379df8bae1dSRodney W. Grimes /* 380df8bae1dSRodney W. Grimes * A process group has become orphaned; 381df8bae1dSRodney W. Grimes * if there are any stopped processes in the group, 382df8bae1dSRodney W. Grimes * hang-up all process in that group. 383df8bae1dSRodney W. Grimes */ 384df8bae1dSRodney W. Grimes static void 385df8bae1dSRodney W. Grimes orphanpg(pg) 386df8bae1dSRodney W. Grimes struct pgrp *pg; 387df8bae1dSRodney W. Grimes { 388df8bae1dSRodney W. Grimes register struct proc *p; 389df8bae1dSRodney W. Grimes 390df8bae1dSRodney W. Grimes for (p = pg->pg_mem; p; p = p->p_pgrpnxt) { 391df8bae1dSRodney W. Grimes if (p->p_stat == SSTOP) { 392df8bae1dSRodney W. Grimes for (p = pg->pg_mem; p; p = p->p_pgrpnxt) { 393df8bae1dSRodney W. Grimes psignal(p, SIGHUP); 394df8bae1dSRodney W. Grimes psignal(p, SIGCONT); 395df8bae1dSRodney W. Grimes } 396df8bae1dSRodney W. Grimes return; 397df8bae1dSRodney W. Grimes } 398df8bae1dSRodney W. Grimes } 399df8bae1dSRodney W. Grimes } 400df8bae1dSRodney W. Grimes 401df8bae1dSRodney W. Grimes #ifdef debug 402df8bae1dSRodney W. Grimes /* DEBUG */ 403df8bae1dSRodney W. Grimes pgrpdump() 404df8bae1dSRodney W. Grimes { 405df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 406df8bae1dSRodney W. Grimes register struct proc *p; 407df8bae1dSRodney W. Grimes register i; 408df8bae1dSRodney W. Grimes 409df8bae1dSRodney W. Grimes for (i=0; i<PIDHSZ; i++) { 410df8bae1dSRodney W. Grimes if (pgrphash[i]) { 411df8bae1dSRodney W. Grimes printf("\tindx %d\n", i); 412df8bae1dSRodney W. Grimes for (pgrp=pgrphash[i]; pgrp; pgrp=pgrp->pg_hforw) { 413df8bae1dSRodney W. Grimes printf("\tpgrp %x, pgid %d, sess %x, sesscnt %d, mem %x\n", 414df8bae1dSRodney W. Grimes pgrp, pgrp->pg_id, pgrp->pg_session, 415df8bae1dSRodney W. Grimes pgrp->pg_session->s_count, pgrp->pg_mem); 416df8bae1dSRodney W. Grimes for (p=pgrp->pg_mem; p; p=p->p_pgrpnxt) { 417df8bae1dSRodney W. Grimes printf("\t\tpid %d addr %x pgrp %x\n", 418df8bae1dSRodney W. Grimes p->p_pid, p, p->p_pgrp); 419df8bae1dSRodney W. Grimes } 420df8bae1dSRodney W. Grimes } 421df8bae1dSRodney W. Grimes 422df8bae1dSRodney W. Grimes } 423df8bae1dSRodney W. Grimes } 424df8bae1dSRodney W. Grimes } 425df8bae1dSRodney W. Grimes #endif /* debug */ 426972f9b20SPoul-Henning Kamp 427972f9b20SPoul-Henning Kamp /* 428972f9b20SPoul-Henning Kamp * Fill in an eproc structure for the specified process. 429972f9b20SPoul-Henning Kamp */ 430972f9b20SPoul-Henning Kamp void 431972f9b20SPoul-Henning Kamp fill_eproc(p, ep) 432972f9b20SPoul-Henning Kamp register struct proc *p; 433972f9b20SPoul-Henning Kamp register struct eproc *ep; 434972f9b20SPoul-Henning Kamp { 435972f9b20SPoul-Henning Kamp register struct tty *tp; 436972f9b20SPoul-Henning Kamp 437972f9b20SPoul-Henning Kamp bzero(ep, sizeof(*ep)); 438972f9b20SPoul-Henning Kamp 439972f9b20SPoul-Henning Kamp ep->e_paddr = p; 440972f9b20SPoul-Henning Kamp if (p->p_cred) { 441972f9b20SPoul-Henning Kamp ep->e_pcred = *p->p_cred; 442972f9b20SPoul-Henning Kamp if (p->p_ucred) 443972f9b20SPoul-Henning Kamp ep->e_ucred = *p->p_ucred; 444972f9b20SPoul-Henning Kamp } 445972f9b20SPoul-Henning Kamp if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) { 446972f9b20SPoul-Henning Kamp register struct vmspace *vm = p->p_vmspace; 447972f9b20SPoul-Henning Kamp 448972f9b20SPoul-Henning Kamp #ifdef pmap_resident_count 449972f9b20SPoul-Henning Kamp ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/ 450972f9b20SPoul-Henning Kamp #else 451972f9b20SPoul-Henning Kamp ep->e_vm.vm_rssize = vm->vm_rssize; 452972f9b20SPoul-Henning Kamp #endif 453972f9b20SPoul-Henning Kamp ep->e_vm.vm_tsize = vm->vm_tsize; 454972f9b20SPoul-Henning Kamp ep->e_vm.vm_dsize = vm->vm_dsize; 455972f9b20SPoul-Henning Kamp ep->e_vm.vm_ssize = vm->vm_ssize; 456972f9b20SPoul-Henning Kamp #ifndef sparc 457972f9b20SPoul-Henning Kamp ep->e_vm.vm_pmap = vm->vm_pmap; 458972f9b20SPoul-Henning Kamp #endif 459972f9b20SPoul-Henning Kamp } 460972f9b20SPoul-Henning Kamp if (p->p_pptr) 461972f9b20SPoul-Henning Kamp ep->e_ppid = p->p_pptr->p_pid; 462972f9b20SPoul-Henning Kamp if (p->p_pgrp) { 463972f9b20SPoul-Henning Kamp ep->e_sess = p->p_pgrp->pg_session; 464972f9b20SPoul-Henning Kamp ep->e_pgid = p->p_pgrp->pg_id; 465972f9b20SPoul-Henning Kamp ep->e_jobc = p->p_pgrp->pg_jobc; 466972f9b20SPoul-Henning Kamp } 467972f9b20SPoul-Henning Kamp if ((p->p_flag & P_CONTROLT) && 468972f9b20SPoul-Henning Kamp (ep->e_sess != NULL) && 469972f9b20SPoul-Henning Kamp ((tp = ep->e_sess->s_ttyp) != NULL)) { 470972f9b20SPoul-Henning Kamp ep->e_tdev = tp->t_dev; 471972f9b20SPoul-Henning Kamp ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 472972f9b20SPoul-Henning Kamp ep->e_tsess = tp->t_session; 473972f9b20SPoul-Henning Kamp } else 474972f9b20SPoul-Henning Kamp ep->e_tdev = NODEV; 475972f9b20SPoul-Henning Kamp if (ep->e_sess && ep->e_sess->s_ttyvp) 476972f9b20SPoul-Henning Kamp ep->e_flag = EPROC_CTTY; 477972f9b20SPoul-Henning Kamp if (SESS_LEADER(p)) 478972f9b20SPoul-Henning Kamp ep->e_flag |= EPROC_SLEADER; 479972f9b20SPoul-Henning Kamp if (p->p_wmesg) { 480972f9b20SPoul-Henning Kamp strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN); 481972f9b20SPoul-Henning Kamp ep->e_wmesg[WMESGLEN] = 0; 482972f9b20SPoul-Henning Kamp } 483972f9b20SPoul-Henning Kamp } 484972f9b20SPoul-Henning Kamp 485972f9b20SPoul-Henning Kamp static int 486972f9b20SPoul-Henning Kamp sysctl_kern_proc SYSCTL_HANDLER_ARGS 487972f9b20SPoul-Henning Kamp { 488972f9b20SPoul-Henning Kamp int *name = (int*) arg1; 489972f9b20SPoul-Henning Kamp u_int namelen = arg2; 490972f9b20SPoul-Henning Kamp struct proc *p; 491972f9b20SPoul-Henning Kamp int doingzomb; 492972f9b20SPoul-Henning Kamp struct eproc eproc; 493972f9b20SPoul-Henning Kamp int error = 0; 494972f9b20SPoul-Henning Kamp 495972f9b20SPoul-Henning Kamp if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL)) 496972f9b20SPoul-Henning Kamp return (EINVAL); 497972f9b20SPoul-Henning Kamp if (!req->oldptr) { 498972f9b20SPoul-Henning Kamp /* 499972f9b20SPoul-Henning Kamp * try over estimating by 5 procs 500972f9b20SPoul-Henning Kamp */ 501972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 502972f9b20SPoul-Henning Kamp if (error) 503972f9b20SPoul-Henning Kamp return (error); 504972f9b20SPoul-Henning Kamp } 505972f9b20SPoul-Henning Kamp p = (struct proc *)allproc; 506972f9b20SPoul-Henning Kamp doingzomb = 0; 507972f9b20SPoul-Henning Kamp again: 508972f9b20SPoul-Henning Kamp for (; p != NULL; p = p->p_next) { 509972f9b20SPoul-Henning Kamp /* 510972f9b20SPoul-Henning Kamp * Skip embryonic processes. 511972f9b20SPoul-Henning Kamp */ 512972f9b20SPoul-Henning Kamp if (p->p_stat == SIDL) 513972f9b20SPoul-Henning Kamp continue; 514972f9b20SPoul-Henning Kamp /* 515972f9b20SPoul-Henning Kamp * TODO - make more efficient (see notes below). 516972f9b20SPoul-Henning Kamp * do by session. 517972f9b20SPoul-Henning Kamp */ 518972f9b20SPoul-Henning Kamp switch (name[0]) { 519972f9b20SPoul-Henning Kamp 520972f9b20SPoul-Henning Kamp case KERN_PROC_PID: 521972f9b20SPoul-Henning Kamp /* could do this with just a lookup */ 522972f9b20SPoul-Henning Kamp if (p->p_pid != (pid_t)name[1]) 523972f9b20SPoul-Henning Kamp continue; 524972f9b20SPoul-Henning Kamp break; 525972f9b20SPoul-Henning Kamp 526972f9b20SPoul-Henning Kamp case KERN_PROC_PGRP: 527972f9b20SPoul-Henning Kamp /* could do this by traversing pgrp */ 528972f9b20SPoul-Henning Kamp if (p->p_pgrp == NULL || p->p_pgrp->pg_id != (pid_t)name[1]) 529972f9b20SPoul-Henning Kamp continue; 530972f9b20SPoul-Henning Kamp break; 531972f9b20SPoul-Henning Kamp 532972f9b20SPoul-Henning Kamp case KERN_PROC_TTY: 533972f9b20SPoul-Henning Kamp if ((p->p_flag & P_CONTROLT) == 0 || 534972f9b20SPoul-Henning Kamp p->p_session == NULL || 535972f9b20SPoul-Henning Kamp p->p_session->s_ttyp == NULL || 536972f9b20SPoul-Henning Kamp p->p_session->s_ttyp->t_dev != (dev_t)name[1]) 537972f9b20SPoul-Henning Kamp continue; 538972f9b20SPoul-Henning Kamp break; 539972f9b20SPoul-Henning Kamp 540972f9b20SPoul-Henning Kamp case KERN_PROC_UID: 541972f9b20SPoul-Henning Kamp if (p->p_ucred == NULL || p->p_ucred->cr_uid != (uid_t)name[1]) 542972f9b20SPoul-Henning Kamp continue; 543972f9b20SPoul-Henning Kamp break; 544972f9b20SPoul-Henning Kamp 545972f9b20SPoul-Henning Kamp case KERN_PROC_RUID: 546972f9b20SPoul-Henning Kamp if (p->p_ucred == NULL || p->p_cred->p_ruid != (uid_t)name[1]) 547972f9b20SPoul-Henning Kamp continue; 548972f9b20SPoul-Henning Kamp break; 549972f9b20SPoul-Henning Kamp } 550972f9b20SPoul-Henning Kamp 551972f9b20SPoul-Henning Kamp fill_eproc(p, &eproc); 552972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req,(caddr_t)p, sizeof(struct proc)); 553972f9b20SPoul-Henning Kamp if (error) 554972f9b20SPoul-Henning Kamp return (error); 555972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc)); 556972f9b20SPoul-Henning Kamp if (error) 557972f9b20SPoul-Henning Kamp return (error); 558972f9b20SPoul-Henning Kamp } 559972f9b20SPoul-Henning Kamp if (doingzomb == 0) { 560972f9b20SPoul-Henning Kamp p = zombproc; 561972f9b20SPoul-Henning Kamp doingzomb++; 562972f9b20SPoul-Henning Kamp goto again; 563972f9b20SPoul-Henning Kamp } 564972f9b20SPoul-Henning Kamp return (0); 565972f9b20SPoul-Henning Kamp } 566972f9b20SPoul-Henning Kamp 567972f9b20SPoul-Henning Kamp SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 568972f9b20SPoul-Henning Kamp sysctl_kern_proc, "Process table"); 569