1 /* $NetBSD: subr_log.c,v 1.49 2008/03/21 21:55:00 ad Exp $ */
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1982, 1986, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)subr_log.c 8.3 (Berkeley) 2/14/95
64 * Error log buffer for kernel printf's.
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: subr_log.c,v 1.49 2008/03/21 21:55:00 ad Exp $");
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
74 #include <sys/vnode.h>
75 #include <sys/ioctl.h>
76 #include <sys/msgbuf.h>
78 #include <sys/syslog.h>
80 #include <sys/select.h>
84 static void logsoftintr(void *);
86 static bool log_async
;
87 static struct selinfo log_selp
; /* process waiting on select call */
88 static pid_t log_pgid
; /* process/group for async I/O */
89 static kcondvar_t log_cv
;
93 int log_open
; /* also used in log() */
94 int msgbufmapped
; /* is the message buffer mapped */
95 int msgbufenabled
; /* is logging to the buffer enabled */
96 struct kern_msgbuf
*msgbufp
; /* the mapped buffer, itself. */
99 initmsgbuf(void *bf
, size_t bufsize
)
101 struct kern_msgbuf
*mbp
;
104 /* Sanity-check the given size. */
105 if (bufsize
< sizeof(struct kern_msgbuf
))
108 mbp
= msgbufp
= (struct kern_msgbuf
*)bf
;
110 new_bufs
= bufsize
- offsetof(struct kern_msgbuf
, msg_bufc
);
111 if ((mbp
->msg_magic
!= MSG_MAGIC
) || (mbp
->msg_bufs
!= new_bufs
) ||
112 (mbp
->msg_bufr
< 0) || (mbp
->msg_bufr
>= mbp
->msg_bufs
) ||
113 (mbp
->msg_bufx
< 0) || (mbp
->msg_bufx
>= mbp
->msg_bufs
)) {
115 * If the buffer magic number is wrong, has changed
116 * size (which shouldn't happen often), or is
117 * internally inconsistent, initialize it.
120 memset(bf
, 0, bufsize
);
121 mbp
->msg_magic
= MSG_MAGIC
;
122 mbp
->msg_bufs
= new_bufs
;
125 /* mark it as ready for use. */
126 msgbufmapped
= msgbufenabled
= 1;
133 mutex_init(&log_lock
, MUTEX_DEFAULT
, IPL_VM
);
135 cv_init(&log_cv
, "klog");
136 log_sih
= softint_establish(SOFTINT_CLOCK
| SOFTINT_MPSAFE
,
142 logopen(dev_t dev
, int flags
, int mode
, struct lwp
*l
)
144 struct kern_msgbuf
*mbp
= msgbufp
;
147 mutex_spin_enter(&log_lock
);
152 log_pgid
= l
->l_proc
->p_pid
; /* signal process only */
154 * The message buffer is initialized during system
155 * configuration. If it's been clobbered, note that
156 * and return an error. (This allows a user to read
157 * the buffer via /dev/kmem, and try to figure out
160 if (mbp
->msg_magic
!= MSG_MAGIC
) {
165 mutex_spin_exit(&log_lock
);
172 logclose(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
175 mutex_spin_enter(&log_lock
);
179 mutex_spin_exit(&log_lock
);
186 logread(dev_t dev
, struct uio
*uio
, int flag
)
188 struct kern_msgbuf
*mbp
= msgbufp
;
192 mutex_spin_enter(&log_lock
);
193 while (mbp
->msg_bufr
== mbp
->msg_bufx
) {
194 if (flag
& IO_NDELAY
) {
195 mutex_spin_exit(&log_lock
);
198 error
= cv_wait_sig(&log_cv
, &log_lock
);
200 mutex_spin_exit(&log_lock
);
204 while (uio
->uio_resid
> 0) {
205 l
= mbp
->msg_bufx
- mbp
->msg_bufr
;
207 l
= mbp
->msg_bufs
- mbp
->msg_bufr
;
208 l
= min(l
, uio
->uio_resid
);
211 mutex_spin_exit(&log_lock
);
212 error
= uiomove(&mbp
->msg_bufc
[mbp
->msg_bufr
], (int)l
, uio
);
213 mutex_spin_enter(&log_lock
);
217 if (mbp
->msg_bufr
< 0 || mbp
->msg_bufr
>= mbp
->msg_bufs
)
220 mutex_spin_exit(&log_lock
);
227 logpoll(dev_t dev
, int events
, struct lwp
*l
)
231 if (events
& (POLLIN
| POLLRDNORM
)) {
232 mutex_spin_enter(&log_lock
);
233 if (msgbufp
->msg_bufr
!= msgbufp
->msg_bufx
)
234 revents
|= events
& (POLLIN
| POLLRDNORM
);
236 selrecord(l
, &log_selp
);
237 mutex_spin_exit(&log_lock
);
244 filt_logrdetach(struct knote
*kn
)
247 mutex_spin_enter(&log_lock
);
248 SLIST_REMOVE(&log_selp
.sel_klist
, kn
, knote
, kn_selnext
);
249 mutex_spin_exit(&log_lock
);
253 filt_logread(struct knote
*kn
, long hint
)
257 if ((hint
& NOTE_SUBMIT
) == 0)
258 mutex_spin_enter(&log_lock
);
259 if (msgbufp
->msg_bufr
== msgbufp
->msg_bufx
) {
261 } else if (msgbufp
->msg_bufr
< msgbufp
->msg_bufx
) {
262 kn
->kn_data
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
265 kn
->kn_data
= (msgbufp
->msg_bufs
- msgbufp
->msg_bufr
) +
269 if ((hint
& NOTE_SUBMIT
) == 0)
270 mutex_spin_exit(&log_lock
);
275 static const struct filterops logread_filtops
=
276 { 1, NULL
, filt_logrdetach
, filt_logread
};
279 logkqfilter(dev_t dev
, struct knote
*kn
)
283 switch (kn
->kn_filter
) {
285 klist
= &log_selp
.sel_klist
;
286 kn
->kn_fop
= &logread_filtops
;
293 mutex_spin_enter(&log_lock
);
295 SLIST_INSERT_HEAD(klist
, kn
, kn_selnext
);
296 mutex_spin_exit(&log_lock
);
305 if (!cold
&& log_open
) {
306 mutex_spin_enter(&log_lock
);
307 selnotify(&log_selp
, 0, NOTE_SUBMIT
);
309 softint_schedule(log_sih
);
310 cv_broadcast(&log_cv
);
311 mutex_spin_exit(&log_lock
);
316 logsoftintr(void *cookie
)
320 if ((pid
= log_pgid
) != 0)
321 fownsignal(pid
, SIGIO
, 0, 0, NULL
);
326 logioctl(dev_t dev
, u_long com
, void *data
, int flag
, struct lwp
*lwp
)
332 /* return number of characters immediately available */
334 mutex_spin_enter(&log_lock
);
335 l
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
337 l
+= msgbufp
->msg_bufs
;
338 mutex_spin_exit(&log_lock
);
346 /* No locking needed, 'thread private'. */
347 log_async
= (*((int *)data
) != 0);
352 return fsetown(&log_pgid
, com
, data
);
356 return fgetown(log_pgid
, com
, data
);
359 return (EPASSTHROUGH
);
367 struct kern_msgbuf
*mbp
;
370 mutex_spin_enter(&log_lock
);
373 if (mbp
->msg_magic
!= MSG_MAGIC
) {
375 * Arguably should panic or somehow notify the
376 * user... but how? Panic may be too drastic,
377 * and would obliterate the message being kicked
378 * out (maybe a panic itself), and printf
379 * would invoke us recursively. Silently punt
380 * for now. If syslog is running, it should
385 mbp
->msg_bufc
[mbp
->msg_bufx
++] = c
;
386 if (mbp
->msg_bufx
< 0 || mbp
->msg_bufx
>= mbp
->msg_bufs
)
388 /* If the buffer is full, keep the most recent data. */
389 if (mbp
->msg_bufr
== mbp
->msg_bufx
) {
390 if (++mbp
->msg_bufr
>= mbp
->msg_bufs
)
396 mutex_spin_exit(&log_lock
);
399 const struct cdevsw log_cdevsw
= {
400 logopen
, logclose
, logread
, nowrite
, logioctl
,
401 nostop
, notty
, logpoll
, nommap
, logkqfilter
,