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