Home | History | Annotate | Line # | Download | only in kern
subr_log.c revision 1.59.12.1
      1  1.59.12.1   thorpej /*	$NetBSD: subr_log.c,v 1.59.12.1 2020/12/14 14:38:14 thorpej 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.59.12.1   thorpej __KERNEL_RCSID(0, "$NetBSD: subr_log.c,v 1.59.12.1 2020/12/14 14:38:14 thorpej 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.54     pooka #include <sys/sysctl.h>
     84       1.54     pooka #include <sys/ktrace.h>
     85       1.54     pooka 
     86       1.54     pooka static int sysctl_msgbuf(SYSCTLFN_PROTO);
     87        1.1       cgd 
     88       1.42        ad static void	logsoftintr(void *);
     89        1.1       cgd 
     90       1.42        ad static bool	log_async;
     91       1.42        ad static struct selinfo log_selp;		/* process waiting on select call */
     92       1.42        ad static pid_t	log_pgid;		/* process/group for async I/O */
     93       1.42        ad static kcondvar_t log_cv;
     94       1.42        ad static void	*log_sih;
     95        1.1       cgd 
     96       1.47        ad kmutex_t log_lock;
     97        1.1       cgd int	log_open;			/* also used in log() */
     98       1.13       leo int	msgbufmapped;			/* is the message buffer mapped */
     99       1.13       leo int	msgbufenabled;			/* is logging to the buffer enabled */
    100       1.13       leo struct	kern_msgbuf *msgbufp;		/* the mapped buffer, itself. */
    101       1.23   gehenna 
    102       1.13       leo void
    103       1.35   thorpej initmsgbuf(void *bf, size_t bufsize)
    104       1.13       leo {
    105       1.18  augustss 	struct kern_msgbuf *mbp;
    106       1.13       leo 	long new_bufs;
    107       1.13       leo 
    108       1.13       leo 	/* Sanity-check the given size. */
    109       1.13       leo 	if (bufsize < sizeof(struct kern_msgbuf))
    110       1.13       leo 		return;
    111       1.13       leo 
    112       1.34  christos 	mbp = msgbufp = (struct kern_msgbuf *)bf;
    113       1.13       leo 
    114       1.13       leo 	new_bufs = bufsize - offsetof(struct kern_msgbuf, msg_bufc);
    115       1.13       leo 	if ((mbp->msg_magic != MSG_MAGIC) || (mbp->msg_bufs != new_bufs) ||
    116       1.13       leo 	    (mbp->msg_bufr < 0) || (mbp->msg_bufr >= mbp->msg_bufs) ||
    117       1.13       leo 	    (mbp->msg_bufx < 0) || (mbp->msg_bufx >= mbp->msg_bufs)) {
    118       1.13       leo 		/*
    119       1.13       leo 		 * If the buffer magic number is wrong, has changed
    120       1.13       leo 		 * size (which shouldn't happen often), or is
    121       1.13       leo 		 * internally inconsistent, initialize it.
    122       1.13       leo 		 */
    123       1.13       leo 
    124       1.34  christos 		memset(bf, 0, bufsize);
    125       1.13       leo 		mbp->msg_magic = MSG_MAGIC;
    126       1.13       leo 		mbp->msg_bufs = new_bufs;
    127       1.13       leo 	}
    128       1.13       leo 
    129       1.13       leo 	/* mark it as ready for use. */
    130       1.13       leo 	msgbufmapped = msgbufenabled = 1;
    131       1.13       leo }
    132        1.1       cgd 
    133       1.42        ad void
    134       1.42        ad loginit(void)
    135       1.42        ad {
    136       1.42        ad 
    137       1.43        ad 	mutex_init(&log_lock, MUTEX_DEFAULT, IPL_VM);
    138       1.42        ad 	selinit(&log_selp);
    139       1.42        ad 	cv_init(&log_cv, "klog");
    140       1.46        ad 	log_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    141       1.42        ad 	    logsoftintr, NULL);
    142       1.54     pooka 
    143       1.55     pooka 	sysctl_createv(NULL, 0, NULL, NULL,
    144       1.54     pooka 		       CTLFLAG_PERMANENT,
    145       1.54     pooka 		       CTLTYPE_INT, "msgbufsize",
    146       1.54     pooka 		       SYSCTL_DESCR("Size of the kernel message buffer"),
    147       1.54     pooka 		       sysctl_msgbuf, 0, NULL, 0,
    148       1.54     pooka 		       CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
    149       1.55     pooka 	sysctl_createv(NULL, 0, NULL, NULL,
    150       1.54     pooka 		       CTLFLAG_PERMANENT,
    151       1.54     pooka 		       CTLTYPE_INT, "msgbuf",
    152       1.54     pooka 		       SYSCTL_DESCR("Kernel message buffer"),
    153       1.54     pooka 		       sysctl_msgbuf, 0, NULL, 0,
    154       1.54     pooka 		       CTL_KERN, KERN_MSGBUF, CTL_EOL);
    155       1.42        ad }
    156       1.42        ad 
    157        1.1       cgd /*ARGSUSED*/
    158       1.35   thorpej static int
    159       1.39      yamt logopen(dev_t dev, int flags, int mode, struct lwp *l)
    160        1.1       cgd {
    161       1.18  augustss 	struct kern_msgbuf *mbp = msgbufp;
    162       1.42        ad 	int error = 0;
    163        1.1       cgd 
    164       1.42        ad 	mutex_spin_enter(&log_lock);
    165       1.42        ad 	if (log_open) {
    166       1.42        ad 		error = EBUSY;
    167       1.42        ad 	} else {
    168       1.42        ad 		log_open = 1;
    169       1.42        ad 		log_pgid = l->l_proc->p_pid;	/* signal process only */
    170       1.42        ad 		/*
    171       1.42        ad 		 * The message buffer is initialized during system
    172       1.42        ad 		 * configuration.  If it's been clobbered, note that
    173       1.42        ad 		 * and return an error.  (This allows a user to read
    174       1.42        ad 		 * the buffer via /dev/kmem, and try to figure out
    175       1.42        ad 		 * what clobbered it.
    176       1.42        ad 		 */
    177       1.42        ad 		if (mbp->msg_magic != MSG_MAGIC) {
    178       1.42        ad 			msgbufenabled = 0;
    179       1.42        ad 			error = ENXIO;
    180       1.42        ad 		}
    181       1.13       leo 	}
    182       1.42        ad 	mutex_spin_exit(&log_lock);
    183        1.1       cgd 
    184       1.42        ad 	return error;
    185        1.1       cgd }
    186        1.1       cgd 
    187        1.1       cgd /*ARGSUSED*/
    188       1.35   thorpej static int
    189       1.39      yamt logclose(dev_t dev, int flag, int mode, struct lwp *l)
    190        1.1       cgd {
    191        1.6       cgd 
    192       1.42        ad 	mutex_spin_enter(&log_lock);
    193       1.42        ad 	log_pgid = 0;
    194        1.1       cgd 	log_open = 0;
    195       1.42        ad 	log_async = 0;
    196       1.42        ad 	mutex_spin_exit(&log_lock);
    197       1.42        ad 
    198       1.42        ad 	return 0;
    199        1.1       cgd }
    200        1.1       cgd 
    201        1.1       cgd /*ARGSUSED*/
    202       1.35   thorpej static int
    203       1.39      yamt logread(dev_t dev, struct uio *uio, int flag)
    204        1.1       cgd {
    205       1.18  augustss 	struct kern_msgbuf *mbp = msgbufp;
    206       1.18  augustss 	long l;
    207        1.1       cgd 	int error = 0;
    208        1.1       cgd 
    209       1.42        ad 	mutex_spin_enter(&log_lock);
    210        1.1       cgd 	while (mbp->msg_bufr == mbp->msg_bufx) {
    211        1.1       cgd 		if (flag & IO_NDELAY) {
    212       1.42        ad 			mutex_spin_exit(&log_lock);
    213       1.42        ad 			return EWOULDBLOCK;
    214        1.1       cgd 		}
    215       1.42        ad 		error = cv_wait_sig(&log_cv, &log_lock);
    216        1.9  christos 		if (error) {
    217       1.42        ad 			mutex_spin_exit(&log_lock);
    218       1.42        ad 			return error;
    219        1.1       cgd 		}
    220        1.1       cgd 	}
    221        1.1       cgd 	while (uio->uio_resid > 0) {
    222        1.1       cgd 		l = mbp->msg_bufx - mbp->msg_bufr;
    223        1.1       cgd 		if (l < 0)
    224       1.13       leo 			l = mbp->msg_bufs - mbp->msg_bufr;
    225       1.59  riastrad 		l = uimin(l, uio->uio_resid);
    226        1.1       cgd 		if (l == 0)
    227        1.1       cgd 			break;
    228       1.42        ad 		mutex_spin_exit(&log_lock);
    229       1.42        ad 		error = uiomove(&mbp->msg_bufc[mbp->msg_bufr], (int)l, uio);
    230       1.42        ad 		mutex_spin_enter(&log_lock);
    231        1.1       cgd 		if (error)
    232        1.1       cgd 			break;
    233        1.1       cgd 		mbp->msg_bufr += l;
    234       1.13       leo 		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
    235        1.1       cgd 			mbp->msg_bufr = 0;
    236        1.1       cgd 	}
    237       1.42        ad 	mutex_spin_exit(&log_lock);
    238       1.42        ad 
    239       1.42        ad 	return error;
    240        1.1       cgd }
    241        1.1       cgd 
    242        1.1       cgd /*ARGSUSED*/
    243       1.35   thorpej static int
    244       1.39      yamt logpoll(dev_t dev, int events, struct lwp *l)
    245        1.1       cgd {
    246       1.12   mycroft 	int revents = 0;
    247        1.1       cgd 
    248       1.17   thorpej 	if (events & (POLLIN | POLLRDNORM)) {
    249       1.42        ad 		mutex_spin_enter(&log_lock);
    250       1.12   mycroft 		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
    251       1.12   mycroft 			revents |= events & (POLLIN | POLLRDNORM);
    252       1.12   mycroft 		else
    253       1.42        ad 			selrecord(l, &log_selp);
    254       1.42        ad 		mutex_spin_exit(&log_lock);
    255       1.17   thorpej 	}
    256        1.1       cgd 
    257       1.42        ad 	return revents;
    258        1.1       cgd }
    259        1.1       cgd 
    260       1.24  jdolecek static void
    261       1.24  jdolecek filt_logrdetach(struct knote *kn)
    262       1.24  jdolecek {
    263       1.24  jdolecek 
    264       1.42        ad 	mutex_spin_enter(&log_lock);
    265  1.59.12.1   thorpej 	selremove_knote(&log_selp, kn);
    266       1.42        ad 	mutex_spin_exit(&log_lock);
    267       1.24  jdolecek }
    268       1.24  jdolecek 
    269       1.24  jdolecek static int
    270       1.39      yamt filt_logread(struct knote *kn, long hint)
    271       1.24  jdolecek {
    272       1.42        ad 	int rv;
    273       1.24  jdolecek 
    274       1.42        ad 	if ((hint & NOTE_SUBMIT) == 0)
    275       1.42        ad 		mutex_spin_enter(&log_lock);
    276       1.42        ad 	if (msgbufp->msg_bufr == msgbufp->msg_bufx) {
    277       1.42        ad 		rv = 0;
    278       1.42        ad 	} else if (msgbufp->msg_bufr < msgbufp->msg_bufx) {
    279       1.24  jdolecek 		kn->kn_data = msgbufp->msg_bufx - msgbufp->msg_bufr;
    280       1.42        ad 		rv = 1;
    281       1.42        ad 	} else {
    282       1.24  jdolecek 		kn->kn_data = (msgbufp->msg_bufs - msgbufp->msg_bufr) +
    283       1.24  jdolecek 		    msgbufp->msg_bufx;
    284       1.42        ad 		rv = 1;
    285       1.42        ad 	}
    286       1.42        ad 	if ((hint & NOTE_SUBMIT) == 0)
    287       1.42        ad 		mutex_spin_exit(&log_lock);
    288       1.24  jdolecek 
    289       1.42        ad 	return rv;
    290       1.24  jdolecek }
    291       1.24  jdolecek 
    292       1.56      maya static const struct filterops logread_filtops = {
    293       1.56      maya 	.f_isfd = 1,
    294       1.56      maya 	.f_attach = NULL,
    295       1.56      maya 	.f_detach = filt_logrdetach,
    296       1.56      maya 	.f_event = filt_logread,
    297       1.56      maya };
    298       1.24  jdolecek 
    299       1.35   thorpej static int
    300       1.39      yamt logkqfilter(dev_t dev, struct knote *kn)
    301       1.24  jdolecek {
    302       1.24  jdolecek 
    303       1.24  jdolecek 	switch (kn->kn_filter) {
    304       1.24  jdolecek 	case EVFILT_READ:
    305       1.24  jdolecek 		kn->kn_fop = &logread_filtops;
    306       1.24  jdolecek 		break;
    307       1.24  jdolecek 
    308       1.24  jdolecek 	default:
    309       1.44     pooka 		return (EINVAL);
    310       1.24  jdolecek 	}
    311       1.24  jdolecek 
    312       1.24  jdolecek 	kn->kn_hook = NULL;
    313  1.59.12.1   thorpej 
    314  1.59.12.1   thorpej 	mutex_spin_enter(&log_lock);
    315  1.59.12.1   thorpej 	selrecord_knote(&log_selp, kn);
    316       1.42        ad 	mutex_spin_exit(&log_lock);
    317       1.24  jdolecek 
    318       1.24  jdolecek 	return (0);
    319       1.24  jdolecek }
    320       1.24  jdolecek 
    321        1.3    andrew void
    322       1.35   thorpej logwakeup(void)
    323        1.1       cgd {
    324       1.42        ad 
    325       1.42        ad 	if (!cold && log_open) {
    326       1.42        ad 		mutex_spin_enter(&log_lock);
    327       1.48     rmind 		selnotify(&log_selp, 0, NOTE_SUBMIT);
    328       1.42        ad 		if (log_async)
    329       1.42        ad 			softint_schedule(log_sih);
    330       1.42        ad 		cv_broadcast(&log_cv);
    331       1.42        ad 		mutex_spin_exit(&log_lock);
    332        1.1       cgd 	}
    333        1.1       cgd }
    334        1.1       cgd 
    335       1.42        ad static void
    336       1.42        ad logsoftintr(void *cookie)
    337       1.42        ad {
    338       1.42        ad 	pid_t pid;
    339       1.42        ad 
    340       1.42        ad 	if ((pid = log_pgid) != 0)
    341       1.42        ad 		fownsignal(pid, SIGIO, 0, 0, NULL);
    342       1.42        ad }
    343       1.42        ad 
    344        1.1       cgd /*ARGSUSED*/
    345       1.35   thorpej static int
    346       1.40  christos logioctl(dev_t dev, u_long com, void *data, int flag, struct lwp *lwp)
    347        1.1       cgd {
    348        1.1       cgd 	long l;
    349        1.1       cgd 
    350        1.1       cgd 	switch (com) {
    351        1.1       cgd 
    352        1.1       cgd 	/* return number of characters immediately available */
    353        1.1       cgd 	case FIONREAD:
    354       1.42        ad 		mutex_spin_enter(&log_lock);
    355        1.1       cgd 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
    356        1.1       cgd 		if (l < 0)
    357       1.13       leo 			l += msgbufp->msg_bufs;
    358       1.42        ad 		mutex_spin_exit(&log_lock);
    359        1.5   deraadt 		*(int *)data = l;
    360        1.1       cgd 		break;
    361        1.1       cgd 
    362        1.1       cgd 	case FIONBIO:
    363        1.1       cgd 		break;
    364        1.1       cgd 
    365        1.1       cgd 	case FIOASYNC:
    366       1.42        ad 		/* No locking needed, 'thread private'. */
    367       1.42        ad 		log_async = (*((int *)data) != 0);
    368        1.1       cgd 		break;
    369        1.1       cgd 
    370        1.1       cgd 	case TIOCSPGRP:
    371       1.32  jdolecek 	case FIOSETOWN:
    372       1.49        ad 		return fsetown(&log_pgid, com, data);
    373        1.1       cgd 
    374        1.1       cgd 	case TIOCGPGRP:
    375       1.32  jdolecek 	case FIOGETOWN:
    376       1.49        ad 		return fgetown(log_pgid, com, data);
    377        1.1       cgd 
    378        1.1       cgd 	default:
    379       1.22     enami 		return (EPASSTHROUGH);
    380        1.1       cgd 	}
    381        1.1       cgd 	return (0);
    382        1.1       cgd }
    383       1.35   thorpej 
    384       1.57  christos static void
    385       1.57  christos logskip(struct kern_msgbuf *mbp)
    386       1.57  christos {
    387       1.57  christos 	/*
    388       1.57  christos 	 * Move forward read pointer to the next line
    389       1.57  christos 	 * in the buffer.  Note that the buffer is
    390       1.57  christos 	 * a ring buffer so we should reset msg_bufr
    391       1.57  christos 	 * to 0 when msg_bufr exceeds msg_bufs.
    392       1.57  christos 	 *
    393       1.57  christos 	 * To prevent to loop forever, give up if we
    394       1.57  christos 	 * cannot find a newline in mbp->msg_bufs
    395       1.57  christos 	 * characters (the max size of the buffer).
    396       1.57  christos 	 */
    397       1.57  christos 	for (int i = 0; i < mbp->msg_bufs; i++) {
    398       1.57  christos 		char c0 = mbp->msg_bufc[mbp->msg_bufr];
    399       1.57  christos 		if (++mbp->msg_bufr >= mbp->msg_bufs)
    400       1.57  christos 			mbp->msg_bufr = 0;
    401       1.57  christos 		if (c0 == '\n')
    402       1.57  christos 			break;
    403       1.57  christos 	}
    404       1.57  christos }
    405       1.57  christos 
    406       1.58  christos static void
    407       1.58  christos logaddchar(struct kern_msgbuf *mbp, int c)
    408       1.58  christos {
    409       1.58  christos 	mbp->msg_bufc[mbp->msg_bufx++] = c;
    410       1.58  christos 	if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
    411       1.58  christos 		mbp->msg_bufx = 0;
    412       1.58  christos 
    413       1.58  christos 	/* If the buffer is full, keep the most recent data. */
    414       1.58  christos 	if (mbp->msg_bufr == mbp->msg_bufx)
    415       1.58  christos 		logskip(mbp);
    416       1.58  christos }
    417       1.58  christos 
    418       1.42        ad void
    419       1.42        ad logputchar(int c)
    420       1.42        ad {
    421       1.42        ad 	struct kern_msgbuf *mbp;
    422       1.42        ad 
    423       1.42        ad 	if (!cold)
    424       1.42        ad 		mutex_spin_enter(&log_lock);
    425       1.57  christos 
    426       1.57  christos 	if (!msgbufenabled)
    427       1.57  christos 		goto out;
    428       1.57  christos 
    429       1.57  christos 	mbp = msgbufp;
    430       1.57  christos 	if (mbp->msg_magic != MSG_MAGIC) {
    431       1.57  christos 		/*
    432       1.57  christos 		 * Arguably should panic or somehow notify the
    433       1.57  christos 		 * user...  but how?  Panic may be too drastic,
    434       1.57  christos 		 * and would obliterate the message being kicked
    435       1.57  christos 		 * out (maybe a panic itself), and printf
    436       1.57  christos 		 * would invoke us recursively.  Silently punt
    437       1.57  christos 		 * for now.  If syslog is running, it should
    438       1.57  christos 		 * notice.
    439       1.57  christos 		 */
    440       1.57  christos 		msgbufenabled = 0;
    441       1.57  christos 		goto out;
    442       1.57  christos 
    443       1.42        ad 	}
    444       1.57  christos 
    445       1.58  christos 	logaddchar(mbp, c);
    446       1.57  christos 
    447       1.57  christos out:
    448       1.42        ad 	if (!cold)
    449       1.42        ad 		mutex_spin_exit(&log_lock);
    450       1.42        ad }
    451       1.42        ad 
    452       1.54     pooka /*
    453       1.54     pooka  * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
    454       1.54     pooka  * former it merely checks the message buffer is set up. For the latter,
    455       1.54     pooka  * it also copies out the data if necessary.
    456       1.54     pooka  */
    457       1.54     pooka static int
    458       1.54     pooka sysctl_msgbuf(SYSCTLFN_ARGS)
    459       1.54     pooka {
    460       1.54     pooka 	char *where = oldp;
    461       1.54     pooka 	size_t len, maxlen;
    462       1.54     pooka 	long beg, end;
    463       1.54     pooka 	extern kmutex_t log_lock;
    464       1.54     pooka 	int error;
    465       1.54     pooka 
    466       1.57  christos 	if (!logenabled(msgbufp)) {
    467       1.54     pooka 		msgbufenabled = 0;
    468       1.54     pooka 		return (ENXIO);
    469       1.54     pooka 	}
    470       1.54     pooka 
    471       1.54     pooka 	switch (rnode->sysctl_num) {
    472       1.54     pooka 	case KERN_MSGBUFSIZE: {
    473       1.54     pooka 		struct sysctlnode node = *rnode;
    474       1.54     pooka 		int msg_bufs = (int)msgbufp->msg_bufs;
    475       1.54     pooka 		node.sysctl_data = &msg_bufs;
    476       1.54     pooka 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    477       1.54     pooka 	}
    478       1.54     pooka 	case KERN_MSGBUF:
    479       1.54     pooka 		break;
    480       1.54     pooka 	default:
    481       1.54     pooka 		return (EOPNOTSUPP);
    482       1.54     pooka 	}
    483       1.54     pooka 
    484       1.54     pooka 	if (newp != NULL)
    485       1.54     pooka 		return (EPERM);
    486       1.54     pooka 
    487       1.54     pooka 	if (oldp == NULL) {
    488       1.54     pooka 		/* always return full buffer size */
    489       1.54     pooka 		*oldlenp = msgbufp->msg_bufs;
    490       1.54     pooka 		return (0);
    491       1.54     pooka 	}
    492       1.54     pooka 
    493       1.54     pooka 	sysctl_unlock();
    494       1.54     pooka 
    495       1.54     pooka 	/*
    496       1.54     pooka 	 * First, copy from the write pointer to the end of
    497       1.54     pooka 	 * message buffer.
    498       1.54     pooka 	 */
    499       1.54     pooka 	error = 0;
    500       1.54     pooka 	mutex_spin_enter(&log_lock);
    501       1.54     pooka 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
    502       1.54     pooka 	beg = msgbufp->msg_bufx;
    503       1.54     pooka 	end = msgbufp->msg_bufs;
    504       1.54     pooka 	mutex_spin_exit(&log_lock);
    505       1.54     pooka 
    506       1.54     pooka 	while (maxlen > 0) {
    507       1.54     pooka 		len = MIN(end - beg, maxlen);
    508       1.54     pooka 		if (len == 0)
    509       1.54     pooka 			break;
    510       1.54     pooka 		/* XXX unlocked, but hardly matters. */
    511       1.54     pooka 		error = copyout(&msgbufp->msg_bufc[beg], where, len);
    512       1.54     pooka 		ktrmibio(-1, UIO_READ, where, len, error);
    513       1.54     pooka 		if (error)
    514       1.54     pooka 			break;
    515       1.54     pooka 		where += len;
    516       1.54     pooka 		maxlen -= len;
    517       1.54     pooka 
    518       1.54     pooka 		/*
    519       1.54     pooka 		 * ... then, copy from the beginning of message buffer to
    520       1.54     pooka 		 * the write pointer.
    521       1.54     pooka 		 */
    522       1.54     pooka 		beg = 0;
    523       1.54     pooka 		end = msgbufp->msg_bufx;
    524       1.54     pooka 	}
    525       1.54     pooka 
    526       1.54     pooka 	sysctl_relock();
    527       1.54     pooka 	return (error);
    528       1.54     pooka }
    529       1.54     pooka 
    530       1.35   thorpej const struct cdevsw log_cdevsw = {
    531       1.51  dholland 	.d_open = logopen,
    532       1.51  dholland 	.d_close = logclose,
    533       1.51  dholland 	.d_read = logread,
    534       1.51  dholland 	.d_write = nowrite,
    535       1.51  dholland 	.d_ioctl = logioctl,
    536       1.51  dholland 	.d_stop = nostop,
    537       1.51  dholland 	.d_tty = notty,
    538       1.51  dholland 	.d_poll = logpoll,
    539       1.51  dholland 	.d_mmap = nommap,
    540       1.51  dholland 	.d_kqfilter = logkqfilter,
    541       1.53  dholland 	.d_discard = nodiscard,
    542       1.51  dholland 	.d_flag = D_OTHER | D_MPSAFE
    543       1.35   thorpej };
    544