Line data Source code
1 : /* $OpenBSD: cons.c,v 1.28 2018/02/19 08:59:52 mpi Exp $ */
2 : /* $NetBSD: cons.c,v 1.30 1996/04/08 19:57:30 jonathan Exp $ */
3 :
4 : /*
5 : * Copyright (c) 1988 University of Utah.
6 : * Copyright (c) 1990, 1993
7 : * The Regents of the University of California. All rights reserved.
8 : *
9 : * This code is derived from software contributed to Berkeley by
10 : * the Systems Programming Group of the University of Utah Computer
11 : * Science Department.
12 : *
13 : * Redistribution and use in source and binary forms, with or without
14 : * modification, are permitted provided that the following conditions
15 : * are met:
16 : * 1. Redistributions of source code must retain the above copyright
17 : * notice, this list of conditions and the following disclaimer.
18 : * 2. Redistributions in binary form must reproduce the above copyright
19 : * notice, this list of conditions and the following disclaimer in the
20 : * documentation and/or other materials provided with the distribution.
21 : * 3. Neither the name of the University nor the names of its contributors
22 : * may be used to endorse or promote products derived from this software
23 : * without specific prior written permission.
24 : *
25 : * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 : * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 : * SUCH DAMAGE.
36 : *
37 : * from: Utah $Hdr: cons.c 1.7 92/01/21$
38 : *
39 : * @(#)cons.c 8.2 (Berkeley) 1/12/94
40 : */
41 :
42 : #include <sys/param.h>
43 : #include <sys/systm.h>
44 : #include <sys/ioctl.h>
45 : #include <sys/tty.h>
46 : #include <sys/conf.h>
47 : #include <sys/vnode.h>
48 : #include <sys/poll.h>
49 :
50 : #include <dev/cons.h>
51 :
52 : struct tty *constty = NULL; /* virtual console output device */
53 : struct vnode *cn_devvp = NULLVP; /* vnode for underlying device. */
54 :
55 : int
56 0 : cnopen(dev_t dev, int flag, int mode, struct proc *p)
57 : {
58 : dev_t cndev;
59 :
60 0 : if (cn_tab == NULL)
61 0 : return (0);
62 :
63 : /*
64 : * always open the 'real' console device, so we don't get nailed
65 : * later. This follows normal device semantics; they always get
66 : * open() calls.
67 : */
68 0 : cndev = cn_tab->cn_dev;
69 0 : if (cndev == NODEV)
70 0 : return (ENXIO);
71 : #ifdef DIAGNOSTIC
72 0 : if (cndev == dev)
73 0 : panic("cnopen: recursive");
74 : #endif
75 0 : if (cn_devvp == NULLVP) {
76 : /* try to get a reference on its vnode, but fail silently */
77 0 : cdevvp(cndev, &cn_devvp);
78 0 : }
79 0 : return ((*cdevsw[major(cndev)].d_open)(cndev, flag, mode, p));
80 0 : }
81 :
82 : int
83 0 : cnclose(dev_t dev, int flag, int mode, struct proc *p)
84 : {
85 0 : struct vnode *vp;
86 :
87 0 : if (cn_tab == NULL)
88 0 : return (0);
89 :
90 : /*
91 : * If the real console isn't otherwise open, close it.
92 : * If it's otherwise open, don't close it, because that'll
93 : * screw up others who have it open.
94 : */
95 0 : dev = cn_tab->cn_dev;
96 0 : if (cn_devvp != NULLVP) {
97 : /* release our reference to real dev's vnode */
98 0 : vrele(cn_devvp);
99 0 : cn_devvp = NULLVP;
100 0 : }
101 0 : if (vfinddev(dev, VCHR, &vp) && vcount(vp))
102 0 : return (0);
103 0 : return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p));
104 0 : }
105 :
106 : int
107 0 : cnread(dev_t dev, struct uio *uio, int flag)
108 : {
109 :
110 : /*
111 : * If we would redirect input, punt. This will keep strange
112 : * things from happening to people who are using the real
113 : * console. Nothing should be using /dev/console for
114 : * input (except a shell in single-user mode, but then,
115 : * one wouldn't TIOCCONS then).
116 : */
117 0 : if (constty != NULL)
118 0 : return 0;
119 0 : else if (cn_tab == NULL)
120 0 : return ENXIO;
121 :
122 0 : dev = cn_tab->cn_dev;
123 0 : return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
124 0 : }
125 :
126 : int
127 0 : cnwrite(dev_t dev, struct uio *uio, int flag)
128 : {
129 :
130 : /*
131 : * Redirect output, if that's appropriate.
132 : * If there's no real console, return ENXIO.
133 : */
134 0 : if (constty != NULL)
135 0 : dev = constty->t_dev;
136 0 : else if (cn_tab == NULL)
137 0 : return ENXIO;
138 : else
139 0 : dev = cn_tab->cn_dev;
140 0 : return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
141 0 : }
142 :
143 : int
144 0 : cnstop(struct tty *tp, int flag)
145 : {
146 0 : return (0);
147 : }
148 :
149 : int
150 0 : cnioctl(dev_t dev, u_long cmd, caddr_t data, int flag,
151 : struct proc *p)
152 : {
153 : int error;
154 :
155 : /*
156 : * Superuser can always use this to wrest control of console
157 : * output from the "virtual" console.
158 : */
159 0 : if (cmd == TIOCCONS && constty != NULL) {
160 0 : error = suser(p);
161 0 : if (error)
162 0 : return (error);
163 0 : constty = NULL;
164 0 : return (0);
165 : }
166 :
167 : /*
168 : * Redirect the ioctl, if that's appropriate.
169 : * Note that strange things can happen, if a program does
170 : * ioctls on /dev/console, then the console is redirected
171 : * out from under it.
172 : */
173 0 : if (constty != NULL)
174 0 : dev = constty->t_dev;
175 0 : else if (cn_tab == NULL)
176 0 : return ENXIO;
177 : else
178 0 : dev = cn_tab->cn_dev;
179 0 : return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
180 0 : }
181 :
182 : /*ARGSUSED*/
183 : int
184 0 : cnpoll(dev_t dev, int rw, struct proc *p)
185 : {
186 :
187 : /*
188 : * Redirect the poll, if that's appropriate.
189 : * I don't want to think of the possible side effects
190 : * of console redirection here.
191 : */
192 0 : if (constty != NULL)
193 0 : dev = constty->t_dev;
194 0 : else if (cn_tab == NULL)
195 0 : return POLLERR;
196 : else
197 0 : dev = cn_tab->cn_dev;
198 0 : return (ttpoll(dev, rw, p));
199 0 : }
200 :
201 :
202 : int
203 0 : cnkqfilter(dev_t dev, struct knote *kn)
204 : {
205 :
206 : /*
207 : * Redirect output, if that's appropriate.
208 : * If there's no real console, return 1.
209 : */
210 0 : if (constty != NULL)
211 0 : dev = constty->t_dev;
212 0 : else if (cn_tab == NULL)
213 0 : return (ENXIO);
214 : else
215 0 : dev = cn_tab->cn_dev;
216 0 : if (cdevsw[major(dev)].d_kqfilter)
217 0 : return ((*cdevsw[major(dev)].d_kqfilter)(dev, kn));
218 0 : return (ENXIO);
219 0 : }
220 :
221 : int
222 0 : cngetc(void)
223 : {
224 :
225 0 : if (cn_tab == NULL)
226 0 : return (0);
227 0 : return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
228 0 : }
229 :
230 : void
231 0 : cnputc(int c)
232 : {
233 :
234 0 : if (cn_tab == NULL)
235 : return;
236 :
237 0 : if (c) {
238 0 : (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
239 0 : if (c == '\n')
240 0 : (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
241 : }
242 0 : }
243 :
244 : void
245 0 : cnpollc(int on)
246 : {
247 : static int refcount = 0;
248 :
249 0 : if (cn_tab == NULL)
250 : return;
251 0 : if (!on)
252 0 : --refcount;
253 0 : if (refcount == 0)
254 0 : (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
255 0 : if (on)
256 0 : ++refcount;
257 0 : }
258 :
259 : void
260 0 : nullcnpollc(dev_t dev, int on)
261 : {
262 :
263 0 : }
264 :
265 : void
266 0 : cnbell(u_int pitch, u_int period, u_int volume)
267 : {
268 0 : if (cn_tab == NULL || cn_tab->cn_bell == NULL)
269 : return;
270 :
271 0 : (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
272 0 : }
|