]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_log.c
xnu-344.49.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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
26 /*
27 * Copyright (c) 1982, 1986, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)subr_log.c 8.3 (Berkeley) 2/14/95
59 */
60
61 /*
62 * Error log buffer for kernel printf's.
63 */
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/proc.h>
68 #include <sys/vnode.h>
69 #include <sys/ioctl.h>
70 #include <sys/msgbuf.h>
71 #include <sys/file.h>
72 #include <sys/errno.h>
73 #include <sys/select.h>
74 #include <kern/thread.h>
75
76 #define LOG_RDPRI (PZERO + 1)
77
78 #define LOG_NBIO 0x02
79 #define LOG_ASYNC 0x04
80 #define LOG_RDWAIT 0x08
81
82 struct logsoftc {
83 int sc_state; /* see above for possibilities */
84 struct selinfo sc_selp; /* thread waiting for select */
85 int sc_pgid; /* process/group for async I/O */
86 } logsoftc;
87
88 int log_open; /* also used in log() */
89 struct msgbuf temp_msgbuf;
90 struct msgbuf *msgbufp;
91 static int _logentrypend = 0;
92
93 /*
94 * Serialize log access. Note that the log can be written at interrupt level,
95 * so any log manipulations that can be done from, or affect, another processor
96 * at interrupt level must be guarded with a spin lock.
97 */
98 decl_simple_lock_data(,log_lock); /* stop races dead in their tracks */
99 #define LOG_LOCK() simple_lock(&log_lock)
100 #define LOG_UNLOCK() simple_unlock(&log_lock)
101 #define LOG_LOCK_INIT() simple_lock_init(&log_lock)
102
103 /*ARGSUSED*/
104 logopen(dev, flags, mode, p)
105 dev_t dev;
106 int flags, mode;
107 struct proc *p;
108 {
109 LOG_LOCK();
110 if (log_open) {
111 LOG_UNLOCK();
112 return (EBUSY);
113 }
114 log_open = 1;
115 logsoftc.sc_pgid = p->p_pid; /* signal process only */
116 /*
117 * Potential race here with putchar() but since putchar should be
118 * called by autoconf, msg_magic should be initialized by the time
119 * we get here.
120 */
121 if (msgbufp->msg_magic != MSG_MAGIC) {
122 register int i;
123
124 msgbufp->msg_magic = MSG_MAGIC;
125 msgbufp->msg_bufx = msgbufp->msg_bufr = 0;
126 for (i=0; i < MSG_BSIZE; i++)
127 msgbufp->msg_bufc[i] = 0;
128 }
129 LOG_UNLOCK();
130
131 return (0);
132 }
133
134 /*ARGSUSED*/
135 int
136 logclose(dev, flag)
137 dev_t dev;
138 {
139 int oldpri;
140 LOG_LOCK();
141 log_open = 0;
142 selwakeup(&logsoftc.sc_selp);
143 oldpri = splhigh();
144 selthreadclear(&logsoftc.sc_selp);
145 splx(oldpri);
146 LOG_UNLOCK();
147 return (0);
148 }
149
150 /*ARGSUSED*/
151 int
152 logread(dev, uio, flag)
153 dev_t dev;
154 struct uio *uio;
155 int flag;
156 {
157 register long l;
158 register int s;
159 int error = 0;
160
161 s = splhigh();
162 while (msgbufp->msg_bufr == msgbufp->msg_bufx) {
163 if (flag & IO_NDELAY) {
164 splx(s);
165 return (EWOULDBLOCK);
166 }
167 if (logsoftc.sc_state & LOG_NBIO) {
168 splx(s);
169 return (EWOULDBLOCK);
170 }
171 logsoftc.sc_state |= LOG_RDWAIT;
172 if (error = tsleep((caddr_t)msgbufp, LOG_RDPRI | PCATCH,
173 "klog", 0)) {
174 splx(s);
175 return (error);
176 }
177 }
178 splx(s);
179 logsoftc.sc_state &= ~LOG_RDWAIT;
180
181 while (uio->uio_resid > 0) {
182 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
183 if (l < 0)
184 l = MSG_BSIZE - msgbufp->msg_bufr;
185 l = min(l, uio->uio_resid);
186 if (l == 0)
187 break;
188 error = uiomove((caddr_t)&msgbufp->msg_bufc[msgbufp->msg_bufr],
189 (int)l, uio);
190 if (error)
191 break;
192 msgbufp->msg_bufr += l;
193 if (msgbufp->msg_bufr < 0 || msgbufp->msg_bufr >= MSG_BSIZE)
194 msgbufp->msg_bufr = 0;
195 }
196 return (error);
197 }
198
199 /*ARGSUSED*/
200 int
201 logselect(dev, rw, wql, p)
202 dev_t dev;
203 int rw;
204 void * wql;
205 struct proc *p;
206 {
207 int s = splhigh();
208
209 switch (rw) {
210
211 case FREAD:
212 if (msgbufp->msg_bufr != msgbufp->msg_bufx) {
213 splx(s);
214 return (1);
215 }
216 selrecord(p, &logsoftc.sc_selp, wql);
217 break;
218 }
219 splx(s);
220 return (0);
221 }
222
223 void
224 logwakeup()
225 {
226 struct proc *p;
227 int pgid;
228 boolean_t funnel_state;
229
230 if (!log_open)
231 return;
232 funnel_state = thread_funnel_set(kernel_flock, TRUE);
233 selwakeup(&logsoftc.sc_selp);
234 if (logsoftc.sc_state & LOG_ASYNC) {
235 LOG_LOCK();
236 pgid = logsoftc.sc_pgid;
237 LOG_UNLOCK();
238 if (pgid < 0)
239 gsignal(-pgid, SIGIO);
240 else if (p = pfind(pgid))
241 psignal(p, SIGIO);
242 }
243 if (logsoftc.sc_state & LOG_RDWAIT) {
244 wakeup((caddr_t)msgbufp);
245 logsoftc.sc_state &= ~LOG_RDWAIT;
246 }
247 (void) thread_funnel_set(kernel_flock, funnel_state);
248 }
249
250 void
251 klogwakeup()
252 {
253
254 if (_logentrypend) {
255 _logentrypend = 0;
256 logwakeup();
257 }
258 }
259
260 /*ARGSUSED*/
261 int
262 logioctl(com, data, flag)
263 caddr_t data;
264 {
265 long l;
266 int s;
267
268 switch (com) {
269
270 /* return number of characters immediately available */
271 case FIONREAD:
272 s = splhigh();
273 l = msgbufp->msg_bufx - msgbufp->msg_bufr;
274 splx(s);
275 if (l < 0)
276 l += MSG_BSIZE;
277 *(off_t *)data = l;
278 break;
279
280 case FIONBIO:
281 if (*(int *)data)
282 logsoftc.sc_state |= LOG_NBIO;
283 else
284 logsoftc.sc_state &= ~LOG_NBIO;
285 break;
286
287 case FIOASYNC:
288 if (*(int *)data)
289 logsoftc.sc_state |= LOG_ASYNC;
290 else
291 logsoftc.sc_state &= ~LOG_ASYNC;
292 break;
293
294 case TIOCSPGRP:
295 LOG_LOCK();
296 logsoftc.sc_pgid = *(int *)data;
297 LOG_UNLOCK();
298 break;
299
300 case TIOCGPGRP:
301 LOG_LOCK();
302 *(int *)data = logsoftc.sc_pgid;
303 LOG_UNLOCK();
304 break;
305
306 default:
307 return (-1);
308 }
309 return (0);
310 }
311
312 void
313 log_init()
314 {
315 msgbufp = &temp_msgbuf;
316 LOG_LOCK_INIT();
317 }
318
319 void
320 log_putc(char c)
321 {
322 register struct msgbuf *mbp;
323
324 if (msgbufp == NULL)
325 msgbufp =&temp_msgbuf;
326
327 mbp = msgbufp;
328 if (mbp-> msg_magic != MSG_MAGIC) {
329 register int i;
330
331 mbp->msg_magic = MSG_MAGIC;
332 mbp->msg_bufx = mbp->msg_bufr = 0;
333 for (i=0; i < MSG_BSIZE; i++)
334 mbp->msg_bufc[i] = 0;
335 }
336 mbp->msg_bufc[mbp->msg_bufx++] = c;
337 _logentrypend = 1;
338 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= MSG_BSIZE)
339 mbp->msg_bufx = 0;
340 }