tty_tty.c revision 1.3 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.3 1993/05/20 02:55:18 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 "select.h"
45 #include "tty.h"
46 #include "proc.h"
47 #include "vnode.h"
48 #include "file.h"
49
50 #define cttyvp(p) ((p)->p_flag&SCTTY ? (p)->p_session->s_ttyvp : NULL)
51
52 /*ARGSUSED*/
53 cttyopen(dev, flag, mode, p)
54 dev_t dev;
55 int flag, mode;
56 struct proc *p;
57 {
58 struct vnode *ttyvp = cttyvp(p);
59 int error;
60
61 if (ttyvp == NULL)
62 return (ENXIO);
63 VOP_LOCK(ttyvp);
64 error = VOP_ACCESS(ttyvp,
65 (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p);
66 if (!error)
67 error = VOP_OPEN(ttyvp, flag, NOCRED, p);
68 VOP_UNLOCK(ttyvp);
69 return (error);
70 }
71
72 /*ARGSUSED*/
73 cttyread(dev, uio, flag)
74 dev_t dev;
75 struct uio *uio;
76 {
77 register struct vnode *ttyvp = cttyvp(uio->uio_procp);
78 int error;
79
80 if (ttyvp == NULL)
81 return (EIO);
82 VOP_LOCK(ttyvp);
83 error = VOP_READ(ttyvp, uio, flag, NOCRED);
84 VOP_UNLOCK(ttyvp);
85 return (error);
86 }
87
88 /*ARGSUSED*/
89 cttywrite(dev, uio, flag)
90 dev_t dev;
91 struct uio *uio;
92 {
93 register struct vnode *ttyvp = cttyvp(uio->uio_procp);
94 int error;
95
96 if (ttyvp == NULL)
97 return (EIO);
98 VOP_LOCK(ttyvp);
99 error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
100 VOP_UNLOCK(ttyvp);
101 return (error);
102 }
103
104 /*ARGSUSED*/
105 cttyioctl(dev, cmd, addr, flag, p)
106 dev_t dev;
107 int cmd;
108 caddr_t addr;
109 int flag;
110 struct proc *p;
111 {
112 struct vnode *ttyvp = cttyvp(p);
113
114 if (ttyvp == NULL)
115 return (EIO);
116 if (cmd == TIOCNOTTY) {
117 if (!SESS_LEADER(p)) {
118 p->p_flag &= ~SCTTY;
119 return (0);
120 } else
121 return (EINVAL);
122 }
123 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
124 }
125
126 /*ARGSUSED*/
127 cttyselect(dev, flag, p)
128 dev_t dev;
129 int flag;
130 struct proc *p;
131 {
132 struct vnode *ttyvp = cttyvp(p);
133
134 if (ttyvp == NULL)
135 return (1); /* try operation to get EOF/failure */
136 return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p));
137 }
138