Home | History | Annotate | Line # | Download | only in dev
cons.c revision 1.17
      1  1.17      cgd /* NetBSD $Id: cons.c,v 1.17 1994/06/27 19:49:45 cgd Exp $ */
      2   1.1      cgd /*
      3   1.1      cgd  * Copyright (c) 1988 University of Utah.
      4  1.17      cgd  * Copyright (c) 1990, 1993
      5  1.17      cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * This code is derived from software contributed to Berkeley by
      8   1.1      cgd  * the Systems Programming Group of the University of Utah Computer
      9   1.1      cgd  * Science Department.
     10   1.1      cgd  *
     11   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1      cgd  * modification, are permitted provided that the following conditions
     13   1.1      cgd  * are met:
     14   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     19   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     20   1.1      cgd  *    must display the following acknowledgement:
     21   1.1      cgd  *	This product includes software developed by the University of
     22   1.1      cgd  *	California, Berkeley and its contributors.
     23   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     24   1.1      cgd  *    may be used to endorse or promote products derived from this software
     25   1.1      cgd  *    without specific prior written permission.
     26   1.1      cgd  *
     27   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1      cgd  * SUCH DAMAGE.
     38   1.1      cgd  *
     39  1.17      cgd  * from: Utah $Hdr: cons.c 1.7 92/01/21$
     40  1.17      cgd  *
     41  1.17      cgd  *	@(#)cons.c	8.2 (Berkeley) 1/12/94
     42   1.1      cgd  */
     43   1.1      cgd 
     44  1.10      cgd #include <sys/param.h>
     45  1.10      cgd #include <sys/proc.h>
     46  1.10      cgd #include <sys/user.h>
     47  1.10      cgd #include <sys/systm.h>
     48  1.10      cgd #include <sys/buf.h>
     49  1.10      cgd #include <sys/ioctl.h>
     50  1.10      cgd #include <sys/tty.h>
     51  1.10      cgd #include <sys/file.h>
     52  1.10      cgd #include <sys/conf.h>
     53  1.10      cgd #include <sys/vnode.h>
     54   1.1      cgd 
     55  1.11      cgd #include <dev/cons.h>
     56   1.1      cgd 
     57  1.10      cgd extern struct consdev constab[];
     58   1.1      cgd 
     59  1.10      cgd struct	tty *constty = NULL;	/* virtual console output device */
     60   1.1      cgd struct	consdev *cn_tab;	/* physical console device info */
     61  1.13      cgd struct	vnode *cn_devvp;	/* vnode for underlying device. */
     62  1.13      cgd 
     63   1.6  deraadt 
     64   1.7   andrew void
     65   1.1      cgd cninit()
     66   1.1      cgd {
     67   1.1      cgd 	register struct consdev *cp;
     68   1.1      cgd 
     69   1.1      cgd 	/*
     70   1.1      cgd 	 * Collect information about all possible consoles
     71   1.1      cgd 	 * and find the one with highest priority
     72   1.1      cgd 	 */
     73   1.1      cgd 	for (cp = constab; cp->cn_probe; cp++) {
     74   1.1      cgd 		(*cp->cn_probe)(cp);
     75   1.1      cgd 		if (cp->cn_pri > CN_DEAD &&
     76   1.1      cgd 		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
     77   1.1      cgd 			cn_tab = cp;
     78   1.1      cgd 	}
     79   1.1      cgd 	/*
     80   1.1      cgd 	 * No console, we can handle it
     81   1.1      cgd 	 */
     82   1.1      cgd 	if ((cp = cn_tab) == NULL)
     83   1.1      cgd 		return;
     84   1.1      cgd 	/*
     85   1.1      cgd 	 * Turn on console
     86   1.1      cgd 	 */
     87   1.1      cgd 	(*cp->cn_init)(cp);
     88   1.1      cgd }
     89   1.1      cgd 
     90   1.7   andrew int
     91   1.1      cgd cnopen(dev, flag, mode, p)
     92   1.1      cgd 	dev_t dev;
     93   1.1      cgd 	int flag, mode;
     94   1.1      cgd 	struct proc *p;
     95   1.1      cgd {
     96   1.1      cgd 	if (cn_tab == NULL)
     97   1.1      cgd 		return (0);
     98  1.10      cgd 
     99  1.10      cgd 	/*
    100  1.10      cgd 	 * always open the 'real' console device, so we don't get nailed
    101  1.10      cgd 	 * later.  This follows normal device semantics; they always get
    102  1.10      cgd 	 * open() calls.
    103  1.10      cgd 	 */
    104   1.1      cgd 	dev = cn_tab->cn_dev;
    105  1.13      cgd 	if (cn_devvp == NULLVP) {
    106  1.15      cgd 		/* try to get a reference on its vnode, but fail silently */
    107  1.15      cgd 		cdevvp(dev, &cn_devvp);
    108  1.13      cgd 	}
    109   1.1      cgd 	return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
    110   1.1      cgd }
    111   1.1      cgd 
    112   1.7   andrew int
    113   1.1      cgd cnclose(dev, flag, mode, p)
    114   1.1      cgd 	dev_t dev;
    115   1.1      cgd 	int flag, mode;
    116   1.1      cgd 	struct proc *p;
    117   1.1      cgd {
    118  1.10      cgd 	struct vnode *vp;
    119  1.10      cgd 
    120   1.1      cgd 	if (cn_tab == NULL)
    121   1.1      cgd 		return (0);
    122  1.10      cgd 
    123  1.10      cgd 	/*
    124  1.10      cgd 	 * If the real console isn't otherwise open, close it.
    125  1.10      cgd 	 * If it's otherwise open, don't close it, because that'll
    126  1.10      cgd 	 * screw up others who have it open.
    127  1.10      cgd 	 */
    128   1.1      cgd 	dev = cn_tab->cn_dev;
    129  1.13      cgd 	if (cn_devvp != NULLVP) {
    130  1.13      cgd 		/* release our reference to real dev's vnode */
    131  1.13      cgd 		vrele(cn_devvp);
    132  1.13      cgd 		cn_devvp = NULLVP;
    133  1.13      cgd 	}
    134  1.16  mycroft 	if (vfinddev(dev, VCHR, &vp) && vcount(vp))
    135  1.10      cgd 		return (0);
    136   1.1      cgd 	return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p));
    137   1.1      cgd }
    138   1.1      cgd 
    139   1.7   andrew int
    140   1.1      cgd cnread(dev, uio, flag)
    141   1.1      cgd 	dev_t dev;
    142   1.1      cgd 	struct uio *uio;
    143   1.1      cgd {
    144  1.10      cgd 	/*
    145  1.10      cgd 	 * If we would redirect input, punt.  This will keep strange
    146  1.10      cgd 	 * things from happening to people who are using the real
    147  1.10      cgd 	 * console.  Nothing should be using /dev/console for
    148  1.10      cgd 	 * input (except a shell in single-user mode, but then,
    149  1.10      cgd 	 * one wouldn't TIOCCONS then).
    150  1.10      cgd 	 */
    151  1.10      cgd 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    152  1.10      cgd 		return 0;
    153  1.10      cgd 	else if (cn_tab == NULL)
    154  1.10      cgd 		return ENXIO;
    155  1.10      cgd 
    156   1.1      cgd 	dev = cn_tab->cn_dev;
    157   1.1      cgd 	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
    158   1.1      cgd }
    159   1.1      cgd 
    160   1.7   andrew int
    161   1.1      cgd cnwrite(dev, uio, flag)
    162   1.1      cgd 	dev_t dev;
    163   1.1      cgd 	struct uio *uio;
    164   1.1      cgd {
    165  1.10      cgd 	/*
    166  1.10      cgd 	 * Redirect output, if that's appropriate.
    167  1.10      cgd 	 * If there's no real console, return ENXIO.
    168  1.10      cgd 	 */
    169  1.10      cgd 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    170   1.2      cgd 		dev = constty->t_dev;
    171  1.10      cgd 	else if (cn_tab == NULL)
    172  1.10      cgd 		return ENXIO;
    173   1.2      cgd 	else
    174   1.2      cgd 		dev = cn_tab->cn_dev;
    175   1.1      cgd 	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
    176   1.1      cgd }
    177   1.1      cgd 
    178   1.7   andrew int
    179   1.1      cgd cnioctl(dev, cmd, data, flag, p)
    180   1.1      cgd 	dev_t dev;
    181   1.1      cgd 	caddr_t data;
    182   1.1      cgd 	struct proc *p;
    183   1.1      cgd {
    184   1.1      cgd 	int error;
    185   1.1      cgd 
    186   1.1      cgd 	/*
    187   1.1      cgd 	 * Superuser can always use this to wrest control of console
    188   1.1      cgd 	 * output from the "virtual" console.
    189   1.1      cgd 	 */
    190  1.10      cgd 	if (cmd == TIOCCONS && constty != NULL) {
    191   1.1      cgd 		error = suser(p->p_ucred, (u_short *) NULL);
    192   1.1      cgd 		if (error)
    193   1.1      cgd 			return (error);
    194   1.1      cgd 		constty = NULL;
    195   1.1      cgd 		return (0);
    196   1.1      cgd 	}
    197  1.10      cgd 
    198  1.10      cgd 	/*
    199  1.10      cgd 	 * Redirect the ioctl, if that's appropriate.
    200  1.10      cgd 	 * Note that strange things can happen, if a program does
    201  1.10      cgd 	 * ioctls on /dev/console, then the console is redirected
    202  1.10      cgd 	 * out from under it.
    203  1.10      cgd 	 */
    204  1.12      cgd 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    205  1.10      cgd 		dev = constty->t_dev;
    206  1.10      cgd 	else if (cn_tab == NULL)
    207  1.10      cgd 		return ENXIO;
    208  1.10      cgd 	else
    209  1.10      cgd 		dev = cn_tab->cn_dev;
    210   1.1      cgd 	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
    211   1.1      cgd }
    212   1.1      cgd 
    213   1.1      cgd /*ARGSUSED*/
    214   1.7   andrew int
    215   1.1      cgd cnselect(dev, rw, p)
    216   1.1      cgd 	dev_t dev;
    217   1.1      cgd 	int rw;
    218   1.1      cgd 	struct proc *p;
    219   1.1      cgd {
    220  1.10      cgd 	/*
    221  1.10      cgd 	 * Redirect the ioctl, if that's appropriate.
    222  1.10      cgd 	 * I don't want to think of the possible side effects
    223  1.10      cgd 	 * of console redirection here.
    224  1.10      cgd 	 */
    225  1.12      cgd 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    226  1.10      cgd 		dev = constty->t_dev;
    227  1.10      cgd 	else if (cn_tab == NULL)
    228  1.10      cgd 		return ENXIO;
    229  1.10      cgd 	else
    230  1.10      cgd 		dev = cn_tab->cn_dev;
    231   1.1      cgd 	return (ttselect(cn_tab->cn_dev, rw, p));
    232   1.1      cgd }
    233   1.1      cgd 
    234   1.7   andrew int
    235   1.1      cgd cngetc()
    236   1.1      cgd {
    237   1.1      cgd 	if (cn_tab == NULL)
    238   1.1      cgd 		return (0);
    239   1.1      cgd 	return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
    240   1.1      cgd }
    241   1.1      cgd 
    242   1.7   andrew int
    243   1.1      cgd cnputc(c)
    244   1.1      cgd 	register int c;
    245   1.1      cgd {
    246   1.1      cgd 	if (cn_tab == NULL)
    247   1.1      cgd 		return;
    248   1.1      cgd 	if (c) {
    249   1.1      cgd 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
    250   1.1      cgd 		if (c == '\n')
    251   1.1      cgd 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
    252   1.1      cgd 	}
    253   1.2      cgd }
    254