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