]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
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. | |
1c79356b | 11 | * |
e5568f75 A |
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 | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
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. | |
1c79356b A |
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> | |
1c79356b A |
72 | |
73 | #define LOG_RDPRI (PZERO + 1) | |
74 | ||
75 | #define LOG_NBIO 0x02 | |
76 | #define LOG_ASYNC 0x04 | |
77 | #define LOG_RDWAIT 0x08 | |
78 | ||
79 | struct logsoftc { | |
80 | int sc_state; /* see above for possibilities */ | |
81 | struct selinfo sc_selp; /* thread waiting for select */ | |
82 | int sc_pgid; /* process/group for async I/O */ | |
83 | } logsoftc; | |
84 | ||
85 | int log_open; /* also used in log() */ | |
86 | struct msgbuf temp_msgbuf; | |
87 | struct msgbuf *msgbufp; | |
88 | static int _logentrypend = 0; | |
89 | ||
90 | /* | |
91 | * Serialize log access. Note that the log can be written at interrupt level, | |
92 | * so any log manipulations that can be done from, or affect, another processor | |
93 | * at interrupt level must be guarded with a spin lock. | |
94 | */ | |
95 | decl_simple_lock_data(,log_lock); /* stop races dead in their tracks */ | |
96 | #define LOG_LOCK() simple_lock(&log_lock) | |
97 | #define LOG_UNLOCK() simple_unlock(&log_lock) | |
98 | #define LOG_LOCK_INIT() simple_lock_init(&log_lock) | |
99 | ||
100 | /*ARGSUSED*/ | |
101 | logopen(dev, flags, mode, p) | |
102 | dev_t dev; | |
103 | int flags, mode; | |
104 | struct proc *p; | |
105 | { | |
1c79356b A |
106 | LOG_LOCK(); |
107 | if (log_open) { | |
108 | LOG_UNLOCK(); | |
1c79356b A |
109 | return (EBUSY); |
110 | } | |
111 | log_open = 1; | |
112 | logsoftc.sc_pgid = p->p_pid; /* signal process only */ | |
113 | /* | |
114 | * Potential race here with putchar() but since putchar should be | |
115 | * called by autoconf, msg_magic should be initialized by the time | |
116 | * we get here. | |
117 | */ | |
118 | if (msgbufp->msg_magic != MSG_MAGIC) { | |
119 | register int i; | |
120 | ||
121 | msgbufp->msg_magic = MSG_MAGIC; | |
122 | msgbufp->msg_bufx = msgbufp->msg_bufr = 0; | |
123 | for (i=0; i < MSG_BSIZE; i++) | |
124 | msgbufp->msg_bufc[i] = 0; | |
125 | } | |
126 | LOG_UNLOCK(); | |
9bccf70c | 127 | |
1c79356b A |
128 | return (0); |
129 | } | |
130 | ||
131 | /*ARGSUSED*/ | |
132 | int | |
133 | logclose(dev, flag) | |
134 | dev_t dev; | |
135 | { | |
136 | int oldpri; | |
137 | LOG_LOCK(); | |
138 | log_open = 0; | |
139 | selwakeup(&logsoftc.sc_selp); | |
140 | oldpri = splhigh(); | |
141 | selthreadclear(&logsoftc.sc_selp); | |
142 | splx(oldpri); | |
143 | LOG_UNLOCK(); | |
144 | return (0); | |
145 | } | |
146 | ||
147 | /*ARGSUSED*/ | |
148 | int | |
149 | logread(dev, uio, flag) | |
150 | dev_t dev; | |
151 | struct uio *uio; | |
152 | int flag; | |
153 | { | |
154 | register long l; | |
155 | register int s; | |
156 | int error = 0; | |
157 | ||
158 | s = splhigh(); | |
159 | while (msgbufp->msg_bufr == msgbufp->msg_bufx) { | |
160 | if (flag & IO_NDELAY) { | |
161 | splx(s); | |
162 | return (EWOULDBLOCK); | |
163 | } | |
164 | if (logsoftc.sc_state & LOG_NBIO) { | |
165 | splx(s); | |
166 | return (EWOULDBLOCK); | |
167 | } | |
168 | logsoftc.sc_state |= LOG_RDWAIT; | |
169 | if (error = tsleep((caddr_t)msgbufp, LOG_RDPRI | PCATCH, | |
170 | "klog", 0)) { | |
171 | splx(s); | |
172 | return (error); | |
173 | } | |
174 | } | |
175 | splx(s); | |
176 | logsoftc.sc_state &= ~LOG_RDWAIT; | |
177 | ||
178 | while (uio->uio_resid > 0) { | |
179 | l = msgbufp->msg_bufx - msgbufp->msg_bufr; | |
180 | if (l < 0) | |
181 | l = MSG_BSIZE - msgbufp->msg_bufr; | |
182 | l = min(l, uio->uio_resid); | |
183 | if (l == 0) | |
184 | break; | |
185 | error = uiomove((caddr_t)&msgbufp->msg_bufc[msgbufp->msg_bufr], | |
186 | (int)l, uio); | |
187 | if (error) | |
188 | break; | |
189 | msgbufp->msg_bufr += l; | |
190 | if (msgbufp->msg_bufr < 0 || msgbufp->msg_bufr >= MSG_BSIZE) | |
191 | msgbufp->msg_bufr = 0; | |
192 | } | |
193 | return (error); | |
194 | } | |
195 | ||
196 | /*ARGSUSED*/ | |
197 | int | |
0b4e3aa0 | 198 | logselect(dev, rw, wql, p) |
1c79356b A |
199 | dev_t dev; |
200 | int rw; | |
0b4e3aa0 | 201 | void * wql; |
1c79356b A |
202 | struct proc *p; |
203 | { | |
204 | int s = splhigh(); | |
205 | ||
206 | switch (rw) { | |
207 | ||
208 | case FREAD: | |
209 | if (msgbufp->msg_bufr != msgbufp->msg_bufx) { | |
210 | splx(s); | |
211 | return (1); | |
212 | } | |
0b4e3aa0 | 213 | selrecord(p, &logsoftc.sc_selp, wql); |
1c79356b A |
214 | break; |
215 | } | |
216 | splx(s); | |
217 | return (0); | |
218 | } | |
219 | ||
220 | void | |
221 | logwakeup() | |
222 | { | |
223 | struct proc *p; | |
224 | int pgid; | |
225 | boolean_t funnel_state; | |
226 | ||
227 | if (!log_open) | |
228 | return; | |
229 | funnel_state = thread_funnel_set(kernel_flock, TRUE); | |
230 | selwakeup(&logsoftc.sc_selp); | |
231 | if (logsoftc.sc_state & LOG_ASYNC) { | |
1c79356b A |
232 | LOG_LOCK(); |
233 | pgid = logsoftc.sc_pgid; | |
234 | LOG_UNLOCK(); | |
235 | if (pgid < 0) | |
236 | gsignal(-pgid, SIGIO); | |
237 | else if (p = pfind(pgid)) | |
238 | psignal(p, SIGIO); | |
1c79356b A |
239 | } |
240 | if (logsoftc.sc_state & LOG_RDWAIT) { | |
241 | wakeup((caddr_t)msgbufp); | |
242 | logsoftc.sc_state &= ~LOG_RDWAIT; | |
243 | } | |
244 | (void) thread_funnel_set(kernel_flock, funnel_state); | |
245 | } | |
246 | ||
247 | void | |
248 | klogwakeup() | |
249 | { | |
250 | ||
251 | if (_logentrypend) { | |
252 | _logentrypend = 0; | |
253 | logwakeup(); | |
254 | } | |
255 | } | |
256 | ||
257 | /*ARGSUSED*/ | |
258 | int | |
55e303ae | 259 | logioctl(dev, com, data, flag) |
1c79356b A |
260 | caddr_t data; |
261 | { | |
262 | long l; | |
263 | int s; | |
264 | ||
265 | switch (com) { | |
266 | ||
267 | /* return number of characters immediately available */ | |
268 | case FIONREAD: | |
269 | s = splhigh(); | |
270 | l = msgbufp->msg_bufx - msgbufp->msg_bufr; | |
271 | splx(s); | |
272 | if (l < 0) | |
273 | l += MSG_BSIZE; | |
274 | *(off_t *)data = l; | |
275 | break; | |
276 | ||
277 | case FIONBIO: | |
278 | if (*(int *)data) | |
279 | logsoftc.sc_state |= LOG_NBIO; | |
280 | else | |
281 | logsoftc.sc_state &= ~LOG_NBIO; | |
282 | break; | |
283 | ||
284 | case FIOASYNC: | |
285 | if (*(int *)data) | |
286 | logsoftc.sc_state |= LOG_ASYNC; | |
287 | else | |
288 | logsoftc.sc_state &= ~LOG_ASYNC; | |
289 | break; | |
290 | ||
291 | case TIOCSPGRP: | |
292 | LOG_LOCK(); | |
293 | logsoftc.sc_pgid = *(int *)data; | |
294 | LOG_UNLOCK(); | |
295 | break; | |
296 | ||
297 | case TIOCGPGRP: | |
298 | LOG_LOCK(); | |
299 | *(int *)data = logsoftc.sc_pgid; | |
300 | LOG_UNLOCK(); | |
301 | break; | |
302 | ||
303 | default: | |
304 | return (-1); | |
305 | } | |
306 | return (0); | |
307 | } | |
308 | ||
309 | void | |
310 | log_init() | |
311 | { | |
312 | msgbufp = &temp_msgbuf; | |
313 | LOG_LOCK_INIT(); | |
314 | } | |
315 | ||
316 | void | |
317 | log_putc(char c) | |
318 | { | |
319 | register struct msgbuf *mbp; | |
320 | ||
321 | if (msgbufp == NULL) | |
322 | msgbufp =&temp_msgbuf; | |
323 | ||
324 | mbp = msgbufp; | |
325 | if (mbp-> msg_magic != MSG_MAGIC) { | |
326 | register int i; | |
327 | ||
328 | mbp->msg_magic = MSG_MAGIC; | |
329 | mbp->msg_bufx = mbp->msg_bufr = 0; | |
330 | for (i=0; i < MSG_BSIZE; i++) | |
331 | mbp->msg_bufc[i] = 0; | |
332 | } | |
333 | mbp->msg_bufc[mbp->msg_bufx++] = c; | |
334 | _logentrypend = 1; | |
335 | if (mbp->msg_bufx < 0 || mbp->msg_bufx >= MSG_BSIZE) | |
336 | mbp->msg_bufx = 0; | |
337 | } |