cons.c revision 1.81 1 /* $NetBSD: cons.c,v 1.81 2022/10/03 19:12:51 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.81 2022/10/03 19:12:51 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 #include <sys/module.h>
57
58 #include <dev/cons.h>
59
60 #include "nullcons.h"
61
62 dev_type_open(cnopen);
63 dev_type_close(cnclose);
64 dev_type_read(cnread);
65 dev_type_write(cnwrite);
66 dev_type_ioctl(cnioctl);
67 dev_type_poll(cnpoll);
68 dev_type_kqfilter(cnkqfilter);
69
70 static bool cn_redirect(dev_t *, int, int *);
71
72 const struct cdevsw cons_cdevsw = {
73 .d_open = cnopen,
74 .d_close = cnclose,
75 .d_read = cnread,
76 .d_write = cnwrite,
77 .d_ioctl = cnioctl,
78 .d_stop = nostop,
79 .d_tty = notty,
80 .d_poll = cnpoll,
81 .d_mmap = nommap,
82 .d_kqfilter = cnkqfilter,
83 .d_discard = nodiscard,
84 .d_flag = D_TTY
85 };
86
87 static struct kmutex cn_lock;
88
89 struct tty *constty = NULL; /* virtual console output device */
90 struct consdev *cn_tab; /* physical console device info */
91 struct vnode *cn_devvp[2]; /* vnode for underlying device. */
92
93 void
94 cn_set_tab(struct consdev *tab)
95 {
96
97 /*
98 * This is a point that we should have KASSERT(cold) or add
99 * synchronization in case this can happen after cold boot.
100 * However, cn_tab initialization is so critical to any
101 * diagnostics or debugging that we need to tread carefully
102 * about introducing new ways to crash. So let's put the
103 * assertion in only after we've audited most or all of the
104 * cn_tab updates.
105 */
106 cn_tab = tab;
107 }
108
109 int
110 cnopen(dev_t dev, int flag, int mode, struct lwp *l)
111 {
112 dev_t cndev;
113 int unit, error;
114
115 unit = minor(dev);
116 if (unit > 1)
117 return ENODEV;
118
119 mutex_enter(&cn_lock);
120
121 if (cn_tab == NULL) {
122 error = 0;
123 goto out;
124 }
125
126 /*
127 * always open the 'real' console device, so we don't get nailed
128 * later. This follows normal device semantics; they always get
129 * open() calls.
130 */
131 cndev = cn_tab->cn_dev;
132 #if NNULLCONS > 0
133 if (cndev == NODEV) {
134 nullconsattach(0);
135 }
136 #else /* NNULLCONS > 0 */
137 if (cndev == NODEV) {
138 /*
139 * This is most likely an error in the console attach
140 * code. Panicking looks better than jumping into nowhere
141 * through cdevsw below....
142 */
143 panic("cnopen: no console device");
144 }
145 #endif /* NNULLCONS > 0 */
146 if (dev == cndev) {
147 /*
148 * This causes cnopen() to be called recursively, which
149 * is generally a bad thing. It is often caused when
150 * dev == 0 and cn_dev has not been set, but was probably
151 * initialised to 0.
152 */
153 panic("cnopen: cn_tab->cn_dev == dev");
154 }
155 if (cn_devvp[unit] != NULLVP) {
156 error = 0;
157 goto out;
158 }
159 if ((error = cdevvp(cndev, &cn_devvp[unit])) != 0) {
160 printf("cnopen: unable to get vnode reference\n");
161 goto out;
162 }
163 vn_lock(cn_devvp[unit], LK_EXCLUSIVE | LK_RETRY);
164 error = VOP_OPEN(cn_devvp[unit], flag, kauth_cred_get());
165 VOP_UNLOCK(cn_devvp[unit]);
166
167 out: mutex_exit(&cn_lock);
168 return error;
169 }
170
171 int
172 cnclose(dev_t dev, int flag, int mode, struct lwp *l)
173 {
174 struct vnode *vp;
175 int unit, error;
176
177 unit = minor(dev);
178
179 mutex_enter(&cn_lock);
180
181 if (cn_tab == NULL) {
182 error = 0;
183 goto out;
184 }
185
186 vp = cn_devvp[unit];
187 cn_devvp[unit] = NULL;
188 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
189 error = VOP_CLOSE(vp, flag, kauth_cred_get());
190 VOP_UNLOCK(vp);
191 vrele(vp);
192
193 out: mutex_exit(&cn_lock);
194 return error;
195 }
196
197 int
198 cnread(dev_t dev, struct uio *uio, int flag)
199 {
200 int error;
201
202 /*
203 * If we would redirect input, punt. This will keep strange
204 * things from happening to people who are using the real
205 * console. Nothing should be using /dev/console for
206 * input (except a shell in single-user mode, but then,
207 * one wouldn't TIOCCONS then).
208 */
209 if (!cn_redirect(&dev, 1, &error))
210 return error;
211 return cdev_read(dev, uio, flag);
212 }
213
214 int
215 cnwrite(dev_t dev, struct uio *uio, int flag)
216 {
217 int error;
218
219 /* Redirect output, if that's appropriate. */
220 if (!cn_redirect(&dev, 0, &error))
221 return error;
222 return cdev_write(dev, uio, flag);
223 }
224
225 int
226 cnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
227 {
228 int error;
229
230 error = 0;
231
232 /*
233 * Superuser can always use this to wrest control of console
234 * output from the "virtual" console.
235 */
236 if (cmd == TIOCCONS && constty != NULL) {
237 error = kauth_authorize_device_tty(l->l_cred,
238 KAUTH_DEVICE_TTY_VIRTUAL, constty);
239 if (!error)
240 constty = NULL;
241 return (error);
242 }
243
244 /*
245 * Redirect the ioctl, if that's appropriate.
246 * Note that strange things can happen, if a program does
247 * ioctls on /dev/console, then the console is redirected
248 * out from under it.
249 */
250 if (!cn_redirect(&dev, 0, &error))
251 return error;
252 return cdev_ioctl(dev, cmd, data, flag, l);
253 }
254
255 /*ARGSUSED*/
256 int
257 cnpoll(dev_t dev, int events, struct lwp *l)
258 {
259 int error;
260
261 /*
262 * Redirect the poll, if that's appropriate.
263 * I don't want to think of the possible side effects
264 * of console redirection here.
265 */
266 if (!cn_redirect(&dev, 0, &error))
267 return POLLHUP;
268 return cdev_poll(dev, events, l);
269 }
270
271 /*ARGSUSED*/
272 int
273 cnkqfilter(dev_t dev, struct knote *kn)
274 {
275 int error;
276
277 /*
278 * Redirect the kqfilter, if that's appropriate.
279 * I don't want to think of the possible side effects
280 * of console redirection here.
281 */
282 if (!cn_redirect(&dev, 0, &error))
283 return error;
284 return cdev_kqfilter(dev, kn);
285 }
286
287 int
288 cngetc(void)
289 {
290 if (cn_tab == NULL)
291 return (0);
292 int s = splhigh();
293 for (;;) {
294 const int rv = (*cn_tab->cn_getc)(cn_tab->cn_dev);
295 if (rv >= 0) {
296 splx(s);
297 return rv;
298 }
299 docritpollhooks();
300 }
301 }
302
303 int
304 cngetsn(char *cp, int size)
305 {
306 char *lp;
307 int c, len;
308
309 cnpollc(1);
310
311 lp = cp;
312 len = 0;
313 for (;;) {
314 c = cngetc();
315 switch (c) {
316 case '\n':
317 case '\r':
318 printf("\n");
319 *lp++ = '\0';
320 cnpollc(0);
321 return (len);
322 case '\b':
323 case '\177':
324 case '#':
325 if (len) {
326 --len;
327 --lp;
328 printf("\b \b");
329 }
330 continue;
331 case '@':
332 case 'u'&037: /* CTRL-u */
333 len = 0;
334 lp = cp;
335 printf("\n");
336 continue;
337 default:
338 if (len + 1 >= size || c < ' ') {
339 printf("\007");
340 continue;
341 }
342 printf("%c", c);
343 ++len;
344 *lp++ = c;
345 }
346 }
347 }
348
349 void
350 cnputc(int c)
351 {
352
353 if (cn_tab == NULL)
354 return;
355
356 /*
357 * XXX
358 * for some reason this causes ARCS firmware to output an endless stream of
359 * whitespaces with n32 kernels, so use the pre-1.74 code for now until I can
360 * figure out why this happens
361 */
362 #ifndef sgimips
363 if (c) {
364 if (c == '\n') {
365 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
366 docritpollhooks();
367 }
368 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
369 }
370 #else
371 if (c) {
372 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
373 if (c == '\n') {
374 docritpollhooks();
375 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
376 }
377 }
378 #endif
379 }
380
381 void
382 cnpollc(int on)
383 {
384 static int refcount = 0;
385
386 if (cn_tab == NULL)
387 return;
388 if (!on)
389 --refcount;
390 if (refcount == 0)
391 (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
392 if (on)
393 ++refcount;
394 }
395
396 void
397 nullcnpollc(dev_t dev, int on)
398 {
399
400 }
401
402 void
403 cnbell(u_int pitch, u_int period, u_int volume)
404 {
405
406 if (cn_tab == NULL || cn_tab->cn_bell == NULL)
407 return;
408 (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
409 }
410
411 void
412 cnflush(void)
413 {
414 if (cn_tab == NULL || cn_tab->cn_flush == NULL)
415 return;
416 (*cn_tab->cn_flush)(cn_tab->cn_dev);
417 }
418
419 void
420 cnhalt(void)
421 {
422 if (cn_tab == NULL || cn_tab->cn_halt == NULL)
423 return;
424 (*cn_tab->cn_halt)(cn_tab->cn_dev);
425 }
426
427 /*
428 * Redirect output, if that's appropriate. If there's no real console,
429 * return ENXIO.
430 *
431 * Call with tty_mutex held.
432 */
433 static bool
434 cn_redirect(dev_t *devp, int is_read, int *error)
435 {
436 dev_t dev = *devp;
437
438 *error = ENXIO;
439 if (constty != NULL && minor(dev) == 0 &&
440 (cn_tab == NULL || (cn_tab->cn_pri != CN_REMOTE))) {
441 if (is_read) {
442 *error = 0;
443 return false;
444 }
445 dev = constty->t_dev;
446 } else if (cn_tab == NULL)
447 return false;
448 else
449 dev = cn_tab->cn_dev;
450 *devp = dev;
451 return true;
452 }
453
454 MODULE(MODULE_CLASS_DRIVER, cons, NULL);
455
456 static int
457 cons_modcmd(modcmd_t cmd, void *arg)
458 {
459
460 switch (cmd) {
461 case MODULE_CMD_INIT:
462 mutex_init(&cn_lock, MUTEX_DEFAULT, IPL_NONE);
463 return 0;
464 case MODULE_CMD_FINI:
465 mutex_destroy(&cn_lock);
466 return 0;
467 default:
468 return ENOTTY;
469 }
470 }
471