1 /* $OpenBSD: tty_tty.c,v 1.6 1997/11/06 05:58:24 csapuntz Exp $ */ 2 /* $NetBSD: tty_tty.c,v 1.13 1996/03/30 22:24:46 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1982, 1986, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)tty_tty.c 8.2 (Berkeley) 9/23/93 37 */ 38 39 /* 40 * Indirect driver for controlling tty. 41 */ 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/ioctl.h> 45 #include <sys/proc.h> 46 #include <sys/tty.h> 47 #include <sys/vnode.h> 48 #include <sys/file.h> 49 #include <sys/conf.h> 50 51 52 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL) 53 54 /*ARGSUSED*/ 55 int 56 cttyopen(dev, flag, mode, p) 57 dev_t dev; 58 int flag, mode; 59 struct proc *p; 60 { 61 struct vnode *ttyvp = cttyvp(p); 62 int error; 63 64 if (ttyvp == NULL) 65 return (ENXIO); 66 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p); 67 #ifdef PARANOID 68 /* 69 * Since group is tty and mode is 620 on most terminal lines 70 * and since sessions protect terminals from processes outside 71 * your session, this check is probably no longer necessary. 72 * Since it inhibits setuid root programs that later switch 73 * to another user from accessing /dev/tty, we have decided 74 * to delete this test. (mckusick 5/93) 75 */ 76 error = VOP_ACCESS(ttyvp, 77 (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p); 78 if (!error) 79 #endif /* PARANOID */ 80 error = VOP_OPEN(ttyvp, flag, NOCRED, p); 81 VOP_UNLOCK(ttyvp, 0, p); 82 return (error); 83 } 84 85 /*ARGSUSED*/ 86 int 87 cttyread(dev, uio, flag) 88 dev_t dev; 89 struct uio *uio; 90 int flag; 91 { 92 struct proc *p = uio->uio_procp; 93 register struct vnode *ttyvp = cttyvp(uio->uio_procp); 94 int error; 95 96 if (ttyvp == NULL) 97 return (EIO); 98 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p); 99 error = VOP_READ(ttyvp, uio, flag, NOCRED); 100 VOP_UNLOCK(ttyvp, 0, p); 101 return (error); 102 } 103 104 /*ARGSUSED*/ 105 int 106 cttywrite(dev, uio, flag) 107 dev_t dev; 108 struct uio *uio; 109 int flag; 110 { 111 struct proc *p = uio->uio_procp; 112 register struct vnode *ttyvp = cttyvp(uio->uio_procp); 113 int error; 114 115 if (ttyvp == NULL) 116 return (EIO); 117 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p); 118 error = VOP_WRITE(ttyvp, uio, flag, NOCRED); 119 VOP_UNLOCK(ttyvp, 0, p); 120 return (error); 121 } 122 123 /*ARGSUSED*/ 124 int 125 cttyioctl(dev, cmd, addr, flag, p) 126 dev_t dev; 127 u_long cmd; 128 caddr_t addr; 129 int flag; 130 struct proc *p; 131 { 132 struct vnode *ttyvp = cttyvp(p); 133 134 if (ttyvp == NULL) 135 return (EIO); 136 if (cmd == TIOCSCTTY) /* XXX */ 137 return (EINVAL); 138 if (cmd == TIOCNOTTY) { 139 if (!SESS_LEADER(p)) { 140 p->p_flag &= ~P_CONTROLT; 141 return (0); 142 } else 143 return (EINVAL); 144 } 145 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p)); 146 } 147 148 /*ARGSUSED*/ 149 int 150 cttyselect(dev, flag, p) 151 dev_t dev; 152 int flag; 153 struct proc *p; 154 { 155 struct vnode *ttyvp = cttyvp(p); 156 157 if (ttyvp == NULL) 158 return (1); /* try operation to get EOF/failure */ 159 return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p)); 160 } 161