Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.24
      1 /*	$NetBSD: kern_proc.c,v 1.24 1998/08/02 04:41:32 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)kern_proc.c	8.7 (Berkeley) 2/14/95
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/map.h>
     41 #include <sys/kernel.h>
     42 #include <sys/proc.h>
     43 #include <sys/buf.h>
     44 #include <sys/acct.h>
     45 #include <sys/wait.h>
     46 #include <sys/file.h>
     47 #include <ufs/ufs/quota.h>
     48 #include <sys/uio.h>
     49 #include <sys/malloc.h>
     50 #include <sys/pool.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/tty.h>
     54 #include <sys/signalvar.h>
     55 
     56 /*
     57  * Structure associated with user cacheing.
     58  */
     59 struct uidinfo {
     60 	LIST_ENTRY(uidinfo) ui_hash;
     61 	uid_t	ui_uid;
     62 	long	ui_proccnt;
     63 };
     64 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
     65 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
     66 u_long uihash;		/* size of hash table - 1 */
     67 
     68 /*
     69  * Other process lists
     70  */
     71 struct pidhashhead *pidhashtbl;
     72 u_long pidhash;
     73 struct pgrphashhead *pgrphashtbl;
     74 u_long pgrphash;
     75 struct proclist allproc;
     76 struct proclist zombproc;
     77 struct pool proc_pool;
     78 
     79 static void orphanpg __P((struct pgrp *));
     80 #ifdef DEBUG
     81 void pgrpdump __P((void));
     82 #endif
     83 
     84 /*
     85  * Initialize global process hashing structures.
     86  */
     87 void
     88 procinit()
     89 {
     90 
     91 	LIST_INIT(&allproc);
     92 	LIST_INIT(&zombproc);
     93 	pidhashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pidhash);
     94 	pgrphashtbl = hashinit(maxproc / 4, M_PROC, M_WAITOK, &pgrphash);
     95 	uihashtbl = hashinit(maxproc / 16, M_PROC, M_WAITOK, &uihash);
     96 	pool_init(&proc_pool, sizeof(struct proc), 0, 0, 0, "procpl",
     97 	    0, NULL, NULL, M_PROC);
     98 }
     99 
    100 /*
    101  * Change the count associated with number of processes
    102  * a given user is using.
    103  */
    104 int
    105 chgproccnt(uid, diff)
    106 	uid_t	uid;
    107 	int	diff;
    108 {
    109 	register struct uidinfo *uip;
    110 	register struct uihashhead *uipp;
    111 
    112 	uipp = UIHASH(uid);
    113 	for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
    114 		if (uip->ui_uid == uid)
    115 			break;
    116 	if (uip) {
    117 		uip->ui_proccnt += diff;
    118 		if (uip->ui_proccnt > 0)
    119 			return (uip->ui_proccnt);
    120 		if (uip->ui_proccnt < 0)
    121 			panic("chgproccnt: procs < 0");
    122 		LIST_REMOVE(uip, ui_hash);
    123 		FREE(uip, M_PROC);
    124 		return (0);
    125 	}
    126 	if (diff <= 0) {
    127 		if (diff == 0)
    128 			return(0);
    129 		panic("chgproccnt: lost user");
    130 	}
    131 	MALLOC(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
    132 	LIST_INSERT_HEAD(uipp, uip, ui_hash);
    133 	uip->ui_uid = uid;
    134 	uip->ui_proccnt = diff;
    135 	return (diff);
    136 }
    137 
    138 /*
    139  * Is p an inferior of the current process?
    140  */
    141 int
    142 inferior(p)
    143 	register struct proc *p;
    144 {
    145 
    146 	for (; p != curproc; p = p->p_pptr)
    147 		if (p->p_pid == 0)
    148 			return (0);
    149 	return (1);
    150 }
    151 
    152 /*
    153  * Locate a process by number
    154  */
    155 struct proc *
    156 pfind(pid)
    157 	register pid_t pid;
    158 {
    159 	register struct proc *p;
    160 
    161 	for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
    162 		if (p->p_pid == pid)
    163 			return (p);
    164 	return (NULL);
    165 }
    166 
    167 /*
    168  * Locate a process group by number
    169  */
    170 struct pgrp *
    171 pgfind(pgid)
    172 	register pid_t pgid;
    173 {
    174 	register struct pgrp *pgrp;
    175 
    176 	for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
    177 		if (pgrp->pg_id == pgid)
    178 			return (pgrp);
    179 	return (NULL);
    180 }
    181 
    182 /*
    183  * Move p to a new or existing process group (and session)
    184  */
    185 int
    186 enterpgrp(p, pgid, mksess)
    187 	register struct proc *p;
    188 	pid_t pgid;
    189 	int mksess;
    190 {
    191 	register struct pgrp *pgrp = pgfind(pgid);
    192 
    193 #ifdef DIAGNOSTIC
    194 	if (pgrp != NULL && mksess)	/* firewalls */
    195 		panic("enterpgrp: setsid into non-empty pgrp");
    196 	if (SESS_LEADER(p))
    197 		panic("enterpgrp: session leader attempted setpgrp");
    198 #endif
    199 	if (pgrp == NULL) {
    200 		pid_t savepid = p->p_pid;
    201 		struct proc *np;
    202 		/*
    203 		 * new process group
    204 		 */
    205 #ifdef DIAGNOSTIC
    206 		if (p->p_pid != pgid)
    207 			panic("enterpgrp: new pgrp and pid != pgid");
    208 #endif
    209 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
    210 		    M_WAITOK);
    211 		if ((np = pfind(savepid)) == NULL || np != p)
    212 			return (ESRCH);
    213 		if (mksess) {
    214 			register struct session *sess;
    215 
    216 			/*
    217 			 * new session
    218 			 */
    219 			MALLOC(sess, struct session *, sizeof(struct session),
    220 			    M_SESSION, M_WAITOK);
    221 			sess->s_sid = p->p_pid;
    222 			sess->s_leader = p;
    223 			sess->s_count = 1;
    224 			sess->s_ttyvp = NULL;
    225 			sess->s_ttyp = NULL;
    226 			bcopy(p->p_session->s_login, sess->s_login,
    227 			    sizeof(sess->s_login));
    228 			p->p_flag &= ~P_CONTROLT;
    229 			pgrp->pg_session = sess;
    230 #ifdef DIAGNOSTIC
    231 			if (p != curproc)
    232 				panic("enterpgrp: mksession and p != curproc");
    233 #endif
    234 		} else {
    235 			pgrp->pg_session = p->p_session;
    236 			pgrp->pg_session->s_count++;
    237 		}
    238 		pgrp->pg_id = pgid;
    239 		LIST_INIT(&pgrp->pg_members);
    240 		LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
    241 		pgrp->pg_jobc = 0;
    242 	} else if (pgrp == p->p_pgrp)
    243 		return (0);
    244 
    245 	/*
    246 	 * Adjust eligibility of affected pgrps to participate in job control.
    247 	 * Increment eligibility counts before decrementing, otherwise we
    248 	 * could reach 0 spuriously during the first call.
    249 	 */
    250 	fixjobc(p, pgrp, 1);
    251 	fixjobc(p, p->p_pgrp, 0);
    252 
    253 	LIST_REMOVE(p, p_pglist);
    254 	if (p->p_pgrp->pg_members.lh_first == 0)
    255 		pgdelete(p->p_pgrp);
    256 	p->p_pgrp = pgrp;
    257 	LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
    258 	return (0);
    259 }
    260 
    261 /*
    262  * remove process from process group
    263  */
    264 int
    265 leavepgrp(p)
    266 	register struct proc *p;
    267 {
    268 
    269 	LIST_REMOVE(p, p_pglist);
    270 	if (p->p_pgrp->pg_members.lh_first == 0)
    271 		pgdelete(p->p_pgrp);
    272 	p->p_pgrp = 0;
    273 	return (0);
    274 }
    275 
    276 /*
    277  * delete a process group
    278  */
    279 void
    280 pgdelete(pgrp)
    281 	register struct pgrp *pgrp;
    282 {
    283 
    284 	if (pgrp->pg_session->s_ttyp != NULL &&
    285 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    286 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    287 	LIST_REMOVE(pgrp, pg_hash);
    288 	if (--pgrp->pg_session->s_count == 0)
    289 		FREE(pgrp->pg_session, M_SESSION);
    290 	FREE(pgrp, M_PGRP);
    291 }
    292 
    293 /*
    294  * Adjust pgrp jobc counters when specified process changes process group.
    295  * We count the number of processes in each process group that "qualify"
    296  * the group for terminal job control (those with a parent in a different
    297  * process group of the same session).  If that count reaches zero, the
    298  * process group becomes orphaned.  Check both the specified process'
    299  * process group and that of its children.
    300  * entering == 0 => p is leaving specified group.
    301  * entering == 1 => p is entering specified group.
    302  */
    303 void
    304 fixjobc(p, pgrp, entering)
    305 	register struct proc *p;
    306 	register struct pgrp *pgrp;
    307 	int entering;
    308 {
    309 	register struct pgrp *hispgrp;
    310 	register struct session *mysession = pgrp->pg_session;
    311 
    312 	/*
    313 	 * Check p's parent to see whether p qualifies its own process
    314 	 * group; if so, adjust count for p's process group.
    315 	 */
    316 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    317 	    hispgrp->pg_session == mysession)
    318 		if (entering)
    319 			pgrp->pg_jobc++;
    320 		else if (--pgrp->pg_jobc == 0)
    321 			orphanpg(pgrp);
    322 
    323 	/*
    324 	 * Check this process' children to see whether they qualify
    325 	 * their process groups; if so, adjust counts for children's
    326 	 * process groups.
    327 	 */
    328 	for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next)
    329 		if ((hispgrp = p->p_pgrp) != pgrp &&
    330 		    hispgrp->pg_session == mysession &&
    331 		    p->p_stat != SZOMB)
    332 			if (entering)
    333 				hispgrp->pg_jobc++;
    334 			else if (--hispgrp->pg_jobc == 0)
    335 				orphanpg(hispgrp);
    336 }
    337 
    338 /*
    339  * A process group has become orphaned;
    340  * if there are any stopped processes in the group,
    341  * hang-up all process in that group.
    342  */
    343 static void
    344 orphanpg(pg)
    345 	struct pgrp *pg;
    346 {
    347 	register struct proc *p;
    348 
    349 	for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
    350 		if (p->p_stat == SSTOP) {
    351 			for (p = pg->pg_members.lh_first; p != 0;
    352 			    p = p->p_pglist.le_next) {
    353 				psignal(p, SIGHUP);
    354 				psignal(p, SIGCONT);
    355 			}
    356 			return;
    357 		}
    358 	}
    359 }
    360 
    361 #ifdef DEBUG
    362 void
    363 pgrpdump()
    364 {
    365 	register struct pgrp *pgrp;
    366 	register struct proc *p;
    367 	register int i;
    368 
    369 	for (i = 0; i <= pgrphash; i++) {
    370 		if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
    371 			printf("\tindx %d\n", i);
    372 			for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
    373 				printf("\tpgrp %p, pgid %d, sess %p, sesscnt %d, mem %p\n",
    374 				    pgrp, pgrp->pg_id, pgrp->pg_session,
    375 				    pgrp->pg_session->s_count,
    376 				    pgrp->pg_members.lh_first);
    377 				for (p = pgrp->pg_members.lh_first; p != 0;
    378 				    p = p->p_pglist.le_next) {
    379 					printf("\t\tpid %d addr %p pgrp %p\n",
    380 					    p->p_pid, p, p->p_pgrp);
    381 				}
    382 			}
    383 		}
    384 	}
    385 }
    386 #endif /* DEBUG */
    387