Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.57
      1 /*	$NetBSD: kern_proc.c,v 1.57 2003/02/01 06:23:43 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/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.57 2003/02/01 06:23:43 thorpej 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()
    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()
    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()
    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()
    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(s)
    287 	int s;
    288 {
    289 
    290 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    291 	splx(s);
    292 }
    293 
    294 /*
    295  * Change the count associated with number of processes
    296  * a given user is using.
    297  */
    298 int
    299 chgproccnt(uid, diff)
    300 	uid_t	uid;
    301 	int	diff;
    302 {
    303 	struct uidinfo *uip;
    304 	struct uihashhead *uipp;
    305 
    306 	uipp = UIHASH(uid);
    307 
    308 	LIST_FOREACH(uip, uipp, ui_hash)
    309 		if (uip->ui_uid == uid)
    310 			break;
    311 
    312 	if (uip) {
    313 		uip->ui_proccnt += diff;
    314 		if (uip->ui_proccnt > 0)
    315 			return (uip->ui_proccnt);
    316 		if (uip->ui_proccnt < 0)
    317 			panic("chgproccnt: procs < 0");
    318 		LIST_REMOVE(uip, ui_hash);
    319 		FREE(uip, M_PROC);
    320 		return (0);
    321 	}
    322 	if (diff <= 0) {
    323 		if (diff == 0)
    324 			return(0);
    325 		panic("chgproccnt: lost user");
    326 	}
    327 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    328 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    329 	uip->ui_uid = uid;
    330 	uip->ui_proccnt = diff;
    331 	return (diff);
    332 }
    333 
    334 /*
    335  * Is p an inferior of q?
    336  */
    337 int
    338 inferior(p, q)
    339 	struct proc *p;
    340 	struct proc *q;
    341 {
    342 
    343 	for (; p != q; p = p->p_pptr)
    344 		if (p->p_pid == 0)
    345 			return (0);
    346 	return (1);
    347 }
    348 
    349 /*
    350  * Locate a process by number
    351  */
    352 struct proc *
    353 pfind(pid)
    354 	pid_t pid;
    355 {
    356 	struct proc *p;
    357 
    358 	proclist_lock_read();
    359 	LIST_FOREACH(p, PIDHASH(pid), p_hash)
    360 		if (p->p_pid == pid)
    361 			goto out;
    362  out:
    363 	proclist_unlock_read();
    364 	return (p);
    365 }
    366 
    367 /*
    368  * Locate a process group by number
    369  */
    370 struct pgrp *
    371 pgfind(pgid)
    372 	pid_t pgid;
    373 {
    374 	struct pgrp *pgrp;
    375 
    376 	LIST_FOREACH(pgrp, PGRPHASH(pgid), pg_hash)
    377 		if (pgrp->pg_id == pgid)
    378 			return (pgrp);
    379 	return (NULL);
    380 }
    381 
    382 /*
    383  * Move p to a new or existing process group (and session)
    384  */
    385 int
    386 enterpgrp(p, pgid, mksess)
    387 	struct proc *p;
    388 	pid_t pgid;
    389 	int mksess;
    390 {
    391 	struct pgrp *pgrp = pgfind(pgid);
    392 
    393 #ifdef DIAGNOSTIC
    394 	if (__predict_false(pgrp != NULL && mksess))	/* firewalls */
    395 		panic("enterpgrp: setsid into non-empty pgrp");
    396 	if (__predict_false(SESS_LEADER(p)))
    397 		panic("enterpgrp: session leader attempted setpgrp");
    398 #endif
    399 	if (pgrp == NULL) {
    400 		pid_t savepid = p->p_pid;
    401 		struct proc *np;
    402 		/*
    403 		 * new process group
    404 		 */
    405 #ifdef DIAGNOSTIC
    406 		if (__predict_false(p->p_pid != pgid))
    407 			panic("enterpgrp: new pgrp and pid != pgid");
    408 #endif
    409 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    410 		if ((np = pfind(savepid)) == NULL || np != p) {
    411 			pool_put(&pgrp_pool, pgrp);
    412 			return (ESRCH);
    413 		}
    414 		if (mksess) {
    415 			struct session *sess;
    416 
    417 			/*
    418 			 * new session
    419 			 */
    420 			MALLOC(sess, struct session *, sizeof(struct session),
    421 			    M_SESSION, M_WAITOK);
    422 			if ((np = pfind(savepid)) == NULL || np != p) {
    423 				FREE(sess, M_SESSION);
    424 				pool_put(&pgrp_pool, pgrp);
    425 				return (ESRCH);
    426 			}
    427 			sess->s_sid = p->p_pid;
    428 			sess->s_leader = p;
    429 			sess->s_count = 1;
    430 			sess->s_ttyvp = NULL;
    431 			sess->s_ttyp = NULL;
    432 			memcpy(sess->s_login, p->p_session->s_login,
    433 			    sizeof(sess->s_login));
    434 			p->p_flag &= ~P_CONTROLT;
    435 			pgrp->pg_session = sess;
    436 #ifdef DIAGNOSTIC
    437 			if (__predict_false(p != curproc))
    438 				panic("enterpgrp: mksession and p != curlwp");
    439 #endif
    440 		} else {
    441 			SESSHOLD(p->p_session);
    442 			pgrp->pg_session = p->p_session;
    443 		}
    444 		pgrp->pg_id = pgid;
    445 		LIST_INIT(&pgrp->pg_members);
    446 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
    447 		pgrp->pg_jobc = 0;
    448 	} else if (pgrp == p->p_pgrp)
    449 		return (0);
    450 
    451 	/*
    452 	 * Adjust eligibility of affected pgrps to participate in job control.
    453 	 * Increment eligibility counts before decrementing, otherwise we
    454 	 * could reach 0 spuriously during the first call.
    455 	 */
    456 	fixjobc(p, pgrp, 1);
    457 	fixjobc(p, p->p_pgrp, 0);
    458 
    459 	LIST_REMOVE(p, p_pglist);
    460 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
    461 		pgdelete(p->p_pgrp);
    462 	p->p_pgrp = pgrp;
    463 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    464 	return (0);
    465 }
    466 
    467 /*
    468  * remove process from process group
    469  */
    470 int
    471 leavepgrp(p)
    472 	struct proc *p;
    473 {
    474 
    475 	LIST_REMOVE(p, p_pglist);
    476 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
    477 		pgdelete(p->p_pgrp);
    478 	p->p_pgrp = 0;
    479 	return (0);
    480 }
    481 
    482 /*
    483  * delete a process group
    484  */
    485 void
    486 pgdelete(pgrp)
    487 	struct pgrp *pgrp;
    488 {
    489 
    490 	/* Remove reference (if any) from tty to this process group */
    491 	if (pgrp->pg_session->s_ttyp != NULL &&
    492 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    493 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    494 	LIST_REMOVE(pgrp, pg_hash);
    495 	SESSRELE(pgrp->pg_session);
    496 	pool_put(&pgrp_pool, pgrp);
    497 }
    498 
    499 /*
    500  * Adjust pgrp jobc counters when specified process changes process group.
    501  * We count the number of processes in each process group that "qualify"
    502  * the group for terminal job control (those with a parent in a different
    503  * process group of the same session).  If that count reaches zero, the
    504  * process group becomes orphaned.  Check both the specified process'
    505  * process group and that of its children.
    506  * entering == 0 => p is leaving specified group.
    507  * entering == 1 => p is entering specified group.
    508  */
    509 void
    510 fixjobc(p, pgrp, entering)
    511 	struct proc *p;
    512 	struct pgrp *pgrp;
    513 	int entering;
    514 {
    515 	struct pgrp *hispgrp;
    516 	struct session *mysession = pgrp->pg_session;
    517 
    518 	/*
    519 	 * Check p's parent to see whether p qualifies its own process
    520 	 * group; if so, adjust count for p's process group.
    521 	 */
    522 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    523 	    hispgrp->pg_session == mysession) {
    524 		if (entering)
    525 			pgrp->pg_jobc++;
    526 		else if (--pgrp->pg_jobc == 0)
    527 			orphanpg(pgrp);
    528 	}
    529 
    530 	/*
    531 	 * Check this process' children to see whether they qualify
    532 	 * their process groups; if so, adjust counts for children's
    533 	 * process groups.
    534 	 */
    535 	LIST_FOREACH(p, &p->p_children, p_sibling) {
    536 		if ((hispgrp = p->p_pgrp) != pgrp &&
    537 		    hispgrp->pg_session == mysession &&
    538 		    P_ZOMBIE(p) == 0) {
    539 			if (entering)
    540 				hispgrp->pg_jobc++;
    541 			else if (--hispgrp->pg_jobc == 0)
    542 				orphanpg(hispgrp);
    543 		}
    544 	}
    545 }
    546 
    547 /*
    548  * A process group has become orphaned;
    549  * if there are any stopped processes in the group,
    550  * hang-up all process in that group.
    551  */
    552 static void
    553 orphanpg(pg)
    554 	struct pgrp *pg;
    555 {
    556 	struct proc *p;
    557 
    558 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    559 		if (p->p_stat == SSTOP) {
    560 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    561 				psignal(p, SIGHUP);
    562 				psignal(p, SIGCONT);
    563 			}
    564 			return;
    565 		}
    566 	}
    567 }
    568 
    569 /* mark process as suid/sgid, reset some values do defaults */
    570 void
    571 p_sugid(p)
    572 	struct proc *p;
    573 {
    574 	struct plimit *newlim;
    575 
    576 	p->p_flag |= P_SUGID;
    577 	/* reset what needs to be reset in plimit */
    578 	if (p->p_limit->pl_corename != defcorename) {
    579 		if (p->p_limit->p_refcnt > 1 &&
    580 		    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    581 			newlim = limcopy(p->p_limit);
    582 			limfree(p->p_limit);
    583 			p->p_limit = newlim;
    584 		}
    585 		free(p->p_limit->pl_corename, M_TEMP);
    586 		p->p_limit->pl_corename = defcorename;
    587 	}
    588 }
    589 
    590 #ifdef DEBUG
    591 void
    592 pgrpdump()
    593 {
    594 	struct pgrp *pgrp;
    595 	struct proc *p;
    596 	int i;
    597 
    598 	for (i = 0; i <= pgrphash; i++) {
    599 		if ((pgrp = LIST_FIRST(&pgrphashtbl[i])) != NULL) {
    600 			printf("\tindx %d\n", i);
    601 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
    602 				printf("\tpgrp %p, pgid %d, sess %p, "
    603 				    "sesscnt %d, mem %p\n",
    604 				    pgrp, pgrp->pg_id, pgrp->pg_session,
    605 				    pgrp->pg_session->s_count,
    606 				    LIST_FIRST(&pgrp->pg_members));
    607 				LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
    608 					printf("\t\tpid %d addr %p pgrp %p\n",
    609 					    p->p_pid, p, p->p_pgrp);
    610 				}
    611 			}
    612 		}
    613 	}
    614 }
    615 #endif /* DEBUG */
    616 
    617 #ifdef KSTACK_CHECK_MAGIC
    618 #include <sys/user.h>
    619 
    620 #define	KSTACK_MAGIC	0xdeadbeaf
    621 
    622 /* XXX should be per process basis? */
    623 int kstackleftmin = KSTACK_SIZE;
    624 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
    625 					  less than this */
    626 
    627 void
    628 kstack_setup_magic(const struct lwp *l)
    629 {
    630 	u_int32_t *ip;
    631 	u_int32_t const *end;
    632 
    633 	KASSERT(l != NULL);
    634 	KASSERT(l != &lwp0);
    635 
    636 	/*
    637 	 * fill all the stack with magic number
    638 	 * so that later modification on it can be detected.
    639 	 */
    640 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
    641 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
    642 	for (; ip < end; ip++) {
    643 		*ip = KSTACK_MAGIC;
    644 	}
    645 }
    646 
    647 void
    648 kstack_check_magic(const struct lwp *l)
    649 {
    650 	u_int32_t const *ip, *end;
    651 	int stackleft;
    652 
    653 	KASSERT(l != NULL);
    654 
    655 	/* don't check proc0 */ /*XXX*/
    656 	if (l == &lwp0)
    657 		return;
    658 
    659 #ifdef __MACHINE_STACK_GROWS_UP
    660 	/* stack grows upwards (eg. hppa) */
    661 	ip = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
    662 	end = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
    663 	for (ip--; ip >= end; ip--)
    664 		if (*ip != KSTACK_MAGIC)
    665 			break;
    666 
    667 	stackleft = (caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (caddr_t)ip;
    668 #else /* __MACHINE_STACK_GROWS_UP */
    669 	/* stack grows downwards (eg. i386) */
    670 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
    671 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
    672 	for (; ip < end; ip++)
    673 		if (*ip != KSTACK_MAGIC)
    674 			break;
    675 
    676 	stackleft = (caddr_t)ip - KSTACK_LOWEST_ADDR(l);
    677 #endif /* __MACHINE_STACK_GROWS_UP */
    678 
    679 	if (kstackleftmin > stackleft) {
    680 		kstackleftmin = stackleft;
    681 		if (stackleft < kstackleftthres)
    682 			printf("warning: kernel stack left %d bytes"
    683 			    "(pid %u:lid %u)\n", stackleft,
    684 			    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
    685 	}
    686 
    687 	if (stackleft <= 0) {
    688 		panic("magic on the top of kernel stack changed for "
    689 		    "pid %u, lid %u: maybe kernel stack overflow",
    690 		    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
    691 	}
    692 }
    693 #endif /* KSTACK_CHECK_MAGIC */
    694