kern_cons.c revision 48104
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.67 1999/05/31 11:25:41 phk 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 */
91SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
92	&cn_dev_t, sizeof cn_dev_t, "T,dev_t", "");
93
94static int cn_mute;
95
96int	cons_unavail = 0;	/* XXX:
97				 * physical console not available for
98				 * input (i.e., it is in graphics mode)
99				 */
100
101static u_char cn_is_open;		/* nonzero if logical console is open */
102static int openmode, openflag;		/* how /dev/console was openned */
103static u_char cn_phys_is_open;		/* nonzero if physical device is open */
104static d_close_t *cn_phys_close;	/* physical device close function */
105static d_open_t *cn_phys_open;		/* physical device open function */
106static struct consdev *cn_tab;		/* physical console device info */
107static struct tty *cn_tp;		/* physical console tty struct */
108#ifdef DEVFS
109static void *cn_devfs_token;		/* represents the devfs entry */
110#endif /* DEVFS */
111
112CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL);
113
114void
115cninit()
116{
117	struct consdev *best_cp, *cp;
118	const struct consdev **list;
119
120	/*
121	 * Find the first console with the highest priority.
122	 */
123	best_cp = NULL;
124	list = (const 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_tp->t_dev;
187}
188
189static void
190cnuninit(void)
191{
192	struct cdevsw *cdp;
193
194	if (cn_tab == NULL)
195		return;
196
197	/*
198	 * Unhook the open and close functions.
199	 */
200	cdp = devsw(cn_tab->cn_dev);
201	cdp->d_close = cn_phys_close;
202	cn_phys_close = NULL;
203	cdp->d_open = cn_phys_open;
204	cn_phys_open = NULL;
205	cn_tp = NULL;
206	cn_dev_t = 0;
207}
208
209/*
210 * User has changed the state of the console muting.
211 * This may require us to open or close the device in question.
212 */
213static int
214sysctl_kern_consmute SYSCTL_HANDLER_ARGS
215{
216	int error;
217	int ocn_mute;
218
219	ocn_mute = cn_mute;
220	error = sysctl_handle_int(oidp, &cn_mute, 0, req);
221	if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) {
222		if(ocn_mute && !cn_mute) {
223			/*
224			 * going from muted to unmuted.. open the physical dev
225			 * if the console has been openned
226			 */
227			cninit_finish();
228			if(cn_is_open)
229				/* XXX curproc is not what we want really */
230				error = cnopen(cn_dev_t, openflag,
231					openmode, curproc);
232			/* if it failed, back it out */
233			if ( error != 0) cnuninit();
234		} else if (!ocn_mute && cn_mute) {
235			/*
236			 * going from unmuted to muted.. close the physical dev
237			 * if it's only open via /dev/console
238			 */
239			if(cn_is_open)
240				error = cnclose(cn_dev_t, openflag,
241					openmode, curproc);
242			if ( error == 0) cnuninit();
243		}
244		if (error != 0) {
245			/*
246	 		 * back out the change if there was an error
247			 */
248			cn_mute = ocn_mute;
249		}
250	}
251	return (error);
252}
253
254SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
255	0, sizeof cn_mute, sysctl_kern_consmute, "I", "");
256
257static int
258cnopen(dev, flag, mode, p)
259	dev_t dev;
260	int flag, mode;
261	struct proc *p;
262{
263	dev_t cndev, physdev;
264	int retval = 0;
265
266	if (cn_tab == NULL)
267		return (0);
268	cndev = cn_tab->cn_dev;
269	physdev = (major(dev) == major(cndev) ? dev : cndev);
270	/*
271	 * If mute is active, then non console opens don't get here
272	 * so we don't need to check for that. They
273	 * bypass this and go straight to the device.
274	 */
275	if(!cn_mute)
276		retval = (*cn_phys_open)(physdev, flag, mode, p);
277	if (retval == 0) {
278		/*
279		 * check if we openned it via /dev/console or
280		 * via the physical entry (e.g. /dev/sio0).
281		 */
282		if (dev == cndev)
283			cn_phys_is_open = 1;
284		else if (physdev == cndev) {
285			openmode = mode;
286			openflag = flag;
287			cn_is_open = 1;
288		}
289	}
290	return (retval);
291}
292
293static int
294cnclose(dev, flag, mode, p)
295	dev_t dev;
296	int flag, mode;
297	struct proc *p;
298{
299	dev_t cndev;
300
301	if (cn_tab == NULL)
302		return (0);
303	cndev = cn_tab->cn_dev;
304	/*
305	 * act appropriatly depending on whether it's /dev/console
306	 * or the pysical device (e.g. /dev/sio) that's being closed.
307	 * in either case, don't actually close the device unless
308	 * both are closed.
309	 */
310	if (dev == cndev) {
311		/* the physical device is about to be closed */
312		cn_phys_is_open = 0;
313		if (cn_is_open) {
314			if (cn_tp) {
315				/* perform a ttyhalfclose() */
316				/* reset session and proc group */
317				cn_tp->t_pgrp = NULL;
318				cn_tp->t_session = NULL;
319			}
320			return (0);
321		}
322	} else if (major(dev) != major(cndev)) {
323		/* the logical console is about to be closed */
324		cn_is_open = 0;
325		if (cn_phys_is_open)
326			return (0);
327		dev = cndev;
328	}
329	if(cn_phys_close)
330		return ((*cn_phys_close)(dev, flag, mode, p));
331	return (0);
332}
333
334static int
335cnread(dev, uio, flag)
336	dev_t dev;
337	struct uio *uio;
338	int flag;
339{
340	if ((cn_tab == NULL) || cn_mute)
341		return (0);
342	dev = cn_tab->cn_dev;
343	return ((*devsw(dev)->d_read)(dev, uio, flag));
344}
345
346static int
347cnwrite(dev, uio, flag)
348	dev_t dev;
349	struct uio *uio;
350	int flag;
351{
352	if ((cn_tab == NULL) || cn_mute) {
353		uio->uio_resid = 0; /* dump the data */
354		return (0);
355	}
356	if (constty)
357		dev = constty->t_dev;
358	else
359		dev = cn_tab->cn_dev;
360	return ((*devsw(dev)->d_write)(dev, uio, flag));
361}
362
363static int
364cnioctl(dev, cmd, data, flag, p)
365	dev_t dev;
366	u_long cmd;
367	caddr_t data;
368	int flag;
369	struct proc *p;
370{
371	int error;
372
373	if ((cn_tab == NULL) || cn_mute)
374		return (0);
375	/*
376	 * Superuser can always use this to wrest control of console
377	 * output from the "virtual" console.
378	 */
379	if (cmd == TIOCCONS && constty) {
380		error = suser(p);
381		if (error)
382			return (error);
383		constty = NULL;
384		return (0);
385	}
386	dev = cn_tab->cn_dev;
387	return ((*devsw(dev)->d_ioctl)(dev, cmd, data, flag, p));
388}
389
390static int
391cnpoll(dev, events, p)
392	dev_t dev;
393	int events;
394	struct proc *p;
395{
396	if ((cn_tab == NULL) || cn_mute)
397		return (1);
398
399	dev = cn_tab->cn_dev;
400
401	return ((*devsw(dev)->d_poll)(dev, events, p));
402}
403
404int
405cngetc()
406{
407	int c;
408	if ((cn_tab == NULL) || cn_mute)
409		return (-1);
410	c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
411	if (c == '\r') c = '\n'; /* console input is always ICRNL */
412	return (c);
413}
414
415int
416cncheckc()
417{
418	if ((cn_tab == NULL) || cn_mute)
419		return (-1);
420	return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
421}
422
423void
424cnputc(c)
425	register int c;
426{
427	if ((cn_tab == NULL) || cn_mute)
428		return;
429	if (c) {
430		if (c == '\n')
431			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
432		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
433	}
434}
435
436static int cn_devsw_installed;
437
438static void
439cn_drvinit(void *unused)
440{
441
442	if( ! cn_devsw_installed ) {
443		cdevsw_add(&cn_cdevsw);
444		cn_devsw_installed = 1;
445#ifdef DEVFS
446		cn_devfs_token = devfs_add_devswf(&cn_cdevsw, 0, DV_CHR,
447						  UID_ROOT, GID_WHEEL, 0600,
448						  "console");
449#endif
450	}
451}
452
453SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL)
454
455
456