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