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