Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.44.2.6
      1 /*	$NetBSD: kern_proc.c,v 1.44.2.6 2002/04/17 00:06:18 nathanw 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/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.44.2.6 2002/04/17 00:06:18 nathanw Exp $");
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/map.h>
     81 #include <sys/kernel.h>
     82 #include <sys/proc.h>
     83 #include <sys/lwp.h>
     84 #include <sys/resourcevar.h>
     85 #include <sys/buf.h>
     86 #include <sys/acct.h>
     87 #include <sys/wait.h>
     88 #include <sys/file.h>
     89 #include <ufs/ufs/quota.h>
     90 #include <sys/uio.h>
     91 #include <sys/malloc.h>
     92 #include <sys/pool.h>
     93 #include <sys/mbuf.h>
     94 #include <sys/ioctl.h>
     95 #include <sys/tty.h>
     96 #include <sys/signalvar.h>
     97 #include <sys/sa.h>
     98 #include <sys/savar.h>
     99 
    100 /*
    101  * Structure associated with user cacheing.
    102  */
    103 struct uidinfo {
    104 	LIST_ENTRY(uidinfo) ui_hash;
    105 	uid_t	ui_uid;
    106 	long	ui_proccnt;
    107 };
    108 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
    109 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
    110 u_long uihash;		/* size of hash table - 1 */
    111 
    112 /*
    113  * Other process lists
    114  */
    115 struct pidhashhead *pidhashtbl;
    116 u_long pidhash;
    117 struct pgrphashhead *pgrphashtbl;
    118 u_long pgrphash;
    119 
    120 struct proclist allproc;
    121 struct proclist zombproc;	/* resources have been freed */
    122 
    123 
    124 /*
    125  * Process list locking:
    126  *
    127  * We have two types of locks on the proclists: read locks and write
    128  * locks.  Read locks can be used in interrupt context, so while we
    129  * hold the write lock, we must also block clock interrupts to
    130  * lock out any scheduling changes that may happen in interrupt
    131  * context.
    132  *
    133  * The proclist lock locks the following structures:
    134  *
    135  *	allproc
    136  *	zombproc
    137  *	pidhashtbl
    138  */
    139 struct lock proclist_lock;
    140 
    141 /*
    142  * Locking of this proclist is special; it's accessed in a
    143  * critical section of process exit, and thus locking it can't
    144  * modify interrupt state.  We use a simple spin lock for this
    145  * proclist.  Processes on this proclist are also on zombproc;
    146  * we use the p_hash member to linkup to deadproc.
    147  */
    148 struct simplelock deadproc_slock;
    149 struct proclist deadproc;	/* dead, but not yet undead */
    150 
    151 struct pool proc_pool;
    152 struct pool lwp_pool;
    153 struct pool lwp_uc_pool;
    154 struct pool pcred_pool;
    155 struct pool plimit_pool;
    156 struct pool pstats_pool;
    157 struct pool pgrp_pool;
    158 struct pool rusage_pool;
    159 struct pool sadata_pool;
    160 struct pool saupcall_pool;
    161 struct pool ptimer_pool;
    162 
    163 /*
    164  * The process list descriptors, used during pid allocation and
    165  * by sysctl.  No locking on this data structure is needed since
    166  * it is completely static.
    167  */
    168 const struct proclist_desc proclists[] = {
    169 	{ &allproc	},
    170 	{ &zombproc	},
    171 	{ NULL		},
    172 };
    173 
    174 static void orphanpg __P((struct pgrp *));
    175 #ifdef DEBUG
    176 void pgrpdump __P((void));
    177 #endif
    178 
    179 /*
    180  * Initialize global process hashing structures.
    181  */
    182 void
    183 procinit()
    184 {
    185 	const struct proclist_desc *pd;
    186 
    187 	for (pd = proclists; pd->pd_list != NULL; pd++)
    188 		LIST_INIT(pd->pd_list);
    189 
    190 	spinlockinit(&proclist_lock, "proclk", 0);
    191 
    192 	LIST_INIT(&deadproc);
    193 	simple_lock_init(&deadproc_slock);
    194 
    195 	LIST_INIT(&alllwp);
    196 	LIST_INIT(&deadlwp);
    197 	LIST_INIT(&zomblwp);
    198 
    199 	pidhashtbl =
    200 	    hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pidhash);
    201 	pgrphashtbl =
    202 	    hashinit(maxproc / 4, HASH_LIST, M_PROC, M_WAITOK, &pgrphash);
    203 	uihashtbl =
    204 	    hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
    205 
    206 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    207 	    &pool_allocator_nointr);
    208 	pool_init(&lwp_pool, sizeof(struct lwp), 0, 0, 0, "lwppl",
    209 	    &pool_allocator_nointr);
    210 	pool_init(&lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
    211 	    &pool_allocator_nointr);
    212 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    213 	    &pool_allocator_nointr);
    214 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
    215 	    &pool_allocator_nointr);
    216 	pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    217 	    &pool_allocator_nointr);
    218 	pool_init(&pstats_pool, sizeof(struct pstats), 0, 0, 0, "pstatspl",
    219 	    &pool_allocator_nointr);
    220 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
    221 	    &pool_allocator_nointr);
    222 	pool_init(&sadata_pool, sizeof(struct sadata), 0, 0, 0, "sadatapl",
    223 	    &pool_allocator_nointr);
    224 	pool_init(&saupcall_pool, sizeof(struct sadata_upcall), 0, 0, 0,
    225 	    "saupcpl",
    226 	    &pool_allocator_nointr);
    227 	pool_init(&ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
    228 	    &pool_allocator_nointr);
    229 }
    230 
    231 /*
    232  * Acquire a read lock on the proclist.
    233  */
    234 void
    235 proclist_lock_read()
    236 {
    237 	int error;
    238 
    239 	error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
    240 #ifdef DIAGNOSTIC
    241 	if (__predict_false(error != 0))
    242 		panic("proclist_lock_read: failed to acquire lock");
    243 #endif
    244 }
    245 
    246 /*
    247  * Release a read lock on the proclist.
    248  */
    249 void
    250 proclist_unlock_read()
    251 {
    252 
    253 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    254 }
    255 
    256 /*
    257  * Acquire a write lock on the proclist.
    258  */
    259 int
    260 proclist_lock_write()
    261 {
    262 	int s, error;
    263 
    264 	s = splclock();
    265 	error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
    266 #ifdef DIAGNOSTIC
    267 	if (__predict_false(error != 0))
    268 		panic("proclist_lock: failed to acquire lock");
    269 #endif
    270 	return (s);
    271 }
    272 
    273 /*
    274  * Release a write lock on the proclist.
    275  */
    276 void
    277 proclist_unlock_write(s)
    278 	int s;
    279 {
    280 
    281 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    282 	splx(s);
    283 }
    284 
    285 /*
    286  * Change the count associated with number of processes
    287  * a given user is using.
    288  */
    289 int
    290 chgproccnt(uid, diff)
    291 	uid_t	uid;
    292 	int	diff;
    293 {
    294 	struct uidinfo *uip;
    295 	struct uihashhead *uipp;
    296 
    297 	uipp = UIHASH(uid);
    298 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
    299 		if (uip->ui_uid == uid)
    300 			break;
    301 	if (uip) {
    302 		uip->ui_proccnt += diff;
    303 		if (uip->ui_proccnt > 0)
    304 			return (uip->ui_proccnt);
    305 		if (uip->ui_proccnt < 0)
    306 			panic("chgproccnt: procs < 0");
    307 		LIST_REMOVE(uip, ui_hash);
    308 		FREE(uip, M_PROC);
    309 		return (0);
    310 	}
    311 	if (diff <= 0) {
    312 		if (diff == 0)
    313 			return(0);
    314 		panic("chgproccnt: lost user");
    315 	}
    316 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    317 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    318 	uip->ui_uid = uid;
    319 	uip->ui_proccnt = diff;
    320 	return (diff);
    321 }
    322 
    323 /*
    324  * Is p an inferior of q?
    325  */
    326 int
    327 inferior(p, q)
    328 	struct proc *p;
    329 	struct proc *q;
    330 {
    331 
    332 	for (; p != q; p = p->p_pptr)
    333 		if (p->p_pid == 0)
    334 			return (0);
    335 	return (1);
    336 }
    337 
    338 /*
    339  * Locate a process by number
    340  */
    341 struct proc *
    342 pfind(pid)
    343 	pid_t pid;
    344 {
    345 	struct proc *p;
    346 
    347 	proclist_lock_read();
    348 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
    349 		if (p->p_pid == pid)
    350 			goto out;
    351  out:
    352 	proclist_unlock_read();
    353 	return (p);
    354 }
    355 
    356 /*
    357  * Locate a process group by number
    358  */
    359 struct pgrp *
    360 pgfind(pgid)
    361 	pid_t pgid;
    362 {
    363 	struct pgrp *pgrp;
    364 
    365 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
    366 		if (pgrp->pg_id == pgid)
    367 			return (pgrp);
    368 	return (NULL);
    369 }
    370 
    371 /*
    372  * Move p to a new or existing process group (and session)
    373  */
    374 int
    375 enterpgrp(p, pgid, mksess)
    376 	struct proc *p;
    377 	pid_t pgid;
    378 	int mksess;
    379 {
    380 	struct pgrp *pgrp = pgfind(pgid);
    381 
    382 #ifdef DIAGNOSTIC
    383 	if (__predict_false(pgrp != NULL && mksess))	/* firewalls */
    384 		panic("enterpgrp: setsid into non-empty pgrp");
    385 	if (__predict_false(SESS_LEADER(p)))
    386 		panic("enterpgrp: session leader attempted setpgrp");
    387 #endif
    388 	if (pgrp == NULL) {
    389 		pid_t savepid = p->p_pid;
    390 		struct proc *np;
    391 		/*
    392 		 * new process group
    393 		 */
    394 #ifdef DIAGNOSTIC
    395 		if (__predict_false(p->p_pid != pgid))
    396 			panic("enterpgrp: new pgrp and pid != pgid");
    397 #endif
    398 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    399 		if ((np = pfind(savepid)) == NULL || np != p)
    400 			return (ESRCH);
    401 		if (mksess) {
    402 			struct session *sess;
    403 
    404 			/*
    405 			 * new session
    406 			 */
    407 			MALLOC(sess, struct session *, sizeof(struct session),
    408 			    M_SESSION, M_WAITOK);
    409 			sess->s_sid = p->p_pid;
    410 			sess->s_leader = p;
    411 			sess->s_count = 1;
    412 			sess->s_ttyvp = NULL;
    413 			sess->s_ttyp = NULL;
    414 			memcpy(sess->s_login, p->p_session->s_login,
    415 			    sizeof(sess->s_login));
    416 			p->p_flag &= ~P_CONTROLT;
    417 			pgrp->pg_session = sess;
    418 #ifdef DIAGNOSTIC
    419 			if (__predict_false(p != curproc->l_proc))
    420 				panic("enterpgrp: mksession and p != curproc");
    421 #endif
    422 		} else {
    423 			SESSHOLD(p->p_session);
    424 			pgrp->pg_session = p->p_session;
    425 		}
    426 		pgrp->pg_id = pgid;
    427 		LIST_INIT(&pgrp->pg_members);
    428 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
    429 		pgrp->pg_jobc = 0;
    430 	} else if (pgrp == p->p_pgrp)
    431 		return (0);
    432 
    433 	/*
    434 	 * Adjust eligibility of affected pgrps to participate in job control.
    435 	 * Increment eligibility counts before decrementing, otherwise we
    436 	 * could reach 0 spuriously during the first call.
    437 	 */
    438 	fixjobc(p, pgrp, 1);
    439 	fixjobc(p, p->p_pgrp, 0);
    440 
    441 	LIST_REMOVE(p, p_pglist);
    442 	if (p->p_pgrp->pg_members.lh_first == 0)
    443 		pgdelete(p->p_pgrp);
    444 	p->p_pgrp = pgrp;
    445 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    446 	return (0);
    447 }
    448 
    449 /*
    450  * remove process from process group
    451  */
    452 int
    453 leavepgrp(p)
    454 	struct proc *p;
    455 {
    456 
    457 	LIST_REMOVE(p, p_pglist);
    458 	if (p->p_pgrp->pg_members.lh_first == 0)
    459 		pgdelete(p->p_pgrp);
    460 	p->p_pgrp = 0;
    461 	return (0);
    462 }
    463 
    464 /*
    465  * delete a process group
    466  */
    467 void
    468 pgdelete(pgrp)
    469 	struct pgrp *pgrp;
    470 {
    471 
    472 	/* Remove reference (if any) from tty to this process group */
    473 	if (pgrp->pg_session->s_ttyp != NULL &&
    474 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    475 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    476 	LIST_REMOVE(pgrp, pg_hash);
    477 	SESSRELE(pgrp->pg_session);
    478 	pool_put(&pgrp_pool, pgrp);
    479 }
    480 
    481 /*
    482  * Adjust pgrp jobc counters when specified process changes process group.
    483  * We count the number of processes in each process group that "qualify"
    484  * the group for terminal job control (those with a parent in a different
    485  * process group of the same session).  If that count reaches zero, the
    486  * process group becomes orphaned.  Check both the specified process'
    487  * process group and that of its children.
    488  * entering == 0 => p is leaving specified group.
    489  * entering == 1 => p is entering specified group.
    490  */
    491 void
    492 fixjobc(p, pgrp, entering)
    493 	struct proc *p;
    494 	struct pgrp *pgrp;
    495 	int entering;
    496 {
    497 	struct pgrp *hispgrp;
    498 	struct session *mysession = pgrp->pg_session;
    499 
    500 	/*
    501 	 * Check p's parent to see whether p qualifies its own process
    502 	 * group; if so, adjust count for p's process group.
    503 	 */
    504 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    505 	    hispgrp->pg_session == mysession) {
    506 		if (entering)
    507 			pgrp->pg_jobc++;
    508 		else if (--pgrp->pg_jobc == 0)
    509 			orphanpg(pgrp);
    510 	}
    511 
    512 	/*
    513 	 * Check this process' children to see whether they qualify
    514 	 * their process groups; if so, adjust counts for children's
    515 	 * process groups.
    516 	 */
    517 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    518 		if ((hispgrp = p->p_pgrp) != pgrp &&
    519 		    hispgrp->pg_session == mysession &&
    520 		    P_ZOMBIE(p) == 0) {
    521 			if (entering)
    522 				hispgrp->pg_jobc++;
    523 			else if (--hispgrp->pg_jobc == 0)
    524 				orphanpg(hispgrp);
    525 		}
    526 	}
    527 }
    528 
    529 /*
    530  * A process group has become orphaned;
    531  * if there are any stopped processes in the group,
    532  * hang-up all process in that group.
    533  */
    534 static void
    535 orphanpg(pg)
    536 	struct pgrp *pg;
    537 {
    538 	struct proc *p;
    539 
    540 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    541 		if (p->p_stat == SSTOP) {
    542 			for (p = pg->pg_members.lh_first; p != 0;
    543 			    p = p->p_pglist.le_next) {
    544 				psignal(p, SIGHUP);
    545 				psignal(p, SIGCONT);
    546 			}
    547 			return;
    548 		}
    549 	}
    550 }
    551 
    552 /* mark process as suid/sgid, reset some values do defaults */
    553 void
    554 p_sugid(p)
    555 	struct proc *p;
    556 {
    557 	struct plimit *newlim;
    558 
    559 	p->p_flag |= P_SUGID;
    560 	/* reset what needs to be reset in plimit */
    561 	if (p->p_limit->pl_corename != defcorename) {
    562 		if (p->p_limit->p_refcnt > 1 &&
    563 		    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    564 			newlim = limcopy(p->p_limit);
    565 			limfree(p->p_limit);
    566 			p->p_limit = newlim;
    567 		} else {
    568 			free(p->p_limit->pl_corename, M_TEMP);
    569 		}
    570 		p->p_limit->pl_corename = defcorename;
    571 	}
    572 }
    573 
    574 
    575 #ifdef DEBUG
    576 void
    577 pgrpdump()
    578 {
    579 	struct pgrp *pgrp;
    580 	struct proc *p;
    581 	int i;
    582 
    583 	for (i = 0; i <= pgrphash; i++) {
    584 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
    585 			printf("\tindx %d\n", i);
    586 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
    587 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
    588 				    pgrp, pgrp->pg_id, pgrp->pg_session,
    589 				    pgrp->pg_session->s_count,
    590 				    pgrp->pg_members.lh_first);
    591 				for (p = pgrp->pg_members.lh_first; p != 0;
    592 				    p = p->p_pglist.le_next) {
    593 					printf("\t\tpid %d addr %p pgrp %p\n",
    594 					    p->p_pid, p, p->p_pgrp);
    595 				}
    596 			}
    597 		}
    598 	}
    599 }
    600 #endif /* DEBUG */
    601