xref: /netbsd-src/sys/kern/tty_tty.c (revision ce63d6c20fc4ec8ddc95c84bb229e3c4ecf82b69)
1 /*-
2  * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
3  * 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  *	from: @(#)tty_tty.c	7.15 (Berkeley) 5/28/91
34  *	$Id: tty_tty.c,v 1.4 1993/05/22 11:41:52 cgd Exp $
35  */
36 
37 /*
38  * Indirect driver for controlling tty.
39  */
40 #include "param.h"
41 #include "systm.h"
42 #include "conf.h"
43 #include "ioctl.h"
44 #include "tty.h"
45 #include "proc.h"
46 #include "vnode.h"
47 #include "file.h"
48 
49 #define cttyvp(p) ((p)->p_flag&SCTTY ? (p)->p_session->s_ttyvp : NULL)
50 
51 /*ARGSUSED*/
52 cttyopen(dev, flag, mode, p)
53 	dev_t dev;
54 	int flag, mode;
55 	struct proc *p;
56 {
57 	struct vnode *ttyvp = cttyvp(p);
58 	int error;
59 
60 	if (ttyvp == NULL)
61 		return (ENXIO);
62 	VOP_LOCK(ttyvp);
63 	error = VOP_ACCESS(ttyvp,
64 	  (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p);
65 	if (!error)
66 		error = VOP_OPEN(ttyvp, flag, NOCRED, p);
67 	VOP_UNLOCK(ttyvp);
68 	return (error);
69 }
70 
71 /*ARGSUSED*/
72 cttyread(dev, uio, flag)
73 	dev_t dev;
74 	struct uio *uio;
75 {
76 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
77 	int error;
78 
79 	if (ttyvp == NULL)
80 		return (EIO);
81 	VOP_LOCK(ttyvp);
82 	error = VOP_READ(ttyvp, uio, flag, NOCRED);
83 	VOP_UNLOCK(ttyvp);
84 	return (error);
85 }
86 
87 /*ARGSUSED*/
88 cttywrite(dev, uio, flag)
89 	dev_t dev;
90 	struct uio *uio;
91 {
92 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
93 	int error;
94 
95 	if (ttyvp == NULL)
96 		return (EIO);
97 	VOP_LOCK(ttyvp);
98 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
99 	VOP_UNLOCK(ttyvp);
100 	return (error);
101 }
102 
103 /*ARGSUSED*/
104 cttyioctl(dev, cmd, addr, flag, p)
105 	dev_t dev;
106 	int cmd;
107 	caddr_t addr;
108 	int flag;
109 	struct proc *p;
110 {
111 	struct vnode *ttyvp = cttyvp(p);
112 
113 	if (ttyvp == NULL)
114 		return (EIO);
115 	if (cmd == TIOCNOTTY) {
116 		if (!SESS_LEADER(p)) {
117 			p->p_flag &= ~SCTTY;
118 			return (0);
119 		} else
120 			return (EINVAL);
121 	}
122 	return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
123 }
124 
125 /*ARGSUSED*/
126 cttyselect(dev, flag, p)
127 	dev_t dev;
128 	int flag;
129 	struct proc *p;
130 {
131 	struct vnode *ttyvp = cttyvp(p);
132 
133 	if (ttyvp == NULL)
134 		return (1);	/* try operation to get EOF/failure */
135 	return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p));
136 }
137