Home | History | Annotate | Line # | Download | only in kern
subr_log.c revision 1.65
      1  1.65       rin /*	$NetBSD: subr_log.c,v 1.65 2025/04/08 04:19:28 rin 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.65       rin __KERNEL_RCSID(0, "$NetBSD: subr_log.c,v 1.65 2025/04/08 04:19:28 rin 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.63  riastrad static 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.65       rin 		l = ulmin(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.64       rin 		error = uiomove(&mbp->msg_bufc[mbp->msg_bufr], 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.60   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.62   thorpej 	.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
    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.62   thorpej 		mutex_spin_enter(&log_lock);
    307  1.62   thorpej 		selrecord_knote(&log_selp, kn);
    308  1.62   thorpej 		mutex_spin_exit(&log_lock);
    309  1.24  jdolecek 		break;
    310  1.24  jdolecek 
    311  1.24  jdolecek 	default:
    312  1.44     pooka 		return (EINVAL);
    313  1.24  jdolecek 	}
    314  1.24  jdolecek 
    315  1.24  jdolecek 	return (0);
    316  1.24  jdolecek }
    317  1.24  jdolecek 
    318   1.3    andrew void
    319  1.35   thorpej logwakeup(void)
    320   1.1       cgd {
    321  1.42        ad 
    322  1.42        ad 	if (!cold && log_open) {
    323  1.42        ad 		mutex_spin_enter(&log_lock);
    324  1.48     rmind 		selnotify(&log_selp, 0, NOTE_SUBMIT);
    325  1.42        ad 		if (log_async)
    326  1.42        ad 			softint_schedule(log_sih);
    327  1.42        ad 		cv_broadcast(&log_cv);
    328  1.42        ad 		mutex_spin_exit(&log_lock);
    329   1.1       cgd 	}
    330   1.1       cgd }
    331   1.1       cgd 
    332  1.42        ad static void
    333  1.42        ad logsoftintr(void *cookie)
    334  1.42        ad {
    335  1.42        ad 	pid_t pid;
    336  1.42        ad 
    337  1.42        ad 	if ((pid = log_pgid) != 0)
    338  1.42        ad 		fownsignal(pid, SIGIO, 0, 0, NULL);
    339  1.42        ad }
    340  1.42        ad 
    341   1.1       cgd /*ARGSUSED*/
    342  1.35   thorpej static int
    343  1.40  christos logioctl(dev_t dev, u_long com, void *data, int flag, struct lwp *lwp)
    344   1.1       cgd {
    345   1.1       cgd 	long l;
    346   1.1       cgd 
    347   1.1       cgd 	switch (com) {
    348   1.1       cgd 
    349   1.1       cgd 	/* return number of characters immediately available */
    350   1.1       cgd 	case FIONREAD:
    351  1.42        ad 		mutex_spin_enter(&log_lock);
    352   1.1       cgd 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
    353   1.1       cgd 		if (l < 0)
    354  1.13       leo 			l += msgbufp->msg_bufs;
    355  1.42        ad 		mutex_spin_exit(&log_lock);
    356   1.5   deraadt 		*(int *)data = l;
    357   1.1       cgd 		break;
    358   1.1       cgd 
    359   1.1       cgd 	case FIONBIO:
    360   1.1       cgd 		break;
    361   1.1       cgd 
    362   1.1       cgd 	case FIOASYNC:
    363  1.42        ad 		/* No locking needed, 'thread private'. */
    364  1.42        ad 		log_async = (*((int *)data) != 0);
    365   1.1       cgd 		break;
    366   1.1       cgd 
    367   1.1       cgd 	case TIOCSPGRP:
    368  1.32  jdolecek 	case FIOSETOWN:
    369  1.49        ad 		return fsetown(&log_pgid, com, data);
    370   1.1       cgd 
    371   1.1       cgd 	case TIOCGPGRP:
    372  1.32  jdolecek 	case FIOGETOWN:
    373  1.49        ad 		return fgetown(log_pgid, com, data);
    374   1.1       cgd 
    375   1.1       cgd 	default:
    376  1.22     enami 		return (EPASSTHROUGH);
    377   1.1       cgd 	}
    378   1.1       cgd 	return (0);
    379   1.1       cgd }
    380  1.35   thorpej 
    381  1.57  christos static void
    382  1.57  christos logskip(struct kern_msgbuf *mbp)
    383  1.57  christos {
    384  1.57  christos 	/*
    385  1.57  christos 	 * Move forward read pointer to the next line
    386  1.57  christos 	 * in the buffer.  Note that the buffer is
    387  1.57  christos 	 * a ring buffer so we should reset msg_bufr
    388  1.57  christos 	 * to 0 when msg_bufr exceeds msg_bufs.
    389  1.57  christos 	 *
    390  1.57  christos 	 * To prevent to loop forever, give up if we
    391  1.57  christos 	 * cannot find a newline in mbp->msg_bufs
    392  1.57  christos 	 * characters (the max size of the buffer).
    393  1.57  christos 	 */
    394  1.57  christos 	for (int i = 0; i < mbp->msg_bufs; i++) {
    395  1.57  christos 		char c0 = mbp->msg_bufc[mbp->msg_bufr];
    396  1.57  christos 		if (++mbp->msg_bufr >= mbp->msg_bufs)
    397  1.57  christos 			mbp->msg_bufr = 0;
    398  1.57  christos 		if (c0 == '\n')
    399  1.57  christos 			break;
    400  1.57  christos 	}
    401  1.57  christos }
    402  1.57  christos 
    403  1.58  christos static void
    404  1.58  christos logaddchar(struct kern_msgbuf *mbp, int c)
    405  1.58  christos {
    406  1.58  christos 	mbp->msg_bufc[mbp->msg_bufx++] = c;
    407  1.58  christos 	if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
    408  1.58  christos 		mbp->msg_bufx = 0;
    409  1.58  christos 
    410  1.58  christos 	/* If the buffer is full, keep the most recent data. */
    411  1.58  christos 	if (mbp->msg_bufr == mbp->msg_bufx)
    412  1.58  christos 		logskip(mbp);
    413  1.58  christos }
    414  1.58  christos 
    415  1.42        ad void
    416  1.42        ad logputchar(int c)
    417  1.42        ad {
    418  1.42        ad 	struct kern_msgbuf *mbp;
    419  1.42        ad 
    420  1.42        ad 	if (!cold)
    421  1.42        ad 		mutex_spin_enter(&log_lock);
    422  1.57  christos 
    423  1.57  christos 	if (!msgbufenabled)
    424  1.57  christos 		goto out;
    425  1.57  christos 
    426  1.57  christos 	mbp = msgbufp;
    427  1.57  christos 	if (mbp->msg_magic != MSG_MAGIC) {
    428  1.57  christos 		/*
    429  1.57  christos 		 * Arguably should panic or somehow notify the
    430  1.57  christos 		 * user...  but how?  Panic may be too drastic,
    431  1.57  christos 		 * and would obliterate the message being kicked
    432  1.57  christos 		 * out (maybe a panic itself), and printf
    433  1.57  christos 		 * would invoke us recursively.  Silently punt
    434  1.57  christos 		 * for now.  If syslog is running, it should
    435  1.57  christos 		 * notice.
    436  1.57  christos 		 */
    437  1.57  christos 		msgbufenabled = 0;
    438  1.57  christos 		goto out;
    439  1.57  christos 
    440  1.42        ad 	}
    441  1.57  christos 
    442  1.58  christos 	logaddchar(mbp, c);
    443  1.57  christos 
    444  1.57  christos out:
    445  1.42        ad 	if (!cold)
    446  1.42        ad 		mutex_spin_exit(&log_lock);
    447  1.42        ad }
    448  1.42        ad 
    449  1.54     pooka /*
    450  1.54     pooka  * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
    451  1.54     pooka  * former it merely checks the message buffer is set up. For the latter,
    452  1.54     pooka  * it also copies out the data if necessary.
    453  1.54     pooka  */
    454  1.54     pooka static int
    455  1.54     pooka sysctl_msgbuf(SYSCTLFN_ARGS)
    456  1.54     pooka {
    457  1.54     pooka 	char *where = oldp;
    458  1.54     pooka 	size_t len, maxlen;
    459  1.54     pooka 	long beg, end;
    460  1.54     pooka 	int error;
    461  1.54     pooka 
    462  1.57  christos 	if (!logenabled(msgbufp)) {
    463  1.54     pooka 		msgbufenabled = 0;
    464  1.54     pooka 		return (ENXIO);
    465  1.54     pooka 	}
    466  1.54     pooka 
    467  1.54     pooka 	switch (rnode->sysctl_num) {
    468  1.54     pooka 	case KERN_MSGBUFSIZE: {
    469  1.54     pooka 		struct sysctlnode node = *rnode;
    470  1.54     pooka 		int msg_bufs = (int)msgbufp->msg_bufs;
    471  1.54     pooka 		node.sysctl_data = &msg_bufs;
    472  1.54     pooka 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    473  1.54     pooka 	}
    474  1.54     pooka 	case KERN_MSGBUF:
    475  1.54     pooka 		break;
    476  1.54     pooka 	default:
    477  1.54     pooka 		return (EOPNOTSUPP);
    478  1.54     pooka 	}
    479  1.54     pooka 
    480  1.54     pooka 	if (newp != NULL)
    481  1.54     pooka 		return (EPERM);
    482  1.54     pooka 
    483  1.54     pooka 	if (oldp == NULL) {
    484  1.54     pooka 		/* always return full buffer size */
    485  1.54     pooka 		*oldlenp = msgbufp->msg_bufs;
    486  1.54     pooka 		return (0);
    487  1.54     pooka 	}
    488  1.54     pooka 
    489  1.54     pooka 	sysctl_unlock();
    490  1.54     pooka 
    491  1.54     pooka 	/*
    492  1.54     pooka 	 * First, copy from the write pointer to the end of
    493  1.54     pooka 	 * message buffer.
    494  1.54     pooka 	 */
    495  1.54     pooka 	error = 0;
    496  1.54     pooka 	mutex_spin_enter(&log_lock);
    497  1.54     pooka 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
    498  1.54     pooka 	beg = msgbufp->msg_bufx;
    499  1.54     pooka 	end = msgbufp->msg_bufs;
    500  1.54     pooka 	mutex_spin_exit(&log_lock);
    501  1.54     pooka 
    502  1.54     pooka 	while (maxlen > 0) {
    503  1.54     pooka 		len = MIN(end - beg, maxlen);
    504  1.54     pooka 		if (len == 0)
    505  1.54     pooka 			break;
    506  1.54     pooka 		/* XXX unlocked, but hardly matters. */
    507  1.54     pooka 		error = copyout(&msgbufp->msg_bufc[beg], where, len);
    508  1.54     pooka 		ktrmibio(-1, UIO_READ, where, len, error);
    509  1.54     pooka 		if (error)
    510  1.54     pooka 			break;
    511  1.54     pooka 		where += len;
    512  1.54     pooka 		maxlen -= len;
    513  1.54     pooka 
    514  1.54     pooka 		/*
    515  1.54     pooka 		 * ... then, copy from the beginning of message buffer to
    516  1.54     pooka 		 * the write pointer.
    517  1.54     pooka 		 */
    518  1.54     pooka 		beg = 0;
    519  1.54     pooka 		end = msgbufp->msg_bufx;
    520  1.54     pooka 	}
    521  1.54     pooka 
    522  1.54     pooka 	sysctl_relock();
    523  1.54     pooka 	return (error);
    524  1.54     pooka }
    525  1.54     pooka 
    526  1.35   thorpej const struct cdevsw log_cdevsw = {
    527  1.51  dholland 	.d_open = logopen,
    528  1.51  dholland 	.d_close = logclose,
    529  1.51  dholland 	.d_read = logread,
    530  1.51  dholland 	.d_write = nowrite,
    531  1.51  dholland 	.d_ioctl = logioctl,
    532  1.51  dholland 	.d_stop = nostop,
    533  1.51  dholland 	.d_tty = notty,
    534  1.51  dholland 	.d_poll = logpoll,
    535  1.51  dholland 	.d_mmap = nommap,
    536  1.51  dholland 	.d_kqfilter = logkqfilter,
    537  1.53  dholland 	.d_discard = nodiscard,
    538  1.51  dholland 	.d_flag = D_OTHER | D_MPSAFE
    539  1.35   thorpej };
    540