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