]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/tty_tty.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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.
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
58 * @(#)tty_tty.c 8.2 (Berkeley) 9/23/93
62 * Indirect driver for controlling tty.
64 #include <sys/param.h>
65 #include <sys/systm.h>
67 #include <sys/ioctl.h>
70 #include <sys/vnode.h>
73 #include <sys/kernel.h>
75 #include <sys/devfsext.h>
78 static d_open_t cttyopen
;
79 static d_read_t cttyread
;
80 static d_write_t cttywrite
;
81 static d_ioctl_t cttyioctl
;
82 static d_select_t cttyselect
;
85 /* Don't make static, fdesc_vnops uses this. */
86 struct cdevsw ctty_cdevsw
=
87 { cttyopen
, nullclose
, cttyread
, cttywrite
, /*1*/
88 cttyioctl
, nullstop
, nullreset
, nodevtotty
,/* tty */
89 cttyselect
, nommap
, NULL
, "ctty", NULL
, -1 };
93 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
97 cttyopen(dev
, flag
, mode
, p
)
102 struct vnode
*ttyvp
= cttyvp(p
);
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.
115 vn_lock(ttyvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
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)
127 error
= VOP_ACCESS(ttyvp
,
128 (flag
&FREAD
? VREAD
: 0) | (flag
&FWRITE
? VWRITE
: 0), p
->p_ucred
, p
);
130 #endif /* PARANOID */
131 error
= VOP_OPEN(ttyvp
, flag
, NOCRED
, p
);
132 VOP_UNLOCK(ttyvp
, 0, p
);
138 cttyread(dev
, uio
, flag
)
143 struct proc
*p
= uio
->uio_procp
;
144 register struct vnode
*ttyvp
= cttyvp(uio
->uio_procp
);
149 vn_lock(ttyvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
150 error
= VOP_READ(ttyvp
, uio
, flag
, NOCRED
);
151 VOP_UNLOCK(ttyvp
, 0, p
);
157 cttywrite(dev
, uio
, flag
)
162 struct proc
*p
= uio
->uio_procp
;
163 register struct vnode
*ttyvp
= cttyvp(uio
->uio_procp
);
168 vn_lock(ttyvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
169 error
= VOP_WRITE(ttyvp
, uio
, flag
, NOCRED
);
170 VOP_UNLOCK(ttyvp
, 0, p
);
177 cttyioctl(dev
, cmd
, addr
, flag
, p
)
185 cttyioctl(dev
, cmd
, addr
, flag
, p
)
193 struct vnode
*ttyvp
= cttyvp(p
);
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
;
206 return (VOP_IOCTL(ttyvp
, cmd
, addr
, flag
, NOCRED
, p
));
211 cttyselect(dev
, flag
, wql
, p
)
217 struct vnode
*ttyvp
= cttyvp(p
);
220 return (1); /* try operation to get EOF/failure */
221 return (VOP_SELECT(ttyvp
, flag
, FREAD
|FWRITE
, NOCRED
, wql
, p
));
225 static ctty_devsw_installed
= 0;
227 static void *ctty_devfs_token
;
231 ctty_drvinit(void *unused
)
235 if( ! ctty_devsw_installed
) {
236 dev
= makedev(CDEV_MAJOR
,0);
237 cdevsw_add(&dev
,&ctty_cdevsw
,NULL
);
238 ctty_devsw_installed
= 1;
241 devfs_add_devswf(&ctty_cdevsw
, 0, DV_CHR
, 0, 0,
247 SYSINIT(cttydev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,ctty_drvinit
,NULL
)