xref: /dragonfly/sys/kern/tty_cons.c (revision 6b5c5d0d)
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1991 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	from: @(#)cons.c	7.2 (Berkeley) 5/9/91
39  * $FreeBSD: src/sys/kern/tty_cons.c,v 1.81.2.4 2001/12/17 18:44:41 guido Exp $
40  * $DragonFly: src/sys/kern/tty_cons.c,v 1.21 2007/05/07 05:21:41 dillon Exp $
41  */
42 
43 #include "opt_ddb.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/conf.h>
48 #include <sys/cons.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/reboot.h>
52 #include <sys/sysctl.h>
53 #include <sys/tty.h>
54 #include <sys/uio.h>
55 #include <sys/msgport.h>
56 #include <sys/msgport2.h>
57 #include <sys/device.h>
58 
59 #include <ddb/ddb.h>
60 
61 #include <machine/cpu.h>
62 
63 static d_open_t cnopen;
64 static d_close_t cnclose;
65 static d_read_t cnread;
66 static d_write_t cnwrite;
67 static d_ioctl_t cnioctl;
68 static d_poll_t cnpoll;
69 static d_kqfilter_t cnkqfilter;
70 
71 static int cnintercept(struct dev_generic_args *ap);
72 
73 #define	CDEV_MAJOR	0
74 static struct dev_ops cn_ops = {
75 	{ "console", CDEV_MAJOR, D_TTY | D_KQFILTER },
76 	.d_open =	cnopen,
77 	.d_close =	cnclose,
78 	.d_read =	cnread,
79 	.d_write =	cnwrite,
80 	.d_ioctl =	cnioctl,
81 	.d_poll =	cnpoll,
82 	.d_kqfilter =	cnkqfilter,
83 };
84 
85 static struct dev_ops cn_iops = {
86 	{ "intercept", CDEV_MAJOR, D_TTY | D_KQFILTER },
87 	.d_default =	cnintercept
88 };
89 
90 static struct dev_ops *cn_fwd_ops;
91 static cdev_t	cn_dev;
92 static udev_t	cn_udev;
93 SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
94 	&cn_udev, sizeof cn_udev, "T,udev_t", "");
95 
96 static int cn_mute;
97 
98 int	cons_unavail = 0;	/* XXX:
99 				 * physical console not available for
100 				 * input (i.e., it is in graphics mode)
101 				 */
102 
103 static u_char cn_is_open;		/* nonzero if logical console is open */
104 static int openmode, openflag;		/* how /dev/console was openned */
105 static cdev_t cn_devfsdev;		/* represents the device private info */
106 static u_char cn_phys_is_open;		/* nonzero if physical device is open */
107 static u_char console_pausing;		/* pause after each line during probe */
108 static char *console_pausestr=
109 "<pause; press any key to proceed to next line or '.' to end pause mode>";
110 
111 struct consdev *cn_tab;		/* physical console device info */
112 struct consdev *gdb_tab;	/* physical gdb debugger device info */
113 
114 CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
115 SET_DECLARE(cons_set, struct consdev);
116 
117 void
118 cninit(void)
119 {
120 	struct consdev *best_cp, *cp, **list;
121 
122 	/*
123 	 * Find the first console with the highest priority.
124 	 */
125 	best_cp = NULL;
126 	SET_FOREACH(list, cons_set) {
127 		cp = *list;
128 		if (cp->cn_probe == NULL)
129 			continue;
130 		(*cp->cn_probe)(cp);
131 		if (cp->cn_pri > CN_DEAD && cp->cn_probegood &&
132 		    (best_cp == NULL || cp->cn_pri > best_cp->cn_pri))
133 			best_cp = cp;
134 	}
135 
136 	/*
137 	 * Check if we should mute the console (for security reasons perhaps)
138 	 * It can be changes dynamically using sysctl kern.consmute
139 	 * once we are up and going.
140 	 *
141 	 */
142         cn_mute = ((boothowto & (RB_MUTE
143 			|RB_SINGLE
144 			|RB_VERBOSE
145 			|RB_ASKNAME
146 			|RB_CONFIG)) == RB_MUTE);
147 
148 	/*
149 	 * If no console, give up.
150 	 */
151 	if (best_cp == NULL) {
152 		if (cn_tab != NULL && cn_tab->cn_term != NULL)
153 			(*cn_tab->cn_term)(cn_tab);
154 		cn_tab = best_cp;
155 		return;
156 	}
157 
158 	/*
159 	 * Initialize console, then attach to it.  This ordering allows
160 	 * debugging using the previous console, if any.
161 	 */
162 	(*best_cp->cn_init)(best_cp);
163 	if (cn_tab != NULL && cn_tab != best_cp) {
164 		/* Turn off the previous console.  */
165 		if (cn_tab->cn_term != NULL)
166 			(*cn_tab->cn_term)(cn_tab);
167 	}
168 	if (boothowto & RB_PAUSE)
169 		console_pausing = 1;
170 	cn_tab = best_cp;
171 }
172 
173 /*
174  * Hook the open and close functions on the selected device.
175  */
176 void
177 cninit_finish(void)
178 {
179 	if ((cn_tab == NULL) || cn_mute)
180 		return;
181 
182 	if (cn_tab->cn_dev == NULL) {
183 		cn_tab->cn_init_fini(cn_tab);
184 	}
185 	if (cn_tab->cn_dev == NULL) {
186 		kprintf("Unable to hook console open and close! cn_tab %p\n",
187 			cn_tab);
188 		return;
189 	}
190 
191 	/*
192 	 * Hook the open and close functions.  XXX bad hack.
193 	 */
194 	if (dev_is_good(cn_tab->cn_dev)) {
195 		cn_fwd_ops = dev_ops_intercept(cn_tab->cn_dev, &cn_iops);
196 	}
197 	cn_dev = cn_tab->cn_dev;
198 	cn_udev = dev2udev(cn_dev);
199 	console_pausing = 0;
200 }
201 
202 static void
203 cnuninit(void)
204 {
205 	if (cn_tab == NULL)
206 		return;
207 
208 	/*
209 	 * Unhook the open and close functions.  XXX bad hack
210 	 */
211 	if (cn_fwd_ops)
212 		dev_ops_restore(cn_tab->cn_dev, cn_fwd_ops);
213 	cn_fwd_ops = NULL;
214 	cn_dev = NULL;
215 	cn_udev = NOUDEV;
216 }
217 
218 /*
219  * User has changed the state of the console muting.
220  * This may require us to open or close the device in question.
221  */
222 static int
223 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
224 {
225 	int error;
226 	int ocn_mute;
227 
228 	ocn_mute = cn_mute;
229 	error = sysctl_handle_int(oidp, &cn_mute, 0, req);
230 	if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) {
231 		if(ocn_mute && !cn_mute) {
232 			/*
233 			 * going from muted to unmuted.. open the physical dev
234 			 * if the console has been openned
235 			 */
236 			cninit_finish();
237 			if (cn_is_open) {
238 				/* XXX curproc is not what we want really */
239 				error = dev_dopen(cn_dev, openflag,
240 						openmode, curproc->p_ucred);
241 			}
242 			/* if it failed, back it out */
243 			if ( error != 0) cnuninit();
244 		} else if (!ocn_mute && cn_mute) {
245 			/*
246 			 * going from unmuted to muted.. close the physical dev
247 			 * if it's only open via /dev/console
248 			 */
249 			if (cn_is_open) {
250 				error = dev_dclose(cn_dev, openflag,
251 						openmode);
252 			}
253 			if (error == 0)
254 				cnuninit();
255 		}
256 		if (error != 0) {
257 			/*
258 	 		 * back out the change if there was an error
259 			 */
260 			cn_mute = ocn_mute;
261 		}
262 	}
263 	return (error);
264 }
265 
266 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
267 	0, sizeof cn_mute, sysctl_kern_consmute, "I", "");
268 
269 /*
270  * We intercept the OPEN and CLOSE calls on the original device, and
271  * forward the rest through.
272  */
273 static int
274 cnintercept(struct dev_generic_args *ap)
275 {
276 	int error;
277 
278 	if (ap->a_desc == &dev_open_desc) {
279 		error = cnopen((struct dev_open_args *)ap);
280 	} else if (ap->a_desc == &dev_close_desc) {
281 		error = cnclose((struct dev_close_args *)ap);
282 	} else if (cn_fwd_ops) {
283 		error = dev_doperate_ops(cn_fwd_ops, ap);
284 	} else {
285 		error = ENXIO;
286 	}
287 	return (error);
288 }
289 
290 /*
291  * cnopen() is called as an intercept function (dev will be that of the
292  * actual physical device representing our console), and also called from
293  * the muting code and from the /dev/console switch (dev will have the
294  * console's cdevsw).
295  */
296 static int
297 cnopen(struct dev_open_args *ap)
298 {
299 	cdev_t dev = ap->a_head.a_dev;
300 	int flag = ap->a_oflags;
301 	int mode = ap->a_devtype;
302 	cdev_t cndev, physdev;
303 	int retval = 0;
304 
305 	if (cn_tab == NULL || cn_fwd_ops == NULL)
306 		return (0);
307 	cndev = cn_tab->cn_dev;
308 	physdev = (major(dev) == major(cndev) ? dev : cndev);
309 
310 	/*
311 	 * If mute is active, then non console opens don't get here
312 	 * so we don't need to check for that. They bypass this and go
313 	 * straight to the device.
314 	 *
315 	 * It is important to note that due to our intercept and the fact
316 	 * that we might be called via the original (intercepted) device,
317 	 * the original device's ops may point to us, so to avoid an
318 	 * infinite recursion we have to forward through cn_fwd_ops.
319 	 * This is a severe hack that really needs to be fixed XXX.
320 	 *
321 	 * XXX at the moment we assume that the port forwarding function
322 	 * is synchronous for open.
323 	 */
324 	if (!cn_mute) {
325 		ap->a_head.a_dev = physdev;
326 		retval = dev_doperate_ops(cn_fwd_ops, &ap->a_head);
327 	}
328 	if (retval == 0) {
329 		/*
330 		 * check if we openned it via /dev/console or
331 		 * via the physical entry (e.g. /dev/sio0).
332 		 */
333 		if (dev == cndev)
334 			cn_phys_is_open = 1;
335 		else if (physdev == cndev) {
336 			openmode = mode;
337 			openflag = flag;
338 			cn_is_open = 1;
339 		}
340 		dev->si_tty = physdev->si_tty;
341 	}
342 	return (retval);
343 }
344 
345 /*
346  * cnclose() is called as a port intercept function (dev will be that of the
347  * actual physical device representing our console), and also called from
348  * the muting code and from the /dev/console switch (dev will have the
349  * console's cdevsw).
350  */
351 static int
352 cnclose(struct dev_close_args *ap)
353 {
354 	cdev_t dev = ap->a_head.a_dev;
355 	cdev_t cndev;
356 	struct tty *cn_tp;
357 
358 	if (cn_tab == NULL || cn_fwd_ops == NULL)
359 		return (0);
360 	cndev = cn_tab->cn_dev;
361 	cn_tp = cndev->si_tty;
362 	/*
363 	 * act appropriatly depending on whether it's /dev/console
364 	 * or the pysical device (e.g. /dev/sio) that's being closed.
365 	 * in either case, don't actually close the device unless
366 	 * both are closed.
367 	 */
368 	if (dev == cndev) {
369 		/* the physical device is about to be closed */
370 		cn_phys_is_open = 0;
371 		if (cn_is_open) {
372 			if (cn_tp) {
373 				/* perform a ttyhalfclose() */
374 				/* reset session and proc group */
375 				ttyclearsession(cn_tp);
376 			}
377 			return (0);
378 		}
379 	} else if (major(dev) != major(cndev)) {
380 		/* the logical console is about to be closed */
381 		cn_is_open = 0;
382 		if (cn_phys_is_open)
383 			return (0);
384 		dev = cndev;
385 	}
386 	if (cn_fwd_ops) {
387 		ap->a_head.a_dev = dev;
388 		return (dev_doperate_ops(cn_fwd_ops, &ap->a_head));
389 	}
390 	return (0);
391 }
392 
393 /*
394  * The following functions are dispatched solely from the /dev/console
395  * device.  Their job is primarily to forward the request through.
396  * If the console is not attached to anything then write()'s are sunk
397  * to null and reads return 0 (mostly).
398  */
399 static int
400 cnread(struct dev_read_args *ap)
401 {
402 	if (cn_tab == NULL || cn_fwd_ops == NULL)
403 		return (0);
404 	ap->a_head.a_dev = cn_tab->cn_dev;
405 	return (dev_doperate(&ap->a_head));
406 }
407 
408 static int
409 cnwrite(struct dev_write_args *ap)
410 {
411 	struct uio *uio = ap->a_uio;
412 	cdev_t dev;
413 
414 	if (cn_tab == NULL || cn_fwd_ops == NULL) {
415 		uio->uio_resid = 0; /* dump the data */
416 		return (0);
417 	}
418 	if (constty)
419 		dev = constty->t_dev;
420 	else
421 		dev = cn_tab->cn_dev;
422 	log_console(uio);
423 	ap->a_head.a_dev = dev;
424 	return (dev_doperate(&ap->a_head));
425 }
426 
427 static int
428 cnioctl(struct dev_ioctl_args *ap)
429 {
430 	int error;
431 
432 	if (cn_tab == NULL || cn_fwd_ops == NULL)
433 		return (0);
434 	KKASSERT(curproc != NULL);
435 	/*
436 	 * Superuser can always use this to wrest control of console
437 	 * output from the "virtual" console.
438 	 */
439 	if (ap->a_cmd == TIOCCONS && constty) {
440 		if (ap->a_cred) {
441 			error = suser_cred(ap->a_cred, 0);
442 			if (error)
443 				return (error);
444 		}
445 		constty = NULL;
446 		return (0);
447 	}
448 	ap->a_head.a_dev = cn_tab->cn_dev;
449 	return (dev_doperate(&ap->a_head));
450 }
451 
452 static int
453 cnpoll(struct dev_poll_args *ap)
454 {
455 	if ((cn_tab == NULL) || cn_mute || cn_fwd_ops == NULL)
456 		return (1);
457 	ap->a_head.a_dev = cn_tab->cn_dev;
458 	return (dev_doperate(&ap->a_head));
459 }
460 
461 static int
462 cnkqfilter(struct dev_kqfilter_args *ap)
463 {
464 	if ((cn_tab == NULL) || cn_mute || cn_fwd_ops == NULL)
465 		return (1);
466 	ap->a_head.a_dev = cn_tab->cn_dev;
467 	return (dev_doperate(&ap->a_head));
468 }
469 
470 /*
471  * These synchronous functions are primarily used the kernel needs to
472  * access the keyboard (e.g. when running the debugger), or output data
473  * directly to the console.
474  */
475 int
476 cngetc(void)
477 {
478 	int c;
479 	if ((cn_tab == NULL) || cn_mute)
480 		return (-1);
481 	c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
482 	if (c == '\r') c = '\n'; /* console input is always ICRNL */
483 	return (c);
484 }
485 
486 int
487 cncheckc(void)
488 {
489 	if ((cn_tab == NULL) || cn_mute)
490 		return (-1);
491 	return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
492 }
493 
494 void
495 cnputc(int c)
496 {
497 	char *cp;
498 
499 	if ((cn_tab == NULL) || cn_mute)
500 		return;
501 	if (c) {
502 		if (c == '\n')
503 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
504 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
505 #ifdef DDB
506 		if (console_pausing && !db_active && (c == '\n')) {
507 #else
508 		if (console_pausing && (c == '\n')) {
509 #endif
510 			for(cp=console_pausestr; *cp != '\0'; cp++)
511 			    (*cn_tab->cn_putc)(cn_tab->cn_dev, *cp);
512 			if (cngetc() == '.')
513 				console_pausing = 0;
514 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
515 			for(cp=console_pausestr; *cp != '\0'; cp++)
516 			    (*cn_tab->cn_putc)(cn_tab->cn_dev, ' ');
517 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
518 		}
519 	}
520 }
521 
522 void
523 cndbctl(int on)
524 {
525 	static int refcount;
526 
527 	if (cn_tab == NULL)
528 		return;
529 	if (!on)
530 		refcount--;
531 	if (refcount == 0 && cn_tab->cn_dbctl != NULL)
532 		(*cn_tab->cn_dbctl)(cn_tab->cn_dev, on);
533 	if (on)
534 		refcount++;
535 }
536 
537 static void
538 cn_drvinit(void *unused)
539 {
540 	dev_ops_add(&cn_ops, 0, 0);
541 	cn_devfsdev = make_dev(&cn_ops, 0, UID_ROOT, GID_WHEEL,
542 				0600, "console");
543 }
544 
545 SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL)
546