]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_log.c
2 * Copyright (c) 2000 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>
65 #include <sys/vnode.h>
66 #include <sys/ioctl.h>
67 #include <sys/msgbuf.h>
69 #include <sys/errno.h>
70 #include <sys/select.h>
71 #include <kern/thread.h>
73 #include <kern/sched_prim.h>
74 #include <kern/parallel.h>
77 #define LOG_RDPRI (PZERO + 1)
80 #define LOG_ASYNC 0x04
81 #define LOG_RDWAIT 0x08
84 int sc_state
; /* see above for possibilities */
85 struct selinfo sc_selp
; /* thread waiting for select */
86 int sc_pgid
; /* process/group for async I/O */
89 int log_open
; /* also used in log() */
90 struct msgbuf temp_msgbuf
;
91 struct msgbuf
*msgbufp
;
92 static int _logentrypend
= 0;
95 * Serialize log access. Note that the log can be written at interrupt level,
96 * so any log manipulations that can be done from, or affect, another processor
97 * at interrupt level must be guarded with a spin lock.
99 decl_simple_lock_data(,log_lock
); /* stop races dead in their tracks */
100 #define LOG_LOCK() simple_lock(&log_lock)
101 #define LOG_UNLOCK() simple_unlock(&log_lock)
102 #define LOG_LOCK_INIT() simple_lock_init(&log_lock)
105 logopen(dev
, flags
, mode
, p
)
110 unix_master(); /* for pg_id, sigh */
118 logsoftc
.sc_pgid
= p
->p_pid
; /* signal process only */
120 * Potential race here with putchar() but since putchar should be
121 * called by autoconf, msg_magic should be initialized by the time
124 if (msgbufp
->msg_magic
!= MSG_MAGIC
) {
127 msgbufp
->msg_magic
= MSG_MAGIC
;
128 msgbufp
->msg_bufx
= msgbufp
->msg_bufr
= 0;
129 for (i
=0; i
< MSG_BSIZE
; i
++)
130 msgbufp
->msg_bufc
[i
] = 0;
145 selwakeup(&logsoftc
.sc_selp
);
147 selthreadclear(&logsoftc
.sc_selp
);
155 logread(dev
, uio
, flag
)
165 while (msgbufp
->msg_bufr
== msgbufp
->msg_bufx
) {
166 if (flag
& IO_NDELAY
) {
168 return (EWOULDBLOCK
);
170 if (logsoftc
.sc_state
& LOG_NBIO
) {
172 return (EWOULDBLOCK
);
174 logsoftc
.sc_state
|= LOG_RDWAIT
;
175 if (error
= tsleep((caddr_t
)msgbufp
, LOG_RDPRI
| PCATCH
,
182 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
184 while (uio
->uio_resid
> 0) {
185 l
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
187 l
= MSG_BSIZE
- msgbufp
->msg_bufr
;
188 l
= min(l
, uio
->uio_resid
);
191 error
= uiomove((caddr_t
)&msgbufp
->msg_bufc
[msgbufp
->msg_bufr
],
195 msgbufp
->msg_bufr
+= l
;
196 if (msgbufp
->msg_bufr
< 0 || msgbufp
->msg_bufr
>= MSG_BSIZE
)
197 msgbufp
->msg_bufr
= 0;
204 logselect(dev
, rw
, wql
, p
)
215 if (msgbufp
->msg_bufr
!= msgbufp
->msg_bufx
) {
219 selrecord(p
, &logsoftc
.sc_selp
, wql
);
231 boolean_t funnel_state
;
235 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
236 selwakeup(&logsoftc
.sc_selp
);
237 if (logsoftc
.sc_state
& LOG_ASYNC
) {
240 pgid
= logsoftc
.sc_pgid
;
243 gsignal(-pgid
, SIGIO
);
244 else if (p
= pfind(pgid
))
248 if (logsoftc
.sc_state
& LOG_RDWAIT
) {
249 wakeup((caddr_t
)msgbufp
);
250 logsoftc
.sc_state
&= ~LOG_RDWAIT
;
252 (void) thread_funnel_set(kernel_flock
, funnel_state
);
267 logioctl(com
, data
, flag
)
275 /* return number of characters immediately available */
278 l
= msgbufp
->msg_bufx
- msgbufp
->msg_bufr
;
287 logsoftc
.sc_state
|= LOG_NBIO
;
289 logsoftc
.sc_state
&= ~LOG_NBIO
;
294 logsoftc
.sc_state
|= LOG_ASYNC
;
296 logsoftc
.sc_state
&= ~LOG_ASYNC
;
301 logsoftc
.sc_pgid
= *(int *)data
;
307 *(int *)data
= logsoftc
.sc_pgid
;
320 msgbufp
= &temp_msgbuf
;
327 register struct msgbuf
*mbp
;
330 msgbufp
=&temp_msgbuf
;
333 if (mbp
-> msg_magic
!= MSG_MAGIC
) {
336 mbp
->msg_magic
= MSG_MAGIC
;
337 mbp
->msg_bufx
= mbp
->msg_bufr
= 0;
338 for (i
=0; i
< MSG_BSIZE
; i
++)
339 mbp
->msg_bufc
[i
] = 0;
341 mbp
->msg_bufc
[mbp
->msg_bufx
++] = c
;
343 if (mbp
->msg_bufx
< 0 || mbp
->msg_bufx
>= MSG_BSIZE
)