Home | History | Annotate | Line # | Download | only in kern
      1 /*	$NetBSD: tty_tty.c,v 1.41 2020/05/23 23:42:43 ad 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.41 2020/05/23 23:42:43 ad 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 #include <sys/kauth.h>
     50 
     51 /* XXXSMP */
     52 #define cttyvp(p) ((p)->p_lflag & PL_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
     53 
     54 /*ARGSUSED*/
     55 static int
     56 cttyopen(dev_t dev, int flag, int mode, struct lwp *l)
     57 {
     58 	struct vnode *ttyvp = cttyvp(l->l_proc);
     59 	int error;
     60 
     61 	if (ttyvp == NULL)
     62 		return (ENXIO);
     63 	vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
     64 #ifdef PARANOID
     65 	/*
     66 	 * Since group is tty and mode is 620 on most terminal lines
     67 	 * and since sessions protect terminals from processes outside
     68 	 * your session, this check is probably no longer necessary.
     69 	 * Since it inhibits setuid root programs that later switch
     70 	 * to another user from accessing /dev/tty, we have decided
     71 	 * to delete this test. (mckusick 5/93)
     72 	 */
     73 	error = VOP_ACCESS(ttyvp,
     74 	  (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), l->l_cred, l);
     75 	if (!error)
     76 #endif /* PARANOID */
     77 		error = VOP_OPEN(ttyvp, flag, NOCRED);
     78 	VOP_UNLOCK(ttyvp);
     79 	return (error);
     80 }
     81 
     82 /*ARGSUSED*/
     83 static int
     84 cttyread(dev_t dev, struct uio *uio, int flag)
     85 {
     86 	struct vnode *ttyvp = cttyvp(curproc);
     87 	int error;
     88 
     89 	if (ttyvp == NULL)
     90 		return (EIO);
     91 	vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
     92 	error = VOP_READ(ttyvp, uio, flag, NOCRED);
     93 	VOP_UNLOCK(ttyvp);
     94 	return (error);
     95 }
     96 
     97 /*ARGSUSED*/
     98 static int
     99 cttywrite(dev_t dev, struct uio *uio, int flag)
    100 {
    101 	struct vnode *ttyvp = cttyvp(curproc);
    102 	int error;
    103 
    104 	if (ttyvp == NULL)
    105 		return (EIO);
    106 	vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
    107 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
    108 	VOP_UNLOCK(ttyvp);
    109 	return (error);
    110 }
    111 
    112 /*ARGSUSED*/
    113 static int
    114 cttyioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    115 {
    116 	struct vnode *ttyvp = cttyvp(l->l_proc);
    117 	int rv;
    118 
    119 	if (ttyvp == NULL)
    120 		return (EIO);
    121 	if (cmd == TIOCSCTTY)		/* XXX */
    122 		return (EINVAL);
    123 	if (cmd == TIOCNOTTY) {
    124 		mutex_enter(&proc_lock);
    125 		if (!SESS_LEADER(l->l_proc)) {
    126 			l->l_proc->p_lflag &= ~PL_CONTROLT;
    127 			rv = 0;
    128 		} else
    129 			rv = EINVAL;
    130 		mutex_exit(&proc_lock);
    131 		return (rv);
    132 	}
    133 	return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED));
    134 }
    135 
    136 /*ARGSUSED*/
    137 static int
    138 cttypoll(dev_t dev, int events, struct lwp *l)
    139 {
    140 	struct vnode *ttyvp = cttyvp(l->l_proc);
    141 
    142 	if (ttyvp == NULL)
    143 		return (seltrue(dev, events, l));
    144 	return (VOP_POLL(ttyvp, events));
    145 }
    146 
    147 static int
    148 cttykqfilter(dev_t dev, struct knote *kn)
    149 {
    150 	/* This is called from filt_fileattach() by the attaching process. */
    151 	struct proc *p = curproc;
    152 	struct vnode *ttyvp = cttyvp(p);
    153 
    154 	if (ttyvp == NULL)
    155 		return (1);
    156 	return (VOP_KQFILTER(ttyvp, kn));
    157 }
    158 
    159 const struct cdevsw ctty_cdevsw = {
    160 	.d_open = cttyopen,
    161 	.d_close = nullclose,
    162 	.d_read = cttyread,
    163 	.d_write = cttywrite,
    164 	.d_ioctl = cttyioctl,
    165 	.d_stop = nullstop,
    166 	.d_tty = notty,
    167 	.d_poll = cttypoll,
    168 	.d_mmap = nommap,
    169 	.d_kqfilter = cttykqfilter,
    170 	.d_discard = nodiscard,
    171 	.d_flag = D_TTY
    172 };
    173