xref: /xnu-11417.140.69/bsd/kern/tty_tty.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1 /*
2  * Copyright (c) 1997-2013 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*-
29  * Copyright (c) 1982, 1986, 1991, 1993
30  *      The Regents of the University of California.  All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright
38  *    notice, this list of conditions and the following disclaimer in the
39  *    documentation and/or other materials provided with the distribution.
40  * 3. All advertising materials mentioning features or use of this software
41  *    must display the following acknowledgement:
42  *      This product includes software developed by the University of
43  *      California, Berkeley and its contributors.
44  * 4. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)tty_tty.c	8.2 (Berkeley) 9/23/93
61  */
62 
63 /*
64  * Indirect driver for controlling tty.
65  */
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/conf.h>
69 #include <sys/ioctl.h>
70 #include <sys/proc_internal.h>
71 #include <sys/tty.h>
72 #include <sys/vnode_internal.h>
73 #include <sys/file_internal.h>
74 #include <sys/kauth.h>
75 
76 /* Forward declarations for cdevsw[] entry */
77 /* XXX we should consider making these static */
78 int cttyopen(dev_t dev, int flag, int mode, proc_t p);
79 int cttyread(dev_t dev, struct uio *uio, int flag);
80 int cttywrite(dev_t dev, struct uio *uio, int flag);
81 int cttyioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, proc_t p);
82 int cttyselect(dev_t dev, int flag, void* wql, proc_t p);
83 static vnode_t cttyvp(proc_t p);
84 
85 int
cttyopen(dev_t dev,int flag,__unused int mode,proc_t p)86 cttyopen(dev_t dev, int flag, __unused int mode, proc_t p)
87 {
88 	vnode_t ttyvp = cttyvp(p);
89 	struct vfs_context context;
90 	int error = 0;
91 	int cttyflag, doclose = 0;
92 	struct session *sessp;
93 	struct pgrp *pg;
94 
95 	if (ttyvp == NULL) {
96 		return ENXIO;
97 	}
98 
99 	context.vc_thread = current_thread();
100 	context.vc_ucred = kauth_cred_proc_ref(p);
101 
102 	pg = proc_pgrp(p, &sessp);
103 	cttyflag = os_ref_get_raw_mask(&sessp->s_refcount) & S_CTTYREF;
104 
105 	/*
106 	 * A little hack--this device, used by many processes,
107 	 * happens to do an open on another device, which can
108 	 * cause unhappiness if the second-level open blocks indefinitely
109 	 * (as could be the case if the master side has hung up).  Since
110 	 * we know that this driver doesn't care about the serializing
111 	 * opens and closes, we can drop the lock. To avoid opencount leak,
112 	 * open the vnode only for the first time.
113 	 */
114 	if (cttyflag == 0) {
115 		devsw_unlock(dev, S_IFCHR);
116 		error = VNOP_OPEN(ttyvp, flag, &context);
117 		devsw_lock(dev, S_IFCHR);
118 
119 		if (error) {
120 			goto out;
121 		}
122 
123 		/*
124 		 * If S_CTTYREF is set, some other thread did an open
125 		 * and was able to set the flag, now perform a close, else
126 		 * set the flag.
127 		 */
128 		if (os_atomic_or_orig(&sessp->s_refcount, S_CTTYREF, relaxed) & S_CTTYREF) {
129 			doclose = 1;
130 		}
131 
132 		/*
133 		 * We have to take a reference here to make sure a close
134 		 * gets called during revoke. Note that once a controlling
135 		 * tty gets opened by this driver, the only way close will
136 		 * get called is when the session leader , whose controlling
137 		 * tty is ttyvp, exits and vnode is revoked. We cannot
138 		 * redirect close from this driver because underlying controlling
139 		 * terminal might change and close may get redirected to a
140 		 * wrong vnode causing panic.
141 		 */
142 		if (doclose) {
143 			devsw_unlock(dev, S_IFCHR);
144 			VNOP_CLOSE(ttyvp, flag, &context);
145 			devsw_lock(dev, S_IFCHR);
146 		} else {
147 			error = vnode_ref(ttyvp);
148 		}
149 	}
150 
151 out:
152 	pgrp_rele(pg);
153 	vnode_put(ttyvp);
154 	kauth_cred_unref(&context.vc_ucred);
155 
156 	return error;
157 }
158 
159 int
cttyread(__unused dev_t dev,struct uio * uio,int flag)160 cttyread(__unused dev_t dev, struct uio *uio, int flag)
161 {
162 	vnode_t ttyvp = cttyvp(current_proc());
163 	struct vfs_context context;
164 	int error;
165 
166 	if (ttyvp == NULL) {
167 		return EIO;
168 	}
169 
170 	context.vc_thread = current_thread();
171 	context.vc_ucred = NOCRED;
172 
173 	error = VNOP_READ(ttyvp, uio, flag, &context);
174 	vnode_put(ttyvp);
175 
176 	return error;
177 }
178 
179 int
cttywrite(__unused dev_t dev,struct uio * uio,int flag)180 cttywrite(__unused dev_t dev, struct uio *uio, int flag)
181 {
182 	vnode_t ttyvp = cttyvp(current_proc());
183 	struct vfs_context context;
184 	int error;
185 
186 	if (ttyvp == NULL) {
187 		return EIO;
188 	}
189 
190 	context.vc_thread = current_thread();
191 	context.vc_ucred = NOCRED;
192 
193 	error = VNOP_WRITE(ttyvp, uio, flag, &context);
194 	vnode_put(ttyvp);
195 
196 	return error;
197 }
198 
199 int
cttyioctl(__unused dev_t dev,u_long cmd,caddr_t addr,int flag,proc_t p)200 cttyioctl(__unused dev_t dev, u_long cmd, caddr_t addr, int flag, proc_t p)
201 {
202 	vnode_t ttyvp = cttyvp(current_proc());
203 	struct vfs_context context;
204 	int error = 0;
205 
206 	if (ttyvp == NULL) {
207 		return EIO;
208 	}
209 	if (cmd == TIOCSCTTY) {  /* don't allow controlling tty to be set    */
210 		error = EINVAL; /* to controlling tty -- infinite recursion */
211 		goto out;
212 	}
213 	if (cmd == TIOCNOTTY) {
214 		struct pgrp *pg = proc_pgrp(p, NULL);
215 
216 		if (!SESS_LEADER(p, pg->pg_session)) {
217 			os_atomic_andnot(&p->p_flag, P_CONTROLT, relaxed);
218 			error = 0;
219 		} else {
220 			error = EINVAL;
221 		}
222 		pgrp_rele(pg);
223 		goto out;
224 	}
225 	context.vc_thread = current_thread();
226 	context.vc_ucred = NOCRED;
227 
228 	error = VNOP_IOCTL(ttyvp, cmd, addr, flag, &context);
229 out:
230 	vnode_put(ttyvp);
231 	return error;
232 }
233 
234 int
cttyselect(__unused dev_t dev,int flag,void * wql,__unused proc_t p)235 cttyselect(__unused dev_t dev, int flag, void* wql, __unused proc_t p)
236 {
237 	vnode_t ttyvp = cttyvp(current_proc());
238 	struct vfs_context context;
239 	int error;
240 
241 	context.vc_thread = current_thread();
242 	context.vc_ucred = NOCRED;
243 
244 	if (ttyvp == NULL) {
245 		return 1;     /* try operation to get EOF/failure */
246 	}
247 	error = VNOP_SELECT(ttyvp, flag, FREAD | FWRITE, wql, &context);
248 	vnode_put(ttyvp);
249 	return error;
250 }
251 
252 /* This returns vnode with ioref */
253 static vnode_t
cttyvp(proc_t p)254 cttyvp(proc_t p)
255 {
256 	struct pgrp *pg;
257 	struct session *sessp;
258 	vnode_t vp;
259 	int vid;
260 
261 	pg = proc_pgrp(p, &sessp);
262 
263 	session_lock(sessp);
264 	vp = (p->p_flag & P_CONTROLT ? sessp->s_ttyvp : NULLVP);
265 	vid = sessp->s_ttyvid;
266 	if (vp) {
267 		vnode_hold(vp);
268 	}
269 	session_unlock(sessp);
270 
271 	pgrp_rele(pg);
272 
273 	if (vp != NULLVP) {
274 		/* cannot get an IO reference, return NULLVP */
275 		if (vnode_getwithvid(vp, vid) != 0) {
276 			vnode_drop(vp);
277 			vp = NULLVP;
278 		} else {
279 			vnode_drop(vp);
280 		}
281 	}
282 	return vp;
283 }
284