Home | History | Annotate | Line # | Download | only in dev
cons.c revision 1.28
      1 /*	$NetBSD: cons.c,v 1.28 1995/11/25 00:03:35 cgd 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 (cn_devvp == NULLVP) {
     79 		/* try to get a reference on its vnode, but fail silently */
     80 		cdevvp(dev, &cn_devvp);
     81 	}
     82 	return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
     83 }
     84 
     85 int
     86 cnclose(dev, flag, mode, p)
     87 	dev_t dev;
     88 	int flag, mode;
     89 	struct proc *p;
     90 {
     91 	struct vnode *vp;
     92 
     93 	if (cn_tab == NULL)
     94 		return (0);
     95 
     96 	/*
     97 	 * If the real console isn't otherwise open, close it.
     98 	 * If it's otherwise open, don't close it, because that'll
     99 	 * screw up others who have it open.
    100 	 */
    101 	dev = cn_tab->cn_dev;
    102 	if (cn_devvp != NULLVP) {
    103 		/* release our reference to real dev's vnode */
    104 		vrele(cn_devvp);
    105 		cn_devvp = NULLVP;
    106 	}
    107 	if (vfinddev(dev, VCHR, &vp) && vcount(vp))
    108 		return (0);
    109 	return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p));
    110 }
    111 
    112 int
    113 cnread(dev, uio, flag)
    114 	dev_t dev;
    115 	struct uio *uio;
    116 	int flag;
    117 {
    118 
    119 	/*
    120 	 * If we would redirect input, punt.  This will keep strange
    121 	 * things from happening to people who are using the real
    122 	 * console.  Nothing should be using /dev/console for
    123 	 * input (except a shell in single-user mode, but then,
    124 	 * one wouldn't TIOCCONS then).
    125 	 */
    126 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    127 		return 0;
    128 	else if (cn_tab == NULL)
    129 		return ENXIO;
    130 
    131 	dev = cn_tab->cn_dev;
    132 	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
    133 }
    134 
    135 int
    136 cnwrite(dev, uio, flag)
    137 	dev_t dev;
    138 	struct uio *uio;
    139 	int flag;
    140 {
    141 
    142 	/*
    143 	 * Redirect output, if that's appropriate.
    144 	 * If there's no real console, return ENXIO.
    145 	 */
    146 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    147 		dev = constty->t_dev;
    148 	else if (cn_tab == NULL)
    149 		return ENXIO;
    150 	else
    151 		dev = cn_tab->cn_dev;
    152 	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
    153 }
    154 
    155 int
    156 cnstop(tp, flag)
    157 	struct tty *tp;
    158 	int flag;
    159 {
    160 
    161 }
    162 
    163 int
    164 cnioctl(dev, cmd, data, flag, p)
    165 	dev_t dev;
    166 	u_long cmd;
    167 	caddr_t data;
    168 	int flag;
    169 	struct proc *p;
    170 {
    171 	int error;
    172 
    173 	/*
    174 	 * Superuser can always use this to wrest control of console
    175 	 * output from the "virtual" console.
    176 	 */
    177 	if (cmd == TIOCCONS && constty != NULL) {
    178 		error = suser(p->p_ucred, (u_short *) NULL);
    179 		if (error)
    180 			return (error);
    181 		constty = NULL;
    182 		return (0);
    183 	}
    184 
    185 	/*
    186 	 * Redirect the ioctl, if that's appropriate.
    187 	 * Note that strange things can happen, if a program does
    188 	 * ioctls on /dev/console, then the console is redirected
    189 	 * out from under it.
    190 	 */
    191 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    192 		dev = constty->t_dev;
    193 	else if (cn_tab == NULL)
    194 		return ENXIO;
    195 	else
    196 		dev = cn_tab->cn_dev;
    197 	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
    198 }
    199 
    200 /*ARGSUSED*/
    201 int
    202 cnselect(dev, rw, p)
    203 	dev_t dev;
    204 	int rw;
    205 	struct proc *p;
    206 {
    207 
    208 	/*
    209 	 * Redirect the ioctl, if that's appropriate.
    210 	 * I don't want to think of the possible side effects
    211 	 * of console redirection here.
    212 	 */
    213 	if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
    214 		dev = constty->t_dev;
    215 	else if (cn_tab == NULL)
    216 		return ENXIO;
    217 	else
    218 		dev = cn_tab->cn_dev;
    219 	return (ttselect(cn_tab->cn_dev, rw, p));
    220 }
    221 
    222 int
    223 cngetc()
    224 {
    225 
    226 	if (cn_tab == NULL)
    227 		return (0);
    228 	return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
    229 }
    230 
    231 int
    232 cnputc(c)
    233 	register int c;
    234 {
    235 
    236 	if (cn_tab == NULL)
    237 		return 0;			/* XXX should be void */
    238 	if (c) {
    239 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
    240 		if (c == '\n')
    241 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
    242 	}
    243 	return 0;				/* XXX should be void */
    244 }
    245 
    246 void
    247 cnpollc(on)
    248 	int on;
    249 {
    250 	static int refcount = 0;
    251 
    252 	if (cn_tab == NULL)
    253 		return;
    254 	if (!on)
    255 		--refcount;
    256 	if (refcount == 0)
    257 		(*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
    258 	if (on)
    259 		++refcount;
    260 }
    261 
    262 void
    263 nullcnpollc(dev, on)
    264 	dev_t dev;
    265 	int on;
    266 {
    267 
    268 }
    269