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