]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2000-2010 Apple, Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1982, 1986, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * Redistribution and use in source and binary forms, with or without | |
34 | * modification, are permitted provided that the following conditions | |
35 | * are met: | |
36 | * 1. Redistributions of source code must retain the above copyright | |
37 | * notice, this list of conditions and the following disclaimer. | |
38 | * 2. Redistributions in binary form must reproduce the above copyright | |
39 | * notice, this list of conditions and the following disclaimer in the | |
40 | * documentation and/or other materials provided with the distribution. | |
41 | * 3. All advertising materials mentioning features or use of this software | |
42 | * must display the following acknowledgement: | |
43 | * This product includes software developed by the University of | |
44 | * California, Berkeley and its contributors. | |
45 | * 4. Neither the name of the University nor the names of its contributors | |
46 | * may be used to endorse or promote products derived from this software | |
47 | * without specific prior written permission. | |
48 | * | |
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
59 | * SUCH DAMAGE. | |
60 | * | |
61 | * @(#)subr_log.c 8.3 (Berkeley) 2/14/95 | |
62 | */ | |
63 | ||
64 | /* | |
65 | * Error log buffer for kernel printf's. | |
66 | */ | |
67 | ||
68 | #include <sys/param.h> | |
69 | #include <sys/systm.h> | |
91447636 | 70 | #include <sys/proc_internal.h> |
1c79356b A |
71 | #include <sys/vnode.h> |
72 | #include <sys/ioctl.h> | |
73 | #include <sys/msgbuf.h> | |
91447636 | 74 | #include <sys/file_internal.h> |
1c79356b A |
75 | #include <sys/errno.h> |
76 | #include <sys/select.h> | |
91447636 | 77 | #include <sys/kernel.h> |
1c79356b | 78 | #include <kern/thread.h> |
91447636 | 79 | #include <sys/lock.h> |
2d21ac55 A |
80 | #include <sys/signalvar.h> |
81 | #include <sys/conf.h> | |
82 | #include <sys/sysctl.h> | |
83 | #include <kern/kalloc.h> | |
6d2010ae | 84 | #include <pexpert/pexpert.h> |
2d21ac55 A |
85 | |
86 | /* XXX should be in a common header somewhere */ | |
87 | extern void klogwakeup(void); | |
88 | extern void logwakeup(void); | |
1c79356b A |
89 | |
90 | #define LOG_RDPRI (PZERO + 1) | |
91 | ||
92 | #define LOG_NBIO 0x02 | |
93 | #define LOG_ASYNC 0x04 | |
94 | #define LOG_RDWAIT 0x08 | |
95 | ||
6d2010ae A |
96 | /* All globals should be accessed under LOG_LOCK() */ |
97 | ||
98 | /* logsoftc only valid while log_open=1 */ | |
1c79356b A |
99 | struct logsoftc { |
100 | int sc_state; /* see above for possibilities */ | |
101 | struct selinfo sc_selp; /* thread waiting for select */ | |
102 | int sc_pgid; /* process/group for async I/O */ | |
103 | } logsoftc; | |
104 | ||
105 | int log_open; /* also used in log() */ | |
6d2010ae A |
106 | char smsg_bufc[CONFIG_MSG_BSIZE]; /* static buffer */ |
107 | struct msgbuf msgbuf = {MSG_MAGIC,sizeof(smsg_bufc),0,0,smsg_bufc}; | |
108 | struct msgbuf *msgbufp = &msgbuf; | |
109 | static int logentrypend = 0; | |
110 | ||
2d21ac55 A |
111 | /* the following are implemented in osfmk/kern/printf.c */ |
112 | extern void bsd_log_lock(void); | |
91447636 A |
113 | extern void bsd_log_unlock(void); |
114 | extern void bsd_log_init(void); | |
1c79356b | 115 | |
2d21ac55 A |
116 | /* XXX wants a linker set so these can be static */ |
117 | extern d_open_t logopen; | |
118 | extern d_close_t logclose; | |
119 | extern d_read_t logread; | |
120 | extern d_ioctl_t logioctl; | |
121 | extern d_select_t logselect; | |
122 | ||
1c79356b A |
123 | /* |
124 | * Serialize log access. Note that the log can be written at interrupt level, | |
125 | * so any log manipulations that can be done from, or affect, another processor | |
126 | * at interrupt level must be guarded with a spin lock. | |
127 | */ | |
91447636 A |
128 | |
129 | #define LOG_LOCK() bsd_log_lock() | |
130 | #define LOG_UNLOCK() bsd_log_unlock() | |
131 | ||
6d2010ae A |
132 | #if DEBUG |
133 | #define LOG_SETSIZE_DEBUG(x...) kprintf(x) | |
134 | #else | |
135 | #define LOG_SETSIZE_DEBUG(x...) do { } while(0) | |
136 | #endif | |
137 | ||
138 | static int sysctl_kern_msgbuf(struct sysctl_oid *oidp, | |
139 | void *arg1, | |
140 | int arg2, | |
141 | struct sysctl_req *req); | |
1c79356b A |
142 | |
143 | /*ARGSUSED*/ | |
2d21ac55 A |
144 | int |
145 | logopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p) | |
1c79356b | 146 | { |
1c79356b A |
147 | LOG_LOCK(); |
148 | if (log_open) { | |
149 | LOG_UNLOCK(); | |
1c79356b A |
150 | return (EBUSY); |
151 | } | |
1c79356b | 152 | logsoftc.sc_pgid = p->p_pid; /* signal process only */ |
6d2010ae | 153 | log_open = 1; |
1c79356b | 154 | |
1c79356b | 155 | LOG_UNLOCK(); |
9bccf70c | 156 | |
1c79356b A |
157 | return (0); |
158 | } | |
159 | ||
160 | /*ARGSUSED*/ | |
161 | int | |
2d21ac55 | 162 | logclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p) |
1c79356b | 163 | { |
1c79356b | 164 | LOG_LOCK(); |
1c79356b | 165 | selwakeup(&logsoftc.sc_selp); |
1c79356b | 166 | selthreadclear(&logsoftc.sc_selp); |
6d2010ae | 167 | log_open = 0; |
1c79356b A |
168 | LOG_UNLOCK(); |
169 | return (0); | |
170 | } | |
171 | ||
172 | /*ARGSUSED*/ | |
173 | int | |
2d21ac55 | 174 | logread(__unused dev_t dev, struct uio *uio, int flag) |
1c79356b | 175 | { |
6d2010ae | 176 | int l; |
1c79356b A |
177 | int error = 0; |
178 | ||
91447636 | 179 | LOG_LOCK(); |
1c79356b A |
180 | while (msgbufp->msg_bufr == msgbufp->msg_bufx) { |
181 | if (flag & IO_NDELAY) { | |
91447636 A |
182 | error = EWOULDBLOCK; |
183 | goto out; | |
1c79356b A |
184 | } |
185 | if (logsoftc.sc_state & LOG_NBIO) { | |
91447636 A |
186 | error = EWOULDBLOCK; |
187 | goto out; | |
1c79356b A |
188 | } |
189 | logsoftc.sc_state |= LOG_RDWAIT; | |
91447636 A |
190 | LOG_UNLOCK(); |
191 | /* | |
192 | * If the wakeup is missed the ligtening bolt will wake this up | |
193 | * if there are any new characters. If that doesn't do it | |
194 | * then wait for 5 sec and reevaluate | |
195 | */ | |
2d21ac55 A |
196 | if ((error = tsleep((caddr_t)msgbufp, LOG_RDPRI | PCATCH, |
197 | "klog", 5 * hz)) != 0) { | |
91447636 A |
198 | /* if it times out; ignore */ |
199 | if (error != EWOULDBLOCK) | |
200 | return (error); | |
1c79356b | 201 | } |
91447636 | 202 | LOG_LOCK(); |
1c79356b | 203 | } |
1c79356b A |
204 | logsoftc.sc_state &= ~LOG_RDWAIT; |
205 | ||
91447636 | 206 | while (uio_resid(uio) > 0) { |
6d2010ae A |
207 | int readpos; |
208 | ||
1c79356b A |
209 | l = msgbufp->msg_bufx - msgbufp->msg_bufr; |
210 | if (l < 0) | |
2d21ac55 | 211 | l = msgbufp->msg_size - msgbufp->msg_bufr; |
91447636 | 212 | l = min(l, uio_resid(uio)); |
1c79356b A |
213 | if (l == 0) |
214 | break; | |
6d2010ae A |
215 | |
216 | readpos = msgbufp->msg_bufr; | |
91447636 | 217 | LOG_UNLOCK(); |
6d2010ae A |
218 | error = uiomove((caddr_t)&msgbufp->msg_bufc[readpos], |
219 | l, uio); | |
91447636 | 220 | LOG_LOCK(); |
1c79356b A |
221 | if (error) |
222 | break; | |
6d2010ae A |
223 | msgbufp->msg_bufr = readpos + l; |
224 | if (msgbufp->msg_bufr >= msgbufp->msg_size) | |
1c79356b A |
225 | msgbufp->msg_bufr = 0; |
226 | } | |
91447636 A |
227 | out: |
228 | LOG_UNLOCK(); | |
1c79356b A |
229 | return (error); |
230 | } | |
231 | ||
232 | /*ARGSUSED*/ | |
233 | int | |
2d21ac55 | 234 | logselect(__unused dev_t dev, int rw, void * wql, struct proc *p) |
1c79356b | 235 | { |
1c79356b A |
236 | switch (rw) { |
237 | ||
238 | case FREAD: | |
91447636 | 239 | LOG_LOCK(); |
1c79356b | 240 | if (msgbufp->msg_bufr != msgbufp->msg_bufx) { |
91447636 | 241 | LOG_UNLOCK(); |
1c79356b A |
242 | return (1); |
243 | } | |
0b4e3aa0 | 244 | selrecord(p, &logsoftc.sc_selp, wql); |
91447636 | 245 | LOG_UNLOCK(); |
1c79356b A |
246 | break; |
247 | } | |
1c79356b A |
248 | return (0); |
249 | } | |
250 | ||
251 | void | |
2d21ac55 | 252 | logwakeup(void) |
1c79356b | 253 | { |
1c79356b | 254 | int pgid; |
1c79356b | 255 | |
91447636 A |
256 | LOG_LOCK(); |
257 | if (!log_open) { | |
258 | LOG_UNLOCK(); | |
1c79356b | 259 | return; |
91447636 | 260 | } |
1c79356b A |
261 | selwakeup(&logsoftc.sc_selp); |
262 | if (logsoftc.sc_state & LOG_ASYNC) { | |
1c79356b A |
263 | pgid = logsoftc.sc_pgid; |
264 | LOG_UNLOCK(); | |
265 | if (pgid < 0) | |
266 | gsignal(-pgid, SIGIO); | |
2d21ac55 A |
267 | else |
268 | proc_signal(pgid, SIGIO); | |
91447636 | 269 | LOG_LOCK(); |
1c79356b A |
270 | } |
271 | if (logsoftc.sc_state & LOG_RDWAIT) { | |
272 | wakeup((caddr_t)msgbufp); | |
273 | logsoftc.sc_state &= ~LOG_RDWAIT; | |
274 | } | |
91447636 | 275 | LOG_UNLOCK(); |
1c79356b A |
276 | } |
277 | ||
278 | void | |
2d21ac55 | 279 | klogwakeup(void) |
1c79356b | 280 | { |
6d2010ae A |
281 | LOG_LOCK(); |
282 | if (logentrypend && log_open) { | |
283 | logentrypend = 0; /* only reset if someone will be reading */ | |
284 | LOG_UNLOCK(); | |
1c79356b | 285 | logwakeup(); |
6d2010ae A |
286 | } else { |
287 | LOG_UNLOCK(); | |
1c79356b A |
288 | } |
289 | } | |
290 | ||
291 | /*ARGSUSED*/ | |
292 | int | |
2d21ac55 | 293 | logioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p) |
1c79356b | 294 | { |
6d2010ae | 295 | int l; |
1c79356b | 296 | |
2d21ac55 | 297 | LOG_LOCK(); |
1c79356b A |
298 | switch (com) { |
299 | ||
300 | /* return number of characters immediately available */ | |
301 | case FIONREAD: | |
1c79356b | 302 | l = msgbufp->msg_bufx - msgbufp->msg_bufr; |
1c79356b | 303 | if (l < 0) |
2d21ac55 | 304 | l += msgbufp->msg_size; |
1c79356b A |
305 | *(off_t *)data = l; |
306 | break; | |
307 | ||
308 | case FIONBIO: | |
309 | if (*(int *)data) | |
310 | logsoftc.sc_state |= LOG_NBIO; | |
311 | else | |
312 | logsoftc.sc_state &= ~LOG_NBIO; | |
313 | break; | |
314 | ||
315 | case FIOASYNC: | |
316 | if (*(int *)data) | |
317 | logsoftc.sc_state |= LOG_ASYNC; | |
318 | else | |
319 | logsoftc.sc_state &= ~LOG_ASYNC; | |
320 | break; | |
321 | ||
322 | case TIOCSPGRP: | |
1c79356b | 323 | logsoftc.sc_pgid = *(int *)data; |
1c79356b A |
324 | break; |
325 | ||
326 | case TIOCGPGRP: | |
1c79356b | 327 | *(int *)data = logsoftc.sc_pgid; |
1c79356b A |
328 | break; |
329 | ||
330 | default: | |
91447636 | 331 | LOG_UNLOCK(); |
1c79356b A |
332 | return (-1); |
333 | } | |
91447636 | 334 | LOG_UNLOCK(); |
1c79356b A |
335 | return (0); |
336 | } | |
337 | ||
338 | void | |
2d21ac55 | 339 | bsd_log_init(void) |
1c79356b | 340 | { |
6d2010ae | 341 | /* After this point, we must be ready to accept characters */ |
1c79356b A |
342 | } |
343 | ||
2d21ac55 A |
344 | |
345 | /* | |
346 | * log_putc_locked | |
347 | * | |
348 | * Decription: Output a character to the log; assumes the LOG_LOCK() is held | |
349 | * by the caller. | |
350 | * | |
351 | * Parameters: c Character to output | |
352 | * | |
353 | * Returns: (void) | |
354 | * | |
355 | * Notes: This functions is used for multibyte output to the log; it | |
356 | * should be used preferrentially where possible to ensure that | |
357 | * log entries do not end up interspersed due to preemption or | |
358 | * SMP reentrancy. | |
359 | */ | |
1c79356b | 360 | void |
2d21ac55 | 361 | log_putc_locked(char c) |
1c79356b | 362 | { |
6d2010ae | 363 | struct msgbuf *mbp; |
1c79356b A |
364 | |
365 | mbp = msgbufp; | |
1c79356b | 366 | mbp->msg_bufc[mbp->msg_bufx++] = c; |
6d2010ae A |
367 | logentrypend = 1; |
368 | if (mbp->msg_bufx >= msgbufp->msg_size) | |
1c79356b | 369 | mbp->msg_bufx = 0; |
2d21ac55 A |
370 | } |
371 | ||
372 | ||
373 | /* | |
374 | * log_putc | |
375 | * | |
376 | * Decription: Output a character to the log; assumes the LOG_LOCK() is NOT | |
377 | * held by the caller. | |
378 | * | |
379 | * Parameters: c Character to output | |
380 | * | |
381 | * Returns: (void) | |
382 | * | |
383 | * Notes: This function is used for syingle byte output to the log. It | |
384 | * primarily exists to maintain binary backward compatibility. | |
385 | */ | |
386 | void | |
387 | log_putc(char c) | |
388 | { | |
2d21ac55 A |
389 | LOG_LOCK(); |
390 | log_putc_locked(c); | |
91447636 | 391 | LOG_UNLOCK(); |
1c79356b | 392 | } |
91447636 | 393 | |
2d21ac55 A |
394 | |
395 | /* | |
396 | * it is possible to increase the kernel log buffer size by adding | |
397 | * msgbuf=n | |
398 | * to the kernel command line, and to read the current size using | |
399 | * sysctl kern.msgbuf | |
400 | * If there is no parameter on the kernel command line, the buffer is | |
6d2010ae A |
401 | * allocated statically and is CONFIG_MSG_BSIZE characters in size, otherwise |
402 | * memory is dynamically allocated. Memory management must already be up. | |
2d21ac55 | 403 | */ |
6d2010ae A |
404 | int |
405 | log_setsize(int size) { | |
2d21ac55 | 406 | char *new_logdata; |
6d2010ae A |
407 | int new_logsize, new_bufr, new_bufx; |
408 | char *old_logdata; | |
409 | int old_logsize, old_bufr, old_bufx; | |
410 | int i, count; | |
411 | char *p, ch; | |
412 | ||
413 | if (size > MAX_MSG_BSIZE) | |
414 | return (EINVAL); | |
415 | ||
416 | if (size <= 0) | |
417 | return (EINVAL); | |
418 | ||
419 | new_logsize = size; | |
2d21ac55 A |
420 | if (!(new_logdata = (char*)kalloc(size))) { |
421 | printf("log_setsize: unable to allocate memory\n"); | |
6d2010ae | 422 | return (ENOMEM); |
2d21ac55 | 423 | } |
6d2010ae A |
424 | bzero(new_logdata, new_logsize); |
425 | ||
2d21ac55 | 426 | LOG_LOCK(); |
6d2010ae A |
427 | |
428 | old_logsize = msgbufp->msg_size; | |
429 | old_logdata = msgbufp->msg_bufc; | |
430 | old_bufr = msgbufp->msg_bufr; | |
431 | old_bufx = msgbufp->msg_bufx; | |
432 | ||
433 | LOG_SETSIZE_DEBUG("log_setsize(%d): old_logdata %p old_logsize %d old_bufr %d old_bufx %d\n", | |
434 | size, old_logdata, old_logsize, old_bufr, old_bufx); | |
435 | ||
436 | /* start "new_logsize" bytes before the write pointer */ | |
437 | if (new_logsize <= old_bufx) { | |
438 | count = new_logsize; | |
439 | p = old_logdata + old_bufx - count; | |
440 | } else { | |
441 | /* | |
442 | * if new buffer is bigger, copy what we have and let the | |
443 | * bzero above handle the difference | |
444 | */ | |
445 | count = MIN(new_logsize, old_logsize); | |
446 | p = old_logdata + old_logsize - (count - old_bufx); | |
447 | } | |
448 | for (i = 0; i < count; i++) { | |
449 | if (p >= old_logdata + old_logsize) | |
450 | p = old_logdata; | |
451 | ||
452 | ch = *p++; | |
453 | new_logdata[i] = ch; | |
454 | } | |
455 | ||
456 | new_bufx = i; | |
457 | if (new_bufx >= new_logsize) | |
458 | new_bufx = 0; | |
459 | msgbufp->msg_bufx = new_bufx; | |
460 | ||
461 | new_bufr = old_bufx - old_bufr; /* how much were we trailing bufx by? */ | |
462 | if (new_bufr < 0) | |
463 | new_bufr += old_logsize; | |
464 | new_bufr = new_bufx - new_bufr; /* now relative to oldest data in new buffer */ | |
465 | if (new_bufr < 0) | |
466 | new_bufr += new_logsize; | |
467 | msgbufp->msg_bufr = new_bufr; | |
468 | ||
469 | msgbufp->msg_size = new_logsize; | |
470 | msgbufp->msg_bufc = new_logdata; | |
471 | ||
472 | LOG_SETSIZE_DEBUG("log_setsize(%d): new_logdata %p new_logsize %d new_bufr %d new_bufx %d\n", | |
473 | size, new_logdata, new_logsize, new_bufr, new_bufx); | |
474 | ||
475 | LOG_UNLOCK(); | |
476 | ||
2d21ac55 A |
477 | /* this memory is now dead - clear it so that it compresses better |
478 | in case of suspend to disk etc. */ | |
6d2010ae A |
479 | bzero(old_logdata, old_logsize); |
480 | if (old_logdata != smsg_bufc) { | |
481 | /* dynamic memory that must be freed */ | |
482 | kfree(old_logdata, old_logsize); | |
483 | } | |
484 | ||
485 | printf("set system log size to %d bytes\n", new_logsize); | |
486 | ||
487 | return 0; | |
488 | } | |
489 | ||
490 | SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0, sysctl_kern_msgbuf, "I", ""); | |
491 | ||
492 | static int sysctl_kern_msgbuf(struct sysctl_oid *oidp __unused, | |
493 | void *arg1 __unused, | |
494 | int arg2 __unused, | |
495 | struct sysctl_req *req) | |
496 | { | |
497 | int old_bufsize, bufsize; | |
498 | int error; | |
499 | ||
500 | LOG_LOCK(); | |
501 | old_bufsize = bufsize = msgbufp->msg_size; | |
2d21ac55 | 502 | LOG_UNLOCK(); |
6d2010ae A |
503 | |
504 | error = sysctl_io_number(req, bufsize, sizeof(bufsize), &bufsize, NULL); | |
505 | if (error) | |
506 | return (error); | |
507 | ||
508 | if (bufsize != old_bufsize) { | |
509 | error = log_setsize(bufsize); | |
510 | } | |
511 | ||
512 | return (error); | |
2d21ac55 A |
513 | } |
514 | ||
2d21ac55 A |
515 | |
516 | /* | |
6d2010ae | 517 | * This should be called by /sbin/dmesg only via libproc. |
2d21ac55 A |
518 | * It returns as much data still in the buffer as possible. |
519 | */ | |
520 | int | |
b0d623f7 A |
521 | log_dmesg(user_addr_t buffer, uint32_t buffersize, int32_t * retval) { |
522 | uint32_t i; | |
6d2010ae | 523 | uint32_t localbuff_size; |
2d21ac55 A |
524 | int error = 0, newl, skip; |
525 | char *localbuff, *p, *copystart, ch; | |
6d2010ae | 526 | size_t copysize; |
2d21ac55 | 527 | |
6d2010ae A |
528 | LOG_LOCK(); |
529 | localbuff_size = (msgbufp->msg_size + 2); /* + '\n' + '\0' */ | |
530 | LOG_UNLOCK(); | |
531 | ||
532 | /* Allocate a temporary non-circular buffer for copyout */ | |
2d21ac55 A |
533 | if (!(localbuff = (char *)kalloc(localbuff_size))) { |
534 | printf("log_dmesg: unable to allocate memory\n"); | |
535 | return (ENOMEM); | |
536 | } | |
6d2010ae | 537 | |
2d21ac55 A |
538 | /* in between here, the log could become bigger, but that's fine */ |
539 | LOG_LOCK(); | |
540 | ||
541 | /* | |
542 | * The message buffer is circular; start at the write pointer, and | |
543 | * make one loop up to write pointer - 1. | |
544 | */ | |
545 | p = msgbufp->msg_bufc + msgbufp->msg_bufx; | |
546 | for (i = newl = skip = 0; p != msgbufp->msg_bufc + msgbufp->msg_bufx - 1; ++p) { | |
547 | if (p >= msgbufp->msg_bufc + msgbufp->msg_size) | |
548 | p = msgbufp->msg_bufc; | |
549 | ch = *p; | |
550 | /* Skip "\n<.*>" syslog sequences. */ | |
551 | if (skip) { | |
552 | if (ch == '>') | |
553 | newl = skip = 0; | |
554 | continue; | |
555 | } | |
556 | if (newl && ch == '<') { | |
557 | skip = 1; | |
558 | continue; | |
559 | } | |
560 | if (ch == '\0') | |
561 | continue; | |
6d2010ae | 562 | newl = (ch == '\n'); |
2d21ac55 | 563 | localbuff[i++] = ch; |
593a1d5f A |
564 | /* The original version of this routine contained a buffer |
565 | * overflow. At the time, a "small" targeted fix was desired | |
566 | * so the change below to check the buffer bounds was made. | |
567 | * TODO: rewrite this needlessly convoluted routine. | |
568 | */ | |
569 | if (i == (localbuff_size - 2)) | |
570 | break; | |
2d21ac55 A |
571 | } |
572 | if (!newl) | |
573 | localbuff[i++] = '\n'; | |
574 | localbuff[i++] = 0; | |
575 | ||
576 | if (buffersize >= i) { | |
577 | copystart = localbuff; | |
578 | copysize = i; | |
579 | } else { | |
580 | copystart = localbuff + i - buffersize; | |
581 | copysize = buffersize; | |
582 | } | |
583 | ||
584 | LOG_UNLOCK(); | |
585 | ||
586 | error = copyout(copystart, buffer, copysize); | |
587 | if (!error) | |
588 | *retval = copysize; | |
589 | ||
590 | kfree(localbuff, localbuff_size); | |
591 | return (error); | |
592 | } |