tty_tty.c revision 1.1.1.3 1 /*-
2 * Copyright (c) 1982, 1986, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)tty_tty.c 8.4 (Berkeley) 5/14/95
34 */
35
36 /*
37 * Indirect driver for controlling tty.
38 */
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/conf.h>
42 #include <sys/ioctl.h>
43 #include <sys/proc.h>
44 #include <sys/tty.h>
45 #include <sys/vnode.h>
46 #include <sys/file.h>
47
48 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
49
50 /*ARGSUSED*/
51 cttyopen(dev, flag, mode, p)
52 dev_t dev;
53 int flag, mode;
54 struct proc *p;
55 {
56 struct vnode *ttyvp = cttyvp(p);
57 int error;
58
59 if (ttyvp == NULL)
60 return (ENXIO);
61 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
62 #ifdef PARANOID
63 /*
64 * Since group is tty and mode is 620 on most terminal lines
65 * and since sessions protect terminals from processes outside
66 * your session, this check is probably no longer necessary.
67 * Since it inhibits setuid root programs that later switch
68 * to another user from accessing /dev/tty, we have decided
69 * to delete this test. (mckusick 5/93)
70 */
71 error = VOP_ACCESS(ttyvp,
72 (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p);
73 if (!error)
74 #endif /* PARANOID */
75 error = VOP_OPEN(ttyvp, flag, NOCRED, p);
76 VOP_UNLOCK(ttyvp, 0, p);
77 return (error);
78 }
79
80 /*ARGSUSED*/
81 cttyread(dev, uio, flag)
82 dev_t dev;
83 struct uio *uio;
84 int flag;
85 {
86 struct proc *p = uio->uio_procp;
87 register struct vnode *ttyvp = cttyvp(p);
88 int error;
89
90 if (ttyvp == NULL)
91 return (EIO);
92 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
93 error = VOP_READ(ttyvp, uio, flag, NOCRED);
94 VOP_UNLOCK(ttyvp, 0, p);
95 return (error);
96 }
97
98 /*ARGSUSED*/
99 cttywrite(dev, uio, flag)
100 dev_t dev;
101 struct uio *uio;
102 int flag;
103 {
104 struct proc *p = uio->uio_procp;
105 struct vnode *ttyvp = cttyvp(uio->uio_procp);
106 int error;
107
108 if (ttyvp == NULL)
109 return (EIO);
110 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
111 error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
112 VOP_UNLOCK(ttyvp, 0, p);
113 return (error);
114 }
115
116 /*ARGSUSED*/
117 cttyioctl(dev, cmd, addr, flag, p)
118 dev_t dev;
119 u_long cmd;
120 caddr_t addr;
121 int flag;
122 struct proc *p;
123 {
124 struct vnode *ttyvp = cttyvp(p);
125
126 if (ttyvp == NULL)
127 return (EIO);
128 if (cmd == TIOCNOTTY) {
129 if (!SESS_LEADER(p)) {
130 p->p_flag &= ~P_CONTROLT;
131 return (0);
132 } else
133 return (EINVAL);
134 }
135 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
136 }
137
138 /*ARGSUSED*/
139 cttyselect(dev, flag, p)
140 dev_t dev;
141 int flag;
142 struct proc *p;
143 {
144 struct vnode *ttyvp = cttyvp(p);
145
146 if (ttyvp == NULL)
147 return (1); /* try operation to get EOF/failure */
148 return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p));
149 }
150