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