Home | History | Annotate | Line # | Download | only in kern
kern_proc.c revision 1.1.1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1982, 1986, 1989, 1991 Regents of the University of California.
      3  1.1  cgd  * 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  cgd  *	@(#)kern_proc.c	7.16 (Berkeley) 6/28/91
     34  1.1  cgd  */
     35  1.1  cgd 
     36  1.1  cgd #include "param.h"
     37  1.1  cgd #include "systm.h"
     38  1.1  cgd #include "kernel.h"
     39  1.1  cgd #include "proc.h"
     40  1.1  cgd #include "buf.h"
     41  1.1  cgd #include "acct.h"
     42  1.1  cgd #include "wait.h"
     43  1.1  cgd #include "file.h"
     44  1.1  cgd #include "../ufs/quota.h"
     45  1.1  cgd #include "uio.h"
     46  1.1  cgd #include "malloc.h"
     47  1.1  cgd #include "mbuf.h"
     48  1.1  cgd #include "ioctl.h"
     49  1.1  cgd #include "tty.h"
     50  1.1  cgd 
     51  1.1  cgd /*
     52  1.1  cgd  * Is p an inferior of the current process?
     53  1.1  cgd  */
     54  1.1  cgd inferior(p)
     55  1.1  cgd 	register struct proc *p;
     56  1.1  cgd {
     57  1.1  cgd 
     58  1.1  cgd 	for (; p != curproc; p = p->p_pptr)
     59  1.1  cgd 		if (p->p_pid == 0)
     60  1.1  cgd 			return (0);
     61  1.1  cgd 	return (1);
     62  1.1  cgd }
     63  1.1  cgd 
     64  1.1  cgd /*
     65  1.1  cgd  * Locate a process by number
     66  1.1  cgd  */
     67  1.1  cgd struct proc *
     68  1.1  cgd pfind(pid)
     69  1.1  cgd 	register pid;
     70  1.1  cgd {
     71  1.1  cgd 	register struct proc *p = pidhash[PIDHASH(pid)];
     72  1.1  cgd 
     73  1.1  cgd 	for (; p; p = p->p_hash)
     74  1.1  cgd 		if (p->p_pid == pid)
     75  1.1  cgd 			return (p);
     76  1.1  cgd 	return ((struct proc *)0);
     77  1.1  cgd }
     78  1.1  cgd 
     79  1.1  cgd /*
     80  1.1  cgd  * Locate a process group by number
     81  1.1  cgd  */
     82  1.1  cgd struct pgrp *
     83  1.1  cgd pgfind(pgid)
     84  1.1  cgd 	register pid_t pgid;
     85  1.1  cgd {
     86  1.1  cgd 	register struct pgrp *pgrp = pgrphash[PIDHASH(pgid)];
     87  1.1  cgd 
     88  1.1  cgd 	for (; pgrp; pgrp = pgrp->pg_hforw)
     89  1.1  cgd 		if (pgrp->pg_id == pgid)
     90  1.1  cgd 			return (pgrp);
     91  1.1  cgd 	return ((struct pgrp *)0);
     92  1.1  cgd }
     93  1.1  cgd 
     94  1.1  cgd /*
     95  1.1  cgd  * Move p to a new or existing process group (and session)
     96  1.1  cgd  */
     97  1.1  cgd enterpgrp(p, pgid, mksess)
     98  1.1  cgd 	register struct proc *p;
     99  1.1  cgd 	pid_t pgid;
    100  1.1  cgd {
    101  1.1  cgd 	register struct pgrp *pgrp = pgfind(pgid);
    102  1.1  cgd 	register struct proc **pp;
    103  1.1  cgd 	register struct proc *cp;
    104  1.1  cgd 	int n;
    105  1.1  cgd 
    106  1.1  cgd #ifdef DIAGNOSTIC
    107  1.1  cgd 	if (pgrp && mksess)	/* firewalls */
    108  1.1  cgd 		panic("enterpgrp: setsid into non-empty pgrp");
    109  1.1  cgd 	if (SESS_LEADER(p))
    110  1.1  cgd 		panic("enterpgrp: session leader attempted setpgrp");
    111  1.1  cgd #endif
    112  1.1  cgd 	if (pgrp == NULL) {
    113  1.1  cgd 		/*
    114  1.1  cgd 		 * new process group
    115  1.1  cgd 		 */
    116  1.1  cgd #ifdef DIAGNOSTIC
    117  1.1  cgd 		if (p->p_pid != pgid)
    118  1.1  cgd 			panic("enterpgrp: new pgrp and pid != pgid");
    119  1.1  cgd #endif
    120  1.1  cgd 		MALLOC(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
    121  1.1  cgd 		       M_WAITOK);
    122  1.1  cgd 		if (mksess) {
    123  1.1  cgd 			register struct session *sess;
    124  1.1  cgd 
    125  1.1  cgd 			/*
    126  1.1  cgd 			 * new session
    127  1.1  cgd 			 */
    128  1.1  cgd 			MALLOC(sess, struct session *, sizeof(struct session),
    129  1.1  cgd 				M_SESSION, M_WAITOK);
    130  1.1  cgd 			sess->s_leader = p;
    131  1.1  cgd 			sess->s_count = 1;
    132  1.1  cgd 			sess->s_ttyvp = NULL;
    133  1.1  cgd 			sess->s_ttyp = NULL;
    134  1.1  cgd 			bcopy(p->p_session->s_login, sess->s_login,
    135  1.1  cgd 			    sizeof(sess->s_login));
    136  1.1  cgd 			p->p_flag &= ~SCTTY;
    137  1.1  cgd 			pgrp->pg_session = sess;
    138  1.1  cgd #ifdef DIAGNOSTIC
    139  1.1  cgd 			if (p != curproc)
    140  1.1  cgd 				panic("enterpgrp: mksession and p != curproc");
    141  1.1  cgd #endif
    142  1.1  cgd 		} else {
    143  1.1  cgd 			pgrp->pg_session = p->p_session;
    144  1.1  cgd 			pgrp->pg_session->s_count++;
    145  1.1  cgd 		}
    146  1.1  cgd 		pgrp->pg_id = pgid;
    147  1.1  cgd 		pgrp->pg_hforw = pgrphash[n = PIDHASH(pgid)];
    148  1.1  cgd 		pgrphash[n] = pgrp;
    149  1.1  cgd 		pgrp->pg_jobc = 0;
    150  1.1  cgd 		pgrp->pg_mem = NULL;
    151  1.1  cgd 	} else if (pgrp == p->p_pgrp)
    152  1.1  cgd 		return;
    153  1.1  cgd 
    154  1.1  cgd 	/*
    155  1.1  cgd 	 * Adjust eligibility of affected pgrps to participate in job control.
    156  1.1  cgd 	 * Increment eligibility counts before decrementing, otherwise we
    157  1.1  cgd 	 * could reach 0 spuriously during the first call.
    158  1.1  cgd 	 */
    159  1.1  cgd 	fixjobc(p, pgrp, 1);
    160  1.1  cgd 	fixjobc(p, p->p_pgrp, 0);
    161  1.1  cgd 
    162  1.1  cgd 	/*
    163  1.1  cgd 	 * unlink p from old process group
    164  1.1  cgd 	 */
    165  1.1  cgd 	for (pp = &p->p_pgrp->pg_mem; *pp; pp = &(*pp)->p_pgrpnxt)
    166  1.1  cgd 		if (*pp == p) {
    167  1.1  cgd 			*pp = p->p_pgrpnxt;
    168  1.1  cgd 			goto done;
    169  1.1  cgd 		}
    170  1.1  cgd 	panic("enterpgrp: can't find p on old pgrp");
    171  1.1  cgd done:
    172  1.1  cgd 	/*
    173  1.1  cgd 	 * delete old if empty
    174  1.1  cgd 	 */
    175  1.1  cgd 	if (p->p_pgrp->pg_mem == 0)
    176  1.1  cgd 		pgdelete(p->p_pgrp);
    177  1.1  cgd 	/*
    178  1.1  cgd 	 * link into new one
    179  1.1  cgd 	 */
    180  1.1  cgd 	p->p_pgrp = pgrp;
    181  1.1  cgd 	p->p_pgrpnxt = pgrp->pg_mem;
    182  1.1  cgd 	pgrp->pg_mem = p;
    183  1.1  cgd }
    184  1.1  cgd 
    185  1.1  cgd /*
    186  1.1  cgd  * remove process from process group
    187  1.1  cgd  */
    188  1.1  cgd leavepgrp(p)
    189  1.1  cgd 	register struct proc *p;
    190  1.1  cgd {
    191  1.1  cgd 	register struct proc **pp = &p->p_pgrp->pg_mem;
    192  1.1  cgd 
    193  1.1  cgd 	for (; *pp; pp = &(*pp)->p_pgrpnxt)
    194  1.1  cgd 		if (*pp == p) {
    195  1.1  cgd 			*pp = p->p_pgrpnxt;
    196  1.1  cgd 			goto done;
    197  1.1  cgd 		}
    198  1.1  cgd 	panic("leavepgrp: can't find p in pgrp");
    199  1.1  cgd done:
    200  1.1  cgd 	if (!p->p_pgrp->pg_mem)
    201  1.1  cgd 		pgdelete(p->p_pgrp);
    202  1.1  cgd 	p->p_pgrp = 0;
    203  1.1  cgd }
    204  1.1  cgd 
    205  1.1  cgd /*
    206  1.1  cgd  * delete a process group
    207  1.1  cgd  */
    208  1.1  cgd pgdelete(pgrp)
    209  1.1  cgd 	register struct pgrp *pgrp;
    210  1.1  cgd {
    211  1.1  cgd 	register struct pgrp **pgp = &pgrphash[PIDHASH(pgrp->pg_id)];
    212  1.1  cgd 
    213  1.1  cgd 	if (pgrp->pg_session->s_ttyp != NULL &&
    214  1.1  cgd 	    pgrp->pg_session->s_ttyp->t_pgrp == pgrp)
    215  1.1  cgd 		pgrp->pg_session->s_ttyp->t_pgrp = NULL;
    216  1.1  cgd 	for (; *pgp; pgp = &(*pgp)->pg_hforw)
    217  1.1  cgd 		if (*pgp == pgrp) {
    218  1.1  cgd 			*pgp = pgrp->pg_hforw;
    219  1.1  cgd 			goto done;
    220  1.1  cgd 		}
    221  1.1  cgd 	panic("pgdelete: can't find pgrp on hash chain");
    222  1.1  cgd done:
    223  1.1  cgd 	if (--pgrp->pg_session->s_count == 0)
    224  1.1  cgd 		FREE(pgrp->pg_session, M_SESSION);
    225  1.1  cgd 	FREE(pgrp, M_PGRP);
    226  1.1  cgd }
    227  1.1  cgd 
    228  1.1  cgd static orphanpg();
    229  1.1  cgd 
    230  1.1  cgd /*
    231  1.1  cgd  * Adjust pgrp jobc counters when specified process changes process group.
    232  1.1  cgd  * We count the number of processes in each process group that "qualify"
    233  1.1  cgd  * the group for terminal job control (those with a parent in a different
    234  1.1  cgd  * process group of the same session).  If that count reaches zero, the
    235  1.1  cgd  * process group becomes orphaned.  Check both the specified process'
    236  1.1  cgd  * process group and that of its children.
    237  1.1  cgd  * entering == 0 => p is leaving specified group.
    238  1.1  cgd  * entering == 1 => p is entering specified group.
    239  1.1  cgd  */
    240  1.1  cgd fixjobc(p, pgrp, entering)
    241  1.1  cgd 	register struct proc *p;
    242  1.1  cgd 	register struct pgrp *pgrp;
    243  1.1  cgd 	int entering;
    244  1.1  cgd {
    245  1.1  cgd 	register struct pgrp *hispgrp;
    246  1.1  cgd 	register struct session *mysession = pgrp->pg_session;
    247  1.1  cgd 
    248  1.1  cgd 	/*
    249  1.1  cgd 	 * Check p's parent to see whether p qualifies its own process
    250  1.1  cgd 	 * group; if so, adjust count for p's process group.
    251  1.1  cgd 	 */
    252  1.1  cgd 	if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
    253  1.1  cgd 	    hispgrp->pg_session == mysession)
    254  1.1  cgd 		if (entering)
    255  1.1  cgd 			pgrp->pg_jobc++;
    256  1.1  cgd 		else if (--pgrp->pg_jobc == 0)
    257  1.1  cgd 			orphanpg(pgrp);
    258  1.1  cgd 
    259  1.1  cgd 	/*
    260  1.1  cgd 	 * Check this process' children to see whether they qualify
    261  1.1  cgd 	 * their process groups; if so, adjust counts for children's
    262  1.1  cgd 	 * process groups.
    263  1.1  cgd 	 */
    264  1.1  cgd 	for (p = p->p_cptr; p; p = p->p_osptr)
    265  1.1  cgd 		if ((hispgrp = p->p_pgrp) != pgrp &&
    266  1.1  cgd 		    hispgrp->pg_session == mysession &&
    267  1.1  cgd 		    p->p_stat != SZOMB)
    268  1.1  cgd 			if (entering)
    269  1.1  cgd 				hispgrp->pg_jobc++;
    270  1.1  cgd 			else if (--hispgrp->pg_jobc == 0)
    271  1.1  cgd 				orphanpg(hispgrp);
    272  1.1  cgd }
    273  1.1  cgd 
    274  1.1  cgd /*
    275  1.1  cgd  * A process group has become orphaned;
    276  1.1  cgd  * if there are any stopped processes in the group,
    277  1.1  cgd  * hang-up all process in that group.
    278  1.1  cgd  */
    279  1.1  cgd static
    280  1.1  cgd orphanpg(pg)
    281  1.1  cgd 	struct pgrp *pg;
    282  1.1  cgd {
    283  1.1  cgd 	register struct proc *p;
    284  1.1  cgd 
    285  1.1  cgd 	for (p = pg->pg_mem; p; p = p->p_pgrpnxt) {
    286  1.1  cgd 		if (p->p_stat == SSTOP) {
    287  1.1  cgd 			for (p = pg->pg_mem; p; p = p->p_pgrpnxt) {
    288  1.1  cgd 				psignal(p, SIGHUP);
    289  1.1  cgd 				psignal(p, SIGCONT);
    290  1.1  cgd 			}
    291  1.1  cgd 			return;
    292  1.1  cgd 		}
    293  1.1  cgd 	}
    294  1.1  cgd }
    295  1.1  cgd 
    296  1.1  cgd #ifdef debug
    297  1.1  cgd /* DEBUG */
    298  1.1  cgd pgrpdump()
    299  1.1  cgd {
    300  1.1  cgd 	register struct pgrp *pgrp;
    301  1.1  cgd 	register struct proc *p;
    302  1.1  cgd 	register i;
    303  1.1  cgd 
    304  1.1  cgd 	for (i=0; i<PIDHSZ; i++) {
    305  1.1  cgd 		if (pgrphash[i]) {
    306  1.1  cgd 		  printf("\tindx %d\n", i);
    307  1.1  cgd 		  for (pgrp=pgrphash[i]; pgrp; pgrp=pgrp->pg_hforw) {
    308  1.1  cgd 		    printf("\tpgrp %x, pgid %d, sess %x, sesscnt %d, mem %x\n",
    309  1.1  cgd 			pgrp, pgrp->pg_id, pgrp->pg_session,
    310  1.1  cgd 			pgrp->pg_session->s_count, pgrp->pg_mem);
    311  1.1  cgd 		    for (p=pgrp->pg_mem; p; p=p->p_pgrpnxt) {
    312  1.1  cgd 			printf("\t\tpid %d addr %x pgrp %x\n",
    313  1.1  cgd 				p->p_pid, p, p->p_pgrp);
    314  1.1  cgd 		    }
    315  1.1  cgd 		  }
    316  1.1  cgd 
    317  1.1  cgd 		}
    318  1.1  cgd 	}
    319  1.1  cgd }
    320  1.1  cgd #endif /* debug */
    321