xref: /openbsd-src/sys/kern/tty_tty.c (revision d724e01ae4dac35949585b9083e28ff2ba35b0b5)
1 /*	$OpenBSD: tty_tty.c,v 1.3 1996/04/21 22:27:32 deraadt Exp $	*/
2 /*	$NetBSD: tty_tty.c,v 1.13 1996/03/30 22:24:46 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1982, 1986, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)tty_tty.c	8.2 (Berkeley) 9/23/93
37  */
38 
39 /*
40  * Indirect driver for controlling tty.
41  */
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/ioctl.h>
45 #include <sys/proc.h>
46 #include <sys/tty.h>
47 #include <sys/vnode.h>
48 #include <sys/file.h>
49 #include <sys/conf.h>
50 
51 
52 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
53 
54 /*ARGSUSED*/
55 int
56 cttyopen(dev, flag, mode, p)
57 	dev_t dev;
58 	int flag, mode;
59 	struct proc *p;
60 {
61 	struct vnode *ttyvp = cttyvp(p);
62 	int error;
63 
64 	if (ttyvp == NULL)
65 		return (ENXIO);
66 	VOP_LOCK(ttyvp);
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);
81 	VOP_UNLOCK(ttyvp);
82 	return (error);
83 }
84 
85 /*ARGSUSED*/
86 int
87 cttyread(dev, uio, flag)
88 	dev_t dev;
89 	struct uio *uio;
90 	int flag;
91 {
92 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
93 	int error;
94 
95 	if (ttyvp == NULL)
96 		return (EIO);
97 	VOP_LOCK(ttyvp);
98 	error = VOP_READ(ttyvp, uio, flag, NOCRED);
99 	VOP_UNLOCK(ttyvp);
100 	return (error);
101 }
102 
103 /*ARGSUSED*/
104 int
105 cttywrite(dev, uio, flag)
106 	dev_t dev;
107 	struct uio *uio;
108 	int flag;
109 {
110 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
111 	int error;
112 
113 	if (ttyvp == NULL)
114 		return (EIO);
115 	VOP_LOCK(ttyvp);
116 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
117 	VOP_UNLOCK(ttyvp);
118 	return (error);
119 }
120 
121 /*ARGSUSED*/
122 int
123 cttyioctl(dev, cmd, addr, flag, p)
124 	dev_t dev;
125 	u_long cmd;
126 	caddr_t addr;
127 	int flag;
128 	struct proc *p;
129 {
130 	struct vnode *ttyvp = cttyvp(p);
131 
132 	if (ttyvp == NULL)
133 		return (EIO);
134 	if (cmd == TIOCSCTTY)		/* XXX */
135 		return (EINVAL);
136 	if (cmd == TIOCNOTTY) {
137 		if (!SESS_LEADER(p)) {
138 			p->p_flag &= ~P_CONTROLT;
139 			return (0);
140 		} else
141 			return (EINVAL);
142 	}
143 	return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
144 }
145 
146 /*ARGSUSED*/
147 int
148 cttyselect(dev, flag, p)
149 	dev_t dev;
150 	int flag;
151 	struct proc *p;
152 {
153 	struct vnode *ttyvp = cttyvp(p);
154 
155 	if (ttyvp == NULL)
156 		return (1);	/* try operation to get EOF/failure */
157 	return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p));
158 }
159