proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / kern / subr_prf.c
CommitLineData
984263bc
MD
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.
9 *
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.
25 *
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.
37 *
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 $
dadab5e9 40 * $DragonFly: src/sys/kern/subr_prf.c,v 1.3 2003/06/25 03:55:57 dillon Exp $
984263bc
MD
41 */
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/msgbuf.h>
47#include <sys/malloc.h>
48#include <sys/proc.h>
49#include <sys/tty.h>
50#include <sys/tprintf.h>
51#include <sys/syslog.h>
52#include <sys/cons.h>
53#include <sys/uio.h>
54#include <sys/sysctl.h>
55
56/*
57 * Note that stdarg.h and the ANSI style va_start macro is used for both
58 * ANSI and traditional C compilers.
59 */
60#include <machine/stdarg.h>
61
62#define TOCONS 0x01
63#define TOTTY 0x02
64#define TOLOG 0x04
65
66/* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
67#define MAXNBUF (sizeof(quad_t) * NBBY + 1)
68
69struct putchar_arg {
70 int flags;
71 int pri;
72 struct tty *tty;
73};
74
75struct snprintf_arg {
76 char *str;
77 size_t remain;
78};
79
80extern int log_open;
81
82struct tty *constty; /* pointer to console "window" tty */
83
84static void (*v_putc)(int) = cnputc; /* routine to putc on virtual console */
85static void msglogchar(int c, int pri);
86static void msgaddchar(int c, void *dummy);
87static void putchar __P((int ch, void *arg));
88static char *ksprintn __P((char *nbuf, u_long num, int base, int *len));
89static char *ksprintqn __P((char *nbuf, u_quad_t num, int base, int *len));
90static void snprintf_func __P((int ch, void *arg));
91
92static int consintr = 1; /* Ok to handle console interrupts? */
93static int msgbufmapped; /* Set when safe to use msgbuf */
94int msgbuftrigger;
95
96static int log_console_output = 1;
97TUNABLE_INT("kern.log_console_output", &log_console_output);
98SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
99 &log_console_output, 0, "");
100
101/*
102 * Warn that a system table is full.
103 */
104void
105tablefull(const char *tab)
106{
107
108 log(LOG_ERR, "%s: table is full\n", tab);
109}
110
111/*
112 * Uprintf prints to the controlling terminal for the current process.
113 * It may block if the tty queue is overfull. No message is printed if
114 * the queue does not clear in a reasonable time.
115 */
116int
117uprintf(const char *fmt, ...)
118{
119 struct proc *p = curproc;
120 va_list ap;
121 struct putchar_arg pca;
122 int retval = 0;
123
124 if (p && p->p_flag & P_CONTROLT &&
125 p->p_session->s_ttyvp) {
126 va_start(ap, fmt);
127 pca.tty = p->p_session->s_ttyp;
128 pca.flags = TOTTY;
129 retval = kvprintf(fmt, putchar, &pca, 10, ap);
130 va_end(ap);
131 }
132 return retval;
133}
134
135tpr_t
dadab5e9 136tprintf_open(struct proc *p)
984263bc
MD
137{
138
dadab5e9 139 if ((p->p_flag & P_CONTROLT) && p->p_session->s_ttyvp) {
984263bc
MD
140 SESSHOLD(p->p_session);
141 return ((tpr_t) p->p_session);
142 }
143 return ((tpr_t) NULL);
144}
145
146void
dadab5e9 147tprintf_close(tpr_t sess)
984263bc 148{
984263bc
MD
149 if (sess)
150 SESSRELE((struct session *) sess);
151}
152
153/*
154 * tprintf prints on the controlling terminal associated
155 * with the given session.
156 */
157int
158tprintf(tpr_t tpr, const char *fmt, ...)
159{
160 register struct session *sess = (struct session *)tpr;
161 struct tty *tp = NULL;
162 int flags = TOLOG;
163 va_list ap;
164 struct putchar_arg pca;
165 int retval;
166
167 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
168 flags |= TOTTY;
169 tp = sess->s_ttyp;
170 }
171 va_start(ap, fmt);
172 pca.tty = tp;
173 pca.flags = flags;
174 pca.pri = LOG_INFO;
175 retval = kvprintf(fmt, putchar, &pca, 10, ap);
176 va_end(ap);
177 msgbuftrigger = 1;
178 return retval;
179}
180
181/*
182 * Ttyprintf displays a message on a tty; it should be used only by
183 * the tty driver, or anything that knows the underlying tty will not
184 * be revoke(2)'d away. Other callers should use tprintf.
185 */
186int
187ttyprintf(struct tty *tp, const char *fmt, ...)
188{
189 va_list ap;
190 struct putchar_arg pca;
191 int retval;
192
193 va_start(ap, fmt);
194 pca.tty = tp;
195 pca.flags = TOTTY;
196 retval = kvprintf(fmt, putchar, &pca, 10, ap);
197 va_end(ap);
198 return retval;
199}
200
201/*
202 * Log writes to the log buffer, and guarantees not to sleep (so can be
203 * called by interrupt routines). If there is no process reading the
204 * log yet, it writes to the console also.
205 */
206int
207log(int level, const char *fmt, ...)
208{
209 va_list ap;
210 int retval;
211 struct putchar_arg pca;
212
213 pca.tty = NULL;
214 pca.pri = level;
215 pca.flags = log_open ? TOLOG : TOCONS;
216
217 va_start(ap, fmt);
218 retval = kvprintf(fmt, putchar, &pca, 10, ap);
219 va_end(ap);
220
221 msgbuftrigger = 1;
222 return (retval);
223}
224
225int
226addlog(const char *fmt, ...)
227{
228 va_list ap;
229 int retval;
230 struct putchar_arg pca;
231
232 pca.tty = NULL;
233 pca.pri = -1;
234 pca.flags = log_open ? TOLOG : TOCONS;
235
236 va_start(ap, fmt);
237 retval = kvprintf(fmt, putchar, &pca, 10, ap);
238 va_end(ap);
239
240 msgbuftrigger = 1;
241 return (retval);
242}
243
244#define CONSCHUNK 128
245
246void
247log_console(struct uio *uio)
248{
249 int c, i, error, iovlen, nl;
250 struct uio muio;
251 struct iovec *miov = NULL;
252 char *consbuffer;
253 int pri;
254
255 if (!log_console_output)
256 return;
257
258 pri = LOG_INFO | LOG_CONSOLE;
259 muio = *uio;
260 iovlen = uio->uio_iovcnt * sizeof (struct iovec);
261 MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
262 MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
263 bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen);
264 muio.uio_iov = miov;
265 uio = &muio;
266
267 nl = 0;
268 while (uio->uio_resid > 0) {
269 c = imin(uio->uio_resid, CONSCHUNK);
270 error = uiomove(consbuffer, c, uio);
271 if (error != 0)
272 return;
273 for (i = 0; i < c; i++) {
274 msglogchar(consbuffer[i], pri);
275 if (consbuffer[i] == '\n')
276 nl = 1;
277 else
278 nl = 0;
279 }
280 }
281 if (!nl)
282 msglogchar('\n', pri);
283 msgbuftrigger = 1;
284 FREE(miov, M_TEMP);
285 FREE(consbuffer, M_TEMP);
286 return;
287}
288
289int
290printf(const char *fmt, ...)
291{
292 va_list ap;
293 int savintr;
294 struct putchar_arg pca;
295 int retval;
296
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 retval = kvprintf(fmt, putchar, &pca, 10, ap);
304 va_end(ap);
305 if (!panicstr)
306 msgbuftrigger = 1;
307 consintr = savintr; /* reenable interrupts */
308 return retval;
309}
310
311int
312vprintf(const char *fmt, va_list ap)
313{
314 int savintr;
315 struct putchar_arg pca;
316 int retval;
317
318 savintr = consintr; /* disable interrupts */
319 consintr = 0;
320 pca.tty = NULL;
321 pca.flags = TOCONS | TOLOG;
322 pca.pri = -1;
323 retval = kvprintf(fmt, putchar, &pca, 10, ap);
324 if (!panicstr)
325 msgbuftrigger = 1;
326 consintr = savintr; /* reenable interrupts */
327 return retval;
328}
329
330/*
331 * Print a character on console or users terminal. If destination is
332 * the console then the last bunch of characters are saved in msgbuf for
333 * inspection later.
334 */
335static void
336putchar(int c, void *arg)
337{
338 struct putchar_arg *ap = (struct putchar_arg*) arg;
339 int flags = ap->flags;
340 struct tty *tp = ap->tty;
341 if (panicstr)
342 constty = NULL;
343 if ((flags & TOCONS) && tp == NULL && constty) {
344 tp = constty;
345 flags |= TOTTY;
346 }
347 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
348 (flags & TOCONS) && tp == constty)
349 constty = NULL;
350 if ((flags & TOLOG))
351 msglogchar(c, ap->pri);
352 if ((flags & TOCONS) && constty == NULL && c != '\0')
353 (*v_putc)(c);
354}
355
356/*
357 * Scaled down version of sprintf(3).
358 */
359int
360sprintf(char *buf, const char *cfmt, ...)
361{
362 int retval;
363 va_list ap;
364
365 va_start(ap, cfmt);
366 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
367 buf[retval] = '\0';
368 va_end(ap);
369 return retval;
370}
371
372/*
373 * Scaled down version of vsprintf(3).
374 */
375int
376vsprintf(char *buf, const char *cfmt, va_list ap)
377{
378 int retval;
379
380 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
381 buf[retval] = '\0';
382 return retval;
383}
384
385/*
386 * Scaled down version of snprintf(3).
387 */
388int
389snprintf(char *str, size_t size, const char *format, ...)
390{
391 int retval;
392 va_list ap;
393
394 va_start(ap, format);
395 retval = vsnprintf(str, size, format, ap);
396 va_end(ap);
397 return(retval);
398}
399
400/*
401 * Scaled down version of vsnprintf(3).
402 */
403int
404vsnprintf(char *str, size_t size, const char *format, va_list ap)
405{
406 struct snprintf_arg info;
407 int retval;
408
409 info.str = str;
410 info.remain = size;
411 retval = kvprintf(format, snprintf_func, &info, 10, ap);
412 if (info.remain >= 1)
413 *info.str++ = '\0';
414 return retval;
415}
416
417static void
418snprintf_func(int ch, void *arg)
419{
420 struct snprintf_arg *const info = arg;
421
422 if (info->remain >= 2) {
423 *info->str++ = ch;
424 info->remain--;
425 }
426}
427
428/*
429 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
430 * order; return an optional length and a pointer to the last character
431 * written in the buffer (i.e., the first character of the string).
432 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
433 */
434static char *
435ksprintn(nbuf, ul, base, lenp)
436 char *nbuf;
437 u_long ul;
438 int base, *lenp;
439{
440 char *p;
441
442 p = nbuf;
443 *p = '\0';
444 do {
445 *++p = hex2ascii(ul % base);
446 } while (ul /= base);
447 if (lenp)
448 *lenp = p - nbuf;
449 return (p);
450}
451/* ksprintn, but for a quad_t. */
452static char *
453ksprintqn(nbuf, uq, base, lenp)
454 char *nbuf;
455 u_quad_t uq;
456 int base, *lenp;
457{
458 char *p;
459
460 p = nbuf;
461 *p = '\0';
462 do {
463 *++p = hex2ascii(uq % base);
464 } while (uq /= base);
465 if (lenp)
466 *lenp = p - nbuf;
467 return (p);
468}
469
470/*
471 * Scaled down version of printf(3).
472 *
473 * Two additional formats:
474 *
475 * The format %b is supported to decode error registers.
476 * Its usage is:
477 *
478 * printf("reg=%b\n", regval, "<base><arg>*");
479 *
480 * where <base> is the output base expressed as a control character, e.g.
481 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
482 * the first of which gives the bit number to be inspected (origin 1), and
483 * the next characters (up to a control character, i.e. a character <= 32),
484 * give the name of the register. Thus:
485 *
486 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
487 *
488 * would produce output:
489 *
490 * reg=3<BITTWO,BITONE>
491 *
492 * XXX: %D -- Hexdump, takes pointer and separator string:
493 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
494 * ("%*D", len, ptr, " " -> XX XX XX XX ...
495 */
496int
497kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
498{
499#define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
500 char nbuf[MAXNBUF];
501 char *p, *q, *d;
502 u_char *up;
503 int ch, n;
504 u_long ul;
505 u_quad_t uq;
506 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
507 int dwidth;
508 char padc;
509 int retval = 0;
510
511 ul = 0;
512 uq = 0;
513 if (!func)
514 d = (char *) arg;
515 else
516 d = NULL;
517
518 if (fmt == NULL)
519 fmt = "(fmt null)\n";
520
521 if (radix < 2 || radix > 36)
522 radix = 10;
523
524 for (;;) {
525 padc = ' ';
526 width = 0;
527 while ((ch = (u_char)*fmt++) != '%') {
528 if (ch == '\0')
529 return retval;
530 PCHAR(ch);
531 }
532 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
533 sign = 0; dot = 0; dwidth = 0;
534reswitch: switch (ch = (u_char)*fmt++) {
535 case '.':
536 dot = 1;
537 goto reswitch;
538 case '#':
539 sharpflag = 1;
540 goto reswitch;
541 case '+':
542 sign = 1;
543 goto reswitch;
544 case '-':
545 ladjust = 1;
546 goto reswitch;
547 case '%':
548 PCHAR(ch);
549 break;
550 case '*':
551 if (!dot) {
552 width = va_arg(ap, int);
553 if (width < 0) {
554 ladjust = !ladjust;
555 width = -width;
556 }
557 } else {
558 dwidth = va_arg(ap, int);
559 }
560 goto reswitch;
561 case '0':
562 if (!dot) {
563 padc = '0';
564 goto reswitch;
565 }
566 case '1': case '2': case '3': case '4':
567 case '5': case '6': case '7': case '8': case '9':
568 for (n = 0;; ++fmt) {
569 n = n * 10 + ch - '0';
570 ch = *fmt;
571 if (ch < '0' || ch > '9')
572 break;
573 }
574 if (dot)
575 dwidth = n;
576 else
577 width = n;
578 goto reswitch;
579 case 'b':
580 ul = va_arg(ap, int);
581 p = va_arg(ap, char *);
582 for (q = ksprintn(nbuf, ul, *p++, NULL); *q;)
583 PCHAR(*q--);
584
585 if (!ul)
586 break;
587
588 for (tmp = 0; *p;) {
589 n = *p++;
590 if (ul & (1 << (n - 1))) {
591 PCHAR(tmp ? ',' : '<');
592 for (; (n = *p) > ' '; ++p)
593 PCHAR(n);
594 tmp = 1;
595 } else
596 for (; *p > ' '; ++p)
597 continue;
598 }
599 if (tmp)
600 PCHAR('>');
601 break;
602 case 'c':
603 PCHAR(va_arg(ap, int));
604 break;
605 case 'D':
606 up = va_arg(ap, u_char *);
607 p = va_arg(ap, char *);
608 if (!width)
609 width = 16;
610 while(width--) {
611 PCHAR(hex2ascii(*up >> 4));
612 PCHAR(hex2ascii(*up & 0x0f));
613 up++;
614 if (width)
615 for (q=p;*q;q++)
616 PCHAR(*q);
617 }
618 break;
619 case 'd':
620 if (qflag)
621 uq = va_arg(ap, quad_t);
622 else if (lflag)
623 ul = va_arg(ap, long);
624 else
625 ul = va_arg(ap, int);
626 sign = 1;
627 base = 10;
628 goto number;
629 case 'l':
630 if (lflag) {
631 lflag = 0;
632 qflag = 1;
633 } else
634 lflag = 1;
635 goto reswitch;
636 case 'o':
637 if (qflag)
638 uq = va_arg(ap, u_quad_t);
639 else if (lflag)
640 ul = va_arg(ap, u_long);
641 else
642 ul = va_arg(ap, u_int);
643 base = 8;
644 goto nosign;
645 case 'p':
646 ul = (uintptr_t)va_arg(ap, void *);
647 base = 16;
648 sharpflag = (width == 0);
649 goto nosign;
650 case 'q':
651 qflag = 1;
652 goto reswitch;
653 case 'n':
654 case 'r':
655 if (qflag)
656 uq = va_arg(ap, u_quad_t);
657 else if (lflag)
658 ul = va_arg(ap, u_long);
659 else
660 ul = sign ?
661 (u_long)va_arg(ap, int) : va_arg(ap, u_int);
662 base = radix;
663 goto number;
664 case 's':
665 p = va_arg(ap, char *);
666 if (p == NULL)
667 p = "(null)";
668 if (!dot)
669 n = strlen (p);
670 else
671 for (n = 0; n < dwidth && p[n]; n++)
672 continue;
673
674 width -= n;
675
676 if (!ladjust && width > 0)
677 while (width--)
678 PCHAR(padc);
679 while (n--)
680 PCHAR(*p++);
681 if (ladjust && width > 0)
682 while (width--)
683 PCHAR(padc);
684 break;
685 case 'u':
686 if (qflag)
687 uq = va_arg(ap, u_quad_t);
688 else if (lflag)
689 ul = va_arg(ap, u_long);
690 else
691 ul = va_arg(ap, u_int);
692 base = 10;
693 goto nosign;
694 case 'x':
695 case 'X':
696 if (qflag)
697 uq = va_arg(ap, u_quad_t);
698 else if (lflag)
699 ul = va_arg(ap, u_long);
700 else
701 ul = va_arg(ap, u_int);
702 base = 16;
703 goto nosign;
704 case 'z':
705 if (qflag)
706 uq = va_arg(ap, u_quad_t);
707 else if (lflag)
708 ul = va_arg(ap, u_long);
709 else
710 ul = sign ?
711 (u_long)va_arg(ap, int) : va_arg(ap, u_int);
712 base = 16;
713 goto number;
714nosign: sign = 0;
715number:
716 if (qflag) {
717 if (sign && (quad_t)uq < 0) {
718 neg = 1;
719 uq = -(quad_t)uq;
720 }
721 p = ksprintqn(nbuf, uq, base, &tmp);
722 } else {
723 if (sign && (long)ul < 0) {
724 neg = 1;
725 ul = -(long)ul;
726 }
727 p = ksprintn(nbuf, ul, base, &tmp);
728 }
729 if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
730 if (base == 8)
731 tmp++;
732 else if (base == 16)
733 tmp += 2;
734 }
735 if (neg)
736 tmp++;
737
738 if (!ladjust && width && (width -= tmp) > 0)
739 while (width--)
740 PCHAR(padc);
741 if (neg)
742 PCHAR('-');
743 if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
744 if (base == 8) {
745 PCHAR('0');
746 } else if (base == 16) {
747 PCHAR('0');
748 PCHAR('x');
749 }
750 }
751
752 while (*p)
753 PCHAR(*p--);
754
755 if (ladjust && width && (width -= tmp) > 0)
756 while (width--)
757 PCHAR(padc);
758
759 break;
760 default:
761 PCHAR('%');
762 if (lflag)
763 PCHAR('l');
764 PCHAR(ch);
765 break;
766 }
767 }
768#undef PCHAR
769}
770
771/*
772 * Put character in log buffer with a particular priority.
773 */
774static void
775msglogchar(int c, int pri)
776{
777 static int lastpri = -1;
778 static int dangling;
779 char nbuf[MAXNBUF];
780 char *p;
781
782 if (!msgbufmapped)
783 return;
784 if (c == '\0' || c == '\r')
785 return;
786 if (pri != -1 && pri != lastpri) {
787 if (dangling) {
788 msgaddchar('\n', NULL);
789 dangling = 0;
790 }
791 msgaddchar('<', NULL);
792 for (p = ksprintn(nbuf, (u_long)pri, 10, NULL); *p;)
793 msgaddchar(*p--, NULL);
794 msgaddchar('>', NULL);
795 lastpri = pri;
796 }
797 msgaddchar(c, NULL);
798 if (c == '\n') {
799 dangling = 0;
800 lastpri = -1;
801 } else {
802 dangling = 1;
803 }
804}
805
806/*
807 * Put char in log buffer
808 */
809static void
810msgaddchar(int c, void *dummy)
811{
812 struct msgbuf *mbp;
813
814 if (!msgbufmapped)
815 return;
816 mbp = msgbufp;
817 mbp->msg_ptr[mbp->msg_bufx++] = c;
818 if (mbp->msg_bufx >= mbp->msg_size)
819 mbp->msg_bufx = 0;
820 /* If the buffer is full, keep the most recent data. */
821 if (mbp->msg_bufr == mbp->msg_bufx) {
822 if (++mbp->msg_bufr >= mbp->msg_size)
823 mbp->msg_bufr = 0;
824 }
825}
826
827static void
828msgbufcopy(struct msgbuf *oldp)
829{
830 int pos;
831
832 pos = oldp->msg_bufr;
833 while (pos != oldp->msg_bufx) {
834 msglogchar(oldp->msg_ptr[pos], -1);
835 if (++pos >= oldp->msg_size)
836 pos = 0;
837 }
838}
839
840void
841msgbufinit(void *ptr, size_t size)
842{
843 char *cp;
844 static struct msgbuf *oldp = NULL;
845
846 size -= sizeof(*msgbufp);
847 cp = (char *)ptr;
848 msgbufp = (struct msgbuf *) (cp + size);
849 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
850 msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) {
851 bzero(cp, size);
852 bzero(msgbufp, sizeof(*msgbufp));
853 msgbufp->msg_magic = MSG_MAGIC;
854 msgbufp->msg_size = (char *)msgbufp - cp;
855 }
856 msgbufp->msg_ptr = cp;
857 if (msgbufmapped && oldp != msgbufp)
858 msgbufcopy(oldp);
859 msgbufmapped = 1;
860 oldp = msgbufp;
861}
862
863/* Sysctls for accessing/clearing the msgbuf */
864static int
865sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
866{
867 int error;
868
869 /*
870 * Unwind the buffer, so that it's linear (possibly starting with
871 * some initial nulls).
872 */
873 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
874 msgbufp->msg_size - msgbufp->msg_bufx, req);
875 if (error)
876 return (error);
877 if (msgbufp->msg_bufx > 0) {
878 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
879 msgbufp->msg_bufx, req);
880 }
881 return (error);
882}
883
884SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
885 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
886
887static int msgbuf_clear;
888
889static int
890sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
891{
892 int error;
893 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
894 if (!error && req->newptr) {
895 /* Clear the buffer and reset write pointer */
896 bzero(msgbufp->msg_ptr, msgbufp->msg_size);
897 msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
898 msgbuf_clear = 0;
899 }
900 return (error);
901}
902
903SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
904 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
905 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
906
907#include "opt_ddb.h"
908#ifdef DDB
909#include <ddb/ddb.h>
910
911DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
912{
913 int i, j;
914
915 if (!msgbufmapped) {
916 db_printf("msgbuf not mapped yet\n");
917 return;
918 }
919 db_printf("msgbufp = %p\n", msgbufp);
920 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
921 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
922 msgbufp->msg_bufx, msgbufp->msg_ptr);
923 for (i = 0; i < msgbufp->msg_size; i++) {
924 j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
925 db_printf("%c", msgbufp->msg_ptr[j]);
926 }
927 db_printf("\n");
928}
929
930#endif /* DDB */