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 #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/cred.h>
367c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
377c478bd9Sstevel@tonic-gate #include <sys/errno.h>
387c478bd9Sstevel@tonic-gate #include <sys/user.h>
397c478bd9Sstevel@tonic-gate #include <sys/mount.h>
407c478bd9Sstevel@tonic-gate #include <sys/proc.h>
417c478bd9Sstevel@tonic-gate #include <sys/signal.h>
427c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
437c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
447c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
457c478bd9Sstevel@tonic-gate #include <sys/session.h>
467c478bd9Sstevel@tonic-gate #include <sys/stream.h>
477c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
487c478bd9Sstevel@tonic-gate #include <sys/debug.h>
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * Return 1 if process pointed to by 'cp' has a parent that would
527c478bd9Sstevel@tonic-gate * prevent its process group from being orphaned, 0 otherwise
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static int
pglinked(cp)567c478bd9Sstevel@tonic-gate pglinked(cp)
577c478bd9Sstevel@tonic-gate register proc_t *cp;
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate register proc_t *pp;
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate if ((pp = cp->p_parent) != NULL &&
647c478bd9Sstevel@tonic-gate pp->p_pgidp != cp->p_pgidp &&
657c478bd9Sstevel@tonic-gate pp->p_sessp == cp->p_sessp)
667c478bd9Sstevel@tonic-gate return (1);
677c478bd9Sstevel@tonic-gate return (0);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate * Send the specified signal to all processes whose process group ID is
727c478bd9Sstevel@tonic-gate * equal to 'pgid'
737c478bd9Sstevel@tonic-gate */
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate void
pgsignal(pidp,sig)767c478bd9Sstevel@tonic-gate pgsignal(pidp, sig)
777c478bd9Sstevel@tonic-gate register struct pid *pidp;
787c478bd9Sstevel@tonic-gate int sig;
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate register proc_t *prp;
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate mutex_enter(&pidlock);
837c478bd9Sstevel@tonic-gate for (prp = pidp->pid_pglink; prp; prp = prp->p_pglink) {
847c478bd9Sstevel@tonic-gate mutex_enter(&prp->p_lock);
857c478bd9Sstevel@tonic-gate sigtoproc(prp, NULL, sig);
867c478bd9Sstevel@tonic-gate mutex_exit(&prp->p_lock);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate * similiar to pgsignal in function except that pidlock mutex is assumed
937c478bd9Sstevel@tonic-gate * to be held by the caller.
947c478bd9Sstevel@tonic-gate */
957c478bd9Sstevel@tonic-gate void
sigtopg(pidp,sig)967c478bd9Sstevel@tonic-gate sigtopg(pidp, sig)
977c478bd9Sstevel@tonic-gate register struct pid *pidp;
987c478bd9Sstevel@tonic-gate int sig;
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate register proc_t *prp;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate for (prp = pidp->pid_pglink; prp; prp = prp->p_pglink) {
1057c478bd9Sstevel@tonic-gate mutex_enter(&prp->p_lock);
1067c478bd9Sstevel@tonic-gate sigtoproc(prp, NULL, sig);
1077c478bd9Sstevel@tonic-gate mutex_exit(&prp->p_lock);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate * Add a process to a process group
1137c478bd9Sstevel@tonic-gate */
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate void
pgjoin(p,pgp)1167c478bd9Sstevel@tonic-gate pgjoin(p, pgp)
1177c478bd9Sstevel@tonic-gate register proc_t *p;
1187c478bd9Sstevel@tonic-gate register struct pid *pgp;
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate p->p_pgidp = pgp;
1237c478bd9Sstevel@tonic-gate
124*e44bd21cSsusans if (pgp->pid_pglink == NULL) {
125*e44bd21cSsusans ASSERT(pgp->pid_pgtail == NULL);
126*e44bd21cSsusans p->p_ppglink = NULL;
127*e44bd21cSsusans p->p_pglink = NULL;
128*e44bd21cSsusans pgp->pid_pglink = p;
129*e44bd21cSsusans pgp->pid_pgtail = p;
130*e44bd21cSsusans } else {
131*e44bd21cSsusans ASSERT(pgp->pid_pgtail != NULL);
132*e44bd21cSsusans if (pglinked(p)) {
133*e44bd21cSsusans p->p_ppglink = NULL;
134*e44bd21cSsusans p->p_pglink = pgp->pid_pglink;
135*e44bd21cSsusans pgp->pid_pglink->p_ppglink = p;
136*e44bd21cSsusans pgp->pid_pglink = p;
137*e44bd21cSsusans } else {
138*e44bd21cSsusans p->p_ppglink = pgp->pid_pgtail;
139*e44bd21cSsusans p->p_pglink = NULL;
140*e44bd21cSsusans pgp->pid_pgtail->p_pglink = p;
141*e44bd21cSsusans pgp->pid_pgtail = p;
142*e44bd21cSsusans }
143*e44bd21cSsusans }
144*e44bd21cSsusans
145*e44bd21cSsusans if (pgp->pid_pglink == pgp->pid_pgtail) {
1467c478bd9Sstevel@tonic-gate PID_HOLD(pgp);
1477c478bd9Sstevel@tonic-gate if (pglinked(p))
1487c478bd9Sstevel@tonic-gate pgp->pid_pgorphaned = 0;
1497c478bd9Sstevel@tonic-gate else
1507c478bd9Sstevel@tonic-gate pgp->pid_pgorphaned = 1;
1517c478bd9Sstevel@tonic-gate } else if (pgp->pid_pgorphaned && pglinked(p))
1527c478bd9Sstevel@tonic-gate pgp->pid_pgorphaned = 0;
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate void
pgexit(prp)1567c478bd9Sstevel@tonic-gate pgexit(prp)
1577c478bd9Sstevel@tonic-gate proc_t *prp;
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate register proc_t *p;
1607c478bd9Sstevel@tonic-gate register struct pid *pgp;
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate pgp = prp->p_pgidp;
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate if (pgp->pid_pglink == prp) {
1677c478bd9Sstevel@tonic-gate ASSERT(prp->p_ppglink == NULL); /* must be at the front */
1687c478bd9Sstevel@tonic-gate pgp->pid_pglink = prp->p_pglink;
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate if (prp->p_ppglink) {
1717c478bd9Sstevel@tonic-gate prp->p_ppglink->p_pglink = prp->p_pglink;
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate if (prp->p_pglink) {
1747c478bd9Sstevel@tonic-gate prp->p_pglink->p_ppglink = prp->p_ppglink;
1757c478bd9Sstevel@tonic-gate }
176*e44bd21cSsusans if (pgp->pid_pgtail == prp) {
177*e44bd21cSsusans pgp->pid_pgtail = prp->p_ppglink;
178*e44bd21cSsusans }
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate prp->p_pgidp = NULL;
1817c478bd9Sstevel@tonic-gate prp->p_pglink = NULL;
1827c478bd9Sstevel@tonic-gate prp->p_ppglink = NULL;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate if ((p = pgp->pid_pglink) == NULL) {
1857c478bd9Sstevel@tonic-gate PID_RELE(pgp);
1867c478bd9Sstevel@tonic-gate } else if (pgp->pid_pgorphaned == 0) {
1877c478bd9Sstevel@tonic-gate do {
1887c478bd9Sstevel@tonic-gate if (pglinked(p)) {
1897c478bd9Sstevel@tonic-gate return;
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate } while ((p = p->p_pglink) != NULL);
1927c478bd9Sstevel@tonic-gate pgp->pid_pgorphaned = 1;
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate * process 'pp' is exiting - check to see if this will
1987c478bd9Sstevel@tonic-gate * orphan its children's process groups
1997c478bd9Sstevel@tonic-gate */
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate void
pgdetach(pp)2027c478bd9Sstevel@tonic-gate pgdetach(pp)
2037c478bd9Sstevel@tonic-gate proc_t *pp;
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate int stopped;
2067c478bd9Sstevel@tonic-gate register proc_t *cp;
2077c478bd9Sstevel@tonic-gate register proc_t *mp;
2087c478bd9Sstevel@tonic-gate register struct pid *pgp;
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate for (cp = pp->p_child; cp; cp = cp->p_sibling) {
2137c478bd9Sstevel@tonic-gate if ((pgp = cp->p_pgidp)->pid_pgorphaned)
2147c478bd9Sstevel@tonic-gate continue;
2157c478bd9Sstevel@tonic-gate stopped = 0;
2167c478bd9Sstevel@tonic-gate mp = pgp->pid_pglink;
2177c478bd9Sstevel@tonic-gate ASSERT(mp != NULL);
2187c478bd9Sstevel@tonic-gate for (;;) {
2197c478bd9Sstevel@tonic-gate if (mp != pp && mp->p_parent != pp && pglinked(mp))
2207c478bd9Sstevel@tonic-gate break;
2217c478bd9Sstevel@tonic-gate if (!stopped && mp != curproc) {
2227c478bd9Sstevel@tonic-gate mutex_enter(&mp->p_lock);
2237c478bd9Sstevel@tonic-gate stopped = jobstopped(mp);
2247c478bd9Sstevel@tonic-gate mutex_exit(&mp->p_lock);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate if ((mp = mp->p_pglink) == NULL) {
2277c478bd9Sstevel@tonic-gate pgp->pid_pgorphaned = 1;
2287c478bd9Sstevel@tonic-gate if (stopped) {
2297c478bd9Sstevel@tonic-gate sigtopg(pgp, SIGHUP);
2307c478bd9Sstevel@tonic-gate sigtopg(pgp, SIGCONT);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate break;
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate * Return 1 if pgid is the process group ID of an existing process group
2407c478bd9Sstevel@tonic-gate * that has members not the process group leader in it.
2417c478bd9Sstevel@tonic-gate *
2427c478bd9Sstevel@tonic-gate * Otherwise, return 0.
2437c478bd9Sstevel@tonic-gate */
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate int
pgmembers(pgid)2467c478bd9Sstevel@tonic-gate pgmembers(pgid)
2477c478bd9Sstevel@tonic-gate register pid_t pgid;
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate register proc_t *prp;
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&pidlock));
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate for (prp = pgfind(pgid); prp; prp = prp->p_pglink)
2547c478bd9Sstevel@tonic-gate if (prp->p_pid != pgid) {
2557c478bd9Sstevel@tonic-gate return (1);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate return (0);
2587c478bd9Sstevel@tonic-gate }
259