Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.13
      1 /*	$NetBSD: kern_proc.c,v 1.13 1996/02/04 02:15:55 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)kern_proc.c	8.4 (Berkeley) 1/4/94
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/map.h>
     41 #include <sys/kernel.h>
     42 #include <sys/proc.h>
     43 #include <sys/buf.h>
     44 #include <sys/acct.h>
     45 #include <sys/wait.h>
     46 #include <sys/file.h>
     47 #include <ufs/ufs/quota.h>
     48 #include <sys/uio.h>
     49 #include <sys/malloc.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/ioctl.h>
     52 #include <sys/tty.h>
     53 #include <sys/signalvar.h>
     54 
     55 /*
     56  * Structure associated with user cacheing.
     57  */
     58 struct uidinfo {
     59 	LIST_ENTRY(uidinfo) ui_hash;
     60 	uid_t	ui_uid;
     61 	long	ui_proccnt;
     62 };
     63 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
     64 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
     65 u_long uihash;		/* size of hash table - 1 */
     66 
     67 /*
     68  * Other process lists
     69  */
     70 struct pidhashhead *pidhashtbl;
     71 u_long pidhash;
     72 struct pgrphashhead *pgrphashtbl;
     73 u_long pgrphash;
     74 struct proclist allproc;
     75 struct proclist zombproc;
     76 
     77 static void orphanpg __P((struct pgrp *));
     78 
     79 /*
     80  * Initialize global process hashing structures.
     81  */
     82 void
     83 procinit()
     84 {
     85 
     86 	LIST_INIT(&allproc);
     87 	LIST_INIT(&zombproc);
     88 	pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
     89 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
     90 	uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
     91 }
     92 
     93 /*
     94  * Change the count associated with number of processes
     95  * a given user is using.
     96  */
     97 int
     98 chgproccnt(uid, diff)
     99 	uid_t	uid;
    100 	int	diff;
    101 {
    102 	register struct uidinfo *uip;
    103 	register struct uihashhead *uipp;
    104 
    105 	uipp = UIHASH(uid);
    106 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
    107 		if (uip->ui_uid == uid)
    108 			break;
    109 	if (uip) {
    110 		uip->ui_proccnt += diff;
    111 		if (uip->ui_proccnt > 0)
    112 			return (uip->ui_proccnt);
    113 		if (uip->ui_proccnt < 0)
    114 			panic("chgproccnt: procs < 0");
    115 		LIST_REMOVE(uip, ui_hash);
    116 		FREE(uip, M_PROC);
    117 		return (0);
    118 	}
    119 	if (diff <= 0) {
    120 		if (diff == 0)
    121 			return(0);
    122 		panic("chgproccnt: lost user");
    123 	}
    124 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    125 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    126 	uip->ui_uid = uid;
    127 	uip->ui_proccnt = diff;
    128 	return (diff);
    129 }
    130 
    131 /*
    132  * Is p an inferior of the current process?
    133  */
    134 int
    135 inferior(p)
    136 	register struct proc *p;
    137 {
    138 
    139 	for (; p != curproc; p = p->p_pptr)
    140 		if (p->p_pid == 0)
    141 			return (0);
    142 	return (1);
    143 }
    144 
    145 /*
    146  * Locate a process by number
    147  */
    148 struct proc *
    149 pfind(pid)
    150 	register pid_t pid;
    151 {
    152 	register struct proc *p;
    153 
    154 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
    155 		if (p->p_pid == pid)
    156 			return (p);
    157 	return (NULL);
    158 }
    159 
    160 /*
    161  * Locate a process group by number
    162  */
    163 struct pgrp *
    164 pgfind(pgid)
    165 	register pid_t pgid;
    166 {
    167 	register struct pgrp *pgrp;
    168 
    169 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
    170 		if (pgrp->pg_id == pgid)
    171 			return (pgrp);
    172 	return (NULL);
    173 }
    174 
    175 /*
    176  * Move p to a new or existing process group (and session)
    177  */
    178 int
    179 enterpgrp(p, pgid, mksess)
    180 	register struct proc *p;
    181 	pid_t pgid;
    182 	int mksess;
    183 {
    184 	register struct pgrp *pgrp = pgfind(pgid);
    185 
    186 #ifdef DIAGNOSTIC
    187 	if (pgrp != NULL && mksess)	/* firewalls */
    188 		panic("enterpgrp: setsid into non-empty pgrp");
    189 	if (SESS_LEADER(p))
    190 		panic("enterpgrp: session leader attempted setpgrp");
    191 #endif
    192 	if (pgrp == NULL) {
    193 		pid_t savepid = p->p_pid;
    194 		struct proc *np;
    195 		/*
    196 		 * new process group
    197 		 */
    198 #ifdef DIAGNOSTIC
    199 		if (p->p_pid != pgid)
    200 			panic("enterpgrp: new pgrp and pid != pgid");
    201 #endif
    202 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
    203 		    M_WAITOK);
    204 		if ((np = pfind(savepid)) == NULL || np != p)
    205 			return (ESRCH);
    206 		if (mksess) {
    207 			register struct session *sess;
    208 
    209 			/*
    210 			 * new session
    211 			 */
    212 			MALLOC(sess, struct session *, sizeof(struct session),
    213 			    M_SESSION, M_WAITOK);
    214 			sess->s_leader = p;
    215 			sess->s_count = 1;
    216 			sess->s_ttyvp = NULL;
    217 			sess->s_ttyp = NULL;
    218 			bcopy(p->p_session->s_login, sess->s_login,
    219 			    sizeof(sess->s_login));
    220 			p->p_flag &= ~P_CONTROLT;
    221 			pgrp->pg_session = sess;
    222 #ifdef DIAGNOSTIC
    223 			if (p != curproc)
    224 				panic("enterpgrp: mksession and p != curproc");
    225 #endif
    226 		} else {
    227 			pgrp->pg_session = p->p_session;
    228 			pgrp->pg_session->s_count++;
    229 		}
    230 		pgrp->pg_id = pgid;
    231 		LIST_INIT(&pgrp->pg_members);
    232 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
    233 		pgrp->pg_jobc = 0;
    234 	} else if (pgrp == p->p_pgrp)
    235 		return (0);
    236 
    237 	/*
    238 	 * Adjust eligibility of affected pgrps to participate in job control.
    239 	 * Increment eligibility counts before decrementing, otherwise we
    240 	 * could reach 0 spuriously during the first call.
    241 	 */
    242 	fixjobc(p, pgrp, 1);
    243 	fixjobc(p, p->p_pgrp, 0);
    244 
    245 	LIST_REMOVE(p, p_pglist);
    246 	if (p->p_pgrp->pg_members.lh_first == 0)
    247 		pgdelete(p->p_pgrp);
    248 	p->p_pgrp = pgrp;
    249 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    250 	return (0);
    251 }
    252 
    253 /*
    254  * remove process from process group
    255  */
    256 int
    257 leavepgrp(p)
    258 	register struct proc *p;
    259 {
    260 
    261 	LIST_REMOVE(p, p_pglist);
    262 	if (p->p_pgrp->pg_members.lh_first == 0)
    263 		pgdelete(p->p_pgrp);
    264 	p->p_pgrp = 0;
    265 	return (0);
    266 }
    267 
    268 /*
    269  * delete a process group
    270  */
    271 void
    272 pgdelete(pgrp)
    273 	register struct pgrp *pgrp;
    274 {
    275 
    276 	if (pgrp->pg_session->s_ttyp != NULL &&
    277 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    278 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    279 	LIST_REMOVE(pgrp, pg_hash);
    280 	if (--pgrp->pg_session->s_count == 0)
    281 		FREE(pgrp->pg_session, M_SESSION);
    282 	FREE(pgrp, M_PGRP);
    283 }
    284 
    285 /*
    286  * Adjust pgrp jobc counters when specified process changes process group.
    287  * We count the number of processes in each process group that "qualify"
    288  * the group for terminal job control (those with a parent in a different
    289  * process group of the same session).  If that count reaches zero, the
    290  * process group becomes orphaned.  Check both the specified process'
    291  * process group and that of its children.
    292  * entering == 0 => p is leaving specified group.
    293  * entering == 1 => p is entering specified group.
    294  */
    295 void
    296 fixjobc(p, pgrp, entering)
    297 	register struct proc *p;
    298 	register struct pgrp *pgrp;
    299 	int entering;
    300 {
    301 	register struct pgrp *hispgrp;
    302 	register struct session *mysession = pgrp->pg_session;
    303 
    304 	/*
    305 	 * Check p's parent to see whether p qualifies its own process
    306 	 * group; if so, adjust count for p's process group.
    307 	 */
    308 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    309 	    hispgrp->pg_session == mysession)
    310 		if (entering)
    311 			pgrp->pg_jobc++;
    312 		else if (--pgrp->pg_jobc == 0)
    313 			orphanpg(pgrp);
    314 
    315 	/*
    316 	 * Check this process' children to see whether they qualify
    317 	 * their process groups; if so, adjust counts for children's
    318 	 * process groups.
    319 	 */
    320 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next)
    321 		if ((hispgrp = p->p_pgrp) != pgrp &&
    322 		    hispgrp->pg_session == mysession &&
    323 		    p->p_stat != SZOMB)
    324 			if (entering)
    325 				hispgrp->pg_jobc++;
    326 			else if (--hispgrp->pg_jobc == 0)
    327 				orphanpg(hispgrp);
    328 }
    329 
    330 /*
    331  * A process group has become orphaned;
    332  * if there are any stopped processes in the group,
    333  * hang-up all process in that group.
    334  */
    335 static void
    336 orphanpg(pg)
    337 	struct pgrp *pg;
    338 {
    339 	register struct proc *p;
    340 
    341 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    342 		if (p->p_stat == SSTOP) {
    343 			for (p = pg->pg_members.lh_first; p != 0;
    344 			    p = p->p_pglist.le_next) {
    345 				psignal(p, SIGHUP);
    346 				psignal(p, SIGCONT);
    347 			}
    348 			return;
    349 		}
    350 	}
    351 }
    352 
    353 #ifdef DEBUG
    354 pgrpdump()
    355 {
    356 	register struct pgrp *pgrp;
    357 	register struct proc *p;
    358 	register i;
    359 
    360 	for (i = 0; i <= pgrphash; i++) {
    361 		if (pgrp = pgrphashtbl[i].lh_first) {
    362 			printf("\tindx %d\n", i);
    363 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
    364 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
    365 				    pgrp, pgrp->pg_id, pgrp->pg_session,
    366 				    pgrp->pg_session->s_count,
    367 				    pgrp->pg_members.lh_first);
    368 				for (p = pgrp->pg_members.lh_first; p != 0;
    369 				    p = p->p_pglist.le_next) {
    370 					printf("\t\tpid %d addr %p pgrp %p\n",
    371 					    p->p_pid, p, p->p_pgrp);
    372 				}
    373 			}
    374 		}
    375 	}
    376 }
    377 #endif /* DEBUG */
    378