Deleted Added
full compact
tty_tty.c (83366) tty_tty.c (91140)
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 * $FreeBSD: head/sys/kern/tty_tty.c 83366 2001-09-12 08:38:13Z julian $
34 * $FreeBSD: head/sys/kern/tty_tty.c 91140 2002-02-23 11:12:57Z tanimura $
35 */
36
37/*
38 * Indirect driver for controlling tty.
39 */
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/conf.h>
44#include <sys/kernel.h>
45#include <sys/lock.h>
35 */
36
37/*
38 * Indirect driver for controlling tty.
39 */
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/conf.h>
44#include <sys/kernel.h>
45#include <sys/lock.h>
46#include <sys/mutex.h>
47#include <sys/sx.h>
46#include <sys/proc.h>
47#include <sys/ttycom.h>
48#include <sys/vnode.h>
49
50static d_open_t cttyopen;
51static d_read_t cttyread;
52static d_write_t cttywrite;
53static d_ioctl_t cttyioctl;
54static d_poll_t cttypoll;
55
56#define CDEV_MAJOR 1
57
58static struct cdevsw ctty_cdevsw = {
59 /* open */ cttyopen,
60 /* close */ nullclose,
61 /* read */ cttyread,
62 /* write */ cttywrite,
63 /* ioctl */ cttyioctl,
64 /* poll */ cttypoll,
65 /* mmap */ nommap,
66 /* strategy */ nostrategy,
67 /* name */ "ctty",
68 /* maj */ CDEV_MAJOR,
69 /* dump */ nodump,
70 /* psize */ nopsize,
71 /* flags */ D_TTY,
72};
73
74#define cttyvp(td) ((td)->td_proc->p_flag & P_CONTROLT ? (td)->td_proc->p_session->s_ttyvp : NULL)
75
76/*ARGSUSED*/
77static int
78cttyopen(dev, flag, mode, td)
79 dev_t dev;
80 int flag, mode;
81 struct thread *td;
82{
48#include <sys/proc.h>
49#include <sys/ttycom.h>
50#include <sys/vnode.h>
51
52static d_open_t cttyopen;
53static d_read_t cttyread;
54static d_write_t cttywrite;
55static d_ioctl_t cttyioctl;
56static d_poll_t cttypoll;
57
58#define CDEV_MAJOR 1
59
60static struct cdevsw ctty_cdevsw = {
61 /* open */ cttyopen,
62 /* close */ nullclose,
63 /* read */ cttyread,
64 /* write */ cttywrite,
65 /* ioctl */ cttyioctl,
66 /* poll */ cttypoll,
67 /* mmap */ nommap,
68 /* strategy */ nostrategy,
69 /* name */ "ctty",
70 /* maj */ CDEV_MAJOR,
71 /* dump */ nodump,
72 /* psize */ nopsize,
73 /* flags */ D_TTY,
74};
75
76#define cttyvp(td) ((td)->td_proc->p_flag & P_CONTROLT ? (td)->td_proc->p_session->s_ttyvp : NULL)
77
78/*ARGSUSED*/
79static int
80cttyopen(dev, flag, mode, td)
81 dev_t dev;
82 int flag, mode;
83 struct thread *td;
84{
83 struct vnode *ttyvp = cttyvp(td);
85 struct vnode *ttyvp;
84 int error;
85
86 int error;
87
88 PROC_LOCK(td->td_proc);
89 SESS_LOCK(td->td_proc->p_session);
90 ttyvp = cttyvp(td);
91 SESS_UNLOCK(td->td_proc->p_session);
92 PROC_UNLOCK(td->td_proc);
93
86 if (ttyvp == NULL)
87 return (ENXIO);
88 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td);
89 error = VOP_OPEN(ttyvp, flag, NOCRED, td);
90 VOP_UNLOCK(ttyvp, 0, td);
91 return (error);
92}
93
94/*ARGSUSED*/
95static int
96cttyread(dev, uio, flag)
97 dev_t dev;
98 struct uio *uio;
99 int flag;
100{
101 struct thread *td = uio->uio_td;
94 if (ttyvp == NULL)
95 return (ENXIO);
96 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td);
97 error = VOP_OPEN(ttyvp, flag, NOCRED, td);
98 VOP_UNLOCK(ttyvp, 0, td);
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 thread *td = uio->uio_td;
102 register struct vnode *ttyvp = cttyvp(td);
110 register struct vnode *ttyvp;
103 int error;
104
111 int error;
112
113 PROC_LOCK(td->td_proc);
114 SESS_LOCK(td->td_proc->p_session);
115 ttyvp = cttyvp(td);
116 SESS_UNLOCK(td->td_proc->p_session);
117 PROC_UNLOCK(td->td_proc);
118
105 if (ttyvp == NULL)
106 return (EIO);
107 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td);
108 error = VOP_READ(ttyvp, uio, flag, NOCRED);
109 VOP_UNLOCK(ttyvp, 0, td);
110 return (error);
111}
112
113/*ARGSUSED*/
114static int
115cttywrite(dev, uio, flag)
116 dev_t dev;
117 struct uio *uio;
118 int flag;
119{
120 struct thread *td = uio->uio_td;
119 if (ttyvp == NULL)
120 return (EIO);
121 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td);
122 error = VOP_READ(ttyvp, uio, flag, NOCRED);
123 VOP_UNLOCK(ttyvp, 0, td);
124 return (error);
125}
126
127/*ARGSUSED*/
128static int
129cttywrite(dev, uio, flag)
130 dev_t dev;
131 struct uio *uio;
132 int flag;
133{
134 struct thread *td = uio->uio_td;
121 struct vnode *ttyvp = cttyvp(uio->uio_td);
135 struct vnode *ttyvp;
122 struct mount *mp;
123 int error;
124
136 struct mount *mp;
137 int error;
138
139 PROC_LOCK(td->td_proc);
140 SESS_LOCK(td->td_proc->p_session);
141 ttyvp = cttyvp(td);
142 SESS_UNLOCK(td->td_proc->p_session);
143 PROC_UNLOCK(td->td_proc);
144
125 if (ttyvp == NULL)
126 return (EIO);
127 mp = NULL;
128 if (ttyvp->v_type != VCHR &&
129 (error = vn_start_write(ttyvp, &mp, V_WAIT | PCATCH)) != 0)
130 return (error);
131 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td);
132 error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
133 VOP_UNLOCK(ttyvp, 0, td);
134 vn_finished_write(mp);
135 return (error);
136}
137
138/*ARGSUSED*/
139static int
140cttyioctl(dev, cmd, addr, flag, td)
141 dev_t dev;
142 u_long cmd;
143 caddr_t addr;
144 int flag;
145 struct thread *td;
146{
145 if (ttyvp == NULL)
146 return (EIO);
147 mp = NULL;
148 if (ttyvp->v_type != VCHR &&
149 (error = vn_start_write(ttyvp, &mp, V_WAIT | PCATCH)) != 0)
150 return (error);
151 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td);
152 error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
153 VOP_UNLOCK(ttyvp, 0, td);
154 vn_finished_write(mp);
155 return (error);
156}
157
158/*ARGSUSED*/
159static int
160cttyioctl(dev, cmd, addr, flag, td)
161 dev_t dev;
162 u_long cmd;
163 caddr_t addr;
164 int flag;
165 struct thread *td;
166{
147 struct vnode *ttyvp = cttyvp(td);
167 struct vnode *ttyvp;
168 int error;
148
169
170 PROC_LOCK(td->td_proc);
171 SESS_LOCK(td->td_proc->p_session);
172 ttyvp = cttyvp(td);
173 SESS_UNLOCK(td->td_proc->p_session);
174 PROC_UNLOCK(td->td_proc);
175
149 if (ttyvp == NULL)
150 return (EIO);
151 if (cmd == TIOCSCTTY) /* don't allow controlling tty to be set */
152 return EINVAL; /* to controlling tty -- infinite recursion */
153 if (cmd == TIOCNOTTY) {
176 if (ttyvp == NULL)
177 return (EIO);
178 if (cmd == TIOCSCTTY) /* don't allow controlling tty to be set */
179 return EINVAL; /* to controlling tty -- infinite recursion */
180 if (cmd == TIOCNOTTY) {
154 if (!SESS_LEADER(td->td_proc)) {
181 PROC_LOCK(td->td_proc);
182 SESS_LOCK(td->td_proc->p_session);
183 error = 0;
184 if (!SESS_LEADER(td->td_proc))
155 td->td_proc->p_flag &= ~P_CONTROLT;
185 td->td_proc->p_flag &= ~P_CONTROLT;
156 return (0);
157 } else
158 return (EINVAL);
186 else
187 error = EINVAL;
188 SESS_UNLOCK(td->td_proc->p_session);
189 PROC_UNLOCK(td->td_proc);
190 return (error);
159 }
160 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, td));
161}
162
163/*ARGSUSED*/
164static int
165cttypoll(dev, events, td)
166 dev_t dev;
167 int events;
168 struct thread *td;
169{
191 }
192 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, td));
193}
194
195/*ARGSUSED*/
196static int
197cttypoll(dev, events, td)
198 dev_t dev;
199 int events;
200 struct thread *td;
201{
170 struct vnode *ttyvp = cttyvp(td);
202 struct vnode *ttyvp;
171
203
204 PROC_LOCK(td->td_proc);
205 SESS_LOCK(td->td_proc->p_session);
206 ttyvp = cttyvp(td);
207 SESS_UNLOCK(td->td_proc->p_session);
208 PROC_UNLOCK(td->td_proc);
209
172 if (ttyvp == NULL)
173 /* try operation to get EOF/failure */
174 return (seltrue(dev, events, td));
175 return (VOP_POLL(ttyvp, events, td->td_proc->p_ucred, td));
176}
177
178static void ctty_clone __P((void *arg, char *name, int namelen, dev_t *dev));
179
180static dev_t ctty;
181
182static void
183ctty_clone(void *arg, char *name, int namelen, dev_t *dev)
184{
185 struct vnode *vp;
186
187 if (*dev != NODEV)
188 return;
189 if (strcmp(name, "tty"))
190 return;
191 vp = cttyvp(curthread);
192 if (vp == NULL) {
193 if (ctty)
194 *dev = ctty;
195 } else
196 *dev = vp->v_rdev;
197}
198
199
200static void ctty_drvinit __P((void *unused));
201static void
202ctty_drvinit(unused)
203 void *unused;
204{
205
206 if (devfs_present) {
207 EVENTHANDLER_REGISTER(dev_clone, ctty_clone, 0, 1000);
208 ctty = make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "ctty");
209 } else {
210 make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "tty");
211 }
212}
213
214SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)
210 if (ttyvp == NULL)
211 /* try operation to get EOF/failure */
212 return (seltrue(dev, events, td));
213 return (VOP_POLL(ttyvp, events, td->td_proc->p_ucred, td));
214}
215
216static void ctty_clone __P((void *arg, char *name, int namelen, dev_t *dev));
217
218static dev_t ctty;
219
220static void
221ctty_clone(void *arg, char *name, int namelen, dev_t *dev)
222{
223 struct vnode *vp;
224
225 if (*dev != NODEV)
226 return;
227 if (strcmp(name, "tty"))
228 return;
229 vp = cttyvp(curthread);
230 if (vp == NULL) {
231 if (ctty)
232 *dev = ctty;
233 } else
234 *dev = vp->v_rdev;
235}
236
237
238static void ctty_drvinit __P((void *unused));
239static void
240ctty_drvinit(unused)
241 void *unused;
242{
243
244 if (devfs_present) {
245 EVENTHANDLER_REGISTER(dev_clone, ctty_clone, 0, 1000);
246 ctty = make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "ctty");
247 } else {
248 make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "tty");
249 }
250}
251
252SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)