xref: /titanic_44/usr/src/uts/common/os/pgrp.c (revision e44bd21c11b4f4cc72e87048602add8046a3d30d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
27*e44bd21cSsusans  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*e44bd21cSsusans  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/cred.h>
387c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/user.h>
417c478bd9Sstevel@tonic-gate #include <sys/mount.h>
427c478bd9Sstevel@tonic-gate #include <sys/proc.h>
437c478bd9Sstevel@tonic-gate #include <sys/signal.h>
447c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
457c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
467c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
477c478bd9Sstevel@tonic-gate #include <sys/session.h>
487c478bd9Sstevel@tonic-gate #include <sys/stream.h>
497c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
507c478bd9Sstevel@tonic-gate #include <sys/debug.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * Return 1 if process pointed to by 'cp' has a parent that would
547c478bd9Sstevel@tonic-gate  * prevent its process group from being orphaned, 0 otherwise
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static int
pglinked(cp)587c478bd9Sstevel@tonic-gate pglinked(cp)
597c478bd9Sstevel@tonic-gate 	register proc_t *cp;
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	register proc_t *pp;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if ((pp = cp->p_parent) != NULL &&
667c478bd9Sstevel@tonic-gate 	    pp->p_pgidp != cp->p_pgidp &&
677c478bd9Sstevel@tonic-gate 	    pp->p_sessp == cp->p_sessp)
687c478bd9Sstevel@tonic-gate 		return (1);
697c478bd9Sstevel@tonic-gate 	return (0);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Send the specified signal to all processes whose process group ID is
747c478bd9Sstevel@tonic-gate  * equal to 'pgid'
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate void
pgsignal(pidp,sig)787c478bd9Sstevel@tonic-gate pgsignal(pidp, sig)
797c478bd9Sstevel@tonic-gate 	register struct pid *pidp;
807c478bd9Sstevel@tonic-gate 	int sig;
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	register proc_t *prp;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
857c478bd9Sstevel@tonic-gate 	for (prp = pidp->pid_pglink; prp; prp = prp->p_pglink) {
867c478bd9Sstevel@tonic-gate 		mutex_enter(&prp->p_lock);
877c478bd9Sstevel@tonic-gate 		sigtoproc(prp, NULL, sig);
887c478bd9Sstevel@tonic-gate 		mutex_exit(&prp->p_lock);
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * similiar to pgsignal in function except that pidlock mutex is assumed
957c478bd9Sstevel@tonic-gate  * to be held by the caller.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate void
sigtopg(pidp,sig)987c478bd9Sstevel@tonic-gate sigtopg(pidp, sig)
997c478bd9Sstevel@tonic-gate 	register struct pid *pidp;
1007c478bd9Sstevel@tonic-gate 	int sig;
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	register proc_t *prp;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	for (prp = pidp->pid_pglink; prp; prp = prp->p_pglink) {
1077c478bd9Sstevel@tonic-gate 		mutex_enter(&prp->p_lock);
1087c478bd9Sstevel@tonic-gate 		sigtoproc(prp, NULL, sig);
1097c478bd9Sstevel@tonic-gate 		mutex_exit(&prp->p_lock);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Add a process to a process group
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate void
pgjoin(p,pgp)1187c478bd9Sstevel@tonic-gate pgjoin(p, pgp)
1197c478bd9Sstevel@tonic-gate 	register proc_t *p;
1207c478bd9Sstevel@tonic-gate 	register struct pid *pgp;
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	p->p_pgidp = pgp;
1257c478bd9Sstevel@tonic-gate 
126*e44bd21cSsusans 	if (pgp->pid_pglink == NULL) {
127*e44bd21cSsusans 		ASSERT(pgp->pid_pgtail == NULL);
128*e44bd21cSsusans 		p->p_ppglink = NULL;
129*e44bd21cSsusans 		p->p_pglink = NULL;
130*e44bd21cSsusans 		pgp->pid_pglink = p;
131*e44bd21cSsusans 		pgp->pid_pgtail = p;
132*e44bd21cSsusans 	} else 	{
133*e44bd21cSsusans 		ASSERT(pgp->pid_pgtail != NULL);
134*e44bd21cSsusans 		if (pglinked(p)) {
135*e44bd21cSsusans 			p->p_ppglink = NULL;
136*e44bd21cSsusans 			p->p_pglink = pgp->pid_pglink;
137*e44bd21cSsusans 			pgp->pid_pglink->p_ppglink = p;
138*e44bd21cSsusans 			pgp->pid_pglink = p;
139*e44bd21cSsusans 		} else {
140*e44bd21cSsusans 			p->p_ppglink = pgp->pid_pgtail;
141*e44bd21cSsusans 			p->p_pglink = NULL;
142*e44bd21cSsusans 			pgp->pid_pgtail->p_pglink = p;
143*e44bd21cSsusans 			pgp->pid_pgtail = p;
144*e44bd21cSsusans 		}
145*e44bd21cSsusans 	}
146*e44bd21cSsusans 
147*e44bd21cSsusans 	if (pgp->pid_pglink == pgp->pid_pgtail) {
1487c478bd9Sstevel@tonic-gate 		PID_HOLD(pgp);
1497c478bd9Sstevel@tonic-gate 		if (pglinked(p))
1507c478bd9Sstevel@tonic-gate 			pgp->pid_pgorphaned = 0;
1517c478bd9Sstevel@tonic-gate 		else
1527c478bd9Sstevel@tonic-gate 			pgp->pid_pgorphaned = 1;
1537c478bd9Sstevel@tonic-gate 	} else if (pgp->pid_pgorphaned && pglinked(p))
1547c478bd9Sstevel@tonic-gate 		pgp->pid_pgorphaned = 0;
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate void
pgexit(prp)1587c478bd9Sstevel@tonic-gate pgexit(prp)
1597c478bd9Sstevel@tonic-gate 	proc_t *prp;
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	register proc_t *p;
1627c478bd9Sstevel@tonic-gate 	register struct pid *pgp;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	pgp = prp->p_pgidp;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if (pgp->pid_pglink == prp) {
1697c478bd9Sstevel@tonic-gate 		ASSERT(prp->p_ppglink == NULL); /* must be at the front */
1707c478bd9Sstevel@tonic-gate 		pgp->pid_pglink = prp->p_pglink;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 	if (prp->p_ppglink) {
1737c478bd9Sstevel@tonic-gate 		prp->p_ppglink->p_pglink = prp->p_pglink;
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	if (prp->p_pglink) {
1767c478bd9Sstevel@tonic-gate 		prp->p_pglink->p_ppglink = prp->p_ppglink;
1777c478bd9Sstevel@tonic-gate 	}
178*e44bd21cSsusans 	if (pgp->pid_pgtail == prp) {
179*e44bd21cSsusans 		pgp->pid_pgtail = prp->p_ppglink;
180*e44bd21cSsusans 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	prp->p_pgidp = NULL;
1837c478bd9Sstevel@tonic-gate 	prp->p_pglink = NULL;
1847c478bd9Sstevel@tonic-gate 	prp->p_ppglink = NULL;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if ((p = pgp->pid_pglink) == NULL) {
1877c478bd9Sstevel@tonic-gate 		PID_RELE(pgp);
1887c478bd9Sstevel@tonic-gate 	} else if (pgp->pid_pgorphaned == 0) {
1897c478bd9Sstevel@tonic-gate 		do {
1907c478bd9Sstevel@tonic-gate 			if (pglinked(p)) {
1917c478bd9Sstevel@tonic-gate 				return;
1927c478bd9Sstevel@tonic-gate 			}
1937c478bd9Sstevel@tonic-gate 		} while ((p = p->p_pglink) != NULL);
1947c478bd9Sstevel@tonic-gate 		pgp->pid_pgorphaned = 1;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * process 'pp' is exiting - check to see if this will
2007c478bd9Sstevel@tonic-gate  * orphan its children's process groups
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate void
pgdetach(pp)2047c478bd9Sstevel@tonic-gate pgdetach(pp)
2057c478bd9Sstevel@tonic-gate 	proc_t *pp;
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	int stopped;
2087c478bd9Sstevel@tonic-gate 	register proc_t *cp;
2097c478bd9Sstevel@tonic-gate 	register proc_t *mp;
2107c478bd9Sstevel@tonic-gate 	register struct pid *pgp;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	for (cp = pp->p_child; cp; cp = cp->p_sibling) {
2157c478bd9Sstevel@tonic-gate 		if ((pgp = cp->p_pgidp)->pid_pgorphaned)
2167c478bd9Sstevel@tonic-gate 			continue;
2177c478bd9Sstevel@tonic-gate 		stopped = 0;
2187c478bd9Sstevel@tonic-gate 		mp = pgp->pid_pglink;
2197c478bd9Sstevel@tonic-gate 		ASSERT(mp != NULL);
2207c478bd9Sstevel@tonic-gate 		for (;;) {
2217c478bd9Sstevel@tonic-gate 			if (mp != pp && mp->p_parent != pp && pglinked(mp))
2227c478bd9Sstevel@tonic-gate 				break;
2237c478bd9Sstevel@tonic-gate 			if (!stopped && mp != curproc) {
2247c478bd9Sstevel@tonic-gate 				mutex_enter(&mp->p_lock);
2257c478bd9Sstevel@tonic-gate 				stopped = jobstopped(mp);
2267c478bd9Sstevel@tonic-gate 				mutex_exit(&mp->p_lock);
2277c478bd9Sstevel@tonic-gate 			}
2287c478bd9Sstevel@tonic-gate 			if ((mp = mp->p_pglink) == NULL) {
2297c478bd9Sstevel@tonic-gate 				pgp->pid_pgorphaned = 1;
2307c478bd9Sstevel@tonic-gate 				if (stopped) {
2317c478bd9Sstevel@tonic-gate 					sigtopg(pgp, SIGHUP);
2327c478bd9Sstevel@tonic-gate 					sigtopg(pgp, SIGCONT);
2337c478bd9Sstevel@tonic-gate 				}
2347c478bd9Sstevel@tonic-gate 				break;
2357c478bd9Sstevel@tonic-gate 			}
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * Return 1 if pgid is the process group ID of an existing process group
2427c478bd9Sstevel@tonic-gate  *	that has members not the process group leader in it.
2437c478bd9Sstevel@tonic-gate  *
2447c478bd9Sstevel@tonic-gate  * Otherwise, return 0.
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate int
pgmembers(pgid)2487c478bd9Sstevel@tonic-gate pgmembers(pgid)
2497c478bd9Sstevel@tonic-gate 	register pid_t pgid;
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	register proc_t *prp;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	for (prp = pgfind(pgid); prp; prp = prp->p_pglink)
2567c478bd9Sstevel@tonic-gate 		if (prp->p_pid != pgid) {
2577c478bd9Sstevel@tonic-gate 			return (1);
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 	return (0);
2607c478bd9Sstevel@tonic-gate }
261