Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.29
      1 /*	$NetBSD: kern_proc.c,v 1.29 1998/09/01 00:08:44 thorpej 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.7 (Berkeley) 2/14/95
     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/resourcevar.h>
     44 #include <sys/buf.h>
     45 #include <sys/acct.h>
     46 #include <sys/wait.h>
     47 #include <sys/file.h>
     48 #include <ufs/ufs/quota.h>
     49 #include <sys/uio.h>
     50 #include <sys/malloc.h>
     51 #include <sys/pool.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/tty.h>
     55 #include <sys/signalvar.h>
     56 
     57 /*
     58  * Structure associated with user cacheing.
     59  */
     60 struct uidinfo {
     61 	LIST_ENTRY(uidinfo) ui_hash;
     62 	uid_t	ui_uid;
     63 	long	ui_proccnt;
     64 };
     65 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
     66 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
     67 u_long uihash;		/* size of hash table - 1 */
     68 
     69 /*
     70  * Other process lists
     71  */
     72 struct pidhashhead *pidhashtbl;
     73 u_long pidhash;
     74 struct pgrphashhead *pgrphashtbl;
     75 u_long pgrphash;
     76 struct proclist allproc;
     77 struct proclist zombproc;
     78 struct pool proc_pool;
     79 struct pool pcred_pool;
     80 struct pool plimit_pool;
     81 struct pool pgrp_pool;
     82 
     83 static void orphanpg __P((struct pgrp *));
     84 #ifdef DEBUG
     85 void pgrpdump __P((void));
     86 #endif
     87 
     88 /*
     89  * Initialize global process hashing structures.
     90  */
     91 void
     92 procinit()
     93 {
     94 
     95 	LIST_INIT(&allproc);
     96 	LIST_INIT(&zombproc);
     97 	pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash);
     98 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash);
     99 	uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash);
    100 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    101 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_PROC);
    102 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    103 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_PGRP);
    104 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
    105 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    106 	pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    107 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    108 }
    109 
    110 /*
    111  * Change the count associated with number of processes
    112  * a given user is using.
    113  */
    114 int
    115 chgproccnt(uid, diff)
    116 	uid_t	uid;
    117 	int	diff;
    118 {
    119 	register struct uidinfo *uip;
    120 	register struct uihashhead *uipp;
    121 
    122 	uipp = UIHASH(uid);
    123 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
    124 		if (uip->ui_uid == uid)
    125 			break;
    126 	if (uip) {
    127 		uip->ui_proccnt += diff;
    128 		if (uip->ui_proccnt > 0)
    129 			return (uip->ui_proccnt);
    130 		if (uip->ui_proccnt < 0)
    131 			panic("chgproccnt: procs < 0");
    132 		LIST_REMOVE(uip, ui_hash);
    133 		FREE(uip, M_PROC);
    134 		return (0);
    135 	}
    136 	if (diff <= 0) {
    137 		if (diff == 0)
    138 			return(0);
    139 		panic("chgproccnt: lost user");
    140 	}
    141 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    142 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    143 	uip->ui_uid = uid;
    144 	uip->ui_proccnt = diff;
    145 	return (diff);
    146 }
    147 
    148 /*
    149  * Is p an inferior of the current process?
    150  */
    151 int
    152 inferior(p)
    153 	register struct proc *p;
    154 {
    155 
    156 	for (; p != curproc; p = p->p_pptr)
    157 		if (p->p_pid == 0)
    158 			return (0);
    159 	return (1);
    160 }
    161 
    162 /*
    163  * Locate a process by number
    164  */
    165 struct proc *
    166 pfind(pid)
    167 	register pid_t pid;
    168 {
    169 	register struct proc *p;
    170 
    171 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
    172 		if (p->p_pid == pid)
    173 			return (p);
    174 	return (NULL);
    175 }
    176 
    177 /*
    178  * Locate a process group by number
    179  */
    180 struct pgrp *
    181 pgfind(pgid)
    182 	register pid_t pgid;
    183 {
    184 	register struct pgrp *pgrp;
    185 
    186 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
    187 		if (pgrp->pg_id == pgid)
    188 			return (pgrp);
    189 	return (NULL);
    190 }
    191 
    192 /*
    193  * Move p to a new or existing process group (and session)
    194  */
    195 int
    196 enterpgrp(p, pgid, mksess)
    197 	register struct proc *p;
    198 	pid_t pgid;
    199 	int mksess;
    200 {
    201 	register struct pgrp *pgrp = pgfind(pgid);
    202 
    203 #ifdef DIAGNOSTIC
    204 	if (pgrp != NULL && mksess)	/* firewalls */
    205 		panic("enterpgrp: setsid into non-empty pgrp");
    206 	if (SESS_LEADER(p))
    207 		panic("enterpgrp: session leader attempted setpgrp");
    208 #endif
    209 	if (pgrp == NULL) {
    210 		pid_t savepid = p->p_pid;
    211 		struct proc *np;
    212 		/*
    213 		 * new process group
    214 		 */
    215 #ifdef DIAGNOSTIC
    216 		if (p->p_pid != pgid)
    217 			panic("enterpgrp: new pgrp and pid != pgid");
    218 #endif
    219 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    220 		if ((np = pfind(savepid)) == NULL || np != p)
    221 			return (ESRCH);
    222 		if (mksess) {
    223 			register struct session *sess;
    224 
    225 			/*
    226 			 * new session
    227 			 */
    228 			MALLOC(sess, struct session *, sizeof(struct session),
    229 			    M_SESSION, M_WAITOK);
    230 			sess->s_sid = p->p_pid;
    231 			sess->s_leader = p;
    232 			sess->s_count = 1;
    233 			sess->s_ttyvp = NULL;
    234 			sess->s_ttyp = NULL;
    235 			memcpy(sess->s_login, p->p_session->s_login,
    236 			    sizeof(sess->s_login));
    237 			p->p_flag &= ~P_CONTROLT;
    238 			pgrp->pg_session = sess;
    239 #ifdef DIAGNOSTIC
    240 			if (p != curproc)
    241 				panic("enterpgrp: mksession and p != curproc");
    242 #endif
    243 		} else {
    244 			pgrp->pg_session = p->p_session;
    245 			pgrp->pg_session->s_count++;
    246 		}
    247 		pgrp->pg_id = pgid;
    248 		LIST_INIT(&pgrp->pg_members);
    249 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
    250 		pgrp->pg_jobc = 0;
    251 	} else if (pgrp == p->p_pgrp)
    252 		return (0);
    253 
    254 	/*
    255 	 * Adjust eligibility of affected pgrps to participate in job control.
    256 	 * Increment eligibility counts before decrementing, otherwise we
    257 	 * could reach 0 spuriously during the first call.
    258 	 */
    259 	fixjobc(p, pgrp, 1);
    260 	fixjobc(p, p->p_pgrp, 0);
    261 
    262 	LIST_REMOVE(p, p_pglist);
    263 	if (p->p_pgrp->pg_members.lh_first == 0)
    264 		pgdelete(p->p_pgrp);
    265 	p->p_pgrp = pgrp;
    266 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    267 	return (0);
    268 }
    269 
    270 /*
    271  * remove process from process group
    272  */
    273 int
    274 leavepgrp(p)
    275 	register struct proc *p;
    276 {
    277 
    278 	LIST_REMOVE(p, p_pglist);
    279 	if (p->p_pgrp->pg_members.lh_first == 0)
    280 		pgdelete(p->p_pgrp);
    281 	p->p_pgrp = 0;
    282 	return (0);
    283 }
    284 
    285 /*
    286  * delete a process group
    287  */
    288 void
    289 pgdelete(pgrp)
    290 	register struct pgrp *pgrp;
    291 {
    292 
    293 	if (pgrp->pg_session->s_ttyp != NULL &&
    294 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    295 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    296 	LIST_REMOVE(pgrp, pg_hash);
    297 	if (--pgrp->pg_session->s_count == 0)
    298 		FREE(pgrp->pg_session, M_SESSION);
    299 	pool_put(&pgrp_pool, pgrp);
    300 }
    301 
    302 /*
    303  * Adjust pgrp jobc counters when specified process changes process group.
    304  * We count the number of processes in each process group that "qualify"
    305  * the group for terminal job control (those with a parent in a different
    306  * process group of the same session).  If that count reaches zero, the
    307  * process group becomes orphaned.  Check both the specified process'
    308  * process group and that of its children.
    309  * entering == 0 => p is leaving specified group.
    310  * entering == 1 => p is entering specified group.
    311  */
    312 void
    313 fixjobc(p, pgrp, entering)
    314 	register struct proc *p;
    315 	register struct pgrp *pgrp;
    316 	int entering;
    317 {
    318 	register struct pgrp *hispgrp;
    319 	register struct session *mysession = pgrp->pg_session;
    320 
    321 	/*
    322 	 * Check p's parent to see whether p qualifies its own process
    323 	 * group; if so, adjust count for p's process group.
    324 	 */
    325 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    326 	    hispgrp->pg_session == mysession) {
    327 		if (entering)
    328 			pgrp->pg_jobc++;
    329 		else if (--pgrp->pg_jobc == 0)
    330 			orphanpg(pgrp);
    331 	}
    332 
    333 	/*
    334 	 * Check this process' children to see whether they qualify
    335 	 * their process groups; if so, adjust counts for children's
    336 	 * process groups.
    337 	 */
    338 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    339 		if ((hispgrp = p->p_pgrp) != pgrp &&
    340 		    hispgrp->pg_session == mysession &&
    341 		    p->p_stat != SZOMB) {
    342 			if (entering)
    343 				hispgrp->pg_jobc++;
    344 			else if (--hispgrp->pg_jobc == 0)
    345 				orphanpg(hispgrp);
    346 		}
    347 	}
    348 }
    349 
    350 /*
    351  * A process group has become orphaned;
    352  * if there are any stopped processes in the group,
    353  * hang-up all process in that group.
    354  */
    355 static void
    356 orphanpg(pg)
    357 	struct pgrp *pg;
    358 {
    359 	register struct proc *p;
    360 
    361 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    362 		if (p->p_stat == SSTOP) {
    363 			for (p = pg->pg_members.lh_first; p != 0;
    364 			    p = p->p_pglist.le_next) {
    365 				psignal(p, SIGHUP);
    366 				psignal(p, SIGCONT);
    367 			}
    368 			return;
    369 		}
    370 	}
    371 }
    372 
    373 #ifdef DEBUG
    374 void
    375 pgrpdump()
    376 {
    377 	register struct pgrp *pgrp;
    378 	register struct proc *p;
    379 	register int i;
    380 
    381 	for (i = 0; i <= pgrphash; i++) {
    382 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
    383 			printf("\tindx %d\n", i);
    384 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
    385 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
    386 				    pgrp, pgrp->pg_id, pgrp->pg_session,
    387 				    pgrp->pg_session->s_count,
    388 				    pgrp->pg_members.lh_first);
    389 				for (p = pgrp->pg_members.lh_first; p != 0;
    390 				    p = p->p_pglist.le_next) {
    391 					printf("\t\tpid %d addr %p pgrp %p\n",
    392 					    p->p_pid, p, p->p_pgrp);
    393 				}
    394 			}
    395 		}
    396 	}
    397 }
    398 #endif /* DEBUG */
    399