Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.35.2.1
      1 /*	$NetBSD: kern_proc.c,v 1.35.2.1 2000/11/20 18:09:02 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
     73  */
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/map.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 
     94 /*
     95  * Structure associated with user cacheing.
     96  */
     97 struct uidinfo {
     98 	LIST_ENTRY(uidinfo) ui_hash;
     99 	uid_t	ui_uid;
    100 	long	ui_proccnt;
    101 };
    102 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
    103 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
    104 u_long uihash;		/* size of hash table - 1 */
    105 
    106 /*
    107  * Other process lists
    108  */
    109 struct pidhashhead *pidhashtbl;
    110 u_long pidhash;
    111 struct pgrphashhead *pgrphashtbl;
    112 u_long pgrphash;
    113 
    114 struct proclist allproc;
    115 struct proclist zombproc;	/* resources have been freed */
    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  *	pidhashtbl
    131  */
    132 struct lock proclist_lock;
    133 
    134 /*
    135  * Locking of this proclist is special; it's accessed in a
    136  * critical section of process exit, and thus locking it can't
    137  * modify interrupt state.  We use a simple spin lock for this
    138  * proclist.  Processes on this proclist are also on zombproc;
    139  * we use the p_hash member to linkup to deadproc.
    140  */
    141 struct simplelock deadproc_slock;
    142 struct proclist deadproc;	/* dead, but not yet undead */
    143 
    144 struct pool proc_pool;
    145 struct pool pcred_pool;
    146 struct pool plimit_pool;
    147 struct pool pgrp_pool;
    148 struct pool rusage_pool;
    149 
    150 /*
    151  * The process list descriptors, used during pid allocation and
    152  * by sysctl.  No locking on this data structure is needed since
    153  * it is completely static.
    154  */
    155 const struct proclist_desc proclists[] = {
    156 	{ &allproc	},
    157 	{ &zombproc	},
    158 	{ NULL		},
    159 };
    160 
    161 static void orphanpg __P((struct pgrp *));
    162 #ifdef DEBUG
    163 void pgrpdump __P((void));
    164 #endif
    165 
    166 /*
    167  * Initialize global process hashing structures.
    168  */
    169 void
    170 procinit()
    171 {
    172 	const struct proclist_desc *pd;
    173 
    174 	for (pd = proclists; pd->pd_list != NULL; pd++)
    175 		LIST_INIT(pd->pd_list);
    176 
    177 	spinlockinit(&proclist_lock, "proclk", 0);
    178 
    179 	LIST_INIT(&deadproc);
    180 	simple_lock_init(&deadproc_slock);
    181 
    182 	pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash);
    183 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash);
    184 	uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash);
    185 
    186 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    187 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_PROC);
    188 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    189 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_PGRP);
    190 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
    191 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    192 	pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    193 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    194 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
    195 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_ZOMBIE);
    196 }
    197 
    198 /*
    199  * Acquire a read lock on the proclist.
    200  */
    201 void
    202 proclist_lock_read()
    203 {
    204 	int error;
    205 
    206 	error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
    207 #ifdef DIAGNOSTIC
    208 	if (__predict_false(error != 0))
    209 		panic("proclist_lock_read: failed to acquire lock");
    210 #endif
    211 }
    212 
    213 /*
    214  * Release a read lock on the proclist.
    215  */
    216 void
    217 proclist_unlock_read()
    218 {
    219 
    220 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    221 }
    222 
    223 /*
    224  * Acquire a write lock on the proclist.
    225  */
    226 int
    227 proclist_lock_write()
    228 {
    229 	int s, error;
    230 
    231 	s = splclock();
    232 	error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
    233 #ifdef DIAGNOSTIC
    234 	if (__predict_false(error != 0))
    235 		panic("proclist_lock: failed to acquire lock");
    236 #endif
    237 	return (s);
    238 }
    239 
    240 /*
    241  * Release a write lock on the proclist.
    242  */
    243 void
    244 proclist_unlock_write(s)
    245 	int s;
    246 {
    247 
    248 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    249 	splx(s);
    250 }
    251 
    252 /*
    253  * Change the count associated with number of processes
    254  * a given user is using.
    255  */
    256 int
    257 chgproccnt(uid, diff)
    258 	uid_t	uid;
    259 	int	diff;
    260 {
    261 	struct uidinfo *uip;
    262 	struct uihashhead *uipp;
    263 
    264 	uipp = UIHASH(uid);
    265 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
    266 		if (uip->ui_uid == uid)
    267 			break;
    268 	if (uip) {
    269 		uip->ui_proccnt += diff;
    270 		if (uip->ui_proccnt > 0)
    271 			return (uip->ui_proccnt);
    272 		if (uip->ui_proccnt < 0)
    273 			panic("chgproccnt: procs < 0");
    274 		LIST_REMOVE(uip, ui_hash);
    275 		FREE(uip, M_PROC);
    276 		return (0);
    277 	}
    278 	if (diff <= 0) {
    279 		if (diff == 0)
    280 			return(0);
    281 		panic("chgproccnt: lost user");
    282 	}
    283 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    284 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    285 	uip->ui_uid = uid;
    286 	uip->ui_proccnt = diff;
    287 	return (diff);
    288 }
    289 
    290 /*
    291  * Is p an inferior of q?
    292  */
    293 int
    294 inferior(p, q)
    295 	struct proc *p;
    296 	struct proc *q;
    297 {
    298 
    299 	for (; p != q; p = p->p_pptr)
    300 		if (p->p_pid == 0)
    301 			return (0);
    302 	return (1);
    303 }
    304 
    305 /*
    306  * Locate a process by number
    307  */
    308 struct proc *
    309 pfind(pid)
    310 	pid_t pid;
    311 {
    312 	struct proc *p;
    313 
    314 	proclist_lock_read();
    315 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
    316 		if (p->p_pid == pid)
    317 			goto out;
    318  out:
    319 	proclist_unlock_read();
    320 	return (p);
    321 }
    322 
    323 /*
    324  * Locate a process group by number
    325  */
    326 struct pgrp *
    327 pgfind(pgid)
    328 	pid_t pgid;
    329 {
    330 	struct pgrp *pgrp;
    331 
    332 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
    333 		if (pgrp->pg_id == pgid)
    334 			return (pgrp);
    335 	return (NULL);
    336 }
    337 
    338 /*
    339  * Move p to a new or existing process group (and session)
    340  */
    341 int
    342 enterpgrp(p, pgid, mksess)
    343 	struct proc *p;
    344 	pid_t pgid;
    345 	int mksess;
    346 {
    347 	struct pgrp *pgrp = pgfind(pgid);
    348 
    349 #ifdef DIAGNOSTIC
    350 	if (__predict_false(pgrp != NULL && mksess))	/* firewalls */
    351 		panic("enterpgrp: setsid into non-empty pgrp");
    352 	if (__predict_false(SESS_LEADER(p)))
    353 		panic("enterpgrp: session leader attempted setpgrp");
    354 #endif
    355 	if (pgrp == NULL) {
    356 		pid_t savepid = p->p_pid;
    357 		struct proc *np;
    358 		/*
    359 		 * new process group
    360 		 */
    361 #ifdef DIAGNOSTIC
    362 		if (__predict_false(p->p_pid != pgid))
    363 			panic("enterpgrp: new pgrp and pid != pgid");
    364 #endif
    365 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    366 		if ((np = pfind(savepid)) == NULL || np != p)
    367 			return (ESRCH);
    368 		if (mksess) {
    369 			struct session *sess;
    370 
    371 			/*
    372 			 * new session
    373 			 */
    374 			MALLOC(sess, struct session *, sizeof(struct session),
    375 			    M_SESSION, M_WAITOK);
    376 			sess->s_sid = p->p_pid;
    377 			sess->s_leader = p;
    378 			sess->s_count = 1;
    379 			sess->s_ttyvp = NULL;
    380 			sess->s_ttyp = NULL;
    381 			memcpy(sess->s_login, p->p_session->s_login,
    382 			    sizeof(sess->s_login));
    383 			p->p_flag &= ~P_CONTROLT;
    384 			pgrp->pg_session = sess;
    385 #ifdef DIAGNOSTIC
    386 			if (__predict_false(p != curproc))
    387 				panic("enterpgrp: mksession and p != curproc");
    388 #endif
    389 		} else {
    390 			pgrp->pg_session = p->p_session;
    391 			pgrp->pg_session->s_count++;
    392 		}
    393 		pgrp->pg_id = pgid;
    394 		LIST_INIT(&pgrp->pg_members);
    395 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
    396 		pgrp->pg_jobc = 0;
    397 	} else if (pgrp == p->p_pgrp)
    398 		return (0);
    399 
    400 	/*
    401 	 * Adjust eligibility of affected pgrps to participate in job control.
    402 	 * Increment eligibility counts before decrementing, otherwise we
    403 	 * could reach 0 spuriously during the first call.
    404 	 */
    405 	fixjobc(p, pgrp, 1);
    406 	fixjobc(p, p->p_pgrp, 0);
    407 
    408 	LIST_REMOVE(p, p_pglist);
    409 	if (p->p_pgrp->pg_members.lh_first == 0)
    410 		pgdelete(p->p_pgrp);
    411 	p->p_pgrp = pgrp;
    412 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    413 	return (0);
    414 }
    415 
    416 /*
    417  * remove process from process group
    418  */
    419 int
    420 leavepgrp(p)
    421 	struct proc *p;
    422 {
    423 
    424 	LIST_REMOVE(p, p_pglist);
    425 	if (p->p_pgrp->pg_members.lh_first == 0)
    426 		pgdelete(p->p_pgrp);
    427 	p->p_pgrp = 0;
    428 	return (0);
    429 }
    430 
    431 /*
    432  * delete a process group
    433  */
    434 void
    435 pgdelete(pgrp)
    436 	struct pgrp *pgrp;
    437 {
    438 
    439 	if (pgrp->pg_session->s_ttyp != NULL &&
    440 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    441 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    442 	LIST_REMOVE(pgrp, pg_hash);
    443 	if (--pgrp->pg_session->s_count == 0)
    444 		FREE(pgrp->pg_session, M_SESSION);
    445 	pool_put(&pgrp_pool, pgrp);
    446 }
    447 
    448 /*
    449  * Adjust pgrp jobc counters when specified process changes process group.
    450  * We count the number of processes in each process group that "qualify"
    451  * the group for terminal job control (those with a parent in a different
    452  * process group of the same session).  If that count reaches zero, the
    453  * process group becomes orphaned.  Check both the specified process'
    454  * process group and that of its children.
    455  * entering == 0 => p is leaving specified group.
    456  * entering == 1 => p is entering specified group.
    457  */
    458 void
    459 fixjobc(p, pgrp, entering)
    460 	struct proc *p;
    461 	struct pgrp *pgrp;
    462 	int entering;
    463 {
    464 	struct pgrp *hispgrp;
    465 	struct session *mysession = pgrp->pg_session;
    466 
    467 	/*
    468 	 * Check p's parent to see whether p qualifies its own process
    469 	 * group; if so, adjust count for p's process group.
    470 	 */
    471 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    472 	    hispgrp->pg_session == mysession) {
    473 		if (entering)
    474 			pgrp->pg_jobc++;
    475 		else if (--pgrp->pg_jobc == 0)
    476 			orphanpg(pgrp);
    477 	}
    478 
    479 	/*
    480 	 * Check this process' children to see whether they qualify
    481 	 * their process groups; if so, adjust counts for children's
    482 	 * process groups.
    483 	 */
    484 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    485 		if ((hispgrp = p->p_pgrp) != pgrp &&
    486 		    hispgrp->pg_session == mysession &&
    487 		    P_ZOMBIE(p) == 0) {
    488 			if (entering)
    489 				hispgrp->pg_jobc++;
    490 			else if (--hispgrp->pg_jobc == 0)
    491 				orphanpg(hispgrp);
    492 		}
    493 	}
    494 }
    495 
    496 /*
    497  * A process group has become orphaned;
    498  * if there are any stopped processes in the group,
    499  * hang-up all process in that group.
    500  */
    501 static void
    502 orphanpg(pg)
    503 	struct pgrp *pg;
    504 {
    505 	struct proc *p;
    506 
    507 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    508 		if (p->p_stat == SSTOP) {
    509 			for (p = pg->pg_members.lh_first; p != 0;
    510 			    p = p->p_pglist.le_next) {
    511 				psignal(p, SIGHUP);
    512 				psignal(p, SIGCONT);
    513 			}
    514 			return;
    515 		}
    516 	}
    517 }
    518 
    519 /* mark process as suid/sgid, reset some values do defaults */
    520 void
    521 p_sugid(p)
    522 	struct proc *p;
    523 {
    524 	struct plimit *newlim;
    525 
    526 	p->p_flag |= P_SUGID;
    527 	/* reset what needs to be reset in plimit */
    528 	if (p->p_limit->pl_corename != defcorename) {
    529 		if (p->p_limit->p_refcnt > 1 &&
    530 		    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    531 			newlim = limcopy(p->p_limit);
    532 			limfree(p->p_limit);
    533 			p->p_limit = newlim;
    534 		} else {
    535 			free(p->p_limit->pl_corename, M_TEMP);
    536 		}
    537 		p->p_limit->pl_corename = defcorename;
    538 	}
    539 }
    540 
    541 
    542 #ifdef DEBUG
    543 void
    544 pgrpdump()
    545 {
    546 	struct pgrp *pgrp;
    547 	struct proc *p;
    548 	int i;
    549 
    550 	for (i = 0; i <= pgrphash; i++) {
    551 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
    552 			printf("\tindx %d\n", i);
    553 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
    554 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
    555 				    pgrp, pgrp->pg_id, pgrp->pg_session,
    556 				    pgrp->pg_session->s_count,
    557 				    pgrp->pg_members.lh_first);
    558 				for (p = pgrp->pg_members.lh_first; p != 0;
    559 				    p = p->p_pglist.le_next) {
    560 					printf("\t\tpid %d addr %p pgrp %p\n",
    561 					    p->p_pid, p, p->p_pgrp);
    562 				}
    563 			}
    564 		}
    565 	}
    566 }
    567 #endif /* DEBUG */
    568