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