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