tty_tty.c revision 1.40.18.1 1 /* $NetBSD: tty_tty.c,v 1.40.18.1 2017/04/27 05:36:37 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1991, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)tty_tty.c 8.2 (Berkeley) 9/23/93
32 */
33
34 /*
35 * Indirect driver for controlling tty.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: tty_tty.c,v 1.40.18.1 2017/04/27 05:36:37 pgoyette Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.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/conf.h>
49 #include <sys/kauth.h>
50 #include <sys/localcount.h>
51
52 /* XXXSMP */
53 #define cttyvp(p) ((p)->p_lflag & PL_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
54
55 /*ARGSUSED*/
56 static int
57 cttyopen(dev_t dev, int flag, int mode, struct lwp *l)
58 {
59 struct vnode *ttyvp = cttyvp(l->l_proc);
60 int error;
61
62 if (ttyvp == NULL)
63 return (ENXIO);
64 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
65 #ifdef PARANOID
66 /*
67 * Since group is tty and mode is 620 on most terminal lines
68 * and since sessions protect terminals from processes outside
69 * your session, this check is probably no longer necessary.
70 * Since it inhibits setuid root programs that later switch
71 * to another user from accessing /dev/tty, we have decided
72 * to delete this test. (mckusick 5/93)
73 */
74 error = VOP_ACCESS(ttyvp,
75 (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), l->l_cred, l);
76 if (!error)
77 #endif /* PARANOID */
78 error = VOP_OPEN(ttyvp, flag, NOCRED);
79 VOP_UNLOCK(ttyvp);
80 return (error);
81 }
82
83 /*ARGSUSED*/
84 static int
85 cttyread(dev_t dev, struct uio *uio, int flag)
86 {
87 struct vnode *ttyvp = cttyvp(curproc);
88 int error;
89
90 if (ttyvp == NULL)
91 return (EIO);
92 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
93 error = VOP_READ(ttyvp, uio, flag, NOCRED);
94 VOP_UNLOCK(ttyvp);
95 return (error);
96 }
97
98 /*ARGSUSED*/
99 static int
100 cttywrite(dev_t dev, struct uio *uio, int flag)
101 {
102 struct vnode *ttyvp = cttyvp(curproc);
103 int error;
104
105 if (ttyvp == NULL)
106 return (EIO);
107 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY);
108 error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
109 VOP_UNLOCK(ttyvp);
110 return (error);
111 }
112
113 /*ARGSUSED*/
114 static int
115 cttyioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
116 {
117 struct vnode *ttyvp = cttyvp(l->l_proc);
118 int rv;
119
120 if (ttyvp == NULL)
121 return (EIO);
122 if (cmd == TIOCSCTTY) /* XXX */
123 return (EINVAL);
124 if (cmd == TIOCNOTTY) {
125 mutex_enter(proc_lock);
126 if (!SESS_LEADER(l->l_proc)) {
127 l->l_proc->p_lflag &= ~PL_CONTROLT;
128 rv = 0;
129 } else
130 rv = EINVAL;
131 mutex_exit(proc_lock);
132 return (rv);
133 }
134 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED));
135 }
136
137 /*ARGSUSED*/
138 static int
139 cttypoll(dev_t dev, int events, struct lwp *l)
140 {
141 struct vnode *ttyvp = cttyvp(l->l_proc);
142
143 if (ttyvp == NULL)
144 return (seltrue(dev, events, l));
145 return (VOP_POLL(ttyvp, events));
146 }
147
148 static int
149 cttykqfilter(dev_t dev, struct knote *kn)
150 {
151 /* This is called from filt_fileattach() by the attaching process. */
152 struct proc *p = curproc;
153 struct vnode *ttyvp = cttyvp(p);
154
155 if (ttyvp == NULL)
156 return (1);
157 return (VOP_KQFILTER(ttyvp, kn));
158 }
159
160 const struct cdevsw ctty_cdevsw = {
161 DEVSW_MODULE_INIT
162 .d_open = cttyopen,
163 .d_close = nullclose,
164 .d_read = cttyread,
165 .d_write = cttywrite,
166 .d_ioctl = cttyioctl,
167 .d_stop = nullstop,
168 .d_tty = notty,
169 .d_poll = cttypoll,
170 .d_mmap = nommap,
171 .d_kqfilter = cttykqfilter,
172 .d_discard = nodiscard,
173 .d_flag = D_TTY
174 };
175