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