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