xref: /freebsd/sys/kern/subr_log.c (revision 23b70c1ae21553acc791d61475afa4708977ad9c)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)subr_log.c	8.1 (Berkeley) 6/10/93
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 
36677b542eSDavid E. O'Brien #include <sys/cdefs.h>
37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
38677b542eSDavid E. O'Brien 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
40df8bae1dSRodney W. Grimes #include <sys/systm.h>
4198d93822SBruce Evans #include <sys/conf.h>
42df8bae1dSRodney W. Grimes #include <sys/proc.h>
43df8bae1dSRodney W. Grimes #include <sys/vnode.h>
4420982410SBruce Evans #include <sys/filio.h>
4520982410SBruce Evans #include <sys/ttycom.h>
46df8bae1dSRodney W. Grimes #include <sys/msgbuf.h>
47797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
4853ac6efbSJulian Elischer #include <sys/kernel.h>
491514b90fSPeter Wemm #include <sys/poll.h>
50831d27a9SDon Lewis #include <sys/filedesc.h>
51a9b13707SJohn Baldwin #include <sys/sysctl.h>
5253ac6efbSJulian Elischer 
53df8bae1dSRodney W. Grimes #define LOG_RDPRI	(PZERO + 1)
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #define LOG_ASYNC	0x04
56df8bae1dSRodney W. Grimes 
5787f6c662SJulian Elischer static	d_open_t	logopen;
5887f6c662SJulian Elischer static	d_close_t	logclose;
5987f6c662SJulian Elischer static	d_read_t	logread;
6087f6c662SJulian Elischer static	d_ioctl_t	logioctl;
611514b90fSPeter Wemm static	d_poll_t	logpoll;
6287f6c662SJulian Elischer 
63a9b13707SJohn Baldwin static	void logtimeout(void *arg);
64a9b13707SJohn Baldwin 
654e2f199eSPoul-Henning Kamp static struct cdevsw log_cdevsw = {
66dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
677ac40f5fSPoul-Henning Kamp 	.d_open =	logopen,
687ac40f5fSPoul-Henning Kamp 	.d_close =	logclose,
697ac40f5fSPoul-Henning Kamp 	.d_read =	logread,
707ac40f5fSPoul-Henning Kamp 	.d_ioctl =	logioctl,
717ac40f5fSPoul-Henning Kamp 	.d_poll =	logpoll,
727ac40f5fSPoul-Henning Kamp 	.d_name =	"log",
734e2f199eSPoul-Henning Kamp };
7487f6c662SJulian Elischer 
7587b6de2bSPoul-Henning Kamp static struct logsoftc {
76df8bae1dSRodney W. Grimes 	int	sc_state;		/* see above for possibilities */
77df8bae1dSRodney W. Grimes 	struct	selinfo sc_selp;	/* process waiting on select call */
7862d6ce3aSDon Lewis 	struct  sigio *sc_sigio;	/* information for async I/O */
79a9b13707SJohn Baldwin 	struct	callout sc_callout;	/* callout to wakeup syslog  */
80df8bae1dSRodney W. Grimes } logsoftc;
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes int			log_open;	/* also used in log() */
83ca1d2f65SEd Schouten static struct cv	log_wakeup;
84ca1d2f65SEd Schouten struct mtx		msgbuf_lock;
85ca1d2f65SEd Schouten MTX_SYSINIT(msgbuf_lock, &msgbuf_lock, "msgbuf lock", MTX_DEF);
86df8bae1dSRodney W. Grimes 
87a9b13707SJohn Baldwin /* Times per second to check for a pending syslog wakeup. */
88a9b13707SJohn Baldwin static int	log_wakeups_per_second = 5;
89a9b13707SJohn Baldwin SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
90a9b13707SJohn Baldwin     &log_wakeups_per_second, 0, "");
91a9b13707SJohn Baldwin 
92df8bae1dSRodney W. Grimes /*ARGSUSED*/
9387f6c662SJulian Elischer static	int
9489c9c53dSPoul-Henning Kamp logopen(struct cdev *dev, int flags, int mode, struct thread *td)
95df8bae1dSRodney W. Grimes {
96ca1d2f65SEd Schouten 
97b2b417bbSBosko Milekic 	if (log_wakeups_per_second < 1) {
98b2b417bbSBosko Milekic 		printf("syslog wakeup is less than one.  Adjusting to 1.\n");
99b2b417bbSBosko Milekic 		log_wakeups_per_second = 1;
100b2b417bbSBosko Milekic 	}
101ca1d2f65SEd Schouten 
102ca1d2f65SEd Schouten 	mtx_lock(&msgbuf_lock);
103ca1d2f65SEd Schouten 	if (log_open) {
104ca1d2f65SEd Schouten 		mtx_unlock(&msgbuf_lock);
105ca1d2f65SEd Schouten 		return (EBUSY);
106ca1d2f65SEd Schouten 	}
107ca1d2f65SEd Schouten 	log_open = 1;
108a9b13707SJohn Baldwin 	callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
109a9b13707SJohn Baldwin 	    logtimeout, NULL);
110ca1d2f65SEd Schouten 	mtx_unlock(&msgbuf_lock);
111ca1d2f65SEd Schouten 
112ca1d2f65SEd Schouten 	fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio);	/* signal process only */
113df8bae1dSRodney W. Grimes 	return (0);
114df8bae1dSRodney W. Grimes }
115df8bae1dSRodney W. Grimes 
116df8bae1dSRodney W. Grimes /*ARGSUSED*/
11787f6c662SJulian Elischer static	int
11889c9c53dSPoul-Henning Kamp logclose(struct cdev *dev, int flag, int mode, struct thread *td)
119df8bae1dSRodney W. Grimes {
120df8bae1dSRodney W. Grimes 
121ca1d2f65SEd Schouten 	funsetown(&logsoftc.sc_sigio);
122ca1d2f65SEd Schouten 
123ca1d2f65SEd Schouten 	mtx_lock(&msgbuf_lock);
1244787f91dSPoul-Henning Kamp 	callout_stop(&logsoftc.sc_callout);
125df8bae1dSRodney W. Grimes 	logsoftc.sc_state = 0;
126ca1d2f65SEd Schouten 	log_open = 0;
127ca1d2f65SEd Schouten 	mtx_unlock(&msgbuf_lock);
128ca1d2f65SEd Schouten 
129df8bae1dSRodney W. Grimes 	return (0);
130df8bae1dSRodney W. Grimes }
131df8bae1dSRodney W. Grimes 
132df8bae1dSRodney W. Grimes /*ARGSUSED*/
13387f6c662SJulian Elischer static	int
13489c9c53dSPoul-Henning Kamp logread(struct cdev *dev, struct uio *uio, int flag)
135df8bae1dSRodney W. Grimes {
1364784a469SIan Dowse 	char buf[128];
13706b6617eSPoul-Henning Kamp 	struct msgbuf *mbp = msgbufp;
138ca1d2f65SEd Schouten 	int error = 0, l;
139df8bae1dSRodney W. Grimes 
140ca1d2f65SEd Schouten 	mtx_lock(&msgbuf_lock);
1414784a469SIan Dowse 	while (msgbuf_getcount(mbp) == 0) {
142df8bae1dSRodney W. Grimes 		if (flag & IO_NDELAY) {
143ca1d2f65SEd Schouten 			mtx_unlock(&msgbuf_lock);
144df8bae1dSRodney W. Grimes 			return (EWOULDBLOCK);
145df8bae1dSRodney W. Grimes 		}
146ca1d2f65SEd Schouten 		if ((error = cv_wait_sig(&log_wakeup, &msgbuf_lock)) != 0) {
147ca1d2f65SEd Schouten 			mtx_unlock(&msgbuf_lock);
148df8bae1dSRodney W. Grimes 			return (error);
149df8bae1dSRodney W. Grimes 		}
150df8bae1dSRodney W. Grimes 	}
151df8bae1dSRodney W. Grimes 
152df8bae1dSRodney W. Grimes 	while (uio->uio_resid > 0) {
1534784a469SIan Dowse 		l = imin(sizeof(buf), uio->uio_resid);
1544784a469SIan Dowse 		l = msgbuf_getbytes(mbp, buf, l);
155df8bae1dSRodney W. Grimes 		if (l == 0)
156df8bae1dSRodney W. Grimes 			break;
157ca1d2f65SEd Schouten 		mtx_unlock(&msgbuf_lock);
1584784a469SIan Dowse 		error = uiomove(buf, l, uio);
159ca1d2f65SEd Schouten 		if (error || uio->uio_resid == 0)
160ca1d2f65SEd Schouten 			return (error);
161ca1d2f65SEd Schouten 		mtx_lock(&msgbuf_lock);
162df8bae1dSRodney W. Grimes 	}
163ca1d2f65SEd Schouten 	mtx_unlock(&msgbuf_lock);
164df8bae1dSRodney W. Grimes 	return (error);
165df8bae1dSRodney W. Grimes }
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes /*ARGSUSED*/
16887f6c662SJulian Elischer static	int
16989c9c53dSPoul-Henning Kamp logpoll(struct cdev *dev, int events, struct thread *td)
170df8bae1dSRodney W. Grimes {
1711514b90fSPeter Wemm 	int revents = 0;
172df8bae1dSRodney W. Grimes 
173dfd5dee1SPeter Wemm 	if (events & (POLLIN | POLLRDNORM)) {
174ca1d2f65SEd Schouten 		mtx_lock(&msgbuf_lock);
1754784a469SIan Dowse 		if (msgbuf_getcount(msgbufp) > 0)
1761514b90fSPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
1771514b90fSPeter Wemm 		else
178ed01445dSJohn Baldwin 			selrecord(td, &logsoftc.sc_selp);
179ca1d2f65SEd Schouten 		mtx_unlock(&msgbuf_lock);
180dfd5dee1SPeter Wemm 	}
1811514b90fSPeter Wemm 	return (revents);
182df8bae1dSRodney W. Grimes }
183df8bae1dSRodney W. Grimes 
184a9b13707SJohn Baldwin static void
185a9b13707SJohn Baldwin logtimeout(void *arg)
186a9b13707SJohn Baldwin {
187a9b13707SJohn Baldwin 
188df8bae1dSRodney W. Grimes 	if (!log_open)
189df8bae1dSRodney W. Grimes 		return;
190b2b417bbSBosko Milekic 	if (log_wakeups_per_second < 1) {
191b2b417bbSBosko Milekic 		printf("syslog wakeup is less than one.  Adjusting to 1.\n");
192b2b417bbSBosko Milekic 		log_wakeups_per_second = 1;
193b2b417bbSBosko Milekic 	}
1944787f91dSPoul-Henning Kamp 	if (msgbuftrigger == 0) {
195ca1d2f65SEd Schouten 		callout_schedule(&logsoftc.sc_callout,
196ca1d2f65SEd Schouten 		    hz / log_wakeups_per_second);
1974787f91dSPoul-Henning Kamp 		return;
1984787f91dSPoul-Henning Kamp 	}
1994787f91dSPoul-Henning Kamp 	msgbuftrigger = 0;
200512824f8SSeigo Tanimura 	selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI);
201831d27a9SDon Lewis 	if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
202f1320723SAlfred Perlstein 		pgsigio(&logsoftc.sc_sigio, SIGIO, 0);
203ca1d2f65SEd Schouten 	cv_broadcastpri(&log_wakeup, LOG_RDPRI);
204ca1d2f65SEd Schouten 	callout_schedule(&logsoftc.sc_callout, hz / log_wakeups_per_second);
205df8bae1dSRodney W. Grimes }
206df8bae1dSRodney W. Grimes 
207df8bae1dSRodney W. Grimes /*ARGSUSED*/
20887f6c662SJulian Elischer static	int
20989c9c53dSPoul-Henning Kamp logioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td)
210df8bae1dSRodney W. Grimes {
211df8bae1dSRodney W. Grimes 
212df8bae1dSRodney W. Grimes 	switch (com) {
213df8bae1dSRodney W. Grimes 
214df8bae1dSRodney W. Grimes 	/* return number of characters immediately available */
215df8bae1dSRodney W. Grimes 	case FIONREAD:
2164784a469SIan Dowse 		*(int *)data = msgbuf_getcount(msgbufp);
217df8bae1dSRodney W. Grimes 		break;
218df8bae1dSRodney W. Grimes 
219df8bae1dSRodney W. Grimes 	case FIONBIO:
220df8bae1dSRodney W. Grimes 		break;
221df8bae1dSRodney W. Grimes 
222df8bae1dSRodney W. Grimes 	case FIOASYNC:
223ca1d2f65SEd Schouten 		mtx_lock(&msgbuf_lock);
224df8bae1dSRodney W. Grimes 		if (*(int *)data)
225df8bae1dSRodney W. Grimes 			logsoftc.sc_state |= LOG_ASYNC;
226df8bae1dSRodney W. Grimes 		else
227df8bae1dSRodney W. Grimes 			logsoftc.sc_state &= ~LOG_ASYNC;
228ca1d2f65SEd Schouten 		mtx_unlock(&msgbuf_lock);
229df8bae1dSRodney W. Grimes 		break;
230df8bae1dSRodney W. Grimes 
231831d27a9SDon Lewis 	case FIOSETOWN:
232831d27a9SDon Lewis 		return (fsetown(*(int *)data, &logsoftc.sc_sigio));
233831d27a9SDon Lewis 
234831d27a9SDon Lewis 	case FIOGETOWN:
23591e97a82SDon Lewis 		*(int *)data = fgetown(&logsoftc.sc_sigio);
236df8bae1dSRodney W. Grimes 		break;
237df8bae1dSRodney W. Grimes 
238831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
239831d27a9SDon Lewis 	case TIOCSPGRP:
240831d27a9SDon Lewis 		return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
241831d27a9SDon Lewis 
242831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead */
243df8bae1dSRodney W. Grimes 	case TIOCGPGRP:
24491e97a82SDon Lewis 		*(int *)data = -fgetown(&logsoftc.sc_sigio);
245df8bae1dSRodney W. Grimes 		break;
246df8bae1dSRodney W. Grimes 
247df8bae1dSRodney W. Grimes 	default:
248bcac5617SJordan K. Hubbard 		return (ENOTTY);
249df8bae1dSRodney W. Grimes 	}
250df8bae1dSRodney W. Grimes 	return (0);
251df8bae1dSRodney W. Grimes }
25253ac6efbSJulian Elischer 
25387f6c662SJulian Elischer static void
25406b6617eSPoul-Henning Kamp log_drvinit(void *unused)
25553ac6efbSJulian Elischer {
25606b6617eSPoul-Henning Kamp 
257ca1d2f65SEd Schouten 	cv_init(&log_wakeup, "klog");
258ca1d2f65SEd Schouten 	callout_init_mtx(&logsoftc.sc_callout, &msgbuf_lock, 0);
259*23b70c1aSKonstantin Belousov 	make_dev_credf(MAKEDEV_ETERNAL, &log_cdevsw, 0, NULL, UID_ROOT,
260*23b70c1aSKonstantin Belousov 	    GID_WHEEL, 0600, "klog");
2617198bf47SJulian Elischer }
26253ac6efbSJulian Elischer 
263237fdd78SRobert Watson SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,log_drvinit,NULL);
264