1 /* $NetBSD: tty_tty.c,v 1.19 2002/10/23 09:14:27 jdolecek Exp $ */ 2 3 /*- 4 * Copyright (c) 1982, 1986, 1991, 1993, 1995 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)tty_tty.c 8.2 (Berkeley) 9/23/93 36 */ 37 38 /* 39 * Indirect driver for controlling tty. 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: tty_tty.c,v 1.19 2002/10/23 09:14:27 jdolecek Exp $"); 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/ioctl.h> 48 #include <sys/proc.h> 49 #include <sys/tty.h> 50 #include <sys/vnode.h> 51 #include <sys/file.h> 52 #include <sys/conf.h> 53 54 55 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL) 56 57 dev_type_open(cttyopen); 58 dev_type_read(cttyread); 59 dev_type_write(cttywrite); 60 dev_type_ioctl(cttyioctl); 61 dev_type_poll(cttypoll); 62 dev_type_kqfilter(cttykqfilter); 63 64 const struct cdevsw ctty_cdevsw = { 65 cttyopen, nullclose, cttyread, cttywrite, cttyioctl, 66 nullstop, notty, cttypoll, nommap, cttykqfilter, D_TTY 67 }; 68 69 /*ARGSUSED*/ 70 int 71 cttyopen(dev, flag, mode, p) 72 dev_t dev; 73 int flag, mode; 74 struct proc *p; 75 { 76 struct vnode *ttyvp = cttyvp(p); 77 int error; 78 79 if (ttyvp == NULL) 80 return (ENXIO); 81 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY); 82 #ifdef PARANOID 83 /* 84 * Since group is tty and mode is 620 on most terminal lines 85 * and since sessions protect terminals from processes outside 86 * your session, this check is probably no longer necessary. 87 * Since it inhibits setuid root programs that later switch 88 * to another user from accessing /dev/tty, we have decided 89 * to delete this test. (mckusick 5/93) 90 */ 91 error = VOP_ACCESS(ttyvp, 92 (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p); 93 if (!error) 94 #endif /* PARANOID */ 95 error = VOP_OPEN(ttyvp, flag, NOCRED, p); 96 VOP_UNLOCK(ttyvp, 0); 97 return (error); 98 } 99 100 /*ARGSUSED*/ 101 int 102 cttyread(dev, uio, flag) 103 dev_t dev; 104 struct uio *uio; 105 int flag; 106 { 107 struct vnode *ttyvp = cttyvp(uio->uio_procp); 108 int error; 109 110 if (ttyvp == NULL) 111 return (EIO); 112 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY); 113 error = VOP_READ(ttyvp, uio, flag, NOCRED); 114 VOP_UNLOCK(ttyvp, 0); 115 return (error); 116 } 117 118 /*ARGSUSED*/ 119 int 120 cttywrite(dev, uio, flag) 121 dev_t dev; 122 struct uio *uio; 123 int flag; 124 { 125 struct vnode *ttyvp = cttyvp(uio->uio_procp); 126 int error; 127 128 if (ttyvp == NULL) 129 return (EIO); 130 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY); 131 error = VOP_WRITE(ttyvp, uio, flag, NOCRED); 132 VOP_UNLOCK(ttyvp, 0); 133 return (error); 134 } 135 136 /*ARGSUSED*/ 137 int 138 cttyioctl(dev, cmd, addr, flag, p) 139 dev_t dev; 140 u_long cmd; 141 caddr_t addr; 142 int flag; 143 struct proc *p; 144 { 145 struct vnode *ttyvp = cttyvp(p); 146 147 if (ttyvp == NULL) 148 return (EIO); 149 if (cmd == TIOCSCTTY) /* XXX */ 150 return (EINVAL); 151 if (cmd == TIOCNOTTY) { 152 if (!SESS_LEADER(p)) { 153 p->p_flag &= ~P_CONTROLT; 154 return (0); 155 } else 156 return (EINVAL); 157 } 158 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p)); 159 } 160 161 /*ARGSUSED*/ 162 int 163 cttypoll(dev, events, p) 164 dev_t dev; 165 int events; 166 struct proc *p; 167 { 168 struct vnode *ttyvp = cttyvp(p); 169 170 if (ttyvp == NULL) 171 return (seltrue(dev, events, p)); 172 return (VOP_POLL(ttyvp, events, p)); 173 } 174 175 int 176 cttykqfilter(dev, kn) 177 dev_t dev; 178 struct knote *kn; 179 { 180 /* This is called from filt_fileattach() by the attaching process. */ 181 struct proc *p = curproc; 182 struct vnode *ttyvp = cttyvp(p); 183 184 if (ttyvp == NULL) 185 return (1); 186 return (VOP_KQFILTER(ttyvp, kn)); 187 } 188