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