Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.74
      1 /*	$NetBSD: kern_proc.c,v 1.74 2004/02/27 02:43:25 junyoung 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. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  *
     68  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
     69  */
     70 
     71 #include <sys/cdefs.h>
     72 __KERNEL_RCSID(0, "$NetBSD: kern_proc.c,v 1.74 2004/02/27 02:43:25 junyoung Exp $");
     73 
     74 #include "opt_kstack.h"
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.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 #include <sys/ras.h>
     94 #include <sys/sa.h>
     95 #include <sys/savar.h>
     96 
     97 /*
     98  * Structure associated with user caching.
     99  */
    100 struct uidinfo {
    101 	LIST_ENTRY(uidinfo) ui_hash;
    102 	uid_t	ui_uid;
    103 	long	ui_proccnt;
    104 };
    105 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
    106 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
    107 u_long uihash;		/* size of hash table - 1 */
    108 
    109 /*
    110  * Other process lists
    111  */
    112 
    113 struct proclist allproc;
    114 struct proclist zombproc;	/* resources have been freed */
    115 
    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 clock interrupts to
    123  * lock out any scheduling changes that may happen in interrupt
    124  * context.
    125  *
    126  * The proclist lock locks the following structures:
    127  *
    128  *	allproc
    129  *	zombproc
    130  *	pid_table
    131  */
    132 struct lock proclist_lock;
    133 
    134 /*
    135  * pid to proc lookup is done by indexing the pid_table array.
    136  * Since pid numbers are only allocated when an empty slot
    137  * has been found, there is no need to search any lists ever.
    138  * (an orphaned pgrp will lock the slot, a session will lock
    139  * the pgrp with the same number.)
    140  * If the table is too small it is reallocated with twice the
    141  * previous size and the entries 'unzipped' into the two halves.
    142  * A linked list of free entries is passed through the pt_proc
    143  * field of 'free' items - set odd to be an invalid ptr.
    144  */
    145 
    146 struct pid_table {
    147 	struct proc	*pt_proc;
    148 	struct pgrp	*pt_pgrp;
    149 };
    150 #if 1	/* strongly typed cast - should be a noop */
    151 static __inline uint p2u(struct proc *p) { return (uint)(uintptr_t)p; }
    152 #else
    153 #define p2u(p) ((uint)p)
    154 #endif
    155 #define P_VALID(p) (!(p2u(p) & 1))
    156 #define P_NEXT(p) (p2u(p) >> 1)
    157 #define P_FREE(pid) ((struct proc *)(uintptr_t)((pid) << 1 | 1))
    158 
    159 #define INITIAL_PID_TABLE_SIZE	(1 << 5)
    160 static struct pid_table *pid_table;
    161 static uint pid_tbl_mask = INITIAL_PID_TABLE_SIZE - 1;
    162 static uint pid_alloc_lim;	/* max we allocate before growing table */
    163 static uint pid_alloc_cnt;	/* number of allocated pids */
    164 
    165 /* links through free slots - never empty! */
    166 static uint next_free_pt, last_free_pt;
    167 static pid_t pid_max = PID_MAX;		/* largest value we allocate */
    168 
    169 struct pool proc_pool;
    170 struct pool lwp_pool;
    171 struct pool lwp_uc_pool;
    172 struct pool pcred_pool;
    173 struct pool plimit_pool;
    174 struct pool pstats_pool;
    175 struct pool pgrp_pool;
    176 struct pool rusage_pool;
    177 struct pool ras_pool;
    178 struct pool sadata_pool;
    179 struct pool saupcall_pool;
    180 struct pool sastack_pool;
    181 struct pool ptimer_pool;
    182 
    183 MALLOC_DEFINE(M_EMULDATA, "emuldata", "Per-process emulation data");
    184 MALLOC_DEFINE(M_PROC, "proc", "Proc structures");
    185 MALLOC_DEFINE(M_SESSION, "session", "session header");
    186 MALLOC_DEFINE(M_SUBPROC, "subproc", "Proc sub-structures");
    187 
    188 /*
    189  * The process list descriptors, used during pid allocation and
    190  * by sysctl.  No locking on this data structure is needed since
    191  * it is completely static.
    192  */
    193 const struct proclist_desc proclists[] = {
    194 	{ &allproc	},
    195 	{ &zombproc	},
    196 	{ NULL		},
    197 };
    198 
    199 static void orphanpg(struct pgrp *);
    200 static void pg_delete(pid_t);
    201 
    202 /*
    203  * Initialize global process hashing structures.
    204  */
    205 void
    206 procinit(void)
    207 {
    208 	const struct proclist_desc *pd;
    209 	int i;
    210 #define	LINK_EMPTY ((PID_MAX + INITIAL_PID_TABLE_SIZE) & ~(INITIAL_PID_TABLE_SIZE - 1))
    211 
    212 	for (pd = proclists; pd->pd_list != NULL; pd++)
    213 		LIST_INIT(pd->pd_list);
    214 
    215 	spinlockinit(&proclist_lock, "proclk", 0);
    216 
    217 	pid_table = malloc(INITIAL_PID_TABLE_SIZE * sizeof *pid_table,
    218 			    M_PROC, M_WAITOK);
    219 	/* Set free list running through table...
    220 	   Preset 'use count' above PID_MAX so we allocate pid 1 next. */
    221 	for (i = 0; i <= pid_tbl_mask; i++) {
    222 		pid_table[i].pt_proc = P_FREE(LINK_EMPTY + i + 1);
    223 		pid_table[i].pt_pgrp = 0;
    224 	}
    225 	/* slot 0 is just grabbed */
    226 	next_free_pt = 1;
    227 	/* Need to fix last entry. */
    228 	last_free_pt = pid_tbl_mask;
    229 	pid_table[last_free_pt].pt_proc = P_FREE(LINK_EMPTY);
    230 	/* point at which we grow table - to avoid reusing pids too often */
    231 	pid_alloc_lim = pid_tbl_mask - 1;
    232 #undef LINK_EMPTY
    233 
    234 	LIST_INIT(&alllwp);
    235 
    236 	uihashtbl =
    237 	    hashinit(maxproc / 16, HASH_LIST, M_PROC, M_WAITOK, &uihash);
    238 
    239 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    240 	    &pool_allocator_nointr);
    241 	pool_init(&lwp_pool, sizeof(struct lwp), 0, 0, 0, "lwppl",
    242 	    &pool_allocator_nointr);
    243 	pool_init(&lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
    244 	    &pool_allocator_nointr);
    245 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    246 	    &pool_allocator_nointr);
    247 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
    248 	    &pool_allocator_nointr);
    249 	pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    250 	    &pool_allocator_nointr);
    251 	pool_init(&pstats_pool, sizeof(struct pstats), 0, 0, 0, "pstatspl",
    252 	    &pool_allocator_nointr);
    253 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
    254 	    &pool_allocator_nointr);
    255 	pool_init(&ras_pool, sizeof(struct ras), 0, 0, 0, "raspl",
    256 	    &pool_allocator_nointr);
    257 	pool_init(&sadata_pool, sizeof(struct sadata), 0, 0, 0, "sadatapl",
    258 	    &pool_allocator_nointr);
    259 	pool_init(&saupcall_pool, sizeof(struct sadata_upcall), 0, 0, 0,
    260 	    "saupcpl", &pool_allocator_nointr);
    261 	pool_init(&sastack_pool, sizeof(struct sastack), 0, 0, 0, "sastackpl",
    262 	    &pool_allocator_nointr);
    263 	pool_init(&ptimer_pool, sizeof(struct ptimer), 0, 0, 0, "ptimerpl",
    264 	    &pool_allocator_nointr);
    265 }
    266 
    267 /*
    268  * Acquire a read lock on the proclist.
    269  */
    270 void
    271 proclist_lock_read(void)
    272 {
    273 	int error;
    274 
    275 	error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
    276 #ifdef DIAGNOSTIC
    277 	if (__predict_false(error != 0))
    278 		panic("proclist_lock_read: failed to acquire lock");
    279 #endif
    280 }
    281 
    282 /*
    283  * Release a read lock on the proclist.
    284  */
    285 void
    286 proclist_unlock_read(void)
    287 {
    288 
    289 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    290 }
    291 
    292 /*
    293  * Acquire a write lock on the proclist.
    294  */
    295 int
    296 proclist_lock_write(void)
    297 {
    298 	int s, error;
    299 
    300 	s = splclock();
    301 	error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
    302 #ifdef DIAGNOSTIC
    303 	if (__predict_false(error != 0))
    304 		panic("proclist_lock: failed to acquire lock");
    305 #endif
    306 	return (s);
    307 }
    308 
    309 /*
    310  * Release a write lock on the proclist.
    311  */
    312 void
    313 proclist_unlock_write(int s)
    314 {
    315 
    316 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    317 	splx(s);
    318 }
    319 
    320 /*
    321  * Change the count associated with number of processes
    322  * a given user is using.
    323  */
    324 int
    325 chgproccnt(uid_t uid, int diff)
    326 {
    327 	struct uidinfo *uip;
    328 	struct uihashhead *uipp;
    329 
    330 	uipp = UIHASH(uid);
    331 
    332 	LIST_FOREACH(uip, uipp, ui_hash)
    333 		if (uip->ui_uid == uid)
    334 			break;
    335 
    336 	if (uip) {
    337 		uip->ui_proccnt += diff;
    338 		if (uip->ui_proccnt > 0)
    339 			return (uip->ui_proccnt);
    340 		if (uip->ui_proccnt < 0)
    341 			panic("chgproccnt: procs < 0");
    342 		LIST_REMOVE(uip, ui_hash);
    343 		FREE(uip, M_PROC);
    344 		return (0);
    345 	}
    346 	if (diff <= 0) {
    347 		if (diff == 0)
    348 			return(0);
    349 		panic("chgproccnt: lost user");
    350 	}
    351 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    352 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    353 	uip->ui_uid = uid;
    354 	uip->ui_proccnt = diff;
    355 	return (diff);
    356 }
    357 
    358 /*
    359  * Check that the specified process group is in the session of the
    360  * specified process.
    361  * Treats -ve ids as process ids.
    362  * Used to validate TIOCSPGRP requests.
    363  */
    364 int
    365 pgid_in_session(struct proc *p, pid_t pg_id)
    366 {
    367 	struct pgrp *pgrp;
    368 
    369 	if (pg_id < 0) {
    370 		struct proc *p1 = pfind(-pg_id);
    371 		if (p1 == NULL)
    372 			return EINVAL;
    373 		pgrp = p1->p_pgrp;
    374 	} else {
    375 		pgrp = pgfind(pg_id);
    376 		if (pgrp == NULL)
    377 			return EINVAL;
    378 	}
    379 	if (pgrp->pg_session != p->p_pgrp->pg_session)
    380 		return EPERM;
    381 	return 0;
    382 }
    383 
    384 /*
    385  * Is p an inferior of q?
    386  */
    387 int
    388 inferior(struct proc *p, struct proc *q)
    389 {
    390 
    391 	for (; p != q; p = p->p_pptr)
    392 		if (p->p_pid == 0)
    393 			return (0);
    394 	return (1);
    395 }
    396 
    397 /*
    398  * Locate a process by number
    399  */
    400 struct proc *
    401 p_find(pid_t pid, uint flags)
    402 {
    403 	struct proc *p;
    404 	char stat;
    405 
    406 	if (!(flags & PFIND_LOCKED))
    407 		proclist_lock_read();
    408 	p = pid_table[pid & pid_tbl_mask].pt_proc;
    409 	/* Only allow live processes to be found by pid. */
    410 	if (P_VALID(p) && p->p_pid == pid &&
    411 	    ((stat = p->p_stat) == SACTIVE || stat == SSTOP
    412 		    || (stat == SZOMB && (flags & PFIND_ZOMBIE)))) {
    413 		if (flags & PFIND_UNLOCK_OK)
    414 			 proclist_unlock_read();
    415 		return p;
    416 	}
    417 	if (flags & PFIND_UNLOCK_FAIL)
    418 		 proclist_unlock_read();
    419 	return NULL;
    420 }
    421 
    422 
    423 /*
    424  * Locate a process group by number
    425  */
    426 struct pgrp *
    427 pg_find(pid_t pgid, uint flags)
    428 {
    429 	struct pgrp *pg;
    430 
    431 	if (!(flags & PFIND_LOCKED))
    432 		proclist_lock_read();
    433 	pg = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    434 	/*
    435 	 * Can't look up a pgrp that only exists because the session
    436 	 * hasn't died yet (traditional)
    437 	 */
    438 	if (pg == NULL || pg->pg_id != pgid || LIST_EMPTY(&pg->pg_members)) {
    439 		if (flags & PFIND_UNLOCK_FAIL)
    440 			 proclist_unlock_read();
    441 		return NULL;
    442 	}
    443 
    444 	if (flags & PFIND_UNLOCK_OK)
    445 		proclist_unlock_read();
    446 	return pg;
    447 }
    448 
    449 /*
    450  * Set entry for process 0
    451  */
    452 void
    453 proc0_insert(struct proc *p, struct lwp *l, struct pgrp *pgrp,
    454 	struct session *sess)
    455 {
    456 	int s;
    457 
    458 	simple_lock_init(&p->p_lock);
    459 	LIST_INIT(&p->p_lwps);
    460 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
    461 	p->p_nlwps = 1;
    462 	simple_lock_init(&p->p_sigctx.ps_silock);
    463 	CIRCLEQ_INIT(&p->p_sigctx.ps_siginfo);
    464 
    465 	s = proclist_lock_write();
    466 
    467 	pid_table[0].pt_proc = p;
    468 	LIST_INSERT_HEAD(&allproc, p, p_list);
    469 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    470 
    471 	p->p_pgrp = pgrp;
    472 	pid_table[0].pt_pgrp = pgrp;
    473 	LIST_INIT(&pgrp->pg_members);
    474 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    475 
    476 	pgrp->pg_session = sess;
    477 	sess->s_count = 1;
    478 	sess->s_sid = 0;
    479 	sess->s_leader = p;
    480 
    481 	proclist_unlock_write(s);
    482 }
    483 
    484 static void
    485 expand_pid_table(void)
    486 {
    487 	uint pt_size = pid_tbl_mask + 1;
    488 	struct pid_table *n_pt, *new_pt;
    489 	struct proc *proc;
    490 	struct pgrp *pgrp;
    491 	int i;
    492 	int s;
    493 	pid_t pid;
    494 
    495 	new_pt = malloc(pt_size * 2 * sizeof *new_pt, M_PROC, M_WAITOK);
    496 
    497 	s = proclist_lock_write();
    498 	if (pt_size != pid_tbl_mask + 1) {
    499 		/* Another process beat us to it... */
    500 		proclist_unlock_write(s);
    501 		FREE(new_pt, M_PROC);
    502 		return;
    503 	}
    504 
    505 	/*
    506 	 * Copy entries from old table into new one.
    507 	 * If 'pid' is 'odd' we need to place in the upper half,
    508 	 * even pid's to the lower half.
    509 	 * Free items stay in the low half so we don't have to
    510 	 * fixup the reference to them.
    511 	 * We stuff free items on the front of the freelist
    512 	 * because we can't write to unmodified entries.
    513 	 * Processing the table backwards maintains a semblance
    514 	 * of issueing pid numbers that increase with time.
    515 	 */
    516 	i = pt_size - 1;
    517 	n_pt = new_pt + i;
    518 	for (; ; i--, n_pt--) {
    519 		proc = pid_table[i].pt_proc;
    520 		pgrp = pid_table[i].pt_pgrp;
    521 		if (!P_VALID(proc)) {
    522 			/* Up 'use count' so that link is valid */
    523 			pid = (P_NEXT(proc) + pt_size) & ~pt_size;
    524 			proc = P_FREE(pid);
    525 			if (pgrp)
    526 				pid = pgrp->pg_id;
    527 		} else
    528 			pid = proc->p_pid;
    529 
    530 		/* Save entry in appropriate half of table */
    531 		n_pt[pid & pt_size].pt_proc = proc;
    532 		n_pt[pid & pt_size].pt_pgrp = pgrp;
    533 
    534 		/* Put other piece on start of free list */
    535 		pid = (pid ^ pt_size) & ~pid_tbl_mask;
    536 		n_pt[pid & pt_size].pt_proc =
    537 				    P_FREE((pid & ~pt_size) | next_free_pt);
    538 		n_pt[pid & pt_size].pt_pgrp = 0;
    539 		next_free_pt = i | (pid & pt_size);
    540 		if (i == 0)
    541 			break;
    542 	}
    543 
    544 	/* Switch tables */
    545 	n_pt = pid_table;
    546 	pid_table = new_pt;
    547 	pid_tbl_mask = pt_size * 2 - 1;
    548 
    549 	/*
    550 	 * pid_max starts as PID_MAX (= 30000), once we have 16384
    551 	 * allocated pids we need it to be larger!
    552 	 */
    553 	if (pid_tbl_mask > PID_MAX) {
    554 		pid_max = pid_tbl_mask * 2 + 1;
    555 		pid_alloc_lim |= pid_alloc_lim << 1;
    556 	} else
    557 		pid_alloc_lim <<= 1;	/* doubles number of free slots... */
    558 
    559 	proclist_unlock_write(s);
    560 	FREE(n_pt, M_PROC);
    561 }
    562 
    563 struct proc *
    564 proc_alloc(void)
    565 {
    566 	struct proc *p;
    567 	int s;
    568 	int nxt;
    569 	pid_t pid;
    570 	struct pid_table *pt;
    571 
    572 	p = pool_get(&proc_pool, PR_WAITOK);
    573 	p->p_stat = SIDL;			/* protect against others */
    574 
    575 	/* allocate next free pid */
    576 
    577 	for (;;expand_pid_table()) {
    578 		if (__predict_false(pid_alloc_cnt >= pid_alloc_lim))
    579 			/* ensure pids cycle through 2000+ values */
    580 			continue;
    581 		s = proclist_lock_write();
    582 		pt = &pid_table[next_free_pt];
    583 #ifdef DIAGNOSTIC
    584 		if (__predict_false(P_VALID(pt->pt_proc) || pt->pt_pgrp))
    585 			panic("proc_alloc: slot busy");
    586 #endif
    587 		nxt = P_NEXT(pt->pt_proc);
    588 		if (nxt & pid_tbl_mask)
    589 			break;
    590 		/* Table full - expand (NB last entry not used....) */
    591 		proclist_unlock_write(s);
    592 	}
    593 
    594 	/* pid is 'saved use count' + 'size' + entry */
    595 	pid = (nxt & ~pid_tbl_mask) + pid_tbl_mask + 1 + next_free_pt;
    596 	if ((uint)pid > (uint)pid_max)
    597 		pid &= pid_tbl_mask;
    598 	p->p_pid = pid;
    599 	next_free_pt = nxt & pid_tbl_mask;
    600 
    601 	/* Grab table slot */
    602 	pt->pt_proc = p;
    603 	pid_alloc_cnt++;
    604 
    605 	proclist_unlock_write(s);
    606 
    607 	return p;
    608 }
    609 
    610 /*
    611  * Free last resources of a process - called from proc_free (in kern_exit.c)
    612  */
    613 void
    614 proc_free_mem(struct proc *p)
    615 {
    616 	int s;
    617 	pid_t pid = p->p_pid;
    618 	struct pid_table *pt;
    619 
    620 	s = proclist_lock_write();
    621 
    622 	pt = &pid_table[pid & pid_tbl_mask];
    623 #ifdef DIAGNOSTIC
    624 	if (__predict_false(pt->pt_proc != p))
    625 		panic("proc_free: pid_table mismatch, pid %x, proc %p",
    626 			pid, p);
    627 #endif
    628 	/* save pid use count in slot */
    629 	pt->pt_proc = P_FREE(pid & ~pid_tbl_mask);
    630 
    631 	if (pt->pt_pgrp == NULL) {
    632 		/* link last freed entry onto ours */
    633 		pid &= pid_tbl_mask;
    634 		pt = &pid_table[last_free_pt];
    635 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pid);
    636 		last_free_pt = pid;
    637 		pid_alloc_cnt--;
    638 	}
    639 
    640 	nprocs--;
    641 	proclist_unlock_write(s);
    642 
    643 	pool_put(&proc_pool, p);
    644 }
    645 
    646 /*
    647  * Move p to a new or existing process group (and session)
    648  *
    649  * If we are creating a new pgrp, the pgid should equal
    650  * the calling process' pid.
    651  * If is only valid to enter a process group that is in the session
    652  * of the process.
    653  * Also mksess should only be set if we are creating a process group
    654  *
    655  * Only called from sys_setsid, sys_setpgid/sys_setpgrp and the
    656  * SYSV setpgrp support for hpux == enterpgrp(curproc, curproc->p_pid)
    657  */
    658 int
    659 enterpgrp(struct proc *p, pid_t pgid, int mksess)
    660 {
    661 	struct pgrp *new_pgrp, *pgrp;
    662 	struct session *sess;
    663 	struct proc *curp = curproc;
    664 	pid_t pid = p->p_pid;
    665 	int rval;
    666 	int s;
    667 	pid_t pg_id = NO_PGID;
    668 
    669 	/* Allocate data areas we might need before doing any validity checks */
    670 	proclist_lock_read();		/* Because pid_table might change */
    671 	if (pid_table[pgid & pid_tbl_mask].pt_pgrp == 0) {
    672 		proclist_unlock_read();
    673 		new_pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    674 	} else {
    675 		proclist_unlock_read();
    676 		new_pgrp = NULL;
    677 	}
    678 	if (mksess)
    679 		MALLOC(sess, struct session *, sizeof(struct session),
    680 			    M_SESSION, M_WAITOK);
    681 	else
    682 		sess = NULL;
    683 
    684 	s = proclist_lock_write();
    685 	rval = EPERM;	/* most common error (to save typing) */
    686 
    687 	/* Check pgrp exists or can be created */
    688 	pgrp = pid_table[pgid & pid_tbl_mask].pt_pgrp;
    689 	if (pgrp != NULL && pgrp->pg_id != pgid)
    690 		goto done;
    691 
    692 	/* Can only set another process under restricted circumstances. */
    693 	if (p != curp) {
    694 		/* must exist and be one of our children... */
    695 		if (p != pid_table[pid & pid_tbl_mask].pt_proc
    696 		    || !inferior(p, curp)) {
    697 			rval = ESRCH;
    698 			goto done;
    699 		}
    700 		/* ... in the same session... */
    701 		if (sess != NULL || p->p_session != curp->p_session)
    702 			goto done;
    703 		/* ... existing pgid must be in same session ... */
    704 		if (pgrp != NULL && pgrp->pg_session != p->p_session)
    705 			goto done;
    706 		/* ... and not done an exec. */
    707 		if (p->p_flag & P_EXEC) {
    708 			rval = EACCES;
    709 			goto done;
    710 		}
    711 	}
    712 
    713 	/* Changing the process group/session of a session
    714 	   leader is definitely off limits. */
    715 	if (SESS_LEADER(p)) {
    716 		if (sess == NULL && p->p_pgrp == pgrp)
    717 			/* unless it's a definite noop */
    718 			rval = 0;
    719 		goto done;
    720 	}
    721 
    722 	/* Can only create a process group with id of process */
    723 	if (pgrp == NULL && pgid != pid)
    724 		goto done;
    725 
    726 	/* Can only create a session if creating pgrp */
    727 	if (sess != NULL && pgrp != NULL)
    728 		goto done;
    729 
    730 	/* Check we allocated memory for a pgrp... */
    731 	if (pgrp == NULL && new_pgrp == NULL)
    732 		goto done;
    733 
    734 	/* Don't attach to 'zombie' pgrp */
    735 	if (pgrp != NULL && LIST_EMPTY(&pgrp->pg_members))
    736 		goto done;
    737 
    738 	/* Expect to succeed now */
    739 	rval = 0;
    740 
    741 	if (pgrp == p->p_pgrp)
    742 		/* nothing to do */
    743 		goto done;
    744 
    745 	/* Ok all setup, link up required structures */
    746 	if (pgrp == NULL) {
    747 		pgrp = new_pgrp;
    748 		new_pgrp = 0;
    749 		if (sess != NULL) {
    750 			sess->s_sid = p->p_pid;
    751 			sess->s_leader = p;
    752 			sess->s_count = 1;
    753 			sess->s_ttyvp = NULL;
    754 			sess->s_ttyp = NULL;
    755 			sess->s_flags = p->p_session->s_flags & ~S_LOGIN_SET;
    756 			memcpy(sess->s_login, p->p_session->s_login,
    757 			    sizeof(sess->s_login));
    758 			p->p_flag &= ~P_CONTROLT;
    759 		} else {
    760 			sess = p->p_pgrp->pg_session;
    761 			SESSHOLD(sess);
    762 		}
    763 		pgrp->pg_session = sess;
    764 		sess = 0;
    765 
    766 		pgrp->pg_id = pgid;
    767 		LIST_INIT(&pgrp->pg_members);
    768 #ifdef DIAGNOSTIC
    769 		if (__predict_false(pid_table[pgid & pid_tbl_mask].pt_pgrp))
    770 			panic("enterpgrp: pgrp table slot in use");
    771 		if (__predict_false(mksess && p != curp))
    772 			panic("enterpgrp: mksession and p != curproc");
    773 #endif
    774 		pid_table[pgid & pid_tbl_mask].pt_pgrp = pgrp;
    775 		pgrp->pg_jobc = 0;
    776 	}
    777 
    778 	/*
    779 	 * Adjust eligibility of affected pgrps to participate in job control.
    780 	 * Increment eligibility counts before decrementing, otherwise we
    781 	 * could reach 0 spuriously during the first call.
    782 	 */
    783 	fixjobc(p, pgrp, 1);
    784 	fixjobc(p, p->p_pgrp, 0);
    785 
    786 	/* Move process to requested group */
    787 	LIST_REMOVE(p, p_pglist);
    788 	if (LIST_EMPTY(&p->p_pgrp->pg_members))
    789 		/* defer delete until we've dumped the lock */
    790 		pg_id = p->p_pgrp->pg_id;
    791 	p->p_pgrp = pgrp;
    792 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    793 
    794     done:
    795 	proclist_unlock_write(s);
    796 	if (sess != NULL)
    797 		free(sess, M_SESSION);
    798 	if (new_pgrp != NULL)
    799 		pool_put(&pgrp_pool, new_pgrp);
    800 	if (pg_id != NO_PGID)
    801 		pg_delete(pg_id);
    802 #ifdef DEBUG_PGRP
    803 	if (__predict_false(rval))
    804 		printf("enterpgrp(%d,%d,%d), curproc %d, rval %d\n",
    805 			pid, pgid, mksess, curp->p_pid, rval);
    806 #endif
    807 	return rval;
    808 }
    809 
    810 /*
    811  * remove process from process group
    812  */
    813 int
    814 leavepgrp(struct proc *p)
    815 {
    816 	int s;
    817 	struct pgrp *pgrp;
    818 	pid_t pg_id;
    819 
    820 	s = proclist_lock_write();
    821 	pgrp = p->p_pgrp;
    822 	LIST_REMOVE(p, p_pglist);
    823 	p->p_pgrp = 0;
    824 	pg_id = LIST_EMPTY(&pgrp->pg_members) ? pgrp->pg_id : NO_PGID;
    825 	proclist_unlock_write(s);
    826 
    827 	if (pg_id != NO_PGID)
    828 		pg_delete(pg_id);
    829 	return 0;
    830 }
    831 
    832 static void
    833 pg_free(pid_t pg_id)
    834 {
    835 	struct pgrp *pgrp;
    836 	struct pid_table *pt;
    837 	int s;
    838 
    839 	s = proclist_lock_write();
    840 	pt = &pid_table[pg_id & pid_tbl_mask];
    841 	pgrp = pt->pt_pgrp;
    842 #ifdef DIAGNOSTIC
    843 	if (__predict_false(!pgrp || pgrp->pg_id != pg_id
    844 	    || !LIST_EMPTY(&pgrp->pg_members)))
    845 		panic("pg_free: process group absent or has members");
    846 #endif
    847 	pt->pt_pgrp = 0;
    848 
    849 	if (!P_VALID(pt->pt_proc)) {
    850 		/* orphaned pgrp, put slot onto free list */
    851 #ifdef DIAGNOSTIC
    852 		if (__predict_false(P_NEXT(pt->pt_proc) & pid_tbl_mask))
    853 			panic("pg_free: process slot on free list");
    854 #endif
    855 
    856 		pg_id &= pid_tbl_mask;
    857 		pt = &pid_table[last_free_pt];
    858 		pt->pt_proc = P_FREE(P_NEXT(pt->pt_proc) | pg_id);
    859 		last_free_pt = pg_id;
    860 		pid_alloc_cnt--;
    861 	}
    862 	proclist_unlock_write(s);
    863 
    864 	pool_put(&pgrp_pool, pgrp);
    865 }
    866 
    867 /*
    868  * delete a process group
    869  */
    870 static void
    871 pg_delete(pid_t pg_id)
    872 {
    873 	struct pgrp *pgrp;
    874 	struct tty *ttyp;
    875 	struct session *ss;
    876 	int s, is_pgrp_leader;
    877 
    878 	s = proclist_lock_write();
    879 	pgrp = pid_table[pg_id & pid_tbl_mask].pt_pgrp;
    880 	if (pgrp == NULL || pgrp->pg_id != pg_id ||
    881 	    !LIST_EMPTY(&pgrp->pg_members)) {
    882 		proclist_unlock_write(s);
    883 		return;
    884 	}
    885 
    886 	ss = pgrp->pg_session;
    887 
    888 	/* Remove reference (if any) from tty to this process group */
    889 	ttyp = ss->s_ttyp;
    890 	if (ttyp != NULL && ttyp->t_pgrp == pgrp) {
    891 		ttyp->t_pgrp = NULL;
    892 #ifdef DIAGNOSTIC
    893 		if (ttyp->t_session != ss)
    894 			panic("pg_delete: wrong session on terminal");
    895 #endif
    896 	}
    897 
    898 	/*
    899 	 * The leading process group in a session is freed
    900 	 * by sessdelete() if last reference.
    901 	 */
    902 	is_pgrp_leader = (ss->s_sid == pgrp->pg_id);
    903 	proclist_unlock_write(s);
    904 	SESSRELE(ss);
    905 
    906 	if (is_pgrp_leader)
    907 		return;
    908 
    909 	pg_free(pg_id);
    910 }
    911 
    912 /*
    913  * Delete session - called from SESSRELE when s_count becomes zero.
    914  */
    915 void
    916 sessdelete(struct session *ss)
    917 {
    918 	/*
    919 	 * We keep the pgrp with the same id as the session in
    920 	 * order to stop a process being given the same pid.
    921 	 * Since the pgrp holds a reference to the session, it
    922 	 * must be a 'zombie' pgrp by now.
    923 	 */
    924 
    925 	pg_free(ss->s_sid);
    926 
    927 	FREE(ss, M_SESSION);
    928 }
    929 
    930 /*
    931  * Adjust pgrp jobc counters when specified process changes process group.
    932  * We count the number of processes in each process group that "qualify"
    933  * the group for terminal job control (those with a parent in a different
    934  * process group of the same session).  If that count reaches zero, the
    935  * process group becomes orphaned.  Check both the specified process'
    936  * process group and that of its children.
    937  * entering == 0 => p is leaving specified group.
    938  * entering == 1 => p is entering specified group.
    939  *
    940  * Call with proclist_lock held.
    941  */
    942 void
    943 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
    944 {
    945 	struct pgrp *hispgrp;
    946 	struct session *mysession = pgrp->pg_session;
    947 	struct proc *child;
    948 
    949 	/*
    950 	 * Check p's parent to see whether p qualifies its own process
    951 	 * group; if so, adjust count for p's process group.
    952 	 */
    953 	hispgrp = p->p_pptr->p_pgrp;
    954 	if (hispgrp != pgrp && hispgrp->pg_session == mysession) {
    955 		if (entering)
    956 			pgrp->pg_jobc++;
    957 		else if (--pgrp->pg_jobc == 0)
    958 			orphanpg(pgrp);
    959 	}
    960 
    961 	/*
    962 	 * Check this process' children to see whether they qualify
    963 	 * their process groups; if so, adjust counts for children's
    964 	 * process groups.
    965 	 */
    966 	LIST_FOREACH(child, &p->p_children, p_sibling) {
    967 		hispgrp = child->p_pgrp;
    968 		if (hispgrp != pgrp && hispgrp->pg_session == mysession &&
    969 		    !P_ZOMBIE(child)) {
    970 			if (entering)
    971 				hispgrp->pg_jobc++;
    972 			else if (--hispgrp->pg_jobc == 0)
    973 				orphanpg(hispgrp);
    974 		}
    975 	}
    976 }
    977 
    978 /*
    979  * A process group has become orphaned;
    980  * if there are any stopped processes in the group,
    981  * hang-up all process in that group.
    982  *
    983  * Call with proclist_lock held.
    984  */
    985 static void
    986 orphanpg(struct pgrp *pg)
    987 {
    988 	struct proc *p;
    989 
    990 	LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    991 		if (p->p_stat == SSTOP) {
    992 			LIST_FOREACH(p, &pg->pg_members, p_pglist) {
    993 				psignal(p, SIGHUP);
    994 				psignal(p, SIGCONT);
    995 			}
    996 			return;
    997 		}
    998 	}
    999 }
   1000 
   1001 /* mark process as suid/sgid, reset some values to defaults */
   1002 void
   1003 p_sugid(struct proc *p)
   1004 {
   1005 	struct plimit *newlim;
   1006 
   1007 	p->p_flag |= P_SUGID;
   1008 	/* reset what needs to be reset in plimit */
   1009 	if (p->p_limit->pl_corename != defcorename) {
   1010 		if (p->p_limit->p_refcnt > 1 &&
   1011 		    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
   1012 			newlim = limcopy(p->p_limit);
   1013 			limfree(p->p_limit);
   1014 			p->p_limit = newlim;
   1015 		}
   1016 		free(p->p_limit->pl_corename, M_TEMP);
   1017 		p->p_limit->pl_corename = defcorename;
   1018 	}
   1019 }
   1020 
   1021 #ifdef DDB
   1022 #include <ddb/db_output.h>
   1023 void pidtbl_dump(void);
   1024 void
   1025 pidtbl_dump(void)
   1026 {
   1027 	struct pid_table *pt;
   1028 	struct proc *p;
   1029 	struct pgrp *pgrp;
   1030 	int id;
   1031 
   1032 	db_printf("pid table %p size %x, next %x, last %x\n",
   1033 		pid_table, pid_tbl_mask+1,
   1034 		next_free_pt, last_free_pt);
   1035 	for (pt = pid_table, id = 0; id <= pid_tbl_mask; id++, pt++) {
   1036 		p = pt->pt_proc;
   1037 		if (!P_VALID(p) && !pt->pt_pgrp)
   1038 			continue;
   1039 		db_printf("  id %x: ", id);
   1040 		if (P_VALID(p))
   1041 			db_printf("proc %p id %d (0x%x) %s\n",
   1042 				p, p->p_pid, p->p_pid, p->p_comm);
   1043 		else
   1044 			db_printf("next %x use %x\n",
   1045 				P_NEXT(p) & pid_tbl_mask,
   1046 				P_NEXT(p) & ~pid_tbl_mask);
   1047 		if ((pgrp = pt->pt_pgrp)) {
   1048 			db_printf("\tsession %p, sid %d, count %d, login %s\n",
   1049 			    pgrp->pg_session, pgrp->pg_session->s_sid,
   1050 			    pgrp->pg_session->s_count,
   1051 			    pgrp->pg_session->s_login);
   1052 			db_printf("\tpgrp %p, pg_id %d, pg_jobc %d, members %p\n",
   1053 			    pgrp, pgrp->pg_id, pgrp->pg_jobc,
   1054 			    pgrp->pg_members.lh_first);
   1055 			for (p = pgrp->pg_members.lh_first; p != 0;
   1056 			    p = p->p_pglist.le_next) {
   1057 				db_printf("\t\tpid %d addr %p pgrp %p %s\n",
   1058 				    p->p_pid, p, p->p_pgrp, p->p_comm);
   1059 			}
   1060 		}
   1061 	}
   1062 }
   1063 #endif /* DDB */
   1064 
   1065 #ifdef KSTACK_CHECK_MAGIC
   1066 #include <sys/user.h>
   1067 
   1068 #define	KSTACK_MAGIC	0xdeadbeaf
   1069 
   1070 /* XXX should be per process basis? */
   1071 int kstackleftmin = KSTACK_SIZE;
   1072 int kstackleftthres = KSTACK_SIZE / 8; /* warn if remaining stack is
   1073 					  less than this */
   1074 
   1075 void
   1076 kstack_setup_magic(const struct lwp *l)
   1077 {
   1078 	u_int32_t *ip;
   1079 	u_int32_t const *end;
   1080 
   1081 	KASSERT(l != NULL);
   1082 	KASSERT(l != &lwp0);
   1083 
   1084 	/*
   1085 	 * fill all the stack with magic number
   1086 	 * so that later modification on it can be detected.
   1087 	 */
   1088 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
   1089 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1090 	for (; ip < end; ip++) {
   1091 		*ip = KSTACK_MAGIC;
   1092 	}
   1093 }
   1094 
   1095 void
   1096 kstack_check_magic(const struct lwp *l)
   1097 {
   1098 	u_int32_t const *ip, *end;
   1099 	int stackleft;
   1100 
   1101 	KASSERT(l != NULL);
   1102 
   1103 	/* don't check proc0 */ /*XXX*/
   1104 	if (l == &lwp0)
   1105 		return;
   1106 
   1107 #ifdef __MACHINE_STACK_GROWS_UP
   1108 	/* stack grows upwards (eg. hppa) */
   1109 	ip = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1110 	end = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
   1111 	for (ip--; ip >= end; ip--)
   1112 		if (*ip != KSTACK_MAGIC)
   1113 			break;
   1114 
   1115 	stackleft = (caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE - (caddr_t)ip;
   1116 #else /* __MACHINE_STACK_GROWS_UP */
   1117 	/* stack grows downwards (eg. i386) */
   1118 	ip = (u_int32_t *)KSTACK_LOWEST_ADDR(l);
   1119 	end = (u_int32_t *)((caddr_t)KSTACK_LOWEST_ADDR(l) + KSTACK_SIZE);
   1120 	for (; ip < end; ip++)
   1121 		if (*ip != KSTACK_MAGIC)
   1122 			break;
   1123 
   1124 	stackleft = (caddr_t)ip - KSTACK_LOWEST_ADDR(l);
   1125 #endif /* __MACHINE_STACK_GROWS_UP */
   1126 
   1127 	if (kstackleftmin > stackleft) {
   1128 		kstackleftmin = stackleft;
   1129 		if (stackleft < kstackleftthres)
   1130 			printf("warning: kernel stack left %d bytes"
   1131 			    "(pid %u:lid %u)\n", stackleft,
   1132 			    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1133 	}
   1134 
   1135 	if (stackleft <= 0) {
   1136 		panic("magic on the top of kernel stack changed for "
   1137 		    "pid %u, lid %u: maybe kernel stack overflow",
   1138 		    (u_int)l->l_proc->p_pid, (u_int)l->l_lid);
   1139 	}
   1140 }
   1141 #endif /* KSTACK_CHECK_MAGIC */
   1142