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 * $Id: tty_tty.c,v 1.26 1999/05/30 16:53:00 phk Exp $ 35 */ 36 37 /* 38 * Indirect driver for controlling tty. 39 */ 40 41 #include "opt_devfs.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/conf.h> 46 #include <sys/lock.h> 47 #include <sys/proc.h> 48 #include <sys/ttycom.h> 49 #include <sys/vnode.h> 50 #include <sys/kernel.h> 51 #ifdef DEVFS 52 #include <sys/devfsext.h> 53 #endif /*DEVFS*/ 54 55 static d_open_t cttyopen; 56 static d_read_t cttyread; 57 static d_write_t cttywrite; 58 static d_ioctl_t cttyioctl; 59 static d_poll_t cttypoll; 60 61 #define CDEV_MAJOR 1 62 /* Don't make this static, since fdesc_vnops uses it. */ 63 struct cdevsw ctty_cdevsw = { 64 /* open */ cttyopen, 65 /* close */ nullclose, 66 /* read */ cttyread, 67 /* write */ cttywrite, 68 /* ioctl */ cttyioctl, 69 /* stop */ nostop, 70 /* reset */ noreset, 71 /* devtotty */ nodevtotty, 72 /* poll */ cttypoll, 73 /* mmap */ nommap, 74 /* strategy */ nostrategy, 75 /* name */ "ctty", 76 /* parms */ noparms, 77 /* maj */ CDEV_MAJOR, 78 /* dump */ nodump, 79 /* psize */ nopsize, 80 /* flags */ D_TTY, 81 /* maxio */ 0, 82 /* bmaj */ -1 83 }; 84 85 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL) 86 87 /*ARGSUSED*/ 88 static int 89 cttyopen(dev, flag, mode, p) 90 dev_t dev; 91 int flag, mode; 92 struct proc *p; 93 { 94 struct vnode *ttyvp = cttyvp(p); 95 int error; 96 97 if (ttyvp == NULL) 98 return (ENXIO); 99 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p); 100 #ifdef PARANOID 101 /* 102 * Since group is tty and mode is 620 on most terminal lines 103 * and since sessions protect terminals from processes outside 104 * your session, this check is probably no longer necessary. 105 * Since it inhibits setuid root programs that later switch 106 * to another user from accessing /dev/tty, we have decided 107 * to delete this test. (mckusick 5/93) 108 */ 109 error = VOP_ACCESS(ttyvp, 110 (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p); 111 if (!error) 112 #endif /* PARANOID */ 113 error = VOP_OPEN(ttyvp, flag, NOCRED, p); 114 VOP_UNLOCK(ttyvp, 0, p); 115 return (error); 116 } 117 118 /*ARGSUSED*/ 119 static int 120 cttyread(dev, uio, flag) 121 dev_t dev; 122 struct uio *uio; 123 int flag; 124 { 125 struct proc *p = uio->uio_procp; 126 register struct vnode *ttyvp = cttyvp(p); 127 int error; 128 129 if (ttyvp == NULL) 130 return (EIO); 131 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p); 132 error = VOP_READ(ttyvp, uio, flag, NOCRED); 133 VOP_UNLOCK(ttyvp, 0, p); 134 return (error); 135 } 136 137 /*ARGSUSED*/ 138 static int 139 cttywrite(dev, uio, flag) 140 dev_t dev; 141 struct uio *uio; 142 int flag; 143 { 144 struct proc *p = uio->uio_procp; 145 struct vnode *ttyvp = cttyvp(uio->uio_procp); 146 int error; 147 148 if (ttyvp == NULL) 149 return (EIO); 150 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p); 151 error = VOP_WRITE(ttyvp, uio, flag, NOCRED); 152 VOP_UNLOCK(ttyvp, 0, p); 153 return (error); 154 } 155 156 /*ARGSUSED*/ 157 static int 158 cttyioctl(dev, cmd, addr, flag, p) 159 dev_t dev; 160 u_long cmd; 161 caddr_t addr; 162 int flag; 163 struct proc *p; 164 { 165 struct vnode *ttyvp = cttyvp(p); 166 167 if (ttyvp == NULL) 168 return (EIO); 169 if (cmd == TIOCSCTTY) /* don't allow controlling tty to be set */ 170 return EINVAL; /* to controlling tty -- infinite recursion */ 171 if (cmd == TIOCNOTTY) { 172 if (!SESS_LEADER(p)) { 173 p->p_flag &= ~P_CONTROLT; 174 return (0); 175 } else 176 return (EINVAL); 177 } 178 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p)); 179 } 180 181 /*ARGSUSED*/ 182 static int 183 cttypoll(dev, events, p) 184 dev_t dev; 185 int events; 186 struct proc *p; 187 { 188 struct vnode *ttyvp = cttyvp(p); 189 190 if (ttyvp == NULL) 191 /* try operation to get EOF/failure */ 192 return (seltrue(dev, events, p)); 193 return (VOP_POLL(ttyvp, events, p->p_ucred, p)); 194 } 195 196 static int ctty_devsw_installed; 197 #ifdef DEVFS 198 static void *ctty_devfs_token; 199 #endif 200 201 static void ctty_drvinit __P((void *unused)); 202 static void 203 ctty_drvinit(unused) 204 void *unused; 205 { 206 207 if( ! ctty_devsw_installed ) { 208 cdevsw_add(&ctty_cdevsw); 209 ctty_devsw_installed = 1; 210 #ifdef DEVFS 211 ctty_devfs_token = 212 devfs_add_devswf(&ctty_cdevsw, 0, DV_CHR, 0, 0, 213 0666, "tty"); 214 #endif 215 } 216 } 217 218 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL) 219