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