2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)tty_tty.c 8.2 (Berkeley) 9/23/93
34 * $FreeBSD: src/sys/kern/tty_tty.c,v 1.30 1999/09/25 18:24:24 phk Exp $
35 * $DragonFly: src/sys/kern/tty_tty.c,v 1.15 2006/05/06 02:43:12 dillon Exp $
39 * Indirect driver for controlling tty.
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <sys/fcntl.h>
48 #include <sys/ttycom.h>
49 #include <sys/vnode.h>
50 #include <sys/kernel.h>
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_poll_t cttypoll;
60 /* Don't make this static, since fdesc_vnops uses it. */
61 struct cdevsw ctty_cdevsw = {
69 /* close */ cttyclose,
71 /* write */ cttywrite,
72 /* ioctl */ cttyioctl,
75 /* strategy */ nostrategy,
80 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
83 * This opens /dev/tty. Because multiple opens of /dev/tty only
84 * generate a single open to the actual tty, the file modes are
85 * locked to FREAD|FWRITE.
88 cttyopen(dev_t dev, int flag, int mode, struct thread *td)
90 struct proc *p = td->td_proc;
97 if (ttyvp->v_flag & VCTTYISOPEN) {
100 vsetflags(ttyvp, VCTTYISOPEN);
101 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
102 error = VOP_OPEN(ttyvp, FREAD|FWRITE, NOCRED, NULL);
104 vclrflags(ttyvp, VCTTYISOPEN);
105 VOP_UNLOCK(ttyvp, 0);
114 * This closes /dev/tty. Because multiple opens of /dev/tty only
115 * generate a single open to the actual tty, the file modes are
116 * locked to FREAD|FWRITE.
119 cttyclose(dev_t dev, int fflag, int devtype, struct thread *td)
121 struct proc *p = td->td_proc;
129 * The tty may have been TIOCNOTTY'd, don't return an
130 * error on close. We just have nothing to do.
134 } else if (ttyvp->v_flag & VCTTYISOPEN) {
135 vclrflags(ttyvp, VCTTYISOPEN);
136 error = vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
138 error = VOP_CLOSE(ttyvp, FREAD|FWRITE);
139 VOP_UNLOCK(ttyvp, 0);
148 * Read from the controlling terminal (/dev/tty). The tty is refed as
149 * of the cttyvp(), but the ref can get ripped out from under us if
150 * the controlling terminal is revoked while we are blocked on the lock,
151 * so use vget() instead of VOP_LOCK.
154 cttyread(dev, uio, flag)
159 struct thread *td = uio->uio_td;
160 struct proc *p = td->td_proc;
168 if ((error = vget(ttyvp, LK_EXCLUSIVE | LK_RETRY)) == 0) {
169 error = VOP_READ(ttyvp, uio, flag, NOCRED);
176 * Read from the controlling terminal (/dev/tty). The tty is refed as
177 * of the cttyvp(), but the ref can get ripped out from under us if
178 * the controlling terminal is revoked while we are blocked on the lock,
179 * so use vget() instead of VOP_LOCK.
182 cttywrite(dev, uio, flag)
187 struct thread *td = uio->uio_td;
188 struct proc *p = td->td_proc;
196 if ((error = vget(ttyvp, LK_EXCLUSIVE | LK_RETRY)) == 0) {
197 error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
205 cttyioctl(dev, cmd, addr, flag, td)
213 struct proc *p = td->td_proc;
219 if (cmd == TIOCSCTTY) /* don't allow controlling tty to be set */
220 return EINVAL; /* to controlling tty -- infinite recursion */
221 if (cmd == TIOCNOTTY) {
222 if (!SESS_LEADER(p)) {
223 p->p_flag &= ~P_CONTROLT;
228 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED));
233 cttypoll(dev_t dev, int events, struct thread *td)
236 struct proc *p = td->td_proc;
241 /* try operation to get EOF/failure */
242 return (seltrue(dev, events, td));
243 return (VOP_POLL(ttyvp, events, p->p_ucred));
246 static void ctty_drvinit (void *unused);
251 cdevsw_add(&ctty_cdevsw, 0, 0);
252 make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "tty");
255 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)