2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * Error log buffer for kernel printf's.
36 #include <sys/param.h>
37 #include <sys/systm.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>
47 #include <sys/filedesc.h>
48 #include <sys/sysctl.h>
50 #define LOG_RDPRI (PZERO + 1)
52 #define LOG_ASYNC 0x04
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;
61 static void logtimeout(void *arg);
63 static struct cdevsw log_cdevsw = {
64 .d_version = D_VERSION,
70 .d_kqfilter = logkqfilter,
74 static int logkqread(struct knote *note, long hint);
75 static void logkqdetach(struct knote *note);
77 static struct filterops log_read_filterops = {
80 .f_detach = logkqdetach,
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 */
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);
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.");
103 logopen(struct cdev *dev, int flags, int mode, struct thread *td)
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;
111 mtx_lock(&msgbuf_lock);
113 mtx_unlock(&msgbuf_lock);
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);
121 fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio); /* signal process only */
127 logclose(struct cdev *dev, int flag, int mode, struct thread *td)
130 funsetown(&logsoftc.sc_sigio);
132 mtx_lock(&msgbuf_lock);
133 callout_stop(&logsoftc.sc_callout);
134 logsoftc.sc_state = 0;
136 mtx_unlock(&msgbuf_lock);
143 logread(struct cdev *dev, struct uio *uio, int flag)
146 struct msgbuf *mbp = msgbufp;
149 mtx_lock(&msgbuf_lock);
150 while (msgbuf_getcount(mbp) == 0) {
151 if (flag & IO_NDELAY) {
152 mtx_unlock(&msgbuf_lock);
153 return (EWOULDBLOCK);
155 if ((error = cv_wait_sig(&log_wakeup, &msgbuf_lock)) != 0) {
156 mtx_unlock(&msgbuf_lock);
161 while (uio->uio_resid > 0) {
162 l = imin(sizeof(buf), uio->uio_resid);
163 l = msgbuf_getbytes(mbp, buf, l);
166 mtx_unlock(&msgbuf_lock);
167 error = uiomove(buf, l, uio);
168 if (error || uio->uio_resid == 0)
170 mtx_lock(&msgbuf_lock);
172 mtx_unlock(&msgbuf_lock);
178 logpoll(struct cdev *dev, int events, struct thread *td)
182 if (events & (POLLIN | POLLRDNORM)) {
183 mtx_lock(&msgbuf_lock);
184 if (msgbuf_getcount(msgbufp) > 0)
185 revents |= events & (POLLIN | POLLRDNORM);
187 selrecord(td, &logsoftc.sc_selp);
188 mtx_unlock(&msgbuf_lock);
194 logkqfilter(struct cdev *dev, struct knote *kn)
197 if (kn->kn_filter != EVFILT_READ)
200 kn->kn_fop = &log_read_filterops;
203 mtx_lock(&msgbuf_lock);
204 knlist_add(&logsoftc.sc_selp.si_note, kn, 1);
205 mtx_unlock(&msgbuf_lock);
210 logkqread(struct knote *kn, long hint)
213 mtx_assert(&msgbuf_lock, MA_OWNED);
214 kn->kn_data = msgbuf_getcount(msgbufp);
215 return (kn->kn_data != 0);
219 logkqdetach(struct knote *kn)
222 mtx_lock(&msgbuf_lock);
223 knlist_remove(&logsoftc.sc_selp.si_note, kn, 1);
224 mtx_unlock(&msgbuf_lock);
228 logtimeout(void *arg)
233 if (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);
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;
246 callout_reset_sbt(&logsoftc.sc_callout,
247 SBT_1S / log_wakeups_per_second, 0, logtimeout, NULL, C_PREL(1));
252 logioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td)
256 /* return number of characters immediately available */
258 *(int *)data = msgbuf_getcount(msgbufp);
265 mtx_lock(&msgbuf_lock);
267 logsoftc.sc_state |= LOG_ASYNC;
269 logsoftc.sc_state &= ~LOG_ASYNC;
270 mtx_unlock(&msgbuf_lock);
274 return (fsetown(*(int *)data, &logsoftc.sc_sigio));
277 *(int *)data = fgetown(&logsoftc.sc_sigio);
280 /* This is deprecated, FIOSETOWN should be used instead. */
282 return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
284 /* This is deprecated, FIOGETOWN should be used instead */
286 *(int *)data = -fgetown(&logsoftc.sc_sigio);
296 log_drvinit(void *unused)
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");
306 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,log_drvinit,NULL);