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