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