xref: /onnv-gate/usr/src/uts/common/os/pgrp.c (revision 749:d7f9da43aeb7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
230Sstevel@tonic-gate /*	  All Rights Reserved  	*/
240Sstevel@tonic-gate 
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*749Ssusans  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*749Ssusans  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/param.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/cred.h>
380Sstevel@tonic-gate #include <sys/vnode.h>
390Sstevel@tonic-gate #include <sys/errno.h>
400Sstevel@tonic-gate #include <sys/user.h>
410Sstevel@tonic-gate #include <sys/mount.h>
420Sstevel@tonic-gate #include <sys/proc.h>
430Sstevel@tonic-gate #include <sys/signal.h>
440Sstevel@tonic-gate #include <sys/siginfo.h>
450Sstevel@tonic-gate #include <sys/ucontext.h>
460Sstevel@tonic-gate #include <sys/prsystm.h>
470Sstevel@tonic-gate #include <sys/session.h>
480Sstevel@tonic-gate #include <sys/stream.h>
490Sstevel@tonic-gate #include <sys/strsubr.h>
500Sstevel@tonic-gate #include <sys/debug.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * Return 1 if process pointed to by 'cp' has a parent that would
540Sstevel@tonic-gate  * prevent its process group from being orphaned, 0 otherwise
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static int
pglinked(cp)580Sstevel@tonic-gate pglinked(cp)
590Sstevel@tonic-gate 	register proc_t *cp;
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	register proc_t *pp;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	if ((pp = cp->p_parent) != NULL &&
660Sstevel@tonic-gate 	    pp->p_pgidp != cp->p_pgidp &&
670Sstevel@tonic-gate 	    pp->p_sessp == cp->p_sessp)
680Sstevel@tonic-gate 		return (1);
690Sstevel@tonic-gate 	return (0);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Send the specified signal to all processes whose process group ID is
740Sstevel@tonic-gate  * equal to 'pgid'
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate void
pgsignal(pidp,sig)780Sstevel@tonic-gate pgsignal(pidp, sig)
790Sstevel@tonic-gate 	register struct pid *pidp;
800Sstevel@tonic-gate 	int sig;
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	register proc_t *prp;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	mutex_enter(&pidlock);
850Sstevel@tonic-gate 	for (prp = pidp->pid_pglink; prp; prp = prp->p_pglink) {
860Sstevel@tonic-gate 		mutex_enter(&prp->p_lock);
870Sstevel@tonic-gate 		sigtoproc(prp, NULL, sig);
880Sstevel@tonic-gate 		mutex_exit(&prp->p_lock);
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 	mutex_exit(&pidlock);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * similiar to pgsignal in function except that pidlock mutex is assumed
950Sstevel@tonic-gate  * to be held by the caller.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate void
sigtopg(pidp,sig)980Sstevel@tonic-gate sigtopg(pidp, sig)
990Sstevel@tonic-gate 	register struct pid *pidp;
1000Sstevel@tonic-gate 	int sig;
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	register proc_t *prp;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	for (prp = pidp->pid_pglink; prp; prp = prp->p_pglink) {
1070Sstevel@tonic-gate 		mutex_enter(&prp->p_lock);
1080Sstevel@tonic-gate 		sigtoproc(prp, NULL, sig);
1090Sstevel@tonic-gate 		mutex_exit(&prp->p_lock);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate  * Add a process to a process group
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate void
pgjoin(p,pgp)1180Sstevel@tonic-gate pgjoin(p, pgp)
1190Sstevel@tonic-gate 	register proc_t *p;
1200Sstevel@tonic-gate 	register struct pid *pgp;
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	p->p_pgidp = pgp;
1250Sstevel@tonic-gate 
126*749Ssusans 	if (pgp->pid_pglink == NULL) {
127*749Ssusans 		ASSERT(pgp->pid_pgtail == NULL);
128*749Ssusans 		p->p_ppglink = NULL;
129*749Ssusans 		p->p_pglink = NULL;
130*749Ssusans 		pgp->pid_pglink = p;
131*749Ssusans 		pgp->pid_pgtail = p;
132*749Ssusans 	} else 	{
133*749Ssusans 		ASSERT(pgp->pid_pgtail != NULL);
134*749Ssusans 		if (pglinked(p)) {
135*749Ssusans 			p->p_ppglink = NULL;
136*749Ssusans 			p->p_pglink = pgp->pid_pglink;
137*749Ssusans 			pgp->pid_pglink->p_ppglink = p;
138*749Ssusans 			pgp->pid_pglink = p;
139*749Ssusans 		} else {
140*749Ssusans 			p->p_ppglink = pgp->pid_pgtail;
141*749Ssusans 			p->p_pglink = NULL;
142*749Ssusans 			pgp->pid_pgtail->p_pglink = p;
143*749Ssusans 			pgp->pid_pgtail = p;
144*749Ssusans 		}
145*749Ssusans 	}
146*749Ssusans 
147*749Ssusans 	if (pgp->pid_pglink == pgp->pid_pgtail) {
1480Sstevel@tonic-gate 		PID_HOLD(pgp);
1490Sstevel@tonic-gate 		if (pglinked(p))
1500Sstevel@tonic-gate 			pgp->pid_pgorphaned = 0;
1510Sstevel@tonic-gate 		else
1520Sstevel@tonic-gate 			pgp->pid_pgorphaned = 1;
1530Sstevel@tonic-gate 	} else if (pgp->pid_pgorphaned && pglinked(p))
1540Sstevel@tonic-gate 		pgp->pid_pgorphaned = 0;
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate void
pgexit(prp)1580Sstevel@tonic-gate pgexit(prp)
1590Sstevel@tonic-gate 	proc_t *prp;
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	register proc_t *p;
1620Sstevel@tonic-gate 	register struct pid *pgp;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	pgp = prp->p_pgidp;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (pgp->pid_pglink == prp) {
1690Sstevel@tonic-gate 		ASSERT(prp->p_ppglink == NULL); /* must be at the front */
1700Sstevel@tonic-gate 		pgp->pid_pglink = prp->p_pglink;
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 	if (prp->p_ppglink) {
1730Sstevel@tonic-gate 		prp->p_ppglink->p_pglink = prp->p_pglink;
1740Sstevel@tonic-gate 	}
1750Sstevel@tonic-gate 	if (prp->p_pglink) {
1760Sstevel@tonic-gate 		prp->p_pglink->p_ppglink = prp->p_ppglink;
1770Sstevel@tonic-gate 	}
178*749Ssusans 	if (pgp->pid_pgtail == prp) {
179*749Ssusans 		pgp->pid_pgtail = prp->p_ppglink;
180*749Ssusans 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	prp->p_pgidp = NULL;
1830Sstevel@tonic-gate 	prp->p_pglink = NULL;
1840Sstevel@tonic-gate 	prp->p_ppglink = NULL;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if ((p = pgp->pid_pglink) == NULL) {
1870Sstevel@tonic-gate 		PID_RELE(pgp);
1880Sstevel@tonic-gate 	} else if (pgp->pid_pgorphaned == 0) {
1890Sstevel@tonic-gate 		do {
1900Sstevel@tonic-gate 			if (pglinked(p)) {
1910Sstevel@tonic-gate 				return;
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 		} while ((p = p->p_pglink) != NULL);
1940Sstevel@tonic-gate 		pgp->pid_pgorphaned = 1;
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate  * process 'pp' is exiting - check to see if this will
2000Sstevel@tonic-gate  * orphan its children's process groups
2010Sstevel@tonic-gate  */
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate void
pgdetach(pp)2040Sstevel@tonic-gate pgdetach(pp)
2050Sstevel@tonic-gate 	proc_t *pp;
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	int stopped;
2080Sstevel@tonic-gate 	register proc_t *cp;
2090Sstevel@tonic-gate 	register proc_t *mp;
2100Sstevel@tonic-gate 	register struct pid *pgp;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	for (cp = pp->p_child; cp; cp = cp->p_sibling) {
2150Sstevel@tonic-gate 		if ((pgp = cp->p_pgidp)->pid_pgorphaned)
2160Sstevel@tonic-gate 			continue;
2170Sstevel@tonic-gate 		stopped = 0;
2180Sstevel@tonic-gate 		mp = pgp->pid_pglink;
2190Sstevel@tonic-gate 		ASSERT(mp != NULL);
2200Sstevel@tonic-gate 		for (;;) {
2210Sstevel@tonic-gate 			if (mp != pp && mp->p_parent != pp && pglinked(mp))
2220Sstevel@tonic-gate 				break;
2230Sstevel@tonic-gate 			if (!stopped && mp != curproc) {
2240Sstevel@tonic-gate 				mutex_enter(&mp->p_lock);
2250Sstevel@tonic-gate 				stopped = jobstopped(mp);
2260Sstevel@tonic-gate 				mutex_exit(&mp->p_lock);
2270Sstevel@tonic-gate 			}
2280Sstevel@tonic-gate 			if ((mp = mp->p_pglink) == NULL) {
2290Sstevel@tonic-gate 				pgp->pid_pgorphaned = 1;
2300Sstevel@tonic-gate 				if (stopped) {
2310Sstevel@tonic-gate 					sigtopg(pgp, SIGHUP);
2320Sstevel@tonic-gate 					sigtopg(pgp, SIGCONT);
2330Sstevel@tonic-gate 				}
2340Sstevel@tonic-gate 				break;
2350Sstevel@tonic-gate 			}
2360Sstevel@tonic-gate 		}
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate  * Return 1 if pgid is the process group ID of an existing process group
2420Sstevel@tonic-gate  *	that has members not the process group leader in it.
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  * Otherwise, return 0.
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate int
pgmembers(pgid)2480Sstevel@tonic-gate pgmembers(pgid)
2490Sstevel@tonic-gate 	register pid_t pgid;
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	register proc_t *prp;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	for (prp = pgfind(pgid); prp; prp = prp->p_pglink)
2560Sstevel@tonic-gate 		if (prp->p_pid != pgid) {
2570Sstevel@tonic-gate 			return (1);
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 	return (0);
2600Sstevel@tonic-gate }
261