]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_log.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)subr_log.c 8.3 (Berkeley) 2/14/95
59 * Error log buffer for kernel printf's.
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/proc_internal.h>
65 #include <sys/vnode.h>
66 #include <sys/ioctl.h>
67 #include <sys/msgbuf.h>
68 #include <sys/file_internal.h>
69 #include <sys/errno.h>
70 #include <sys/select.h>
71 #include <sys/kernel.h>
72 #include <kern/thread.h>
75 #define LOG_RDPRI (PZERO + 1)
78 #define LOG_ASYNC 0x04
79 #define LOG_RDWAIT 0x08
82 int sc_state
; /* see above for possibilities */
83 struct selinfo sc_selp
; /* thread waiting for select */
84 int sc_pgid
; /* process/group for async I/O */
87 int log_open
; /* also used in log() */
88 struct msgbuf temp_msgbuf
;
89 struct msgbuf
*msgbufp
;
90 static int _logentrypend
= 0;
91 static int log_inited
= 0;
92 void bsd_log_lock(void);
93 /* the following two are implemented in osfmk/kern/printf.c */
94 extern void bsd_log_unlock(void);
95 extern void bsd_log_init(void);
98 * Serialize log access. Note that the log can be written at interrupt level,
99 * so any log manipulations that can be done from, or affect, another processor
100 * at interrupt level must be guarded with a spin lock.
103 #define LOG_LOCK() bsd_log_lock()
104 #define LOG_UNLOCK() bsd_log_unlock()
108 logopen(dev
, flags
, mode
, p
)
119 logsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
121 * Potential race here with putchar() but since putchar should be
122 * called by autoconf, msg_magic should be initialized by the time
125 if (msgbufp
->msg_magic
!= MSG_MAGIC
) {
128 msgbufp
->msg_magic
= MSG_MAGIC
;
129 msgbufp
->msg_bufx
= msgbufp
->msg_bufr
= 0;
130 for (i
=0; i
< MSG_BSIZE
; i
++)
131 msgbufp
->msg_bufc
[i
] = 0;
146 selwakeup(&logsoftc
.sc_selp
);
147 selthreadclear(&logsoftc
.sc_selp
);
154 logread(dev
, uio
, flag
)
162 char localbuff
[MSG_BSIZE
];
166 while (msgbufp
->msg_bufr
== msgbufp
->msg_bufx
) {
167 if (flag
& IO_NDELAY
) {
171 if (logsoftc
.sc_state
& LOG_NBIO
) {
175 logsoftc
.sc_state
|= LOG_RDWAIT
;
178 * If the wakeup is missed the ligtening bolt will wake this up
179 * if there are any new characters. If that doesn't do it
180 * then wait for 5 sec and reevaluate
182 if (error
= tsleep((caddr_t
)msgbufp
, LOG_RDPRI
| PCATCH
,
184 /* if it times out; ignore */
185 if (error
!= EWOULDBLOCK
)
190 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
193 while (uio_resid(uio
) > 0) {
194 l
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
196 l
= MSG_BSIZE
- msgbufp
->msg_bufr
;
197 l
= min(l
, uio_resid(uio
));
200 bcopy(&msgbufp
->msg_bufc
[msgbufp
->msg_bufr
], &localbuff
[0], l
);
202 error
= uiomove((caddr_t
)&localbuff
[0],
207 msgbufp
->msg_bufr
+= l
;
208 if (msgbufp
->msg_bufr
< 0 || msgbufp
->msg_bufr
>= MSG_BSIZE
)
209 msgbufp
->msg_bufr
= 0;
218 logselect(dev
, rw
, wql
, p
)
229 if (msgbufp
->msg_bufr
!= msgbufp
->msg_bufx
) {
233 selrecord(p
, &logsoftc
.sc_selp
, wql
);
245 boolean_t funnel_state
;
252 selwakeup(&logsoftc
.sc_selp
);
253 if (logsoftc
.sc_state
& LOG_ASYNC
) {
254 pgid
= logsoftc
.sc_pgid
;
257 gsignal(-pgid
, SIGIO
);
258 else if (p
= pfind(pgid
))
262 if (logsoftc
.sc_state
& LOG_RDWAIT
) {
263 wakeup((caddr_t
)msgbufp
);
264 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
281 logioctl(dev
, com
, data
, flag
)
290 /* return number of characters immediately available */
292 l
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
300 logsoftc
.sc_state
|= LOG_NBIO
;
302 logsoftc
.sc_state
&= ~LOG_NBIO
;
307 logsoftc
.sc_state
|= LOG_ASYNC
;
309 logsoftc
.sc_state
&= ~LOG_ASYNC
;
313 logsoftc
.sc_pgid
= *(int *)data
;
317 *(int *)data
= logsoftc
.sc_pgid
;
332 msgbufp
= &temp_msgbuf
;
340 register struct msgbuf
*mbp
;
343 panic("bsd log is not inited");
348 if (mbp
-> msg_magic
!= MSG_MAGIC
) {
351 mbp
->msg_magic
= MSG_MAGIC
;
352 mbp
->msg_bufx
= mbp
->msg_bufr
= 0;
353 for (i
=0; i
< MSG_BSIZE
; i
++)
354 mbp
->msg_bufc
[i
] = 0;
356 mbp
->msg_bufc
[mbp
->msg_bufx
++] = c
;
358 if (mbp
->msg_bufx
< 0 || mbp
->msg_bufx
>= MSG_BSIZE
)