Home | History | Annotate | Line # | Download | only in kern
tty_tty.c revision 1.16.10.2
      1 /*	$NetBSD: tty_tty.c,v 1.16.10.2 2001/09/18 19:13:53 fvdl 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 #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 /*ARGSUSED*/
     54 int
     55 do_cttyopen(devvp, flag, mode, p, vpp)
     56 	struct vnode *devvp;
     57 	int flag, mode;
     58 	struct proc *p;
     59 	struct vnode **vpp;
     60 {
     61 	struct vnode *ttyvp = cttyvp(p);
     62 	int error;
     63 
     64 	if (ttyvp == NULL)
     65 		return (ENXIO);
     66 	vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
     67 #ifdef PARANOID
     68 	/*
     69 	 * Since group is tty and mode is 620 on most terminal lines
     70 	 * and since sessions protect terminals from processes outside
     71 	 * your session, this check is probably no longer necessary.
     72 	 * Since it inhibits setuid root programs that later switch
     73 	 * to another user from accessing /dev/tty, we have decided
     74 	 * to delete this test. (mckusick 5/93)
     75 	 */
     76 	error = VOP_ACCESS(ttyvp,
     77 	  (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p);
     78 	if (!error)
     79 #endif /* PARANOID */
     80 		error = VOP_OPEN(ttyvp, flag, NOCRED, p, vpp);
     81 	VOP_UNLOCK(ttyvp, 0);
     82 	return (error);
     83 }
     84 
     85 int
     86 cttyopen(devvp, flag, mode, p)
     87 	struct vnode *devvp;
     88 	int flag;
     89 	int mode;
     90 	struct proc *p;
     91 {
     92 	return do_cttyopen(devvp, flag, mode, p, NULL);
     93 }
     94 
     95 /*ARGSUSED*/
     96 int
     97 cttyread(devvp, uio, flag)
     98 	struct vnode *devvp;
     99 	struct uio *uio;
    100 	int flag;
    101 {
    102 	struct vnode *ttyvp = cttyvp(uio->uio_procp);
    103 	int error;
    104 
    105 	if (ttyvp == NULL)
    106 		return (EIO);
    107 	vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
    108 	error = VOP_READ(ttyvp, uio, flag, NOCRED);
    109 	VOP_UNLOCK(ttyvp, 0);
    110 	return (error);
    111 }
    112 
    113 /*ARGSUSED*/
    114 int
    115 cttywrite(devvp, uio, flag)
    116 	struct vnode *devvp;
    117 	struct uio *uio;
    118 	int flag;
    119 {
    120 	struct vnode *ttyvp = cttyvp(uio->uio_procp);
    121 	int error;
    122 
    123 	if (ttyvp == NULL)
    124 		return (EIO);
    125 	vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
    126 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
    127 	VOP_UNLOCK(ttyvp, 0);
    128 	return (error);
    129 }
    130 
    131 /*ARGSUSED*/
    132 int
    133 cttyioctl(devvp, cmd, addr, flag, p)
    134 	struct vnode *devvp;
    135 	u_long cmd;
    136 	caddr_t addr;
    137 	int flag;
    138 	struct proc *p;
    139 {
    140 	struct vnode *ttyvp = cttyvp(p);
    141 
    142 	if (ttyvp == NULL)
    143 		return (EIO);
    144 	if (cmd == TIOCSCTTY)		/* XXX */
    145 		return (EINVAL);
    146 	if (cmd == TIOCNOTTY) {
    147 		if (!SESS_LEADER(p)) {
    148 			p->p_flag &= ~P_CONTROLT;
    149 			return (0);
    150 		} else
    151 			return (EINVAL);
    152 	}
    153 	return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
    154 }
    155 
    156 /*ARGSUSED*/
    157 int
    158 cttypoll(devvp, events, p)
    159 	struct vnode *devvp;
    160 	int events;
    161 	struct proc *p;
    162 {
    163 	struct vnode *ttyvp = cttyvp(p);
    164 
    165 	if (ttyvp == NULL)
    166 		return (seltrue(devvp, events, p));
    167 	return (VOP_POLL(ttyvp, events, p));
    168 }
    169