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$ 35 */ 36 37 /* 38 * Indirect driver for controlling tty. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/conf.h> 44 #include <sys/kernel.h> 45 #include <sys/lock.h> 46 #include <sys/mutex.h> 47 #include <sys/sx.h> 48 #include <sys/proc.h> 49 #include <sys/ttycom.h> 50 #include <sys/vnode.h> 51 52 static d_open_t cttyopen; 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 60 static struct cdevsw ctty_cdevsw = { 61 /* open */ cttyopen, 62 /* close */ nullclose, 63 /* read */ cttyread, 64 /* write */ cttywrite, 65 /* ioctl */ cttyioctl, 66 /* poll */ cttypoll, 67 /* mmap */ nommap, 68 /* strategy */ nostrategy, 69 /* name */ "ctty", 70 /* maj */ CDEV_MAJOR, 71 /* dump */ nodump, 72 /* psize */ nopsize, 73 /* flags */ D_TTY, 74 }; 75 76 #define cttyvp(td) ((td)->td_proc->p_flag & P_CONTROLT ? (td)->td_proc->p_session->s_ttyvp : NULL) 77 78 /*ARGSUSED*/ 79 static int 80 cttyopen(dev, flag, mode, td) 81 dev_t dev; 82 int flag, mode; 83 struct thread *td; 84 { 85 struct vnode *ttyvp; 86 int error; 87 88 PROC_LOCK(td->td_proc); 89 SESS_LOCK(td->td_proc->p_session); 90 ttyvp = cttyvp(td); 91 SESS_UNLOCK(td->td_proc->p_session); 92 PROC_UNLOCK(td->td_proc); 93 94 if (ttyvp == NULL) 95 return (ENXIO); 96 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td); 97 error = VOP_OPEN(ttyvp, flag, NOCRED, td); 98 VOP_UNLOCK(ttyvp, 0, td); 99 return (error); 100 } 101 102 /*ARGSUSED*/ 103 static int 104 cttyread(dev, uio, flag) 105 dev_t dev; 106 struct uio *uio; 107 int flag; 108 { 109 struct thread *td = uio->uio_td; 110 register struct vnode *ttyvp; 111 int error; 112 113 PROC_LOCK(td->td_proc); 114 SESS_LOCK(td->td_proc->p_session); 115 ttyvp = cttyvp(td); 116 SESS_UNLOCK(td->td_proc->p_session); 117 PROC_UNLOCK(td->td_proc); 118 119 if (ttyvp == NULL) 120 return (EIO); 121 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td); 122 error = VOP_READ(ttyvp, uio, flag, NOCRED); 123 VOP_UNLOCK(ttyvp, 0, td); 124 return (error); 125 } 126 127 /*ARGSUSED*/ 128 static int 129 cttywrite(dev, uio, flag) 130 dev_t dev; 131 struct uio *uio; 132 int flag; 133 { 134 struct thread *td = uio->uio_td; 135 struct vnode *ttyvp; 136 struct mount *mp; 137 int error; 138 139 PROC_LOCK(td->td_proc); 140 SESS_LOCK(td->td_proc->p_session); 141 ttyvp = cttyvp(td); 142 SESS_UNLOCK(td->td_proc->p_session); 143 PROC_UNLOCK(td->td_proc); 144 145 if (ttyvp == NULL) 146 return (EIO); 147 mp = NULL; 148 if (ttyvp->v_type != VCHR && 149 (error = vn_start_write(ttyvp, &mp, V_WAIT | PCATCH)) != 0) 150 return (error); 151 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td); 152 error = VOP_WRITE(ttyvp, uio, flag, NOCRED); 153 VOP_UNLOCK(ttyvp, 0, td); 154 vn_finished_write(mp); 155 return (error); 156 } 157 158 /*ARGSUSED*/ 159 static int 160 cttyioctl(dev, cmd, addr, flag, td) 161 dev_t dev; 162 u_long cmd; 163 caddr_t addr; 164 int flag; 165 struct thread *td; 166 { 167 struct vnode *ttyvp; 168 int error; 169 170 PROC_LOCK(td->td_proc); 171 SESS_LOCK(td->td_proc->p_session); 172 ttyvp = cttyvp(td); 173 SESS_UNLOCK(td->td_proc->p_session); 174 PROC_UNLOCK(td->td_proc); 175 176 if (ttyvp == NULL) 177 return (EIO); 178 if (cmd == TIOCSCTTY) /* don't allow controlling tty to be set */ 179 return EINVAL; /* to controlling tty -- infinite recursion */ 180 if (cmd == TIOCNOTTY) { 181 PROC_LOCK(td->td_proc); 182 SESS_LOCK(td->td_proc->p_session); 183 error = 0; 184 if (!SESS_LEADER(td->td_proc)) 185 td->td_proc->p_flag &= ~P_CONTROLT; 186 else 187 error = EINVAL; 188 SESS_UNLOCK(td->td_proc->p_session); 189 PROC_UNLOCK(td->td_proc); 190 return (error); 191 } 192 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, td)); 193 } 194 195 /*ARGSUSED*/ 196 static int 197 cttypoll(dev, events, td) 198 dev_t dev; 199 int events; 200 struct thread *td; 201 { 202 struct vnode *ttyvp; 203 204 PROC_LOCK(td->td_proc); 205 SESS_LOCK(td->td_proc->p_session); 206 ttyvp = cttyvp(td); 207 SESS_UNLOCK(td->td_proc->p_session); 208 PROC_UNLOCK(td->td_proc); 209 210 if (ttyvp == NULL) 211 /* try operation to get EOF/failure */ 212 return (seltrue(dev, events, td)); 213 return (VOP_POLL(ttyvp, events, td->td_ucred, td)); 214 } 215 216 static void ctty_clone(void *arg, char *name, int namelen, dev_t *dev); 217 218 static dev_t ctty; 219 220 static void 221 ctty_clone(void *arg, char *name, int namelen, dev_t *dev) 222 { 223 struct vnode *vp; 224 225 if (*dev != NODEV) 226 return; 227 if (strcmp(name, "tty")) 228 return; 229 vp = cttyvp(curthread); 230 if (vp == NULL) { 231 if (ctty) 232 *dev = ctty; 233 } else 234 *dev = vp->v_rdev; 235 } 236 237 238 static void ctty_drvinit(void *unused); 239 static void 240 ctty_drvinit(unused) 241 void *unused; 242 { 243 244 if (devfs_present) { 245 EVENTHANDLER_REGISTER(dev_clone, ctty_clone, 0, 1000); 246 ctty = make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "ctty"); 247 } else { 248 make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "tty"); 249 } 250 } 251 252 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL) 253