]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/subr_log.c
xnu-6153.101.6.tar.gz
[apple/xnu.git] / bsd / kern / subr_log.c
1 /*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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 <machine/atomic.h>
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/proc_internal.h>
72 #include <sys/vnode.h>
73 #include <stdbool.h>
74 #include <firehose/tracepoint_private.h>
75 #include <firehose/chunk_private.h>
76 #include <firehose/ioctl_private.h>
77 #include <os/firehose_buffer_private.h>
78
79 #include <os/log_private.h>
80 #include <sys/ioctl.h>
81 #include <sys/msgbuf.h>
82 #include <sys/file_internal.h>
83 #include <sys/errno.h>
84 #include <sys/select.h>
85 #include <sys/kernel.h>
86 #include <kern/thread.h>
87 #include <kern/sched_prim.h>
88 #include <kern/simple_lock.h>
89 #include <sys/lock.h>
90 #include <sys/signalvar.h>
91 #include <sys/conf.h>
92 #include <sys/sysctl.h>
93 #include <sys/queue.h>
94 #include <kern/kalloc.h>
95 #include <pexpert/pexpert.h>
96 #include <mach/mach_port.h>
97 #include <mach/mach_vm.h>
98 #include <mach/vm_map.h>
99 #include <vm/vm_kern.h>
100 #include <kern/task.h>
101 #include <kern/locks.h>
102
103 /* XXX should be in a common header somewhere */
104 extern void logwakeup(struct msgbuf *);
105 extern void oslogwakeup(void);
106 extern void oslog_streamwakeup(void);
107 static void oslog_streamwakeup_locked(void);
108 vm_offset_t kernel_firehose_addr = 0;
109
110 /* log message counters for streaming mode */
111 uint32_t oslog_s_streamed_msgcount = 0;
112 uint32_t oslog_s_dropped_msgcount = 0;
113 extern uint32_t oslog_s_error_count;
114
115 #define LOG_RDPRI (PZERO + 1)
116
117 #define LOG_NBIO 0x02
118 #define LOG_ASYNC 0x04
119 #define LOG_RDWAIT 0x08
120
121 /* All globals should be accessed under LOG_LOCK() */
122
123 static char amsg_bufc[1024];
124 static struct msgbuf aslbuf = {.msg_magic = MSG_MAGIC, .msg_size = sizeof(amsg_bufc), .msg_bufx = 0, .msg_bufr = 0, .msg_bufc = amsg_bufc};
125 struct msgbuf *aslbufp __attribute__((used)) = &aslbuf;
126
127 /* logsoftc only valid while log_open=1 */
128 struct logsoftc {
129 int sc_state; /* see above for possibilities */
130 struct selinfo sc_selp; /* thread waiting for select */
131 int sc_pgid; /* process/group for async I/O */
132 struct msgbuf *sc_mbp;
133 } logsoftc;
134
135 static int log_open;
136 char smsg_bufc[CONFIG_MSG_BSIZE]; /* static buffer */
137 char oslog_stream_bufc[FIREHOSE_CHUNK_SIZE]; /* static buffer */
138 struct firehose_chunk_s oslog_boot_buf = {
139 .fc_pos = {
140 .fcp_next_entry_offs = offsetof(struct firehose_chunk_s, fc_data),
141 .fcp_private_offs = FIREHOSE_CHUNK_SIZE,
142 .fcp_refcnt = 1, // indicate that there is a writer to this chunk
143 .fcp_stream = firehose_stream_persist,
144 .fcp_flag_io = 1, // for now, lets assume this is coming from the io bank
145 },
146 }; /* static buffer */
147 firehose_chunk_t firehose_boot_chunk = &oslog_boot_buf;
148 struct msgbuf msgbuf = {.msg_magic = MSG_MAGIC, .msg_size = sizeof(smsg_bufc), .msg_bufx = 0, .msg_bufr = 0, .msg_bufc = smsg_bufc};
149 struct msgbuf oslog_stream_buf = {.msg_magic = MSG_MAGIC, .msg_size = 0, .msg_bufx = 0, .msg_bufr = 0, .msg_bufc = NULL};
150 struct msgbuf *msgbufp __attribute__((used)) = &msgbuf;
151 struct msgbuf *oslog_streambufp __attribute__((used)) = &oslog_stream_buf;
152
153 // List entries for keeping track of the streaming buffer
154 static oslog_stream_buf_entry_t oslog_stream_buf_entries;
155
156 #define OSLOG_NUM_STREAM_ENTRIES 64
157 #define OSLOG_STREAM_BUF_SIZE 4096
158
159 int oslog_open = 0;
160 int os_log_wakeup = 0;
161 int oslog_stream_open = 0;
162 int oslog_stream_buf_bytesavail = 0;
163 int oslog_stream_buf_size = OSLOG_STREAM_BUF_SIZE;
164 int oslog_stream_num_entries = OSLOG_NUM_STREAM_ENTRIES;
165
166 uint8_t __firehose_buffer_kernel_chunk_count = FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT;
167 uint8_t __firehose_num_kernel_io_pages = FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES;
168
169 /* oslogsoftc only valid while oslog_open=1 */
170 struct oslogsoftc {
171 int sc_state; /* see above for possibilities */
172 struct selinfo sc_selp; /* thread waiting for select */
173 int sc_pgid; /* process/group for async I/O */
174 } oslogsoftc;
175
176 struct oslog_streamsoftc {
177 int sc_state; /* see above for possibilities */
178 struct selinfo sc_selp; /* thread waiting for select */
179 int sc_pgid; /* process/group for async I/O */
180 } oslog_streamsoftc;
181
182 STAILQ_HEAD(, oslog_stream_buf_entry_s) oslog_stream_free_head =
183 STAILQ_HEAD_INITIALIZER(oslog_stream_free_head);
184 STAILQ_HEAD(, oslog_stream_buf_entry_s) oslog_stream_buf_head =
185 STAILQ_HEAD_INITIALIZER(oslog_stream_buf_head);
186
187 /* defined in osfmk/kern/printf.c */
188 extern void oslog_lock_init(void);
189 extern void bsd_log_lock(void);
190 extern void bsd_log_unlock(void);
191
192 /* defined for osfmk/kern/printf.c */
193 void bsd_log_init(void);
194
195 /*
196 * Ideally this file would define this lock, but bsd doesn't have the definition
197 * for lock groups.
198 */
199 decl_lck_spin_data(extern, oslog_stream_lock);
200 #define stream_lock() lck_spin_lock(&oslog_stream_lock)
201 #define stream_unlock() lck_spin_unlock(&oslog_stream_lock)
202
203 /* XXX wants a linker set so these can be static */
204 extern d_open_t logopen;
205 extern d_close_t logclose;
206 extern d_read_t logread;
207 extern d_ioctl_t logioctl;
208 extern d_select_t logselect;
209
210 /* XXX wants a linker set so these can be static */
211 extern d_open_t oslogopen;
212 extern d_close_t oslogclose;
213 extern d_select_t oslogselect;
214 extern d_ioctl_t oslogioctl;
215
216 /* XXX wants a linker set so these can be static */
217 extern d_open_t oslog_streamopen;
218 extern d_close_t oslog_streamclose;
219 extern d_read_t oslog_streamread;
220 extern d_ioctl_t oslog_streamioctl;
221 extern d_select_t oslog_streamselect;
222
223 void oslog_init(void);
224 void oslog_setsize(int size);
225 void oslog_streamwrite_locked(firehose_tracepoint_id_u ftid,
226 uint64_t stamp, const void *pubdata, size_t publen);
227 void oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry);
228 static oslog_stream_buf_entry_t oslog_stream_find_free_buf_entry_locked(void);
229 static void oslog_streamwrite_append_bytes(const char *buffer, int buflen);
230
231 /*
232 * Serialize log access. Note that the log can be written at interrupt level,
233 * so any log manipulations that can be done from, or affect, another processor
234 * at interrupt level must be guarded with a spin lock.
235 */
236
237 #define LOG_LOCK() bsd_log_lock()
238 #define LOG_UNLOCK() bsd_log_unlock()
239
240 #if DEBUG
241 #define LOG_SETSIZE_DEBUG(x...) kprintf(x)
242 #else
243 #define LOG_SETSIZE_DEBUG(x...) do { } while(0)
244 #endif
245
246 static int sysctl_kern_msgbuf(struct sysctl_oid *oidp,
247 void *arg1, int arg2, struct sysctl_req *req);
248
249 /*ARGSUSED*/
250 int
251 logopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p)
252 {
253 LOG_LOCK();
254 if (log_open) {
255 LOG_UNLOCK();
256 return EBUSY;
257 }
258 if (atm_get_diagnostic_config() & ATM_ENABLE_LEGACY_LOGGING) {
259 logsoftc.sc_mbp = msgbufp;
260 } else {
261 /*
262 * Support for messagetracer (kern_asl_msg())
263 * In this mode, /dev/klog exports only ASL-formatted messages
264 * written into aslbufp via vaddlog().
265 */
266 logsoftc.sc_mbp = aslbufp;
267 }
268 logsoftc.sc_pgid = p->p_pid; /* signal process only */
269 log_open = 1;
270
271 LOG_UNLOCK();
272
273 return 0;
274 }
275
276 /*ARGSUSED*/
277 int
278 logclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p)
279 {
280 LOG_LOCK();
281 logsoftc.sc_state &= ~(LOG_NBIO | LOG_ASYNC);
282 selwakeup(&logsoftc.sc_selp);
283 selthreadclear(&logsoftc.sc_selp);
284 log_open = 0;
285 LOG_UNLOCK();
286 return 0;
287 }
288
289
290 int
291 oslogopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p)
292 {
293 LOG_LOCK();
294 if (oslog_open) {
295 LOG_UNLOCK();
296 return EBUSY;
297 }
298 oslogsoftc.sc_pgid = p->p_pid; /* signal process only */
299 oslog_open = 1;
300
301 LOG_UNLOCK();
302 return 0;
303 }
304
305 int
306 oslogclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p)
307 {
308 LOG_LOCK();
309 oslogsoftc.sc_state &= ~(LOG_NBIO | LOG_ASYNC);
310 selwakeup(&oslogsoftc.sc_selp);
311 selthreadclear(&oslogsoftc.sc_selp);
312 oslog_open = 0;
313 LOG_UNLOCK();
314 return 0;
315 }
316
317 int
318 oslog_streamopen(__unused dev_t dev, __unused int flags, __unused int mode, struct proc *p)
319 {
320 char *oslog_stream_msg_bufc = NULL;
321 oslog_stream_buf_entry_t entries = NULL;
322
323 stream_lock();
324 if (oslog_stream_open) {
325 stream_unlock();
326 return EBUSY;
327 }
328 stream_unlock();
329
330 // Allocate the stream buffer
331 oslog_stream_msg_bufc = kalloc(oslog_stream_buf_size);
332 if (!oslog_stream_msg_bufc) {
333 return ENOMEM;
334 }
335 /* Zeroing to avoid copying uninitialized struct padding to userspace. */
336 bzero(oslog_stream_msg_bufc, oslog_stream_buf_size);
337
338 /* entries to support kernel logging in stream mode */
339 size_t entries_size = oslog_stream_num_entries * sizeof(struct oslog_stream_buf_entry_s);
340 entries = kalloc(entries_size);
341 if (!entries) {
342 kfree(oslog_stream_msg_bufc, oslog_stream_buf_size);
343 return ENOMEM;
344 }
345 /* Zeroing to avoid copying uninitialized struct padding to userspace. */
346 bzero(entries, entries_size);
347
348 stream_lock();
349 if (oslog_stream_open) {
350 stream_unlock();
351 kfree(oslog_stream_msg_bufc, oslog_stream_buf_size);
352 kfree(entries, entries_size);
353 return EBUSY;
354 }
355
356 assert(oslog_streambufp->msg_bufc == NULL);
357 oslog_streambufp->msg_bufc = oslog_stream_msg_bufc;
358 oslog_streambufp->msg_size = oslog_stream_buf_size;
359
360 oslog_stream_buf_entries = entries;
361
362 STAILQ_INIT(&oslog_stream_free_head);
363 STAILQ_INIT(&oslog_stream_buf_head);
364
365 for (int i = 0; i < oslog_stream_num_entries; i++) {
366 oslog_stream_buf_entries[i].type = oslog_stream_link_type_log;
367 STAILQ_INSERT_TAIL(&oslog_stream_free_head, &oslog_stream_buf_entries[i], buf_entries);
368 }
369
370 /* there should be no pending entries in the stream */
371 assert(STAILQ_EMPTY(&oslog_stream_buf_head));
372 assert(oslog_streambufp->msg_bufx == 0);
373 assert(oslog_streambufp->msg_bufr == 0);
374
375 oslog_streambufp->msg_bufx = 0;
376 oslog_streambufp->msg_bufr = 0;
377 oslog_streamsoftc.sc_pgid = p->p_pid; /* signal process only */
378 oslog_stream_open = 1;
379 oslog_stream_buf_bytesavail = oslog_stream_buf_size;
380 stream_unlock();
381
382 return 0;
383 }
384
385 int
386 oslog_streamclose(__unused dev_t dev, __unused int flag, __unused int devtype, __unused struct proc *p)
387 {
388 oslog_stream_buf_entry_t next_entry = NULL;
389 char *oslog_stream_msg_bufc = NULL;
390 oslog_stream_buf_entry_t entries = NULL;
391
392 stream_lock();
393
394 if (oslog_stream_open == 0) {
395 stream_unlock();
396 return EBADF;
397 }
398
399 // Consume all log lines
400 while (!STAILQ_EMPTY(&oslog_stream_buf_head)) {
401 next_entry = STAILQ_FIRST(&oslog_stream_buf_head);
402 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries);
403 }
404 oslog_streamwakeup_locked();
405 oslog_streamsoftc.sc_state &= ~(LOG_NBIO | LOG_ASYNC);
406 selwakeup(&oslog_streamsoftc.sc_selp);
407 selthreadclear(&oslog_streamsoftc.sc_selp);
408 oslog_stream_open = 0;
409 oslog_streambufp->msg_bufr = 0;
410 oslog_streambufp->msg_bufx = 0;
411 oslog_stream_msg_bufc = oslog_streambufp->msg_bufc;
412 oslog_streambufp->msg_bufc = NULL;
413 entries = oslog_stream_buf_entries;
414 oslog_stream_buf_entries = NULL;
415 oslog_streambufp->msg_size = 0;
416
417 stream_unlock();
418
419 // Free the stream buffer
420 kfree(oslog_stream_msg_bufc, oslog_stream_buf_size);
421 // Free the list entries
422 kfree(entries, oslog_stream_num_entries * sizeof(struct oslog_stream_buf_entry_s));
423
424 return 0;
425 }
426
427 /*ARGSUSED*/
428 int
429 logread(__unused dev_t dev, struct uio *uio, int flag)
430 {
431 int l;
432 int error = 0;
433 struct msgbuf *mbp = logsoftc.sc_mbp;
434
435 LOG_LOCK();
436 while (mbp->msg_bufr == mbp->msg_bufx) {
437 if (flag & IO_NDELAY) {
438 error = EWOULDBLOCK;
439 goto out;
440 }
441 if (logsoftc.sc_state & LOG_NBIO) {
442 error = EWOULDBLOCK;
443 goto out;
444 }
445 logsoftc.sc_state |= LOG_RDWAIT;
446 LOG_UNLOCK();
447 /*
448 * If the wakeup is missed
449 * then wait for 5 sec and reevaluate
450 */
451 if ((error = tsleep((caddr_t)mbp, LOG_RDPRI | PCATCH,
452 "klog", 5 * hz)) != 0) {
453 /* if it times out; ignore */
454 if (error != EWOULDBLOCK) {
455 return error;
456 }
457 }
458 LOG_LOCK();
459 }
460 logsoftc.sc_state &= ~LOG_RDWAIT;
461
462 while (uio_resid(uio) > 0) {
463 int readpos;
464
465 l = mbp->msg_bufx - mbp->msg_bufr;
466 if (l < 0) {
467 l = mbp->msg_size - mbp->msg_bufr;
468 }
469 l = min(l, uio_resid(uio));
470 if (l == 0) {
471 break;
472 }
473
474 readpos = mbp->msg_bufr;
475 LOG_UNLOCK();
476 error = uiomove((caddr_t)&mbp->msg_bufc[readpos], l, uio);
477 LOG_LOCK();
478 if (error) {
479 break;
480 }
481 mbp->msg_bufr = readpos + l;
482 if (mbp->msg_bufr >= mbp->msg_size) {
483 mbp->msg_bufr = 0;
484 }
485 }
486 out:
487 LOG_UNLOCK();
488 return error;
489 }
490
491 /*ARGSUSED*/
492 int
493 oslog_streamread(__unused dev_t dev, struct uio *uio, int flag)
494 {
495 int error = 0;
496 int copy_size = 0;
497 static char logline[FIREHOSE_CHUNK_SIZE];
498
499 stream_lock();
500
501 if (!oslog_stream_open) {
502 stream_unlock();
503 return EBADF;
504 }
505
506 while (STAILQ_EMPTY(&oslog_stream_buf_head)) {
507 assert(oslog_stream_buf_bytesavail == oslog_stream_buf_size);
508
509 if (flag & IO_NDELAY || oslog_streamsoftc.sc_state & LOG_NBIO) {
510 stream_unlock();
511 return EWOULDBLOCK;
512 }
513
514 oslog_streamsoftc.sc_state |= LOG_RDWAIT;
515 wait_result_t wr = assert_wait((event_t)oslog_streambufp,
516 THREAD_INTERRUPTIBLE);
517 if (wr == THREAD_WAITING) {
518 stream_unlock();
519 wr = thread_block(THREAD_CONTINUE_NULL);
520 stream_lock();
521 }
522
523 switch (wr) {
524 case THREAD_AWAKENED:
525 case THREAD_TIMED_OUT:
526 break;
527 default:
528 stream_unlock();
529 return EINTR;
530 }
531 }
532
533 if (!oslog_stream_open) {
534 stream_unlock();
535 return EBADF;
536 }
537
538 int logpos = 0;
539 oslog_stream_buf_entry_t read_entry = NULL;
540 uint16_t rec_length;
541
542 read_entry = STAILQ_FIRST(&oslog_stream_buf_head);
543 assert(read_entry != NULL);
544 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries);
545
546 // Copy the timestamp first
547 memcpy(logline + logpos, &read_entry->timestamp, sizeof(uint64_t));
548 logpos += sizeof(uint64_t);
549
550 switch (read_entry->type) {
551 /* Handle metadata messages */
552 case oslog_stream_link_type_metadata:
553 {
554 memcpy(logline + logpos,
555 (read_entry->metadata), read_entry->size);
556 logpos += read_entry->size;
557
558 stream_unlock();
559
560 // Free the list entry
561 kfree(read_entry, (sizeof(struct oslog_stream_buf_entry_s) + read_entry->size));
562 break;
563 }
564 /* Handle log messages */
565 case oslog_stream_link_type_log:
566 {
567 /* ensure that the correct read entry was dequeued */
568 assert(read_entry->offset == oslog_streambufp->msg_bufr);
569 rec_length = read_entry->size;
570
571 // If the next log line is contiguous in the buffer, copy it out.
572 if (read_entry->offset + rec_length <= oslog_streambufp->msg_size) {
573 memcpy(logline + logpos,
574 oslog_streambufp->msg_bufc + read_entry->offset, rec_length);
575
576 oslog_streambufp->msg_bufr += rec_length;
577 if (oslog_streambufp->msg_bufr == oslog_streambufp->msg_size) {
578 oslog_streambufp->msg_bufr = 0;
579 }
580 logpos += rec_length;
581 } else {
582 // Otherwise, copy until the end of the buffer, and
583 // copy the remaining bytes starting at index 0.
584 int bytes_left = oslog_streambufp->msg_size - read_entry->offset;
585 memcpy(logline + logpos,
586 oslog_streambufp->msg_bufc + read_entry->offset, bytes_left);
587 logpos += bytes_left;
588 rec_length -= bytes_left;
589
590 memcpy(logline + logpos, (const void *)oslog_streambufp->msg_bufc,
591 rec_length);
592 oslog_streambufp->msg_bufr = rec_length;
593 logpos += rec_length;
594 }
595
596 oslog_stream_buf_bytesavail += read_entry->size;
597 assert(oslog_stream_buf_bytesavail <= oslog_stream_buf_size);
598
599 assert(oslog_streambufp->msg_bufr < oslog_streambufp->msg_size);
600 STAILQ_INSERT_TAIL(&oslog_stream_free_head, read_entry, buf_entries);
601
602 stream_unlock();
603 break;
604 }
605 default:
606 {
607 panic("Got unexpected log entry type: %hhu\n", read_entry->type);
608 }
609 }
610
611 copy_size = min(logpos, uio_resid(uio));
612 if (copy_size != 0) {
613 error = uiomove((caddr_t)logline, copy_size, uio);
614 }
615 os_atomic_inc(&oslog_s_streamed_msgcount, relaxed);
616
617 return error;
618 }
619
620 /*ARGSUSED*/
621 int
622 logselect(__unused dev_t dev, int rw, void * wql, struct proc *p)
623 {
624 const struct msgbuf *mbp = logsoftc.sc_mbp;
625
626 switch (rw) {
627 case FREAD:
628 LOG_LOCK();
629 if (mbp->msg_bufr != mbp->msg_bufx) {
630 LOG_UNLOCK();
631 return 1;
632 }
633 selrecord(p, &logsoftc.sc_selp, wql);
634 LOG_UNLOCK();
635 break;
636 }
637 return 0;
638 }
639
640 int
641 oslogselect(__unused dev_t dev, int rw, void * wql, struct proc *p)
642 {
643 switch (rw) {
644 case FREAD:
645 LOG_LOCK();
646 if (os_log_wakeup) {
647 LOG_UNLOCK();
648 return 1;
649 }
650 selrecord(p, &oslogsoftc.sc_selp, wql);
651 LOG_UNLOCK();
652 break;
653 }
654 return 0;
655 }
656
657 int
658 oslog_streamselect(__unused dev_t dev, int rw, void * wql, struct proc *p)
659 {
660 int ret = 0;
661
662 stream_lock();
663
664 switch (rw) {
665 case FREAD:
666 if (STAILQ_EMPTY(&oslog_stream_buf_head)) {
667 selrecord(p, &oslog_streamsoftc.sc_selp, wql);
668 } else {
669 ret = 1;
670 }
671 break;
672 }
673
674 stream_unlock();
675 return ret;
676 }
677
678 void
679 logwakeup(struct msgbuf *mbp)
680 {
681 /* cf. r24974766 & r25201228*/
682 if (oslog_is_safe() == FALSE) {
683 return;
684 }
685
686 LOG_LOCK();
687 if (!log_open) {
688 LOG_UNLOCK();
689 return;
690 }
691 if (NULL == mbp) {
692 mbp = logsoftc.sc_mbp;
693 }
694 if (mbp != logsoftc.sc_mbp) {
695 goto out;
696 }
697 selwakeup(&logsoftc.sc_selp);
698 if (logsoftc.sc_state & LOG_ASYNC) {
699 int pgid = logsoftc.sc_pgid;
700 LOG_UNLOCK();
701 if (pgid < 0) {
702 gsignal(-pgid, SIGIO);
703 } else {
704 proc_signal(pgid, SIGIO);
705 }
706 LOG_LOCK();
707 }
708 if (logsoftc.sc_state & LOG_RDWAIT) {
709 wakeup((caddr_t)mbp);
710 logsoftc.sc_state &= ~LOG_RDWAIT;
711 }
712 out:
713 LOG_UNLOCK();
714 }
715
716 void
717 oslogwakeup(void)
718 {
719 LOG_LOCK();
720 if (!oslog_open) {
721 LOG_UNLOCK();
722 return;
723 }
724 selwakeup(&oslogsoftc.sc_selp);
725 os_log_wakeup = 1;
726 LOG_UNLOCK();
727 }
728
729 static void
730 oslog_streamwakeup_locked(void)
731 {
732 LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED);
733 if (!oslog_stream_open) {
734 return;
735 }
736 selwakeup(&oslog_streamsoftc.sc_selp);
737 if (oslog_streamsoftc.sc_state & LOG_RDWAIT) {
738 wakeup((caddr_t)oslog_streambufp);
739 oslog_streamsoftc.sc_state &= ~LOG_RDWAIT;
740 }
741 }
742
743 void
744 oslog_streamwakeup(void)
745 {
746 /* cf. r24974766 & r25201228*/
747 if (oslog_is_safe() == FALSE) {
748 return;
749 }
750
751 stream_lock();
752 oslog_streamwakeup_locked();
753 stream_unlock();
754 }
755
756 /*ARGSUSED*/
757 int
758 logioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p)
759 {
760 int l;
761 const struct msgbuf *mbp = logsoftc.sc_mbp;
762
763 LOG_LOCK();
764 switch (com) {
765 /* return number of characters immediately available */
766 case FIONREAD:
767 l = mbp->msg_bufx - mbp->msg_bufr;
768 if (l < 0) {
769 l += mbp->msg_size;
770 }
771 *(off_t *)data = l;
772 break;
773
774 case FIONBIO:
775 if (*(int *)data) {
776 logsoftc.sc_state |= LOG_NBIO;
777 } else {
778 logsoftc.sc_state &= ~LOG_NBIO;
779 }
780 break;
781
782 case FIOASYNC:
783 if (*(int *)data) {
784 logsoftc.sc_state |= LOG_ASYNC;
785 } else {
786 logsoftc.sc_state &= ~LOG_ASYNC;
787 }
788 break;
789
790 case TIOCSPGRP:
791 logsoftc.sc_pgid = *(int *)data;
792 break;
793
794 case TIOCGPGRP:
795 *(int *)data = logsoftc.sc_pgid;
796 break;
797
798 default:
799 LOG_UNLOCK();
800 return -1;
801 }
802 LOG_UNLOCK();
803 return 0;
804 }
805
806 /*ARGSUSED*/
807 int
808 oslogioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p)
809 {
810 int ret = 0;
811 mach_vm_size_t buffer_size = (__firehose_buffer_kernel_chunk_count * FIREHOSE_CHUNK_SIZE);
812 firehose_buffer_map_info_t map_info = {0, 0};
813 firehose_buffer_t kernel_firehose_buffer = NULL;
814 mach_vm_address_t user_addr = 0;
815 mach_port_t mem_entry_ptr = MACH_PORT_NULL;
816
817 switch (com) {
818 /* return number of characters immediately available */
819
820 case LOGBUFFERMAP:
821 kernel_firehose_buffer = (firehose_buffer_t)kernel_firehose_addr;
822
823 ret = mach_make_memory_entry_64(kernel_map,
824 &buffer_size,
825 (mach_vm_offset_t) kernel_firehose_buffer,
826 (MAP_MEM_VM_SHARE | VM_PROT_READ),
827 &mem_entry_ptr,
828 MACH_PORT_NULL);
829 if (ret == KERN_SUCCESS) {
830 ret = mach_vm_map_kernel(get_task_map(current_task()),
831 &user_addr,
832 buffer_size,
833 0, /* mask */
834 VM_FLAGS_ANYWHERE,
835 VM_MAP_KERNEL_FLAGS_NONE,
836 VM_KERN_MEMORY_NONE,
837 mem_entry_ptr,
838 0, /* offset */
839 FALSE, /* copy */
840 VM_PROT_READ,
841 VM_PROT_READ,
842 VM_INHERIT_SHARE);
843 }
844
845 if (ret == KERN_SUCCESS) {
846 map_info.fbmi_addr = (uint64_t) (user_addr);
847 map_info.fbmi_size = buffer_size;
848 bcopy(&map_info, data, sizeof(firehose_buffer_map_info_t));
849 }
850 break;
851 case LOGFLUSHED:
852 LOG_LOCK();
853 os_log_wakeup = 0;
854 LOG_UNLOCK();
855 __firehose_merge_updates(*(firehose_push_reply_t *)(data));
856 break;
857 default:
858 return -1;
859 }
860 return 0;
861 }
862
863 /*ARGSUSED*/
864 int
865 oslog_streamioctl(__unused dev_t dev, u_long com, caddr_t data, __unused int flag, __unused struct proc *p)
866 {
867 int err = 0;
868
869 stream_lock();
870
871 switch (com) {
872 case FIONBIO:
873 if (data && *(int *)data) {
874 oslog_streamsoftc.sc_state |= LOG_NBIO;
875 } else {
876 oslog_streamsoftc.sc_state &= ~LOG_NBIO;
877 }
878 break;
879 case FIOASYNC:
880 if (data && *(int *)data) {
881 oslog_streamsoftc.sc_state |= LOG_ASYNC;
882 } else {
883 oslog_streamsoftc.sc_state &= ~LOG_ASYNC;
884 }
885 break;
886 default:
887 err = -1;
888 break;
889 }
890
891 stream_unlock();
892 return err;
893 }
894
895 void
896 bsd_log_init(void)
897 {
898 /* After this point, we must be ready to accept characters */
899 }
900
901 void
902 oslog_init(void)
903 {
904 kern_return_t kr;
905 if (!PE_parse_boot_argn("firehose_chunk_count", &__firehose_buffer_kernel_chunk_count, sizeof(__firehose_buffer_kernel_chunk_count))) {
906 __firehose_buffer_kernel_chunk_count = FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT;
907 }
908 if (!PE_parse_boot_argn("firehose_io_pages", &__firehose_num_kernel_io_pages, sizeof(__firehose_num_kernel_io_pages))) {
909 __firehose_num_kernel_io_pages = FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES;
910 }
911 if (!__firehose_kernel_configuration_valid(__firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages)) {
912 printf("illegal firehose configuration %u/%u, using defaults\n", __firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages);
913 __firehose_buffer_kernel_chunk_count = FIREHOSE_BUFFER_KERNEL_DEFAULT_CHUNK_COUNT;
914 __firehose_num_kernel_io_pages = FIREHOSE_BUFFER_KERNEL_DEFAULT_IO_PAGES;
915 }
916 vm_size_t size = __firehose_buffer_kernel_chunk_count * FIREHOSE_CHUNK_SIZE;
917
918 oslog_lock_init();
919
920 kr = kmem_alloc_flags(kernel_map, &kernel_firehose_addr,
921 size + (2 * PAGE_SIZE), VM_KERN_MEMORY_LOG,
922 KMA_GUARD_FIRST | KMA_GUARD_LAST);
923 if (kr != KERN_SUCCESS) {
924 panic("Failed to allocate memory for firehose logging buffer");
925 }
926 kernel_firehose_addr += PAGE_SIZE;
927 bzero((void *)kernel_firehose_addr, size);
928 /* register buffer with firehose */
929 kernel_firehose_addr = (vm_offset_t)__firehose_buffer_create((size_t *) &size);
930
931 printf("oslog_init completed, %u chunks, %u io pages\n", __firehose_buffer_kernel_chunk_count, __firehose_num_kernel_io_pages);
932 }
933
934 /*
935 * log_putc_locked
936 *
937 * Decription: Output a character to the log; assumes the LOG_LOCK() is held
938 * by the caller.
939 *
940 * Parameters: c Character to output
941 *
942 * Returns: (void)
943 *
944 * Notes: This functions is used for multibyte output to the log; it
945 * should be used preferrentially where possible to ensure that
946 * log entries do not end up interspersed due to preemption or
947 * SMP reentrancy.
948 */
949 void
950 log_putc_locked(struct msgbuf *mbp, char c)
951 {
952 mbp->msg_bufc[mbp->msg_bufx++] = c;
953 if (mbp->msg_bufx >= mbp->msg_size) {
954 mbp->msg_bufx = 0;
955 }
956 }
957
958 static oslog_stream_buf_entry_t
959 oslog_stream_find_free_buf_entry_locked(void)
960 {
961 struct msgbuf *mbp;
962 oslog_stream_buf_entry_t buf_entry = NULL;
963
964 LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED);
965
966 mbp = oslog_streambufp;
967
968 buf_entry = STAILQ_FIRST(&oslog_stream_free_head);
969 if (buf_entry) {
970 STAILQ_REMOVE_HEAD(&oslog_stream_free_head, buf_entries);
971 } else {
972 // If no list elements are available in the free-list,
973 // consume the next log line so we can free up its list element
974 oslog_stream_buf_entry_t prev_entry = NULL;
975
976 buf_entry = STAILQ_FIRST(&oslog_stream_buf_head);
977 while (buf_entry->type == oslog_stream_link_type_metadata) {
978 prev_entry = buf_entry;
979 buf_entry = STAILQ_NEXT(buf_entry, buf_entries);
980 }
981
982 if (prev_entry == NULL) {
983 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries);
984 } else {
985 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head, prev_entry, buf_entries);
986 }
987
988 mbp->msg_bufr += buf_entry->size;
989 oslog_s_dropped_msgcount++;
990 if (mbp->msg_bufr >= mbp->msg_size) {
991 mbp->msg_bufr = (mbp->msg_bufr % mbp->msg_size);
992 }
993 }
994
995 return buf_entry;
996 }
997
998 void
999 oslog_streamwrite_metadata_locked(oslog_stream_buf_entry_t m_entry)
1000 {
1001 LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED);
1002 STAILQ_INSERT_TAIL(&oslog_stream_buf_head, m_entry, buf_entries);
1003
1004 return;
1005 }
1006
1007 static void
1008 oslog_streamwrite_append_bytes(const char *buffer, int buflen)
1009 {
1010 struct msgbuf *mbp;
1011
1012 LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED);
1013
1014 assert(oslog_stream_buf_bytesavail >= buflen);
1015 oslog_stream_buf_bytesavail -= buflen;
1016 assert(oslog_stream_buf_bytesavail >= 0);
1017
1018 mbp = oslog_streambufp;
1019 if (mbp->msg_bufx + buflen <= mbp->msg_size) {
1020 /*
1021 * If this will fit without needing to be split across the end
1022 * of the buffer, copy it directly in one go.
1023 */
1024 memcpy((void *)(mbp->msg_bufc + mbp->msg_bufx), buffer, buflen);
1025
1026 mbp->msg_bufx += buflen;
1027 if (mbp->msg_bufx == mbp->msg_size) {
1028 mbp->msg_bufx = 0;
1029 }
1030 } else {
1031 /*
1032 * Copy up to the end of the stream buffer, and then put what remains
1033 * at the beginning.
1034 */
1035 int bytes_left = mbp->msg_size - mbp->msg_bufx;
1036 memcpy((void *)(mbp->msg_bufc + mbp->msg_bufx), buffer, bytes_left);
1037
1038 buflen -= bytes_left;
1039 buffer += bytes_left;
1040
1041 // Copy the remainder of the data from the beginning of stream
1042 memcpy((void *)mbp->msg_bufc, buffer, buflen);
1043 mbp->msg_bufx = buflen;
1044 }
1045 return;
1046 }
1047
1048 void
1049 oslog_streamwrite_locked(firehose_tracepoint_id_u ftid,
1050 uint64_t stamp, const void *pubdata, size_t publen)
1051 {
1052 struct msgbuf *mbp;
1053 oslog_stream_buf_entry_t buf_entry = NULL;
1054 oslog_stream_buf_entry_t next_entry = NULL;
1055
1056 uint16_t ft_size = offsetof(struct firehose_tracepoint_s, ft_data);
1057 int ft_length = ft_size + publen;
1058
1059 LCK_SPIN_ASSERT(&oslog_stream_lock, LCK_ASSERT_OWNED);
1060
1061 mbp = oslog_streambufp;
1062 if (ft_length > mbp->msg_size) {
1063 os_atomic_inc(&oslog_s_error_count, relaxed);
1064 return;
1065 }
1066
1067 // Ensure that we have a list element for this record
1068 buf_entry = oslog_stream_find_free_buf_entry_locked();
1069
1070 assert(buf_entry != NULL);
1071
1072 while (ft_length > oslog_stream_buf_bytesavail) {
1073 oslog_stream_buf_entry_t prev_entry = NULL;
1074
1075 next_entry = STAILQ_FIRST(&oslog_stream_buf_head);
1076 assert(next_entry != NULL);
1077 while (next_entry->type == oslog_stream_link_type_metadata) {
1078 prev_entry = next_entry;
1079 next_entry = STAILQ_NEXT(next_entry, buf_entries);
1080 }
1081
1082 if (prev_entry == NULL) {
1083 STAILQ_REMOVE_HEAD(&oslog_stream_buf_head, buf_entries);
1084 } else {
1085 STAILQ_REMOVE_AFTER(&oslog_stream_buf_head, prev_entry, buf_entries);
1086 }
1087
1088 mbp->msg_bufr += next_entry->size;
1089 if (mbp->msg_bufr >= mbp->msg_size) {
1090 mbp->msg_bufr = (mbp->msg_bufr % mbp->msg_size);
1091 }
1092
1093 oslog_s_dropped_msgcount++;
1094 oslog_stream_buf_bytesavail += next_entry->size;
1095 assert(oslog_stream_buf_bytesavail <= oslog_stream_buf_size);
1096
1097 STAILQ_INSERT_TAIL(&oslog_stream_free_head, next_entry, buf_entries);
1098 }
1099
1100 assert(ft_length <= oslog_stream_buf_bytesavail);
1101
1102 // Write the log line and update the list entry for this record
1103 buf_entry->offset = mbp->msg_bufx;
1104 buf_entry->size = ft_length;
1105 buf_entry->timestamp = stamp;
1106 buf_entry->type = oslog_stream_link_type_log;
1107
1108 // Construct a tracepoint
1109 struct firehose_tracepoint_s fs = {
1110 .ft_thread = thread_tid(current_thread()),
1111 .ft_id.ftid_value = ftid.ftid_value,
1112 .ft_length = publen
1113 };
1114
1115 oslog_streamwrite_append_bytes((char *)&fs, sizeof(fs));
1116 oslog_streamwrite_append_bytes(pubdata, publen);
1117
1118 assert(mbp->msg_bufr < mbp->msg_size);
1119 // Insert the element to the buffer data list
1120 STAILQ_INSERT_TAIL(&oslog_stream_buf_head, buf_entry, buf_entries);
1121
1122 return;
1123 }
1124
1125
1126
1127 /*
1128 * log_putc
1129 *
1130 * Decription: Output a character to the log; assumes the LOG_LOCK() is NOT
1131 * held by the caller.
1132 *
1133 * Parameters: c Character to output
1134 *
1135 * Returns: (void)
1136 *
1137 * Notes: This function is used for single byte output to the log. It
1138 * primarily exists to maintain binary backward compatibility.
1139 */
1140 void
1141 log_putc(char c)
1142 {
1143 int unread_count = 0;
1144 LOG_LOCK();
1145 log_putc_locked(msgbufp, c);
1146 unread_count = msgbufp->msg_bufx - msgbufp->msg_bufr;
1147 LOG_UNLOCK();
1148
1149 if (unread_count < 0) {
1150 unread_count = 0 - unread_count;
1151 }
1152 if (c == '\n' || unread_count >= (msgbufp->msg_size / 2)) {
1153 logwakeup(msgbufp);
1154 }
1155 }
1156
1157
1158 /*
1159 * it is possible to increase the kernel log buffer size by adding
1160 * msgbuf=n
1161 * to the kernel command line, and to read the current size using
1162 * sysctl kern.msgbuf
1163 * If there is no parameter on the kernel command line, the buffer is
1164 * allocated statically and is CONFIG_MSG_BSIZE characters in size, otherwise
1165 * memory is dynamically allocated. Memory management must already be up.
1166 */
1167 int
1168 log_setsize(int size)
1169 {
1170 char *new_logdata;
1171 int new_logsize, new_bufr, new_bufx;
1172 char *old_logdata;
1173 int old_logsize, old_bufr, old_bufx;
1174 int i, count;
1175 char *p, ch;
1176
1177 if (size > MAX_MSG_BSIZE) {
1178 return EINVAL;
1179 }
1180
1181 if (size <= 0) {
1182 return EINVAL;
1183 }
1184
1185 new_logsize = size;
1186 if (!(new_logdata = (char*)kalloc(size))) {
1187 printf("log_setsize: unable to allocate memory\n");
1188 return ENOMEM;
1189 }
1190 bzero(new_logdata, new_logsize);
1191
1192 LOG_LOCK();
1193
1194 old_logsize = msgbufp->msg_size;
1195 old_logdata = msgbufp->msg_bufc;
1196 old_bufr = msgbufp->msg_bufr;
1197 old_bufx = msgbufp->msg_bufx;
1198
1199 LOG_SETSIZE_DEBUG("log_setsize(%d): old_logdata %p old_logsize %d old_bufr %d old_bufx %d\n",
1200 size, old_logdata, old_logsize, old_bufr, old_bufx);
1201
1202 /* start "new_logsize" bytes before the write pointer */
1203 if (new_logsize <= old_bufx) {
1204 count = new_logsize;
1205 p = old_logdata + old_bufx - count;
1206 } else {
1207 /*
1208 * if new buffer is bigger, copy what we have and let the
1209 * bzero above handle the difference
1210 */
1211 count = MIN(new_logsize, old_logsize);
1212 p = old_logdata + old_logsize - (count - old_bufx);
1213 }
1214 for (i = 0; i < count; i++) {
1215 if (p >= old_logdata + old_logsize) {
1216 p = old_logdata;
1217 }
1218
1219 ch = *p++;
1220 new_logdata[i] = ch;
1221 }
1222
1223 new_bufx = i;
1224 if (new_bufx >= new_logsize) {
1225 new_bufx = 0;
1226 }
1227 msgbufp->msg_bufx = new_bufx;
1228
1229 new_bufr = old_bufx - old_bufr; /* how much were we trailing bufx by? */
1230 if (new_bufr < 0) {
1231 new_bufr += old_logsize;
1232 }
1233 new_bufr = new_bufx - new_bufr; /* now relative to oldest data in new buffer */
1234 if (new_bufr < 0) {
1235 new_bufr += new_logsize;
1236 }
1237 msgbufp->msg_bufr = new_bufr;
1238
1239 msgbufp->msg_size = new_logsize;
1240 msgbufp->msg_bufc = new_logdata;
1241
1242 LOG_SETSIZE_DEBUG("log_setsize(%d): new_logdata %p new_logsize %d new_bufr %d new_bufx %d\n",
1243 size, new_logdata, new_logsize, new_bufr, new_bufx);
1244
1245 LOG_UNLOCK();
1246
1247 /* this memory is now dead - clear it so that it compresses better
1248 * in case of suspend to disk etc. */
1249 bzero(old_logdata, old_logsize);
1250 if (old_logdata != smsg_bufc) {
1251 /* dynamic memory that must be freed */
1252 kfree(old_logdata, old_logsize);
1253 }
1254
1255 printf("set system log size to %d bytes\n", new_logsize);
1256
1257 return 0;
1258 }
1259
1260 void
1261 oslog_setsize(int size)
1262 {
1263 uint16_t scale = 0;
1264 // If the size is less than the default stream buffer
1265 // do nothing
1266 if (size <= OSLOG_STREAM_BUF_SIZE) {
1267 return;
1268 }
1269
1270 scale = (uint16_t) (size / OSLOG_STREAM_BUF_SIZE);
1271
1272 oslog_stream_buf_size = size;
1273 oslog_stream_num_entries = scale * OSLOG_NUM_STREAM_ENTRIES;
1274 printf("oslog_setsize: new buffer size = %d, new num entries= %d\n", oslog_stream_buf_size, oslog_stream_num_entries);
1275 }
1276
1277 SYSCTL_PROC(_kern, OID_AUTO, msgbuf,
1278 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, 0, 0,
1279 sysctl_kern_msgbuf, "I", "");
1280
1281 static int
1282 sysctl_kern_msgbuf(struct sysctl_oid *oidp __unused,
1283 void *arg1 __unused, int arg2 __unused, struct sysctl_req *req)
1284 {
1285 int old_bufsize, bufsize;
1286 int error;
1287
1288 LOG_LOCK();
1289 old_bufsize = bufsize = msgbufp->msg_size;
1290 LOG_UNLOCK();
1291
1292 error = sysctl_io_number(req, bufsize, sizeof(bufsize), &bufsize, NULL);
1293 if (error) {
1294 return error;
1295 }
1296
1297 if (bufsize != old_bufsize) {
1298 error = log_setsize(bufsize);
1299 }
1300
1301 return error;
1302 }
1303
1304
1305 /*
1306 * This should be called by /sbin/dmesg only via libproc.
1307 * It returns as much data still in the buffer as possible.
1308 */
1309 int
1310 log_dmesg(user_addr_t buffer, uint32_t buffersize, int32_t * retval)
1311 {
1312 uint32_t i;
1313 uint32_t localbuff_size;
1314 int error = 0, newl, skip;
1315 char *localbuff, *p, *copystart, ch;
1316 size_t copysize;
1317
1318 LOG_LOCK();
1319 localbuff_size = (msgbufp->msg_size + 2); /* + '\n' + '\0' */
1320 LOG_UNLOCK();
1321
1322 /* Allocate a temporary non-circular buffer for copyout */
1323 if (!(localbuff = (char *)kalloc(localbuff_size))) {
1324 printf("log_dmesg: unable to allocate memory\n");
1325 return ENOMEM;
1326 }
1327
1328 /* in between here, the log could become bigger, but that's fine */
1329 LOG_LOCK();
1330
1331 /*
1332 * The message buffer is circular; start at the write pointer, and
1333 * make one loop up to write pointer - 1.
1334 */
1335 p = msgbufp->msg_bufc + msgbufp->msg_bufx;
1336 for (i = newl = skip = 0; p != msgbufp->msg_bufc + msgbufp->msg_bufx - 1; ++p) {
1337 if (p >= msgbufp->msg_bufc + msgbufp->msg_size) {
1338 p = msgbufp->msg_bufc;
1339 }
1340 ch = *p;
1341 /* Skip "\n<.*>" syslog sequences. */
1342 if (skip) {
1343 if (ch == '>') {
1344 newl = skip = 0;
1345 }
1346 continue;
1347 }
1348 if (newl && ch == '<') {
1349 skip = 1;
1350 continue;
1351 }
1352 if (ch == '\0') {
1353 continue;
1354 }
1355 newl = (ch == '\n');
1356 localbuff[i++] = ch;
1357 /* The original version of this routine contained a buffer
1358 * overflow. At the time, a "small" targeted fix was desired
1359 * so the change below to check the buffer bounds was made.
1360 * TODO: rewrite this needlessly convoluted routine.
1361 */
1362 if (i == (localbuff_size - 2)) {
1363 break;
1364 }
1365 }
1366 if (!newl) {
1367 localbuff[i++] = '\n';
1368 }
1369 localbuff[i++] = 0;
1370
1371 if (buffersize >= i) {
1372 copystart = localbuff;
1373 copysize = i;
1374 } else {
1375 copystart = localbuff + i - buffersize;
1376 copysize = buffersize;
1377 }
1378
1379 LOG_UNLOCK();
1380
1381 error = copyout(copystart, buffer, copysize);
1382 if (!error) {
1383 *retval = copysize;
1384 }
1385
1386 kfree(localbuff, localbuff_size);
1387 return error;
1388 }
1389
1390 #ifdef CONFIG_XNUPOST
1391
1392 uint32_t find_pattern_in_buffer(char * pattern, uint32_t len, int expected_count);
1393
1394 /*
1395 * returns count of pattern found in systemlog buffer.
1396 * stops searching further if count reaches expected_count.
1397 */
1398 uint32_t
1399 find_pattern_in_buffer(char * pattern, uint32_t len, int expected_count)
1400 {
1401 int match_count = 0;
1402 int i = 0;
1403 int j = 0;
1404 int no_match = 0;
1405 int pos = 0;
1406 char ch = 0;
1407
1408 if (pattern == NULL || len == 0 || expected_count == 0) {
1409 return 0;
1410 }
1411
1412 for (i = 0; i < msgbufp->msg_size; i++) {
1413 no_match = 0;
1414 for (j = 0; j < (int)len; j++) {
1415 pos = (msgbufp->msg_bufx + i + j) % msgbufp->msg_size;
1416 ch = msgbufp->msg_bufc[pos];
1417 if (ch != pattern[j]) {
1418 no_match = 1;
1419 break;
1420 }
1421 }
1422 if (no_match == 0) {
1423 match_count++;
1424 if (match_count >= expected_count) {
1425 break;
1426 }
1427 }
1428 }
1429 return match_count;
1430 }
1431
1432 #endif