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