xref: /freebsd/sys/kern/subr_log.c (revision d2387d42b8da231a5b95cbc313825fb2aadf26f6)
1 /*
2  * Copyright (c) 1982, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)subr_log.c	8.1 (Berkeley) 6/10/93
34  */
35 
36 /*
37  * Error log buffer for kernel printf's.
38  */
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/conf.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/filio.h>
49 #include <sys/ttycom.h>
50 #include <sys/msgbuf.h>
51 #include <sys/signalvar.h>
52 #include <sys/kernel.h>
53 #include <sys/poll.h>
54 #include <sys/filedesc.h>
55 #include <sys/sysctl.h>
56 
57 #define LOG_RDPRI	(PZERO + 1)
58 
59 #define LOG_ASYNC	0x04
60 #define LOG_RDWAIT	0x08
61 
62 static	d_open_t	logopen;
63 static	d_close_t	logclose;
64 static	d_read_t	logread;
65 static	d_ioctl_t	logioctl;
66 static	d_poll_t	logpoll;
67 
68 static	void logtimeout(void *arg);
69 
70 #define CDEV_MAJOR 7
71 static struct cdevsw log_cdevsw = {
72 	.d_version =	D_VERSION,
73 	.d_flags =	D_NEEDGIANT,
74 	.d_open =	logopen,
75 	.d_close =	logclose,
76 	.d_read =	logread,
77 	.d_ioctl =	logioctl,
78 	.d_poll =	logpoll,
79 	.d_name =	"log",
80 	.d_maj =	CDEV_MAJOR,
81 };
82 
83 static struct logsoftc {
84 	int	sc_state;		/* see above for possibilities */
85 	struct	selinfo sc_selp;	/* process waiting on select call */
86 	struct  sigio *sc_sigio;	/* information for async I/O */
87 	struct	callout sc_callout;	/* callout to wakeup syslog  */
88 } logsoftc;
89 
90 int	log_open;			/* also used in log() */
91 
92 /* Times per second to check for a pending syslog wakeup. */
93 static int	log_wakeups_per_second = 5;
94 SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
95     &log_wakeups_per_second, 0, "");
96 
97 /*ARGSUSED*/
98 static	int
99 logopen(dev_t dev, int flags, int mode, struct thread *td)
100 {
101 	if (log_open)
102 		return (EBUSY);
103 	log_open = 1;
104 	callout_init(&logsoftc.sc_callout, 0);
105 	fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio);	/* signal process only */
106 	if (log_wakeups_per_second < 1) {
107 		printf("syslog wakeup is less than one.  Adjusting to 1.\n");
108 		log_wakeups_per_second = 1;
109 	}
110 	callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
111 	    logtimeout, NULL);
112 	return (0);
113 }
114 
115 /*ARGSUSED*/
116 static	int
117 logclose(dev_t dev, int flag, int mode, struct thread *td)
118 {
119 
120 	log_open = 0;
121 	callout_stop(&logsoftc.sc_callout);
122 	logsoftc.sc_state = 0;
123 	funsetown(&logsoftc.sc_sigio);
124 	return (0);
125 }
126 
127 /*ARGSUSED*/
128 static	int
129 logread(dev_t dev, struct uio *uio, int flag)
130 {
131 	char buf[128];
132 	struct msgbuf *mbp = msgbufp;
133 	int error = 0, l, s;
134 
135 	s = splhigh();
136 	while (msgbuf_getcount(mbp) == 0) {
137 		if (flag & IO_NDELAY) {
138 			splx(s);
139 			return (EWOULDBLOCK);
140 		}
141 		logsoftc.sc_state |= LOG_RDWAIT;
142 		if ((error = tsleep(mbp, LOG_RDPRI | PCATCH, "klog", 0))) {
143 			splx(s);
144 			return (error);
145 		}
146 	}
147 	splx(s);
148 	logsoftc.sc_state &= ~LOG_RDWAIT;
149 
150 	while (uio->uio_resid > 0) {
151 		l = imin(sizeof(buf), uio->uio_resid);
152 		l = msgbuf_getbytes(mbp, buf, l);
153 		if (l == 0)
154 			break;
155 		error = uiomove(buf, l, uio);
156 		if (error)
157 			break;
158 	}
159 	return (error);
160 }
161 
162 /*ARGSUSED*/
163 static	int
164 logpoll(dev_t dev, int events, struct thread *td)
165 {
166 	int s;
167 	int revents = 0;
168 
169 	s = splhigh();
170 
171 	if (events & (POLLIN | POLLRDNORM)) {
172 		if (msgbuf_getcount(msgbufp) > 0)
173 			revents |= events & (POLLIN | POLLRDNORM);
174 		else
175 			selrecord(td, &logsoftc.sc_selp);
176 	}
177 	splx(s);
178 	return (revents);
179 }
180 
181 static void
182 logtimeout(void *arg)
183 {
184 
185 	if (!log_open)
186 		return;
187 	if (log_wakeups_per_second < 1) {
188 		printf("syslog wakeup is less than one.  Adjusting to 1.\n");
189 		log_wakeups_per_second = 1;
190 	}
191 	if (msgbuftrigger == 0) {
192 		callout_reset(&logsoftc.sc_callout,
193 		    hz / log_wakeups_per_second, logtimeout, NULL);
194 		return;
195 	}
196 	msgbuftrigger = 0;
197 	selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI);
198 	if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
199 		pgsigio(&logsoftc.sc_sigio, SIGIO, 0);
200 	if (logsoftc.sc_state & LOG_RDWAIT) {
201 		wakeup(msgbufp);
202 		logsoftc.sc_state &= ~LOG_RDWAIT;
203 	}
204 	callout_reset(&logsoftc.sc_callout, hz / log_wakeups_per_second,
205 	    logtimeout, NULL);
206 }
207 
208 /*ARGSUSED*/
209 static	int
210 logioctl(dev_t dev, u_long com, caddr_t data, int flag, struct thread *td)
211 {
212 
213 	switch (com) {
214 
215 	/* return number of characters immediately available */
216 	case FIONREAD:
217 		*(int *)data = msgbuf_getcount(msgbufp);
218 		break;
219 
220 	case FIONBIO:
221 		break;
222 
223 	case FIOASYNC:
224 		if (*(int *)data)
225 			logsoftc.sc_state |= LOG_ASYNC;
226 		else
227 			logsoftc.sc_state &= ~LOG_ASYNC;
228 		break;
229 
230 	case FIOSETOWN:
231 		return (fsetown(*(int *)data, &logsoftc.sc_sigio));
232 
233 	case FIOGETOWN:
234 		*(int *)data = fgetown(&logsoftc.sc_sigio);
235 		break;
236 
237 	/* This is deprecated, FIOSETOWN should be used instead. */
238 	case TIOCSPGRP:
239 		return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
240 
241 	/* This is deprecated, FIOGETOWN should be used instead */
242 	case TIOCGPGRP:
243 		*(int *)data = -fgetown(&logsoftc.sc_sigio);
244 		break;
245 
246 	default:
247 		return (ENOTTY);
248 	}
249 	return (0);
250 }
251 
252 static void
253 log_drvinit(void *unused)
254 {
255 
256 	make_dev(&log_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "klog");
257 }
258 
259 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,log_drvinit,NULL)
260