Home | History | Annotate | Line # | Download | only in kern
subr_log.c revision 1.20.6.1
      1  1.20.6.1   thorpej /*	$NetBSD: subr_log.c,v 1.20.6.1 2001/09/08 04:07:06 thorpej Exp $	*/
      2       1.7       cgd 
      3       1.1       cgd /*
      4       1.6       cgd  * Copyright (c) 1982, 1986, 1993
      5       1.6       cgd  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8       1.1       cgd  * modification, are permitted provided that the following conditions
      9       1.1       cgd  * are met:
     10       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16       1.1       cgd  *    must display the following acknowledgement:
     17       1.1       cgd  *	This product includes software developed by the University of
     18       1.1       cgd  *	California, Berkeley and its contributors.
     19       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20       1.1       cgd  *    may be used to endorse or promote products derived from this software
     21       1.1       cgd  *    without specific prior written permission.
     22       1.1       cgd  *
     23       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33       1.1       cgd  * SUCH DAMAGE.
     34       1.1       cgd  *
     35      1.15      fvdl  *	@(#)subr_log.c	8.3 (Berkeley) 2/14/95
     36       1.1       cgd  */
     37       1.1       cgd 
     38       1.1       cgd /*
     39       1.1       cgd  * Error log buffer for kernel printf's.
     40       1.1       cgd  */
     41       1.1       cgd 
     42       1.4   mycroft #include <sys/param.h>
     43       1.4   mycroft #include <sys/systm.h>
     44       1.4   mycroft #include <sys/proc.h>
     45       1.4   mycroft #include <sys/vnode.h>
     46       1.4   mycroft #include <sys/ioctl.h>
     47       1.4   mycroft #include <sys/msgbuf.h>
     48       1.4   mycroft #include <sys/file.h>
     49       1.9  christos #include <sys/signalvar.h>
     50      1.10  christos #include <sys/syslog.h>
     51      1.11  christos #include <sys/conf.h>
     52      1.12   mycroft #include <sys/select.h>
     53      1.12   mycroft #include <sys/poll.h>
     54       1.1       cgd 
     55       1.1       cgd #define LOG_RDPRI	(PZERO + 1)
     56       1.1       cgd 
     57       1.1       cgd #define LOG_ASYNC	0x04
     58       1.1       cgd #define LOG_RDWAIT	0x08
     59       1.1       cgd 
     60       1.1       cgd struct logsoftc {
     61       1.1       cgd 	int	sc_state;		/* see above for possibilities */
     62       1.6       cgd 	struct	selinfo sc_selp;	/* process waiting on select call */
     63       1.1       cgd 	int	sc_pgid;		/* process/group for async I/O */
     64       1.1       cgd } logsoftc;
     65       1.1       cgd 
     66       1.1       cgd int	log_open;			/* also used in log() */
     67      1.13       leo int	msgbufmapped;			/* is the message buffer mapped */
     68      1.13       leo int	msgbufenabled;			/* is logging to the buffer enabled */
     69      1.13       leo struct	kern_msgbuf *msgbufp;		/* the mapped buffer, itself. */
     70      1.13       leo 
     71      1.13       leo void
     72      1.13       leo initmsgbuf(buf, bufsize)
     73      1.13       leo 	caddr_t buf;
     74      1.13       leo 	size_t bufsize;
     75      1.13       leo {
     76      1.18  augustss 	struct kern_msgbuf *mbp;
     77      1.13       leo 	long new_bufs;
     78      1.13       leo 
     79      1.13       leo 	/* Sanity-check the given size. */
     80      1.13       leo 	if (bufsize < sizeof(struct kern_msgbuf))
     81      1.13       leo 		return;
     82      1.13       leo 
     83      1.13       leo 	mbp = msgbufp = (struct kern_msgbuf *)buf;
     84      1.13       leo 
     85      1.13       leo 	new_bufs = bufsize - offsetof(struct kern_msgbuf, msg_bufc);
     86      1.13       leo 	if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
     87      1.13       leo 	    (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
     88      1.13       leo 	    (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
     89      1.13       leo 		/*
     90      1.13       leo 		 * If the buffer magic number is wrong, has changed
     91      1.13       leo 		 * size (which shouldn't happen often), or is
     92      1.13       leo 		 * internally inconsistent, initialize it.
     93      1.13       leo 		 */
     94      1.13       leo 
     95      1.16     perry 		memset(buf, 0, bufsize);
     96      1.13       leo 		mbp->msg_magic = MSG_MAGIC;
     97      1.13       leo 		mbp->msg_bufs = new_bufs;
     98      1.13       leo 	}
     99      1.13       leo 
    100      1.13       leo 	/* mark it as ready for use. */
    101      1.13       leo 	msgbufmapped = msgbufenabled = 1;
    102      1.13       leo }
    103       1.1       cgd 
    104       1.1       cgd /*ARGSUSED*/
    105       1.3    andrew int
    106       1.1       cgd logopen(dev, flags, mode, p)
    107       1.1       cgd 	dev_t dev;
    108       1.1       cgd 	int flags, mode;
    109       1.1       cgd 	struct proc *p;
    110       1.1       cgd {
    111      1.18  augustss 	struct kern_msgbuf *mbp = msgbufp;
    112       1.1       cgd 
    113       1.1       cgd 	if (log_open)
    114       1.1       cgd 		return (EBUSY);
    115       1.1       cgd 	log_open = 1;
    116       1.1       cgd 	logsoftc.sc_pgid = p->p_pid;		/* signal process only */
    117       1.1       cgd 	/*
    118      1.13       leo 	 * The message buffer is initialized during system configuration.
    119      1.13       leo 	 * If it's been clobbered, note that and return an error.  (This
    120      1.13       leo 	 * allows a user to potentially read the buffer via /dev/kmem,
    121      1.13       leo 	 * and try to figure out what clobbered it.
    122       1.1       cgd 	 */
    123       1.1       cgd 	if (mbp->msg_magic != MSG_MAGIC) {
    124      1.13       leo 		msgbufenabled = 0;
    125      1.13       leo 		return (ENXIO);
    126      1.13       leo 	}
    127       1.1       cgd 
    128       1.1       cgd 	return (0);
    129       1.1       cgd }
    130       1.1       cgd 
    131       1.1       cgd /*ARGSUSED*/
    132       1.3    andrew int
    133       1.6       cgd logclose(dev, flag, mode, p)
    134       1.1       cgd 	dev_t dev;
    135       1.9  christos 	int flag, mode;
    136       1.9  christos 	struct proc *p;
    137       1.1       cgd {
    138       1.6       cgd 
    139       1.1       cgd 	log_open = 0;
    140       1.1       cgd 	logsoftc.sc_state = 0;
    141       1.6       cgd 	return (0);
    142       1.1       cgd }
    143       1.1       cgd 
    144       1.1       cgd /*ARGSUSED*/
    145       1.3    andrew int
    146       1.1       cgd logread(dev, uio, flag)
    147       1.1       cgd 	dev_t dev;
    148       1.1       cgd 	struct uio *uio;
    149       1.1       cgd 	int flag;
    150       1.1       cgd {
    151      1.18  augustss 	struct kern_msgbuf *mbp = msgbufp;
    152      1.18  augustss 	long l;
    153      1.18  augustss 	int s;
    154       1.1       cgd 	int error = 0;
    155       1.1       cgd 
    156       1.1       cgd 	s = splhigh();
    157       1.1       cgd 	while (mbp->msg_bufr == mbp->msg_bufx) {
    158       1.1       cgd 		if (flag & IO_NDELAY) {
    159       1.1       cgd 			splx(s);
    160       1.1       cgd 			return (EWOULDBLOCK);
    161       1.1       cgd 		}
    162       1.1       cgd 		logsoftc.sc_state |= LOG_RDWAIT;
    163       1.9  christos 		error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
    164       1.9  christos 			       "klog", 0);
    165       1.9  christos 		if (error) {
    166       1.1       cgd 			splx(s);
    167       1.1       cgd 			return (error);
    168       1.1       cgd 		}
    169       1.1       cgd 	}
    170       1.1       cgd 	splx(s);
    171       1.1       cgd 	logsoftc.sc_state &= ~LOG_RDWAIT;
    172       1.1       cgd 
    173       1.1       cgd 	while (uio->uio_resid > 0) {
    174       1.1       cgd 		l = mbp->msg_bufx - mbp->msg_bufr;
    175       1.1       cgd 		if (l < 0)
    176      1.13       leo 			l = mbp->msg_bufs - mbp->msg_bufr;
    177       1.6       cgd 		l = min(l, uio->uio_resid);
    178       1.1       cgd 		if (l == 0)
    179       1.1       cgd 			break;
    180       1.1       cgd 		error = uiomove((caddr_t)&mbp->msg_bufc[mbp->msg_bufr],
    181       1.1       cgd 			(int)l, uio);
    182       1.1       cgd 		if (error)
    183       1.1       cgd 			break;
    184       1.1       cgd 		mbp->msg_bufr += l;
    185      1.13       leo 		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
    186       1.1       cgd 			mbp->msg_bufr = 0;
    187       1.1       cgd 	}
    188       1.1       cgd 	return (error);
    189       1.1       cgd }
    190       1.1       cgd 
    191       1.1       cgd /*ARGSUSED*/
    192       1.3    andrew int
    193      1.12   mycroft logpoll(dev, events, p)
    194       1.1       cgd 	dev_t dev;
    195      1.12   mycroft 	int events;
    196       1.1       cgd 	struct proc *p;
    197       1.1       cgd {
    198      1.12   mycroft 	int revents = 0;
    199       1.1       cgd 	int s = splhigh();
    200       1.1       cgd 
    201      1.17   thorpej 	if (events & (POLLIN | POLLRDNORM)) {
    202      1.12   mycroft 		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
    203      1.12   mycroft 			revents |= events & (POLLIN | POLLRDNORM);
    204      1.12   mycroft 		else
    205      1.12   mycroft 			selrecord(p, &logsoftc.sc_selp);
    206      1.17   thorpej 	}
    207       1.1       cgd 
    208       1.1       cgd 	splx(s);
    209      1.12   mycroft 	return (revents);
    210       1.1       cgd }
    211       1.1       cgd 
    212  1.20.6.1   thorpej static void
    213  1.20.6.1   thorpej filt_logrdetach(struct knote *kn)
    214  1.20.6.1   thorpej {
    215  1.20.6.1   thorpej 	int s;
    216  1.20.6.1   thorpej 
    217  1.20.6.1   thorpej 	s = splhigh();
    218  1.20.6.1   thorpej 	SLIST_REMOVE(&logsoftc.sc_selp.si_klist, kn, knote, kn_selnext);
    219  1.20.6.1   thorpej 	splx(s);
    220  1.20.6.1   thorpej }
    221  1.20.6.1   thorpej 
    222  1.20.6.1   thorpej static int
    223  1.20.6.1   thorpej filt_logread(struct knote *kn, long hint)
    224  1.20.6.1   thorpej {
    225  1.20.6.1   thorpej 
    226  1.20.6.1   thorpej 	if (msgbufp->msg_bufr == msgbufp->msg_bufx)
    227  1.20.6.1   thorpej 		return (0);
    228  1.20.6.1   thorpej 
    229  1.20.6.1   thorpej 	if (msgbufp->msg_bufr < msgbufp->msg_bufx)
    230  1.20.6.1   thorpej 		kn->kn_data = msgbufp->msg_bufx - msgbufp->msg_bufr;
    231  1.20.6.1   thorpej 	else
    232  1.20.6.1   thorpej 		kn->kn_data = (msgbufp->msg_bufs - msgbufp->msg_bufr) +
    233  1.20.6.1   thorpej 		    msgbufp->msg_bufx;
    234  1.20.6.1   thorpej 
    235  1.20.6.1   thorpej 	return (1);
    236  1.20.6.1   thorpej }
    237  1.20.6.1   thorpej 
    238  1.20.6.1   thorpej static const struct filterops logread_filtops =
    239  1.20.6.1   thorpej 	{ 1, NULL, filt_logrdetach, filt_logread };
    240  1.20.6.1   thorpej 
    241  1.20.6.1   thorpej int
    242  1.20.6.1   thorpej logkqfilter(dev_t dev, struct knote *kn)
    243  1.20.6.1   thorpej {
    244  1.20.6.1   thorpej 	struct klist *klist;
    245  1.20.6.1   thorpej 	int s;
    246  1.20.6.1   thorpej 
    247  1.20.6.1   thorpej 	switch (kn->kn_filter) {
    248  1.20.6.1   thorpej 	case EVFILT_READ:
    249  1.20.6.1   thorpej 		klist = &logsoftc.sc_selp.si_klist;
    250  1.20.6.1   thorpej 		kn->kn_fop = &logread_filtops;
    251  1.20.6.1   thorpej 		break;
    252  1.20.6.1   thorpej 
    253  1.20.6.1   thorpej 	default:
    254  1.20.6.1   thorpej 		return (1);
    255  1.20.6.1   thorpej 	}
    256  1.20.6.1   thorpej 
    257  1.20.6.1   thorpej 	kn->kn_hook = NULL;
    258  1.20.6.1   thorpej 
    259  1.20.6.1   thorpej 	s = splhigh();
    260  1.20.6.1   thorpej 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    261  1.20.6.1   thorpej 	splx(s);
    262  1.20.6.1   thorpej 
    263  1.20.6.1   thorpej 	return (0);
    264  1.20.6.1   thorpej }
    265  1.20.6.1   thorpej 
    266       1.3    andrew void
    267       1.1       cgd logwakeup()
    268       1.1       cgd {
    269       1.1       cgd 	struct proc *p;
    270       1.1       cgd 
    271       1.1       cgd 	if (!log_open)
    272       1.1       cgd 		return;
    273  1.20.6.1   thorpej 	selnotify(&logsoftc.sc_selp, 0);
    274       1.1       cgd 	if (logsoftc.sc_state & LOG_ASYNC) {
    275       1.1       cgd 		if (logsoftc.sc_pgid < 0)
    276       1.1       cgd 			gsignal(-logsoftc.sc_pgid, SIGIO);
    277      1.20     jhawk 		else if (logsoftc.sc_pgid > 0 &&
    278      1.20     jhawk 		    (p = pfind(logsoftc.sc_pgid)) != NULL)
    279       1.1       cgd 			psignal(p, SIGIO);
    280       1.1       cgd 	}
    281       1.1       cgd 	if (logsoftc.sc_state & LOG_RDWAIT) {
    282       1.1       cgd 		wakeup((caddr_t)msgbufp);
    283       1.1       cgd 		logsoftc.sc_state &= ~LOG_RDWAIT;
    284       1.1       cgd 	}
    285       1.1       cgd }
    286       1.1       cgd 
    287       1.1       cgd /*ARGSUSED*/
    288       1.3    andrew int
    289       1.9  christos logioctl(dev, com, data, flag, p)
    290       1.3    andrew 	dev_t dev;
    291       1.8       cgd 	u_long com;
    292       1.1       cgd 	caddr_t data;
    293       1.3    andrew 	int flag;
    294       1.9  christos 	struct proc *p;
    295       1.1       cgd {
    296       1.1       cgd 	long l;
    297       1.1       cgd 	int s;
    298       1.1       cgd 
    299       1.1       cgd 	switch (com) {
    300       1.1       cgd 
    301       1.1       cgd 	/* return number of characters immediately available */
    302       1.1       cgd 	case FIONREAD:
    303       1.1       cgd 		s = splhigh();
    304       1.1       cgd 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
    305       1.1       cgd 		splx(s);
    306       1.1       cgd 		if (l < 0)
    307      1.13       leo 			l += msgbufp->msg_bufs;
    308       1.5   deraadt 		*(int *)data = l;
    309       1.1       cgd 		break;
    310       1.1       cgd 
    311       1.1       cgd 	case FIONBIO:
    312       1.1       cgd 		break;
    313       1.1       cgd 
    314       1.1       cgd 	case FIOASYNC:
    315       1.1       cgd 		if (*(int *)data)
    316       1.1       cgd 			logsoftc.sc_state |= LOG_ASYNC;
    317       1.1       cgd 		else
    318       1.1       cgd 			logsoftc.sc_state &= ~LOG_ASYNC;
    319       1.1       cgd 		break;
    320       1.1       cgd 
    321       1.1       cgd 	case TIOCSPGRP:
    322       1.1       cgd 		logsoftc.sc_pgid = *(int *)data;
    323       1.1       cgd 		break;
    324       1.1       cgd 
    325       1.1       cgd 	case TIOCGPGRP:
    326       1.1       cgd 		*(int *)data = logsoftc.sc_pgid;
    327       1.1       cgd 		break;
    328       1.1       cgd 
    329       1.1       cgd 	default:
    330       1.1       cgd 		return (-1);
    331       1.1       cgd 	}
    332       1.1       cgd 	return (0);
    333       1.1       cgd }
    334