xref: /freebsd/sys/kern/subr_log.c (revision db33c6f3ae9d1231087710068ee4ea5398aacca7)
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/param.h>
37 #include <sys/systm.h>
38 #include <sys/conf.h>
39 #include <sys/proc.h>
40 #include <sys/vnode.h>
41 #include <sys/filio.h>
42 #include <sys/ttycom.h>
43 #include <sys/msgbuf.h>
44 #include <sys/signalvar.h>
45 #include <sys/kernel.h>
46 #include <sys/poll.h>
47 #include <sys/filedesc.h>
48 #include <sys/sysctl.h>
49 
50 #define LOG_RDPRI	(PZERO + 1)
51 
52 #define LOG_ASYNC	0x04
53 
54 static	d_open_t	logopen;
55 static	d_close_t	logclose;
56 static	d_read_t	logread;
57 static	d_ioctl_t	logioctl;
58 static	d_poll_t	logpoll;
59 static	d_kqfilter_t	logkqfilter;
60 
61 static	void logtimeout(void *arg);
62 
63 static struct cdevsw log_cdevsw = {
64 	.d_version =	D_VERSION,
65 	.d_open =	logopen,
66 	.d_close =	logclose,
67 	.d_read =	logread,
68 	.d_ioctl =	logioctl,
69 	.d_poll =	logpoll,
70 	.d_kqfilter =	logkqfilter,
71 	.d_name =	"log",
72 };
73 
74 static int	logkqread(struct knote *note, long hint);
75 static void	logkqdetach(struct knote *note);
76 
77 static const struct filterops log_read_filterops = {
78 	.f_isfd =	1,
79 	.f_attach =	NULL,
80 	.f_detach =	logkqdetach,
81 	.f_event =	logkqread,
82 };
83 
84 static struct logsoftc {
85 	int	sc_state;		/* see above for possibilities */
86 	struct	selinfo sc_selp;	/* process waiting on select call */
87 	struct  sigio *sc_sigio;	/* information for async I/O */
88 	struct	callout sc_callout;	/* callout to wakeup syslog  */
89 } logsoftc;
90 
91 int			log_open;	/* also used in log() */
92 static struct cv	log_wakeup;
93 struct mtx		msgbuf_lock;
94 MTX_SYSINIT(msgbuf_lock, &msgbuf_lock, "msgbuf lock", MTX_DEF);
95 
96 static int	log_wakeups_per_second = 5;
97 SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
98     &log_wakeups_per_second, 0,
99     "How often (times per second) to check for /dev/log waiters.");
100 
101 /*ARGSUSED*/
102 static	int
103 logopen(struct cdev *dev, int flags, int mode, struct thread *td)
104 {
105 
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 
111 	mtx_lock(&msgbuf_lock);
112 	if (log_open) {
113 		mtx_unlock(&msgbuf_lock);
114 		return (EBUSY);
115 	}
116 	log_open = 1;
117 	callout_reset_sbt(&logsoftc.sc_callout,
118 	    SBT_1S / log_wakeups_per_second, 0, logtimeout, NULL, C_PREL(1));
119 	mtx_unlock(&msgbuf_lock);
120 
121 	fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio);	/* signal process only */
122 	return (0);
123 }
124 
125 /*ARGSUSED*/
126 static	int
127 logclose(struct cdev *dev, int flag, int mode, struct thread *td)
128 {
129 
130 	funsetown(&logsoftc.sc_sigio);
131 
132 	mtx_lock(&msgbuf_lock);
133 	callout_stop(&logsoftc.sc_callout);
134 	logsoftc.sc_state = 0;
135 	log_open = 0;
136 	mtx_unlock(&msgbuf_lock);
137 
138 	return (0);
139 }
140 
141 /*ARGSUSED*/
142 static	int
143 logread(struct cdev *dev, struct uio *uio, int flag)
144 {
145 	char buf[128];
146 	struct msgbuf *mbp = msgbufp;
147 	int error = 0, l;
148 
149 	mtx_lock(&msgbuf_lock);
150 	while (msgbuf_getcount(mbp) == 0) {
151 		if (flag & IO_NDELAY) {
152 			mtx_unlock(&msgbuf_lock);
153 			return (EWOULDBLOCK);
154 		}
155 		if ((error = cv_wait_sig(&log_wakeup, &msgbuf_lock)) != 0) {
156 			mtx_unlock(&msgbuf_lock);
157 			return (error);
158 		}
159 	}
160 
161 	while (uio->uio_resid > 0) {
162 		l = imin(sizeof(buf), uio->uio_resid);
163 		l = msgbuf_getbytes(mbp, buf, l);
164 		if (l == 0)
165 			break;
166 		mtx_unlock(&msgbuf_lock);
167 		error = uiomove(buf, l, uio);
168 		if (error || uio->uio_resid == 0)
169 			return (error);
170 		mtx_lock(&msgbuf_lock);
171 	}
172 	mtx_unlock(&msgbuf_lock);
173 	return (error);
174 }
175 
176 /*ARGSUSED*/
177 static	int
178 logpoll(struct cdev *dev, int events, struct thread *td)
179 {
180 	int revents = 0;
181 
182 	if (events & (POLLIN | POLLRDNORM)) {
183 		mtx_lock(&msgbuf_lock);
184 		if (msgbuf_getcount(msgbufp) > 0)
185 			revents |= events & (POLLIN | POLLRDNORM);
186 		else
187 			selrecord(td, &logsoftc.sc_selp);
188 		mtx_unlock(&msgbuf_lock);
189 	}
190 	return (revents);
191 }
192 
193 static int
194 logkqfilter(struct cdev *dev, struct knote *kn)
195 {
196 
197 	if (kn->kn_filter != EVFILT_READ)
198 		return (EINVAL);
199 
200 	kn->kn_fop = &log_read_filterops;
201 	kn->kn_hook = NULL;
202 
203 	mtx_lock(&msgbuf_lock);
204 	knlist_add(&logsoftc.sc_selp.si_note, kn, 1);
205 	mtx_unlock(&msgbuf_lock);
206 	return (0);
207 }
208 
209 static int
210 logkqread(struct knote *kn, long hint)
211 {
212 
213 	mtx_assert(&msgbuf_lock, MA_OWNED);
214 	kn->kn_data = msgbuf_getcount(msgbufp);
215 	return (kn->kn_data != 0);
216 }
217 
218 static void
219 logkqdetach(struct knote *kn)
220 {
221 
222 	mtx_lock(&msgbuf_lock);
223 	knlist_remove(&logsoftc.sc_selp.si_note, kn, 1);
224 	mtx_unlock(&msgbuf_lock);
225 }
226 
227 static void
228 logtimeout(void *arg)
229 {
230 
231 	if (!log_open)
232 		return;
233 	if (msgbuftrigger == 0)
234 		goto done;
235 	msgbuftrigger = 0;
236 	selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI);
237 	KNOTE_LOCKED(&logsoftc.sc_selp.si_note, 0);
238 	if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
239 		pgsigio(&logsoftc.sc_sigio, SIGIO, 0);
240 	cv_broadcastpri(&log_wakeup, LOG_RDPRI);
241 done:
242 	if (log_wakeups_per_second < 1) {
243 		printf("syslog wakeup is less than one.  Adjusting to 1.\n");
244 		log_wakeups_per_second = 1;
245 	}
246 	callout_reset_sbt(&logsoftc.sc_callout,
247 	    SBT_1S / log_wakeups_per_second, 0, logtimeout, NULL, C_PREL(1));
248 }
249 
250 /*ARGSUSED*/
251 static	int
252 logioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td)
253 {
254 
255 	switch (com) {
256 	/* return number of characters immediately available */
257 	case FIONREAD:
258 		*(int *)data = msgbuf_getcount(msgbufp);
259 		break;
260 
261 	case FIONBIO:
262 		break;
263 
264 	case FIOASYNC:
265 		mtx_lock(&msgbuf_lock);
266 		if (*(int *)data)
267 			logsoftc.sc_state |= LOG_ASYNC;
268 		else
269 			logsoftc.sc_state &= ~LOG_ASYNC;
270 		mtx_unlock(&msgbuf_lock);
271 		break;
272 
273 	case FIOSETOWN:
274 		return (fsetown(*(int *)data, &logsoftc.sc_sigio));
275 
276 	case FIOGETOWN:
277 		*(int *)data = fgetown(&logsoftc.sc_sigio);
278 		break;
279 
280 	/* This is deprecated, FIOSETOWN should be used instead. */
281 	case TIOCSPGRP:
282 		return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
283 
284 	/* This is deprecated, FIOGETOWN should be used instead */
285 	case TIOCGPGRP:
286 		*(int *)data = -fgetown(&logsoftc.sc_sigio);
287 		break;
288 
289 	default:
290 		return (ENOTTY);
291 	}
292 	return (0);
293 }
294 
295 static void
296 log_drvinit(void *unused)
297 {
298 
299 	cv_init(&log_wakeup, "klog");
300 	callout_init_mtx(&logsoftc.sc_callout, &msgbuf_lock, 0);
301 	knlist_init_mtx(&logsoftc.sc_selp.si_note, &msgbuf_lock);
302 	make_dev_credf(MAKEDEV_ETERNAL, &log_cdevsw, 0, NULL, UID_ROOT,
303 	    GID_WHEEL, 0600, "klog");
304 }
305 
306 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,log_drvinit,NULL);
307