cons.c revision 1.78 1 /* $NetBSD: cons.c,v 1.78 2022/08/22 00:20:45 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * the Systems Programming Group of the University of Utah Computer
10 * Science Department.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: Utah $Hdr: cons.c 1.7 92/01/21$
37 *
38 * @(#)cons.c 8.2 (Berkeley) 1/12/94
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.78 2022/08/22 00:20:45 riastradh Exp $");
43
44 #include <sys/param.h>
45 #include <sys/proc.h>
46 #include <sys/systm.h>
47 #include <sys/buf.h>
48 #include <sys/ioctl.h>
49 #include <sys/poll.h>
50 #include <sys/tty.h>
51 #include <sys/file.h>
52 #include <sys/conf.h>
53 #include <sys/vnode.h>
54 #include <sys/kauth.h>
55 #include <sys/mutex.h>
56
57 #include <dev/cons.h>
58
59 #include "nullcons.h"
60
61 dev_type_open(cnopen);
62 dev_type_close(cnclose);
63 dev_type_read(cnread);
64 dev_type_write(cnwrite);
65 dev_type_ioctl(cnioctl);
66 dev_type_poll(cnpoll);
67 dev_type_kqfilter(cnkqfilter);
68
69 static bool cn_redirect(dev_t *, int, int *);
70
71 const struct cdevsw cons_cdevsw = {
72 .d_open = cnopen,
73 .d_close = cnclose,
74 .d_read = cnread,
75 .d_write = cnwrite,
76 .d_ioctl = cnioctl,
77 .d_stop = nostop,
78 .d_tty = notty,
79 .d_poll = cnpoll,
80 .d_mmap = nommap,
81 .d_kqfilter = cnkqfilter,
82 .d_discard = nodiscard,
83 .d_flag = D_TTY
84 };
85
86 struct tty *constty = NULL; /* virtual console output device */
87 struct consdev *cn_tab; /* physical console device info */
88 struct vnode *cn_devvp[2]; /* vnode for underlying device. */
89
90 int
91 cnopen(dev_t dev, int flag, int mode, struct lwp *l)
92 {
93 dev_t cndev;
94 int unit, error;
95
96 unit = minor(dev);
97 if (unit > 1)
98 return ENODEV;
99
100 if (cn_tab == NULL)
101 return (0);
102
103 /*
104 * always open the 'real' console device, so we don't get nailed
105 * later. This follows normal device semantics; they always get
106 * open() calls.
107 */
108 cndev = cn_tab->cn_dev;
109 #if NNULLCONS > 0
110 if (cndev == NODEV) {
111 nullconsattach(0);
112 }
113 #else /* NNULLCONS > 0 */
114 if (cndev == NODEV) {
115 /*
116 * This is most likely an error in the console attach
117 * code. Panicking looks better than jumping into nowhere
118 * through cdevsw below....
119 */
120 panic("cnopen: no console device");
121 }
122 #endif /* NNULLCONS > 0 */
123 if (dev == cndev) {
124 /*
125 * This causes cnopen() to be called recursively, which
126 * is generally a bad thing. It is often caused when
127 * dev == 0 and cn_dev has not been set, but was probably
128 * initialised to 0.
129 */
130 panic("cnopen: cn_tab->cn_dev == dev");
131 }
132 if (cn_devvp[unit] != NULLVP)
133 return 0;
134 if ((error = cdevvp(cndev, &cn_devvp[unit])) != 0)
135 printf("cnopen: unable to get vnode reference\n");
136 vn_lock(cn_devvp[unit], LK_EXCLUSIVE | LK_RETRY);
137 error = VOP_OPEN(cn_devvp[unit], flag, kauth_cred_get());
138 VOP_UNLOCK(cn_devvp[unit]);
139 return error;
140 }
141
142 int
143 cnclose(dev_t dev, int flag, int mode, struct lwp *l)
144 {
145 struct vnode *vp;
146 int unit, error;
147
148 unit = minor(dev);
149
150 if (cn_tab == NULL)
151 return (0);
152
153 vp = cn_devvp[unit];
154 cn_devvp[unit] = NULL;
155 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
156 error = VOP_CLOSE(vp, flag, kauth_cred_get());
157 VOP_UNLOCK(vp);
158 vrele(vp);
159 return error;
160 }
161
162 int
163 cnread(dev_t dev, struct uio *uio, int flag)
164 {
165 int error;
166
167 /*
168 * If we would redirect input, punt. This will keep strange
169 * things from happening to people who are using the real
170 * console. Nothing should be using /dev/console for
171 * input (except a shell in single-user mode, but then,
172 * one wouldn't TIOCCONS then).
173 */
174 if (!cn_redirect(&dev, 1, &error))
175 return error;
176 return cdev_read(dev, uio, flag);
177 }
178
179 int
180 cnwrite(dev_t dev, struct uio *uio, int flag)
181 {
182 int error;
183
184 /* Redirect output, if that's appropriate. */
185 if (!cn_redirect(&dev, 0, &error))
186 return error;
187 return cdev_write(dev, uio, flag);
188 }
189
190 int
191 cnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
192 {
193 int error;
194
195 error = 0;
196
197 /*
198 * Superuser can always use this to wrest control of console
199 * output from the "virtual" console.
200 */
201 if (cmd == TIOCCONS && constty != NULL) {
202 error = kauth_authorize_device_tty(l->l_cred,
203 KAUTH_DEVICE_TTY_VIRTUAL, constty);
204 if (!error)
205 constty = NULL;
206 return (error);
207 }
208
209 /*
210 * Redirect the ioctl, if that's appropriate.
211 * Note that strange things can happen, if a program does
212 * ioctls on /dev/console, then the console is redirected
213 * out from under it.
214 */
215 if (!cn_redirect(&dev, 0, &error))
216 return error;
217 return cdev_ioctl(dev, cmd, data, flag, l);
218 }
219
220 /*ARGSUSED*/
221 int
222 cnpoll(dev_t dev, int events, struct lwp *l)
223 {
224 int error;
225
226 /*
227 * Redirect the poll, if that's appropriate.
228 * I don't want to think of the possible side effects
229 * of console redirection here.
230 */
231 if (!cn_redirect(&dev, 0, &error))
232 return POLLHUP;
233 return cdev_poll(dev, events, l);
234 }
235
236 /*ARGSUSED*/
237 int
238 cnkqfilter(dev_t dev, struct knote *kn)
239 {
240 int error;
241
242 /*
243 * Redirect the kqfilter, if that's appropriate.
244 * I don't want to think of the possible side effects
245 * of console redirection here.
246 */
247 if (!cn_redirect(&dev, 0, &error))
248 return error;
249 return cdev_kqfilter(dev, kn);
250 }
251
252 int
253 cngetc(void)
254 {
255 if (cn_tab == NULL)
256 return (0);
257 int s = splhigh();
258 for (;;) {
259 const int rv = (*cn_tab->cn_getc)(cn_tab->cn_dev);
260 if (rv >= 0) {
261 splx(s);
262 return rv;
263 }
264 docritpollhooks();
265 }
266 }
267
268 int
269 cngetsn(char *cp, int size)
270 {
271 char *lp;
272 int c, len;
273
274 cnpollc(1);
275
276 lp = cp;
277 len = 0;
278 for (;;) {
279 c = cngetc();
280 switch (c) {
281 case '\n':
282 case '\r':
283 printf("\n");
284 *lp++ = '\0';
285 cnpollc(0);
286 return (len);
287 case '\b':
288 case '\177':
289 case '#':
290 if (len) {
291 --len;
292 --lp;
293 printf("\b \b");
294 }
295 continue;
296 case '@':
297 case 'u'&037: /* CTRL-u */
298 len = 0;
299 lp = cp;
300 printf("\n");
301 continue;
302 default:
303 if (len + 1 >= size || c < ' ') {
304 printf("\007");
305 continue;
306 }
307 printf("%c", c);
308 ++len;
309 *lp++ = c;
310 }
311 }
312 }
313
314 void
315 cnputc(int c)
316 {
317
318 if (cn_tab == NULL)
319 return;
320
321 /*
322 * XXX
323 * for some reason this causes ARCS firmware to output an endless stream of
324 * whitespaces with n32 kernels, so use the pre-1.74 code for now until I can
325 * figure out why this happens
326 */
327 #ifndef sgimips
328 if (c) {
329 if (c == '\n') {
330 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
331 docritpollhooks();
332 }
333 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
334 }
335 #else
336 if (c) {
337 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
338 if (c == '\n') {
339 docritpollhooks();
340 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
341 }
342 }
343 #endif
344 }
345
346 void
347 cnpollc(int on)
348 {
349 static int refcount = 0;
350
351 if (cn_tab == NULL)
352 return;
353 if (!on)
354 --refcount;
355 if (refcount == 0)
356 (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
357 if (on)
358 ++refcount;
359 }
360
361 void
362 nullcnpollc(dev_t dev, int on)
363 {
364
365 }
366
367 void
368 cnbell(u_int pitch, u_int period, u_int volume)
369 {
370
371 if (cn_tab == NULL || cn_tab->cn_bell == NULL)
372 return;
373 (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
374 }
375
376 void
377 cnflush(void)
378 {
379 if (cn_tab == NULL || cn_tab->cn_flush == NULL)
380 return;
381 (*cn_tab->cn_flush)(cn_tab->cn_dev);
382 }
383
384 void
385 cnhalt(void)
386 {
387 if (cn_tab == NULL || cn_tab->cn_halt == NULL)
388 return;
389 (*cn_tab->cn_halt)(cn_tab->cn_dev);
390 }
391
392 /*
393 * Redirect output, if that's appropriate. If there's no real console,
394 * return ENXIO.
395 *
396 * Call with tty_mutex held.
397 */
398 static bool
399 cn_redirect(dev_t *devp, int is_read, int *error)
400 {
401 dev_t dev = *devp;
402
403 *error = ENXIO;
404 if (constty != NULL && minor(dev) == 0 &&
405 (cn_tab == NULL || (cn_tab->cn_pri != CN_REMOTE))) {
406 if (is_read) {
407 *error = 0;
408 return false;
409 }
410 dev = constty->t_dev;
411 } else if (cn_tab == NULL)
412 return false;
413 else
414 dev = cn_tab->cn_dev;
415 *devp = dev;
416 return true;
417 }
418