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 * $FreeBSD: src/sys/kern/tty_tty.c,v 1.30 1999/09/25 18:24:24 phk Exp $ 35 * $DragonFly: src/sys/kern/tty_tty.c,v 1.10 2004/10/12 19:20:46 dillon Exp $ 36 */ 37 38 /* 39 * Indirect driver for controlling tty. 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/conf.h> 45 #include <sys/lock.h> 46 #include <sys/proc.h> 47 #include <sys/ttycom.h> 48 #include <sys/vnode.h> 49 #include <sys/kernel.h> 50 51 static d_open_t cttyopen; 52 static d_close_t cttyclose; 53 static d_read_t cttyread; 54 static d_write_t cttywrite; 55 static d_ioctl_t cttyioctl; 56 static d_poll_t cttypoll; 57 58 #define CDEV_MAJOR 1 59 /* Don't make this static, since fdesc_vnops uses it. */ 60 struct cdevsw ctty_cdevsw = { 61 /* name */ "ctty", 62 /* maj */ CDEV_MAJOR, 63 /* flags */ D_TTY, 64 /* port */ NULL, 65 /* clone */ NULL, 66 67 /* open */ cttyopen, 68 /* close */ cttyclose, 69 /* read */ cttyread, 70 /* write */ cttywrite, 71 /* ioctl */ cttyioctl, 72 /* poll */ cttypoll, 73 /* mmap */ nommap, 74 /* strategy */ nostrategy, 75 /* dump */ nodump, 76 /* psize */ nopsize 77 }; 78 79 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL) 80 81 /*ARGSUSED*/ 82 static int 83 cttyopen(dev_t dev, int flag, int mode, struct thread *td) 84 { 85 struct proc *p = td->td_proc; 86 struct vnode *ttyvp; 87 int error; 88 89 KKASSERT(p); 90 ttyvp = cttyvp(p); 91 if (ttyvp) { 92 if (ttyvp->v_flag & VCTTYISOPEN) { 93 error = 0; 94 } else { 95 vsetflags(ttyvp, VCTTYISOPEN); 96 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td); 97 error = VOP_OPEN(ttyvp, flag, NOCRED, td); 98 VOP_UNLOCK(ttyvp, 0, td); 99 } 100 } else { 101 error = ENXIO; 102 } 103 return (error); 104 } 105 106 static int 107 cttyclose(dev_t dev, int fflag, int devtype, struct thread *td) 108 { 109 struct proc *p = td->td_proc; 110 struct vnode *ttyvp; 111 int error; 112 113 KKASSERT(p); 114 ttyvp = cttyvp(p); 115 if (ttyvp == NULL) { 116 /* 117 * The tty may have been TIOCNOTTY'd, don't return an 118 * error on close. We just have nothing to do. 119 */ 120 /* error = EIO; */ 121 error = 0; 122 } else if (ttyvp->v_flag & VCTTYISOPEN) { 123 vclrflags(ttyvp, VCTTYISOPEN); 124 error = vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td); 125 if (error == 0) { 126 error = VOP_CLOSE(ttyvp, fflag, td); 127 VOP_UNLOCK(ttyvp, 0, td); 128 } 129 } else { 130 error = 0; 131 } 132 return(error); 133 } 134 135 /*ARGSUSED*/ 136 static int 137 cttyread(dev, uio, flag) 138 dev_t dev; 139 struct uio *uio; 140 int flag; 141 { 142 struct thread *td = uio->uio_td; 143 struct proc *p = td->td_proc; 144 struct vnode *ttyvp; 145 int error; 146 147 KKASSERT(p); 148 ttyvp = cttyvp(p); 149 if (ttyvp == NULL) 150 return (EIO); 151 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td); 152 error = VOP_READ(ttyvp, uio, flag, NOCRED); 153 VOP_UNLOCK(ttyvp, 0, td); 154 return (error); 155 } 156 157 /*ARGSUSED*/ 158 static int 159 cttywrite(dev, uio, flag) 160 dev_t dev; 161 struct uio *uio; 162 int flag; 163 { 164 struct thread *td = uio->uio_td; 165 struct proc *p = td->td_proc; 166 struct vnode *ttyvp; 167 int error; 168 169 KKASSERT(p); 170 ttyvp = cttyvp(p); 171 if (ttyvp == NULL) 172 return (EIO); 173 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td); 174 error = VOP_WRITE(ttyvp, uio, flag, NOCRED); 175 VOP_UNLOCK(ttyvp, 0, td); 176 return (error); 177 } 178 179 /*ARGSUSED*/ 180 static int 181 cttyioctl(dev, cmd, addr, flag, td) 182 dev_t dev; 183 u_long cmd; 184 caddr_t addr; 185 int flag; 186 struct thread *td; 187 { 188 struct vnode *ttyvp; 189 struct proc *p = td->td_proc; 190 191 KKASSERT(p); 192 ttyvp = cttyvp(p); 193 if (ttyvp == NULL) 194 return (EIO); 195 if (cmd == TIOCSCTTY) /* don't allow controlling tty to be set */ 196 return EINVAL; /* to controlling tty -- infinite recursion */ 197 if (cmd == TIOCNOTTY) { 198 if (!SESS_LEADER(p)) { 199 p->p_flag &= ~P_CONTROLT; 200 return (0); 201 } else 202 return (EINVAL); 203 } 204 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, td)); 205 } 206 207 /*ARGSUSED*/ 208 static int 209 cttypoll(dev_t dev, int events, struct thread *td) 210 { 211 struct vnode *ttyvp; 212 struct proc *p = td->td_proc; 213 214 KKASSERT(p); 215 ttyvp = cttyvp(p); 216 if (ttyvp == NULL) 217 /* try operation to get EOF/failure */ 218 return (seltrue(dev, events, td)); 219 return (VOP_POLL(ttyvp, events, p->p_ucred, td)); 220 } 221 222 static void ctty_drvinit (void *unused); 223 static void 224 ctty_drvinit(unused) 225 void *unused; 226 { 227 cdevsw_add(&ctty_cdevsw, 0, 0); 228 make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "tty"); 229 } 230 231 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL) 232