]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/tty_tty.c
xnu-1504.9.17.tar.gz
[apple/xnu.git] / bsd / kern / tty_tty.c
1 /*
2 * Copyright (c) 1997-2006 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
86 int
87 cttyopen(__unused dev_t dev, int flag, __unused int mode, proc_t p)
88 {
89 vnode_t ttyvp = cttyvp(p);
90 struct vfs_context context;
91 int error;
92
93 if (ttyvp == NULL)
94 return (ENXIO);
95
96 context.vc_thread = current_thread();
97 context.vc_ucred = kauth_cred_proc_ref(p);
98
99 error = VNOP_OPEN(ttyvp, flag, &context);
100 vnode_put(ttyvp);
101 kauth_cred_unref(&context.vc_ucred);
102
103 return (error);
104 }
105
106 int
107 cttyread(__unused dev_t dev, struct uio *uio, int flag)
108 {
109 vnode_t ttyvp = cttyvp(current_proc());
110 struct vfs_context context;
111 int error;
112
113 if (ttyvp == NULL)
114 return (EIO);
115
116 context.vc_thread = current_thread();
117 context.vc_ucred = NOCRED;
118
119 error = VNOP_READ(ttyvp, uio, flag, &context);
120 vnode_put(ttyvp);
121
122 return (error);
123 }
124
125 int
126 cttywrite(__unused dev_t dev, struct uio *uio, int flag)
127 {
128 vnode_t ttyvp = cttyvp(current_proc());
129 struct vfs_context context;
130 int error;
131
132 if (ttyvp == NULL)
133 return (EIO);
134
135 context.vc_thread = current_thread();
136 context.vc_ucred = NOCRED;
137
138 error = VNOP_WRITE(ttyvp, uio, flag, &context);
139 vnode_put(ttyvp);
140
141 return (error);
142 }
143
144 int
145 cttyioctl(__unused dev_t dev, u_long cmd, caddr_t addr, int flag, proc_t p)
146 {
147 vnode_t ttyvp = cttyvp(current_proc());
148 struct vfs_context context;
149 struct session *sessp;
150 int error = 0;
151
152 if (ttyvp == NULL)
153 return (EIO);
154 if (cmd == TIOCSCTTY) { /* don't allow controlling tty to be set */
155 error = EINVAL; /* to controlling tty -- infinite recursion */
156 goto out;
157 }
158 if (cmd == TIOCNOTTY) {
159 sessp = proc_session(p);
160 if (!SESS_LEADER(p, sessp)) {
161 OSBitAndAtomic(~((uint32_t)P_CONTROLT), &p->p_flag);
162 if (sessp != SESSION_NULL)
163 session_rele(sessp);
164 error = 0;
165 goto out;
166 } else {
167 if (sessp != SESSION_NULL)
168 session_rele(sessp);
169 error = EINVAL;
170 goto out;
171 }
172 }
173 context.vc_thread = current_thread();
174 context.vc_ucred = NOCRED;
175
176 error = VNOP_IOCTL(ttyvp, cmd, addr, flag, &context);
177 out:
178 vnode_put(ttyvp);
179 return (error);
180 }
181
182 int
183 cttyselect(__unused dev_t dev, int flag, void* wql, __unused proc_t p)
184 {
185 vnode_t ttyvp = cttyvp(current_proc());
186 struct vfs_context context;
187 int error;
188
189 context.vc_thread = current_thread();
190 context.vc_ucred = NOCRED;
191
192 if (ttyvp == NULL)
193 return (1); /* try operation to get EOF/failure */
194 error = VNOP_SELECT(ttyvp, flag, FREAD|FWRITE, wql, &context);
195 vnode_put(ttyvp);
196 return (error);
197 }
198
199 /* This returns vnode with ioref */
200 static vnode_t
201 cttyvp(proc_t p)
202 {
203 vnode_t vp;
204 int vid;
205 struct session *sessp;
206
207 sessp = proc_session(p);
208
209 session_lock(sessp);
210 vp = (p->p_flag & P_CONTROLT ? sessp->s_ttyvp : NULLVP);
211 vid = sessp->s_ttyvid;
212 session_unlock(sessp);
213
214 session_rele(sessp);
215
216 if (vp != NULLVP) {
217 /* cannot get an IO reference, return NULLVP */
218 if (vnode_getwithvid(vp, vid) != 0)
219 vp = NULLVP;
220 }
221 return(vp);
222 }
223