Home | History | Annotate | Line # | Download | only in dev
cons.c revision 1.67.2.1
      1  1.67.2.1    jruoho /*	$NetBSD: cons.c,v 1.67.2.1 2011/06/06 09:07:38 jruoho Exp $	*/
      2      1.18       cgd 
      3       1.1       cgd /*
      4  1.67.2.1    jruoho  * Copyright (c) 1988 University of Utah.
      5      1.17       cgd  * Copyright (c) 1990, 1993
      6      1.17       cgd  *	The Regents of the University of California.  All rights reserved.
      7       1.1       cgd  *
      8       1.1       cgd  * This code is derived from software contributed to Berkeley by
      9       1.1       cgd  * the Systems Programming Group of the University of Utah Computer
     10       1.1       cgd  * Science Department.
     11       1.1       cgd  *
     12       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     13       1.1       cgd  * modification, are permitted provided that the following conditions
     14       1.1       cgd  * are met:
     15       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     17       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     19       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     20      1.49       agc  * 3. Neither the name of the University nor the names of its contributors
     21      1.49       agc  *    may be used to endorse or promote products derived from this software
     22      1.49       agc  *    without specific prior written permission.
     23      1.49       agc  *
     24      1.49       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25      1.49       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26      1.49       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27      1.49       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28      1.49       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29      1.49       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30      1.49       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31      1.49       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32      1.49       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33      1.49       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34      1.49       agc  * SUCH DAMAGE.
     35      1.49       agc  *
     36      1.49       agc  * from: Utah $Hdr: cons.c 1.7 92/01/21$
     37      1.49       agc  *
     38      1.49       agc  *	@(#)cons.c	8.2 (Berkeley) 1/12/94
     39      1.49       agc  */
     40      1.49       agc 
     41      1.41     lukem #include <sys/cdefs.h>
     42  1.67.2.1    jruoho __KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.67.2.1 2011/06/06 09:07:38 jruoho Exp $");
     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/systm.h>
     47      1.10       cgd #include <sys/buf.h>
     48      1.10       cgd #include <sys/ioctl.h>
     49      1.56        ws #include <sys/poll.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.58      elad #include <sys/kauth.h>
     55      1.62        ad #include <sys/mutex.h>
     56       1.1       cgd 
     57      1.11       cgd #include <dev/cons.h>
     58       1.1       cgd 
     59      1.43   gehenna dev_type_open(cnopen);
     60      1.43   gehenna dev_type_close(cnclose);
     61      1.43   gehenna dev_type_read(cnread);
     62      1.43   gehenna dev_type_write(cnwrite);
     63      1.43   gehenna dev_type_ioctl(cnioctl);
     64      1.43   gehenna dev_type_poll(cnpoll);
     65      1.45  jdolecek dev_type_kqfilter(cnkqfilter);
     66      1.43   gehenna 
     67      1.64        ad static bool cn_redirect(dev_t *, int, int *);
     68      1.50       dsl 
     69      1.43   gehenna const struct cdevsw cons_cdevsw = {
     70      1.43   gehenna 	cnopen, cnclose, cnread, cnwrite, cnioctl,
     71      1.45  jdolecek 	nostop, notty, cnpoll, nommap, cnkqfilter, D_TTY
     72      1.43   gehenna };
     73      1.43   gehenna 
     74      1.10       cgd struct	tty *constty = NULL;	/* virtual console output device */
     75      1.52       cdi struct	consdev *cn_tab;	/* physical console device info */
     76      1.50       dsl struct	vnode *cn_devvp[2];	/* vnode for underlying device. */
     77       1.1       cgd 
     78       1.7    andrew int
     79      1.57  christos cnopen(dev_t dev, int flag, int mode, struct lwp *l)
     80       1.1       cgd {
     81      1.37       mrg 	dev_t cndev;
     82      1.65        ad 	int unit, error;
     83      1.50       dsl 
     84      1.50       dsl 	unit = minor(dev);
     85      1.50       dsl 	if (unit > 1)
     86      1.50       dsl 		return ENODEV;
     87      1.21   mycroft 
     88       1.1       cgd 	if (cn_tab == NULL)
     89       1.1       cgd 		return (0);
     90      1.10       cgd 
     91      1.10       cgd 	/*
     92      1.10       cgd 	 * always open the 'real' console device, so we don't get nailed
     93      1.10       cgd 	 * later.  This follows normal device semantics; they always get
     94      1.10       cgd 	 * open() calls.
     95      1.10       cgd 	 */
     96      1.37       mrg 	cndev = cn_tab->cn_dev;
     97      1.37       mrg 	if (cndev == NODEV) {
     98      1.33       leo 		/*
     99      1.33       leo 		 * This is most likely an error in the console attach
    100      1.53       wiz 		 * code. Panicking looks better than jumping into nowhere
    101      1.33       leo 		 * through cdevsw below....
    102      1.33       leo 		 */
    103      1.44    provos 		panic("cnopen: no console device");
    104      1.33       leo 	}
    105      1.37       mrg 	if (dev == cndev) {
    106      1.37       mrg 		/*
    107      1.37       mrg 		 * This causes cnopen() to be called recursively, which
    108      1.37       mrg 		 * is generally a bad thing.  It is often caused when
    109      1.37       mrg 		 * dev == 0 and cn_dev has not been set, but was probably
    110      1.37       mrg 		 * initialised to 0.
    111      1.37       mrg 		 */
    112      1.44    provos 		panic("cnopen: cn_tab->cn_dev == dev");
    113      1.37       mrg 	}
    114      1.65        ad 	if (cn_devvp[unit] != NULLVP)
    115      1.65        ad 		return 0;
    116      1.65        ad 	if ((error = cdevvp(cndev, &cn_devvp[unit])) != 0)
    117      1.65        ad 		printf("cnopen: unable to get vnode reference\n");
    118      1.65        ad 	error = vn_lock(cn_devvp[unit], LK_EXCLUSIVE | LK_RETRY);
    119      1.65        ad 	if (error == 0) {
    120      1.65        ad 		error = VOP_OPEN(cn_devvp[unit], flag, kauth_cred_get());
    121      1.67   hannken 		VOP_UNLOCK(cn_devvp[unit]);
    122      1.13       cgd 	}
    123      1.65        ad 	return error;
    124       1.1       cgd }
    125      1.54     perry 
    126       1.7    andrew int
    127      1.57  christos cnclose(dev_t dev, int flag, int mode, struct lwp *l)
    128       1.1       cgd {
    129      1.10       cgd 	struct vnode *vp;
    130      1.65        ad 	int unit, error;
    131      1.50       dsl 
    132      1.50       dsl 	unit = minor(dev);
    133      1.10       cgd 
    134       1.1       cgd 	if (cn_tab == NULL)
    135       1.1       cgd 		return (0);
    136      1.10       cgd 
    137      1.65        ad 	vp = cn_devvp[unit];
    138      1.65        ad 	cn_devvp[unit] = NULL;
    139      1.65        ad 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    140      1.65        ad 	if (error == 0) {
    141      1.65        ad 		error = VOP_CLOSE(vp, flag, kauth_cred_get());
    142      1.67   hannken 		VOP_UNLOCK(vp);
    143      1.13       cgd 	}
    144      1.65        ad 	return error;
    145       1.1       cgd }
    146      1.54     perry 
    147       1.7    andrew int
    148      1.46      matt cnread(dev_t dev, struct uio *uio, int flag)
    149       1.1       cgd {
    150      1.50       dsl 	int error;
    151      1.21   mycroft 
    152      1.10       cgd 	/*
    153      1.10       cgd 	 * If we would redirect input, punt.  This will keep strange
    154      1.10       cgd 	 * things from happening to people who are using the real
    155      1.10       cgd 	 * console.  Nothing should be using /dev/console for
    156      1.10       cgd 	 * input (except a shell in single-user mode, but then,
    157      1.10       cgd 	 * one wouldn't TIOCCONS then).
    158      1.10       cgd 	 */
    159      1.64        ad 	if (!cn_redirect(&dev, 1, &error))
    160      1.50       dsl 		return error;
    161      1.64        ad 	return cdev_read(dev, uio, flag);
    162       1.1       cgd }
    163      1.54     perry 
    164       1.7    andrew int
    165      1.46      matt cnwrite(dev_t dev, struct uio *uio, int flag)
    166       1.1       cgd {
    167      1.50       dsl 	int error;
    168      1.21   mycroft 
    169      1.50       dsl 	/* Redirect output, if that's appropriate. */
    170      1.64        ad 	if (!cn_redirect(&dev, 0, &error))
    171      1.50       dsl 		return error;
    172      1.64        ad 	return cdev_write(dev, uio, flag);
    173      1.25   mycroft }
    174      1.25   mycroft 
    175       1.7    andrew int
    176      1.63  christos cnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    177       1.1       cgd {
    178       1.1       cgd 	int error;
    179       1.1       cgd 
    180      1.62        ad 	error = 0;
    181      1.62        ad 
    182       1.1       cgd 	/*
    183       1.1       cgd 	 * Superuser can always use this to wrest control of console
    184       1.1       cgd 	 * output from the "virtual" console.
    185       1.1       cgd 	 */
    186      1.10       cgd 	if (cmd == TIOCCONS && constty != NULL) {
    187      1.59        ad 		error = kauth_authorize_generic(l->l_cred,
    188      1.59        ad 		    KAUTH_GENERIC_ISSUSER, NULL);
    189      1.62        ad 		if (!error)
    190      1.62        ad 			constty = NULL;
    191      1.62        ad 		return (error);
    192       1.1       cgd 	}
    193      1.10       cgd 
    194      1.10       cgd 	/*
    195      1.10       cgd 	 * Redirect the ioctl, if that's appropriate.
    196      1.10       cgd 	 * Note that strange things can happen, if a program does
    197      1.10       cgd 	 * ioctls on /dev/console, then the console is redirected
    198      1.10       cgd 	 * out from under it.
    199      1.10       cgd 	 */
    200      1.64        ad 	if (!cn_redirect(&dev, 0, &error))
    201      1.64        ad 		return error;
    202      1.64        ad 	return cdev_ioctl(dev, cmd, data, flag, l);
    203       1.1       cgd }
    204       1.1       cgd 
    205       1.1       cgd /*ARGSUSED*/
    206       1.7    andrew int
    207      1.57  christos cnpoll(dev_t dev, int events, struct lwp *l)
    208       1.1       cgd {
    209      1.50       dsl 	int error;
    210      1.21   mycroft 
    211      1.10       cgd 	/*
    212      1.38     lukem 	 * Redirect the poll, if that's appropriate.
    213      1.10       cgd 	 * I don't want to think of the possible side effects
    214      1.10       cgd 	 * of console redirection here.
    215      1.10       cgd 	 */
    216      1.64        ad 	if (!cn_redirect(&dev, 0, &error))
    217      1.56        ws 		return POLLHUP;
    218      1.64        ad 	return cdev_poll(dev, events, l);
    219      1.45  jdolecek }
    220      1.45  jdolecek 
    221      1.45  jdolecek /*ARGSUSED*/
    222      1.45  jdolecek int
    223      1.46      matt cnkqfilter(dev_t dev, struct knote *kn)
    224      1.45  jdolecek {
    225      1.50       dsl 	int error;
    226      1.45  jdolecek 
    227      1.45  jdolecek 	/*
    228      1.45  jdolecek 	 * Redirect the kqfilter, if that's appropriate.
    229      1.45  jdolecek 	 * I don't want to think of the possible side effects
    230      1.45  jdolecek 	 * of console redirection here.
    231      1.45  jdolecek 	 */
    232      1.64        ad 	if (!cn_redirect(&dev, 0, &error))
    233      1.50       dsl 		return error;
    234      1.64        ad 	return cdev_kqfilter(dev, kn);
    235       1.1       cgd }
    236       1.1       cgd 
    237       1.7    andrew int
    238      1.46      matt cngetc(void)
    239       1.1       cgd {
    240       1.1       cgd 	if (cn_tab == NULL)
    241       1.1       cgd 		return (0);
    242       1.1       cgd 	return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
    243      1.36    itojun }
    244      1.36    itojun 
    245      1.36    itojun int
    246      1.46      matt cngetsn(char *cp, int size)
    247      1.36    itojun {
    248      1.36    itojun 	char *lp;
    249      1.36    itojun 	int c, len;
    250      1.36    itojun 
    251      1.36    itojun 	cnpollc(1);
    252      1.36    itojun 
    253      1.36    itojun 	lp = cp;
    254      1.36    itojun 	len = 0;
    255      1.36    itojun 	for (;;) {
    256      1.36    itojun 		c = cngetc();
    257      1.36    itojun 		switch (c) {
    258      1.36    itojun 		case '\n':
    259      1.36    itojun 		case '\r':
    260      1.36    itojun 			printf("\n");
    261      1.36    itojun 			*lp++ = '\0';
    262      1.36    itojun 			cnpollc(0);
    263      1.36    itojun 			return (len);
    264      1.36    itojun 		case '\b':
    265      1.36    itojun 		case '\177':
    266      1.36    itojun 		case '#':
    267      1.36    itojun 			if (len) {
    268      1.36    itojun 				--len;
    269      1.36    itojun 				--lp;
    270      1.36    itojun 				printf("\b \b");
    271      1.36    itojun 			}
    272      1.36    itojun 			continue;
    273      1.36    itojun 		case '@':
    274      1.40  jdolecek 		case 'u'&037:	/* CTRL-u */
    275      1.36    itojun 			len = 0;
    276      1.36    itojun 			lp = cp;
    277      1.36    itojun 			printf("\n");
    278      1.36    itojun 			continue;
    279      1.36    itojun 		default:
    280      1.36    itojun 			if (len + 1 >= size || c < ' ') {
    281      1.40  jdolecek 				printf("\007");
    282      1.36    itojun 				continue;
    283      1.36    itojun 			}
    284      1.36    itojun 			printf("%c", c);
    285      1.36    itojun 			++len;
    286      1.36    itojun 			*lp++ = c;
    287      1.36    itojun 		}
    288      1.36    itojun 	}
    289       1.1       cgd }
    290       1.1       cgd 
    291      1.29  christos void
    292      1.46      matt cnputc(int c)
    293       1.1       cgd {
    294      1.21   mycroft 
    295       1.1       cgd 	if (cn_tab == NULL)
    296      1.54     perry 		return;
    297      1.29  christos 
    298       1.1       cgd 	if (c) {
    299       1.1       cgd 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
    300       1.1       cgd 		if (c == '\n')
    301       1.1       cgd 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
    302       1.1       cgd 	}
    303      1.19   mycroft }
    304      1.19   mycroft 
    305      1.19   mycroft void
    306      1.46      matt cnpollc(int on)
    307      1.19   mycroft {
    308      1.19   mycroft 	static int refcount = 0;
    309      1.19   mycroft 
    310      1.19   mycroft 	if (cn_tab == NULL)
    311      1.19   mycroft 		return;
    312      1.19   mycroft 	if (!on)
    313      1.19   mycroft 		--refcount;
    314      1.19   mycroft 	if (refcount == 0)
    315      1.19   mycroft 		(*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
    316      1.19   mycroft 	if (on)
    317      1.19   mycroft 		++refcount;
    318      1.21   mycroft }
    319      1.21   mycroft 
    320      1.21   mycroft void
    321      1.61  christos nullcnpollc(dev_t dev, int on)
    322      1.21   mycroft {
    323      1.21   mycroft 
    324      1.34   thorpej }
    325      1.34   thorpej 
    326      1.34   thorpej void
    327      1.46      matt cnbell(u_int pitch, u_int period, u_int volume)
    328      1.34   thorpej {
    329      1.34   thorpej 
    330      1.34   thorpej 	if (cn_tab == NULL || cn_tab->cn_bell == NULL)
    331      1.34   thorpej 		return;
    332      1.34   thorpej 	(*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
    333      1.46      matt }
    334      1.46      matt 
    335      1.46      matt void
    336      1.46      matt cnflush(void)
    337      1.46      matt {
    338      1.46      matt 	if (cn_tab == NULL || cn_tab->cn_flush == NULL)
    339      1.46      matt 		return;
    340      1.46      matt 	(*cn_tab->cn_flush)(cn_tab->cn_dev);
    341      1.46      matt }
    342      1.54     perry 
    343      1.46      matt void
    344      1.46      matt cnhalt(void)
    345      1.46      matt {
    346      1.46      matt 	if (cn_tab == NULL || cn_tab->cn_halt == NULL)
    347      1.46      matt 		return;
    348      1.46      matt 	(*cn_tab->cn_halt)(cn_tab->cn_dev);
    349      1.50       dsl }
    350      1.50       dsl 
    351      1.62        ad /*
    352      1.62        ad  * Redirect output, if that's appropriate.  If there's no real console,
    353      1.62        ad  * return ENXIO.
    354      1.62        ad  *
    355      1.62        ad  * Call with tty_mutex held.
    356      1.62        ad  */
    357      1.64        ad static bool
    358      1.50       dsl cn_redirect(dev_t *devp, int is_read, int *error)
    359      1.50       dsl {
    360      1.50       dsl 	dev_t dev = *devp;
    361      1.50       dsl 
    362      1.50       dsl 	*error = ENXIO;
    363      1.50       dsl 	if (constty != NULL && minor(dev) == 0 &&
    364      1.55    martin 	    (cn_tab == NULL || (cn_tab->cn_pri != CN_REMOTE))) {
    365      1.50       dsl 		if (is_read) {
    366      1.50       dsl 			*error = 0;
    367      1.64        ad 			return false;
    368      1.50       dsl 		}
    369      1.50       dsl 		dev = constty->t_dev;
    370      1.50       dsl 	} else if (cn_tab == NULL)
    371      1.64        ad 		return false;
    372      1.50       dsl 	else
    373      1.50       dsl 		dev = cn_tab->cn_dev;
    374      1.50       dsl 	*devp = dev;
    375      1.64        ad 	return true;
    376       1.2       cgd }
    377