1 /* $NetBSD: tty_tty.c,v 1.18 2002/09/06 13:18:43 gehenna 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.18 2002/09/06 13:18:43 gehenna 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 63 const struct cdevsw ctty_cdevsw = { 64 cttyopen, nullclose, cttyread, cttywrite, cttyioctl, 65 nullstop, notty, cttypoll, nommap, D_TTY 66 }; 67 68 /*ARGSUSED*/ 69 int 70 cttyopen(dev, flag, mode, p) 71 dev_t dev; 72 int flag, mode; 73 struct proc *p; 74 { 75 struct vnode *ttyvp = cttyvp(p); 76 int error; 77 78 if (ttyvp == NULL) 79 return (ENXIO); 80 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY); 81 #ifdef PARANOID 82 /* 83 * Since group is tty and mode is 620 on most terminal lines 84 * and since sessions protect terminals from processes outside 85 * your session, this check is probably no longer necessary. 86 * Since it inhibits setuid root programs that later switch 87 * to another user from accessing /dev/tty, we have decided 88 * to delete this test. (mckusick 5/93) 89 */ 90 error = VOP_ACCESS(ttyvp, 91 (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p); 92 if (!error) 93 #endif /* PARANOID */ 94 error = VOP_OPEN(ttyvp, flag, NOCRED, p); 95 VOP_UNLOCK(ttyvp, 0); 96 return (error); 97 } 98 99 /*ARGSUSED*/ 100 int 101 cttyread(dev, uio, flag) 102 dev_t dev; 103 struct uio *uio; 104 int flag; 105 { 106 struct vnode *ttyvp = cttyvp(uio->uio_procp); 107 int error; 108 109 if (ttyvp == NULL) 110 return (EIO); 111 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY); 112 error = VOP_READ(ttyvp, uio, flag, NOCRED); 113 VOP_UNLOCK(ttyvp, 0); 114 return (error); 115 } 116 117 /*ARGSUSED*/ 118 int 119 cttywrite(dev, uio, flag) 120 dev_t dev; 121 struct uio *uio; 122 int flag; 123 { 124 struct vnode *ttyvp = cttyvp(uio->uio_procp); 125 int error; 126 127 if (ttyvp == NULL) 128 return (EIO); 129 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY); 130 error = VOP_WRITE(ttyvp, uio, flag, NOCRED); 131 VOP_UNLOCK(ttyvp, 0); 132 return (error); 133 } 134 135 /*ARGSUSED*/ 136 int 137 cttyioctl(dev, cmd, addr, flag, p) 138 dev_t dev; 139 u_long cmd; 140 caddr_t addr; 141 int flag; 142 struct proc *p; 143 { 144 struct vnode *ttyvp = cttyvp(p); 145 146 if (ttyvp == NULL) 147 return (EIO); 148 if (cmd == TIOCSCTTY) /* XXX */ 149 return (EINVAL); 150 if (cmd == TIOCNOTTY) { 151 if (!SESS_LEADER(p)) { 152 p->p_flag &= ~P_CONTROLT; 153 return (0); 154 } else 155 return (EINVAL); 156 } 157 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p)); 158 } 159 160 /*ARGSUSED*/ 161 int 162 cttypoll(dev, events, p) 163 dev_t dev; 164 int events; 165 struct proc *p; 166 { 167 struct vnode *ttyvp = cttyvp(p); 168 169 if (ttyvp == NULL) 170 return (seltrue(dev, events, p)); 171 return (VOP_POLL(ttyvp, events, p)); 172 } 173