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