xref: /freebsd/sys/kern/tty_tty.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)tty_tty.c	8.2 (Berkeley) 9/23/93
34  */
35 
36 /*
37  * Indirect driver for controlling tty.
38  */
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/conf.h>
42 #include <sys/ioctl.h>
43 #include <sys/proc.h>
44 #include <sys/tty.h>
45 #include <sys/vnode.h>
46 #include <sys/file.h>
47 
48 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
49 
50 /*ARGSUSED*/
51 int
52 cttyopen(dev, flag, mode, p)
53 	dev_t dev;
54 	int flag, mode;
55 	struct proc *p;
56 {
57 	struct vnode *ttyvp = cttyvp(p);
58 	int error;
59 
60 	if (ttyvp == NULL)
61 		return (ENXIO);
62 	VOP_LOCK(ttyvp);
63 #ifdef PARANOID
64 	/*
65 	 * Since group is tty and mode is 620 on most terminal lines
66 	 * and since sessions protect terminals from processes outside
67 	 * your session, this check is probably no longer necessary.
68 	 * Since it inhibits setuid root programs that later switch
69 	 * to another user from accessing /dev/tty, we have decided
70 	 * to delete this test. (mckusick 5/93)
71 	 */
72 	error = VOP_ACCESS(ttyvp,
73 	  (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p);
74 	if (!error)
75 #endif /* PARANOID */
76 		error = VOP_OPEN(ttyvp, flag, NOCRED, p);
77 	VOP_UNLOCK(ttyvp);
78 	return (error);
79 }
80 
81 /*ARGSUSED*/
82 int
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 int
101 cttywrite(dev, uio, flag)
102 	dev_t dev;
103 	struct uio *uio;
104 	int flag;
105 {
106 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
107 	int error;
108 
109 	if (ttyvp == NULL)
110 		return (EIO);
111 	VOP_LOCK(ttyvp);
112 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
113 	VOP_UNLOCK(ttyvp);
114 	return (error);
115 }
116 
117 /*ARGSUSED*/
118 int
119 cttyioctl(dev, cmd, addr, flag, p)
120 	dev_t dev;
121 	int cmd;
122 	caddr_t addr;
123 	int flag;
124 	struct proc *p;
125 {
126 	struct vnode *ttyvp = cttyvp(p);
127 
128 	if (ttyvp == NULL)
129 		return (EIO);
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 int
142 cttyselect(dev, flag, p)
143 	dev_t dev;
144 	int flag;
145 	struct proc *p;
146 {
147 	struct vnode *ttyvp = cttyvp(p);
148 
149 	if (ttyvp == NULL)
150 		return (1);	/* try operation to get EOF/failure */
151 	return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p));
152 }
153