]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/tty_tty.c
xnu-517.3.15.tar.gz
[apple/xnu.git] / bsd / kern / tty_tty.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
26/*-
27 * Copyright (c) 1982, 1986, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)tty_tty.c 8.2 (Berkeley) 9/23/93
59 */
60
61/*
62 * Indirect driver for controlling tty.
63 */
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/conf.h>
67#include <sys/ioctl.h>
68#include <sys/proc.h>
69#include <sys/tty.h>
70#include <sys/vnode.h>
71#include <sys/file.h>
72#ifndef NeXT
73#include <sys/kernel.h>
74#ifdef DEVFS
75#include <sys/devfsext.h>
76#endif /*DEVFS*/
77
78static d_open_t cttyopen;
79static d_read_t cttyread;
80static d_write_t cttywrite;
81static d_ioctl_t cttyioctl;
82static d_select_t cttyselect;
83
84#define CDEV_MAJOR 1
85/* Don't make static, fdesc_vnops uses this. */
86struct cdevsw ctty_cdevsw =
87 { cttyopen, nullclose, cttyread, cttywrite, /*1*/
88 cttyioctl, nullstop, nullreset, nodevtotty,/* tty */
89 cttyselect, nommap, NULL, "ctty", NULL, -1 };
90
91#endif /* !NeXT */
92
93#define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
94
95/*ARGSUSED*/
96int
97cttyopen(dev, flag, mode, p)
98 dev_t dev;
99 int flag, mode;
100 struct proc *p;
101{
102 struct vnode *ttyvp = cttyvp(p);
103 int error;
104
105 if (ttyvp == NULL)
106 return (ENXIO);
107#ifndef NeXT
108 VOP_LOCK(ttyvp);
109#else
110 /*
111 * This is the only place that NeXT Guarding has been used for
112 * VOP_.*LOCK style calls. Note all of the other diffs should
113 * use the three paramater lock/unlock.
114 */
115 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
116#endif
117
118#ifdef PARANOID
119 /*
120 * Since group is tty and mode is 620 on most terminal lines
121 * and since sessions protect terminals from processes outside
122 * your session, this check is probably no longer necessary.
123 * Since it inhibits setuid root programs that later switch
124 * to another user from accessing /dev/tty, we have decided
125 * to delete this test. (mckusick 5/93)
126 */
127 error = VOP_ACCESS(ttyvp,
128 (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p);
129 if (!error)
130#endif /* PARANOID */
131 error = VOP_OPEN(ttyvp, flag, NOCRED, p);
132 VOP_UNLOCK(ttyvp, 0, p);
133 return (error);
134}
135
136/*ARGSUSED*/
137int
138cttyread(dev, uio, flag)
139 dev_t dev;
140 struct uio *uio;
141 int flag;
142{
143 struct proc *p = uio->uio_procp;
144 register struct vnode *ttyvp = cttyvp(uio->uio_procp);
145 int error;
146
147 if (ttyvp == NULL)
148 return (EIO);
149 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
150 error = VOP_READ(ttyvp, uio, flag, NOCRED);
151 VOP_UNLOCK(ttyvp, 0, p);
152 return (error);
153}
154
155/*ARGSUSED*/
156int
157cttywrite(dev, uio, flag)
158 dev_t dev;
159 struct uio *uio;
160 int flag;
161{
162 struct proc *p = uio->uio_procp;
163 register struct vnode *ttyvp = cttyvp(uio->uio_procp);
164 int error;
165
166 if (ttyvp == NULL)
167 return (EIO);
168 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
169 error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
170 VOP_UNLOCK(ttyvp, 0, p);
171 return (error);
172}
173
174/*ARGSUSED*/
175#ifndef NeXT
176static int
177cttyioctl(dev, cmd, addr, flag, p)
178 dev_t dev;
179 int cmd;
180 caddr_t addr;
181 int flag;
182 struct proc *p;
183#else
184int
185cttyioctl(dev, cmd, addr, flag, p)
186 dev_t dev;
187 u_long cmd;
188 caddr_t addr;
189 int flag;
190 struct proc *p;
191#endif /* !NeXT */
192{
193 struct vnode *ttyvp = cttyvp(p);
194
195 if (ttyvp == NULL)
196 return (EIO);
197 if (cmd == TIOCSCTTY) /* don't allow controlling tty to be set */
198 return EINVAL; /* to controlling tty -- infinite recursion */
199 if (cmd == TIOCNOTTY) {
200 if (!SESS_LEADER(p)) {
201 p->p_flag &= ~P_CONTROLT;
202 return (0);
203 } else
204 return (EINVAL);
205 }
206 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
207}
208
209/*ARGSUSED*/
210int
0b4e3aa0 211cttyselect(dev, flag, wql, p)
1c79356b
A
212 dev_t dev;
213 int flag;
0b4e3aa0 214 void * wql;
1c79356b
A
215 struct proc *p;
216{
217 struct vnode *ttyvp = cttyvp(p);
218
219 if (ttyvp == NULL)
220 return (1); /* try operation to get EOF/failure */
0b4e3aa0 221 return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, wql, p));
1c79356b
A
222}
223
224#ifndef NeXT
225static ctty_devsw_installed = 0;
226#ifdef DEVFS
227static void *ctty_devfs_token;
228#endif
229
230static void
231ctty_drvinit(void *unused)
232{
233 dev_t dev;
234
235 if( ! ctty_devsw_installed ) {
236 dev = makedev(CDEV_MAJOR,0);
237 cdevsw_add(&dev,&ctty_cdevsw,NULL);
238 ctty_devsw_installed = 1;
239#ifdef DEVFS
240 ctty_devfs_token =
241 devfs_add_devswf(&ctty_cdevsw, 0, DV_CHR, 0, 0,
242 0666, "tty");
243#endif
244 }
245}
246
247SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)
248
249
250#endif /* !NeXT */