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