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
5*9acbbeafSnn35248  * Common Development and Distribution License (the "License").
6*9acbbeafSnn35248  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*9acbbeafSnn35248 /*
22*9acbbeafSnn35248  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*9acbbeafSnn35248  * Use is subject to license terms.
24*9acbbeafSnn35248  */
25*9acbbeafSnn35248 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/file.h>
357c478bd9Sstevel@tonic-gate #include <sys/proc.h>
367c478bd9Sstevel@tonic-gate #include <sys/session.h>
377c478bd9Sstevel@tonic-gate #include <sys/debug.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /* ARGSUSED */
407c478bd9Sstevel@tonic-gate int
setpgrp(int flag,int pid,int pgid)417c478bd9Sstevel@tonic-gate setpgrp(int flag, int pid, int pgid)
427c478bd9Sstevel@tonic-gate {
43*9acbbeafSnn35248 	proc_t	*p = curproc;
44*9acbbeafSnn35248 	int	retval = 0;
45*9acbbeafSnn35248 	int	sid;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	switch (flag) {
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	case 1: /* setpgrp() */
507c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
517c478bd9Sstevel@tonic-gate 		if (p->p_sessp->s_sidp != p->p_pidp && !pgmembers(p->p_pid)) {
527c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
537c478bd9Sstevel@tonic-gate 			sess_create();
547c478bd9Sstevel@tonic-gate 		} else
557c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
56*9acbbeafSnn35248 		mutex_enter(&p->p_splock);
57*9acbbeafSnn35248 		sid = p->p_sessp->s_sid;
58*9acbbeafSnn35248 		mutex_exit(&p->p_splock);
59*9acbbeafSnn35248 		return (sid);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	case 3: /* setsid() */
627c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
637c478bd9Sstevel@tonic-gate 		if (p->p_pgidp == p->p_pidp || pgmembers(p->p_pid)) {
647c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
657c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
667c478bd9Sstevel@tonic-gate 		}
677c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
687c478bd9Sstevel@tonic-gate 		sess_create();
69*9acbbeafSnn35248 		mutex_enter(&p->p_splock);
70*9acbbeafSnn35248 		sid = p->p_sessp->s_sid;
71*9acbbeafSnn35248 		mutex_exit(&p->p_splock);
72*9acbbeafSnn35248 		return (sid);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	case 5: /* setpgid() */
757c478bd9Sstevel@tonic-gate 	{
767c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
777c478bd9Sstevel@tonic-gate 		if (pid == 0)
787c478bd9Sstevel@tonic-gate 			pid = p->p_pid;
797c478bd9Sstevel@tonic-gate 		else if (pid < 0 || pid >= maxpid) {
807c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
817c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
827c478bd9Sstevel@tonic-gate 		} else if (pid != p->p_pid) {
837c478bd9Sstevel@tonic-gate 			for (p = p->p_child; /* empty */; p = p->p_sibling) {
847c478bd9Sstevel@tonic-gate 				if (p == NULL) {
857c478bd9Sstevel@tonic-gate 					mutex_exit(&pidlock);
867c478bd9Sstevel@tonic-gate 					return (set_errno(ESRCH));
877c478bd9Sstevel@tonic-gate 				}
887c478bd9Sstevel@tonic-gate 				if (p->p_pid == pid)
897c478bd9Sstevel@tonic-gate 					break;
907c478bd9Sstevel@tonic-gate 			}
917c478bd9Sstevel@tonic-gate 			if (p->p_flag & SEXECED) {
927c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
937c478bd9Sstevel@tonic-gate 				return (set_errno(EACCES));
947c478bd9Sstevel@tonic-gate 			}
957c478bd9Sstevel@tonic-gate 			if (p->p_sessp != ttoproc(curthread)->p_sessp) {
967c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
977c478bd9Sstevel@tonic-gate 				return (set_errno(EPERM));
987c478bd9Sstevel@tonic-gate 			}
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 		if (p->p_sessp->s_sid == pid) {
1027c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1037c478bd9Sstevel@tonic-gate 			return (set_errno(EPERM));
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 		if (pgid == 0)
1077c478bd9Sstevel@tonic-gate 			pgid = p->p_pid;
1087c478bd9Sstevel@tonic-gate 		else if (pgid < 0 || pgid >= maxpid) {
1097c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1107c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		if (p->p_pgrp == pgid) {
1147c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1157c478bd9Sstevel@tonic-gate 			break;
1167c478bd9Sstevel@tonic-gate 		} else if (p->p_pid == pgid) {
1177c478bd9Sstevel@tonic-gate 			/*
1187c478bd9Sstevel@tonic-gate 			 * We need to protect p_pgidp with p_lock because
1197c478bd9Sstevel@tonic-gate 			 * /proc looks at it while holding only p_lock.
1207c478bd9Sstevel@tonic-gate 			 */
1217c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
1227c478bd9Sstevel@tonic-gate 			pgexit(p);
1237c478bd9Sstevel@tonic-gate 			pgjoin(p, p->p_pidp);
1247c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1257c478bd9Sstevel@tonic-gate 		} else {
1267c478bd9Sstevel@tonic-gate 			register proc_t *q;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 			if ((q = pgfind(pgid)) == NULL ||
1297c478bd9Sstevel@tonic-gate 			    q->p_sessp != p->p_sessp) {
1307c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
1317c478bd9Sstevel@tonic-gate 				return (set_errno(EPERM));
1327c478bd9Sstevel@tonic-gate 			}
1337c478bd9Sstevel@tonic-gate 			/*
1347c478bd9Sstevel@tonic-gate 			 * See comment above about p_lock and /proc
1357c478bd9Sstevel@tonic-gate 			 */
1367c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
1377c478bd9Sstevel@tonic-gate 			pgexit(p);
1387c478bd9Sstevel@tonic-gate 			pgjoin(p, q->p_pgidp);
1397c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
1427c478bd9Sstevel@tonic-gate 		break;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	case 0: /* getpgrp() */
1467c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
1477c478bd9Sstevel@tonic-gate 		retval = p->p_pgrp;
1487c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
1497c478bd9Sstevel@tonic-gate 		break;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	case 2: /* getsid() */
1527c478bd9Sstevel@tonic-gate 	case 4: /* getpgid() */
1537c478bd9Sstevel@tonic-gate 		if (pid < 0 || pid >= maxpid) {
1547c478bd9Sstevel@tonic-gate 			return (set_errno(EINVAL));
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
1577c478bd9Sstevel@tonic-gate 		if (pid != 0 && p->p_pid != pid &&
1587c478bd9Sstevel@tonic-gate 		    ((p = prfind(pid)) == NULL || p->p_stat == SIDL)) {
1597c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1607c478bd9Sstevel@tonic-gate 			return (set_errno(ESRCH));
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 		if (flag == 2)
1637c478bd9Sstevel@tonic-gate 			retval = p->p_sessp->s_sid;
1647c478bd9Sstevel@tonic-gate 		else
1657c478bd9Sstevel@tonic-gate 			retval = p->p_pgrp;
1667c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
1677c478bd9Sstevel@tonic-gate 		break;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	return (retval);
1717c478bd9Sstevel@tonic-gate }
172