xref: /illumos-gate/usr/src/uts/common/os/session.c (revision ad1660d085d30810ababff96bbae49ef2d5938ea)
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 /*
23*ad1660d0Smeem  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
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/file.h>
387c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/signal.h>
417c478bd9Sstevel@tonic-gate #include <sys/cred.h>
427c478bd9Sstevel@tonic-gate #include <sys/policy.h>
437c478bd9Sstevel@tonic-gate #include <sys/conf.h>
447c478bd9Sstevel@tonic-gate #include <sys/debug.h>
457c478bd9Sstevel@tonic-gate #include <sys/proc.h>
467c478bd9Sstevel@tonic-gate #include <sys/session.h>
477c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
487c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
497c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate sess_t session0 = {
527c478bd9Sstevel@tonic-gate 	1,	/* s_ref   */
537c478bd9Sstevel@tonic-gate 	NODEV,	/* s_dev   */
547c478bd9Sstevel@tonic-gate 	NULL,	/* s_vp    */
557c478bd9Sstevel@tonic-gate 	&pid0,	/* s_sidp  */
567c478bd9Sstevel@tonic-gate 	NULL	/* s_cred  */
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate void
607c478bd9Sstevel@tonic-gate sess_rele(sess_t *sp)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	ASSERT(sp->s_ref != 0);
657c478bd9Sstevel@tonic-gate 	if (--sp->s_ref == 0) {
667c478bd9Sstevel@tonic-gate 		if (sp == &session0)
677c478bd9Sstevel@tonic-gate 			panic("sp == &session0");
687c478bd9Sstevel@tonic-gate 		PID_RELE(sp->s_sidp);
697c478bd9Sstevel@tonic-gate 		mutex_destroy(&sp->s_lock);
707c478bd9Sstevel@tonic-gate 		cv_destroy(&sp->s_wait_cv);
717c478bd9Sstevel@tonic-gate 		kmem_free(sp, sizeof (sess_t));
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate void
767c478bd9Sstevel@tonic-gate sess_create(void)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	proc_t *pp;
797c478bd9Sstevel@tonic-gate 	sess_t *sp;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	pp = ttoproc(curthread);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	sp = kmem_zalloc(sizeof (sess_t), KM_SLEEP);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	mutex_init(&sp->s_lock, NULL, MUTEX_DEFAULT, NULL);
867c478bd9Sstevel@tonic-gate 	cv_init(&sp->s_wait_cv, NULL, CV_DEFAULT, NULL);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 * We need to protect p_pgidp with p_lock because
927c478bd9Sstevel@tonic-gate 	 * /proc looks at it while holding only p_lock.
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
957c478bd9Sstevel@tonic-gate 	pgexit(pp);
967c478bd9Sstevel@tonic-gate 	SESS_RELE(pp->p_sessp);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	sp->s_sidp = pp->p_pidp;
997c478bd9Sstevel@tonic-gate 	sp->s_ref = 1;
1007c478bd9Sstevel@tonic-gate 	sp->s_dev = NODEV;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	pp->p_sessp = sp;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	pgjoin(pp, pp->p_pidp);
1057c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	PID_HOLD(sp->s_sidp);
1087c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate void
1127c478bd9Sstevel@tonic-gate freectty(sess_t *sp)
1137c478bd9Sstevel@tonic-gate {
114*ad1660d0Smeem 	vnode_t *vp = sp->s_vp;
115*ad1660d0Smeem 	cred_t *cred = sp->s_cred;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	strfreectty(vp->v_stream);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	mutex_enter(&sp->s_lock);
1207c478bd9Sstevel@tonic-gate 	while (sp->s_cnt > 0) {
1217c478bd9Sstevel@tonic-gate 		cv_wait(&sp->s_wait_cv, &sp->s_lock);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	ASSERT(sp->s_cnt == 0);
1247c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_count >= 1);
1257c478bd9Sstevel@tonic-gate 	sp->s_vp = NULL;
126*ad1660d0Smeem 	sp->s_cred = NULL;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/*
129*ad1660d0Smeem 	 * It is possible for the VOP_CLOSE below to call stralloctty()
1307c478bd9Sstevel@tonic-gate 	 * and reallocate a new tty vnode.  To prevent that the
1317c478bd9Sstevel@tonic-gate 	 * session is marked as closing here.
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	sp->s_flag = SESS_CLOSE;
1357c478bd9Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/*
1387c478bd9Sstevel@tonic-gate 	 * This will be the only thread with access to
1397c478bd9Sstevel@tonic-gate 	 * this vnode, from this point on.
1407c478bd9Sstevel@tonic-gate 	 */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	(void) VOP_CLOSE(vp, 0, 1, (offset_t)0, cred);
1437c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	crfree(cred);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate  *	++++++++++++++++++++++++
1507c478bd9Sstevel@tonic-gate  *	++  SunOS4.1 Buyback  ++
1517c478bd9Sstevel@tonic-gate  *	++++++++++++++++++++++++
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  * vhangup: Revoke access of the current tty by all processes
1547c478bd9Sstevel@tonic-gate  * Used by privileged users to give a "clean" terminal at login
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate int
157*ad1660d0Smeem vhangup(void)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	if (secpolicy_sys_config(CRED(), B_FALSE) != 0)
1607c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
1617c478bd9Sstevel@tonic-gate 	/*
1627c478bd9Sstevel@tonic-gate 	 * This routine used to call freectty() under a condition that
1637c478bd9Sstevel@tonic-gate 	 * could never happen.  So this code has never actually done
164*ad1660d0Smeem 	 * anything, and evidently nobody has ever noticed.
1657c478bd9Sstevel@tonic-gate 	 */
1667c478bd9Sstevel@tonic-gate 	return (0);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate dev_t
1707c478bd9Sstevel@tonic-gate cttydev(proc_t *pp)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	sess_t *sp = pp->p_sessp;
1737c478bd9Sstevel@tonic-gate 	if (sp->s_vp == NULL)
1747c478bd9Sstevel@tonic-gate 		return (NODEV);
1757c478bd9Sstevel@tonic-gate 	return (sp->s_dev);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate void
1797c478bd9Sstevel@tonic-gate alloctty(proc_t *pp, vnode_t *vp)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	sess_t *sp = pp->p_sessp;
1827c478bd9Sstevel@tonic-gate 	cred_t *crp;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	sp->s_vp = vp;
1857c478bd9Sstevel@tonic-gate 	sp->s_dev = vp->v_rdev;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
1887c478bd9Sstevel@tonic-gate 	crhold(crp = pp->p_cred);
1897c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
1907c478bd9Sstevel@tonic-gate 	sp->s_cred = crp;
1917c478bd9Sstevel@tonic-gate }
192