Home | History | Annotate | Line # | Download | only in kern
subr_log.c revision 1.63.8.1
      1  1.63.8.1  perseant /*	$NetBSD: subr_log.c,v 1.63.8.1 2025/08/02 05:57:42 perseant 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.63.8.1  perseant __KERNEL_RCSID(0, "$NetBSD: subr_log.c,v 1.63.8.1 2025/08/02 05:57:42 perseant 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.63.8.1  perseant 		char buf[128]; /* taken from FreeBSD */
    223  1.63.8.1  perseant 
    224       1.1       cgd 		l = mbp->msg_bufx - mbp->msg_bufr;
    225       1.1       cgd 		if (l < 0)
    226      1.13       leo 			l = mbp->msg_bufs - mbp->msg_bufr;
    227  1.63.8.1  perseant 		l = ulmin(l, uio->uio_resid);
    228       1.1       cgd 		if (l == 0)
    229       1.1       cgd 			break;
    230  1.63.8.1  perseant 
    231  1.63.8.1  perseant 		l = ulmin(l, sizeof(buf));
    232  1.63.8.1  perseant 		memcpy(buf, &mbp->msg_bufc[mbp->msg_bufr], l);
    233  1.63.8.1  perseant 		mbp->msg_bufr += l;
    234  1.63.8.1  perseant 		if (mbp->msg_bufr < 0 || mbp->msg_bufr >= mbp->msg_bufs)
    235  1.63.8.1  perseant 			mbp->msg_bufr = 0;
    236      1.42        ad 		mutex_spin_exit(&log_lock);
    237  1.63.8.1  perseant 		error = uiomove(buf, l, uio);
    238      1.42        ad 		mutex_spin_enter(&log_lock);
    239       1.1       cgd 		if (error)
    240       1.1       cgd 			break;
    241       1.1       cgd 	}
    242      1.42        ad 	mutex_spin_exit(&log_lock);
    243      1.42        ad 
    244      1.42        ad 	return error;
    245       1.1       cgd }
    246       1.1       cgd 
    247       1.1       cgd /*ARGSUSED*/
    248      1.35   thorpej static int
    249      1.39      yamt logpoll(dev_t dev, int events, struct lwp *l)
    250       1.1       cgd {
    251      1.12   mycroft 	int revents = 0;
    252       1.1       cgd 
    253      1.17   thorpej 	if (events & (POLLIN | POLLRDNORM)) {
    254      1.42        ad 		mutex_spin_enter(&log_lock);
    255      1.12   mycroft 		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
    256      1.12   mycroft 			revents |= events & (POLLIN | POLLRDNORM);
    257      1.12   mycroft 		else
    258      1.42        ad 			selrecord(l, &log_selp);
    259      1.42        ad 		mutex_spin_exit(&log_lock);
    260      1.17   thorpej 	}
    261       1.1       cgd 
    262      1.42        ad 	return revents;
    263       1.1       cgd }
    264       1.1       cgd 
    265      1.24  jdolecek static void
    266      1.24  jdolecek filt_logrdetach(struct knote *kn)
    267      1.24  jdolecek {
    268      1.24  jdolecek 
    269      1.42        ad 	mutex_spin_enter(&log_lock);
    270      1.60   thorpej 	selremove_knote(&log_selp, kn);
    271      1.42        ad 	mutex_spin_exit(&log_lock);
    272      1.24  jdolecek }
    273      1.24  jdolecek 
    274      1.24  jdolecek static int
    275      1.39      yamt filt_logread(struct knote *kn, long hint)
    276      1.24  jdolecek {
    277      1.42        ad 	int rv;
    278      1.24  jdolecek 
    279      1.42        ad 	if ((hint & NOTE_SUBMIT) == 0)
    280      1.42        ad 		mutex_spin_enter(&log_lock);
    281      1.42        ad 	if (msgbufp->msg_bufr == msgbufp->msg_bufx) {
    282      1.42        ad 		rv = 0;
    283      1.42        ad 	} else if (msgbufp->msg_bufr < msgbufp->msg_bufx) {
    284      1.24  jdolecek 		kn->kn_data = msgbufp->msg_bufx - msgbufp->msg_bufr;
    285      1.42        ad 		rv = 1;
    286      1.42        ad 	} else {
    287      1.24  jdolecek 		kn->kn_data = (msgbufp->msg_bufs - msgbufp->msg_bufr) +
    288      1.24  jdolecek 		    msgbufp->msg_bufx;
    289      1.42        ad 		rv = 1;
    290      1.42        ad 	}
    291      1.42        ad 	if ((hint & NOTE_SUBMIT) == 0)
    292      1.42        ad 		mutex_spin_exit(&log_lock);
    293      1.24  jdolecek 
    294      1.42        ad 	return rv;
    295      1.24  jdolecek }
    296      1.24  jdolecek 
    297      1.56      maya static const struct filterops logread_filtops = {
    298      1.62   thorpej 	.f_flags = FILTEROP_ISFD | FILTEROP_MPSAFE,
    299      1.56      maya 	.f_attach = NULL,
    300      1.56      maya 	.f_detach = filt_logrdetach,
    301      1.56      maya 	.f_event = filt_logread,
    302      1.56      maya };
    303      1.24  jdolecek 
    304      1.35   thorpej static int
    305      1.39      yamt logkqfilter(dev_t dev, struct knote *kn)
    306      1.24  jdolecek {
    307      1.24  jdolecek 
    308      1.24  jdolecek 	switch (kn->kn_filter) {
    309      1.24  jdolecek 	case EVFILT_READ:
    310      1.24  jdolecek 		kn->kn_fop = &logread_filtops;
    311      1.62   thorpej 		mutex_spin_enter(&log_lock);
    312      1.62   thorpej 		selrecord_knote(&log_selp, kn);
    313      1.62   thorpej 		mutex_spin_exit(&log_lock);
    314      1.24  jdolecek 		break;
    315      1.24  jdolecek 
    316      1.24  jdolecek 	default:
    317      1.44     pooka 		return (EINVAL);
    318      1.24  jdolecek 	}
    319      1.24  jdolecek 
    320      1.24  jdolecek 	return (0);
    321      1.24  jdolecek }
    322      1.24  jdolecek 
    323       1.3    andrew void
    324      1.35   thorpej logwakeup(void)
    325       1.1       cgd {
    326      1.42        ad 
    327      1.42        ad 	if (!cold && log_open) {
    328      1.42        ad 		mutex_spin_enter(&log_lock);
    329      1.48     rmind 		selnotify(&log_selp, 0, NOTE_SUBMIT);
    330      1.42        ad 		if (log_async)
    331      1.42        ad 			softint_schedule(log_sih);
    332      1.42        ad 		cv_broadcast(&log_cv);
    333      1.42        ad 		mutex_spin_exit(&log_lock);
    334       1.1       cgd 	}
    335       1.1       cgd }
    336       1.1       cgd 
    337      1.42        ad static void
    338      1.42        ad logsoftintr(void *cookie)
    339      1.42        ad {
    340      1.42        ad 	pid_t pid;
    341      1.42        ad 
    342      1.42        ad 	if ((pid = log_pgid) != 0)
    343      1.42        ad 		fownsignal(pid, SIGIO, 0, 0, NULL);
    344      1.42        ad }
    345      1.42        ad 
    346       1.1       cgd /*ARGSUSED*/
    347      1.35   thorpej static int
    348      1.40  christos logioctl(dev_t dev, u_long com, void *data, int flag, struct lwp *lwp)
    349       1.1       cgd {
    350       1.1       cgd 	long l;
    351       1.1       cgd 
    352       1.1       cgd 	switch (com) {
    353       1.1       cgd 
    354       1.1       cgd 	/* return number of characters immediately available */
    355       1.1       cgd 	case FIONREAD:
    356      1.42        ad 		mutex_spin_enter(&log_lock);
    357       1.1       cgd 		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
    358       1.1       cgd 		if (l < 0)
    359      1.13       leo 			l += msgbufp->msg_bufs;
    360      1.42        ad 		mutex_spin_exit(&log_lock);
    361       1.5   deraadt 		*(int *)data = l;
    362       1.1       cgd 		break;
    363       1.1       cgd 
    364       1.1       cgd 	case FIONBIO:
    365       1.1       cgd 		break;
    366       1.1       cgd 
    367       1.1       cgd 	case FIOASYNC:
    368      1.42        ad 		/* No locking needed, 'thread private'. */
    369      1.42        ad 		log_async = (*((int *)data) != 0);
    370       1.1       cgd 		break;
    371       1.1       cgd 
    372       1.1       cgd 	case TIOCSPGRP:
    373      1.32  jdolecek 	case FIOSETOWN:
    374      1.49        ad 		return fsetown(&log_pgid, com, data);
    375       1.1       cgd 
    376       1.1       cgd 	case TIOCGPGRP:
    377      1.32  jdolecek 	case FIOGETOWN:
    378      1.49        ad 		return fgetown(log_pgid, com, data);
    379       1.1       cgd 
    380       1.1       cgd 	default:
    381      1.22     enami 		return (EPASSTHROUGH);
    382       1.1       cgd 	}
    383       1.1       cgd 	return (0);
    384       1.1       cgd }
    385      1.35   thorpej 
    386      1.57  christos static void
    387      1.57  christos logskip(struct kern_msgbuf *mbp)
    388      1.57  christos {
    389      1.57  christos 	/*
    390      1.57  christos 	 * Move forward read pointer to the next line
    391      1.57  christos 	 * in the buffer.  Note that the buffer is
    392      1.57  christos 	 * a ring buffer so we should reset msg_bufr
    393      1.57  christos 	 * to 0 when msg_bufr exceeds msg_bufs.
    394      1.57  christos 	 *
    395      1.57  christos 	 * To prevent to loop forever, give up if we
    396      1.57  christos 	 * cannot find a newline in mbp->msg_bufs
    397      1.57  christos 	 * characters (the max size of the buffer).
    398      1.57  christos 	 */
    399      1.57  christos 	for (int i = 0; i < mbp->msg_bufs; i++) {
    400      1.57  christos 		char c0 = mbp->msg_bufc[mbp->msg_bufr];
    401      1.57  christos 		if (++mbp->msg_bufr >= mbp->msg_bufs)
    402      1.57  christos 			mbp->msg_bufr = 0;
    403      1.57  christos 		if (c0 == '\n')
    404      1.57  christos 			break;
    405      1.57  christos 	}
    406      1.57  christos }
    407      1.57  christos 
    408      1.58  christos static void
    409      1.58  christos logaddchar(struct kern_msgbuf *mbp, int c)
    410      1.58  christos {
    411      1.58  christos 	mbp->msg_bufc[mbp->msg_bufx++] = c;
    412      1.58  christos 	if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
    413      1.58  christos 		mbp->msg_bufx = 0;
    414      1.58  christos 
    415      1.58  christos 	/* If the buffer is full, keep the most recent data. */
    416      1.58  christos 	if (mbp->msg_bufr == mbp->msg_bufx)
    417      1.58  christos 		logskip(mbp);
    418      1.58  christos }
    419      1.58  christos 
    420      1.42        ad void
    421      1.42        ad logputchar(int c)
    422      1.42        ad {
    423      1.42        ad 	struct kern_msgbuf *mbp;
    424      1.42        ad 
    425      1.42        ad 	if (!cold)
    426      1.42        ad 		mutex_spin_enter(&log_lock);
    427      1.57  christos 
    428      1.57  christos 	if (!msgbufenabled)
    429      1.57  christos 		goto out;
    430      1.57  christos 
    431      1.57  christos 	mbp = msgbufp;
    432      1.57  christos 	if (mbp->msg_magic != MSG_MAGIC) {
    433      1.57  christos 		/*
    434      1.57  christos 		 * Arguably should panic or somehow notify the
    435      1.57  christos 		 * user...  but how?  Panic may be too drastic,
    436      1.57  christos 		 * and would obliterate the message being kicked
    437      1.57  christos 		 * out (maybe a panic itself), and printf
    438      1.57  christos 		 * would invoke us recursively.  Silently punt
    439      1.57  christos 		 * for now.  If syslog is running, it should
    440      1.57  christos 		 * notice.
    441      1.57  christos 		 */
    442      1.57  christos 		msgbufenabled = 0;
    443      1.57  christos 		goto out;
    444      1.57  christos 
    445      1.42        ad 	}
    446      1.57  christos 
    447      1.58  christos 	logaddchar(mbp, c);
    448      1.57  christos 
    449      1.57  christos out:
    450      1.42        ad 	if (!cold)
    451      1.42        ad 		mutex_spin_exit(&log_lock);
    452      1.42        ad }
    453      1.42        ad 
    454      1.54     pooka /*
    455      1.54     pooka  * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
    456      1.54     pooka  * former it merely checks the message buffer is set up. For the latter,
    457      1.54     pooka  * it also copies out the data if necessary.
    458      1.54     pooka  */
    459      1.54     pooka static int
    460      1.54     pooka sysctl_msgbuf(SYSCTLFN_ARGS)
    461      1.54     pooka {
    462      1.54     pooka 	char *where = oldp;
    463      1.54     pooka 	size_t len, maxlen;
    464      1.54     pooka 	long beg, end;
    465      1.54     pooka 	int error;
    466      1.54     pooka 
    467      1.57  christos 	if (!logenabled(msgbufp)) {
    468      1.54     pooka 		msgbufenabled = 0;
    469      1.54     pooka 		return (ENXIO);
    470      1.54     pooka 	}
    471      1.54     pooka 
    472      1.54     pooka 	switch (rnode->sysctl_num) {
    473      1.54     pooka 	case KERN_MSGBUFSIZE: {
    474      1.54     pooka 		struct sysctlnode node = *rnode;
    475      1.54     pooka 		int msg_bufs = (int)msgbufp->msg_bufs;
    476      1.54     pooka 		node.sysctl_data = &msg_bufs;
    477      1.54     pooka 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    478      1.54     pooka 	}
    479      1.54     pooka 	case KERN_MSGBUF:
    480      1.54     pooka 		break;
    481      1.54     pooka 	default:
    482      1.54     pooka 		return (EOPNOTSUPP);
    483      1.54     pooka 	}
    484      1.54     pooka 
    485      1.54     pooka 	if (newp != NULL)
    486      1.54     pooka 		return (EPERM);
    487      1.54     pooka 
    488      1.54     pooka 	if (oldp == NULL) {
    489      1.54     pooka 		/* always return full buffer size */
    490      1.54     pooka 		*oldlenp = msgbufp->msg_bufs;
    491      1.54     pooka 		return (0);
    492      1.54     pooka 	}
    493      1.54     pooka 
    494      1.54     pooka 	sysctl_unlock();
    495      1.54     pooka 
    496      1.54     pooka 	/*
    497      1.54     pooka 	 * First, copy from the write pointer to the end of
    498      1.54     pooka 	 * message buffer.
    499      1.54     pooka 	 */
    500      1.54     pooka 	error = 0;
    501      1.54     pooka 	mutex_spin_enter(&log_lock);
    502      1.54     pooka 	maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
    503      1.54     pooka 	beg = msgbufp->msg_bufx;
    504      1.54     pooka 	end = msgbufp->msg_bufs;
    505      1.54     pooka 	mutex_spin_exit(&log_lock);
    506      1.54     pooka 
    507      1.54     pooka 	while (maxlen > 0) {
    508      1.54     pooka 		len = MIN(end - beg, maxlen);
    509      1.54     pooka 		if (len == 0)
    510      1.54     pooka 			break;
    511      1.54     pooka 		/* XXX unlocked, but hardly matters. */
    512      1.54     pooka 		error = copyout(&msgbufp->msg_bufc[beg], where, len);
    513      1.54     pooka 		ktrmibio(-1, UIO_READ, where, len, error);
    514      1.54     pooka 		if (error)
    515      1.54     pooka 			break;
    516      1.54     pooka 		where += len;
    517      1.54     pooka 		maxlen -= len;
    518      1.54     pooka 
    519      1.54     pooka 		/*
    520      1.54     pooka 		 * ... then, copy from the beginning of message buffer to
    521      1.54     pooka 		 * the write pointer.
    522      1.54     pooka 		 */
    523      1.54     pooka 		beg = 0;
    524      1.54     pooka 		end = msgbufp->msg_bufx;
    525      1.54     pooka 	}
    526      1.54     pooka 
    527      1.54     pooka 	sysctl_relock();
    528      1.54     pooka 	return (error);
    529      1.54     pooka }
    530      1.54     pooka 
    531      1.35   thorpej const struct cdevsw log_cdevsw = {
    532      1.51  dholland 	.d_open = logopen,
    533      1.51  dholland 	.d_close = logclose,
    534      1.51  dholland 	.d_read = logread,
    535      1.51  dholland 	.d_write = nowrite,
    536      1.51  dholland 	.d_ioctl = logioctl,
    537      1.51  dholland 	.d_stop = nostop,
    538      1.51  dholland 	.d_tty = notty,
    539      1.51  dholland 	.d_poll = logpoll,
    540      1.51  dholland 	.d_mmap = nommap,
    541      1.51  dholland 	.d_kqfilter = logkqfilter,
    542      1.53  dholland 	.d_discard = nodiscard,
    543      1.51  dholland 	.d_flag = D_OTHER | D_MPSAFE
    544      1.35   thorpej };
    545