xref: /titanic_50/usr/src/uts/common/os/session.c (revision 108322fb1c3ed341aba9c80c9774df0ed9e35768)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/types.h>
34 #include <sys/sysmacros.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/file.h>
38 #include <sys/vnode.h>
39 #include <sys/errno.h>
40 #include <sys/signal.h>
41 #include <sys/cred.h>
42 #include <sys/policy.h>
43 #include <sys/conf.h>
44 #include <sys/debug.h>
45 #include <sys/proc.h>
46 #include <sys/session.h>
47 #include <sys/kmem.h>
48 #include <sys/cmn_err.h>
49 #include <sys/strsubr.h>
50 
51 sess_t session0 = {
52 	1,	/* s_ref   */
53 	NODEV,	/* s_dev   */
54 	NULL,	/* s_vp    */
55 	&pid0,	/* s_sidp  */
56 	NULL	/* s_cred  */
57 };
58 
59 void
60 sess_rele(sess_t *sp)
61 {
62 	ASSERT(MUTEX_HELD(&pidlock));
63 
64 	ASSERT(sp->s_ref != 0);
65 	if (--sp->s_ref == 0) {
66 		if (sp == &session0)
67 			panic("sp == &session0");
68 		PID_RELE(sp->s_sidp);
69 		mutex_destroy(&sp->s_lock);
70 		cv_destroy(&sp->s_wait_cv);
71 		kmem_free(sp, sizeof (sess_t));
72 	}
73 }
74 
75 void
76 sess_create(void)
77 {
78 	proc_t *pp;
79 	sess_t *sp;
80 
81 	pp = ttoproc(curthread);
82 
83 	sp = kmem_zalloc(sizeof (sess_t), KM_SLEEP);
84 
85 	mutex_init(&sp->s_lock, NULL, MUTEX_DEFAULT, NULL);
86 	cv_init(&sp->s_wait_cv, NULL, CV_DEFAULT, NULL);
87 
88 	mutex_enter(&pidlock);
89 
90 	/*
91 	 * We need to protect p_pgidp with p_lock because
92 	 * /proc looks at it while holding only p_lock.
93 	 */
94 	mutex_enter(&pp->p_lock);
95 	pgexit(pp);
96 	SESS_RELE(pp->p_sessp);
97 
98 	sp->s_sidp = pp->p_pidp;
99 	sp->s_ref = 1;
100 	sp->s_dev = NODEV;
101 
102 	pp->p_sessp = sp;
103 
104 	pgjoin(pp, pp->p_pidp);
105 	mutex_exit(&pp->p_lock);
106 
107 	PID_HOLD(sp->s_sidp);
108 	mutex_exit(&pidlock);
109 }
110 
111 void
112 freectty(sess_t *sp)
113 {
114 	vnode_t *vp = sp->s_vp;
115 	cred_t *cred = sp->s_cred;
116 
117 	strfreectty(vp->v_stream);
118 
119 	mutex_enter(&sp->s_lock);
120 	while (sp->s_cnt > 0) {
121 		cv_wait(&sp->s_wait_cv, &sp->s_lock);
122 	}
123 	ASSERT(sp->s_cnt == 0);
124 	ASSERT(vp->v_count >= 1);
125 	sp->s_vp = NULL;
126 	sp->s_cred = NULL;
127 
128 	/*
129 	 * It is possible for the VOP_CLOSE below to call stralloctty()
130 	 * and reallocate a new tty vnode.  To prevent that the
131 	 * session is marked as closing here.
132 	 */
133 
134 	sp->s_flag = SESS_CLOSE;
135 	mutex_exit(&sp->s_lock);
136 
137 	/*
138 	 * This will be the only thread with access to
139 	 * this vnode, from this point on.
140 	 */
141 
142 	(void) VOP_CLOSE(vp, 0, 1, (offset_t)0, cred);
143 	VN_RELE(vp);
144 
145 	crfree(cred);
146 }
147 
148 /*
149  *	++++++++++++++++++++++++
150  *	++  SunOS4.1 Buyback  ++
151  *	++++++++++++++++++++++++
152  *
153  * vhangup: Revoke access of the current tty by all processes
154  * Used by privileged users to give a "clean" terminal at login
155  */
156 int
157 vhangup(void)
158 {
159 	if (secpolicy_sys_config(CRED(), B_FALSE) != 0)
160 		return (set_errno(EPERM));
161 	/*
162 	 * This routine used to call freectty() under a condition that
163 	 * could never happen.  So this code has never actually done
164 	 * anything, and evidently nobody has ever noticed.
165 	 */
166 	return (0);
167 }
168 
169 dev_t
170 cttydev(proc_t *pp)
171 {
172 	sess_t *sp = pp->p_sessp;
173 	if (sp->s_vp == NULL)
174 		return (NODEV);
175 	return (sp->s_dev);
176 }
177 
178 void
179 alloctty(proc_t *pp, vnode_t *vp)
180 {
181 	sess_t *sp = pp->p_sessp;
182 	cred_t *crp;
183 
184 	sp->s_vp = vp;
185 	sp->s_dev = vp->v_rdev;
186 
187 	mutex_enter(&pp->p_crlock);
188 	crhold(crp = pp->p_cred);
189 	mutex_exit(&pp->p_crlock);
190 	sp->s_cred = crp;
191 }
192