]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_log.c
xnu-201.14.tar.gz
[apple/xnu.git] / bsd / kern / subr_log.c
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
83 struct 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
89 int log_open; /* also used in log() */
90 struct msgbuf temp_msgbuf;
91 struct msgbuf *msgbufp;
92 static 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 */
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)
103
104 /*ARGSUSED*/
105 logopen(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*/
138 int
139 logclose(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*/
154 int
155 logread(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*/
203 int
204 logselect(dev, rw, wql, p)
205 dev_t dev;
206 int rw;
207 void * wql;
208 struct proc *p;
209 {
210 int s = splhigh();
211
212 switch (rw) {
213
214 case FREAD:
215 if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
216 splx(s);
217 return (1);
218 }
219 selrecord(p, &logsoftc.sc_selp, wql);
220 break;
221 }
222 splx(s);
223 return (0);
224 }
225
226 void
227 logwakeup()
228 {
229 struct proc *p;
230 int pgid;
231 boolean_t funnel_state;
232
233 if (!log_open)
234 return;
235 funnel_state = thread_funnel_set(kernel_flock, TRUE);
236 selwakeup(&logsoftc.sc_selp);
237 if (logsoftc.sc_state & LOG_ASYNC) {
238 unix_master();
239 LOG_LOCK();
240 pgid = logsoftc.sc_pgid;
241 LOG_UNLOCK();
242 if (pgid < 0)
243 gsignal(-pgid, SIGIO);
244 else if (p = pfind(pgid))
245 psignal(p, SIGIO);
246 unix_release();
247 }
248 if (logsoftc.sc_state & LOG_RDWAIT) {
249 wakeup((caddr_t)msgbufp);
250 logsoftc.sc_state &= ~LOG_RDWAIT;
251 }
252 (void) thread_funnel_set(kernel_flock, funnel_state);
253 }
254
255 void
256 klogwakeup()
257 {
258
259 if (_logentrypend) {
260 _logentrypend = 0;
261 logwakeup();
262 }
263 }
264
265 /*ARGSUSED*/
266 int
267 logioctl(com, data, flag)
268 caddr_t data;
269 {
270 long l;
271 int s;
272
273 switch (com) {
274
275 /* return number of characters immediately available */
276 case FIONREAD:
277 s = splhigh();
278 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
279 splx(s);
280 if (l < 0)
281 l += MSG_BSIZE;
282 *(off_t *)data = l;
283 break;
284
285 case FIONBIO:
286 if (*(int *)data)
287 logsoftc.sc_state |= LOG_NBIO;
288 else
289 logsoftc.sc_state &= ~LOG_NBIO;
290 break;
291
292 case FIOASYNC:
293 if (*(int *)data)
294 logsoftc.sc_state |= LOG_ASYNC;
295 else
296 logsoftc.sc_state &= ~LOG_ASYNC;
297 break;
298
299 case TIOCSPGRP:
300 LOG_LOCK();
301 logsoftc.sc_pgid = *(int *)data;
302 LOG_UNLOCK();
303 break;
304
305 case TIOCGPGRP:
306 LOG_LOCK();
307 *(int *)data = logsoftc.sc_pgid;
308 LOG_UNLOCK();
309 break;
310
311 default:
312 return (-1);
313 }
314 return (0);
315 }
316
317 void
318 log_init()
319 {
320 msgbufp = &temp_msgbuf;
321 LOG_LOCK_INIT();
322 }
323
324 void
325 log_putc(char c)
326 {
327 register struct msgbuf *mbp;
328
329 if (msgbufp == NULL)
330 msgbufp =&temp_msgbuf;
331
332 mbp = msgbufp;
333 if (mbp-> msg_magic != MSG_MAGIC) {
334 register int i;
335
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;
340 }
341 mbp->msg_bufc[mbp->msg_bufx++] = c;
342 _logentrypend = 1;
343 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= MSG_BSIZE)
344 mbp->msg_bufx = 0;
345 }