subr_log.c revision 216952
1139804Simp/*-
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 * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
291541Srgrimes *	@(#)subr_log.c	8.1 (Berkeley) 6/10/93
301541Srgrimes */
311541Srgrimes
321541Srgrimes/*
331541Srgrimes * Error log buffer for kernel printf's.
341541Srgrimes */
351541Srgrimes
36116182Sobrien#include <sys/cdefs.h>
37116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_log.c 216952 2011-01-04 10:59:38Z kib $");
38116182Sobrien
391541Srgrimes#include <sys/param.h>
401541Srgrimes#include <sys/systm.h>
4112577Sbde#include <sys/conf.h>
421541Srgrimes#include <sys/proc.h>
431541Srgrimes#include <sys/vnode.h>
4424206Sbde#include <sys/filio.h>
4524206Sbde#include <sys/ttycom.h>
461541Srgrimes#include <sys/msgbuf.h>
473308Sphk#include <sys/signalvar.h>
4812517Sjulian#include <sys/kernel.h>
4929357Speter#include <sys/poll.h>
5041086Struckman#include <sys/filedesc.h>
5170069Sjhb#include <sys/sysctl.h>
5212517Sjulian
531541Srgrimes#define LOG_RDPRI	(PZERO + 1)
541541Srgrimes
551541Srgrimes#define LOG_ASYNC	0x04
561541Srgrimes
5712675Sjulianstatic	d_open_t	logopen;
5812675Sjulianstatic	d_close_t	logclose;
5912675Sjulianstatic	d_read_t	logread;
6012675Sjulianstatic	d_ioctl_t	logioctl;
6129357Speterstatic	d_poll_t	logpoll;
6212675Sjulian
6370069Sjhbstatic	void logtimeout(void *arg);
6470069Sjhb
6547625Sphkstatic struct cdevsw log_cdevsw = {
66126080Sphk	.d_version =	D_VERSION,
67111815Sphk	.d_open =	logopen,
68111815Sphk	.d_close =	logclose,
69111815Sphk	.d_read =	logread,
70111815Sphk	.d_ioctl =	logioctl,
71111815Sphk	.d_poll =	logpoll,
72111815Sphk	.d_name =	"log",
7347625Sphk};
7412675Sjulian
7512819Sphkstatic struct logsoftc {
761541Srgrimes	int	sc_state;		/* see above for possibilities */
771541Srgrimes	struct	selinfo sc_selp;	/* process waiting on select call */
7841087Struckman	struct  sigio *sc_sigio;	/* information for async I/O */
7970069Sjhb	struct	callout sc_callout;	/* callout to wakeup syslog  */
801541Srgrimes} logsoftc;
811541Srgrimes
82198860Sedint			log_open;	/* also used in log() */
83198860Sedstatic struct cv	log_wakeup;
84198860Sedstruct mtx		msgbuf_lock;
85198860SedMTX_SYSINIT(msgbuf_lock, &msgbuf_lock, "msgbuf lock", MTX_DEF);
861541Srgrimes
8770069Sjhb/* Times per second to check for a pending syslog wakeup. */
8870069Sjhbstatic int	log_wakeups_per_second = 5;
8970069SjhbSYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
9070069Sjhb    &log_wakeups_per_second, 0, "");
9170069Sjhb
921541Srgrimes/*ARGSUSED*/
9312675Sjulianstatic	int
94130585Sphklogopen(struct cdev *dev, int flags, int mode, struct thread *td)
951541Srgrimes{
96198860Sed
97116634Sbmilekic	if (log_wakeups_per_second < 1) {
98116634Sbmilekic		printf("syslog wakeup is less than one.  Adjusting to 1.\n");
99116634Sbmilekic		log_wakeups_per_second = 1;
100116634Sbmilekic	}
101198860Sed
102198860Sed	mtx_lock(&msgbuf_lock);
103198860Sed	if (log_open) {
104198860Sed		mtx_unlock(&msgbuf_lock);
105198860Sed		return (EBUSY);
106198860Sed	}
107198860Sed	log_open = 1;
10870069Sjhb	callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
10970069Sjhb	    logtimeout, NULL);
110198860Sed	mtx_unlock(&msgbuf_lock);
111198860Sed
112198860Sed	fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio);	/* signal process only */
1131541Srgrimes	return (0);
1141541Srgrimes}
1151541Srgrimes
1161541Srgrimes/*ARGSUSED*/
11712675Sjulianstatic	int
118130585Sphklogclose(struct cdev *dev, int flag, int mode, struct thread *td)
1191541Srgrimes{
1201541Srgrimes
121198860Sed	funsetown(&logsoftc.sc_sigio);
122198860Sed
123198860Sed	mtx_lock(&msgbuf_lock);
12470069Sjhb	callout_stop(&logsoftc.sc_callout);
1251541Srgrimes	logsoftc.sc_state = 0;
126198860Sed	log_open = 0;
127198860Sed	mtx_unlock(&msgbuf_lock);
128198860Sed
1291541Srgrimes	return (0);
1301541Srgrimes}
1311541Srgrimes
1321541Srgrimes/*ARGSUSED*/
13312675Sjulianstatic	int
134130585Sphklogread(struct cdev *dev, struct uio *uio, int flag)
1351541Srgrimes{
136116660Siedowse	char buf[128];
13769741Sphk	struct msgbuf *mbp = msgbufp;
138198860Sed	int error = 0, l;
1391541Srgrimes
140198860Sed	mtx_lock(&msgbuf_lock);
141116660Siedowse	while (msgbuf_getcount(mbp) == 0) {
1421541Srgrimes		if (flag & IO_NDELAY) {
143198860Sed			mtx_unlock(&msgbuf_lock);
1441541Srgrimes			return (EWOULDBLOCK);
1451541Srgrimes		}
146198860Sed		if ((error = cv_wait_sig(&log_wakeup, &msgbuf_lock)) != 0) {
147198860Sed			mtx_unlock(&msgbuf_lock);
1481541Srgrimes			return (error);
1491541Srgrimes		}
1501541Srgrimes	}
1511541Srgrimes
1521541Srgrimes	while (uio->uio_resid > 0) {
153116660Siedowse		l = imin(sizeof(buf), uio->uio_resid);
154116660Siedowse		l = msgbuf_getbytes(mbp, buf, l);
1551541Srgrimes		if (l == 0)
1561541Srgrimes			break;
157198860Sed		mtx_unlock(&msgbuf_lock);
158116660Siedowse		error = uiomove(buf, l, uio);
159198860Sed		if (error || uio->uio_resid == 0)
160198860Sed			return (error);
161198860Sed		mtx_lock(&msgbuf_lock);
1621541Srgrimes	}
163198860Sed	mtx_unlock(&msgbuf_lock);
1641541Srgrimes	return (error);
1651541Srgrimes}
1661541Srgrimes
1671541Srgrimes/*ARGSUSED*/
16812675Sjulianstatic	int
169130585Sphklogpoll(struct cdev *dev, int events, struct thread *td)
1701541Srgrimes{
17129357Speter	int revents = 0;
1721541Srgrimes
17346568Speter	if (events & (POLLIN | POLLRDNORM)) {
174198860Sed		mtx_lock(&msgbuf_lock);
175116660Siedowse		if (msgbuf_getcount(msgbufp) > 0)
17629357Speter			revents |= events & (POLLIN | POLLRDNORM);
17729357Speter		else
17883805Sjhb			selrecord(td, &logsoftc.sc_selp);
179198860Sed		mtx_unlock(&msgbuf_lock);
18046568Speter	}
18129357Speter	return (revents);
1821541Srgrimes}
1831541Srgrimes
18470069Sjhbstatic void
18570069Sjhblogtimeout(void *arg)
18670069Sjhb{
18770069Sjhb
18877057Sphk	if (!log_open)
18970069Sjhb		return;
190116634Sbmilekic	if (log_wakeups_per_second < 1) {
191116634Sbmilekic		printf("syslog wakeup is less than one.  Adjusting to 1.\n");
192116634Sbmilekic		log_wakeups_per_second = 1;
193116634Sbmilekic	}
19477057Sphk	if (msgbuftrigger == 0) {
195198860Sed		callout_schedule(&logsoftc.sc_callout,
196198860Sed		    hz / log_wakeups_per_second);
19777057Sphk		return;
19877057Sphk	}
19970239Sphk	msgbuftrigger = 0;
200122352Stanimura	selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI);
20141086Struckman	if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
20295883Salfred		pgsigio(&logsoftc.sc_sigio, SIGIO, 0);
203198860Sed	cv_broadcastpri(&log_wakeup, LOG_RDPRI);
204198860Sed	callout_schedule(&logsoftc.sc_callout, hz / log_wakeups_per_second);
2051541Srgrimes}
2061541Srgrimes
2071541Srgrimes/*ARGSUSED*/
20812675Sjulianstatic	int
209130585Sphklogioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td)
2101541Srgrimes{
2111541Srgrimes
2121541Srgrimes	switch (com) {
2131541Srgrimes
2141541Srgrimes	/* return number of characters immediately available */
2151541Srgrimes	case FIONREAD:
216116660Siedowse		*(int *)data = msgbuf_getcount(msgbufp);
2171541Srgrimes		break;
2181541Srgrimes
2191541Srgrimes	case FIONBIO:
2201541Srgrimes		break;
2211541Srgrimes
2221541Srgrimes	case FIOASYNC:
223198860Sed		mtx_lock(&msgbuf_lock);
2241541Srgrimes		if (*(int *)data)
2251541Srgrimes			logsoftc.sc_state |= LOG_ASYNC;
2261541Srgrimes		else
2271541Srgrimes			logsoftc.sc_state &= ~LOG_ASYNC;
228198860Sed		mtx_unlock(&msgbuf_lock);
2291541Srgrimes		break;
2301541Srgrimes
23141086Struckman	case FIOSETOWN:
23241086Struckman		return (fsetown(*(int *)data, &logsoftc.sc_sigio));
23341086Struckman
23441086Struckman	case FIOGETOWN:
235104393Struckman		*(int *)data = fgetown(&logsoftc.sc_sigio);
2361541Srgrimes		break;
2371541Srgrimes
23841086Struckman	/* This is deprecated, FIOSETOWN should be used instead. */
23941086Struckman	case TIOCSPGRP:
24041086Struckman		return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
24141086Struckman
24241086Struckman	/* This is deprecated, FIOGETOWN should be used instead */
2431541Srgrimes	case TIOCGPGRP:
244104393Struckman		*(int *)data = -fgetown(&logsoftc.sc_sigio);
2451541Srgrimes		break;
2461541Srgrimes
2471541Srgrimes	default:
2488161Sjkh		return (ENOTTY);
2491541Srgrimes	}
2501541Srgrimes	return (0);
2511541Srgrimes}
25212517Sjulian
25312675Sjulianstatic void
25469741Sphklog_drvinit(void *unused)
25512517Sjulian{
25669741Sphk
257198860Sed	cv_init(&log_wakeup, "klog");
258198860Sed	callout_init_mtx(&logsoftc.sc_callout, &msgbuf_lock, 0);
259216952Skib	make_dev_credf(MAKEDEV_ETERNAL, &log_cdevsw, 0, NULL, UID_ROOT,
260216952Skib	    GID_WHEEL, 0600, "klog");
26112517Sjulian}
26212517Sjulian
263177253SrwatsonSYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,log_drvinit,NULL);
264