xref: /freebsd/sys/kern/tty_tty.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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  * $FreeBSD$
35  */
36 
37 /*
38  * Indirect driver for controlling tty.
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/lock.h>
45 #include <sys/proc.h>
46 #include <sys/ttycom.h>
47 #include <sys/vnode.h>
48 #include <sys/kernel.h>
49 
50 static	d_open_t	cttyopen;
51 static	d_read_t	cttyread;
52 static	d_write_t	cttywrite;
53 static	d_ioctl_t	cttyioctl;
54 static	d_poll_t	cttypoll;
55 
56 #define	CDEV_MAJOR	1
57 /* Don't make this static, since fdesc_vnops uses it. */
58 struct cdevsw ctty_cdevsw = {
59 	/* open */	cttyopen,
60 	/* close */	nullclose,
61 	/* read */	cttyread,
62 	/* write */	cttywrite,
63 	/* ioctl */	cttyioctl,
64 	/* poll */	cttypoll,
65 	/* mmap */	nommap,
66 	/* strategy */	nostrategy,
67 	/* name */	"ctty",
68 	/* maj */	CDEV_MAJOR,
69 	/* dump */	nodump,
70 	/* psize */	nopsize,
71 	/* flags */	D_TTY,
72 	/* bmaj */	-1
73 };
74 
75 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
76 
77 /*ARGSUSED*/
78 static	int
79 cttyopen(dev, flag, mode, p)
80 	dev_t dev;
81 	int flag, mode;
82 	struct proc *p;
83 {
84 	struct vnode *ttyvp = cttyvp(p);
85 	int error;
86 
87 	if (ttyvp == NULL)
88 		return (ENXIO);
89 	vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
90 #ifdef PARANOID
91 	/*
92 	 * Since group is tty and mode is 620 on most terminal lines
93 	 * and since sessions protect terminals from processes outside
94 	 * your session, this check is probably no longer necessary.
95 	 * Since it inhibits setuid root programs that later switch
96 	 * to another user from accessing /dev/tty, we have decided
97 	 * to delete this test. (mckusick 5/93)
98 	 */
99 	error = VOP_ACCESS(ttyvp,
100 	  (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p);
101 	if (!error)
102 #endif /* PARANOID */
103 		error = VOP_OPEN(ttyvp, flag, NOCRED, p);
104 	VOP_UNLOCK(ttyvp, 0, p);
105 	return (error);
106 }
107 
108 /*ARGSUSED*/
109 static	int
110 cttyread(dev, uio, flag)
111 	dev_t dev;
112 	struct uio *uio;
113 	int flag;
114 {
115 	struct proc *p = uio->uio_procp;
116 	register struct vnode *ttyvp = cttyvp(p);
117 	int error;
118 
119 	if (ttyvp == NULL)
120 		return (EIO);
121 	vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
122 	error = VOP_READ(ttyvp, uio, flag, NOCRED);
123 	VOP_UNLOCK(ttyvp, 0, p);
124 	return (error);
125 }
126 
127 /*ARGSUSED*/
128 static	int
129 cttywrite(dev, uio, flag)
130 	dev_t dev;
131 	struct uio *uio;
132 	int flag;
133 {
134 	struct proc *p = uio->uio_procp;
135 	struct vnode *ttyvp = cttyvp(uio->uio_procp);
136 	int error;
137 
138 	if (ttyvp == NULL)
139 		return (EIO);
140 	vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
141 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
142 	VOP_UNLOCK(ttyvp, 0, p);
143 	return (error);
144 }
145 
146 /*ARGSUSED*/
147 static	int
148 cttyioctl(dev, cmd, addr, flag, p)
149 	dev_t dev;
150 	u_long cmd;
151 	caddr_t addr;
152 	int flag;
153 	struct proc *p;
154 {
155 	struct vnode *ttyvp = cttyvp(p);
156 
157 	if (ttyvp == NULL)
158 		return (EIO);
159 	if (cmd == TIOCSCTTY)  /* don't allow controlling tty to be set    */
160 		return EINVAL; /* to controlling tty -- infinite recursion */
161 	if (cmd == TIOCNOTTY) {
162 		if (!SESS_LEADER(p)) {
163 			p->p_flag &= ~P_CONTROLT;
164 			return (0);
165 		} else
166 			return (EINVAL);
167 	}
168 	return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
169 }
170 
171 /*ARGSUSED*/
172 static	int
173 cttypoll(dev, events, p)
174 	dev_t dev;
175 	int events;
176 	struct proc *p;
177 {
178 	struct vnode *ttyvp = cttyvp(p);
179 
180 	if (ttyvp == NULL)
181 		/* try operation to get EOF/failure */
182 		return (seltrue(dev, events, p));
183 	return (VOP_POLL(ttyvp, events, p->p_ucred, p));
184 }
185 
186 static void ctty_drvinit __P((void *unused));
187 static void
188 ctty_drvinit(unused)
189 	void *unused;
190 {
191 
192 	make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "tty");
193 }
194 
195 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)
196