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 * 33b75356e1SJeffrey Hsu * @(#)kern_proc.c 8.7 (Berkeley) 2/14/95 343ce93e4eSPoul-Henning Kamp * $Id: kern_proc.c,v 1.18 1996/05/30 01:21:49 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 6587b6de2bSPoul-Henning Kamp static void pgdelete __P((struct pgrp *)); 6626f9a767SRodney W. Grimes 67df8bae1dSRodney W. Grimes /* 68df8bae1dSRodney W. Grimes * Structure associated with user cacheing. 69df8bae1dSRodney W. Grimes */ 70b75356e1SJeffrey Hsu struct uidinfo { 71b75356e1SJeffrey Hsu LIST_ENTRY(uidinfo) ui_hash; 72df8bae1dSRodney W. Grimes uid_t ui_uid; 73df8bae1dSRodney W. Grimes long ui_proccnt; 74b75356e1SJeffrey Hsu }; 75b75356e1SJeffrey Hsu #define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 76b75356e1SJeffrey Hsu LIST_HEAD(uihashhead, uidinfo) *uihashtbl; 7787b6de2bSPoul-Henning Kamp static u_long uihash; /* size of hash table - 1 */ 78df8bae1dSRodney W. Grimes 7998d93822SBruce Evans static void orphanpg __P((struct pgrp *pg)); 8092ff4bb0SBruce Evans static void pgrpdump __P((void)); 8198d93822SBruce Evans 82df8bae1dSRodney W. Grimes /* 83b75356e1SJeffrey Hsu * Other process lists 84b75356e1SJeffrey Hsu */ 85b75356e1SJeffrey Hsu struct pidhashhead *pidhashtbl; 86b75356e1SJeffrey Hsu u_long pidhash; 87b75356e1SJeffrey Hsu struct pgrphashhead *pgrphashtbl; 88b75356e1SJeffrey Hsu u_long pgrphash; 89b75356e1SJeffrey Hsu struct proclist allproc; 90b75356e1SJeffrey Hsu struct proclist zombproc; 91b75356e1SJeffrey Hsu 92b75356e1SJeffrey Hsu /* 93b75356e1SJeffrey Hsu * Initialize global process hashing structures. 94df8bae1dSRodney W. Grimes */ 9526f9a767SRodney W. Grimes void 96b75356e1SJeffrey Hsu procinit() 97df8bae1dSRodney W. Grimes { 98df8bae1dSRodney W. Grimes 99b75356e1SJeffrey Hsu LIST_INIT(&allproc); 100b75356e1SJeffrey Hsu LIST_INIT(&zombproc); 101b75356e1SJeffrey Hsu pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); 102b75356e1SJeffrey Hsu pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash); 103df8bae1dSRodney W. Grimes uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash); 104df8bae1dSRodney W. Grimes } 105df8bae1dSRodney W. Grimes 106df8bae1dSRodney W. Grimes /* 107df8bae1dSRodney W. Grimes * Change the count associated with number of processes 108df8bae1dSRodney W. Grimes * a given user is using. 109df8bae1dSRodney W. Grimes */ 110df8bae1dSRodney W. Grimes int 111df8bae1dSRodney W. Grimes chgproccnt(uid, diff) 112df8bae1dSRodney W. Grimes uid_t uid; 113df8bae1dSRodney W. Grimes int diff; 114df8bae1dSRodney W. Grimes { 115b75356e1SJeffrey Hsu register struct uidinfo *uip; 116b75356e1SJeffrey Hsu register struct uihashhead *uipp; 117df8bae1dSRodney W. Grimes 118b75356e1SJeffrey Hsu uipp = UIHASH(uid); 119b75356e1SJeffrey Hsu for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next) 120df8bae1dSRodney W. Grimes if (uip->ui_uid == uid) 121df8bae1dSRodney W. Grimes break; 122df8bae1dSRodney W. Grimes if (uip) { 123df8bae1dSRodney W. Grimes uip->ui_proccnt += diff; 124df8bae1dSRodney W. Grimes if (uip->ui_proccnt > 0) 125df8bae1dSRodney W. Grimes return (uip->ui_proccnt); 126df8bae1dSRodney W. Grimes if (uip->ui_proccnt < 0) 127df8bae1dSRodney W. Grimes panic("chgproccnt: procs < 0"); 128b75356e1SJeffrey Hsu LIST_REMOVE(uip, ui_hash); 129df8bae1dSRodney W. Grimes FREE(uip, M_PROC); 130df8bae1dSRodney W. Grimes return (0); 131df8bae1dSRodney W. Grimes } 132df8bae1dSRodney W. Grimes if (diff <= 0) { 133df8bae1dSRodney W. Grimes if (diff == 0) 134df8bae1dSRodney W. Grimes return(0); 135df8bae1dSRodney W. Grimes panic("chgproccnt: lost user"); 136df8bae1dSRodney W. Grimes } 137df8bae1dSRodney W. Grimes MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK); 138b75356e1SJeffrey Hsu LIST_INSERT_HEAD(uipp, uip, ui_hash); 139df8bae1dSRodney W. Grimes uip->ui_uid = uid; 140df8bae1dSRodney W. Grimes uip->ui_proccnt = diff; 141df8bae1dSRodney W. Grimes return (diff); 142df8bae1dSRodney W. Grimes } 143df8bae1dSRodney W. Grimes 144df8bae1dSRodney W. Grimes /* 145df8bae1dSRodney W. Grimes * Is p an inferior of the current process? 146df8bae1dSRodney W. Grimes */ 14726f9a767SRodney W. Grimes int 148df8bae1dSRodney W. Grimes inferior(p) 149df8bae1dSRodney W. Grimes register struct proc *p; 150df8bae1dSRodney W. Grimes { 151df8bae1dSRodney W. Grimes 152df8bae1dSRodney W. Grimes for (; p != curproc; p = p->p_pptr) 153df8bae1dSRodney W. Grimes if (p->p_pid == 0) 154df8bae1dSRodney W. Grimes return (0); 155df8bae1dSRodney W. Grimes return (1); 156df8bae1dSRodney W. Grimes } 157df8bae1dSRodney W. Grimes 158df8bae1dSRodney W. Grimes /* 159df8bae1dSRodney W. Grimes * Locate a process by number 160df8bae1dSRodney W. Grimes */ 161df8bae1dSRodney W. Grimes struct proc * 162df8bae1dSRodney W. Grimes pfind(pid) 163df8bae1dSRodney W. Grimes register pid_t pid; 164df8bae1dSRodney W. Grimes { 165df8bae1dSRodney W. Grimes register struct proc *p; 166df8bae1dSRodney W. Grimes 167b75356e1SJeffrey Hsu for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next) 168df8bae1dSRodney W. Grimes if (p->p_pid == pid) 169df8bae1dSRodney W. Grimes return (p); 170df8bae1dSRodney W. Grimes return (NULL); 171df8bae1dSRodney W. Grimes } 172df8bae1dSRodney W. Grimes 173df8bae1dSRodney W. Grimes /* 174df8bae1dSRodney W. Grimes * Locate a process group by number 175df8bae1dSRodney W. Grimes */ 176df8bae1dSRodney W. Grimes struct pgrp * 177df8bae1dSRodney W. Grimes pgfind(pgid) 178df8bae1dSRodney W. Grimes register pid_t pgid; 179df8bae1dSRodney W. Grimes { 180df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 181df8bae1dSRodney W. Grimes 182b75356e1SJeffrey Hsu for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; 183b75356e1SJeffrey Hsu pgrp = pgrp->pg_hash.le_next) 184df8bae1dSRodney W. Grimes if (pgrp->pg_id == pgid) 185df8bae1dSRodney W. Grimes return (pgrp); 186df8bae1dSRodney W. Grimes return (NULL); 187df8bae1dSRodney W. Grimes } 188df8bae1dSRodney W. Grimes 189df8bae1dSRodney W. Grimes /* 190df8bae1dSRodney W. Grimes * Move p to a new or existing process group (and session) 191df8bae1dSRodney W. Grimes */ 19226f9a767SRodney W. Grimes int 193df8bae1dSRodney W. Grimes enterpgrp(p, pgid, mksess) 194df8bae1dSRodney W. Grimes register struct proc *p; 195df8bae1dSRodney W. Grimes pid_t pgid; 196df8bae1dSRodney W. Grimes int mksess; 197df8bae1dSRodney W. Grimes { 198df8bae1dSRodney W. Grimes register struct pgrp *pgrp = pgfind(pgid); 199df8bae1dSRodney W. Grimes 200df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 201df8bae1dSRodney W. Grimes if (pgrp != NULL && mksess) /* firewalls */ 202df8bae1dSRodney W. Grimes panic("enterpgrp: setsid into non-empty pgrp"); 203df8bae1dSRodney W. Grimes if (SESS_LEADER(p)) 204df8bae1dSRodney W. Grimes panic("enterpgrp: session leader attempted setpgrp"); 205df8bae1dSRodney W. Grimes #endif 206df8bae1dSRodney W. Grimes if (pgrp == NULL) { 207df8bae1dSRodney W. Grimes pid_t savepid = p->p_pid; 208df8bae1dSRodney W. Grimes struct proc *np; 209df8bae1dSRodney W. Grimes /* 210df8bae1dSRodney W. Grimes * new process group 211df8bae1dSRodney W. Grimes */ 212df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 213df8bae1dSRodney W. Grimes if (p->p_pid != pgid) 214df8bae1dSRodney W. Grimes panic("enterpgrp: new pgrp and pid != pgid"); 215df8bae1dSRodney W. Grimes #endif 216df8bae1dSRodney W. Grimes MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP, 217df8bae1dSRodney W. Grimes M_WAITOK); 218df8bae1dSRodney W. Grimes if ((np = pfind(savepid)) == NULL || np != p) 219df8bae1dSRodney W. Grimes return (ESRCH); 220df8bae1dSRodney W. Grimes if (mksess) { 221df8bae1dSRodney W. Grimes register struct session *sess; 222df8bae1dSRodney W. Grimes 223df8bae1dSRodney W. Grimes /* 224df8bae1dSRodney W. Grimes * new session 225df8bae1dSRodney W. Grimes */ 226df8bae1dSRodney W. Grimes MALLOC(sess, struct session *, sizeof(struct session), 227df8bae1dSRodney W. Grimes M_SESSION, M_WAITOK); 228df8bae1dSRodney W. Grimes sess->s_leader = p; 229df8bae1dSRodney W. Grimes sess->s_count = 1; 230df8bae1dSRodney W. Grimes sess->s_ttyvp = NULL; 231df8bae1dSRodney W. Grimes sess->s_ttyp = NULL; 232df8bae1dSRodney W. Grimes bcopy(p->p_session->s_login, sess->s_login, 233df8bae1dSRodney W. Grimes sizeof(sess->s_login)); 234df8bae1dSRodney W. Grimes p->p_flag &= ~P_CONTROLT; 235df8bae1dSRodney W. Grimes pgrp->pg_session = sess; 236df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC 237df8bae1dSRodney W. Grimes if (p != curproc) 238df8bae1dSRodney W. Grimes panic("enterpgrp: mksession and p != curproc"); 239df8bae1dSRodney W. Grimes #endif 240df8bae1dSRodney W. Grimes } else { 241df8bae1dSRodney W. Grimes pgrp->pg_session = p->p_session; 242df8bae1dSRodney W. Grimes pgrp->pg_session->s_count++; 243df8bae1dSRodney W. Grimes } 244df8bae1dSRodney W. Grimes pgrp->pg_id = pgid; 245b75356e1SJeffrey Hsu LIST_INIT(&pgrp->pg_members); 246b75356e1SJeffrey Hsu LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash); 247df8bae1dSRodney W. Grimes pgrp->pg_jobc = 0; 248df8bae1dSRodney W. Grimes } else if (pgrp == p->p_pgrp) 249df8bae1dSRodney W. Grimes return (0); 250df8bae1dSRodney W. Grimes 251df8bae1dSRodney W. Grimes /* 252df8bae1dSRodney W. Grimes * Adjust eligibility of affected pgrps to participate in job control. 253df8bae1dSRodney W. Grimes * Increment eligibility counts before decrementing, otherwise we 254df8bae1dSRodney W. Grimes * could reach 0 spuriously during the first call. 255df8bae1dSRodney W. Grimes */ 256df8bae1dSRodney W. Grimes fixjobc(p, pgrp, 1); 257df8bae1dSRodney W. Grimes fixjobc(p, p->p_pgrp, 0); 258df8bae1dSRodney W. Grimes 259b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist); 260b75356e1SJeffrey Hsu if (p->p_pgrp->pg_members.lh_first == 0) 261df8bae1dSRodney W. Grimes pgdelete(p->p_pgrp); 262df8bae1dSRodney W. Grimes p->p_pgrp = pgrp; 263b75356e1SJeffrey Hsu LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist); 264df8bae1dSRodney W. Grimes return (0); 265df8bae1dSRodney W. Grimes } 266df8bae1dSRodney W. Grimes 267df8bae1dSRodney W. Grimes /* 268df8bae1dSRodney W. Grimes * remove process from process group 269df8bae1dSRodney W. Grimes */ 27026f9a767SRodney W. Grimes int 271df8bae1dSRodney W. Grimes leavepgrp(p) 272df8bae1dSRodney W. Grimes register struct proc *p; 273df8bae1dSRodney W. Grimes { 274df8bae1dSRodney W. Grimes 275b75356e1SJeffrey Hsu LIST_REMOVE(p, p_pglist); 276b75356e1SJeffrey Hsu if (p->p_pgrp->pg_members.lh_first == 0) 277df8bae1dSRodney W. Grimes pgdelete(p->p_pgrp); 278df8bae1dSRodney W. Grimes p->p_pgrp = 0; 279df8bae1dSRodney W. Grimes return (0); 280df8bae1dSRodney W. Grimes } 281df8bae1dSRodney W. Grimes 282df8bae1dSRodney W. Grimes /* 283df8bae1dSRodney W. Grimes * delete a process group 284df8bae1dSRodney W. Grimes */ 28587b6de2bSPoul-Henning Kamp static void 286df8bae1dSRodney W. Grimes pgdelete(pgrp) 287df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 288df8bae1dSRodney W. Grimes { 289df8bae1dSRodney W. Grimes 290df8bae1dSRodney W. Grimes if (pgrp->pg_session->s_ttyp != NULL && 291df8bae1dSRodney W. Grimes pgrp->pg_session->s_ttyp->t_pgrp == pgrp) 292df8bae1dSRodney W. Grimes pgrp->pg_session->s_ttyp->t_pgrp = NULL; 293b75356e1SJeffrey Hsu LIST_REMOVE(pgrp, pg_hash); 294df8bae1dSRodney W. Grimes if (--pgrp->pg_session->s_count == 0) 295df8bae1dSRodney W. Grimes FREE(pgrp->pg_session, M_SESSION); 296df8bae1dSRodney W. Grimes FREE(pgrp, M_PGRP); 297df8bae1dSRodney W. Grimes } 298df8bae1dSRodney W. Grimes 299df8bae1dSRodney W. Grimes /* 300df8bae1dSRodney W. Grimes * Adjust pgrp jobc counters when specified process changes process group. 301df8bae1dSRodney W. Grimes * We count the number of processes in each process group that "qualify" 302df8bae1dSRodney W. Grimes * the group for terminal job control (those with a parent in a different 303df8bae1dSRodney W. Grimes * process group of the same session). If that count reaches zero, the 304df8bae1dSRodney W. Grimes * process group becomes orphaned. Check both the specified process' 305df8bae1dSRodney W. Grimes * process group and that of its children. 306df8bae1dSRodney W. Grimes * entering == 0 => p is leaving specified group. 307df8bae1dSRodney W. Grimes * entering == 1 => p is entering specified group. 308df8bae1dSRodney W. Grimes */ 30926f9a767SRodney W. Grimes void 310df8bae1dSRodney W. Grimes fixjobc(p, pgrp, entering) 311df8bae1dSRodney W. Grimes register struct proc *p; 312df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 313df8bae1dSRodney W. Grimes int entering; 314df8bae1dSRodney W. Grimes { 315df8bae1dSRodney W. Grimes register struct pgrp *hispgrp; 316df8bae1dSRodney W. Grimes register struct session *mysession = pgrp->pg_session; 317df8bae1dSRodney W. Grimes 318df8bae1dSRodney W. Grimes /* 319df8bae1dSRodney W. Grimes * Check p's parent to see whether p qualifies its own process 320df8bae1dSRodney W. Grimes * group; if so, adjust count for p's process group. 321df8bae1dSRodney W. Grimes */ 322df8bae1dSRodney W. Grimes if ((hispgrp = p->p_pptr->p_pgrp) != pgrp && 323df8bae1dSRodney W. Grimes hispgrp->pg_session == mysession) 324df8bae1dSRodney W. Grimes if (entering) 325df8bae1dSRodney W. Grimes pgrp->pg_jobc++; 326df8bae1dSRodney W. Grimes else if (--pgrp->pg_jobc == 0) 327df8bae1dSRodney W. Grimes orphanpg(pgrp); 328df8bae1dSRodney W. Grimes 329df8bae1dSRodney W. Grimes /* 330df8bae1dSRodney W. Grimes * Check this process' children to see whether they qualify 331df8bae1dSRodney W. Grimes * their process groups; if so, adjust counts for children's 332df8bae1dSRodney W. Grimes * process groups. 333df8bae1dSRodney W. Grimes */ 334b75356e1SJeffrey Hsu for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) 335df8bae1dSRodney W. Grimes if ((hispgrp = p->p_pgrp) != pgrp && 336df8bae1dSRodney W. Grimes hispgrp->pg_session == mysession && 337df8bae1dSRodney W. Grimes p->p_stat != SZOMB) 338df8bae1dSRodney W. Grimes if (entering) 339df8bae1dSRodney W. Grimes hispgrp->pg_jobc++; 340df8bae1dSRodney W. Grimes else if (--hispgrp->pg_jobc == 0) 341df8bae1dSRodney W. Grimes orphanpg(hispgrp); 342df8bae1dSRodney W. Grimes } 343df8bae1dSRodney W. Grimes 344df8bae1dSRodney W. Grimes /* 345df8bae1dSRodney W. Grimes * A process group has become orphaned; 346df8bae1dSRodney W. Grimes * if there are any stopped processes in the group, 347df8bae1dSRodney W. Grimes * hang-up all process in that group. 348df8bae1dSRodney W. Grimes */ 349df8bae1dSRodney W. Grimes static void 350df8bae1dSRodney W. Grimes orphanpg(pg) 351df8bae1dSRodney W. Grimes struct pgrp *pg; 352df8bae1dSRodney W. Grimes { 353df8bae1dSRodney W. Grimes register struct proc *p; 354df8bae1dSRodney W. Grimes 355b75356e1SJeffrey Hsu for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) { 356df8bae1dSRodney W. Grimes if (p->p_stat == SSTOP) { 357b75356e1SJeffrey Hsu for (p = pg->pg_members.lh_first; p != 0; 358b75356e1SJeffrey Hsu p = p->p_pglist.le_next) { 359df8bae1dSRodney W. Grimes psignal(p, SIGHUP); 360df8bae1dSRodney W. Grimes psignal(p, SIGCONT); 361df8bae1dSRodney W. Grimes } 362df8bae1dSRodney W. Grimes return; 363df8bae1dSRodney W. Grimes } 364df8bae1dSRodney W. Grimes } 365df8bae1dSRodney W. Grimes } 366df8bae1dSRodney W. Grimes 367b75356e1SJeffrey Hsu #ifdef DEBUG 36892ff4bb0SBruce Evans static void 369df8bae1dSRodney W. Grimes pgrpdump() 370df8bae1dSRodney W. Grimes { 371df8bae1dSRodney W. Grimes register struct pgrp *pgrp; 372df8bae1dSRodney W. Grimes register struct proc *p; 373df8bae1dSRodney W. Grimes register i; 374df8bae1dSRodney W. Grimes 375b75356e1SJeffrey Hsu for (i = 0; i <= pgrphash; i++) { 376b75356e1SJeffrey Hsu if (pgrp = pgrphashtbl[i].lh_first) { 377df8bae1dSRodney W. Grimes printf("\tindx %d\n", i); 378b75356e1SJeffrey Hsu for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) { 379df8bae1dSRodney W. Grimes printf("\tpgrp %x, pgid %d, sess %x, sesscnt %d, mem %x\n", 380df8bae1dSRodney W. Grimes pgrp, pgrp->pg_id, pgrp->pg_session, 381b75356e1SJeffrey Hsu pgrp->pg_session->s_count, 382b75356e1SJeffrey Hsu pgrp->pg_members.lh_first); 383b75356e1SJeffrey Hsu for (p = pgrp->pg_members.lh_first; p != 0; 384b75356e1SJeffrey Hsu p = p->p_pglist.le_next) { 385df8bae1dSRodney W. Grimes printf("\t\tpid %d addr %x pgrp %x\n", 386df8bae1dSRodney W. Grimes p->p_pid, p, p->p_pgrp); 387df8bae1dSRodney W. Grimes } 388df8bae1dSRodney W. Grimes } 389df8bae1dSRodney W. Grimes } 390df8bae1dSRodney W. Grimes } 391df8bae1dSRodney W. Grimes } 392b75356e1SJeffrey Hsu #endif /* DEBUG */ 393972f9b20SPoul-Henning Kamp 394972f9b20SPoul-Henning Kamp /* 395972f9b20SPoul-Henning Kamp * Fill in an eproc structure for the specified process. 396972f9b20SPoul-Henning Kamp */ 397972f9b20SPoul-Henning Kamp void 398972f9b20SPoul-Henning Kamp fill_eproc(p, ep) 399972f9b20SPoul-Henning Kamp register struct proc *p; 400972f9b20SPoul-Henning Kamp register struct eproc *ep; 401972f9b20SPoul-Henning Kamp { 402972f9b20SPoul-Henning Kamp register struct tty *tp; 403972f9b20SPoul-Henning Kamp 404972f9b20SPoul-Henning Kamp bzero(ep, sizeof(*ep)); 405972f9b20SPoul-Henning Kamp 406972f9b20SPoul-Henning Kamp ep->e_paddr = p; 407972f9b20SPoul-Henning Kamp if (p->p_cred) { 408972f9b20SPoul-Henning Kamp ep->e_pcred = *p->p_cred; 409972f9b20SPoul-Henning Kamp if (p->p_ucred) 410972f9b20SPoul-Henning Kamp ep->e_ucred = *p->p_ucred; 411972f9b20SPoul-Henning Kamp } 412972f9b20SPoul-Henning Kamp if (p->p_stat != SIDL && p->p_stat != SZOMB && p->p_vmspace != NULL) { 413972f9b20SPoul-Henning Kamp register struct vmspace *vm = p->p_vmspace; 414972f9b20SPoul-Henning Kamp 415972f9b20SPoul-Henning Kamp #ifdef pmap_resident_count 416972f9b20SPoul-Henning Kamp ep->e_vm.vm_rssize = pmap_resident_count(&vm->vm_pmap); /*XXX*/ 417972f9b20SPoul-Henning Kamp #else 418972f9b20SPoul-Henning Kamp ep->e_vm.vm_rssize = vm->vm_rssize; 419972f9b20SPoul-Henning Kamp #endif 420972f9b20SPoul-Henning Kamp ep->e_vm.vm_tsize = vm->vm_tsize; 421972f9b20SPoul-Henning Kamp ep->e_vm.vm_dsize = vm->vm_dsize; 422972f9b20SPoul-Henning Kamp ep->e_vm.vm_ssize = vm->vm_ssize; 423972f9b20SPoul-Henning Kamp #ifndef sparc 424972f9b20SPoul-Henning Kamp ep->e_vm.vm_pmap = vm->vm_pmap; 425972f9b20SPoul-Henning Kamp #endif 426972f9b20SPoul-Henning Kamp } 427972f9b20SPoul-Henning Kamp if (p->p_pptr) 428972f9b20SPoul-Henning Kamp ep->e_ppid = p->p_pptr->p_pid; 429972f9b20SPoul-Henning Kamp if (p->p_pgrp) { 430972f9b20SPoul-Henning Kamp ep->e_pgid = p->p_pgrp->pg_id; 431972f9b20SPoul-Henning Kamp ep->e_jobc = p->p_pgrp->pg_jobc; 432cd73303cSDavid Greenman ep->e_sess = p->p_pgrp->pg_session; 433cd73303cSDavid Greenman 434cd73303cSDavid Greenman if (ep->e_sess) { 4358735cebcSPeter Wemm bcopy(ep->e_sess->s_login, ep->e_login, sizeof(ep->e_login)); 436cd73303cSDavid Greenman if (ep->e_sess->s_ttyvp) 437cd73303cSDavid Greenman ep->e_flag = EPROC_CTTY; 438cd73303cSDavid Greenman if (p->p_session && SESS_LEADER(p)) 439cd73303cSDavid Greenman ep->e_flag |= EPROC_SLEADER; 440cd73303cSDavid Greenman } 441cd73303cSDavid Greenman } 442972f9b20SPoul-Henning Kamp if ((p->p_flag & P_CONTROLT) && 443972f9b20SPoul-Henning Kamp (ep->e_sess != NULL) && 444972f9b20SPoul-Henning Kamp ((tp = ep->e_sess->s_ttyp) != NULL)) { 445972f9b20SPoul-Henning Kamp ep->e_tdev = tp->t_dev; 446972f9b20SPoul-Henning Kamp ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 447972f9b20SPoul-Henning Kamp ep->e_tsess = tp->t_session; 448972f9b20SPoul-Henning Kamp } else 449972f9b20SPoul-Henning Kamp ep->e_tdev = NODEV; 450972f9b20SPoul-Henning Kamp if (p->p_wmesg) { 451972f9b20SPoul-Henning Kamp strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN); 452972f9b20SPoul-Henning Kamp ep->e_wmesg[WMESGLEN] = 0; 453972f9b20SPoul-Henning Kamp } 454972f9b20SPoul-Henning Kamp } 455972f9b20SPoul-Henning Kamp 4563ce93e4eSPoul-Henning Kamp static struct proc * 4573ce93e4eSPoul-Henning Kamp zpfind(pid_t pid) 4583ce93e4eSPoul-Henning Kamp { 4593ce93e4eSPoul-Henning Kamp struct proc *p; 4603ce93e4eSPoul-Henning Kamp 4613ce93e4eSPoul-Henning Kamp for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next) 4623ce93e4eSPoul-Henning Kamp if (p->p_pid == pid) 4633ce93e4eSPoul-Henning Kamp return (p); 4643ce93e4eSPoul-Henning Kamp return (NULL); 4653ce93e4eSPoul-Henning Kamp } 4663ce93e4eSPoul-Henning Kamp 4673ce93e4eSPoul-Henning Kamp 4683ce93e4eSPoul-Henning Kamp static int 4693ce93e4eSPoul-Henning Kamp sysctl_out_proc(struct proc *p, struct sysctl_req *req, int doingzomb) 4703ce93e4eSPoul-Henning Kamp { 4713ce93e4eSPoul-Henning Kamp struct eproc eproc; 4723ce93e4eSPoul-Henning Kamp int error; 4733ce93e4eSPoul-Henning Kamp pid_t pid = p->p_pid; 4743ce93e4eSPoul-Henning Kamp 4753ce93e4eSPoul-Henning Kamp fill_eproc(p, &eproc); 4763ce93e4eSPoul-Henning Kamp error = SYSCTL_OUT(req,(caddr_t)p, sizeof(struct proc)); 4773ce93e4eSPoul-Henning Kamp if (error) 4783ce93e4eSPoul-Henning Kamp return (error); 4793ce93e4eSPoul-Henning Kamp error = SYSCTL_OUT(req,(caddr_t)&eproc, sizeof(eproc)); 4803ce93e4eSPoul-Henning Kamp if (error) 4813ce93e4eSPoul-Henning Kamp return (error); 4823ce93e4eSPoul-Henning Kamp if (!doingzomb && pid && (pfind(pid) != p)) 4833ce93e4eSPoul-Henning Kamp return EAGAIN; 4843ce93e4eSPoul-Henning Kamp if (doingzomb && zpfind(pid) != p) 4853ce93e4eSPoul-Henning Kamp return EAGAIN; 4863ce93e4eSPoul-Henning Kamp return (0); 4873ce93e4eSPoul-Henning Kamp } 4883ce93e4eSPoul-Henning Kamp 489972f9b20SPoul-Henning Kamp static int 490972f9b20SPoul-Henning Kamp sysctl_kern_proc SYSCTL_HANDLER_ARGS 491972f9b20SPoul-Henning Kamp { 492972f9b20SPoul-Henning Kamp int *name = (int*) arg1; 493972f9b20SPoul-Henning Kamp u_int namelen = arg2; 494972f9b20SPoul-Henning Kamp struct proc *p; 495972f9b20SPoul-Henning Kamp int doingzomb; 496972f9b20SPoul-Henning Kamp int error = 0; 497972f9b20SPoul-Henning Kamp 4983ce93e4eSPoul-Henning Kamp if (oidp->oid_number == KERN_PROC_PID) { 4993ce93e4eSPoul-Henning Kamp if (namelen != 1) 500972f9b20SPoul-Henning Kamp return (EINVAL); 5013ce93e4eSPoul-Henning Kamp p = pfind((pid_t)name[0]); 5023ce93e4eSPoul-Henning Kamp if (!p) 5033ce93e4eSPoul-Henning Kamp return (0); 5043ce93e4eSPoul-Henning Kamp error = sysctl_out_proc(p, req, 0); 5053ce93e4eSPoul-Henning Kamp return (error); 5063ce93e4eSPoul-Henning Kamp } 5073ce93e4eSPoul-Henning Kamp if (oidp->oid_number == KERN_PROC_ALL && !namelen) 5083ce93e4eSPoul-Henning Kamp ; 5093ce93e4eSPoul-Henning Kamp else if (oidp->oid_number != KERN_PROC_ALL && namelen == 1) 5103ce93e4eSPoul-Henning Kamp ; 5113ce93e4eSPoul-Henning Kamp else 5123ce93e4eSPoul-Henning Kamp return (EINVAL); 5133ce93e4eSPoul-Henning Kamp 514972f9b20SPoul-Henning Kamp if (!req->oldptr) { 5153ce93e4eSPoul-Henning Kamp /* overestimate by 5 procs */ 516972f9b20SPoul-Henning Kamp error = SYSCTL_OUT(req, 0, sizeof (struct kinfo_proc) * 5); 517972f9b20SPoul-Henning Kamp if (error) 518972f9b20SPoul-Henning Kamp return (error); 519972f9b20SPoul-Henning Kamp } 5203ce93e4eSPoul-Henning Kamp for (doingzomb=0 ; doingzomb < 2 ; doingzomb++) { 5213ce93e4eSPoul-Henning Kamp if (!doingzomb) 522b75356e1SJeffrey Hsu p = allproc.lh_first; 5233ce93e4eSPoul-Henning Kamp else 5243ce93e4eSPoul-Henning Kamp p = zombproc.lh_first; 525b75356e1SJeffrey Hsu for (; p != 0; p = p->p_list.le_next) { 526972f9b20SPoul-Henning Kamp /* 527972f9b20SPoul-Henning Kamp * Skip embryonic processes. 528972f9b20SPoul-Henning Kamp */ 529972f9b20SPoul-Henning Kamp if (p->p_stat == SIDL) 530972f9b20SPoul-Henning Kamp continue; 531972f9b20SPoul-Henning Kamp /* 532972f9b20SPoul-Henning Kamp * TODO - make more efficient (see notes below). 533972f9b20SPoul-Henning Kamp * do by session. 534972f9b20SPoul-Henning Kamp */ 5353ce93e4eSPoul-Henning Kamp switch (oidp->oid_number) { 536972f9b20SPoul-Henning Kamp 537972f9b20SPoul-Henning Kamp case KERN_PROC_PGRP: 538972f9b20SPoul-Henning Kamp /* could do this by traversing pgrp */ 5393ce93e4eSPoul-Henning Kamp if (p->p_pgrp == NULL || 5403ce93e4eSPoul-Henning Kamp p->p_pgrp->pg_id != (pid_t)name[0]) 541972f9b20SPoul-Henning Kamp continue; 542972f9b20SPoul-Henning Kamp break; 543972f9b20SPoul-Henning Kamp 544972f9b20SPoul-Henning Kamp case KERN_PROC_TTY: 545972f9b20SPoul-Henning Kamp if ((p->p_flag & P_CONTROLT) == 0 || 546972f9b20SPoul-Henning Kamp p->p_session == NULL || 547972f9b20SPoul-Henning Kamp p->p_session->s_ttyp == NULL || 5483ce93e4eSPoul-Henning Kamp p->p_session->s_ttyp->t_dev != (dev_t)name[0]) 549972f9b20SPoul-Henning Kamp continue; 550972f9b20SPoul-Henning Kamp break; 551972f9b20SPoul-Henning Kamp 552972f9b20SPoul-Henning Kamp case KERN_PROC_UID: 5533ce93e4eSPoul-Henning Kamp if (p->p_ucred == NULL || 5543ce93e4eSPoul-Henning Kamp p->p_ucred->cr_uid != (uid_t)name[0]) 555972f9b20SPoul-Henning Kamp continue; 556972f9b20SPoul-Henning Kamp break; 557972f9b20SPoul-Henning Kamp 558972f9b20SPoul-Henning Kamp case KERN_PROC_RUID: 5593ce93e4eSPoul-Henning Kamp if (p->p_ucred == NULL || 5603ce93e4eSPoul-Henning Kamp p->p_cred->p_ruid != (uid_t)name[0]) 561972f9b20SPoul-Henning Kamp continue; 562972f9b20SPoul-Henning Kamp break; 563972f9b20SPoul-Henning Kamp } 564972f9b20SPoul-Henning Kamp 5653ce93e4eSPoul-Henning Kamp error = sysctl_out_proc(p, req, doingzomb); 566972f9b20SPoul-Henning Kamp if (error) 567972f9b20SPoul-Henning Kamp return (error); 568972f9b20SPoul-Henning Kamp } 569972f9b20SPoul-Henning Kamp } 570972f9b20SPoul-Henning Kamp return (0); 571972f9b20SPoul-Henning Kamp } 572972f9b20SPoul-Henning Kamp 5733ce93e4eSPoul-Henning Kamp 5743ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD, 0, "Process table"); 5753ce93e4eSPoul-Henning Kamp 5763ce93e4eSPoul-Henning Kamp SYSCTL_PROC(_kern_proc, KERN_PROC_ALL, all, CTLFLAG_RD|CTLTYPE_STRUCT, 5773ce93e4eSPoul-Henning Kamp 0, 0, sysctl_kern_proc, "S,proc", ""); 5783ce93e4eSPoul-Henning Kamp 5793ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_PGRP, pgrp, CTLFLAG_RD, 5803ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 5813ce93e4eSPoul-Henning Kamp 5823ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_TTY, tty, CTLFLAG_RD, 5833ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 5843ce93e4eSPoul-Henning Kamp 5853ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_UID, uid, CTLFLAG_RD, 5863ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 5873ce93e4eSPoul-Henning Kamp 5883ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_RUID, ruid, CTLFLAG_RD, 5893ce93e4eSPoul-Henning Kamp sysctl_kern_proc, "Process table"); 5903ce93e4eSPoul-Henning Kamp 5913ce93e4eSPoul-Henning Kamp SYSCTL_NODE(_kern_proc, KERN_PROC_PID, pid, CTLFLAG_RD, 592972f9b20SPoul-Henning Kamp sysctl_kern_proc, "Process table"); 593