xref: /dragonfly/sys/kern/tty_cons.c (revision 8a0bcd56)
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/priv.h>
52 #include <sys/reboot.h>
53 #include <sys/sysctl.h>
54 #include <sys/tty.h>
55 #include <sys/uio.h>
56 #include <sys/msgport.h>
57 #include <sys/msgport2.h>
58 #include <sys/device.h>
59 
60 #include <ddb/ddb.h>
61 
62 #include <machine/cpu.h>
63 
64 static d_open_t cnopen;
65 static d_close_t cnclose;
66 static d_read_t cnread;
67 static d_write_t cnwrite;
68 static d_ioctl_t cnioctl;
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 },
76 	.d_open =	cnopen,
77 	.d_close =	cnclose,
78 	.d_read =	cnread,
79 	.d_write =	cnwrite,
80 	.d_ioctl =	cnioctl,
81 	.d_kqfilter =	cnkqfilter,
82 };
83 
84 static struct dev_ops cn_iops = {
85 	{ "intercept", CDEV_MAJOR, D_TTY },
86 	.d_default =    cnintercept
87 };
88 
89 static struct dev_ops *cn_fwd_ops;
90 static cdev_t	cn_dev;
91 
92 //XXX: get this shit out! (alexh)
93 #if 0
94 SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
95 	&cn_udev, sizeof cn_udev, "T,udev_t", "");
96 #endif
97 
98 static int cn_mute;
99 
100 int	cons_unavail = 0;	/* XXX:
101 				 * physical console not available for
102 				 * input (i.e., it is in graphics mode)
103 				 */
104 
105 static u_char cn_is_open;		/* nonzero if logical console is open */
106 static int openmode, openflag;		/* how /dev/console was openned */
107 static cdev_t cn_devfsdev;		/* represents the device private info */
108 static u_char cn_phys_is_open;		/* nonzero if physical device is open */
109 static u_char console_pausing;		/* pause after each line during probe */
110 static char *console_pausestr=
111 "<pause; press any key to proceed to next line or '.' to end pause mode>";
112 
113 struct consdev *cn_tab;		/* physical console device info */
114 struct consdev *gdb_tab;	/* physical gdb debugger device info */
115 
116 CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
117 SET_DECLARE(cons_set, struct consdev);
118 
119 void
120 cninit(void)
121 {
122 	struct consdev *best_cp, *cp, **list;
123 
124 	/*
125 	 * Workaround for token acquisition and releases during the
126 	 * console init.  For some reason if lwkt_gettoken()'s mpcount
127 	 * optimization is turned off the console init blows up.  It
128 	 * might be trying to kprintf() something in the middle of
129 	 * its init.
130 	 */
131 	lwkt_gettoken(&tty_token);
132 
133 	/*
134 	 * Check if we should mute the console (for security reasons perhaps)
135 	 * It can be changes dynamically using sysctl kern.consmute
136 	 * once we are up and going.
137 	 *
138 	 */
139 	cn_mute = ((boothowto & (RB_MUTE
140 			|RB_SINGLE
141 			|RB_VERBOSE
142 			|RB_ASKNAME
143 			|RB_CONFIG)) == RB_MUTE);
144 
145 	/*
146 	 * Find the first console with the highest priority.
147 	 */
148 	best_cp = NULL;
149 	SET_FOREACH(list, cons_set) {
150 		cp = *list;
151 		if (cp->cn_probe == NULL)
152 			continue;
153 		(*cp->cn_probe)(cp);
154 		if (cp->cn_pri > CN_DEAD && cp->cn_probegood &&
155 		    (best_cp == NULL || cp->cn_pri > best_cp->cn_pri))
156 			best_cp = cp;
157 	}
158 
159 
160 	/*
161 	 * If no console, give up.
162 	 */
163 	if (best_cp == NULL) {
164 		if (cn_tab != NULL && cn_tab->cn_term != NULL)
165 			(*cn_tab->cn_term)(cn_tab);
166 		goto done;
167 	}
168 
169 	/*
170 	 * Initialize console, then attach to it.  This ordering allows
171 	 * debugging using the previous console, if any.
172 	 */
173 	(*best_cp->cn_init)(best_cp);
174 	if (cn_tab != NULL && cn_tab != best_cp) {
175 		/* Turn off the previous console.  */
176 		if (cn_tab->cn_term != NULL)
177 			(*cn_tab->cn_term)(cn_tab);
178 	}
179 	if (boothowto & RB_PAUSE)
180 		console_pausing = 1;
181 done:
182 	cn_tab = best_cp;
183 
184 	/*
185 	 * We can safely release the token after the init is done.
186 	 * Also assert that the mpcount is still correct or otherwise
187 	 * the SMP/AP boot will blow up on us.
188 	 */
189 	lwkt_reltoken(&tty_token);
190 #ifdef SMP
191 	KKASSERT(curthread->td_mpcount == 1);
192 #endif
193 }
194 
195 
196 /*
197  * Hook the open and close functions on the selected device.
198  */
199 void
200 cninit_finish(void)
201 {
202 	if ((cn_tab == NULL) || cn_mute)
203 		return;
204 	if (cn_tab->cn_dev == NULL) {
205 		cn_tab->cn_init_fini(cn_tab);
206 		if (cn_tab->cn_dev == NULL) {
207 			kprintf("Unable to hook console! cn_tab %p\n", cn_tab);
208 			return;
209 		}
210 	}
211 
212 	cn_fwd_ops = dev_ops_intercept(cn_tab->cn_dev, &cn_iops);
213 	cn_dev = cn_tab->cn_dev;
214 	console_pausing = 0;
215 }
216 
217 static void
218 cnuninit(void)
219 {
220 	if (cn_tab == NULL)
221 		return;
222 	if (cn_fwd_ops)
223 		dev_ops_restore(cn_tab->cn_dev, cn_fwd_ops);
224 	cn_fwd_ops = NULL;
225 	cn_dev = NULL;
226 }
227 
228 
229 /*
230  * User has changed the state of the console muting.
231  * This may require us to open or close the device in question.
232  */
233 static int
234 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
235 {
236 	int error;
237 	int ocn_mute;
238 
239 	ocn_mute = cn_mute;
240 	error = sysctl_handle_int(oidp, &cn_mute, 0, req);
241 	if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) {
242 		if(ocn_mute && !cn_mute) {
243 			/*
244 			 * going from muted to unmuted.. open the physical dev
245 			 * if the console has been openned
246 			 */
247 			cninit_finish();
248 			if (cn_is_open) {
249 				/* XXX curproc is not what we want really */
250 				error = dev_dopen(cn_dev, openflag,
251 						openmode, curproc->p_ucred);
252 			}
253 			/* if it failed, back it out */
254 			if ( error != 0) cnuninit();
255 		} else if (!ocn_mute && cn_mute) {
256 			/*
257 			 * going from unmuted to muted.. close the physical dev
258 			 * if it's only open via /dev/console
259 			 */
260 			if (cn_is_open) {
261 				error = dev_dclose(cn_dev, openflag,
262 						   openmode);
263 			}
264 			if (error == 0)
265 				cnuninit();
266 		}
267 		if (error != 0) {
268 			/*
269 	 		 * back out the change if there was an error
270 			 */
271 			cn_mute = ocn_mute;
272 		}
273 	}
274 	return (error);
275 }
276 
277 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
278 	0, sizeof cn_mute, sysctl_kern_consmute, "I", "");
279 
280 
281 /*
282  * We intercept the OPEN and CLOSE calls on the original device, and
283  * forward the rest through.
284  */
285 static int
286 cnintercept(struct dev_generic_args *ap)
287 {
288 	int error;
289 
290 	if (ap->a_desc == &dev_open_desc) {
291 		error = cnopen((struct dev_open_args *)ap);
292 	} else if (ap->a_desc == &dev_close_desc) {
293 		error = cnclose((struct dev_close_args *)ap);
294 	} else if (cn_fwd_ops) {
295 		error = dev_doperate_ops(cn_fwd_ops, ap);
296 	} else {
297 		error = ENXIO;
298 	}
299 	return (error);
300 }
301 
302 /*
303  * cnopen() is called as an intercept function (dev will be that of the
304  * actual physical device representing our console), and also called from
305  * the muting code and from the /dev/console switch (dev will have the
306  * console's cdevsw).
307  */
308 static int
309 cnopen(struct dev_open_args *ap)
310 {
311 	cdev_t dev = ap->a_head.a_dev;
312 	int flag = ap->a_oflags;
313 	int mode = ap->a_devtype;
314 	cdev_t cndev;
315 	cdev_t physdev;
316 	int retval = 0;
317 
318 	if (cn_tab == NULL || cn_fwd_ops == NULL)
319 		return (0);
320 
321 	cndev = cn_tab->cn_dev;		/* actual physical device */
322 	physdev = (dev == cn_devfsdev) ? cndev : dev;
323 
324 	/*
325 	 * If mute is active, then non console opens don't get here
326 	 * so we don't need to check for that. They bypass this and go
327 	 * straight to the device.
328 	 *
329 	 * It is important to note that due to our intercept and the fact
330 	 * that we might be called via the original (intercepted) device,
331 	 * the original device's ops may point to us, so to avoid an
332 	 * infinite recursion we have to forward through cn_fwd_ops.
333 	 * This is a severe hack that really needs to be fixed XXX.
334 	 *
335 	 * XXX at the moment we assume that the port forwarding function
336 	 * is synchronous for open.
337 	 */
338 	if (!cn_mute) {
339 		ap->a_head.a_dev = physdev;
340 		retval = dev_doperate_ops(cn_fwd_ops, &ap->a_head);
341 	}
342 	if (retval == 0) {
343 		/*
344 		 * check if we openned it via /dev/console or
345 		 * via the physical entry (e.g. /dev/sio0).
346 		 */
347 		if (dev == cndev) {
348 			cn_phys_is_open = 1;
349 		} else if (physdev == cndev) {
350 			openmode = mode;
351 			openflag = flag;
352 			cn_is_open = 1;
353 		}
354 		dev->si_tty = cndev->si_tty;
355 	}
356 	return (retval);
357 }
358 
359 /*
360  * cnclose() is called as a port intercept function (dev will be that of the
361  * actual physical device representing our console), and also called from
362  * the muting code and from the /dev/console switch (dev will have the
363  * console's cdevsw).
364  */
365 static int
366 cnclose(struct dev_close_args *ap)
367 {
368 	struct tty *cn_tp;
369 	cdev_t cndev;
370 	cdev_t physdev;
371 	cdev_t dev = ap->a_head.a_dev;
372 
373 	if (cn_tab == NULL || cn_fwd_ops == NULL)
374 		return(0);
375 	cndev = cn_tab->cn_dev;
376 	cn_tp = cndev->si_tty;
377 	physdev = (dev == cn_devfsdev) ? cndev : dev;
378 
379 	/*
380 	 * act appropriatly depending on whether it's /dev/console
381 	 * or the pysical device (e.g. /dev/sio) that's being closed.
382 	 * in either case, don't actually close the device unless
383 	 * both are closed.
384 	 */
385 	if (dev == cndev) {
386 		/* the physical device is about to be closed */
387 		cn_phys_is_open = 0;
388 		if (cn_is_open) {
389 			if (cn_tp) {
390 				/* perform a ttyhalfclose() */
391 				/* reset session and proc group */
392 				ttyclearsession(cn_tp);
393 			}
394 			return(0);
395 		}
396 	} else if (physdev == cndev) {
397 		/* the logical console is about to be closed */
398 		cn_is_open = 0;
399 		if (cn_phys_is_open)
400 			return(0);
401 		dev = cndev;
402 	}
403 	if (cn_fwd_ops) {
404 		ap->a_head.a_dev = dev;
405 		return (dev_doperate_ops(cn_fwd_ops, &ap->a_head));
406 	}
407 	return (0);
408 }
409 
410 /*
411  * The following functions are dispatched solely from the /dev/console
412  * device.  Their job is primarily to forward the request through.
413  * If the console is not attached to anything then write()'s are sunk
414  * to null and reads return 0 (mostly).
415  */
416 static int
417 cnread(struct dev_read_args *ap)
418 {
419 	if (cn_tab == NULL || cn_fwd_ops == NULL)
420 		return (0);
421 	ap->a_head.a_dev = cn_tab->cn_dev;
422 	return (dev_doperate(&ap->a_head));
423 }
424 
425 static int
426 cnwrite(struct dev_write_args *ap)
427 {
428 	struct uio *uio = ap->a_uio;
429 	cdev_t dev;
430 
431 	if (cn_tab == NULL || cn_fwd_ops == NULL) {
432 		uio->uio_resid = 0; /* dump the data */
433 		return (0);
434 	}
435 	if (constty)
436 		dev = constty->t_dev;
437 	else
438 		dev = cn_tab->cn_dev;
439 	log_console(uio);
440 	ap->a_head.a_dev = dev;
441 	return (dev_doperate(&ap->a_head));
442 }
443 
444 static int
445 cnioctl(struct dev_ioctl_args *ap)
446 {
447 	int error;
448 
449 	if (cn_tab == NULL || cn_fwd_ops == NULL)
450 		return (0);
451 	KKASSERT(curproc != NULL);
452 	/*
453 	 * Superuser can always use this to wrest control of console
454 	 * output from the "virtual" console.
455 	 */
456 	if (ap->a_cmd == TIOCCONS && constty) {
457 		if (ap->a_cred) {
458 			error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0);
459 			if (error)
460 				return (error);
461 		}
462 		constty = NULL;
463 		return (0);
464 	}
465 	ap->a_head.a_dev = cn_tab->cn_dev;
466 	return (dev_doperate(&ap->a_head));
467 }
468 
469 static int
470 cnkqfilter(struct dev_kqfilter_args *ap)
471 {
472 	if ((cn_tab == NULL) || cn_mute || cn_fwd_ops == NULL)
473 		return (1);
474 	ap->a_head.a_dev = cn_tab->cn_dev;
475 	return (dev_doperate(&ap->a_head));
476 }
477 
478 /*
479  * These synchronous functions are primarily used the kernel needs to
480  * access the keyboard (e.g. when running the debugger), or output data
481  * directly to the console.
482  */
483 int
484 cngetc(void)
485 {
486 	int c;
487 	if ((cn_tab == NULL) || cn_mute)
488 		return (-1);
489 	c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
490 	if (c == '\r') c = '\n'; /* console input is always ICRNL */
491 	return (c);
492 }
493 
494 int
495 cncheckc(void)
496 {
497 	if ((cn_tab == NULL) || cn_mute)
498 		return (-1);
499 	return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
500 }
501 
502 void
503 cnputc(int c)
504 {
505 	char *cp;
506 
507 	if ((cn_tab == NULL) || cn_mute)
508 		return;
509 	if (c) {
510 		if (c == '\n')
511 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
512 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
513 #ifdef DDB
514 		if (console_pausing && !db_active && (c == '\n')) {
515 #else
516 		if (console_pausing && (c == '\n')) {
517 #endif
518 			for(cp=console_pausestr; *cp != '\0'; cp++)
519 			    (*cn_tab->cn_putc)(cn_tab->cn_dev, *cp);
520 			if (cngetc() == '.')
521 				console_pausing = 0;
522 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
523 			for(cp=console_pausestr; *cp != '\0'; cp++)
524 			    (*cn_tab->cn_putc)(cn_tab->cn_dev, ' ');
525 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
526 		}
527 	}
528 }
529 
530 void
531 cndbctl(int on)
532 {
533 	static int refcount;
534 
535 	if (cn_tab == NULL)
536 		return;
537 	if (!on)
538 		refcount--;
539 	if (refcount == 0 && cn_tab->cn_dbctl != NULL)
540 		(*cn_tab->cn_dbctl)(cn_tab->cn_dev, on);
541 	if (on)
542 		refcount++;
543 }
544 
545 static void
546 cn_drvinit(void *unused)
547 {
548 	cn_devfsdev = make_only_devfs_dev(&cn_ops, 0, UID_ROOT, GID_WHEEL,
549 					  0600, "console");
550 }
551 
552 SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL)
553