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$ 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/proc.h> 47 #include <sys/ttycom.h> 48 #include <sys/vnode.h> 49 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; 55 56 #define CDEV_MAJOR 1 57 58 static 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*/ 77 static int 78 cttyopen(dev, flag, mode, td) 79 dev_t dev; 80 int flag, mode; 81 struct thread *td; 82 { 83 struct vnode *ttyvp = cttyvp(td); 84 int error; 85 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*/ 95 static int 96 cttyread(dev, uio, flag) 97 dev_t dev; 98 struct uio *uio; 99 int flag; 100 { 101 struct thread *td = uio->uio_td; 102 register struct vnode *ttyvp = cttyvp(td); 103 int error; 104 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*/ 114 static int 115 cttywrite(dev, uio, flag) 116 dev_t dev; 117 struct uio *uio; 118 int flag; 119 { 120 struct thread *td = uio->uio_td; 121 struct vnode *ttyvp = cttyvp(uio->uio_td); 122 struct mount *mp; 123 int error; 124 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*/ 139 static int 140 cttyioctl(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 { 147 struct vnode *ttyvp = cttyvp(td); 148 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) { 154 if (!SESS_LEADER(td->td_proc)) { 155 td->td_proc->p_flag &= ~P_CONTROLT; 156 return (0); 157 } else 158 return (EINVAL); 159 } 160 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, td)); 161 } 162 163 /*ARGSUSED*/ 164 static int 165 cttypoll(dev, events, td) 166 dev_t dev; 167 int events; 168 struct thread *td; 169 { 170 struct vnode *ttyvp = cttyvp(td); 171 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 178 static void ctty_clone __P((void *arg, char *name, int namelen, dev_t *dev)); 179 180 static dev_t ctty; 181 182 static void 183 ctty_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 200 static void ctty_drvinit __P((void *unused)); 201 static void 202 ctty_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 214 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL) 215