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