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 34972f9b20SPoul-Henning Kamp * $Id: kern_proc.c,v 1.10 1995/05/30 08:05:37 rgrimes 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> 41972f9b20SPoul-Henning Kamp #include <sys/user.h> 42df8bae1dSRodney W. Grimes #include <sys/proc.h> 43df8bae1dSRodney W. Grimes #include <sys/buf.h> 44df8bae1dSRodney W. Grimes #include <sys/acct.h> 45df8bae1dSRodney W. Grimes #include <sys/wait.h> 46df8bae1dSRodney W. Grimes #include <sys/file.h> 47df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h> 48df8bae1dSRodney W. Grimes #include <sys/uio.h> 49df8bae1dSRodney W. Grimes #include <sys/malloc.h> 50df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 51df8bae1dSRodney W. Grimes #include <sys/ioctl.h> 52df8bae1dSRodney W. Grimes #include <sys/tty.h> 53bb56ec4aSPoul-Henning Kamp #include <sys/signalvar.h> 54df8bae1dSRodney W. Grimes 55e8fb0b2cSDavid Greenman struct prochd qs[NQS]; /* as good a place as any... */ 56e8fb0b2cSDavid Greenman struct prochd rtqs[NQS]; /* Space for REALTIME queues too */ 577216391eSDavid Greenman struct prochd idqs[NQS]; /* Space for IDLE queues too */ 58e8fb0b2cSDavid Greenman 59f23b4c91SGarrett Wollman volatile struct proc *allproc; /* all processes */ 60f23b4c91SGarrett Wollman struct proc *zombproc; /* just zombies */ 61f23b4c91SGarrett Wollman 6226f9a767SRodney W. Grimes void pgdelete __P((struct pgrp *)); 6326f9a767SRodney W. Grimes 64df8bae1dSRodney W. Grimes /* 65df8bae1dSRodney W. Grimes * Structure associated with user cacheing. 66df8bae1dSRodney W. Grimes */ 67df8bae1dSRodney W. Grimes struct uidinfo { 68df8bae1dSRodney W. Grimes struct uidinfo *ui_next; 69df8bae1dSRodney W. Grimes struct uidinfo **ui_prev; 70df8bae1dSRodney W. Grimes uid_t ui_uid; 71df8bae1dSRodney W. Grimes long ui_proccnt; 72df8bae1dSRodney W. Grimes } **uihashtbl; 73df8bae1dSRodney W. Grimes u_long uihash; /* size of hash table - 1 */ 74df8bae1dSRodney W. Grimes #define UIHASH(uid) ((uid) & uihash) 75df8bae1dSRodney W. Grimes 76df8bae1dSRodney W. Grimes /* 77df8bae1dSRodney W. Grimes * Allocate a hash table. 78df8bae1dSRodney W. Grimes */ 7926f9a767SRodney W. Grimes void 80df8bae1dSRodney W. Grimes usrinfoinit() 81df8bae1dSRodney W. Grimes { 82df8bae1dSRodney W. Grimes 83df8bae1dSRodney W. Grimes uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash); 84df8bae1dSRodney W. Grimes } 85df8bae1dSRodney W. Grimes 86df8bae1dSRodney W. Grimes /* 87df8bae1dSRodney W. Grimes * Change the count associated with number of processes 88df8bae1dSRodney W. Grimes * a given user is using. 89df8bae1dSRodney W. Grimes */ 90df8bae1dSRodney W. Grimes int 91df8bae1dSRodney W. Grimes chgproccnt(uid, diff) 92df8bae1dSRodney W. Grimes uid_t uid; 93df8bae1dSRodney W. Grimes int diff; 94df8bae1dSRodney W. Grimes { 95df8bae1dSRodney W. Grimes register struct uidinfo **uipp, *uip, *uiq; 96df8bae1dSRodney W. Grimes 97df8bae1dSRodney W. Grimes uipp = &uihashtbl[UIHASH(uid)]; 98df8bae1dSRodney W. Grimes for (uip = *uipp; uip; uip = uip->ui_next) 99df8bae1dSRodney W. Grimes if (uip->ui_uid == uid) 100df8bae1dSRodney W. Grimes break; 101df8bae1dSRodney W. Grimes if (uip) { 102df8bae1dSRodney W. Grimes uip->ui_proccnt += diff; 103df8bae1dSRodney W. Grimes if (uip->ui_proccnt > 0) 104df8bae1dSRodney W. Grimes return (uip->ui_proccnt); 105df8bae1dSRodney W. Grimes if (uip->ui_proccnt < 0) 106df8bae1dSRodney W. Grimes panic("chgproccnt: procs < 0"); 107bb56ec4aSPoul-Henning Kamp if ((uiq = uip->ui_next)) 108df8bae1dSRodney W. Grimes uiq->ui_prev = uip->ui_prev; 109df8bae1dSRodney W. Grimes *uip->ui_prev = uiq; 110df8bae1dSRodney W. Grimes FREE(uip, M_PROC); 111df8bae1dSRodney W. Grimes return (0); 112df8bae1dSRodney W. Grimes } 113df8bae1dSRodney W. Grimes if (diff <= 0) { 114df8bae1dSRodney W. Grimes if (diff == 0) 115df8bae1dSRodney W. Grimes return(0); 116df8bae1dSRodney W. Grimes panic("chgproccnt: lost user"); 117df8bae1dSRodney W. Grimes } 118df8bae1dSRodney W. Grimes MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK); 119bb56ec4aSPoul-Henning Kamp if ((uiq = *uipp)) 120df8bae1dSRodney W. Grimes uiq->ui_prev = &uip->ui_next; 121df8bae1dSRodney W. Grimes uip->ui_next = uiq; 122df8bae1dSRodney W. Grimes uip->ui_prev = uipp; 123df8bae1dSRodney W. Grimes *uipp = uip; 124df8bae1dSRodney W. Grimes uip->ui_uid = uid; 125df8bae1dSRodney W. Grimes uip->ui_proccnt = diff; 126df8bae1dSRodney W. Grimes return (diff); 127df8bae1dSRodney W. Grimes } 128df8bae1dSRodney W. Grimes 129df8bae1dSRodney W. Grimes /* 130df8bae1dSRodney W. Grimes * Is p an inferior of the current process? 131df8bae1dSRodney W. Grimes */ 13226f9a767SRodney W. Grimes int 133df8bae1dSRodney W. Grimes inferior(p) 134df8bae1dSRodney W. Grimes register struct proc *p; 135df8bae1dSRodney W. Grimes { 136df8bae1dSRodney W. Grimes 137df8bae1dSRodney W. Grimes for (; p != curproc; p = p->p_pptr) 138df8bae1dSRodney W. Grimes if (p->p_pid == 0) 139df8bae1dSRodney W. Grimes return (0); 140df8bae1dSRodney W. Grimes return (1); 141df8bae1dSRodney W. Grimes } 142df8bae1dSRodney W. Grimes 143df8bae1dSRodney W. Grimes /* 144df8bae1dSRodney W. Grimes * Locate a process by number 145df8bae1dSRodney W. Grimes */ 146df8bae1dSRodney W. Grimes struct proc * 147df8bae1dSRodney W. Grimes pfind(pid) 148df8bae1dSRodney W. Grimes register pid_t pid; 149df8bae1dSRodney W. Grimes { 150df8bae1dSRodney W. Grimes register struct proc *p; 151df8bae1dSRodney W. Grimes 152df8bae1dSRodney W. Grimes for (p = pidhash[PIDHASH(pid)]; p != NULL; p = p->p_hash) 153df8bae1dSRodney W. Grimes if (p->p_pid == pid) 154df8bae1dSRodney W. Grimes return (p); 155df8bae1dSRodney W. Grimes return (NULL); 156df8bae1dSRodney W. Grimes } 157df8bae1dSRodney W. Grimes 158df8bae1dSRodney W. Grimes /* 159df8bae1dSRodney W. Grimes * Locate a process group by number 160df8bae1dSRodney W. Grimes */ 161df8bae1dSRodney W. Grimes struct pgrp * 162df8bae1dSRodney W. Grimes pgfind(pgid) 163df8bae1dSRodney W. Grimes register pid_t pgid; 164df8bae1dSRodney W. Grimes { 165df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 166df8bae1dSRodney W. Grimes 167df8bae1dSRodney W. Grimes for (pgrp = pgrphash[PIDHASH(pgid)]; 168df8bae1dSRodney W. Grimes pgrp != NULL; pgrp = pgrp->pg_hforw) 169df8bae1dSRodney W. Grimes if (pgrp->pg_id == pgid) 170df8bae1dSRodney W. Grimes return (pgrp); 171df8bae1dSRodney W. Grimes return (NULL); 172df8bae1dSRodney W. Grimes } 173df8bae1dSRodney W. Grimes 174df8bae1dSRodney W. Grimes /* 175df8bae1dSRodney W. Grimes * Move p to a new or existing process group (and session) 176df8bae1dSRodney W. Grimes */ 17726f9a767SRodney W. Grimes int 178df8bae1dSRodney W. Grimes enterpgrp(p, pgid, mksess) 179df8bae1dSRodney W. Grimes register struct proc *p; 180df8bae1dSRodney W. Grimes pid_t pgid; 181df8bae1dSRodney W. Grimes int mksess; 182df8bae1dSRodney W. Grimes { 183df8bae1dSRodney W. Grimes register struct pgrp *pgrp = pgfind(pgid); 184df8bae1dSRodney W. Grimes register struct proc **pp; 185df8bae1dSRodney W. Grimes int n; 186df8bae1dSRodney W. Grimes 187df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 188df8bae1dSRodney W. Grimes if (pgrp != NULL && mksess) /* firewalls */ 189df8bae1dSRodney W. Grimes panic("enterpgrp: setsid into non-empty pgrp"); 190df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) 191df8bae1dSRodney W. Grimes panic("enterpgrp: session leader attempted setpgrp"); 192df8bae1dSRodney W. Grimes #endif 193df8bae1dSRodney W. Grimes if (pgrp == NULL) { 194df8bae1dSRodney W. Grimes pid_t savepid = p->p_pid; 195df8bae1dSRodney W. Grimes struct proc *np; 196df8bae1dSRodney W. Grimes /* 197df8bae1dSRodney W. Grimes * new process group 198df8bae1dSRodney W. Grimes */ 199df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 200df8bae1dSRodney W. Grimes if (p->p_pid != pgid) 201df8bae1dSRodney W. Grimes panic("enterpgrp: new pgrp and pid != pgid"); 202df8bae1dSRodney W. Grimes #endif 203df8bae1dSRodney W. Grimes MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, 204df8bae1dSRodney W. Grimes M_WAITOK); 205df8bae1dSRodney W. Grimes if ((np = pfind(savepid)) == NULL || np != p) 206df8bae1dSRodney W. Grimes return (ESRCH); 207df8bae1dSRodney W. Grimes if (mksess) { 208df8bae1dSRodney W. Grimes register struct session *sess; 209df8bae1dSRodney W. Grimes 210df8bae1dSRodney W. Grimes /* 211df8bae1dSRodney W. Grimes * new session 212df8bae1dSRodney W. Grimes */ 213df8bae1dSRodney W. Grimes MALLOC(sess, struct session *, sizeof(struct session), 214df8bae1dSRodney W. Grimes M_SESSION, M_WAITOK); 215df8bae1dSRodney W. Grimes sess->s_leader = p; 216df8bae1dSRodney W. Grimes sess->s_count = 1; 217df8bae1dSRodney W. Grimes sess->s_ttyvp = NULL; 218df8bae1dSRodney W. Grimes sess->s_ttyp = NULL; 219df8bae1dSRodney W. Grimes bcopy(p->p_session->s_login, sess->s_login, 220df8bae1dSRodney W. Grimes sizeof(sess->s_login)); 221df8bae1dSRodney W. Grimes p->p_flag &= ~P_CONTROLT; 222df8bae1dSRodney W. Grimes pgrp->pg_session = sess; 223df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 224df8bae1dSRodney W. Grimes if (p != curproc) 225df8bae1dSRodney W. Grimes panic("enterpgrp: mksession and p != curproc"); 226df8bae1dSRodney W. Grimes #endif 227df8bae1dSRodney W. Grimes } else { 228df8bae1dSRodney W. Grimes pgrp->pg_session = p->p_session; 229df8bae1dSRodney W. Grimes pgrp->pg_session->s_count++; 230df8bae1dSRodney W. Grimes } 231df8bae1dSRodney W. Grimes pgrp->pg_id = pgid; 232df8bae1dSRodney W. Grimes pgrp->pg_hforw = pgrphash[n = PIDHASH(pgid)]; 233df8bae1dSRodney W. Grimes pgrphash[n] = pgrp; 234df8bae1dSRodney W. Grimes pgrp->pg_jobc = 0; 235df8bae1dSRodney W. Grimes pgrp->pg_mem = NULL; 236df8bae1dSRodney W. Grimes } else if (pgrp == p->p_pgrp) 237df8bae1dSRodney W. Grimes return (0); 238df8bae1dSRodney W. Grimes 239df8bae1dSRodney W. Grimes /* 240df8bae1dSRodney W. Grimes * Adjust eligibility of affected pgrps to participate in job control. 241df8bae1dSRodney W. Grimes * Increment eligibility counts before decrementing, otherwise we 242df8bae1dSRodney W. Grimes * could reach 0 spuriously during the first call. 243df8bae1dSRodney W. Grimes */ 244df8bae1dSRodney W. Grimes fixjobc(p, pgrp, 1); 245df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 246df8bae1dSRodney W. Grimes 247df8bae1dSRodney W. Grimes /* 248df8bae1dSRodney W. Grimes * unlink p from old process group 249df8bae1dSRodney W. Grimes */ 250df8bae1dSRodney W. Grimes for (pp = &p->p_pgrp->pg_mem; *pp; pp = &(*pp)->p_pgrpnxt) { 251df8bae1dSRodney W. Grimes if (*pp == p) { 252df8bae1dSRodney W. Grimes *pp = p->p_pgrpnxt; 253df8bae1dSRodney W. Grimes break; 254df8bae1dSRodney W. Grimes } 255df8bae1dSRodney W. Grimes } 256df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 257df8bae1dSRodney W. Grimes if (pp == NULL) 258df8bae1dSRodney W. Grimes panic("enterpgrp: can't find p on old pgrp"); 259df8bae1dSRodney W. Grimes #endif 260df8bae1dSRodney W. Grimes /* 261df8bae1dSRodney W. Grimes * delete old if empty 262df8bae1dSRodney W. Grimes */ 263df8bae1dSRodney W. Grimes if (p->p_pgrp->pg_mem == 0) 264df8bae1dSRodney W. Grimes pgdelete(p->p_pgrp); 265df8bae1dSRodney W. Grimes /* 266df8bae1dSRodney W. Grimes * link into new one 267df8bae1dSRodney W. Grimes */ 268df8bae1dSRodney W. Grimes p->p_pgrp = pgrp; 269df8bae1dSRodney W. Grimes p->p_pgrpnxt = pgrp->pg_mem; 270df8bae1dSRodney W. Grimes pgrp->pg_mem = p; 271df8bae1dSRodney W. Grimes return (0); 272df8bae1dSRodney W. Grimes } 273df8bae1dSRodney W. Grimes 274df8bae1dSRodney W. Grimes /* 275df8bae1dSRodney W. Grimes * remove process from process group 276df8bae1dSRodney W. Grimes */ 27726f9a767SRodney W. Grimes int 278df8bae1dSRodney W. Grimes leavepgrp(p) 279df8bae1dSRodney W. Grimes register struct proc *p; 280df8bae1dSRodney W. Grimes { 281df8bae1dSRodney W. Grimes register struct proc **pp = &p->p_pgrp->pg_mem; 282df8bae1dSRodney W. Grimes 283df8bae1dSRodney W. Grimes for (; *pp; pp = &(*pp)->p_pgrpnxt) { 284df8bae1dSRodney W. Grimes if (*pp == p) { 285df8bae1dSRodney W. Grimes *pp = p->p_pgrpnxt; 286df8bae1dSRodney W. Grimes break; 287df8bae1dSRodney W. Grimes } 288df8bae1dSRodney W. Grimes } 289df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 290df8bae1dSRodney W. Grimes if (pp == NULL) 291df8bae1dSRodney W. Grimes panic("leavepgrp: can't find p in pgrp"); 292df8bae1dSRodney W. Grimes #endif 293df8bae1dSRodney W. Grimes if (!p->p_pgrp->pg_mem) 294df8bae1dSRodney W. Grimes pgdelete(p->p_pgrp); 295df8bae1dSRodney W. Grimes p->p_pgrp = 0; 296df8bae1dSRodney W. Grimes return (0); 297df8bae1dSRodney W. Grimes } 298df8bae1dSRodney W. Grimes 299df8bae1dSRodney W. Grimes /* 300df8bae1dSRodney W. Grimes * delete a process group 301df8bae1dSRodney W. Grimes */ 30226f9a767SRodney W. Grimes void 303df8bae1dSRodney W. Grimes pgdelete(pgrp) 304df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 305df8bae1dSRodney W. Grimes { 306df8bae1dSRodney W. Grimes register struct pgrp **pgp = &pgrphash[PIDHASH(pgrp->pg_id)]; 307df8bae1dSRodney W. Grimes 308df8bae1dSRodney W. Grimes if (pgrp->pg_session->s_ttyp != NULL && 309df8bae1dSRodney W. Grimes pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 310df8bae1dSRodney W. Grimes pgrp->pg_session->s_ttyp->t_pgrp = NULL; 311df8bae1dSRodney W. Grimes for (; *pgp; pgp = &(*pgp)->pg_hforw) { 312df8bae1dSRodney W. Grimes if (*pgp == pgrp) { 313df8bae1dSRodney W. Grimes *pgp = pgrp->pg_hforw; 314df8bae1dSRodney W. Grimes break; 315df8bae1dSRodney W. Grimes } 316df8bae1dSRodney W. Grimes } 317df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 318df8bae1dSRodney W. Grimes if (pgp == NULL) 319df8bae1dSRodney W. Grimes panic("pgdelete: can't find pgrp on hash chain"); 320df8bae1dSRodney W. Grimes #endif 321df8bae1dSRodney W. Grimes if (--pgrp->pg_session->s_count == 0) 322df8bae1dSRodney W. Grimes FREE(pgrp->pg_session, M_SESSION); 323df8bae1dSRodney W. Grimes FREE(pgrp, M_PGRP); 324df8bae1dSRodney W. Grimes } 325df8bae1dSRodney W. Grimes 326df8bae1dSRodney W. Grimes static void orphanpg(); 327df8bae1dSRodney W. Grimes 328df8bae1dSRodney W. Grimes /* 329df8bae1dSRodney W. Grimes * Adjust pgrp jobc counters when specified process changes process group. 330df8bae1dSRodney W. Grimes * We count the number of processes in each process group that "qualify" 331df8bae1dSRodney W. Grimes * the group for terminal job control (those with a parent in a different 332df8bae1dSRodney W. Grimes * process group of the same session). If that count reaches zero, the 333df8bae1dSRodney W. Grimes * process group becomes orphaned. Check both the specified process' 334df8bae1dSRodney W. Grimes * process group and that of its children. 335df8bae1dSRodney W. Grimes * entering == 0 => p is leaving specified group. 336df8bae1dSRodney W. Grimes * entering == 1 => p is entering specified group. 337df8bae1dSRodney W. Grimes */ 33826f9a767SRodney W. Grimes void 339df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering) 340df8bae1dSRodney W. Grimes register struct proc *p; 341df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 342df8bae1dSRodney W. Grimes int entering; 343df8bae1dSRodney W. Grimes { 344df8bae1dSRodney W. Grimes register struct pgrp *hispgrp; 345df8bae1dSRodney W. Grimes register struct session *mysession = pgrp->pg_session; 346df8bae1dSRodney W. Grimes 347df8bae1dSRodney W. Grimes /* 348df8bae1dSRodney W. Grimes * Check p's parent to see whether p qualifies its own process 349df8bae1dSRodney W. Grimes * group; if so, adjust count for p's process group. 350df8bae1dSRodney W. Grimes */ 351df8bae1dSRodney W. Grimes if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 352df8bae1dSRodney W. Grimes hispgrp->pg_session == mysession) 353df8bae1dSRodney W. Grimes if (entering) 354df8bae1dSRodney W. Grimes pgrp->pg_jobc++; 355df8bae1dSRodney W. Grimes else if (--pgrp->pg_jobc == 0) 356df8bae1dSRodney W. Grimes orphanpg(pgrp); 357df8bae1dSRodney W. Grimes 358df8bae1dSRodney W. Grimes /* 359df8bae1dSRodney W. Grimes * Check this process' children to see whether they qualify 360df8bae1dSRodney W. Grimes * their process groups; if so, adjust counts for children's 361df8bae1dSRodney W. Grimes * process groups. 362df8bae1dSRodney W. Grimes */ 363df8bae1dSRodney W. Grimes for (p = p->p_cptr; p; p = p->p_osptr) 364df8bae1dSRodney W. Grimes if ((hispgrp = p->p_pgrp) != pgrp && 365df8bae1dSRodney W. Grimes hispgrp->pg_session == mysession && 366df8bae1dSRodney W. Grimes p->p_stat != SZOMB) 367df8bae1dSRodney W. Grimes if (entering) 368df8bae1dSRodney W. Grimes hispgrp->pg_jobc++; 369df8bae1dSRodney W. Grimes else if (--hispgrp->pg_jobc == 0) 370df8bae1dSRodney W. Grimes orphanpg(hispgrp); 371df8bae1dSRodney W. Grimes } 372df8bae1dSRodney W. Grimes 373df8bae1dSRodney W. Grimes /* 374df8bae1dSRodney W. Grimes * A process group has become orphaned; 375df8bae1dSRodney W. Grimes * if there are any stopped processes in the group, 376df8bae1dSRodney W. Grimes * hang-up all process in that group. 377df8bae1dSRodney W. Grimes */ 378df8bae1dSRodney W. Grimes static void 379df8bae1dSRodney W. Grimes orphanpg(pg) 380df8bae1dSRodney W. Grimes struct pgrp *pg; 381df8bae1dSRodney W. Grimes { 382df8bae1dSRodney W. Grimes register struct proc *p; 383df8bae1dSRodney W. Grimes 384df8bae1dSRodney W. Grimes for (p = pg->pg_mem; p; p = p->p_pgrpnxt) { 385df8bae1dSRodney W. Grimes if (p->p_stat == SSTOP) { 386df8bae1dSRodney W. Grimes for (p = pg->pg_mem; p; p = p->p_pgrpnxt) { 387df8bae1dSRodney W. Grimes psignal(p, SIGHUP); 388df8bae1dSRodney W. Grimes psignal(p, SIGCONT); 389df8bae1dSRodney W. Grimes } 390df8bae1dSRodney W. Grimes return; 391df8bae1dSRodney W. Grimes } 392df8bae1dSRodney W. Grimes } 393df8bae1dSRodney W. Grimes } 394df8bae1dSRodney W. Grimes 395df8bae1dSRodney W. Grimes #ifdef debug 396df8bae1dSRodney W. Grimes /* DEBUG */ 397df8bae1dSRodney W. Grimes pgrpdump() 398df8bae1dSRodney W. Grimes { 399df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 400df8bae1dSRodney W. Grimes register struct proc *p; 401df8bae1dSRodney W. Grimes register i; 402df8bae1dSRodney W. Grimes 403df8bae1dSRodney W. Grimes for (i=0; i<PIDHSZ; i++) { 404df8bae1dSRodney W. Grimes if (pgrphash[i]) { 405df8bae1dSRodney W. Grimes printf("\tindx %d\n", i); 406df8bae1dSRodney W. Grimes for (pgrp=pgrphash[i]; pgrp; pgrp=pgrp->pg_hforw) { 407df8bae1dSRodney W. Grimes printf("\tpgrp %x, pgid %d, sess %x, sesscnt %d, mem %x\n", 408df8bae1dSRodney W. Grimes pgrp, pgrp->pg_id, pgrp->pg_session, 409df8bae1dSRodney W. Grimes pgrp->pg_session->s_count, pgrp->pg_mem); 410df8bae1dSRodney W. Grimes for (p=pgrp->pg_mem; p; p=p->p_pgrpnxt) { 411df8bae1dSRodney W. Grimes printf("\t\tpid %d addr %x pgrp %x\n", 412df8bae1dSRodney W. Grimes p->p_pid, p, p->p_pgrp); 413df8bae1dSRodney W. Grimes } 414df8bae1dSRodney W. Grimes } 415df8bae1dSRodney W. Grimes 416df8bae1dSRodney W. Grimes } 417df8bae1dSRodney W. Grimes } 418df8bae1dSRodney W. Grimes } 419df8bae1dSRodney W. Grimes #endif /* debug */ 420972f9b20SPoul-Henning Kamp 421972f9b20SPoul-Henning Kamp /* 422972f9b20SPoul-Henning Kamp * Fill in an eproc structure for the specified process. 423972f9b20SPoul-Henning Kamp */ 424972f9b20SPoul-Henning Kamp void 425972f9b20SPoul-Henning Kamp fill_eproc(p, ep) 426972f9b20SPoul-Henning Kamp register struct proc *p; 427972f9b20SPoul-Henning Kamp register struct eproc *ep; 428972f9b20SPoul-Henning Kamp { 429972f9b20SPoul-Henning Kamp register struct tty *tp; 430972f9b20SPoul-Henning Kamp 431972f9b20SPoul-Henning Kamp bzero(ep, sizeof(*ep)); 432972f9b20SPoul-Henning Kamp 433972f9b20SPoul-Henning Kamp ep->e_paddr = p; 434972f9b20SPoul-Henning Kamp if (p->p_cred) { 435972f9b20SPoul-Henning Kamp ep->e_pcred = *p->p_cred; 436972f9b20SPoul-Henning Kamp if (p->p_ucred) 437972f9b20SPoul-Henning Kamp ep->e_ucred = *p->p_ucred; 438972f9b20SPoul-Henning Kamp } 439972f9b20SPoul-Henning Kamp if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) { 440972f9b20SPoul-Henning Kamp register struct vmspace *vm = p->p_vmspace; 441972f9b20SPoul-Henning Kamp 442972f9b20SPoul-Henning Kamp #ifdef pmap_resident_count 443972f9b20SPoul-Henning Kamp ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/ 444972f9b20SPoul-Henning Kamp #else 445972f9b20SPoul-Henning Kamp ep->e_vm.vm_rssize = vm->vm_rssize; 446972f9b20SPoul-Henning Kamp #endif 447972f9b20SPoul-Henning Kamp ep->e_vm.vm_tsize = vm->vm_tsize; 448972f9b20SPoul-Henning Kamp ep->e_vm.vm_dsize = vm->vm_dsize; 449972f9b20SPoul-Henning Kamp ep->e_vm.vm_ssize = vm->vm_ssize; 450972f9b20SPoul-Henning Kamp #ifndef sparc 451972f9b20SPoul-Henning Kamp ep->e_vm.vm_pmap = vm->vm_pmap; 452972f9b20SPoul-Henning Kamp #endif 453972f9b20SPoul-Henning Kamp } 454972f9b20SPoul-Henning Kamp if (p->p_pptr) 455972f9b20SPoul-Henning Kamp ep->e_ppid = p->p_pptr->p_pid; 456972f9b20SPoul-Henning Kamp if (p->p_pgrp) { 457972f9b20SPoul-Henning Kamp ep->e_sess = p->p_pgrp->pg_session; 458972f9b20SPoul-Henning Kamp ep->e_pgid = p->p_pgrp->pg_id; 459972f9b20SPoul-Henning Kamp ep->e_jobc = p->p_pgrp->pg_jobc; 460972f9b20SPoul-Henning Kamp } 461972f9b20SPoul-Henning Kamp if ((p->p_flag & P_CONTROLT) && 462972f9b20SPoul-Henning Kamp (ep->e_sess != NULL) && 463972f9b20SPoul-Henning Kamp ((tp = ep->e_sess->s_ttyp) != NULL)) { 464972f9b20SPoul-Henning Kamp ep->e_tdev = tp->t_dev; 465972f9b20SPoul-Henning Kamp ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 466972f9b20SPoul-Henning Kamp ep->e_tsess = tp->t_session; 467972f9b20SPoul-Henning Kamp } else 468972f9b20SPoul-Henning Kamp ep->e_tdev = NODEV; 469972f9b20SPoul-Henning Kamp if (ep->e_sess && ep->e_sess->s_ttyvp) 470972f9b20SPoul-Henning Kamp ep->e_flag = EPROC_CTTY; 471972f9b20SPoul-Henning Kamp if (SESS_LEADER(p)) 472972f9b20SPoul-Henning Kamp ep->e_flag |= EPROC_SLEADER; 473972f9b20SPoul-Henning Kamp if (p->p_wmesg) { 474972f9b20SPoul-Henning Kamp strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN); 475972f9b20SPoul-Henning Kamp ep->e_wmesg[WMESGLEN] = 0; 476972f9b20SPoul-Henning Kamp } 477972f9b20SPoul-Henning Kamp } 478972f9b20SPoul-Henning Kamp 479972f9b20SPoul-Henning Kamp static int 480972f9b20SPoul-Henning Kamp sysctl_kern_proc SYSCTL_HANDLER_ARGS 481972f9b20SPoul-Henning Kamp { 482972f9b20SPoul-Henning Kamp int *name = (int*) arg1; 483972f9b20SPoul-Henning Kamp u_int namelen = arg2; 484972f9b20SPoul-Henning Kamp struct proc *p; 485972f9b20SPoul-Henning Kamp int doingzomb; 486972f9b20SPoul-Henning Kamp struct eproc eproc; 487972f9b20SPoul-Henning Kamp int error = 0; 488972f9b20SPoul-Henning Kamp 489972f9b20SPoul-Henning Kamp if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL)) 490972f9b20SPoul-Henning Kamp return (EINVAL); 491972f9b20SPoul-Henning Kamp if (!req->oldptr) { 492972f9b20SPoul-Henning Kamp /* 493972f9b20SPoul-Henning Kamp * try over estimating by 5 procs 494972f9b20SPoul-Henning Kamp */ 495972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 496972f9b20SPoul-Henning Kamp if (error) 497972f9b20SPoul-Henning Kamp return (error); 498972f9b20SPoul-Henning Kamp } 499972f9b20SPoul-Henning Kamp p = (struct proc *)allproc; 500972f9b20SPoul-Henning Kamp doingzomb = 0; 501972f9b20SPoul-Henning Kamp again: 502972f9b20SPoul-Henning Kamp for (; p != NULL; p = p->p_next) { 503972f9b20SPoul-Henning Kamp /* 504972f9b20SPoul-Henning Kamp * Skip embryonic processes. 505972f9b20SPoul-Henning Kamp */ 506972f9b20SPoul-Henning Kamp if (p->p_stat == SIDL) 507972f9b20SPoul-Henning Kamp continue; 508972f9b20SPoul-Henning Kamp /* 509972f9b20SPoul-Henning Kamp * TODO - make more efficient (see notes below). 510972f9b20SPoul-Henning Kamp * do by session. 511972f9b20SPoul-Henning Kamp */ 512972f9b20SPoul-Henning Kamp switch (name[0]) { 513972f9b20SPoul-Henning Kamp 514972f9b20SPoul-Henning Kamp case KERN_PROC_PID: 515972f9b20SPoul-Henning Kamp /* could do this with just a lookup */ 516972f9b20SPoul-Henning Kamp if (p->p_pid != (pid_t)name[1]) 517972f9b20SPoul-Henning Kamp continue; 518972f9b20SPoul-Henning Kamp break; 519972f9b20SPoul-Henning Kamp 520972f9b20SPoul-Henning Kamp case KERN_PROC_PGRP: 521972f9b20SPoul-Henning Kamp /* could do this by traversing pgrp */ 522972f9b20SPoul-Henning Kamp if (p->p_pgrp == NULL || p->p_pgrp->pg_id != (pid_t)name[1]) 523972f9b20SPoul-Henning Kamp continue; 524972f9b20SPoul-Henning Kamp break; 525972f9b20SPoul-Henning Kamp 526972f9b20SPoul-Henning Kamp case KERN_PROC_TTY: 527972f9b20SPoul-Henning Kamp if ((p->p_flag & P_CONTROLT) == 0 || 528972f9b20SPoul-Henning Kamp p->p_session == NULL || 529972f9b20SPoul-Henning Kamp p->p_session->s_ttyp == NULL || 530972f9b20SPoul-Henning Kamp p->p_session->s_ttyp->t_dev != (dev_t)name[1]) 531972f9b20SPoul-Henning Kamp continue; 532972f9b20SPoul-Henning Kamp break; 533972f9b20SPoul-Henning Kamp 534972f9b20SPoul-Henning Kamp case KERN_PROC_UID: 535972f9b20SPoul-Henning Kamp if (p->p_ucred == NULL || p->p_ucred->cr_uid != (uid_t)name[1]) 536972f9b20SPoul-Henning Kamp continue; 537972f9b20SPoul-Henning Kamp break; 538972f9b20SPoul-Henning Kamp 539972f9b20SPoul-Henning Kamp case KERN_PROC_RUID: 540972f9b20SPoul-Henning Kamp if (p->p_ucred == NULL || p->p_cred->p_ruid != (uid_t)name[1]) 541972f9b20SPoul-Henning Kamp continue; 542972f9b20SPoul-Henning Kamp break; 543972f9b20SPoul-Henning Kamp } 544972f9b20SPoul-Henning Kamp 545972f9b20SPoul-Henning Kamp fill_eproc(p, &eproc); 546972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req,(caddr_t)p, sizeof(struct proc)); 547972f9b20SPoul-Henning Kamp if (error) 548972f9b20SPoul-Henning Kamp return (error); 549972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc)); 550972f9b20SPoul-Henning Kamp if (error) 551972f9b20SPoul-Henning Kamp return (error); 552972f9b20SPoul-Henning Kamp } 553972f9b20SPoul-Henning Kamp if (doingzomb == 0) { 554972f9b20SPoul-Henning Kamp p = zombproc; 555972f9b20SPoul-Henning Kamp doingzomb++; 556972f9b20SPoul-Henning Kamp goto again; 557972f9b20SPoul-Henning Kamp } 558972f9b20SPoul-Henning Kamp return (0); 559972f9b20SPoul-Henning Kamp } 560972f9b20SPoul-Henning Kamp 561972f9b20SPoul-Henning Kamp SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 562972f9b20SPoul-Henning Kamp sysctl_kern_proc, "Process table"); 563