tty_tty.c revision 1.1.1.2 1 /*-
2 * Copyright (c) 1982, 1986, 1991, 1993
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.2 (Berkeley) 9/23/93
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 VOP_LOCK(ttyvp);
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);
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 register struct vnode *ttyvp = cttyvp(uio->uio_procp);
87 int error;
88
89 if (ttyvp == NULL)
90 return (EIO);
91 VOP_LOCK(ttyvp);
92 error = VOP_READ(ttyvp, uio, flag, NOCRED);
93 VOP_UNLOCK(ttyvp);
94 return (error);
95 }
96
97 /*ARGSUSED*/
98 cttywrite(dev, uio, flag)
99 dev_t dev;
100 struct uio *uio;
101 int flag;
102 {
103 register struct vnode *ttyvp = cttyvp(uio->uio_procp);
104 int error;
105
106 if (ttyvp == NULL)
107 return (EIO);
108 VOP_LOCK(ttyvp);
109 error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
110 VOP_UNLOCK(ttyvp);
111 return (error);
112 }
113
114 /*ARGSUSED*/
115 cttyioctl(dev, cmd, addr, flag, p)
116 dev_t dev;
117 int cmd;
118 caddr_t addr;
119 int flag;
120 struct proc *p;
121 {
122 struct vnode *ttyvp = cttyvp(p);
123
124 if (ttyvp == NULL)
125 return (EIO);
126 if (cmd == TIOCNOTTY) {
127 if (!SESS_LEADER(p)) {
128 p->p_flag &= ~P_CONTROLT;
129 return (0);
130 } else
131 return (EINVAL);
132 }
133 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
134 }
135
136 /*ARGSUSED*/
137 cttyselect(dev, flag, p)
138 dev_t dev;
139 int flag;
140 struct proc *p;
141 {
142 struct vnode *ttyvp = cttyvp(p);
143
144 if (ttyvp == NULL)
145 return (1); /* try operation to get EOF/failure */
146 return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p));
147 }
148