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
30*9acbbeafSnn35248 #pragma ident "%Z%%M% %I% %E% SMI" /* from SVr4.0 1.78 */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/errno.h>
377c478bd9Sstevel@tonic-gate #include <sys/file.h>
387c478bd9Sstevel@tonic-gate #include <sys/proc.h>
397c478bd9Sstevel@tonic-gate #include <sys/session.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /* ARGSUSED */
437c478bd9Sstevel@tonic-gate int
setpgrp(int flag,int pid,int pgid)447c478bd9Sstevel@tonic-gate setpgrp(int flag, int pid, int pgid)
457c478bd9Sstevel@tonic-gate {
46*9acbbeafSnn35248 proc_t *p = curproc;
47*9acbbeafSnn35248 int retval = 0;
48*9acbbeafSnn35248 int sid;
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate switch (flag) {
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate case 1: /* setpgrp() */
537c478bd9Sstevel@tonic-gate mutex_enter(&pidlock);
547c478bd9Sstevel@tonic-gate if (p->p_sessp->s_sidp != p->p_pidp && !pgmembers(p->p_pid)) {
557c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
567c478bd9Sstevel@tonic-gate sess_create();
577c478bd9Sstevel@tonic-gate } else
587c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
59*9acbbeafSnn35248 mutex_enter(&p->p_splock);
60*9acbbeafSnn35248 sid = p->p_sessp->s_sid;
61*9acbbeafSnn35248 mutex_exit(&p->p_splock);
62*9acbbeafSnn35248 return (sid);
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate case 3: /* setsid() */
657c478bd9Sstevel@tonic-gate mutex_enter(&pidlock);
667c478bd9Sstevel@tonic-gate if (p->p_pgidp == p->p_pidp || pgmembers(p->p_pid)) {
677c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
687c478bd9Sstevel@tonic-gate return (set_errno(EPERM));
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
717c478bd9Sstevel@tonic-gate sess_create();
72*9acbbeafSnn35248 mutex_enter(&p->p_splock);
73*9acbbeafSnn35248 sid = p->p_sessp->s_sid;
74*9acbbeafSnn35248 mutex_exit(&p->p_splock);
75*9acbbeafSnn35248 return (sid);
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate case 5: /* setpgid() */
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate mutex_enter(&pidlock);
807c478bd9Sstevel@tonic-gate if (pid == 0)
817c478bd9Sstevel@tonic-gate pid = p->p_pid;
827c478bd9Sstevel@tonic-gate else if (pid < 0 || pid >= maxpid) {
837c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
847c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
857c478bd9Sstevel@tonic-gate } else if (pid != p->p_pid) {
867c478bd9Sstevel@tonic-gate for (p = p->p_child; /* empty */; p = p->p_sibling) {
877c478bd9Sstevel@tonic-gate if (p == NULL) {
887c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
897c478bd9Sstevel@tonic-gate return (set_errno(ESRCH));
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate if (p->p_pid == pid)
927c478bd9Sstevel@tonic-gate break;
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate if (p->p_flag & SEXECED) {
957c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
967c478bd9Sstevel@tonic-gate return (set_errno(EACCES));
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate if (p->p_sessp != ttoproc(curthread)->p_sessp) {
997c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
1007c478bd9Sstevel@tonic-gate return (set_errno(EPERM));
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate if (p->p_sessp->s_sid == pid) {
1057c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
1067c478bd9Sstevel@tonic-gate return (set_errno(EPERM));
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate if (pgid == 0)
1107c478bd9Sstevel@tonic-gate pgid = p->p_pid;
1117c478bd9Sstevel@tonic-gate else if (pgid < 0 || pgid >= maxpid) {
1127c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
1137c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate if (p->p_pgrp == pgid) {
1177c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
1187c478bd9Sstevel@tonic-gate break;
1197c478bd9Sstevel@tonic-gate } else if (p->p_pid == pgid) {
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * We need to protect p_pgidp with p_lock because
1227c478bd9Sstevel@tonic-gate * /proc looks at it while holding only p_lock.
1237c478bd9Sstevel@tonic-gate */
1247c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
1257c478bd9Sstevel@tonic-gate pgexit(p);
1267c478bd9Sstevel@tonic-gate pgjoin(p, p->p_pidp);
1277c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
1287c478bd9Sstevel@tonic-gate } else {
1297c478bd9Sstevel@tonic-gate register proc_t *q;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate if ((q = pgfind(pgid)) == NULL ||
1327c478bd9Sstevel@tonic-gate q->p_sessp != p->p_sessp) {
1337c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
1347c478bd9Sstevel@tonic-gate return (set_errno(EPERM));
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate * See comment above about p_lock and /proc
1387c478bd9Sstevel@tonic-gate */
1397c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock);
1407c478bd9Sstevel@tonic-gate pgexit(p);
1417c478bd9Sstevel@tonic-gate pgjoin(p, q->p_pgidp);
1427c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
1457c478bd9Sstevel@tonic-gate break;
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate case 0: /* getpgrp() */
1497c478bd9Sstevel@tonic-gate mutex_enter(&pidlock);
1507c478bd9Sstevel@tonic-gate retval = p->p_pgrp;
1517c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
1527c478bd9Sstevel@tonic-gate break;
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate case 2: /* getsid() */
1557c478bd9Sstevel@tonic-gate case 4: /* getpgid() */
1567c478bd9Sstevel@tonic-gate if (pid < 0 || pid >= maxpid) {
1577c478bd9Sstevel@tonic-gate return (set_errno(EINVAL));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate mutex_enter(&pidlock);
1607c478bd9Sstevel@tonic-gate if (pid != 0 && p->p_pid != pid &&
1617c478bd9Sstevel@tonic-gate ((p = prfind(pid)) == NULL || p->p_stat == SIDL)) {
1627c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
1637c478bd9Sstevel@tonic-gate return (set_errno(ESRCH));
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate if (flag == 2)
1667c478bd9Sstevel@tonic-gate retval = p->p_sessp->s_sid;
1677c478bd9Sstevel@tonic-gate else
1687c478bd9Sstevel@tonic-gate retval = p->p_pgrp;
1697c478bd9Sstevel@tonic-gate mutex_exit(&pidlock);
1707c478bd9Sstevel@tonic-gate break;
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate return (retval);
1747c478bd9Sstevel@tonic-gate }
175