]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
316670eb | 2 | * Copyright (c) 1997-2012 Apple Computer, Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
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 | |
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. | |
8f6c56a5 | 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 | 78 | int cttyopen(dev_t dev, int flag, int mode, proc_t p); |
316670eb | 79 | int cttyclose(dev_t dev, int flag, int mode, proc_t p); |
91447636 A |
80 | int cttyread(dev_t dev, struct uio *uio, int flag); |
81 | int cttywrite(dev_t dev, struct uio *uio, int flag); | |
2d21ac55 A |
82 | int cttyioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, proc_t p); |
83 | int cttyselect(dev_t dev, int flag, void* wql, proc_t p); | |
84 | static vnode_t cttyvp(proc_t p); | |
1c79356b | 85 | |
1c79356b | 86 | int |
6d2010ae | 87 | cttyopen(dev_t dev, int flag, __unused int mode, proc_t p) |
1c79356b | 88 | { |
316670eb | 89 | vnode_t ttyvp; |
1c79356b A |
90 | int error; |
91 | ||
6d2010ae A |
92 | /* |
93 | * A little hack--this device, used by many processes, | |
316670eb A |
94 | * does an open on another device, which can cause unhappiness |
95 | * if the second-level open blocks indefinitely (e.g. if the | |
96 | * master side has hung up). This driver doesn't care | |
97 | * about serializing opens and closes, so drop the lock. | |
6d2010ae A |
98 | */ |
99 | devsw_unlock(dev, S_IFCHR); | |
316670eb A |
100 | |
101 | if ((ttyvp = cttyvp(p)) == NULL) { | |
102 | error = ENXIO; | |
103 | } else { | |
104 | struct vfs_context context; | |
105 | ||
106 | context.vc_thread = current_thread(); | |
107 | context.vc_ucred = kauth_cred_proc_ref(p); | |
108 | ||
109 | error = VNOP_OPEN(ttyvp, flag, &context); | |
110 | ||
111 | kauth_cred_unref(&context.vc_ucred); | |
112 | vnode_put(ttyvp); | |
113 | } | |
114 | ||
6d2010ae | 115 | devsw_lock(dev, S_IFCHR); |
316670eb A |
116 | return (error); |
117 | } | |
6d2010ae | 118 | |
316670eb A |
119 | /* |
120 | * This driver is marked D_TRACKCLOSE and so gets a close | |
121 | * for every open so that ttyvp->v_specinfo->si_count can be kept sane. | |
122 | */ | |
123 | int | |
124 | cttyclose(dev_t dev, int flag, __unused int mode, proc_t p) | |
125 | { | |
126 | vnode_t ttyvp; | |
127 | int error; | |
128 | ||
129 | /* See locking commentary above. */ | |
91447636 | 130 | |
316670eb A |
131 | devsw_unlock(dev, S_IFCHR); |
132 | ||
133 | if ((ttyvp = cttyvp(p)) == NULL) { | |
134 | error = ENXIO; | |
135 | } else { | |
136 | struct vfs_context context; | |
137 | ||
138 | context.vc_thread = current_thread(); | |
139 | context.vc_ucred = kauth_cred_proc_ref(p); | |
140 | ||
141 | error = VNOP_CLOSE(ttyvp, flag, &context); | |
142 | ||
143 | kauth_cred_unref(&context.vc_ucred); | |
144 | vnode_put(ttyvp); | |
145 | } | |
146 | ||
147 | devsw_lock(dev, S_IFCHR); | |
1c79356b A |
148 | return (error); |
149 | } | |
150 | ||
1c79356b | 151 | int |
91447636 | 152 | cttyread(__unused dev_t dev, struct uio *uio, int flag) |
1c79356b | 153 | { |
2d21ac55 | 154 | vnode_t ttyvp = cttyvp(current_proc()); |
91447636 | 155 | struct vfs_context context; |
1c79356b A |
156 | int error; |
157 | ||
158 | if (ttyvp == NULL) | |
159 | return (EIO); | |
91447636 | 160 | |
2d21ac55 | 161 | context.vc_thread = current_thread(); |
91447636 A |
162 | context.vc_ucred = NOCRED; |
163 | ||
164 | error = VNOP_READ(ttyvp, uio, flag, &context); | |
2d21ac55 | 165 | vnode_put(ttyvp); |
91447636 | 166 | |
1c79356b A |
167 | return (error); |
168 | } | |
169 | ||
1c79356b | 170 | int |
91447636 | 171 | cttywrite(__unused dev_t dev, struct uio *uio, int flag) |
1c79356b | 172 | { |
2d21ac55 | 173 | vnode_t ttyvp = cttyvp(current_proc()); |
91447636 | 174 | struct vfs_context context; |
1c79356b A |
175 | int error; |
176 | ||
177 | if (ttyvp == NULL) | |
178 | return (EIO); | |
91447636 | 179 | |
2d21ac55 | 180 | context.vc_thread = current_thread(); |
91447636 A |
181 | context.vc_ucred = NOCRED; |
182 | ||
183 | error = VNOP_WRITE(ttyvp, uio, flag, &context); | |
2d21ac55 | 184 | vnode_put(ttyvp); |
91447636 | 185 | |
1c79356b A |
186 | return (error); |
187 | } | |
188 | ||
1c79356b | 189 | int |
2d21ac55 | 190 | cttyioctl(__unused dev_t dev, u_long cmd, caddr_t addr, int flag, proc_t p) |
1c79356b | 191 | { |
2d21ac55 | 192 | vnode_t ttyvp = cttyvp(current_proc()); |
91447636 | 193 | struct vfs_context context; |
2d21ac55 A |
194 | struct session *sessp; |
195 | int error = 0; | |
1c79356b A |
196 | |
197 | if (ttyvp == NULL) | |
198 | return (EIO); | |
2d21ac55 A |
199 | if (cmd == TIOCSCTTY) { /* don't allow controlling tty to be set */ |
200 | error = EINVAL; /* to controlling tty -- infinite recursion */ | |
201 | goto out; | |
202 | } | |
1c79356b | 203 | if (cmd == TIOCNOTTY) { |
2d21ac55 A |
204 | sessp = proc_session(p); |
205 | if (!SESS_LEADER(p, sessp)) { | |
b0d623f7 | 206 | OSBitAndAtomic(~((uint32_t)P_CONTROLT), &p->p_flag); |
2d21ac55 A |
207 | if (sessp != SESSION_NULL) |
208 | session_rele(sessp); | |
209 | error = 0; | |
210 | goto out; | |
211 | } else { | |
212 | if (sessp != SESSION_NULL) | |
213 | session_rele(sessp); | |
214 | error = EINVAL; | |
215 | goto out; | |
216 | } | |
1c79356b | 217 | } |
2d21ac55 | 218 | context.vc_thread = current_thread(); |
91447636 A |
219 | context.vc_ucred = NOCRED; |
220 | ||
2d21ac55 A |
221 | error = VNOP_IOCTL(ttyvp, cmd, addr, flag, &context); |
222 | out: | |
223 | vnode_put(ttyvp); | |
224 | return (error); | |
1c79356b A |
225 | } |
226 | ||
1c79356b | 227 | int |
2d21ac55 | 228 | cttyselect(__unused dev_t dev, int flag, void* wql, __unused proc_t p) |
1c79356b | 229 | { |
2d21ac55 | 230 | vnode_t ttyvp = cttyvp(current_proc()); |
91447636 | 231 | struct vfs_context context; |
2d21ac55 | 232 | int error; |
91447636 | 233 | |
2d21ac55 | 234 | context.vc_thread = current_thread(); |
91447636 | 235 | context.vc_ucred = NOCRED; |
1c79356b A |
236 | |
237 | if (ttyvp == NULL) | |
238 | return (1); /* try operation to get EOF/failure */ | |
2d21ac55 A |
239 | error = VNOP_SELECT(ttyvp, flag, FREAD|FWRITE, wql, &context); |
240 | vnode_put(ttyvp); | |
241 | return (error); | |
1c79356b A |
242 | } |
243 | ||
2d21ac55 A |
244 | /* This returns vnode with ioref */ |
245 | static vnode_t | |
246 | cttyvp(proc_t p) | |
1c79356b | 247 | { |
2d21ac55 A |
248 | vnode_t vp; |
249 | int vid; | |
250 | struct session *sessp; | |
1c79356b | 251 | |
2d21ac55 | 252 | sessp = proc_session(p); |
1c79356b | 253 | |
2d21ac55 A |
254 | session_lock(sessp); |
255 | vp = (p->p_flag & P_CONTROLT ? sessp->s_ttyvp : NULLVP); | |
256 | vid = sessp->s_ttyvid; | |
257 | session_unlock(sessp); | |
258 | ||
259 | session_rele(sessp); | |
260 | ||
261 | if (vp != NULLVP) { | |
262 | /* cannot get an IO reference, return NULLVP */ | |
263 | if (vnode_getwithvid(vp, vid) != 0) | |
264 | vp = NULLVP; | |
265 | } | |
266 | return(vp); | |
267 | } | |
1c79356b | 268 |