Home | History | Annotate | Line # | Download | only in kern
subr_log.c revision 1.43
      1  1.43        ad /*	$NetBSD: subr_log.c,v 1.43 2007/12/05 07:06:54 ad Exp $	*/
      2  1.42        ad 
      3  1.42        ad /*-
      4  1.42        ad  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.42        ad  * All rights reserved.
      6  1.42        ad  *
      7  1.42        ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.42        ad  * by Andrew Doran.
      9  1.42        ad  *
     10  1.42        ad  * Redistribution and use in source and binary forms, with or without
     11  1.42        ad  * modification, are permitted provided that the following conditions
     12  1.42        ad  * are met:
     13  1.42        ad  * 1. Redistributions of source code must retain the above copyright
     14  1.42        ad  *    notice, this list of conditions and the following disclaimer.
     15  1.42        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.42        ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.42        ad  *    documentation and/or other materials provided with the distribution.
     18  1.42        ad  * 3. All advertising materials mentioning features or use of this software
     19  1.42        ad  *    must display the following acknowledgement:
     20  1.42        ad  *	This product includes software developed by the NetBSD
     21  1.42        ad  *	Foundation, Inc. and its contributors.
     22  1.42        ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.42        ad  *    contributors may be used to endorse or promote products derived
     24  1.42        ad  *    from this software without specific prior written permission.
     25  1.42        ad  *
     26  1.42        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.42        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.42        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.42        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.42        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.42        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.42        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.42        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.42        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.42        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.42        ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.42        ad  */
     38   1.7       cgd 
     39   1.1       cgd /*
     40   1.6       cgd  * Copyright (c) 1982, 1986, 1993
     41   1.6       cgd  *	The Regents of the University of California.  All rights reserved.
     42   1.1       cgd  *
     43   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     44   1.1       cgd  * modification, are permitted provided that the following conditions
     45   1.1       cgd  * are met:
     46   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     47   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     48   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     50   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     51  1.29       agc  * 3. Neither the name of the University nor the names of its contributors
     52   1.1       cgd  *    may be used to endorse or promote products derived from this software
     53   1.1       cgd  *    without specific prior written permission.
     54   1.1       cgd  *
     55   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65   1.1       cgd  * SUCH DAMAGE.
     66   1.1       cgd  *
     67  1.15      fvdl  *	@(#)subr_log.c	8.3 (Berkeley) 2/14/95
     68   1.1       cgd  */
     69   1.1       cgd 
     70   1.1       cgd /*
     71   1.1       cgd  * Error log buffer for kernel printf's.
     72   1.1       cgd  */
     73  1.21     lukem 
     74  1.21     lukem #include <sys/cdefs.h>
     75  1.43        ad __KERNEL_RCSID(0, "$NetBSD: subr_log.c,v 1.43 2007/12/05 07:06:54 ad Exp $");
     76   1.1       cgd 
     77   1.4   mycroft #include <sys/param.h>
     78   1.4   mycroft #include <sys/systm.h>
     79  1.42        ad #include <sys/kernel.h>
     80   1.4   mycroft #include <sys/proc.h>
     81   1.4   mycroft #include <sys/vnode.h>
     82   1.4   mycroft #include <sys/ioctl.h>
     83   1.4   mycroft #include <sys/msgbuf.h>
     84   1.4   mycroft #include <sys/file.h>
     85  1.10  christos #include <sys/syslog.h>
     86  1.11  christos #include <sys/conf.h>
     87  1.12   mycroft #include <sys/select.h>
     88  1.42        ad #include <sys/poll.h>
     89  1.42        ad #include <sys/intr.h>
     90   1.1       cgd 
     91  1.42        ad static void	logsoftintr(void *);
     92   1.1       cgd 
     93  1.42        ad static bool	log_async;
     94  1.42        ad static struct selinfo log_selp;		/* process waiting on select call */
     95  1.42        ad static pid_t	log_pgid;		/* process/group for async I/O */
     96  1.42        ad static kcondvar_t log_cv;
     97  1.42        ad static kmutex_t log_lock;
     98  1.42        ad static void	*log_sih;
     99   1.1       cgd 
    100   1.1       cgd int	log_open;			/* also used in log() */
    101  1.13       leo int	msgbufmapped;			/* is the message buffer mapped */
    102  1.13       leo int	msgbufenabled;			/* is logging to the buffer enabled */
    103  1.13       leo struct	kern_msgbuf *msgbufp;		/* the mapped buffer, itself. */
    104  1.23   gehenna 
    105  1.13       leo void
    106  1.35   thorpej initmsgbuf(void *bf, size_t bufsize)
    107  1.13       leo {
    108  1.18  augustss 	struct kern_msgbuf *mbp;
    109  1.13       leo 	long new_bufs;
    110  1.13       leo 
    111  1.13       leo 	/* Sanity-check the given size. */
    112  1.13       leo 	if (bufsize < sizeof(struct kern_msgbuf))
    113  1.13       leo 		return;
    114  1.13       leo 
    115  1.34  christos 	mbp = msgbufp = (struct kern_msgbuf *)bf;
    116  1.13       leo 
    117  1.13       leo 	new_bufs = bufsize - offsetof(struct kern_msgbuf, msg_bufc);
    118  1.13       leo 	if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
    119  1.13       leo 	    (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
    120  1.13       leo 	    (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
    121  1.13       leo 		/*
    122  1.13       leo 		 * If the buffer magic number is wrong, has changed
    123  1.13       leo 		 * size (which shouldn't happen often), or is
    124  1.13       leo 		 * internally inconsistent, initialize it.
    125  1.13       leo 		 */
    126  1.13       leo 
    127  1.34  christos 		memset(bf, 0, bufsize);
    128  1.13       leo 		mbp->msg_magic = MSG_MAGIC;
    129  1.13       leo 		mbp->msg_bufs = new_bufs;
    130  1.13       leo 	}
    131  1.13       leo 
    132  1.13       leo 	/* mark it as ready for use. */
    133  1.13       leo 	msgbufmapped = msgbufenabled = 1;
    134  1.13       leo }
    135   1.1       cgd 
    136  1.42        ad void
    137  1.42        ad loginit(void)
    138  1.42        ad {
    139  1.42        ad 
    140  1.43        ad 	mutex_init(&log_lock, MUTEX_DEFAULT, IPL_VM);
    141  1.42        ad 	selinit(&log_selp);
    142  1.42        ad 	cv_init(&log_cv, "klog");
    143  1.42        ad 	log_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    144  1.42        ad 	    logsoftintr, NULL);
    145  1.42        ad }
    146  1.42        ad 
    147   1.1       cgd /*ARGSUSED*/
    148  1.35   thorpej static int
    149  1.39      yamt logopen(dev_t dev, int flags, int mode, struct lwp *l)
    150   1.1       cgd {
    151  1.18  augustss 	struct kern_msgbuf *mbp = msgbufp;
    152  1.42        ad 	int error = 0;
    153   1.1       cgd 
    154  1.42        ad 	mutex_spin_enter(&log_lock);
    155  1.42        ad 	if (log_open) {
    156  1.42        ad 		error = EBUSY;
    157  1.42        ad 	} else {
    158  1.42        ad 		log_open = 1;
    159  1.42        ad 		log_pgid = l->l_proc->p_pid;	/* signal process only */
    160  1.42        ad 		/*
    161  1.42        ad 		 * The message buffer is initialized during system
    162  1.42        ad 		 * configuration.  If it's been clobbered, note that
    163  1.42        ad 		 * and return an error.  (This allows a user to read
    164  1.42        ad 		 * the buffer via /dev/kmem, and try to figure out
    165  1.42        ad 		 * what clobbered it.
    166  1.42        ad 		 */
    167  1.42        ad 		if (mbp->msg_magic != MSG_MAGIC) {
    168  1.42        ad 			msgbufenabled = 0;
    169  1.42        ad 			error = ENXIO;
    170  1.42        ad 		}
    171  1.13       leo 	}
    172  1.42        ad 	mutex_spin_exit(&log_lock);
    173   1.1       cgd 
    174  1.42        ad 	return error;
    175   1.1       cgd }
    176   1.1       cgd 
    177   1.1       cgd /*ARGSUSED*/
    178  1.35   thorpej static int
    179  1.39      yamt logclose(dev_t dev, int flag, int mode, struct lwp *l)
    180   1.1       cgd {
    181   1.6       cgd 
    182  1.42        ad 	mutex_spin_enter(&log_lock);
    183  1.42        ad 	log_pgid = 0;
    184   1.1       cgd 	log_open = 0;
    185  1.42        ad 	log_async = 0;
    186  1.42        ad 	mutex_spin_exit(&log_lock);
    187  1.42        ad 
    188  1.42        ad 	return 0;
    189   1.1       cgd }
    190   1.1       cgd 
    191   1.1       cgd /*ARGSUSED*/
    192  1.35   thorpej static int
    193  1.39      yamt logread(dev_t dev, struct uio *uio, int flag)
    194   1.1       cgd {
    195  1.18  augustss 	struct kern_msgbuf *mbp = msgbufp;
    196  1.18  augustss 	long l;
    197   1.1       cgd 	int error = 0;
    198   1.1       cgd 
    199  1.42        ad 	mutex_spin_enter(&log_lock);
    200   1.1       cgd 	while (mbp->msg_bufr == mbp->msg_bufx) {
    201   1.1       cgd 		if (flag & IO_NDELAY) {
    202  1.42        ad 			mutex_spin_exit(&log_lock);
    203  1.42        ad 			return EWOULDBLOCK;
    204   1.1       cgd 		}
    205  1.42        ad 		error = cv_wait_sig(&log_cv, &log_lock);
    206   1.9  christos 		if (error) {
    207  1.42        ad 			mutex_spin_exit(&log_lock);
    208  1.42        ad 			return error;
    209   1.1       cgd 		}
    210   1.1       cgd 	}
    211   1.1       cgd 	while (uio->uio_resid > 0) {
    212   1.1       cgd 		l = mbp->msg_bufx - mbp->msg_bufr;
    213   1.1       cgd 		if (l < 0)
    214  1.13       leo 			l = mbp->msg_bufs - mbp->msg_bufr;
    215   1.6       cgd 		l = min(l, uio->uio_resid);
    216   1.1       cgd 		if (l == 0)
    217   1.1       cgd 			break;
    218  1.42        ad 		mutex_spin_exit(&log_lock);
    219  1.42        ad 		error = uiomove(&mbp->msg_bufc[mbp->msg_bufr], (int)l, uio);
    220  1.42        ad 		mutex_spin_enter(&log_lock);
    221   1.1       cgd 		if (error)
    222   1.1       cgd 			break;
    223   1.1       cgd 		mbp->msg_bufr += l;
    224  1.13       leo 		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
    225   1.1       cgd 			mbp->msg_bufr = 0;
    226   1.1       cgd 	}
    227  1.42        ad 	mutex_spin_exit(&log_lock);
    228  1.42        ad 
    229  1.42        ad 	return error;
    230   1.1       cgd }
    231   1.1       cgd 
    232   1.1       cgd /*ARGSUSED*/
    233  1.35   thorpej static int
    234  1.39      yamt logpoll(dev_t dev, int events, struct lwp *l)
    235   1.1       cgd {
    236  1.12   mycroft 	int revents = 0;
    237   1.1       cgd 
    238  1.17   thorpej 	if (events & (POLLIN | POLLRDNORM)) {
    239  1.42        ad 		mutex_spin_enter(&log_lock);
    240  1.12   mycroft 		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
    241  1.12   mycroft 			revents |= events & (POLLIN | POLLRDNORM);
    242  1.12   mycroft 		else
    243  1.42        ad 			selrecord(l, &log_selp);
    244  1.42        ad 		mutex_spin_exit(&log_lock);
    245  1.17   thorpej 	}
    246   1.1       cgd 
    247  1.42        ad 	return revents;
    248   1.1       cgd }
    249   1.1       cgd 
    250  1.24  jdolecek static void
    251  1.24  jdolecek filt_logrdetach(struct knote *kn)
    252  1.24  jdolecek {
    253  1.24  jdolecek 
    254  1.42        ad 	mutex_spin_enter(&log_lock);
    255  1.42        ad 	SLIST_REMOVE(&log_selp.sel_klist, kn, knote, kn_selnext);
    256  1.42        ad 	mutex_spin_exit(&log_lock);
    257  1.24  jdolecek }
    258  1.24  jdolecek 
    259  1.24  jdolecek static int
    260  1.39      yamt filt_logread(struct knote *kn, long hint)
    261  1.24  jdolecek {
    262  1.42        ad 	int rv;
    263  1.24  jdolecek 
    264  1.42        ad 	if ((hint & NOTE_SUBMIT) == 0)
    265  1.42        ad 		mutex_spin_enter(&log_lock);
    266  1.42        ad 	if (msgbufp->msg_bufr == msgbufp->msg_bufx) {
    267  1.42        ad 		rv = 0;
    268  1.42        ad 	} else if (msgbufp->msg_bufr < msgbufp->msg_bufx) {
    269  1.24  jdolecek 		kn->kn_data = msgbufp->msg_bufx - msgbufp->msg_bufr;
    270  1.42        ad 		rv = 1;
    271  1.42        ad 	} else {
    272  1.24  jdolecek 		kn->kn_data = (msgbufp->msg_bufs - msgbufp->msg_bufr) +
    273  1.24  jdolecek 		    msgbufp->msg_bufx;
    274  1.42        ad 		rv = 1;
    275  1.42        ad 	}
    276  1.42        ad 	if ((hint & NOTE_SUBMIT) == 0)
    277  1.42        ad 		mutex_spin_exit(&log_lock);
    278  1.24  jdolecek 
    279  1.42        ad 	return rv;
    280  1.24  jdolecek }
    281  1.24  jdolecek 
    282  1.24  jdolecek static const struct filterops logread_filtops =
    283  1.24  jdolecek 	{ 1, NULL, filt_logrdetach, filt_logread };
    284  1.24  jdolecek 
    285  1.35   thorpej static int
    286  1.39      yamt logkqfilter(dev_t dev, struct knote *kn)
    287  1.24  jdolecek {
    288  1.24  jdolecek 	struct klist *klist;
    289  1.24  jdolecek 
    290  1.24  jdolecek 	switch (kn->kn_filter) {
    291  1.24  jdolecek 	case EVFILT_READ:
    292  1.42        ad 		klist = &log_selp.sel_klist;
    293  1.24  jdolecek 		kn->kn_fop = &logread_filtops;
    294  1.24  jdolecek 		break;
    295  1.24  jdolecek 
    296  1.24  jdolecek 	default:
    297  1.24  jdolecek 		return (1);
    298  1.24  jdolecek 	}
    299  1.24  jdolecek 
    300  1.42        ad 	mutex_spin_enter(&log_lock);
    301  1.24  jdolecek 	kn->kn_hook = NULL;
    302  1.24  jdolecek 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    303  1.42        ad 	mutex_spin_exit(&log_lock);
    304  1.24  jdolecek 
    305  1.24  jdolecek 	return (0);
    306  1.24  jdolecek }
    307  1.24  jdolecek 
    308   1.3    andrew void
    309  1.35   thorpej logwakeup(void)
    310   1.1       cgd {
    311  1.42        ad 
    312  1.42        ad 	if (!cold && log_open) {
    313  1.42        ad 		mutex_spin_enter(&log_lock);
    314  1.42        ad 		selnotify(&log_selp, NOTE_SUBMIT);
    315  1.42        ad 		if (log_async)
    316  1.42        ad 			softint_schedule(log_sih);
    317  1.42        ad 		cv_broadcast(&log_cv);
    318  1.42        ad 		mutex_spin_exit(&log_lock);
    319   1.1       cgd 	}
    320   1.1       cgd }
    321   1.1       cgd 
    322  1.42        ad static void
    323  1.42        ad logsoftintr(void *cookie)
    324  1.42        ad {
    325  1.42        ad 	pid_t pid;
    326  1.42        ad 
    327  1.42        ad 	if ((pid = log_pgid) != 0)
    328  1.42        ad 		fownsignal(pid, SIGIO, 0, 0, NULL);
    329  1.42        ad }
    330  1.42        ad 
    331   1.1       cgd /*ARGSUSED*/
    332  1.35   thorpej static int
    333  1.40  christos logioctl(dev_t dev, u_long com, void *data, int flag, struct lwp *lwp)
    334   1.1       cgd {
    335  1.36  christos 	struct proc *p = lwp->l_proc;
    336   1.1       cgd 	long l;
    337   1.1       cgd 
    338   1.1       cgd 	switch (com) {
    339   1.1       cgd 
    340   1.1       cgd 	/* return number of characters immediately available */
    341   1.1       cgd 	case FIONREAD:
    342  1.42        ad 		mutex_spin_enter(&log_lock);
    343   1.1       cgd 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
    344   1.1       cgd 		if (l < 0)
    345  1.13       leo 			l += msgbufp->msg_bufs;
    346  1.42        ad 		mutex_spin_exit(&log_lock);
    347   1.5   deraadt 		*(int *)data = l;
    348   1.1       cgd 		break;
    349   1.1       cgd 
    350   1.1       cgd 	case FIONBIO:
    351   1.1       cgd 		break;
    352   1.1       cgd 
    353   1.1       cgd 	case FIOASYNC:
    354  1.42        ad 		/* No locking needed, 'thread private'. */
    355  1.42        ad 		log_async = (*((int *)data) != 0);
    356   1.1       cgd 		break;
    357   1.1       cgd 
    358   1.1       cgd 	case TIOCSPGRP:
    359  1.32  jdolecek 	case FIOSETOWN:
    360  1.42        ad 		return fsetown(p, &log_pgid, com, data);
    361   1.1       cgd 
    362   1.1       cgd 	case TIOCGPGRP:
    363  1.32  jdolecek 	case FIOGETOWN:
    364  1.42        ad 		return fgetown(p, log_pgid, com, data);
    365   1.1       cgd 
    366   1.1       cgd 	default:
    367  1.22     enami 		return (EPASSTHROUGH);
    368   1.1       cgd 	}
    369   1.1       cgd 	return (0);
    370   1.1       cgd }
    371  1.35   thorpej 
    372  1.42        ad void
    373  1.42        ad logputchar(int c)
    374  1.42        ad {
    375  1.42        ad 	struct kern_msgbuf *mbp;
    376  1.42        ad 
    377  1.42        ad 	if (!cold)
    378  1.42        ad 		mutex_spin_enter(&log_lock);
    379  1.42        ad 	if (msgbufenabled) {
    380  1.42        ad 		mbp = msgbufp;
    381  1.42        ad 		if (mbp->msg_magic != MSG_MAGIC) {
    382  1.42        ad 			/*
    383  1.42        ad 			 * Arguably should panic or somehow notify the
    384  1.42        ad 			 * user...  but how?  Panic may be too drastic,
    385  1.42        ad 			 * and would obliterate the message being kicked
    386  1.42        ad 			 * out (maybe a panic itself), and printf
    387  1.42        ad 			 * would invoke us recursively.  Silently punt
    388  1.42        ad 			 * for now.  If syslog is running, it should
    389  1.42        ad 			 * notice.
    390  1.42        ad 			 */
    391  1.42        ad 			msgbufenabled = 0;
    392  1.42        ad 		} else {
    393  1.42        ad 			mbp->msg_bufc[mbp->msg_bufx++] = c;
    394  1.42        ad 			if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
    395  1.42        ad 				mbp->msg_bufx = 0;
    396  1.42        ad 			/* If the buffer is full, keep the most recent data. */
    397  1.42        ad 			if (mbp->msg_bufr == mbp->msg_bufx) {
    398  1.42        ad 				 if (++mbp->msg_bufr >= mbp->msg_bufs)
    399  1.42        ad 					mbp->msg_bufr = 0;
    400  1.42        ad 			}
    401  1.42        ad 		}
    402  1.42        ad 	}
    403  1.42        ad 	if (!cold)
    404  1.42        ad 		mutex_spin_exit(&log_lock);
    405  1.42        ad }
    406  1.42        ad 
    407  1.35   thorpej const struct cdevsw log_cdevsw = {
    408  1.35   thorpej 	logopen, logclose, logread, nowrite, logioctl,
    409  1.42        ad 	nostop, notty, logpoll, nommap, logkqfilter,
    410  1.42        ad 	D_OTHER | D_MPSAFE
    411  1.35   thorpej };
    412