xref: /openbsd-src/sys/kern/tty_tty.c (revision df930be708d50e9715f173caa26ffe1b7599b157)
1 /*	$NetBSD: tty_tty.c,v 1.11 1994/10/30 21:48:05 cgd 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/conf.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 
50 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
51 
52 /*ARGSUSED*/
53 cttyopen(dev, flag, mode, p)
54 	dev_t dev;
55 	int flag, mode;
56 	struct proc *p;
57 {
58 	struct vnode *ttyvp = cttyvp(p);
59 	int error;
60 
61 	if (ttyvp == NULL)
62 		return (ENXIO);
63 	VOP_LOCK(ttyvp);
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), p->p_ucred, p);
75 	if (!error)
76 #endif /* PARANOID */
77 		error = VOP_OPEN(ttyvp, flag, NOCRED, p);
78 	VOP_UNLOCK(ttyvp);
79 	return (error);
80 }
81 
82 /*ARGSUSED*/
83 cttyread(dev, uio, flag)
84 	dev_t dev;
85 	struct uio *uio;
86 	int flag;
87 {
88 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
89 	int error;
90 
91 	if (ttyvp == NULL)
92 		return (EIO);
93 	VOP_LOCK(ttyvp);
94 	error = VOP_READ(ttyvp, uio, flag, NOCRED);
95 	VOP_UNLOCK(ttyvp);
96 	return (error);
97 }
98 
99 /*ARGSUSED*/
100 cttywrite(dev, uio, flag)
101 	dev_t dev;
102 	struct uio *uio;
103 	int flag;
104 {
105 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
106 	int error;
107 
108 	if (ttyvp == NULL)
109 		return (EIO);
110 	VOP_LOCK(ttyvp);
111 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
112 	VOP_UNLOCK(ttyvp);
113 	return (error);
114 }
115 
116 /*ARGSUSED*/
117 cttyioctl(dev, cmd, addr, flag, p)
118 	dev_t dev;
119 	u_long cmd;
120 	caddr_t addr;
121 	int flag;
122 	struct proc *p;
123 {
124 	struct vnode *ttyvp = cttyvp(p);
125 
126 	if (ttyvp == NULL)
127 		return (EIO);
128 	if (cmd == TIOCSCTTY)		/* XXX */
129 		return (EINVAL);
130 	if (cmd == TIOCNOTTY) {
131 		if (!SESS_LEADER(p)) {
132 			p->p_flag &= ~P_CONTROLT;
133 			return (0);
134 		} else
135 			return (EINVAL);
136 	}
137 	return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
138 }
139 
140 /*ARGSUSED*/
141 cttyselect(dev, flag, p)
142 	dev_t dev;
143 	int flag;
144 	struct proc *p;
145 {
146 	struct vnode *ttyvp = cttyvp(p);
147 
148 	if (ttyvp == NULL)
149 		return (1);	/* try operation to get EOF/failure */
150 	return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p));
151 }
152