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