xref: /freebsd/sys/kern/tty_tty.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
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  * $Id: tty_tty.c,v 1.3 1994/08/02 07:42:57 davidg Exp $
35  */
36 
37 /*
38  * Indirect driver for controlling tty.
39  */
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/conf.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 
49 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
50 
51 /*ARGSUSED*/
52 int
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 int
84 cttyread(dev, uio, flag)
85 	dev_t dev;
86 	struct uio *uio;
87 	int flag;
88 {
89 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
90 	int error;
91 
92 	if (ttyvp == NULL)
93 		return (EIO);
94 	VOP_LOCK(ttyvp);
95 	error = VOP_READ(ttyvp, uio, flag, NOCRED);
96 	VOP_UNLOCK(ttyvp);
97 	return (error);
98 }
99 
100 /*ARGSUSED*/
101 int
102 cttywrite(dev, uio, flag)
103 	dev_t dev;
104 	struct uio *uio;
105 	int flag;
106 {
107 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
108 	int error;
109 
110 	if (ttyvp == NULL)
111 		return (EIO);
112 	VOP_LOCK(ttyvp);
113 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
114 	VOP_UNLOCK(ttyvp);
115 	return (error);
116 }
117 
118 /*ARGSUSED*/
119 int
120 cttyioctl(dev, cmd, addr, flag, p)
121 	dev_t dev;
122 	int cmd;
123 	caddr_t addr;
124 	int flag;
125 	struct proc *p;
126 {
127 	struct vnode *ttyvp = cttyvp(p);
128 
129 	if (ttyvp == NULL)
130 		return (EIO);
131 	if (cmd == TIOCSCTTY)  /* don't allow controlling tty to be set    */
132 		return EINVAL; /* to controlling tty -- infinite recursion */
133 	if (cmd == TIOCNOTTY) {
134 		if (!SESS_LEADER(p)) {
135 			p->p_flag &= ~P_CONTROLT;
136 			return (0);
137 		} else
138 			return (EINVAL);
139 	}
140 	return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
141 }
142 
143 /*ARGSUSED*/
144 int
145 cttyselect(dev, flag, p)
146 	dev_t dev;
147 	int flag;
148 	struct proc *p;
149 {
150 	struct vnode *ttyvp = cttyvp(p);
151 
152 	if (ttyvp == NULL)
153 		return (1);	/* try operation to get EOF/failure */
154 	return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p));
155 }
156