Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.35
      1 /*	$NetBSD: kern_proc.c,v 1.35 1999/09/28 14:47:03 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 softclock interrupts (since
    123  * the interrupt context is the timeout-driven schedcpu()).
    124  *
    125  * The proclist lock locks the following structures:
    126  *
    127  *	allproc
    128  *	zombproc
    129  *	pidhashtbl
    130  */
    131 struct lock proclist_lock;
    132 
    133 /*
    134  * Locking of this proclist is special; it's accessed in a
    135  * critical section of process exit, and thus locking it can't
    136  * modify interrupt state.  We use a simple spin lock for this
    137  * proclist.  Processes on this proclist are also on zombproc;
    138  * we use the p_hash member to linkup to deadproc.
    139  */
    140 struct simplelock deadproc_slock;
    141 struct proclist deadproc;	/* dead, but not yet undead */
    142 
    143 struct pool proc_pool;
    144 struct pool pcred_pool;
    145 struct pool plimit_pool;
    146 struct pool pgrp_pool;
    147 struct pool rusage_pool;
    148 
    149 /*
    150  * The process list descriptors, used during pid allocation and
    151  * by sysctl.  No locking on this data structure is needed since
    152  * it is completely static.
    153  */
    154 const struct proclist_desc proclists[] = {
    155 	{ &allproc	},
    156 	{ &zombproc	},
    157 	{ NULL		},
    158 };
    159 
    160 static void orphanpg __P((struct pgrp *));
    161 #ifdef DEBUG
    162 void pgrpdump __P((void));
    163 #endif
    164 
    165 /*
    166  * Initialize global process hashing structures.
    167  */
    168 void
    169 procinit()
    170 {
    171 	const struct proclist_desc *pd;
    172 
    173 	for (pd = proclists; pd->pd_list != NULL; pd++)
    174 		LIST_INIT(pd->pd_list);
    175 
    176 	spinlockinit(&proclist_lock, "proclk", 0);
    177 
    178 	LIST_INIT(&deadproc);
    179 	simple_lock_init(&deadproc_slock);
    180 
    181 	pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash);
    182 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash);
    183 	uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash);
    184 
    185 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
    186 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_PROC);
    187 	pool_init(&pgrp_pool, sizeof(struct pgrp), 0, 0, 0, "pgrppl",
    188 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_PGRP);
    189 	pool_init(&pcred_pool, sizeof(struct pcred), 0, 0, 0, "pcredpl",
    190 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    191 	pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, 0, "plimitpl",
    192 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_SUBPROC);
    193 	pool_init(&rusage_pool, sizeof(struct rusage), 0, 0, 0, "rusgepl",
    194 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_ZOMBIE);
    195 }
    196 
    197 /*
    198  * Acquire a read lock on the proclist.
    199  */
    200 void
    201 proclist_lock_read()
    202 {
    203 	int error, s;
    204 
    205 	s = splstatclock();
    206 	error = spinlockmgr(&proclist_lock, LK_SHARED, NULL);
    207 #ifdef DIAGNOSTIC
    208 	if (error)
    209 		panic("proclist_lock_read: failed to acquire lock");
    210 #endif
    211 	splx(s);
    212 }
    213 
    214 /*
    215  * Release a read lock on the proclist.
    216  */
    217 void
    218 proclist_unlock_read()
    219 {
    220 	int s;
    221 
    222 	s = splstatclock();
    223 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    224 	splx(s);
    225 }
    226 
    227 /*
    228  * Acquire a write lock on the proclist.
    229  */
    230 int
    231 proclist_lock_write()
    232 {
    233 	int error, s;
    234 
    235 	s = splstatclock();
    236 	error = spinlockmgr(&proclist_lock, LK_EXCLUSIVE, NULL);
    237 #ifdef DIAGNOSTIC
    238 	if (error != 0)
    239 		panic("proclist_lock: failed to acquire lock");
    240 #endif
    241 	return (s);
    242 }
    243 
    244 /*
    245  * Release a write lock on the proclist.
    246  */
    247 void
    248 proclist_unlock_write(s)
    249 	int s;
    250 {
    251 
    252 	(void) spinlockmgr(&proclist_lock, LK_RELEASE, NULL);
    253 	splx(s);
    254 }
    255 
    256 /*
    257  * Change the count associated with number of processes
    258  * a given user is using.
    259  */
    260 int
    261 chgproccnt(uid, diff)
    262 	uid_t	uid;
    263 	int	diff;
    264 {
    265 	register struct uidinfo *uip;
    266 	register struct uihashhead *uipp;
    267 
    268 	uipp = UIHASH(uid);
    269 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
    270 		if (uip->ui_uid == uid)
    271 			break;
    272 	if (uip) {
    273 		uip->ui_proccnt += diff;
    274 		if (uip->ui_proccnt > 0)
    275 			return (uip->ui_proccnt);
    276 		if (uip->ui_proccnt < 0)
    277 			panic("chgproccnt: procs < 0");
    278 		LIST_REMOVE(uip, ui_hash);
    279 		FREE(uip, M_PROC);
    280 		return (0);
    281 	}
    282 	if (diff <= 0) {
    283 		if (diff == 0)
    284 			return(0);
    285 		panic("chgproccnt: lost user");
    286 	}
    287 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    288 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    289 	uip->ui_uid = uid;
    290 	uip->ui_proccnt = diff;
    291 	return (diff);
    292 }
    293 
    294 /*
    295  * Is p an inferior of the current process?
    296  */
    297 int
    298 inferior(p)
    299 	register struct proc *p;
    300 {
    301 
    302 	for (; p != curproc; p = p->p_pptr)
    303 		if (p->p_pid == 0)
    304 			return (0);
    305 	return (1);
    306 }
    307 
    308 /*
    309  * Locate a process by number
    310  */
    311 struct proc *
    312 pfind(pid)
    313 	register pid_t pid;
    314 {
    315 	struct proc *p;
    316 
    317 	proclist_lock_read();
    318 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
    319 		if (p->p_pid == pid)
    320 			goto out;
    321  out:
    322 	proclist_unlock_read();
    323 	return (p);
    324 }
    325 
    326 /*
    327  * Locate a process group by number
    328  */
    329 struct pgrp *
    330 pgfind(pgid)
    331 	register pid_t pgid;
    332 {
    333 	register struct pgrp *pgrp;
    334 
    335 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
    336 		if (pgrp->pg_id == pgid)
    337 			return (pgrp);
    338 	return (NULL);
    339 }
    340 
    341 /*
    342  * Move p to a new or existing process group (and session)
    343  */
    344 int
    345 enterpgrp(p, pgid, mksess)
    346 	register struct proc *p;
    347 	pid_t pgid;
    348 	int mksess;
    349 {
    350 	register struct pgrp *pgrp = pgfind(pgid);
    351 
    352 #ifdef DIAGNOSTIC
    353 	if (pgrp != NULL && mksess)	/* firewalls */
    354 		panic("enterpgrp: setsid into non-empty pgrp");
    355 	if (SESS_LEADER(p))
    356 		panic("enterpgrp: session leader attempted setpgrp");
    357 #endif
    358 	if (pgrp == NULL) {
    359 		pid_t savepid = p->p_pid;
    360 		struct proc *np;
    361 		/*
    362 		 * new process group
    363 		 */
    364 #ifdef DIAGNOSTIC
    365 		if (p->p_pid != pgid)
    366 			panic("enterpgrp: new pgrp and pid != pgid");
    367 #endif
    368 		pgrp = pool_get(&pgrp_pool, PR_WAITOK);
    369 		if ((np = pfind(savepid)) == NULL || np != p)
    370 			return (ESRCH);
    371 		if (mksess) {
    372 			register struct session *sess;
    373 
    374 			/*
    375 			 * new session
    376 			 */
    377 			MALLOC(sess, struct session *, sizeof(struct session),
    378 			    M_SESSION, M_WAITOK);
    379 			sess->s_sid = p->p_pid;
    380 			sess->s_leader = p;
    381 			sess->s_count = 1;
    382 			sess->s_ttyvp = NULL;
    383 			sess->s_ttyp = NULL;
    384 			memcpy(sess->s_login, p->p_session->s_login,
    385 			    sizeof(sess->s_login));
    386 			p->p_flag &= ~P_CONTROLT;
    387 			pgrp->pg_session = sess;
    388 #ifdef DIAGNOSTIC
    389 			if (p != curproc)
    390 				panic("enterpgrp: mksession and p != curproc");
    391 #endif
    392 		} else {
    393 			pgrp->pg_session = p->p_session;
    394 			pgrp->pg_session->s_count++;
    395 		}
    396 		pgrp->pg_id = pgid;
    397 		LIST_INIT(&pgrp->pg_members);
    398 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
    399 		pgrp->pg_jobc = 0;
    400 	} else if (pgrp == p->p_pgrp)
    401 		return (0);
    402 
    403 	/*
    404 	 * Adjust eligibility of affected pgrps to participate in job control.
    405 	 * Increment eligibility counts before decrementing, otherwise we
    406 	 * could reach 0 spuriously during the first call.
    407 	 */
    408 	fixjobc(p, pgrp, 1);
    409 	fixjobc(p, p->p_pgrp, 0);
    410 
    411 	LIST_REMOVE(p, p_pglist);
    412 	if (p->p_pgrp->pg_members.lh_first == 0)
    413 		pgdelete(p->p_pgrp);
    414 	p->p_pgrp = pgrp;
    415 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    416 	return (0);
    417 }
    418 
    419 /*
    420  * remove process from process group
    421  */
    422 int
    423 leavepgrp(p)
    424 	register struct proc *p;
    425 {
    426 
    427 	LIST_REMOVE(p, p_pglist);
    428 	if (p->p_pgrp->pg_members.lh_first == 0)
    429 		pgdelete(p->p_pgrp);
    430 	p->p_pgrp = 0;
    431 	return (0);
    432 }
    433 
    434 /*
    435  * delete a process group
    436  */
    437 void
    438 pgdelete(pgrp)
    439 	register struct pgrp *pgrp;
    440 {
    441 
    442 	if (pgrp->pg_session->s_ttyp != NULL &&
    443 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    444 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    445 	LIST_REMOVE(pgrp, pg_hash);
    446 	if (--pgrp->pg_session->s_count == 0)
    447 		FREE(pgrp->pg_session, M_SESSION);
    448 	pool_put(&pgrp_pool, pgrp);
    449 }
    450 
    451 /*
    452  * Adjust pgrp jobc counters when specified process changes process group.
    453  * We count the number of processes in each process group that "qualify"
    454  * the group for terminal job control (those with a parent in a different
    455  * process group of the same session).  If that count reaches zero, the
    456  * process group becomes orphaned.  Check both the specified process'
    457  * process group and that of its children.
    458  * entering == 0 => p is leaving specified group.
    459  * entering == 1 => p is entering specified group.
    460  */
    461 void
    462 fixjobc(p, pgrp, entering)
    463 	register struct proc *p;
    464 	register struct pgrp *pgrp;
    465 	int entering;
    466 {
    467 	register struct pgrp *hispgrp;
    468 	register struct session *mysession = pgrp->pg_session;
    469 
    470 	/*
    471 	 * Check p's parent to see whether p qualifies its own process
    472 	 * group; if so, adjust count for p's process group.
    473 	 */
    474 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    475 	    hispgrp->pg_session == mysession) {
    476 		if (entering)
    477 			pgrp->pg_jobc++;
    478 		else if (--pgrp->pg_jobc == 0)
    479 			orphanpg(pgrp);
    480 	}
    481 
    482 	/*
    483 	 * Check this process' children to see whether they qualify
    484 	 * their process groups; if so, adjust counts for children's
    485 	 * process groups.
    486 	 */
    487 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next) {
    488 		if ((hispgrp = p->p_pgrp) != pgrp &&
    489 		    hispgrp->pg_session == mysession &&
    490 		    P_ZOMBIE(p) == 0) {
    491 			if (entering)
    492 				hispgrp->pg_jobc++;
    493 			else if (--hispgrp->pg_jobc == 0)
    494 				orphanpg(hispgrp);
    495 		}
    496 	}
    497 }
    498 
    499 /*
    500  * A process group has become orphaned;
    501  * if there are any stopped processes in the group,
    502  * hang-up all process in that group.
    503  */
    504 static void
    505 orphanpg(pg)
    506 	struct pgrp *pg;
    507 {
    508 	register struct proc *p;
    509 
    510 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    511 		if (p->p_stat == SSTOP) {
    512 			for (p = pg->pg_members.lh_first; p != 0;
    513 			    p = p->p_pglist.le_next) {
    514 				psignal(p, SIGHUP);
    515 				psignal(p, SIGCONT);
    516 			}
    517 			return;
    518 		}
    519 	}
    520 }
    521 
    522 /* mark process as suid/sgid, reset some values do defaults */
    523 void
    524 p_sugid(p)
    525 	struct proc *p;
    526 {
    527 	struct plimit *newlim;
    528 
    529 	p->p_flag |= P_SUGID;
    530 	/* reset what needs to be reset in plimit */
    531 	if (p->p_limit->pl_corename != defcorename) {
    532 		if (p->p_limit->p_refcnt > 1 &&
    533 		    (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
    534 			newlim = limcopy(p->p_limit);
    535 			limfree(p->p_limit);
    536 			p->p_limit = newlim;
    537 		} else {
    538 			free(p->p_limit->pl_corename, M_TEMP);
    539 		}
    540 		p->p_limit->pl_corename = defcorename;
    541 	}
    542 }
    543 
    544 
    545 #ifdef DEBUG
    546 void
    547 pgrpdump()
    548 {
    549 	register struct pgrp *pgrp;
    550 	register struct proc *p;
    551 	register int i;
    552 
    553 	for (i = 0; i <= pgrphash; i++) {
    554 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
    555 			printf("\tindx %d\n", i);
    556 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
    557 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
    558 				    pgrp, pgrp->pg_id, pgrp->pg_session,
    559 				    pgrp->pg_session->s_count,
    560 				    pgrp->pg_members.lh_first);
    561 				for (p = pgrp->pg_members.lh_first; p != 0;
    562 				    p = p->p_pglist.le_next) {
    563 					printf("\t\tpid %d addr %p pgrp %p\n",
    564 					    p->p_pid, p, p->p_pgrp);
    565 				}
    566 			}
    567 		}
    568 	}
    569 }
    570 #endif /* DEBUG */
    571