xref: /freebsd/sys/kern/subr_log.c (revision ef9ffb8594eee294334ced627755bf5b46b48f9f)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1569a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
32df8bae1dSRodney W. Grimes /*
33df8bae1dSRodney W. Grimes  * Error log buffer for kernel printf's.
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
36df8bae1dSRodney W. Grimes #include <sys/param.h>
37df8bae1dSRodney W. Grimes #include <sys/systm.h>
3898d93822SBruce Evans #include <sys/conf.h>
39df8bae1dSRodney W. Grimes #include <sys/proc.h>
40df8bae1dSRodney W. Grimes #include <sys/vnode.h>
4120982410SBruce Evans #include <sys/filio.h>
4220982410SBruce Evans #include <sys/ttycom.h>
43df8bae1dSRodney W. Grimes #include <sys/msgbuf.h>
44797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
4553ac6efbSJulian Elischer #include <sys/kernel.h>
461514b90fSPeter Wemm #include <sys/poll.h>
47831d27a9SDon Lewis #include <sys/filedesc.h>
48a9b13707SJohn Baldwin #include <sys/sysctl.h>
4953ac6efbSJulian Elischer 
50df8bae1dSRodney W. Grimes #define LOG_RDPRI	(PZERO + 1)
51df8bae1dSRodney W. Grimes 
52df8bae1dSRodney W. Grimes #define LOG_ASYNC	0x04
53df8bae1dSRodney W. Grimes 
5487f6c662SJulian Elischer static	d_open_t	logopen;
5587f6c662SJulian Elischer static	d_close_t	logclose;
5687f6c662SJulian Elischer static	d_read_t	logread;
5787f6c662SJulian Elischer static	d_ioctl_t	logioctl;
581514b90fSPeter Wemm static	d_poll_t	logpoll;
596af519cfSKonstantin Belousov static	d_kqfilter_t	logkqfilter;
6087f6c662SJulian Elischer 
61a9b13707SJohn Baldwin static	void logtimeout(void *arg);
62a9b13707SJohn Baldwin 
634e2f199eSPoul-Henning Kamp static struct cdevsw log_cdevsw = {
64dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
657ac40f5fSPoul-Henning Kamp 	.d_open =	logopen,
667ac40f5fSPoul-Henning Kamp 	.d_close =	logclose,
677ac40f5fSPoul-Henning Kamp 	.d_read =	logread,
687ac40f5fSPoul-Henning Kamp 	.d_ioctl =	logioctl,
697ac40f5fSPoul-Henning Kamp 	.d_poll =	logpoll,
706af519cfSKonstantin Belousov 	.d_kqfilter =	logkqfilter,
717ac40f5fSPoul-Henning Kamp 	.d_name =	"log",
724e2f199eSPoul-Henning Kamp };
7387f6c662SJulian Elischer 
746af519cfSKonstantin Belousov static int	logkqread(struct knote *note, long hint);
756af519cfSKonstantin Belousov static void	logkqdetach(struct knote *note);
766af519cfSKonstantin Belousov 
77*ef9ffb85SMark Johnston static const struct filterops log_read_filterops = {
786af519cfSKonstantin Belousov 	.f_isfd =	1,
796af519cfSKonstantin Belousov 	.f_attach =	NULL,
806af519cfSKonstantin Belousov 	.f_detach =	logkqdetach,
816af519cfSKonstantin Belousov 	.f_event =	logkqread,
826af519cfSKonstantin Belousov };
836af519cfSKonstantin Belousov 
8487b6de2bSPoul-Henning Kamp static struct logsoftc {
85df8bae1dSRodney W. Grimes 	int	sc_state;		/* see above for possibilities */
86df8bae1dSRodney W. Grimes 	struct	selinfo sc_selp;	/* process waiting on select call */
8762d6ce3aSDon Lewis 	struct  sigio *sc_sigio;	/* information for async I/O */
88a9b13707SJohn Baldwin 	struct	callout sc_callout;	/* callout to wakeup syslog  */
89df8bae1dSRodney W. Grimes } logsoftc;
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes int			log_open;	/* also used in log() */
92ca1d2f65SEd Schouten static struct cv	log_wakeup;
93ca1d2f65SEd Schouten struct mtx		msgbuf_lock;
94ca1d2f65SEd Schouten MTX_SYSINIT(msgbuf_lock, &msgbuf_lock, "msgbuf lock", MTX_DEF);
95df8bae1dSRodney W. Grimes 
96a9b13707SJohn Baldwin static int	log_wakeups_per_second = 5;
97a9b13707SJohn Baldwin SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
98c51e4962SJohn Baldwin     &log_wakeups_per_second, 0,
99c51e4962SJohn Baldwin     "How often (times per second) to check for /dev/log waiters.");
100a9b13707SJohn Baldwin 
101df8bae1dSRodney W. Grimes /*ARGSUSED*/
10287f6c662SJulian Elischer static	int
logopen(struct cdev * dev,int flags,int mode,struct thread * td)10389c9c53dSPoul-Henning Kamp logopen(struct cdev *dev, int flags, int mode, struct thread *td)
104df8bae1dSRodney W. Grimes {
105ca1d2f65SEd Schouten 
106b2b417bbSBosko Milekic 	if (log_wakeups_per_second < 1) {
107b2b417bbSBosko Milekic 		printf("syslog wakeup is less than one.  Adjusting to 1.\n");
108b2b417bbSBosko Milekic 		log_wakeups_per_second = 1;
109b2b417bbSBosko Milekic 	}
110ca1d2f65SEd Schouten 
111ca1d2f65SEd Schouten 	mtx_lock(&msgbuf_lock);
112ca1d2f65SEd Schouten 	if (log_open) {
113ca1d2f65SEd Schouten 		mtx_unlock(&msgbuf_lock);
114ca1d2f65SEd Schouten 		return (EBUSY);
115ca1d2f65SEd Schouten 	}
116ca1d2f65SEd Schouten 	log_open = 1;
117c38250c9SDavide Italiano 	callout_reset_sbt(&logsoftc.sc_callout,
118c38250c9SDavide Italiano 	    SBT_1S / log_wakeups_per_second, 0, logtimeout, NULL, C_PREL(1));
119ca1d2f65SEd Schouten 	mtx_unlock(&msgbuf_lock);
120ca1d2f65SEd Schouten 
121ca1d2f65SEd Schouten 	fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio);	/* signal process only */
122df8bae1dSRodney W. Grimes 	return (0);
123df8bae1dSRodney W. Grimes }
124df8bae1dSRodney W. Grimes 
125df8bae1dSRodney W. Grimes /*ARGSUSED*/
12687f6c662SJulian Elischer static	int
logclose(struct cdev * dev,int flag,int mode,struct thread * td)12789c9c53dSPoul-Henning Kamp logclose(struct cdev *dev, int flag, int mode, struct thread *td)
128df8bae1dSRodney W. Grimes {
129df8bae1dSRodney W. Grimes 
130ca1d2f65SEd Schouten 	funsetown(&logsoftc.sc_sigio);
131ca1d2f65SEd Schouten 
132ca1d2f65SEd Schouten 	mtx_lock(&msgbuf_lock);
1334787f91dSPoul-Henning Kamp 	callout_stop(&logsoftc.sc_callout);
134df8bae1dSRodney W. Grimes 	logsoftc.sc_state = 0;
135ca1d2f65SEd Schouten 	log_open = 0;
136ca1d2f65SEd Schouten 	mtx_unlock(&msgbuf_lock);
137ca1d2f65SEd Schouten 
138df8bae1dSRodney W. Grimes 	return (0);
139df8bae1dSRodney W. Grimes }
140df8bae1dSRodney W. Grimes 
141df8bae1dSRodney W. Grimes /*ARGSUSED*/
14287f6c662SJulian Elischer static	int
logread(struct cdev * dev,struct uio * uio,int flag)14389c9c53dSPoul-Henning Kamp logread(struct cdev *dev, struct uio *uio, int flag)
144df8bae1dSRodney W. Grimes {
1454784a469SIan Dowse 	char buf[128];
14606b6617eSPoul-Henning Kamp 	struct msgbuf *mbp = msgbufp;
147ca1d2f65SEd Schouten 	int error = 0, l;
148df8bae1dSRodney W. Grimes 
149ca1d2f65SEd Schouten 	mtx_lock(&msgbuf_lock);
1504784a469SIan Dowse 	while (msgbuf_getcount(mbp) == 0) {
151df8bae1dSRodney W. Grimes 		if (flag & IO_NDELAY) {
152ca1d2f65SEd Schouten 			mtx_unlock(&msgbuf_lock);
153df8bae1dSRodney W. Grimes 			return (EWOULDBLOCK);
154df8bae1dSRodney W. Grimes 		}
155ca1d2f65SEd Schouten 		if ((error = cv_wait_sig(&log_wakeup, &msgbuf_lock)) != 0) {
156ca1d2f65SEd Schouten 			mtx_unlock(&msgbuf_lock);
157df8bae1dSRodney W. Grimes 			return (error);
158df8bae1dSRodney W. Grimes 		}
159df8bae1dSRodney W. Grimes 	}
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes 	while (uio->uio_resid > 0) {
1624784a469SIan Dowse 		l = imin(sizeof(buf), uio->uio_resid);
1634784a469SIan Dowse 		l = msgbuf_getbytes(mbp, buf, l);
164df8bae1dSRodney W. Grimes 		if (l == 0)
165df8bae1dSRodney W. Grimes 			break;
166ca1d2f65SEd Schouten 		mtx_unlock(&msgbuf_lock);
1674784a469SIan Dowse 		error = uiomove(buf, l, uio);
168ca1d2f65SEd Schouten 		if (error || uio->uio_resid == 0)
169ca1d2f65SEd Schouten 			return (error);
170ca1d2f65SEd Schouten 		mtx_lock(&msgbuf_lock);
171df8bae1dSRodney W. Grimes 	}
172ca1d2f65SEd Schouten 	mtx_unlock(&msgbuf_lock);
173df8bae1dSRodney W. Grimes 	return (error);
174df8bae1dSRodney W. Grimes }
175df8bae1dSRodney W. Grimes 
176df8bae1dSRodney W. Grimes /*ARGSUSED*/
17787f6c662SJulian Elischer static	int
logpoll(struct cdev * dev,int events,struct thread * td)17889c9c53dSPoul-Henning Kamp logpoll(struct cdev *dev, int events, struct thread *td)
179df8bae1dSRodney W. Grimes {
1801514b90fSPeter Wemm 	int revents = 0;
181df8bae1dSRodney W. Grimes 
182dfd5dee1SPeter Wemm 	if (events & (POLLIN | POLLRDNORM)) {
183ca1d2f65SEd Schouten 		mtx_lock(&msgbuf_lock);
1844784a469SIan Dowse 		if (msgbuf_getcount(msgbufp) > 0)
1851514b90fSPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
1861514b90fSPeter Wemm 		else
187ed01445dSJohn Baldwin 			selrecord(td, &logsoftc.sc_selp);
188ca1d2f65SEd Schouten 		mtx_unlock(&msgbuf_lock);
189dfd5dee1SPeter Wemm 	}
1901514b90fSPeter Wemm 	return (revents);
191df8bae1dSRodney W. Grimes }
192df8bae1dSRodney W. Grimes 
1936af519cfSKonstantin Belousov static int
logkqfilter(struct cdev * dev,struct knote * kn)1946af519cfSKonstantin Belousov logkqfilter(struct cdev *dev, struct knote *kn)
1956af519cfSKonstantin Belousov {
1966af519cfSKonstantin Belousov 
1976af519cfSKonstantin Belousov 	if (kn->kn_filter != EVFILT_READ)
1986af519cfSKonstantin Belousov 		return (EINVAL);
1996af519cfSKonstantin Belousov 
2006af519cfSKonstantin Belousov 	kn->kn_fop = &log_read_filterops;
2016af519cfSKonstantin Belousov 	kn->kn_hook = NULL;
2026af519cfSKonstantin Belousov 
2036af519cfSKonstantin Belousov 	mtx_lock(&msgbuf_lock);
2046af519cfSKonstantin Belousov 	knlist_add(&logsoftc.sc_selp.si_note, kn, 1);
2056af519cfSKonstantin Belousov 	mtx_unlock(&msgbuf_lock);
2066af519cfSKonstantin Belousov 	return (0);
2076af519cfSKonstantin Belousov }
2086af519cfSKonstantin Belousov 
2096af519cfSKonstantin Belousov static int
logkqread(struct knote * kn,long hint)2106af519cfSKonstantin Belousov logkqread(struct knote *kn, long hint)
2116af519cfSKonstantin Belousov {
2126af519cfSKonstantin Belousov 
2136af519cfSKonstantin Belousov 	mtx_assert(&msgbuf_lock, MA_OWNED);
2146af519cfSKonstantin Belousov 	kn->kn_data = msgbuf_getcount(msgbufp);
2156af519cfSKonstantin Belousov 	return (kn->kn_data != 0);
2166af519cfSKonstantin Belousov }
2176af519cfSKonstantin Belousov 
2186af519cfSKonstantin Belousov static void
logkqdetach(struct knote * kn)2196af519cfSKonstantin Belousov logkqdetach(struct knote *kn)
2206af519cfSKonstantin Belousov {
2216af519cfSKonstantin Belousov 
2226af519cfSKonstantin Belousov 	mtx_lock(&msgbuf_lock);
2236af519cfSKonstantin Belousov 	knlist_remove(&logsoftc.sc_selp.si_note, kn, 1);
2246af519cfSKonstantin Belousov 	mtx_unlock(&msgbuf_lock);
2256af519cfSKonstantin Belousov }
2266af519cfSKonstantin Belousov 
227a9b13707SJohn Baldwin static void
logtimeout(void * arg)228a9b13707SJohn Baldwin logtimeout(void *arg)
229a9b13707SJohn Baldwin {
230a9b13707SJohn Baldwin 
231df8bae1dSRodney W. Grimes 	if (!log_open)
232df8bae1dSRodney W. Grimes 		return;
233c38250c9SDavide Italiano 	if (msgbuftrigger == 0)
234c38250c9SDavide Italiano 		goto done;
2354787f91dSPoul-Henning Kamp 	msgbuftrigger = 0;
236512824f8SSeigo Tanimura 	selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI);
2376af519cfSKonstantin Belousov 	KNOTE_LOCKED(&logsoftc.sc_selp.si_note, 0);
238831d27a9SDon Lewis 	if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
239f1320723SAlfred Perlstein 		pgsigio(&logsoftc.sc_sigio, SIGIO, 0);
240ca1d2f65SEd Schouten 	cv_broadcastpri(&log_wakeup, LOG_RDPRI);
241c38250c9SDavide Italiano done:
242c38250c9SDavide Italiano 	if (log_wakeups_per_second < 1) {
243c38250c9SDavide Italiano 		printf("syslog wakeup is less than one.  Adjusting to 1.\n");
244c38250c9SDavide Italiano 		log_wakeups_per_second = 1;
245c38250c9SDavide Italiano 	}
246c38250c9SDavide Italiano 	callout_reset_sbt(&logsoftc.sc_callout,
247c38250c9SDavide Italiano 	    SBT_1S / log_wakeups_per_second, 0, logtimeout, NULL, C_PREL(1));
248df8bae1dSRodney W. Grimes }
249df8bae1dSRodney W. Grimes 
250df8bae1dSRodney W. Grimes /*ARGSUSED*/
25187f6c662SJulian Elischer static	int
logioctl(struct cdev * dev,u_long com,caddr_t data,int flag,struct thread * td)25289c9c53dSPoul-Henning Kamp logioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td)
253df8bae1dSRodney W. Grimes {
254df8bae1dSRodney W. Grimes 
255df8bae1dSRodney W. Grimes 	switch (com) {
256df8bae1dSRodney W. Grimes 	/* return number of characters immediately available */
257df8bae1dSRodney W. Grimes 	case FIONREAD:
2584784a469SIan Dowse 		*(int *)data = msgbuf_getcount(msgbufp);
259df8bae1dSRodney W. Grimes 		break;
260df8bae1dSRodney W. Grimes 
261df8bae1dSRodney W. Grimes 	case FIONBIO:
262df8bae1dSRodney W. Grimes 		break;
263df8bae1dSRodney W. Grimes 
264df8bae1dSRodney W. Grimes 	case FIOASYNC:
265ca1d2f65SEd Schouten 		mtx_lock(&msgbuf_lock);
266df8bae1dSRodney W. Grimes 		if (*(int *)data)
267df8bae1dSRodney W. Grimes 			logsoftc.sc_state |= LOG_ASYNC;
268df8bae1dSRodney W. Grimes 		else
269df8bae1dSRodney W. Grimes 			logsoftc.sc_state &= ~LOG_ASYNC;
270ca1d2f65SEd Schouten 		mtx_unlock(&msgbuf_lock);
271df8bae1dSRodney W. Grimes 		break;
272df8bae1dSRodney W. Grimes 
273831d27a9SDon Lewis 	case FIOSETOWN:
274831d27a9SDon Lewis 		return (fsetown(*(int *)data, &logsoftc.sc_sigio));
275831d27a9SDon Lewis 
276831d27a9SDon Lewis 	case FIOGETOWN:
27791e97a82SDon Lewis 		*(int *)data = fgetown(&logsoftc.sc_sigio);
278df8bae1dSRodney W. Grimes 		break;
279df8bae1dSRodney W. Grimes 
280831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
281831d27a9SDon Lewis 	case TIOCSPGRP:
282831d27a9SDon Lewis 		return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
283831d27a9SDon Lewis 
284831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead */
285df8bae1dSRodney W. Grimes 	case TIOCGPGRP:
28691e97a82SDon Lewis 		*(int *)data = -fgetown(&logsoftc.sc_sigio);
287df8bae1dSRodney W. Grimes 		break;
288df8bae1dSRodney W. Grimes 
289df8bae1dSRodney W. Grimes 	default:
290bcac5617SJordan K. Hubbard 		return (ENOTTY);
291df8bae1dSRodney W. Grimes 	}
292df8bae1dSRodney W. Grimes 	return (0);
293df8bae1dSRodney W. Grimes }
29453ac6efbSJulian Elischer 
29587f6c662SJulian Elischer static void
log_drvinit(void * unused)29606b6617eSPoul-Henning Kamp log_drvinit(void *unused)
29753ac6efbSJulian Elischer {
29806b6617eSPoul-Henning Kamp 
299ca1d2f65SEd Schouten 	cv_init(&log_wakeup, "klog");
300ca1d2f65SEd Schouten 	callout_init_mtx(&logsoftc.sc_callout, &msgbuf_lock, 0);
3016af519cfSKonstantin Belousov 	knlist_init_mtx(&logsoftc.sc_selp.si_note, &msgbuf_lock);
30223b70c1aSKonstantin Belousov 	make_dev_credf(MAKEDEV_ETERNAL, &log_cdevsw, 0, NULL, UID_ROOT,
30323b70c1aSKonstantin Belousov 	    GID_WHEEL, 0600, "klog");
3047198bf47SJulian Elischer }
30553ac6efbSJulian Elischer 
306237fdd78SRobert Watson SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,log_drvinit,NULL);
307