Fix typo.
[dragonfly.git] / sys / kern / subr_prf.c
blobde4617ce811eaa1d3fac5cf17cffbd86728c95de
1 /*-
2 * Copyright (c) 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
38 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/subr_prf.c,v 1.61.2.5 2002/08/31 18:22:08 dwmalone Exp $
40 * $DragonFly: src/sys/kern/subr_prf.c,v 1.20 2008/01/04 12:16:19 matthias Exp $
43 #include "opt_ddb.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/msgbuf.h>
49 #include <sys/malloc.h>
50 #include <sys/proc.h>
51 #include <sys/tty.h>
52 #include <sys/tprintf.h>
53 #include <sys/stdint.h>
54 #include <sys/syslog.h>
55 #include <sys/cons.h>
56 #include <sys/uio.h>
57 #include <sys/sysctl.h>
58 #include <sys/lock.h>
59 #include <sys/ctype.h>
61 #ifdef DDB
62 #include <ddb/ddb.h>
63 #endif
66 * Note that stdarg.h and the ANSI style va_start macro is used for both
67 * ANSI and traditional C compilers. We use the __ machine version to stay
68 * within the kernel header file set.
70 #include <machine/stdarg.h>
72 #define TOCONS 0x01
73 #define TOTTY 0x02
74 #define TOLOG 0x04
76 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
77 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
79 struct putchar_arg {
80 int flags;
81 int pri;
82 struct tty *tty;
85 struct snprintf_arg {
86 char *str;
87 size_t remain;
90 extern int log_open;
92 struct tty *constty; /* pointer to console "window" tty */
94 static void (*v_putc)(int) = cnputc; /* routine to putc on virtual console */
95 static void msglogchar(int c, int pri);
96 static void msgaddchar(int c, void *dummy);
97 static void kputchar (int ch, void *arg);
98 static char *ksprintn (char *nbuf, uintmax_t num, int base, int *lenp,
99 int upper);
100 static void snprintf_func (int ch, void *arg);
102 static int consintr = 1; /* Ok to handle console interrupts? */
103 static int msgbufmapped; /* Set when safe to use msgbuf */
104 int msgbuftrigger;
106 static int log_console_output = 1;
107 TUNABLE_INT("kern.log_console_output", &log_console_output);
108 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
109 &log_console_output, 0, "");
111 static int unprivileged_read_msgbuf = 1;
112 SYSCTL_INT(_security, OID_AUTO, unprivileged_read_msgbuf, CTLFLAG_RW,
113 &unprivileged_read_msgbuf, 0,
114 "Unprivileged processes may read the kernel message buffer");
117 * Warn that a system table is full.
119 void
120 tablefull(const char *tab)
123 log(LOG_ERR, "%s: table is full\n", tab);
127 * Uprintf prints to the controlling terminal for the current process.
130 uprintf(const char *fmt, ...)
132 struct proc *p = curproc;
133 __va_list ap;
134 struct putchar_arg pca;
135 int retval = 0;
137 if (p && p->p_flag & P_CONTROLT &&
138 p->p_session->s_ttyvp) {
139 __va_start(ap, fmt);
140 pca.tty = p->p_session->s_ttyp;
141 pca.flags = TOTTY;
143 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
144 __va_end(ap);
146 return (retval);
149 tpr_t
150 tprintf_open(struct proc *p)
153 if ((p->p_flag & P_CONTROLT) && p->p_session->s_ttyvp) {
154 sess_hold(p->p_session);
155 return ((tpr_t) p->p_session);
157 return ((tpr_t) NULL);
160 void
161 tprintf_close(tpr_t sess)
163 if (sess)
164 sess_rele((struct session *) sess);
168 * tprintf prints on the controlling terminal associated
169 * with the given session.
172 tprintf(tpr_t tpr, const char *fmt, ...)
174 struct session *sess = (struct session *)tpr;
175 struct tty *tp = NULL;
176 int flags = TOLOG;
177 __va_list ap;
178 struct putchar_arg pca;
179 int retval;
181 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
182 flags |= TOTTY;
183 tp = sess->s_ttyp;
185 __va_start(ap, fmt);
186 pca.tty = tp;
187 pca.flags = flags;
188 pca.pri = LOG_INFO;
189 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
190 __va_end(ap);
191 msgbuftrigger = 1;
192 return (retval);
196 * Ttyprintf displays a message on a tty; it should be used only by
197 * the tty driver, or anything that knows the underlying tty will not
198 * be revoke(2)'d away. Other callers should use tprintf.
201 ttyprintf(struct tty *tp, const char *fmt, ...)
203 __va_list ap;
204 struct putchar_arg pca;
205 int retval;
207 __va_start(ap, fmt);
208 pca.tty = tp;
209 pca.flags = TOTTY;
210 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
211 __va_end(ap);
212 return (retval);
216 * Log writes to the log buffer, and guarantees not to sleep (so can be
217 * called by interrupt routines). If there is no process reading the
218 * log yet, it writes to the console also.
221 log(int level, const char *fmt, ...)
223 __va_list ap;
224 int retval;
225 struct putchar_arg pca;
227 pca.tty = NULL;
228 pca.pri = level;
229 pca.flags = log_open ? TOLOG : TOCONS;
231 __va_start(ap, fmt);
232 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
233 __va_end(ap);
235 msgbuftrigger = 1;
236 return (retval);
239 #define CONSCHUNK 128
241 void
242 log_console(struct uio *uio)
244 int c, i, error, iovlen, nl;
245 struct uio muio;
246 struct iovec *miov = NULL;
247 char *consbuffer;
248 int pri;
250 if (!log_console_output)
251 return;
253 pri = LOG_INFO | LOG_CONSOLE;
254 muio = *uio;
255 iovlen = uio->uio_iovcnt * sizeof (struct iovec);
256 MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
257 MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
258 bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen);
259 muio.uio_iov = miov;
260 uio = &muio;
262 nl = 0;
263 while (uio->uio_resid > 0) {
264 c = imin(uio->uio_resid, CONSCHUNK);
265 error = uiomove(consbuffer, c, uio);
266 if (error != 0)
267 break;
268 for (i = 0; i < c; i++) {
269 msglogchar(consbuffer[i], pri);
270 if (consbuffer[i] == '\n')
271 nl = 1;
272 else
273 nl = 0;
276 if (!nl)
277 msglogchar('\n', pri);
278 msgbuftrigger = 1;
279 FREE(miov, M_TEMP);
280 FREE(consbuffer, M_TEMP);
281 return;
285 * Output to the console.
287 * NOT YET ENTIRELY MPSAFE
290 kprintf(const char *fmt, ...)
292 __va_list ap;
293 int savintr;
294 struct putchar_arg pca;
295 int retval;
297 savintr = consintr; /* disable interrupts */
298 consintr = 0;
299 __va_start(ap, fmt);
300 pca.tty = NULL;
301 pca.flags = TOCONS | TOLOG;
302 pca.pri = -1;
303 cons_lock();
304 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
305 cons_unlock();
306 __va_end(ap);
307 if (!panicstr)
308 msgbuftrigger = 1;
309 consintr = savintr; /* reenable interrupts */
310 return (retval);
314 kvprintf(const char *fmt, __va_list ap)
316 int savintr;
317 struct putchar_arg pca;
318 int retval;
320 savintr = consintr; /* disable interrupts */
321 consintr = 0;
322 pca.tty = NULL;
323 pca.flags = TOCONS | TOLOG;
324 pca.pri = -1;
325 cons_lock();
326 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
327 cons_unlock();
328 if (!panicstr)
329 msgbuftrigger = 1;
330 consintr = savintr; /* reenable interrupts */
331 return (retval);
335 * Limited rate kprintf. The passed rate structure must be initialized
336 * with the desired reporting frequency. A frequency of 0 will result in
337 * no output.
339 void
340 krateprintf(struct krate *rate, const char *fmt, ...)
342 __va_list ap;
344 if (rate->ticks != (int)time_second) {
345 rate->ticks = (int)time_second;
346 rate->count = 0;
348 if (rate->count < rate->freq) {
349 ++rate->count;
350 __va_start(ap, fmt);
351 kvprintf(fmt, ap);
352 __va_end(ap);
357 * Print a character on console or users terminal. If destination is
358 * the console then the last bunch of characters are saved in msgbuf for
359 * inspection later.
361 * NOT YET ENTIRELY MPSAFE, EVEN WHEN LOGGING JUST TO THE SYSCONSOLE.
363 static void
364 kputchar(int c, void *arg)
366 struct putchar_arg *ap = (struct putchar_arg*) arg;
367 int flags = ap->flags;
368 struct tty *tp = ap->tty;
369 if (panicstr)
370 constty = NULL;
371 if ((flags & TOCONS) && tp == NULL && constty) {
372 tp = constty;
373 flags |= TOTTY;
375 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
376 (flags & TOCONS) && tp == constty)
377 constty = NULL;
378 if ((flags & TOLOG))
379 msglogchar(c, ap->pri);
380 if ((flags & TOCONS) && constty == NULL && c != '\0')
381 (*v_putc)(c);
385 * Scaled down version of sprintf(3).
388 ksprintf(char *buf, const char *cfmt, ...)
390 int retval;
391 __va_list ap;
393 __va_start(ap, cfmt);
394 retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap);
395 buf[retval] = '\0';
396 __va_end(ap);
397 return (retval);
401 * Scaled down version of vsprintf(3).
404 kvsprintf(char *buf, const char *cfmt, __va_list ap)
406 int retval;
408 retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap);
409 buf[retval] = '\0';
410 return (retval);
414 * Scaled down version of snprintf(3).
417 ksnprintf(char *str, size_t size, const char *format, ...)
419 int retval;
420 __va_list ap;
422 __va_start(ap, format);
423 retval = kvsnprintf(str, size, format, ap);
424 __va_end(ap);
425 return(retval);
429 * Scaled down version of vsnprintf(3).
432 kvsnprintf(char *str, size_t size, const char *format, __va_list ap)
434 struct snprintf_arg info;
435 int retval;
437 info.str = str;
438 info.remain = size;
439 retval = kvcprintf(format, snprintf_func, &info, 10, ap);
440 if (info.remain >= 1)
441 *info.str++ = '\0';
442 return (retval);
445 static void
446 snprintf_func(int ch, void *arg)
448 struct snprintf_arg *const info = arg;
450 if (info->remain >= 2) {
451 *info->str++ = ch;
452 info->remain--;
457 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
458 * order; return an optional length and a pointer to the last character
459 * written in the buffer (i.e., the first character of the string).
460 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
462 static char *
463 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
465 char *p, c;
467 p = nbuf;
468 *p = '\0';
469 do {
470 c = hex2ascii(num % base);
471 *++p = upper ? toupper(c) : c;
472 } while (num /= base);
473 if (lenp)
474 *lenp = p - nbuf;
475 return (p);
479 * Scaled down version of printf(3).
481 * Two additional formats:
483 * The format %b is supported to decode error registers.
484 * Its usage is:
486 * kprintf("reg=%b\n", regval, "<base><arg>*");
488 * where <base> is the output base expressed as a control character, e.g.
489 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
490 * the first of which gives the bit number to be inspected (origin 1), and
491 * the next characters (up to a control character, i.e. a character <= 32),
492 * give the name of the register. Thus:
494 * kvcprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
496 * would produce output:
498 * reg=3<BITTWO,BITONE>
500 * XXX: %D -- Hexdump, takes pointer and separator string:
501 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
502 * ("%*D", len, ptr, " " -> XX XX XX XX ...
505 kvcprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, __va_list ap)
507 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
508 char nbuf[MAXNBUF];
509 char *d;
510 const char *p, *percent, *q;
511 u_char *up;
512 int ch, n;
513 uintmax_t num;
514 int base, tmp, width, ladjust, sharpflag, neg, sign, dot;
515 int jflag, lflag, qflag, tflag;
516 int dwidth, upper;
517 char padc;
518 int retval = 0, stop = 0;
520 num = 0;
521 if (!func)
522 d = (char *) arg;
523 else
524 d = NULL;
526 if (fmt == NULL)
527 fmt = "(fmt null)\n";
529 if (radix < 2 || radix > 36)
530 radix = 10;
532 for (;;) {
533 padc = ' ';
534 width = 0;
535 while ((ch = (u_char)*fmt++) != '%' || stop) {
536 if (ch == '\0')
537 return (retval);
538 PCHAR(ch);
540 percent = fmt - 1;
541 dot = dwidth = ladjust = neg = sharpflag = sign = upper = 0;
542 jflag = lflag = qflag = tflag = 0;
544 reswitch:
545 switch (ch = (u_char)*fmt++) {
546 case '.':
547 dot = 1;
548 goto reswitch;
549 case '#':
550 sharpflag = 1;
551 goto reswitch;
552 case '+':
553 sign = 1;
554 goto reswitch;
555 case '-':
556 ladjust = 1;
557 goto reswitch;
558 case '%':
559 PCHAR(ch);
560 break;
561 case '*':
562 if (!dot) {
563 width = __va_arg(ap, int);
564 if (width < 0) {
565 ladjust = !ladjust;
566 width = -width;
568 } else {
569 dwidth = __va_arg(ap, int);
571 goto reswitch;
572 case '0':
573 if (!dot) {
574 padc = '0';
575 goto reswitch;
577 case '1': case '2': case '3': case '4':
578 case '5': case '6': case '7': case '8': case '9':
579 for (n = 0;; ++fmt) {
580 n = n * 10 + ch - '0';
581 ch = *fmt;
582 if (ch < '0' || ch > '9')
583 break;
585 if (dot)
586 dwidth = n;
587 else
588 width = n;
589 goto reswitch;
590 case 'b':
591 num = (u_int)__va_arg(ap, int);
592 p = __va_arg(ap, char *);
593 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
594 PCHAR(*q--);
596 if (num == 0)
597 break;
599 for (tmp = 0; *p;) {
600 n = *p++;
601 if (num & (1 << (n - 1))) {
602 PCHAR(tmp ? ',' : '<');
603 for (; (n = *p) > ' '; ++p)
604 PCHAR(n);
605 tmp = 1;
606 } else
607 for (; *p > ' '; ++p)
608 continue;
610 if (tmp)
611 PCHAR('>');
612 break;
613 case 'c':
614 PCHAR(__va_arg(ap, int));
615 break;
616 case 'D':
617 up = __va_arg(ap, u_char *);
618 p = __va_arg(ap, char *);
619 if (!width)
620 width = 16;
621 while(width--) {
622 PCHAR(hex2ascii(*up >> 4));
623 PCHAR(hex2ascii(*up & 0x0f));
624 up++;
625 if (width)
626 for (q=p;*q;q++)
627 PCHAR(*q);
629 break;
630 case 'd':
631 case 'i':
632 base = 10;
633 sign = 1;
634 goto handle_sign;
635 case 'j':
636 jflag = 1;
637 goto reswitch;
638 case 'l':
639 if (lflag) {
640 lflag = 0;
641 qflag = 1;
642 } else
643 lflag = 1;
644 goto reswitch;
645 case 'n':
646 if (jflag)
647 *(__va_arg(ap, intmax_t *)) = retval;
648 else if (lflag)
649 *(__va_arg(ap, long *)) = retval;
650 else if (qflag)
651 *(__va_arg(ap, quad_t *)) = retval;
652 else
653 *(__va_arg(ap, int *)) = retval;
654 break;
655 case 'o':
656 base = 8;
657 goto handle_nosign;
658 case 'p':
659 base = 16;
660 sharpflag = (width == 0);
661 sign = 0;
662 num = (uintptr_t)__va_arg(ap, void *);
663 goto number;
664 case 'q':
665 qflag = 1;
666 goto reswitch;
667 case 'r':
668 base = radix;
669 if (sign)
670 goto handle_sign;
671 goto handle_nosign;
672 case 's':
673 p = __va_arg(ap, char *);
674 if (p == NULL)
675 p = "(null)";
676 if (!dot)
677 n = strlen (p);
678 else
679 for (n = 0; n < dwidth && p[n]; n++)
680 continue;
682 width -= n;
684 if (!ladjust && width > 0)
685 while (width--)
686 PCHAR(padc);
687 while (n--)
688 PCHAR(*p++);
689 if (ladjust && width > 0)
690 while (width--)
691 PCHAR(padc);
692 break;
693 case 't':
694 tflag = 1;
695 goto reswitch;
696 case 'u':
697 base = 10;
698 goto handle_nosign;
699 case 'X':
700 upper = 1;
701 /* FALLTHROUGH */
702 case 'x':
703 base = 16;
704 goto handle_nosign;
705 case 'z':
706 base = 16;
707 sign = 1;
708 goto handle_sign;
709 handle_nosign:
710 sign = 0;
711 if (jflag)
712 num = __va_arg(ap, uintmax_t);
713 else if (lflag)
714 num = __va_arg(ap, u_long);
715 else if (qflag)
716 num = __va_arg(ap, u_quad_t);
717 else if (tflag)
718 num = __va_arg(ap, ptrdiff_t);
719 else
720 num = __va_arg(ap, u_int);
721 goto number;
722 handle_sign:
723 if (jflag)
724 num = __va_arg(ap, intmax_t);
725 else if (lflag)
726 num = __va_arg(ap, long);
727 else if (qflag)
728 num = __va_arg(ap, quad_t);
729 else if (tflag)
730 num = __va_arg(ap, ptrdiff_t);
731 else
732 num = __va_arg(ap, int);
733 number:
734 if (sign && (intmax_t)num < 0) {
735 neg = 1;
736 num = -(intmax_t)num;
738 p = ksprintn(nbuf, num, base, &tmp, upper);
739 if (sharpflag && num != 0) {
740 if (base == 8)
741 tmp++;
742 else if (base == 16)
743 tmp += 2;
745 if (neg)
746 tmp++;
748 if (!ladjust && padc != '0' && width &&
749 (width -= tmp) > 0) {
750 while (width--)
751 PCHAR(padc);
753 if (neg)
754 PCHAR('-');
755 if (sharpflag && num != 0) {
756 if (base == 8) {
757 PCHAR('0');
758 } else if (base == 16) {
759 PCHAR('0');
760 PCHAR('x');
763 if (!ladjust && width && (width -= tmp) > 0)
764 while (width--)
765 PCHAR(padc);
767 while (*p)
768 PCHAR(*p--);
770 if (ladjust && width && (width -= tmp) > 0)
771 while (width--)
772 PCHAR(padc);
774 break;
775 default:
776 while (percent < fmt)
777 PCHAR(*percent++);
779 * Since we ignore an formatting argument it is no
780 * longer safe to obey the remaining formatting
781 * arguments as the arguments will no longer match
782 * the format specs.
784 stop = 1;
785 break;
788 #undef PCHAR
792 * Put character in log buffer with a particular priority.
794 * MPSAFE
796 static void
797 msglogchar(int c, int pri)
799 static int lastpri = -1;
800 static int dangling;
801 char nbuf[MAXNBUF];
802 char *p;
804 if (!msgbufmapped)
805 return;
806 if (c == '\0' || c == '\r')
807 return;
808 if (pri != -1 && pri != lastpri) {
809 if (dangling) {
810 msgaddchar('\n', NULL);
811 dangling = 0;
813 msgaddchar('<', NULL);
814 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
815 msgaddchar(*p--, NULL);
816 msgaddchar('>', NULL);
817 lastpri = pri;
819 msgaddchar(c, NULL);
820 if (c == '\n') {
821 dangling = 0;
822 lastpri = -1;
823 } else {
824 dangling = 1;
829 * Put char in log buffer. Make sure nothing blows up beyond repair if
830 * we have an MP race.
832 * MPSAFE.
834 static void
835 msgaddchar(int c, void *dummy)
837 struct msgbuf *mbp;
838 int rindex;
839 int windex;
841 if (!msgbufmapped)
842 return;
843 mbp = msgbufp;
844 windex = mbp->msg_bufx;
845 mbp->msg_ptr[windex] = c;
846 if (++windex >= mbp->msg_size)
847 windex = 0;
848 rindex = mbp->msg_bufr;
849 if (windex == rindex) {
850 rindex += 32;
851 if (rindex >= mbp->msg_size)
852 rindex -= mbp->msg_size;
853 mbp->msg_bufr = rindex;
855 mbp->msg_bufx = windex;
858 static void
859 msgbufcopy(struct msgbuf *oldp)
861 int pos;
863 pos = oldp->msg_bufr;
864 while (pos != oldp->msg_bufx) {
865 msglogchar(oldp->msg_ptr[pos], -1);
866 if (++pos >= oldp->msg_size)
867 pos = 0;
871 void
872 msgbufinit(void *ptr, size_t size)
874 char *cp;
875 static struct msgbuf *oldp = NULL;
877 size -= sizeof(*msgbufp);
878 cp = (char *)ptr;
879 msgbufp = (struct msgbuf *) (cp + size);
880 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
881 msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) {
882 bzero(cp, size);
883 bzero(msgbufp, sizeof(*msgbufp));
884 msgbufp->msg_magic = MSG_MAGIC;
885 msgbufp->msg_size = (char *)msgbufp - cp;
887 msgbufp->msg_ptr = cp;
888 if (msgbufmapped && oldp != msgbufp)
889 msgbufcopy(oldp);
890 msgbufmapped = 1;
891 oldp = msgbufp;
894 /* Sysctls for accessing/clearing the msgbuf */
896 static int
897 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
899 struct ucred *cred;
900 int error;
903 * Only wheel or root can access the message log.
905 if (unprivileged_read_msgbuf == 0) {
906 KKASSERT(req->td->td_proc);
907 cred = req->td->td_proc->p_ucred;
909 if ((cred->cr_prison || groupmember(0, cred) == 0) &&
910 suser(req->td) != 0
912 return (EPERM);
917 * Unwind the buffer, so that it's linear (possibly starting with
918 * some initial nulls).
920 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
921 msgbufp->msg_size - msgbufp->msg_bufx, req);
922 if (error)
923 return (error);
924 if (msgbufp->msg_bufx > 0) {
925 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
926 msgbufp->msg_bufx, req);
928 return (error);
931 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
932 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
934 static int msgbuf_clear;
936 static int
937 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
939 int error;
940 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
941 if (!error && req->newptr) {
942 /* Clear the buffer and reset write pointer */
943 bzero(msgbufp->msg_ptr, msgbufp->msg_size);
944 msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
945 msgbuf_clear = 0;
947 return (error);
950 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
951 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
952 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
954 #ifdef DDB
956 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
958 int i, j;
960 if (!msgbufmapped) {
961 db_printf("msgbuf not mapped yet\n");
962 return;
964 db_printf("msgbufp = %p\n", msgbufp);
965 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
966 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
967 msgbufp->msg_bufx, msgbufp->msg_ptr);
968 for (i = 0; i < msgbufp->msg_size; i++) {
969 j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
970 db_printf("%c", msgbufp->msg_ptr[j]);
972 db_printf("\n");
975 #endif /* DDB */