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 }, 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 vhold(ttyvp); 101 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY); 102 if (ttyvp != cttyvp(p) || (ttyvp->v_flag & VCTTYISOPEN)) { 103 kprintf("Warning: cttyopen: race avoided\n"); 104 vn_unlock(ttyvp); 105 vdrop(ttyvp); 106 goto retry; 107 } 108 vsetflags(ttyvp, VCTTYISOPEN); 109 error = VOP_OPEN(ttyvp, FREAD|FWRITE, ap->a_cred, NULL); 110 if (error) 111 vclrflags(ttyvp, VCTTYISOPEN); 112 vn_unlock(ttyvp); 113 vdrop(ttyvp); 114 return(error); 115 } 116 117 /* 118 * This closes /dev/tty. Because multiple opens of /dev/tty only 119 * generate a single open to the actual tty, the file modes are 120 * locked to FREAD|FWRITE. 121 */ 122 static int 123 cttyclose(struct dev_close_args *ap) 124 { 125 struct proc *p = curproc; 126 struct vnode *ttyvp; 127 int error; 128 129 KKASSERT(p); 130 retry: 131 /* 132 * The tty may have been TIOCNOTTY'd, don't return an 133 * error on close. We just have nothing to do. 134 */ 135 if ((ttyvp = cttyvp(p)) == NULL) 136 return(0); 137 if (ttyvp->v_flag & VCTTYISOPEN) { 138 /* 139 * Avoid a nasty race if we block while getting the lock. 140 */ 141 vref(ttyvp); 142 error = vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY); 143 if (error) { 144 vrele(ttyvp); 145 goto retry; 146 } 147 if (ttyvp != cttyvp(p) || (ttyvp->v_flag & VCTTYISOPEN) == 0) { 148 kprintf("Warning: cttyclose: race avoided\n"); 149 vn_unlock(ttyvp); 150 vrele(ttyvp); 151 goto retry; 152 } 153 vclrflags(ttyvp, VCTTYISOPEN); 154 error = VOP_CLOSE(ttyvp, FREAD|FWRITE); 155 vn_unlock(ttyvp); 156 vrele(ttyvp); 157 } else { 158 error = 0; 159 } 160 return(error); 161 } 162 163 /* 164 * Read from the controlling terminal (/dev/tty). The tty is refed as 165 * of the cttyvp(), but the ref can get ripped out from under us if 166 * the controlling terminal is revoked while we are blocked on the lock, 167 * so use vget() instead of vn_lock(). 168 */ 169 static int 170 cttyread(struct dev_read_args *ap) 171 { 172 struct proc *p = curproc; 173 struct vnode *ttyvp; 174 int error; 175 176 KKASSERT(p); 177 ttyvp = cttyvp(p); 178 if (ttyvp == NULL) 179 return (EIO); 180 if ((error = vget(ttyvp, LK_EXCLUSIVE | LK_RETRY)) == 0) { 181 error = VOP_READ(ttyvp, ap->a_uio, ap->a_ioflag, NOCRED); 182 vput(ttyvp); 183 } 184 return (error); 185 } 186 187 /* 188 * Read from the controlling terminal (/dev/tty). The tty is refed as 189 * of the cttyvp(), but the ref can get ripped out from under us if 190 * the controlling terminal is revoked while we are blocked on the lock, 191 * so use vget() instead of vn_lock(). 192 */ 193 static int 194 cttywrite(struct dev_write_args *ap) 195 { 196 struct proc *p = curproc; 197 struct vnode *ttyvp; 198 int error; 199 200 KKASSERT(p); 201 ttyvp = cttyvp(p); 202 if (ttyvp == NULL) 203 return (EIO); 204 if ((error = vget(ttyvp, LK_EXCLUSIVE | LK_RETRY)) == 0) { 205 error = VOP_WRITE(ttyvp, ap->a_uio, ap->a_ioflag, NOCRED); 206 vput(ttyvp); 207 } 208 return (error); 209 } 210 211 /*ARGSUSED*/ 212 static int 213 cttyioctl(struct dev_ioctl_args *ap) 214 { 215 struct vnode *ttyvp; 216 struct proc *p = curproc; 217 218 KKASSERT(p); 219 lwkt_gettoken(&p->p_token); 220 lwkt_gettoken(&proc_token); 221 ttyvp = cttyvp(p); 222 if (ttyvp == NULL) { 223 lwkt_reltoken(&proc_token); 224 lwkt_reltoken(&p->p_token); 225 return (EIO); 226 } 227 /* 228 * Don't allow controlling tty to be set to the controlling tty 229 * (infinite recursion). 230 */ 231 if (ap->a_cmd == TIOCSCTTY) { 232 lwkt_reltoken(&proc_token); 233 lwkt_reltoken(&p->p_token); 234 return EINVAL; 235 } 236 if (ap->a_cmd == TIOCNOTTY) { 237 if (!SESS_LEADER(p)) { 238 p->p_flags &= ~P_CONTROLT; 239 lwkt_reltoken(&proc_token); 240 lwkt_reltoken(&p->p_token); 241 return (0); 242 } else { 243 lwkt_reltoken(&proc_token); 244 lwkt_reltoken(&p->p_token); 245 return (EINVAL); 246 } 247 } 248 lwkt_reltoken(&proc_token); 249 lwkt_reltoken(&p->p_token); 250 251 return (VOP_IOCTL(ttyvp, ap->a_cmd, ap->a_data, ap->a_fflag, 252 ap->a_cred, ap->a_sysmsg)); 253 } 254 255 static struct filterops cttyfiltops_read = 256 { FILTEROP_ISFD, NULL, cttyfilt_detach, cttyfilt_read }; 257 static struct filterops cttyfiltops_write = 258 { FILTEROP_ISFD, NULL, cttyfilt_detach, cttyfilt_write }; 259 260 static int 261 cttykqfilter(struct dev_kqfilter_args *ap) 262 { 263 cdev_t dev = ap->a_head.a_dev; 264 struct proc *p = curproc; 265 struct knote *kn = ap->a_kn; 266 struct vnode *ttyvp; 267 268 KKASSERT(p); 269 ttyvp = cttyvp(p); 270 271 if (ttyvp != NULL) 272 return (VOP_KQFILTER(ttyvp, kn)); 273 274 ap->a_result = 0; 275 276 switch (kn->kn_filter) { 277 case EVFILT_READ: 278 kn->kn_fop = &cttyfiltops_read; 279 kn->kn_hook = (caddr_t)dev; 280 break; 281 case EVFILT_WRITE: 282 kn->kn_fop = &cttyfiltops_write; 283 kn->kn_hook = (caddr_t)dev; 284 break; 285 default: 286 ap->a_result = EOPNOTSUPP; 287 return (0); 288 } 289 290 return (0); 291 } 292 293 static void 294 cttyfilt_detach(struct knote *kn) {} 295 296 static int 297 cttyfilt_read(struct knote *kn, long hint) 298 { 299 cdev_t dev = (cdev_t)kn->kn_hook; 300 301 if (seltrue(dev, POLLIN | POLLRDNORM)) 302 return (1); 303 304 return (0); 305 } 306 307 static int 308 cttyfilt_write(struct knote *kn, long hint) 309 { 310 cdev_t dev = (cdev_t)kn->kn_hook; 311 312 if (seltrue(dev, POLLOUT | POLLWRNORM)) 313 return (1); 314 315 return (0); 316 } 317 318 static void 319 ctty_drvinit(void *unused __unused) 320 { 321 make_dev(&ctty_ops, 0, 0, 0, 0666, "tty"); 322 } 323 324 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL) 325