FreeBSD/Linux Kernel Cross Reference
sys/kern/tty_tty.c
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 * $FreeBSD$
35 */
36
37 /*
38 * Indirect driver for controlling tty.
39 */
40
41 #include "opt_devfs.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/conf.h>
46 #include <sys/lock.h>
47 #include <sys/proc.h>
48 #include <sys/ttycom.h>
49 #include <sys/vnode.h>
50 #include <sys/kernel.h>
51 #ifdef DEVFS
52 #include <sys/devfsext.h>
53 #endif /*DEVFS*/
54
55 static d_open_t cttyopen;
56 static d_read_t cttyread;
57 static d_write_t cttywrite;
58 static d_ioctl_t cttyioctl;
59 static d_poll_t cttypoll;
60
61 #define CDEV_MAJOR 1
62 /* Don't make this static, since fdesc_vnops uses it. */
63 struct cdevsw ctty_cdevsw = {
64 cttyopen, nullclose, cttyread, cttywrite,
65 cttyioctl, nullstop, nullreset, nodevtotty,
66 cttypoll, nommap, NULL, "ctty",
67 NULL, -1, nodump, nopsize,
68 D_TTY,
69 };
70
71 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
72
73 /*ARGSUSED*/
74 static int
75 cttyopen(dev, flag, mode, p)
76 dev_t dev;
77 int flag, mode;
78 struct proc *p;
79 {
80 struct vnode *ttyvp = cttyvp(p);
81 int error;
82
83 if (ttyvp == NULL)
84 return (ENXIO);
85 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
86 #ifdef PARANOID
87 /*
88 * Since group is tty and mode is 620 on most terminal lines
89 * and since sessions protect terminals from processes outside
90 * your session, this check is probably no longer necessary.
91 * Since it inhibits setuid root programs that later switch
92 * to another user from accessing /dev/tty, we have decided
93 * to delete this test. (mckusick 5/93)
94 */
95 error = VOP_ACCESS(ttyvp,
96 (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p);
97 if (!error)
98 #endif /* PARANOID */
99 error = VOP_OPEN(ttyvp, flag, NOCRED, p);
100 VOP_UNLOCK(ttyvp, 0, p);
101 return (error);
102 }
103
104 /*ARGSUSED*/
105 static int
106 cttyread(dev, uio, flag)
107 dev_t dev;
108 struct uio *uio;
109 int flag;
110 {
111 struct proc *p = uio->uio_procp;
112 register struct vnode *ttyvp = cttyvp(p);
113 int error;
114
115 if (ttyvp == NULL)
116 return (EIO);
117 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
118 error = VOP_READ(ttyvp, uio, flag, NOCRED);
119 VOP_UNLOCK(ttyvp, 0, p);
120 return (error);
121 }
122
123 /*ARGSUSED*/
124 static int
125 cttywrite(dev, uio, flag)
126 dev_t dev;
127 struct uio *uio;
128 int flag;
129 {
130 struct proc *p = uio->uio_procp;
131 struct vnode *ttyvp = cttyvp(uio->uio_procp);
132 int error;
133
134 if (ttyvp == NULL)
135 return (EIO);
136 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
137 error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
138 VOP_UNLOCK(ttyvp, 0, p);
139 return (error);
140 }
141
142 /*ARGSUSED*/
143 static int
144 cttyioctl(dev, cmd, addr, flag, p)
145 dev_t dev;
146 u_long cmd;
147 caddr_t addr;
148 int flag;
149 struct proc *p;
150 {
151 struct vnode *ttyvp = cttyvp(p);
152
153 if (ttyvp == NULL)
154 return (EIO);
155 if (cmd == TIOCSCTTY) /* don't allow controlling tty to be set */
156 return EINVAL; /* to controlling tty -- infinite recursion */
157 if (cmd == TIOCNOTTY) {
158 if (!SESS_LEADER(p)) {
159 p->p_flag &= ~P_CONTROLT;
160 return (0);
161 } else
162 return (EINVAL);
163 }
164 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
165 }
166
167 /*ARGSUSED*/
168 static int
169 cttypoll(dev, events, p)
170 dev_t dev;
171 int events;
172 struct proc *p;
173 {
174 struct vnode *ttyvp = cttyvp(p);
175
176 if (ttyvp == NULL)
177 /* try operation to get EOF/failure */
178 return (seltrue(dev, events, p));
179 return (VOP_POLL(ttyvp, events, p->p_ucred, p));
180 }
181
182 static int ctty_devsw_installed;
183 #ifdef DEVFS
184 static void *ctty_devfs_token;
185 #endif
186
187 static void ctty_drvinit __P((void *unused));
188 static void
189 ctty_drvinit(unused)
190 void *unused;
191 {
192 dev_t dev;
193
194 if( ! ctty_devsw_installed ) {
195 dev = makedev(CDEV_MAJOR,0);
196 cdevsw_add(&dev,&ctty_cdevsw,NULL);
197 ctty_devsw_installed = 1;
198 #ifdef DEVFS
199 ctty_devfs_token =
200 devfs_add_devswf(&ctty_cdevsw, 0, DV_CHR, 0, 0,
201 0666, "tty");
202 #endif
203 }
204 }
205
206 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)
Cache object: 5e72c54edf32cea24d0de490280dcad9
|