Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.33
      1 /*	$NetBSD: kern_proc.c,v 1.33 1999/07/22 21:08:31 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
     73  */
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/map.h>
     78 #include <sys/kernel.h>
     79 #include <sys/proc.h>
     80 #include <sys/resourcevar.h>
     81 #include <sys/buf.h>
     82 #include <sys/acct.h>
     83 #include <sys/wait.h>
     84 #include <sys/file.h>
     85 #include <ufs/ufs/quota.h>
     86 #include <sys/uio.h>
     87 #include <sys/malloc.h>
     88 #include <sys/pool.h>
     89 #include <sys/mbuf.h>
     90 #include <sys/ioctl.h>
     91 #include <sys/tty.h>
     92 #include <sys/signalvar.h>
     93 
     94 /*
     95  * Structure associated with user cacheing.
     96  */
     97 struct uidinfo {
     98 	LIST_ENTRY(uidinfo) ui_hash;
     99 	uid_t	ui_uid;
    100 	long	ui_proccnt;
    101 };
    102 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
    103 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
    104 u_long uihash;		/* size of hash table - 1 */
    105 
    106 /*
    107  * Other process lists
    108  */
    109 struct pidhashhead *pidhashtbl;
    110 u_long pidhash;
    111 struct pgrphashhead *pgrphashtbl;
    112 u_long pgrphash;
    113 
    114 struct proclist allproc;
    115 struct proclist zombproc;	/* resources have been freed */
    116 
    117 /*
    118  * Process list locking:
    119  *
    120  * We have two types of locks on the proclists: read locks and write
    121  * locks.  Read locks can be used in interrupt context, so while we
    122  * hold the write lock, we must also block softclock interrupts (since
    123  * the interrupt context is the timeout-driven schedcpu()).
    124  *
    125  * The proclist lock locks the following structures:
    126  *
    127  *	allproc
    128  *	zombproc
    129  *	pidhashtbl
    130  */
    131 struct lock proclist_lock;
    132 
    133 /*
    134  * Locking of this proclist is special; it's accessed in a
    135  * critical section of process exit, and thus locking it can't
    136  * modify interrupt state.  We use a simple spin lock for this
    137  * proclist.  Processes on this proclist are also on zombproc;
    138  * we use the p_hash member to linkup to deadproc.
    139  */
    140 struct simplelock deadproc_slock;
    141 struct proclist deadproc;	/* dead, but not yet undead */
    142 
    143 struct pool proc_pool;
    144 struct pool pcred_pool;
    145 struct pool plimit_pool;
    146 struct pool pgrp_pool;
    147 struct pool rusage_pool;
    148 
    149 /*
    150  * The process list descriptors, used during pid allocation and
    151  * by sysctl.  No locking on this data structure is needed since
    152  * it is completely static.
    153  */
    154 const struct proclist_desc proclists[] = {
    155 	{ &allproc	},
    156 	{ &zombproc	},
    157 	{ NULL		},
    158 };
    159 
    160 static void orphanpg __P((struct pgrp *));
    161 #ifdef DEBUG
    162 void pgrpdump __P((void));
    163 #endif
    164 
    165 /*
    166  * Initialize global process hashing structures.
    167  */
    168 void
    169 procinit()
    170 {
    171 	const struct proclist_desc *pd;
    172 
    173 	for (pd = proclists; pd->pd_list != NULL; pd++)
    174 		LIST_INIT(pd->pd_list);
    175 
    176 	lockinit(&proclist_lock, PZERO, "proclk", 0, 0);
    177 
    178 	LIST_INIT(&deadproc);
    179 	simple_lock_init(&deadproc_slock);
    180 
    181 	pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash);
    182 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash);
    183 	uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash);
    184 
    185 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    186 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_PROC);
    187 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    188 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_PGRP);
    189 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
    190 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    191 	pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    192 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    193 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
    194 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_ZOMBIE);
    195 }
    196 
    197 /*
    198  * Acquire a read lock on the proclist.
    199  */
    200 void
    201 proclist_lock_read(flags)
    202 	int flags;
    203 {
    204 	int error, s;
    205 
    206 	/* Block schedcpu() while we acquire the lock. */
    207 	s = splsoftclock();
    208 
    209 	/*
    210 	 * We spin here if called with LK_NOWAIT; schedcpu() uses that
    211 	 * to prevent sleeping.
    212 	 */
    213 	do {
    214 		error = lockmgr(&proclist_lock, LK_SHARED | flags, NULL);
    215 #ifdef DIAGNOSTIC
    216 		if (error != 0 && error != EBUSY)
    217 			panic("proclist_lock_read: failed to acquire lock");
    218 #endif
    219 	} while (error != 0);
    220 
    221 	/* Let schedcpu() back in. */
    222 	splx(s);
    223 }
    224 
    225 /*
    226  * Release a read lock on the proclist.
    227  */
    228 void
    229 proclist_unlock_read()
    230 {
    231 	int s;
    232 
    233 	/* Block schedcpu() while we release the lock. */
    234 	s = splsoftclock();
    235 
    236 	(void) lockmgr(&proclist_lock, LK_RELEASE, NULL);
    237 
    238 	/* Let schedcpu() back in. */
    239 	splx(s);
    240 }
    241 
    242 /*
    243  * Acquire a write lock on the proclist.
    244  */
    245 int
    246 proclist_lock_write()
    247 {
    248 	int error, s;
    249 
    250 	/* Block schedcpu() while lock is held. */
    251 	s = splsoftclock();
    252 
    253 	error = lockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
    254 #ifdef DIAGNOSTIC
    255 	if (error != 0)
    256 		panic("proclist_lock: failed to acquire lock");
    257 #endif
    258 
    259 	return (s);
    260 }
    261 
    262 /*
    263  * Release a write lock on the proclist.
    264  */
    265 void
    266 proclist_unlock_write(s)
    267 	int s;
    268 {
    269 
    270 	(void) lockmgr(&proclist_lock, LK_RELEASE, NULL);
    271 
    272 	/* Let schedcpu() back in. */
    273 	splx(s);
    274 }
    275 
    276 /*
    277  * Change the count associated with number of processes
    278  * a given user is using.
    279  */
    280 int
    281 chgproccnt(uid, diff)
    282 	uid_t	uid;
    283 	int	diff;
    284 {
    285 	register struct uidinfo *uip;
    286 	register struct uihashhead *uipp;
    287 
    288 	uipp = UIHASH(uid);
    289 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
    290 		if (uip->ui_uid == uid)
    291 			break;
    292 	if (uip) {
    293 		uip->ui_proccnt += diff;
    294 		if (uip->ui_proccnt > 0)
    295 			return (uip->ui_proccnt);
    296 		if (uip->ui_proccnt < 0)
    297 			panic("chgproccnt: procs < 0");
    298 		LIST_REMOVE(uip, ui_hash);
    299 		FREE(uip, M_PROC);
    300 		return (0);
    301 	}
    302 	if (diff <= 0) {
    303 		if (diff == 0)
    304 			return(0);
    305 		panic("chgproccnt: lost user");
    306 	}
    307 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    308 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    309 	uip->ui_uid = uid;
    310 	uip->ui_proccnt = diff;
    311 	return (diff);
    312 }
    313 
    314 /*
    315  * Is p an inferior of the current process?
    316  */
    317 int
    318 inferior(p)
    319 	register struct proc *p;
    320 {
    321 
    322 	for (; p != curproc; p = p->p_pptr)
    323 		if (p->p_pid == 0)
    324 			return (0);
    325 	return (1);
    326 }
    327 
    328 /*
    329  * Locate a process by number
    330  */
    331 struct proc *
    332 pfind(pid)
    333 	register pid_t pid;
    334 {
    335 	struct proc *p;
    336 
    337 	proclist_lock_read(0);
    338 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
    339 		if (p->p_pid == pid)
    340 			goto out;
    341  out:
    342 	proclist_unlock_read();
    343 	return (p);
    344 }
    345 
    346 /*
    347  * Locate a process group by number
    348  */
    349 struct pgrp *
    350 pgfind(pgid)
    351 	register pid_t pgid;
    352 {
    353 	register struct pgrp *pgrp;
    354 
    355 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
    356 		if (pgrp->pg_id == pgid)
    357 			return (pgrp);
    358 	return (NULL);
    359 }
    360 
    361 /*
    362  * Move p to a new or existing process group (and session)
    363  */
    364 int
    365 enterpgrp(p, pgid, mksess)
    366 	register struct proc *p;
    367 	pid_t pgid;
    368 	int mksess;
    369 {
    370 	register struct pgrp *pgrp = pgfind(pgid);
    371 
    372 #ifdef DIAGNOSTIC
    373 	if (pgrp != NULL && mksess)	/* firewalls */
    374 		panic("enterpgrp: setsid into non-empty pgrp");
    375 	if (SESS_LEADER(p))
    376 		panic("enterpgrp: session leader attempted setpgrp");
    377 #endif
    378 	if (pgrp == NULL) {
    379 		pid_t savepid = p->p_pid;
    380 		struct proc *np;
    381 		/*
    382 		 * new process group
    383 		 */
    384 #ifdef DIAGNOSTIC
    385 		if (p->p_pid != pgid)
    386 			panic("enterpgrp: new pgrp and pid != pgid");
    387 #endif
    388 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    389 		if ((np = pfind(savepid)) == NULL || np != p)
    390 			return (ESRCH);
    391 		if (mksess) {
    392 			register struct session *sess;
    393 
    394 			/*
    395 			 * new session
    396 			 */
    397 			MALLOC(sess, struct session *, sizeof(struct session),
    398 			    M_SESSION, M_WAITOK);
    399 			sess->s_sid = p->p_pid;
    400 			sess->s_leader = p;
    401 			sess->s_count = 1;
    402 			sess->s_ttyvp = NULL;
    403 			sess->s_ttyp = NULL;
    404 			memcpy(sess->s_login, p->p_session->s_login,
    405 			    sizeof(sess->s_login));
    406 			p->p_flag &= ~P_CONTROLT;
    407 			pgrp->pg_session = sess;
    408 #ifdef DIAGNOSTIC
    409 			if (p != curproc)
    410 				panic("enterpgrp: mksession and p != curproc");
    411 #endif
    412 		} else {
    413 			pgrp->pg_session = p->p_session;
    414 			pgrp->pg_session->s_count++;
    415 		}
    416 		pgrp->pg_id = pgid;
    417 		LIST_INIT(&pgrp->pg_members);
    418 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
    419 		pgrp->pg_jobc = 0;
    420 	} else if (pgrp == p->p_pgrp)
    421 		return (0);
    422 
    423 	/*
    424 	 * Adjust eligibility of affected pgrps to participate in job control.
    425 	 * Increment eligibility counts before decrementing, otherwise we
    426 	 * could reach 0 spuriously during the first call.
    427 	 */
    428 	fixjobc(p, pgrp, 1);
    429 	fixjobc(p, p->p_pgrp, 0);
    430 
    431 	LIST_REMOVE(p, p_pglist);
    432 	if (p->p_pgrp->pg_members.lh_first == 0)
    433 		pgdelete(p->p_pgrp);
    434 	p->p_pgrp = pgrp;
    435 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    436 	return (0);
    437 }
    438 
    439 /*
    440  * remove process from process group
    441  */
    442 int
    443 leavepgrp(p)
    444 	register struct proc *p;
    445 {
    446 
    447 	LIST_REMOVE(p, p_pglist);
    448 	if (p->p_pgrp->pg_members.lh_first == 0)
    449 		pgdelete(p->p_pgrp);
    450 	p->p_pgrp = 0;
    451 	return (0);
    452 }
    453 
    454 /*
    455  * delete a process group
    456  */
    457 void
    458 pgdelete(pgrp)
    459 	register struct pgrp *pgrp;
    460 {
    461 
    462 	if (pgrp->pg_session->s_ttyp != NULL &&
    463 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    464 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    465 	LIST_REMOVE(pgrp, pg_hash);
    466 	if (--pgrp->pg_session->s_count == 0)
    467 		FREE(pgrp->pg_session, M_SESSION);
    468 	pool_put(&pgrp_pool, pgrp);
    469 }
    470 
    471 /*
    472  * Adjust pgrp jobc counters when specified process changes process group.
    473  * We count the number of processes in each process group that "qualify"
    474  * the group for terminal job control (those with a parent in a different
    475  * process group of the same session).  If that count reaches zero, the
    476  * process group becomes orphaned.  Check both the specified process'
    477  * process group and that of its children.
    478  * entering == 0 => p is leaving specified group.
    479  * entering == 1 => p is entering specified group.
    480  */
    481 void
    482 fixjobc(p, pgrp, entering)
    483 	register struct proc *p;
    484 	register struct pgrp *pgrp;
    485 	int entering;
    486 {
    487 	register struct pgrp *hispgrp;
    488 	register struct session *mysession = pgrp->pg_session;
    489 
    490 	/*
    491 	 * Check p's parent to see whether p qualifies its own process
    492 	 * group; if so, adjust count for p's process group.
    493 	 */
    494 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    495 	    hispgrp->pg_session == mysession) {
    496 		if (entering)
    497 			pgrp->pg_jobc++;
    498 		else if (--pgrp->pg_jobc == 0)
    499 			orphanpg(pgrp);
    500 	}
    501 
    502 	/*
    503 	 * Check this process' children to see whether they qualify
    504 	 * their process groups; if so, adjust counts for children's
    505 	 * process groups.
    506 	 */
    507 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    508 		if ((hispgrp = p->p_pgrp) != pgrp &&
    509 		    hispgrp->pg_session == mysession &&
    510 		    P_ZOMBIE(p) == 0) {
    511 			if (entering)
    512 				hispgrp->pg_jobc++;
    513 			else if (--hispgrp->pg_jobc == 0)
    514 				orphanpg(hispgrp);
    515 		}
    516 	}
    517 }
    518 
    519 /*
    520  * A process group has become orphaned;
    521  * if there are any stopped processes in the group,
    522  * hang-up all process in that group.
    523  */
    524 static void
    525 orphanpg(pg)
    526 	struct pgrp *pg;
    527 {
    528 	register struct proc *p;
    529 
    530 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    531 		if (p->p_stat == SSTOP) {
    532 			for (p = pg->pg_members.lh_first; p != 0;
    533 			    p = p->p_pglist.le_next) {
    534 				psignal(p, SIGHUP);
    535 				psignal(p, SIGCONT);
    536 			}
    537 			return;
    538 		}
    539 	}
    540 }
    541 
    542 #ifdef DEBUG
    543 void
    544 pgrpdump()
    545 {
    546 	register struct pgrp *pgrp;
    547 	register struct proc *p;
    548 	register int i;
    549 
    550 	for (i = 0; i <= pgrphash; i++) {
    551 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
    552 			printf("\tindx %d\n", i);
    553 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
    554 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
    555 				    pgrp, pgrp->pg_id, pgrp->pg_session,
    556 				    pgrp->pg_session->s_count,
    557 				    pgrp->pg_members.lh_first);
    558 				for (p = pgrp->pg_members.lh_first; p != 0;
    559 				    p = p->p_pglist.le_next) {
    560 					printf("\t\tpid %d addr %p pgrp %p\n",
    561 					    p->p_pid, p, p->p_pgrp);
    562 				}
    563 			}
    564 		}
    565 	}
    566 }
    567 #endif /* DEBUG */
    568