subr_log.c revision 77057
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1986, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)subr_log.c	8.1 (Berkeley) 6/10/93
3450477Speter * $FreeBSD: head/sys/kern/subr_log.c 77057 2001-05-23 19:02:50Z phk $
351541Srgrimes */
361541Srgrimes
371541Srgrimes/*
381541Srgrimes * Error log buffer for kernel printf's.
391541Srgrimes */
401541Srgrimes
411541Srgrimes#include <sys/param.h>
421541Srgrimes#include <sys/systm.h>
4312577Sbde#include <sys/conf.h>
441541Srgrimes#include <sys/proc.h>
451541Srgrimes#include <sys/vnode.h>
4624206Sbde#include <sys/filio.h>
4724206Sbde#include <sys/ttycom.h>
481541Srgrimes#include <sys/msgbuf.h>
493308Sphk#include <sys/signalvar.h>
5012517Sjulian#include <sys/kernel.h>
5129357Speter#include <sys/poll.h>
5241086Struckman#include <sys/filedesc.h>
5370069Sjhb#include <sys/sysctl.h>
5412517Sjulian
551541Srgrimes#define LOG_RDPRI	(PZERO + 1)
561541Srgrimes
571541Srgrimes#define LOG_ASYNC	0x04
581541Srgrimes#define LOG_RDWAIT	0x08
591541Srgrimes
6012675Sjulianstatic	d_open_t	logopen;
6112675Sjulianstatic	d_close_t	logclose;
6212675Sjulianstatic	d_read_t	logread;
6312675Sjulianstatic	d_ioctl_t	logioctl;
6429357Speterstatic	d_poll_t	logpoll;
6512675Sjulian
6670069Sjhbstatic	void logtimeout(void *arg);
6770069Sjhb
6812675Sjulian#define CDEV_MAJOR 7
6947625Sphkstatic struct cdevsw log_cdevsw = {
7047625Sphk	/* open */	logopen,
7147625Sphk	/* close */	logclose,
7247625Sphk	/* read */	logread,
7347625Sphk	/* write */	nowrite,
7447625Sphk	/* ioctl */	logioctl,
7547625Sphk	/* poll */	logpoll,
7647625Sphk	/* mmap */	nommap,
7747625Sphk	/* strategy */	nostrategy,
7847625Sphk	/* name */	"log",
7947625Sphk	/* maj */	CDEV_MAJOR,
8047625Sphk	/* dump */	nodump,
8147625Sphk	/* psize */	nopsize,
8247625Sphk	/* flags */	0,
8347625Sphk};
8412675Sjulian
8512819Sphkstatic struct logsoftc {
861541Srgrimes	int	sc_state;		/* see above for possibilities */
871541Srgrimes	struct	selinfo sc_selp;	/* process waiting on select call */
8841087Struckman	struct  sigio *sc_sigio;	/* information for async I/O */
8970069Sjhb	struct	callout sc_callout;	/* callout to wakeup syslog  */
901541Srgrimes} logsoftc;
911541Srgrimes
921541Srgrimesint	log_open;			/* also used in log() */
931541Srgrimes
9470069Sjhb/* Times per second to check for a pending syslog wakeup. */
9570069Sjhbstatic int	log_wakeups_per_second = 5;
9670069SjhbSYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
9770069Sjhb    &log_wakeups_per_second, 0, "");
9870069Sjhb
991541Srgrimes/*ARGSUSED*/
10012675Sjulianstatic	int
10169741Sphklogopen(dev_t dev, int flags, int mode, struct proc *p)
1021541Srgrimes{
1031541Srgrimes	if (log_open)
1041541Srgrimes		return (EBUSY);
1051541Srgrimes	log_open = 1;
10670069Sjhb	callout_init(&logsoftc.sc_callout, 0);
10741086Struckman	fsetown(p->p_pid, &logsoftc.sc_sigio);	/* signal process only */
10870069Sjhb	callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
10970069Sjhb	    logtimeout, NULL);
1101541Srgrimes	return (0);
1111541Srgrimes}
1121541Srgrimes
1131541Srgrimes/*ARGSUSED*/
11412675Sjulianstatic	int
11569741Sphklogclose(dev_t dev, int flag, int mode, struct proc *p)
1161541Srgrimes{
1171541Srgrimes
11877057Sphk	log_open = 0;
11970069Sjhb	callout_stop(&logsoftc.sc_callout);
1201541Srgrimes	logsoftc.sc_state = 0;
12141086Struckman	funsetown(logsoftc.sc_sigio);
1221541Srgrimes	return (0);
1231541Srgrimes}
1241541Srgrimes
1251541Srgrimes/*ARGSUSED*/
12612675Sjulianstatic	int
12769741Sphklogread(dev_t dev, struct uio *uio, int flag)
1281541Srgrimes{
12969741Sphk	struct msgbuf *mbp = msgbufp;
13069741Sphk	long l;
13169741Sphk	int s;
1321541Srgrimes	int error = 0;
1331541Srgrimes
1341541Srgrimes	s = splhigh();
1351541Srgrimes	while (mbp->msg_bufr == mbp->msg_bufx) {
1361541Srgrimes		if (flag & IO_NDELAY) {
1371541Srgrimes			splx(s);
1381541Srgrimes			return (EWOULDBLOCK);
1391541Srgrimes		}
1401541Srgrimes		logsoftc.sc_state |= LOG_RDWAIT;
1413098Sphk		if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
1423098Sphk		    "klog", 0))) {
1431541Srgrimes			splx(s);
1441541Srgrimes			return (error);
1451541Srgrimes		}
1461541Srgrimes	}
1471541Srgrimes	splx(s);
1481541Srgrimes	logsoftc.sc_state &= ~LOG_RDWAIT;
1491541Srgrimes
1501541Srgrimes	while (uio->uio_resid > 0) {
1511541Srgrimes		l = mbp->msg_bufx - mbp->msg_bufr;
1521541Srgrimes		if (l < 0)
15336179Sphk			l = mbp->msg_size - mbp->msg_bufr;
1541541Srgrimes		l = min(l, uio->uio_resid);
1551541Srgrimes		if (l == 0)
1561541Srgrimes			break;
15736179Sphk		error = uiomove((caddr_t)msgbufp->msg_ptr + mbp->msg_bufr,
15836179Sphk		    (int)l, uio);
1591541Srgrimes		if (error)
1601541Srgrimes			break;
1611541Srgrimes		mbp->msg_bufr += l;
16236179Sphk		if (mbp->msg_bufr >= mbp->msg_size)
1631541Srgrimes			mbp->msg_bufr = 0;
1641541Srgrimes	}
1651541Srgrimes	return (error);
1661541Srgrimes}
1671541Srgrimes
1681541Srgrimes/*ARGSUSED*/
16912675Sjulianstatic	int
17069741Sphklogpoll(dev_t dev, int events, struct proc *p)
1711541Srgrimes{
17229357Speter	int s;
17329357Speter	int revents = 0;
1741541Srgrimes
17529357Speter	s = splhigh();
1761541Srgrimes
17746568Speter	if (events & (POLLIN | POLLRDNORM)) {
17829357Speter		if (msgbufp->msg_bufr != msgbufp->msg_bufx)
17929357Speter			revents |= events & (POLLIN | POLLRDNORM);
18029357Speter		else
18129357Speter			selrecord(p, &logsoftc.sc_selp);
18246568Speter	}
1831541Srgrimes	splx(s);
18429357Speter	return (revents);
1851541Srgrimes}
1861541Srgrimes
18770069Sjhbstatic void
18870069Sjhblogtimeout(void *arg)
18970069Sjhb{
19070069Sjhb
19177057Sphk	if (!log_open)
19270069Sjhb		return;
19377057Sphk	if (msgbuftrigger == 0) {
19477057Sphk		callout_reset(&logsoftc.sc_callout,
19577057Sphk		    hz / log_wakeups_per_second, logtimeout, NULL);
19677057Sphk		return;
19777057Sphk	}
19870239Sphk	msgbuftrigger = 0;
1991541Srgrimes	selwakeup(&logsoftc.sc_selp);
20041086Struckman	if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
20141086Struckman		pgsigio(logsoftc.sc_sigio, SIGIO, 0);
2021541Srgrimes	if (logsoftc.sc_state & LOG_RDWAIT) {
2031541Srgrimes		wakeup((caddr_t)msgbufp);
2041541Srgrimes		logsoftc.sc_state &= ~LOG_RDWAIT;
2051541Srgrimes	}
20670069Sjhb	callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
20770069Sjhb	    logtimeout, NULL);
2081541Srgrimes}
2091541Srgrimes
2101541Srgrimes/*ARGSUSED*/
21112675Sjulianstatic	int
21269741Sphklogioctl(dev_t dev, u_long com, caddr_t data, int flag, struct proc *p)
2131541Srgrimes{
2141541Srgrimes	long l;
2151541Srgrimes	int s;
2161541Srgrimes
2171541Srgrimes	switch (com) {
2181541Srgrimes
2191541Srgrimes	/* return number of characters immediately available */
2201541Srgrimes	case FIONREAD:
2211541Srgrimes		s = splhigh();
2221541Srgrimes		l = msgbufp->msg_bufx - msgbufp->msg_bufr;
2231541Srgrimes		splx(s);
2241541Srgrimes		if (l < 0)
22536179Sphk			l += msgbufp->msg_size;
2261541Srgrimes		*(int *)data = l;
2271541Srgrimes		break;
2281541Srgrimes
2291541Srgrimes	case FIONBIO:
2301541Srgrimes		break;
2311541Srgrimes
2321541Srgrimes	case FIOASYNC:
2331541Srgrimes		if (*(int *)data)
2341541Srgrimes			logsoftc.sc_state |= LOG_ASYNC;
2351541Srgrimes		else
2361541Srgrimes			logsoftc.sc_state &= ~LOG_ASYNC;
2371541Srgrimes		break;
2381541Srgrimes
23941086Struckman	case FIOSETOWN:
24041086Struckman		return (fsetown(*(int *)data, &logsoftc.sc_sigio));
24141086Struckman
24241086Struckman	case FIOGETOWN:
24341086Struckman		*(int *)data = fgetown(logsoftc.sc_sigio);
2441541Srgrimes		break;
2451541Srgrimes
24641086Struckman	/* This is deprecated, FIOSETOWN should be used instead. */
24741086Struckman	case TIOCSPGRP:
24841086Struckman		return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
24941086Struckman
25041086Struckman	/* This is deprecated, FIOGETOWN should be used instead */
2511541Srgrimes	case TIOCGPGRP:
25241086Struckman		*(int *)data = -fgetown(logsoftc.sc_sigio);
2531541Srgrimes		break;
2541541Srgrimes
2551541Srgrimes	default:
2568161Sjkh		return (ENOTTY);
2571541Srgrimes	}
2581541Srgrimes	return (0);
2591541Srgrimes}
26012517Sjulian
26112675Sjulianstatic void
26269741Sphklog_drvinit(void *unused)
26312517Sjulian{
26469741Sphk
26550254Sphk	make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog");
26612517Sjulian}
26712517Sjulian
26812517SjulianSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
269