kern_cons.c revision 49049
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 *	$Id: cons.c,v 1.69 1999/06/26 12:19:03 peter Exp $
40 */
41
42#include "opt_devfs.h"
43
44#include <sys/param.h>
45#ifdef DEVFS
46#include <sys/devfsext.h>
47#endif /*DEVFS*/
48#include <sys/systm.h>
49#include <sys/conf.h>
50#include <sys/kernel.h>
51#include <sys/reboot.h>
52#include <sys/sysctl.h>
53#include <sys/proc.h>
54#include <sys/tty.h>
55#include <sys/uio.h>
56
57#include <machine/cpu.h>
58#include <machine/cons.h>
59
60static	d_open_t	cnopen;
61static	d_close_t	cnclose;
62static	d_read_t	cnread;
63static	d_write_t	cnwrite;
64static	d_ioctl_t	cnioctl;
65static	d_poll_t	cnpoll;
66
67#define	CDEV_MAJOR	0
68static struct cdevsw cn_cdevsw = {
69	/* open */	cnopen,
70	/* close */	cnclose,
71	/* read */	cnread,
72	/* write */	cnwrite,
73	/* ioctl */	cnioctl,
74	/* stop */	nostop,
75	/* reset */	noreset,
76	/* devtotty */	nodevtotty,
77	/* poll */	cnpoll,
78	/* mmap */	nommap,
79	/* strategy */	nostrategy,
80	/* name */	"console",
81	/* parms */	noparms,
82	/* maj */	CDEV_MAJOR,
83	/* dump */	nodump,
84	/* psize */	nopsize,
85	/* flags */	D_TTY,
86	/* maxio */	0,
87	/* bmaj */	-1
88};
89
90static dev_t	cn_dev_t; 	/* seems to be never really used */
91static udev_t	cn_udev_t;
92SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
93	&cn_udev_t, sizeof cn_udev_t, "T,dev_t", "");
94
95static int cn_mute;
96
97int	cons_unavail = 0;	/* XXX:
98				 * physical console not available for
99				 * input (i.e., it is in graphics mode)
100				 */
101
102static u_char cn_is_open;		/* nonzero if logical console is open */
103static int openmode, openflag;		/* how /dev/console was openned */
104static u_char cn_phys_is_open;		/* nonzero if physical device is open */
105static d_close_t *cn_phys_close;	/* physical device close function */
106static d_open_t *cn_phys_open;		/* physical device open function */
107static struct consdev *cn_tab;		/* physical console device info */
108static struct tty *cn_tp;		/* physical console tty struct */
109#ifdef DEVFS
110static void *cn_devfs_token;		/* represents the devfs entry */
111#endif /* DEVFS */
112
113CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL);
114
115void
116cninit()
117{
118	struct consdev **list, *best_cp, *cp;
119
120	/*
121	 * Find the first console with the highest priority.
122	 */
123	best_cp = NULL;
124	list = (struct consdev **)cons_set.ls_items;
125	while ((cp = *list++) != NULL) {
126		if (cp->cn_probe == NULL)
127			continue;
128		(*cp->cn_probe)(cp);
129		if (cp->cn_pri > CN_DEAD &&
130		    (best_cp == NULL || cp->cn_pri > best_cp->cn_pri))
131			best_cp = cp;
132	}
133
134	/*
135	 * Check if we should mute the console (for security reasons perhaps)
136	 * It can be changes dynamically using sysctl kern.consmute
137	 * once we are up and going.
138	 *
139	 */
140        cn_mute = ((boothowto & (RB_MUTE
141			|RB_SINGLE
142			|RB_VERBOSE
143			|RB_ASKNAME
144			|RB_CONFIG)) == RB_MUTE);
145
146	/*
147	 * If no console, give up.
148	 */
149	if (best_cp == NULL) {
150		if (cn_tab != NULL && cn_tab->cn_term != NULL)
151			(*cn_tab->cn_term)(cn_tab);
152		cn_tab = best_cp;
153		return;
154	}
155
156	/*
157	 * Initialize console, then attach to it.  This ordering allows
158	 * debugging using the previous console, if any.
159	 */
160	(*best_cp->cn_init)(best_cp);
161	if (cn_tab != NULL && cn_tab != best_cp) {
162		/* Turn off the previous console.  */
163		if (cn_tab->cn_term != NULL)
164			(*cn_tab->cn_term)(cn_tab);
165	}
166	cn_tab = best_cp;
167}
168
169void
170cninit_finish()
171{
172	struct cdevsw *cdp;
173
174	if ((cn_tab == NULL) || cn_mute)
175		return;
176
177	/*
178	 * Hook the open and close functions.
179	 */
180	cdp = devsw(cn_tab->cn_dev);
181	cn_phys_close = cdp->d_close;
182	cdp->d_close = cnclose;
183	cn_phys_open = cdp->d_open;
184	cdp->d_open = cnopen;
185	cn_tp = (*cdp->d_devtotty)(cn_tab->cn_dev);
186	cn_dev_t = cn_tab->cn_dev;
187	cn_udev_t = dev2udev(cn_dev_t);
188}
189
190static void
191cnuninit(void)
192{
193	struct cdevsw *cdp;
194
195	if (cn_tab == NULL)
196		return;
197
198	/*
199	 * Unhook the open and close functions.
200	 */
201	cdp = devsw(cn_tab->cn_dev);
202	cdp->d_close = cn_phys_close;
203	cn_phys_close = NULL;
204	cdp->d_open = cn_phys_open;
205	cn_phys_open = NULL;
206	cn_tp = NULL;
207	cn_dev_t = NODEV;
208	cn_udev_t = NOUDEV;
209}
210
211/*
212 * User has changed the state of the console muting.
213 * This may require us to open or close the device in question.
214 */
215static int
216sysctl_kern_consmute SYSCTL_HANDLER_ARGS
217{
218	int error;
219	int ocn_mute;
220
221	ocn_mute = cn_mute;
222	error = sysctl_handle_int(oidp, &cn_mute, 0, req);
223	if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) {
224		if(ocn_mute && !cn_mute) {
225			/*
226			 * going from muted to unmuted.. open the physical dev
227			 * if the console has been openned
228			 */
229			cninit_finish();
230			if(cn_is_open)
231				/* XXX curproc is not what we want really */
232				error = cnopen(cn_dev_t, openflag,
233					openmode, curproc);
234			/* if it failed, back it out */
235			if ( error != 0) cnuninit();
236		} else if (!ocn_mute && cn_mute) {
237			/*
238			 * going from unmuted to muted.. close the physical dev
239			 * if it's only open via /dev/console
240			 */
241			if(cn_is_open)
242				error = cnclose(cn_dev_t, openflag,
243					openmode, curproc);
244			if ( error == 0) cnuninit();
245		}
246		if (error != 0) {
247			/*
248	 		 * back out the change if there was an error
249			 */
250			cn_mute = ocn_mute;
251		}
252	}
253	return (error);
254}
255
256SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
257	0, sizeof cn_mute, sysctl_kern_consmute, "I", "");
258
259static int
260cnopen(dev, flag, mode, p)
261	dev_t dev;
262	int flag, mode;
263	struct proc *p;
264{
265	dev_t cndev, physdev;
266	int retval = 0;
267
268	if (cn_tab == NULL)
269		return (0);
270	cndev = cn_tab->cn_dev;
271	physdev = (major(dev) == major(cndev) ? dev : cndev);
272	/*
273	 * If mute is active, then non console opens don't get here
274	 * so we don't need to check for that. They
275	 * bypass this and go straight to the device.
276	 */
277	if(!cn_mute)
278		retval = (*cn_phys_open)(physdev, flag, mode, p);
279	if (retval == 0) {
280		/*
281		 * check if we openned it via /dev/console or
282		 * via the physical entry (e.g. /dev/sio0).
283		 */
284		if (dev == cndev)
285			cn_phys_is_open = 1;
286		else if (physdev == cndev) {
287			openmode = mode;
288			openflag = flag;
289			cn_is_open = 1;
290		}
291	}
292	return (retval);
293}
294
295static int
296cnclose(dev, flag, mode, p)
297	dev_t dev;
298	int flag, mode;
299	struct proc *p;
300{
301	dev_t cndev;
302
303	if (cn_tab == NULL)
304		return (0);
305	cndev = cn_tab->cn_dev;
306	/*
307	 * act appropriatly depending on whether it's /dev/console
308	 * or the pysical device (e.g. /dev/sio) that's being closed.
309	 * in either case, don't actually close the device unless
310	 * both are closed.
311	 */
312	if (dev == cndev) {
313		/* the physical device is about to be closed */
314		cn_phys_is_open = 0;
315		if (cn_is_open) {
316			if (cn_tp) {
317				/* perform a ttyhalfclose() */
318				/* reset session and proc group */
319				cn_tp->t_pgrp = NULL;
320				cn_tp->t_session = NULL;
321			}
322			return (0);
323		}
324	} else if (major(dev) != major(cndev)) {
325		/* the logical console is about to be closed */
326		cn_is_open = 0;
327		if (cn_phys_is_open)
328			return (0);
329		dev = cndev;
330	}
331	if(cn_phys_close)
332		return ((*cn_phys_close)(dev, flag, mode, p));
333	return (0);
334}
335
336static int
337cnread(dev, uio, flag)
338	dev_t dev;
339	struct uio *uio;
340	int flag;
341{
342	if ((cn_tab == NULL) || cn_mute)
343		return (0);
344	dev = cn_tab->cn_dev;
345	return ((*devsw(dev)->d_read)(dev, uio, flag));
346}
347
348static int
349cnwrite(dev, uio, flag)
350	dev_t dev;
351	struct uio *uio;
352	int flag;
353{
354	if ((cn_tab == NULL) || cn_mute) {
355		uio->uio_resid = 0; /* dump the data */
356		return (0);
357	}
358	if (constty)
359		dev = constty->t_dev;
360	else
361		dev = cn_tab->cn_dev;
362	return ((*devsw(dev)->d_write)(dev, uio, flag));
363}
364
365static int
366cnioctl(dev, cmd, data, flag, p)
367	dev_t dev;
368	u_long cmd;
369	caddr_t data;
370	int flag;
371	struct proc *p;
372{
373	int error;
374
375	if ((cn_tab == NULL) || cn_mute)
376		return (0);
377	/*
378	 * Superuser can always use this to wrest control of console
379	 * output from the "virtual" console.
380	 */
381	if (cmd == TIOCCONS && constty) {
382		error = suser(p);
383		if (error)
384			return (error);
385		constty = NULL;
386		return (0);
387	}
388	dev = cn_tab->cn_dev;
389	return ((*devsw(dev)->d_ioctl)(dev, cmd, data, flag, p));
390}
391
392static int
393cnpoll(dev, events, p)
394	dev_t dev;
395	int events;
396	struct proc *p;
397{
398	if ((cn_tab == NULL) || cn_mute)
399		return (1);
400
401	dev = cn_tab->cn_dev;
402
403	return ((*devsw(dev)->d_poll)(dev, events, p));
404}
405
406int
407cngetc()
408{
409	int c;
410	if ((cn_tab == NULL) || cn_mute)
411		return (-1);
412	c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
413	if (c == '\r') c = '\n'; /* console input is always ICRNL */
414	return (c);
415}
416
417int
418cncheckc()
419{
420	if ((cn_tab == NULL) || cn_mute)
421		return (-1);
422	return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
423}
424
425void
426cnputc(c)
427	register int c;
428{
429	if ((cn_tab == NULL) || cn_mute)
430		return;
431	if (c) {
432		if (c == '\n')
433			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
434		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
435	}
436}
437
438static int cn_devsw_installed;
439
440static void
441cn_drvinit(void *unused)
442{
443
444	if( ! cn_devsw_installed ) {
445		cdevsw_add(&cn_cdevsw);
446		cn_devsw_installed = 1;
447#ifdef DEVFS
448		cn_devfs_token = devfs_add_devswf(&cn_cdevsw, 0, DV_CHR,
449						  UID_ROOT, GID_WHEEL, 0600,
450						  "console");
451#endif
452	}
453}
454
455SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL)
456
457
458