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
38 * Indirect driver for controlling tty.
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/kernel.h>
47 #include <sys/ttycom.h>
48 #include <sys/vnode.h>
50 static d_open_t cttyopen;
51 static d_read_t cttyread;
52 static d_write_t cttywrite;
53 static d_ioctl_t cttyioctl;
54 static d_poll_t cttypoll;
58 static struct cdevsw ctty_cdevsw = {
60 /* close */ nullclose,
62 /* write */ cttywrite,
63 /* ioctl */ cttyioctl,
66 /* strategy */ nostrategy,
74 #define cttyvp(td) ((td)->td_proc->p_flag & P_CONTROLT ? (td)->td_proc->p_session->s_ttyvp : NULL)
78 cttyopen(dev, flag, mode, td)
83 struct vnode *ttyvp = cttyvp(td);
88 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td);
89 error = VOP_OPEN(ttyvp, flag, NOCRED, td);
90 VOP_UNLOCK(ttyvp, 0, td);
96 cttyread(dev, uio, flag)
101 struct thread *td = uio->uio_td;
102 register struct vnode *ttyvp = cttyvp(td);
107 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td);
108 error = VOP_READ(ttyvp, uio, flag, NOCRED);
109 VOP_UNLOCK(ttyvp, 0, td);
115 cttywrite(dev, uio, flag)
120 struct thread *td = uio->uio_td;
121 struct vnode *ttyvp = cttyvp(uio->uio_td);
128 if (ttyvp->v_type != VCHR &&
129 (error = vn_start_write(ttyvp, &mp, V_WAIT | PCATCH)) != 0)
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);
140 cttyioctl(dev, cmd, addr, flag, td)
147 struct vnode *ttyvp = cttyvp(td);
151 if (cmd == TIOCSCTTY) /* don't allow controlling tty to be set */
152 return EINVAL; /* to controlling tty -- infinite recursion */
153 if (cmd == TIOCNOTTY) {
154 if (!SESS_LEADER(td->td_proc)) {
155 td->td_proc->p_flag &= ~P_CONTROLT;
160 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, td));
165 cttypoll(dev, events, td)
170 struct vnode *ttyvp = cttyvp(td);
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));
178 static void ctty_clone __P((void *arg, char *name, int namelen, dev_t *dev));
183 ctty_clone(void *arg, char *name, int namelen, dev_t *dev)
189 if (strcmp(name, "tty"))
191 vp = cttyvp(curthread);
200 static void ctty_drvinit __P((void *unused));
207 EVENTHANDLER_REGISTER(dev_clone, ctty_clone, 0, 1000);
208 ctty = make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "ctty");
210 make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "tty");
214 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)