]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/tty_tty.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / kern / tty_tty.c
CommitLineData
1c79356b 1/*
39236c6e 2 * Copyright (c) 1997-2013 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
1c79356b
A
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>
91447636 70#include <sys/proc_internal.h>
1c79356b 71#include <sys/tty.h>
91447636
A
72#include <sys/vnode_internal.h>
73#include <sys/file_internal.h>
2d21ac55 74#include <sys/kauth.h>
91447636
A
75
76/* Forward declarations for cdevsw[] entry */
77/* XXX we should consider making these static */
2d21ac55 78int cttyopen(dev_t dev, int flag, int mode, proc_t p);
91447636
A
79int cttyread(dev_t dev, struct uio *uio, int flag);
80int cttywrite(dev_t dev, struct uio *uio, int flag);
2d21ac55
A
81int cttyioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, proc_t p);
82int cttyselect(dev_t dev, int flag, void* wql, proc_t p);
83static vnode_t cttyvp(proc_t p);
1c79356b 84
1c79356b 85int
6d2010ae 86cttyopen(dev_t dev, int flag, __unused int mode, proc_t p)
1c79356b 87{
39236c6e
A
88 vnode_t ttyvp = cttyvp(p);
89 struct vfs_context context;
90 int error = 0;
91 int cttyflag, doclose = 0;
92 struct session *sessp;
1c79356b 93
0a7de745
A
94 if (ttyvp == NULL) {
95 return ENXIO;
96 }
316670eb 97
39236c6e
A
98 context.vc_thread = current_thread();
99 context.vc_ucred = kauth_cred_proc_ref(p);
316670eb 100
39236c6e
A
101 sessp = proc_session(p);
102 session_lock(sessp);
0a7de745 103 cttyflag = sessp->s_flags & S_CTTYREF;
39236c6e 104 session_unlock(sessp);
316670eb 105
39236c6e
A
106 /*
107 * A little hack--this device, used by many processes,
0a7de745
A
108 * happens to do an open on another device, which can
109 * cause unhappiness if the second-level open blocks indefinitely
39236c6e
A
110 * (as could be the case if the master side has hung up). Since
111 * we know that this driver doesn't care about the serializing
112 * opens and closes, we can drop the lock. To avoid opencount leak,
0a7de745 113 * open the vnode only for the first time.
39236c6e
A
114 */
115 if (cttyflag == 0) {
116 devsw_unlock(dev, S_IFCHR);
316670eb 117 error = VNOP_OPEN(ttyvp, flag, &context);
39236c6e
A
118 devsw_lock(dev, S_IFCHR);
119
0a7de745 120 if (error) {
39236c6e 121 goto out;
0a7de745
A
122 }
123
39236c6e
A
124 /*
125 * If S_CTTYREF is set, some other thread did an open
126 * and was able to set the flag, now perform a close, else
127 * set the flag.
128 */
129 session_lock(sessp);
0a7de745 130 if (cttyflag == (sessp->s_flags & S_CTTYREF)) {
39236c6e 131 sessp->s_flags |= S_CTTYREF;
0a7de745 132 } else {
39236c6e 133 doclose = 1;
0a7de745 134 }
39236c6e
A
135 session_unlock(sessp);
136
137 /*
138 * We have to take a reference here to make sure a close
0a7de745 139 * gets called during revoke. Note that once a controlling
39236c6e
A
140 * tty gets opened by this driver, the only way close will
141 * get called is when the session leader , whose controlling
0a7de745 142 * tty is ttyvp, exits and vnode is revoked. We cannot
39236c6e 143 * redirect close from this driver because underlying controlling
0a7de745 144 * terminal might change and close may get redirected to a
39236c6e
A
145 * wrong vnode causing panic.
146 */
147 if (doclose) {
148 devsw_unlock(dev, S_IFCHR);
149 VNOP_CLOSE(ttyvp, flag, &context);
150 devsw_lock(dev, S_IFCHR);
151 } else {
152 error = vnode_ref(ttyvp);
153 }
316670eb 154 }
39236c6e
A
155out:
156 session_rele(sessp);
316670eb 157
39236c6e
A
158 vnode_put(ttyvp);
159 kauth_cred_unref(&context.vc_ucred);
316670eb 160
0a7de745 161 return error;
1c79356b
A
162}
163
1c79356b 164int
91447636 165cttyread(__unused dev_t dev, struct uio *uio, int flag)
1c79356b 166{
2d21ac55 167 vnode_t ttyvp = cttyvp(current_proc());
91447636 168 struct vfs_context context;
1c79356b
A
169 int error;
170
0a7de745
A
171 if (ttyvp == NULL) {
172 return EIO;
173 }
91447636 174
2d21ac55 175 context.vc_thread = current_thread();
91447636
A
176 context.vc_ucred = NOCRED;
177
178 error = VNOP_READ(ttyvp, uio, flag, &context);
2d21ac55 179 vnode_put(ttyvp);
91447636 180
0a7de745 181 return error;
1c79356b
A
182}
183
1c79356b 184int
91447636 185cttywrite(__unused dev_t dev, struct uio *uio, int flag)
1c79356b 186{
2d21ac55 187 vnode_t ttyvp = cttyvp(current_proc());
91447636 188 struct vfs_context context;
1c79356b
A
189 int error;
190
0a7de745
A
191 if (ttyvp == NULL) {
192 return EIO;
193 }
91447636 194
2d21ac55 195 context.vc_thread = current_thread();
91447636
A
196 context.vc_ucred = NOCRED;
197
198 error = VNOP_WRITE(ttyvp, uio, flag, &context);
2d21ac55 199 vnode_put(ttyvp);
91447636 200
0a7de745 201 return error;
1c79356b
A
202}
203
1c79356b 204int
2d21ac55 205cttyioctl(__unused dev_t dev, u_long cmd, caddr_t addr, int flag, proc_t p)
1c79356b 206{
2d21ac55 207 vnode_t ttyvp = cttyvp(current_proc());
91447636 208 struct vfs_context context;
2d21ac55
A
209 struct session *sessp;
210 int error = 0;
1c79356b 211
0a7de745
A
212 if (ttyvp == NULL) {
213 return EIO;
214 }
215 if (cmd == TIOCSCTTY) { /* don't allow controlling tty to be set */
2d21ac55
A
216 error = EINVAL; /* to controlling tty -- infinite recursion */
217 goto out;
218 }
1c79356b 219 if (cmd == TIOCNOTTY) {
2d21ac55
A
220 sessp = proc_session(p);
221 if (!SESS_LEADER(p, sessp)) {
b0d623f7 222 OSBitAndAtomic(~((uint32_t)P_CONTROLT), &p->p_flag);
0a7de745 223 if (sessp != SESSION_NULL) {
2d21ac55 224 session_rele(sessp);
0a7de745 225 }
2d21ac55
A
226 error = 0;
227 goto out;
228 } else {
0a7de745 229 if (sessp != SESSION_NULL) {
2d21ac55 230 session_rele(sessp);
0a7de745 231 }
2d21ac55
A
232 error = EINVAL;
233 goto out;
234 }
1c79356b 235 }
2d21ac55 236 context.vc_thread = current_thread();
91447636
A
237 context.vc_ucred = NOCRED;
238
2d21ac55
A
239 error = VNOP_IOCTL(ttyvp, cmd, addr, flag, &context);
240out:
241 vnode_put(ttyvp);
0a7de745 242 return error;
1c79356b
A
243}
244
1c79356b 245int
2d21ac55 246cttyselect(__unused dev_t dev, int flag, void* wql, __unused proc_t p)
1c79356b 247{
2d21ac55 248 vnode_t ttyvp = cttyvp(current_proc());
91447636 249 struct vfs_context context;
2d21ac55 250 int error;
91447636 251
2d21ac55 252 context.vc_thread = current_thread();
91447636 253 context.vc_ucred = NOCRED;
1c79356b 254
0a7de745
A
255 if (ttyvp == NULL) {
256 return 1; /* try operation to get EOF/failure */
257 }
258 error = VNOP_SELECT(ttyvp, flag, FREAD | FWRITE, wql, &context);
2d21ac55 259 vnode_put(ttyvp);
0a7de745 260 return error;
1c79356b
A
261}
262
2d21ac55
A
263/* This returns vnode with ioref */
264static vnode_t
265cttyvp(proc_t p)
1c79356b 266{
2d21ac55
A
267 vnode_t vp;
268 int vid;
269 struct session *sessp;
1c79356b 270
2d21ac55 271 sessp = proc_session(p);
1c79356b 272
2d21ac55
A
273 session_lock(sessp);
274 vp = (p->p_flag & P_CONTROLT ? sessp->s_ttyvp : NULLVP);
0a7de745 275 vid = sessp->s_ttyvid;
2d21ac55
A
276 session_unlock(sessp);
277
278 session_rele(sessp);
279
280 if (vp != NULLVP) {
281 /* cannot get an IO reference, return NULLVP */
0a7de745 282 if (vnode_getwithvid(vp, vid) != 0) {
2d21ac55 283 vp = NULLVP;
0a7de745 284 }
2d21ac55 285 }
0a7de745 286 return vp;
2d21ac55 287}