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
31 * @(#)subr_log.c 8.1 (Berkeley) 6/10/93
35 * Error log buffer for kernel printf's.
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
43 #include <sys/vnode.h>
44 #include <sys/filio.h>
45 #include <sys/ttycom.h>
46 #include <sys/msgbuf.h>
47 #include <sys/signalvar.h>
48 #include <sys/kernel.h>
50 #include <sys/filedesc.h>
51 #include <sys/sysctl.h>
53 #define LOG_RDPRI (PZERO + 1)
55 #define LOG_ASYNC 0x04
57 static d_open_t logopen;
58 static d_close_t logclose;
59 static d_read_t logread;
60 static d_ioctl_t logioctl;
61 static d_poll_t logpoll;
62 static d_kqfilter_t logkqfilter;
64 static void logtimeout(void *arg);
66 static struct cdevsw log_cdevsw = {
67 .d_version = D_VERSION,
73 .d_kqfilter = logkqfilter,
77 static int logkqread(struct knote *note, long hint);
78 static void logkqdetach(struct knote *note);
80 static struct filterops log_read_filterops = {
83 .f_detach = logkqdetach,
87 static struct logsoftc {
88 int sc_state; /* see above for possibilities */
89 struct selinfo sc_selp; /* process waiting on select call */
90 struct sigio *sc_sigio; /* information for async I/O */
91 struct callout sc_callout; /* callout to wakeup syslog */
94 int log_open; /* also used in log() */
95 static struct cv log_wakeup;
96 struct mtx msgbuf_lock;
97 MTX_SYSINIT(msgbuf_lock, &msgbuf_lock, "msgbuf lock", MTX_DEF);
99 static int log_wakeups_per_second = 5;
100 SYSCTL_INT(_kern, OID_AUTO, log_wakeups_per_second, CTLFLAG_RW,
101 &log_wakeups_per_second, 0,
102 "How often (times per second) to check for /dev/log waiters.");
106 logopen(struct cdev *dev, int flags, int mode, struct thread *td)
109 if (log_wakeups_per_second < 1) {
110 printf("syslog wakeup is less than one. Adjusting to 1.\n");
111 log_wakeups_per_second = 1;
114 mtx_lock(&msgbuf_lock);
116 mtx_unlock(&msgbuf_lock);
120 callout_reset_sbt(&logsoftc.sc_callout,
121 SBT_1S / log_wakeups_per_second, 0, logtimeout, NULL, C_PREL(1));
122 mtx_unlock(&msgbuf_lock);
124 fsetown(td->td_proc->p_pid, &logsoftc.sc_sigio); /* signal process only */
130 logclose(struct cdev *dev, int flag, int mode, struct thread *td)
133 funsetown(&logsoftc.sc_sigio);
135 mtx_lock(&msgbuf_lock);
136 callout_stop(&logsoftc.sc_callout);
137 logsoftc.sc_state = 0;
139 mtx_unlock(&msgbuf_lock);
146 logread(struct cdev *dev, struct uio *uio, int flag)
149 struct msgbuf *mbp = msgbufp;
152 mtx_lock(&msgbuf_lock);
153 while (msgbuf_getcount(mbp) == 0) {
154 if (flag & IO_NDELAY) {
155 mtx_unlock(&msgbuf_lock);
156 return (EWOULDBLOCK);
158 if ((error = cv_wait_sig(&log_wakeup, &msgbuf_lock)) != 0) {
159 mtx_unlock(&msgbuf_lock);
164 while (uio->uio_resid > 0) {
165 l = imin(sizeof(buf), uio->uio_resid);
166 l = msgbuf_getbytes(mbp, buf, l);
169 mtx_unlock(&msgbuf_lock);
170 error = uiomove(buf, l, uio);
171 if (error || uio->uio_resid == 0)
173 mtx_lock(&msgbuf_lock);
175 mtx_unlock(&msgbuf_lock);
181 logpoll(struct cdev *dev, int events, struct thread *td)
185 if (events & (POLLIN | POLLRDNORM)) {
186 mtx_lock(&msgbuf_lock);
187 if (msgbuf_getcount(msgbufp) > 0)
188 revents |= events & (POLLIN | POLLRDNORM);
190 selrecord(td, &logsoftc.sc_selp);
191 mtx_unlock(&msgbuf_lock);
197 logkqfilter(struct cdev *dev, struct knote *kn)
200 if (kn->kn_filter != EVFILT_READ)
203 kn->kn_fop = &log_read_filterops;
206 mtx_lock(&msgbuf_lock);
207 knlist_add(&logsoftc.sc_selp.si_note, kn, 1);
208 mtx_unlock(&msgbuf_lock);
213 logkqread(struct knote *kn, long hint)
216 mtx_assert(&msgbuf_lock, MA_OWNED);
217 kn->kn_data = msgbuf_getcount(msgbufp);
218 return (kn->kn_data != 0);
222 logkqdetach(struct knote *kn)
225 mtx_lock(&msgbuf_lock);
226 knlist_remove(&logsoftc.sc_selp.si_note, kn, 1);
227 mtx_unlock(&msgbuf_lock);
231 logtimeout(void *arg)
236 if (msgbuftrigger == 0)
239 selwakeuppri(&logsoftc.sc_selp, LOG_RDPRI);
240 KNOTE_LOCKED(&logsoftc.sc_selp.si_note, 0);
241 if ((logsoftc.sc_state & LOG_ASYNC) && logsoftc.sc_sigio != NULL)
242 pgsigio(&logsoftc.sc_sigio, SIGIO, 0);
243 cv_broadcastpri(&log_wakeup, LOG_RDPRI);
245 if (log_wakeups_per_second < 1) {
246 printf("syslog wakeup is less than one. Adjusting to 1.\n");
247 log_wakeups_per_second = 1;
249 callout_reset_sbt(&logsoftc.sc_callout,
250 SBT_1S / log_wakeups_per_second, 0, logtimeout, NULL, C_PREL(1));
255 logioctl(struct cdev *dev, u_long com, caddr_t data, int flag, struct thread *td)
259 /* return number of characters immediately available */
261 *(int *)data = msgbuf_getcount(msgbufp);
268 mtx_lock(&msgbuf_lock);
270 logsoftc.sc_state |= LOG_ASYNC;
272 logsoftc.sc_state &= ~LOG_ASYNC;
273 mtx_unlock(&msgbuf_lock);
277 return (fsetown(*(int *)data, &logsoftc.sc_sigio));
280 *(int *)data = fgetown(&logsoftc.sc_sigio);
283 /* This is deprecated, FIOSETOWN should be used instead. */
285 return (fsetown(-(*(int *)data), &logsoftc.sc_sigio));
287 /* This is deprecated, FIOGETOWN should be used instead */
289 *(int *)data = -fgetown(&logsoftc.sc_sigio);
299 log_drvinit(void *unused)
302 cv_init(&log_wakeup, "klog");
303 callout_init_mtx(&logsoftc.sc_callout, &msgbuf_lock, 0);
304 knlist_init_mtx(&logsoftc.sc_selp.si_note, &msgbuf_lock);
305 make_dev_credf(MAKEDEV_ETERNAL, &log_cdevsw, 0, NULL, UID_ROOT,
306 GID_WHEEL, 0600, "klog");
309 SYSINIT(logdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,log_drvinit,NULL);