xref: /dragonfly/sys/kern/tty_tty.c (revision e6d22e9b)
1 /*-
2  * (MPSAFE)
3  *
4  * Copyright (c) 1982, 1986, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)tty_tty.c	8.2 (Berkeley) 9/23/93
32  * $FreeBSD: src/sys/kern/tty_tty.c,v 1.30 1999/09/25 18:24:24 phk Exp $
33  */
34 
35 /*
36  * Indirect driver for controlling tty.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43 #include <sys/lock.h>
44 #include <sys/fcntl.h>
45 #include <sys/proc.h>
46 #include <sys/ttycom.h>
47 #include <sys/vnode.h>
48 #include <sys/kernel.h>
49 #include <sys/poll.h> /* XXX: poll args used in KQ filters */
50 #include <sys/event.h>
51 
52 static	d_open_t	cttyopen;
53 static	d_close_t	cttyclose;
54 static	d_read_t	cttyread;
55 static	d_write_t	cttywrite;
56 static	d_ioctl_t	cttyioctl;
57 static	d_kqfilter_t	cttykqfilter;
58 
59 static void cttyfilt_detach(struct knote *);
60 static int cttyfilt_read(struct knote *, long);
61 static int cttyfilt_write(struct knote *, long);
62 
63 #define	CDEV_MAJOR	1
64 static struct dev_ops ctty_ops = {
65 	{ "ctty", 0, D_TTY | D_MPSAFE },
66 	.d_open =	cttyopen,
67 	.d_close =	cttyclose,
68 	.d_read =	cttyread,
69 	.d_write =	cttywrite,
70 	.d_ioctl =	cttyioctl,
71 	.d_kqfilter =	cttykqfilter
72 };
73 
74 #define cttyvp(p) (((p)->p_flags & P_CONTROLT) ? \
75 			(p)->p_session->s_ttyvp : NULL)
76 
77 /*
78  * This opens /dev/tty.  Because multiple opens of /dev/tty only
79  * generate a single open to the actual tty, the file modes are
80  * locked to FREAD|FWRITE.
81  */
82 static	int
83 cttyopen(struct dev_open_args *ap)
84 {
85 	struct proc *p = curproc;
86 	struct vnode *ttyvp;
87 	int error;
88 
89 	KKASSERT(p);
90 retry:
91 	if ((ttyvp = cttyvp(p)) == NULL)
92 		return (ENXIO);
93 	if (ttyvp->v_flag & VCTTYISOPEN)
94 		return (0);
95 
96 	/*
97 	 * Messy interlock, don't let the vnode go away while we try to
98 	 * lock it and check for race after we might have blocked.
99 	 *
100 	 * WARNING! The device open (devfs_spec_open()) temporarily
101 	 *	    releases the vnode lock on ttyvp when issuing the
102 	 *	    dev_dopen(), which means that the VCTTYISOPEn flag
103 	 *	    can race during the VOP_OPEN().
104 	 *
105 	 *	    If something does race we have to undo our potentially
106 	 *	    extra open.
107 	 */
108 	vhold(ttyvp);
109 	vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
110 	if (ttyvp != cttyvp(p) || (ttyvp->v_flag & VCTTYISOPEN)) {
111 		vn_unlock(ttyvp);
112 		vdrop(ttyvp);
113 		goto retry;
114 	}
115 	error = VOP_OPEN(ttyvp, FREAD|FWRITE, ap->a_cred, NULL);
116 
117 	/*
118 	 * Race against ctty close or change.  This case has been validated
119 	 * and occurs every so often during synth builds.
120 	 */
121 	if (ttyvp != cttyvp(p) || (ttyvp->v_flag & VCTTYISOPEN)) {
122 		if (error == 0)
123 			VOP_CLOSE(ttyvp, FREAD|FWRITE, NULL);
124 		vn_unlock(ttyvp);
125 		vdrop(ttyvp);
126 		goto retry;
127 	}
128 	if (error == 0)
129 		vsetflags(ttyvp, VCTTYISOPEN);
130 	vn_unlock(ttyvp);
131 	vdrop(ttyvp);
132 	return(error);
133 }
134 
135 /*
136  * This closes /dev/tty.  Because multiple opens of /dev/tty only
137  * generate a single open to the actual tty, the file modes are
138  * locked to FREAD|FWRITE.
139  */
140 static int
141 cttyclose(struct dev_close_args *ap)
142 {
143 	struct proc *p = curproc;
144 	struct vnode *ttyvp;
145 	int error;
146 
147 	KKASSERT(p);
148 retry:
149 	/*
150 	 * The tty may have been TIOCNOTTY'd, don't return an
151 	 * error on close.  We just have nothing to do.
152 	 */
153 	if ((ttyvp = cttyvp(p)) == NULL)
154 		return(0);
155 	if (ttyvp->v_flag & VCTTYISOPEN) {
156 		/*
157 		 * Avoid a nasty race if we block while getting the lock.
158 		 */
159 		vref(ttyvp);
160 		error = vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY |
161 				       LK_FAILRECLAIM);
162 		if (error) {
163 			vrele(ttyvp);
164 			goto retry;
165 		}
166 		if (ttyvp != cttyvp(p) || (ttyvp->v_flag & VCTTYISOPEN) == 0) {
167 			kprintf("Warning: cttyclose: race avoided\n");
168 			vn_unlock(ttyvp);
169 			vrele(ttyvp);
170 			goto retry;
171 		}
172 		vclrflags(ttyvp, VCTTYISOPEN);
173 		error = VOP_CLOSE(ttyvp, FREAD|FWRITE, NULL);
174 		vn_unlock(ttyvp);
175 		vrele(ttyvp);
176 	} else {
177 		error = 0;
178 	}
179 	return(error);
180 }
181 
182 /*
183  * Read from the controlling terminal (/dev/tty).  The tty is refed as
184  * of the cttyvp(), but the ref can get ripped out from under us if
185  * the controlling terminal is revoked while we are blocked on the lock,
186  * so use vget() instead of vn_lock().
187  */
188 static	int
189 cttyread(struct dev_read_args *ap)
190 {
191 	struct proc *p = curproc;
192 	struct vnode *ttyvp;
193 	int error;
194 
195 	KKASSERT(p);
196 	ttyvp = cttyvp(p);
197 	if (ttyvp == NULL)
198 		return (EIO);
199 	if ((error = vget(ttyvp, LK_EXCLUSIVE | LK_RETRY)) == 0) {
200 		error = VOP_READ(ttyvp, ap->a_uio, ap->a_ioflag, NOCRED);
201 		vput(ttyvp);
202 	}
203 	return (error);
204 }
205 
206 /*
207  * Read from the controlling terminal (/dev/tty).  The tty is refed as
208  * of the cttyvp(), but the ref can get ripped out from under us if
209  * the controlling terminal is revoked while we are blocked on the lock,
210  * so use vget() instead of vn_lock().
211  */
212 static	int
213 cttywrite(struct dev_write_args *ap)
214 {
215 	struct proc *p = curproc;
216 	struct vnode *ttyvp;
217 	int error;
218 
219 	KKASSERT(p);
220 	ttyvp = cttyvp(p);
221 	if (ttyvp == NULL)
222 		return (EIO);
223 	if ((error = vget(ttyvp, LK_EXCLUSIVE | LK_RETRY)) == 0) {
224 		error = VOP_WRITE(ttyvp, ap->a_uio, ap->a_ioflag, NOCRED);
225 		vput(ttyvp);
226 	}
227 	return (error);
228 }
229 
230 /*ARGSUSED*/
231 static	int
232 cttyioctl(struct dev_ioctl_args *ap)
233 {
234 	struct vnode *ttyvp;
235 	struct proc *p = curproc;
236 
237 	KKASSERT(p);
238 	lwkt_gettoken(&p->p_token);
239 	ttyvp = cttyvp(p);
240 	if (ttyvp == NULL) {
241 		lwkt_reltoken(&p->p_token);
242 		return (EIO);
243 	}
244 	/*
245 	 * Don't allow controlling tty to be set to the controlling tty
246 	 * (infinite recursion).
247 	 */
248 	if (ap->a_cmd == TIOCSCTTY) {
249 		lwkt_reltoken(&p->p_token);
250 		return EINVAL;
251 	}
252 	if (ap->a_cmd == TIOCNOTTY) {
253 		if (!SESS_LEADER(p)) {
254 			p->p_flags &= ~P_CONTROLT;
255 			lwkt_reltoken(&p->p_token);
256 			return (0);
257 		} else {
258 			lwkt_reltoken(&p->p_token);
259 			return (EINVAL);
260 		}
261 	}
262 	lwkt_reltoken(&p->p_token);
263 
264 	return (VOP_IOCTL(ttyvp, ap->a_cmd, ap->a_data, ap->a_fflag,
265 			  ap->a_cred, ap->a_sysmsg));
266 }
267 
268 static struct filterops cttyfiltops_read =
269 	{ FILTEROP_ISFD | FILTEROP_MPSAFE, NULL,
270 	  cttyfilt_detach, cttyfilt_read };
271 static struct filterops cttyfiltops_write =
272 	{ FILTEROP_ISFD | FILTEROP_MPSAFE, NULL,
273 	  cttyfilt_detach, cttyfilt_write };
274 
275 static int
276 cttykqfilter(struct dev_kqfilter_args *ap)
277 {
278 	cdev_t dev = ap->a_head.a_dev;
279 	struct proc *p = curproc;
280 	struct knote *kn = ap->a_kn;
281 	struct vnode *ttyvp;
282 
283 	KKASSERT(p);
284 	ttyvp = cttyvp(p);
285 
286 	if (ttyvp != NULL)
287 		return (VOP_KQFILTER(ttyvp, kn));
288 
289 	ap->a_result = 0;
290 
291 	switch (kn->kn_filter) {
292 	case EVFILT_READ:
293 		kn->kn_fop = &cttyfiltops_read;
294 		kn->kn_hook = (caddr_t)dev;
295 		break;
296 	case EVFILT_WRITE:
297 		kn->kn_fop = &cttyfiltops_write;
298 		kn->kn_hook = (caddr_t)dev;
299 		break;
300 	default:
301 		ap->a_result = EOPNOTSUPP;
302 		return (0);
303 	}
304 
305 	return (0);
306 }
307 
308 static void
309 cttyfilt_detach(struct knote *kn) {}
310 
311 static int
312 cttyfilt_read(struct knote *kn, long hint)
313 {
314 	cdev_t dev = (cdev_t)kn->kn_hook;
315 
316 	if (seltrue(dev, POLLIN | POLLRDNORM))
317 		return (1);
318 
319 	return (0);
320 }
321 
322 static int
323 cttyfilt_write(struct knote *kn, long hint)
324 {
325 	cdev_t dev = (cdev_t)kn->kn_hook;
326 
327 	if (seltrue(dev, POLLOUT | POLLWRNORM))
328 		return (1);
329 
330 	return (0);
331 }
332 
333 static void
334 ctty_drvinit(void *unused __unused)
335 {
336 	make_dev(&ctty_ops, 0, 0, 0, 0666, "tty");
337 }
338 
339 SYSINIT(cttydev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR,
340 	ctty_drvinit, NULL);
341