]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/subr_log.c
xnu-124.7.tar.gz
[apple/xnu.git] / bsd / kern / subr_log.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23/*
24 * Copyright (c) 1982, 1986, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
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.
42 *
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
53 * SUCH DAMAGE.
54 *
55 * @(#)subr_log.c 8.3 (Berkeley) 2/14/95
56 */
57
58/*
59 * Error log buffer for kernel printf's.
60 */
61
62#include <sys/param.h>
63#include <sys/systm.h>
64#include <sys/proc.h>
65#include <sys/vnode.h>
66#include <sys/ioctl.h>
67#include <sys/msgbuf.h>
68#include <sys/file.h>
69#include <sys/errno.h>
70#include <sys/select.h>
71#include <kern/thread.h>
72#if 0 /* [ */
73#include <kern/sched_prim.h>
74#include <kern/parallel.h>
75#endif /* 0 ] */
76
77#define LOG_RDPRI (PZERO + 1)
78
79#define LOG_NBIO 0x02
80#define LOG_ASYNC 0x04
81#define LOG_RDWAIT 0x08
82
83struct logsoftc {
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 */
87} logsoftc;
88
89int log_open; /* also used in log() */
90struct msgbuf temp_msgbuf;
91struct msgbuf *msgbufp;
92static int _logentrypend = 0;
93
94/*
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.
98 */
99decl_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)
103
104/*ARGSUSED*/
105logopen(dev, flags, mode, p)
106 dev_t dev;
107 int flags, mode;
108 struct proc *p;
109{
110 unix_master(); /* for pg_id, sigh */
111 LOG_LOCK();
112 if (log_open) {
113 LOG_UNLOCK();
114 unix_release();
115 return (EBUSY);
116 }
117 log_open = 1;
118 logsoftc.sc_pgid = p->p_pid; /* signal process only */
119 /*
120 * Potential race here with putchar() but since putchar should be
121 * called by autoconf, msg_magic should be initialized by the time
122 * we get here.
123 */
124 if (msgbufp->msg_magic != MSG_MAGIC) {
125 register int i;
126
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;
131 }
132 LOG_UNLOCK();
133 unix_release();
134 return (0);
135}
136
137/*ARGSUSED*/
138int
139logclose(dev, flag)
140 dev_t dev;
141{
142 int oldpri;
143 LOG_LOCK();
144 log_open = 0;
145 selwakeup(&logsoftc.sc_selp);
146 oldpri = splhigh();
147 selthreadclear(&logsoftc.sc_selp);
148 splx(oldpri);
149 LOG_UNLOCK();
150 return (0);
151}
152
153/*ARGSUSED*/
154int
155logread(dev, uio, flag)
156 dev_t dev;
157 struct uio *uio;
158 int flag;
159{
160 register long l;
161 register int s;
162 int error = 0;
163
164 s = splhigh();
165 while (msgbufp->msg_bufr == msgbufp->msg_bufx) {
166 if (flag & IO_NDELAY) {
167 splx(s);
168 return (EWOULDBLOCK);
169 }
170 if (logsoftc.sc_state & LOG_NBIO) {
171 splx(s);
172 return (EWOULDBLOCK);
173 }
174 logsoftc.sc_state |= LOG_RDWAIT;
175 if (error = tsleep((caddr_t)msgbufp, LOG_RDPRI | PCATCH,
176 "klog", 0)) {
177 splx(s);
178 return (error);
179 }
180 }
181 splx(s);
182 logsoftc.sc_state &= ~LOG_RDWAIT;
183
184 while (uio->uio_resid > 0) {
185 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
186 if (l < 0)
187 l = MSG_BSIZE - msgbufp->msg_bufr;
188 l = min(l, uio->uio_resid);
189 if (l == 0)
190 break;
191 error = uiomove((caddr_t)&msgbufp->msg_bufc[msgbufp->msg_bufr],
192 (int)l, uio);
193 if (error)
194 break;
195 msgbufp->msg_bufr += l;
196 if (msgbufp->msg_bufr < 0 || msgbufp->msg_bufr >= MSG_BSIZE)
197 msgbufp->msg_bufr = 0;
198 }
199 return (error);
200}
201
202/*ARGSUSED*/
203int
204logselect(dev, rw, p)
205 dev_t dev;
206 int rw;
207 struct proc *p;
208{
209 int s = splhigh();
210
211 switch (rw) {
212
213 case FREAD:
214 if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
215 splx(s);
216 return (1);
217 }
218 selrecord(p, &logsoftc.sc_selp);
219 break;
220 }
221 splx(s);
222 return (0);
223}
224
225void
226logwakeup()
227{
228 struct proc *p;
229 int pgid;
230 boolean_t funnel_state;
231
232 if (!log_open)
233 return;
234 funnel_state = thread_funnel_set(kernel_flock, TRUE);
235 selwakeup(&logsoftc.sc_selp);
236 if (logsoftc.sc_state & LOG_ASYNC) {
237 unix_master();
238 LOG_LOCK();
239 pgid = logsoftc.sc_pgid;
240 LOG_UNLOCK();
241 if (pgid < 0)
242 gsignal(-pgid, SIGIO);
243 else if (p = pfind(pgid))
244 psignal(p, SIGIO);
245 unix_release();
246 }
247 if (logsoftc.sc_state & LOG_RDWAIT) {
248 wakeup((caddr_t)msgbufp);
249 logsoftc.sc_state &= ~LOG_RDWAIT;
250 }
251 (void) thread_funnel_set(kernel_flock, funnel_state);
252}
253
254void
255klogwakeup()
256{
257
258 if (_logentrypend) {
259 _logentrypend = 0;
260 logwakeup();
261 }
262}
263
264/*ARGSUSED*/
265int
266logioctl(com, data, flag)
267 caddr_t data;
268{
269 long l;
270 int s;
271
272 switch (com) {
273
274 /* return number of characters immediately available */
275 case FIONREAD:
276 s = splhigh();
277 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
278 splx(s);
279 if (l < 0)
280 l += MSG_BSIZE;
281 *(off_t *)data = l;
282 break;
283
284 case FIONBIO:
285 if (*(int *)data)
286 logsoftc.sc_state |= LOG_NBIO;
287 else
288 logsoftc.sc_state &= ~LOG_NBIO;
289 break;
290
291 case FIOASYNC:
292 if (*(int *)data)
293 logsoftc.sc_state |= LOG_ASYNC;
294 else
295 logsoftc.sc_state &= ~LOG_ASYNC;
296 break;
297
298 case TIOCSPGRP:
299 LOG_LOCK();
300 logsoftc.sc_pgid = *(int *)data;
301 LOG_UNLOCK();
302 break;
303
304 case TIOCGPGRP:
305 LOG_LOCK();
306 *(int *)data = logsoftc.sc_pgid;
307 LOG_UNLOCK();
308 break;
309
310 default:
311 return (-1);
312 }
313 return (0);
314}
315
316void
317log_init()
318{
319 msgbufp = &temp_msgbuf;
320 LOG_LOCK_INIT();
321}
322
323void
324log_putc(char c)
325{
326 register struct msgbuf *mbp;
327
328 if (msgbufp == NULL)
329 msgbufp =&temp_msgbuf;
330
331 mbp = msgbufp;
332 if (mbp-> msg_magic != MSG_MAGIC) {
333 register int i;
334
335 mbp->msg_magic = MSG_MAGIC;
336 mbp->msg_bufx = mbp->msg_bufr = 0;
337 for (i=0; i < MSG_BSIZE; i++)
338 mbp->msg_bufc[i] = 0;
339 }
340 mbp->msg_bufc[mbp->msg_bufx++] = c;
341 _logentrypend = 1;
342 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= MSG_BSIZE)
343 mbp->msg_bufx = 0;
344}