kernel - Force NFSv3 for diskless nfs mount
[dragonfly.git] / sys / kern / subr_prf.c
blob4de2beac1e002584c5fcd7e49436013bdf2ea581
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.21 2008/07/17 23:56:23 dillon 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/priv.h>
52 #include <sys/tty.h>
53 #include <sys/tprintf.h>
54 #include <sys/stdint.h>
55 #include <sys/syslog.h>
56 #include <sys/cons.h>
57 #include <sys/uio.h>
58 #include <sys/sysctl.h>
59 #include <sys/lock.h>
60 #include <sys/ctype.h>
61 #include <sys/eventhandler.h>
62 #include <sys/kthread.h>
64 #include <sys/thread2.h>
65 #include <sys/spinlock2.h>
67 #ifdef DDB
68 #include <ddb/ddb.h>
69 #endif
72 * Note that stdarg.h and the ANSI style va_start macro is used for both
73 * ANSI and traditional C compilers. We use the __ machine version to stay
74 * within the kernel header file set.
76 #include <machine/stdarg.h>
78 #define TOCONS 0x01
79 #define TOTTY 0x02
80 #define TOLOG 0x04
81 #define TOWAKEUP 0x08
83 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
84 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
86 struct putchar_arg {
87 int flags;
88 int pri;
89 struct tty *tty;
92 struct snprintf_arg {
93 char *str;
94 size_t remain;
97 extern int log_open;
99 struct tty *constty; /* pointer to console "window" tty */
101 static void msglogchar(int c, int pri);
102 static void msgaddchar(int c, void *dummy);
103 static void kputchar (int ch, void *arg);
104 static char *ksprintn (char *nbuf, uintmax_t num, int base, int *lenp,
105 int upper);
106 static void snprintf_func (int ch, void *arg);
108 static int consintr = 1; /* Ok to handle console interrupts? */
109 static int msgbufmapped; /* Set when safe to use msgbuf */
110 static struct spinlock cons_spin = SPINLOCK_INITIALIZER(cons_spin);
111 static thread_t constty_td = NULL;
113 int msgbuftrigger;
115 static int log_console_output = 1;
116 TUNABLE_INT("kern.log_console_output", &log_console_output);
117 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
118 &log_console_output, 0, "");
120 static int unprivileged_read_msgbuf = 1;
121 SYSCTL_INT(_security, OID_AUTO, unprivileged_read_msgbuf, CTLFLAG_RW,
122 &unprivileged_read_msgbuf, 0,
123 "Unprivileged processes may read the kernel message buffer");
126 * Warn that a system table is full.
128 void
129 tablefull(const char *tab)
132 log(LOG_ERR, "%s: table is full\n", tab);
136 * Uprintf prints to the controlling terminal for the current process.
139 uprintf(const char *fmt, ...)
141 struct proc *p = curproc;
142 __va_list ap;
143 struct putchar_arg pca;
144 int retval = 0;
146 if (p && p->p_flag & P_CONTROLT &&
147 p->p_session->s_ttyvp) {
148 __va_start(ap, fmt);
149 pca.tty = p->p_session->s_ttyp;
150 pca.flags = TOTTY;
152 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
153 __va_end(ap);
155 return (retval);
158 tpr_t
159 tprintf_open(struct proc *p)
161 if ((p->p_flag & P_CONTROLT) && p->p_session->s_ttyvp) {
162 sess_hold(p->p_session);
163 return ((tpr_t) p->p_session);
165 return ((tpr_t) NULL);
168 void
169 tprintf_close(tpr_t sess)
171 if (sess)
172 sess_rele((struct session *) sess);
176 * tprintf prints on the controlling terminal associated
177 * with the given session.
180 tprintf(tpr_t tpr, const char *fmt, ...)
182 struct session *sess = (struct session *)tpr;
183 struct tty *tp = NULL;
184 int flags = TOLOG;
185 __va_list ap;
186 struct putchar_arg pca;
187 int retval;
189 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
190 flags |= TOTTY;
191 tp = sess->s_ttyp;
193 __va_start(ap, fmt);
194 pca.tty = tp;
195 pca.flags = flags;
196 pca.pri = LOG_INFO;
197 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
198 __va_end(ap);
199 msgbuftrigger = 1;
200 return (retval);
204 * Ttyprintf displays a message on a tty; it should be used only by
205 * the tty driver, or anything that knows the underlying tty will not
206 * be revoke(2)'d away. Other callers should use tprintf.
209 ttyprintf(struct tty *tp, const char *fmt, ...)
211 __va_list ap;
212 struct putchar_arg pca;
213 int retval;
215 __va_start(ap, fmt);
216 pca.tty = tp;
217 pca.flags = TOTTY;
218 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
219 __va_end(ap);
220 return (retval);
224 * Log writes to the log buffer, and guarantees not to sleep (so can be
225 * called by interrupt routines). If there is no process reading the
226 * log yet, it writes to the console also.
229 log(int level, const char *fmt, ...)
231 __va_list ap;
232 int retval;
233 struct putchar_arg pca;
235 pca.tty = NULL;
236 pca.pri = level;
237 pca.flags = log_open ? TOLOG : TOCONS;
239 __va_start(ap, fmt);
240 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
241 __va_end(ap);
243 msgbuftrigger = 1;
244 return (retval);
247 #define CONSCHUNK 128
249 void
250 log_console(struct uio *uio)
252 int c, i, error, iovlen, nl;
253 struct uio muio;
254 struct iovec *miov = NULL;
255 char *consbuffer;
256 int pri;
258 if (!log_console_output)
259 return;
261 pri = LOG_INFO | LOG_CONSOLE;
262 muio = *uio;
263 iovlen = uio->uio_iovcnt * sizeof (struct iovec);
264 MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
265 MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
266 bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen);
267 muio.uio_iov = miov;
268 uio = &muio;
270 nl = 0;
271 while (uio->uio_resid > 0) {
272 c = (int)szmin(uio->uio_resid, CONSCHUNK);
273 error = uiomove(consbuffer, (size_t)c, uio);
274 if (error != 0)
275 break;
276 for (i = 0; i < c; i++) {
277 msglogchar(consbuffer[i], pri);
278 if (consbuffer[i] == '\n')
279 nl = 1;
280 else
281 nl = 0;
284 if (!nl)
285 msglogchar('\n', pri);
286 msgbuftrigger = 1;
287 FREE(miov, M_TEMP);
288 FREE(consbuffer, M_TEMP);
289 return;
293 * Output to the console.
296 kprintf(const char *fmt, ...)
298 __va_list ap;
299 int savintr;
300 struct putchar_arg pca;
301 int retval;
303 savintr = consintr; /* disable interrupts */
304 consintr = 0;
305 __va_start(ap, fmt);
306 pca.tty = NULL;
307 pca.flags = TOCONS | TOLOG;
308 pca.pri = -1;
309 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
310 __va_end(ap);
311 if (!panicstr)
312 msgbuftrigger = 1;
313 consintr = savintr; /* reenable interrupts */
314 return (retval);
318 kvprintf(const char *fmt, __va_list ap)
320 int savintr;
321 struct putchar_arg pca;
322 int retval;
324 savintr = consintr; /* disable interrupts */
325 consintr = 0;
326 pca.tty = NULL;
327 pca.flags = TOCONS | TOLOG;
328 pca.pri = -1;
329 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
330 if (!panicstr)
331 msgbuftrigger = 1;
332 consintr = savintr; /* reenable interrupts */
333 return (retval);
337 * Limited rate kprintf. The passed rate structure must be initialized
338 * with the desired reporting frequency. A frequency of 0 will result in
339 * no output.
341 * count may be initialized to a negative number to allow an initial
342 * burst.
344 void
345 krateprintf(struct krate *rate, const char *fmt, ...)
347 __va_list ap;
349 if (rate->ticks != (int)time_second) {
350 rate->ticks = (int)time_second;
351 if (rate->count > 0)
352 rate->count = 0;
354 if (rate->count < rate->freq) {
355 ++rate->count;
356 __va_start(ap, fmt);
357 kvprintf(fmt, ap);
358 __va_end(ap);
363 * Print a character to the dmesg log, the console, and/or the user's
364 * terminal.
366 * NOTE: TOTTY does not require nonblocking operation, but TOCONS
367 * and TOLOG do. When we have a constty we still output to
368 * the real console but we have a monitoring thread which
369 * we wakeup which tracks the log.
371 static void
372 kputchar(int c, void *arg)
374 struct putchar_arg *ap = (struct putchar_arg*) arg;
375 int flags = ap->flags;
376 struct tty *tp = ap->tty;
378 if (panicstr)
379 constty = NULL;
380 if ((flags & TOCONS) && tp == NULL && constty)
381 flags |= TOLOG | TOWAKEUP;
382 if ((flags & TOTTY) && tputchar(c, tp) < 0)
383 ap->flags &= ~TOTTY;
384 if ((flags & TOLOG))
385 msglogchar(c, ap->pri);
386 if ((flags & TOCONS) && c)
387 cnputc(c);
388 if (flags & TOWAKEUP)
389 wakeup(constty_td);
393 * Scaled down version of sprintf(3).
396 ksprintf(char *buf, const char *cfmt, ...)
398 int retval;
399 __va_list ap;
401 __va_start(ap, cfmt);
402 retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap);
403 buf[retval] = '\0';
404 __va_end(ap);
405 return (retval);
409 * Scaled down version of vsprintf(3).
412 kvsprintf(char *buf, const char *cfmt, __va_list ap)
414 int retval;
416 retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap);
417 buf[retval] = '\0';
418 return (retval);
422 * Scaled down version of snprintf(3).
425 ksnprintf(char *str, size_t size, const char *format, ...)
427 int retval;
428 __va_list ap;
430 __va_start(ap, format);
431 retval = kvsnprintf(str, size, format, ap);
432 __va_end(ap);
433 return(retval);
437 * Scaled down version of vsnprintf(3).
440 kvsnprintf(char *str, size_t size, const char *format, __va_list ap)
442 struct snprintf_arg info;
443 int retval;
445 info.str = str;
446 info.remain = size;
447 retval = kvcprintf(format, snprintf_func, &info, 10, ap);
448 if (info.remain >= 1)
449 *info.str++ = '\0';
450 return (retval);
454 ksnrprintf(char *str, size_t size, int radix, const char *format, ...)
456 int retval;
457 __va_list ap;
459 __va_start(ap, format);
460 retval = kvsnrprintf(str, size, radix, format, ap);
461 __va_end(ap);
462 return(retval);
466 kvsnrprintf(char *str, size_t size, int radix, const char *format, __va_list ap)
468 struct snprintf_arg info;
469 int retval;
471 info.str = str;
472 info.remain = size;
473 retval = kvcprintf(format, snprintf_func, &info, radix, ap);
474 if (info.remain >= 1)
475 *info.str++ = '\0';
476 return (retval);
480 kvasnrprintf(char **strp, size_t size, int radix,
481 const char *format, __va_list ap)
483 struct snprintf_arg info;
484 int retval;
486 *strp = kmalloc(size, M_TEMP, M_WAITOK);
487 info.str = *strp;
488 info.remain = size;
489 retval = kvcprintf(format, snprintf_func, &info, radix, ap);
490 if (info.remain >= 1)
491 *info.str++ = '\0';
492 return (retval);
495 void
496 kvasfree(char **strp)
498 if (*strp) {
499 kfree(*strp, M_TEMP);
500 *strp = NULL;
504 static void
505 snprintf_func(int ch, void *arg)
507 struct snprintf_arg *const info = arg;
509 if (info->remain >= 2) {
510 *info->str++ = ch;
511 info->remain--;
516 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
517 * order; return an optional length and a pointer to the last character
518 * written in the buffer (i.e., the first character of the string).
519 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
521 static char *
522 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
524 char *p, c;
526 p = nbuf;
527 *p = '\0';
528 do {
529 c = hex2ascii(num % base);
530 *++p = upper ? toupper(c) : c;
531 } while (num /= base);
532 if (lenp)
533 *lenp = p - nbuf;
534 return (p);
538 * Scaled down version of printf(3).
540 * Two additional formats:
542 * The format %b is supported to decode error registers.
543 * Its usage is:
545 * kprintf("reg=%b\n", regval, "<base><arg>*");
547 * where <base> is the output base expressed as a control character, e.g.
548 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
549 * the first of which gives the bit number to be inspected (origin 1), and
550 * the next characters (up to a control character, i.e. a character <= 32),
551 * give the name of the register. Thus:
553 * kvcprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
555 * would produce output:
557 * reg=3<BITTWO,BITONE>
559 * XXX: %D -- Hexdump, takes pointer and separator string:
560 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
561 * ("%*D", len, ptr, " " -> XX XX XX XX ...
564 #define PCHAR(c) {int cc=(c); if(func) (*func)(cc,arg); else *d++=cc; retval++;}
567 kvcprintf(char const *fmt, void (*func)(int, void*), void *arg,
568 int radix, __va_list ap)
570 char nbuf[MAXNBUF];
571 char *d;
572 const char *p, *percent, *q;
573 u_char *up;
574 int ch, n;
575 uintmax_t num;
576 int base, tmp, width, ladjust, sharpflag, neg, sign, dot;
577 int cflag, hflag, jflag, lflag, qflag, tflag, zflag;
578 int dwidth, upper;
579 char padc;
580 int retval = 0, stop = 0;
581 int usespin;
584 * Make a supreme effort to avoid reentrant panics or deadlocks.
586 if (func == kputchar) {
587 if (mycpu->gd_flags & GDF_KPRINTF)
588 return(0);
589 atomic_set_long(&mycpu->gd_flags, GDF_KPRINTF);
592 num = 0;
593 if (!func)
594 d = (char *) arg;
595 else
596 d = NULL;
598 if (fmt == NULL)
599 fmt = "(fmt null)\n";
601 if (radix < 2 || radix > 36)
602 radix = 10;
604 usespin = (panic_cpu_gd != mycpu &&
605 func == kputchar &&
606 (((struct putchar_arg *)arg)->flags & TOTTY) == 0);
607 if (usespin) {
608 crit_enter_hard();
609 spin_lock(&cons_spin);
612 for (;;) {
613 padc = ' ';
614 width = 0;
615 while ((ch = (u_char)*fmt++) != '%' || stop) {
616 if (ch == '\0')
617 goto done;
618 PCHAR(ch);
620 percent = fmt - 1;
621 dot = dwidth = ladjust = neg = sharpflag = sign = upper = 0;
622 cflag = hflag = jflag = lflag = qflag = tflag = zflag = 0;
624 reswitch:
625 switch (ch = (u_char)*fmt++) {
626 case '.':
627 dot = 1;
628 goto reswitch;
629 case '#':
630 sharpflag = 1;
631 goto reswitch;
632 case '+':
633 sign = 1;
634 goto reswitch;
635 case '-':
636 ladjust = 1;
637 goto reswitch;
638 case '%':
639 PCHAR(ch);
640 break;
641 case '*':
642 if (!dot) {
643 width = __va_arg(ap, int);
644 if (width < 0) {
645 ladjust = !ladjust;
646 width = -width;
648 } else {
649 dwidth = __va_arg(ap, int);
651 goto reswitch;
652 case '0':
653 if (!dot) {
654 padc = '0';
655 goto reswitch;
657 case '1': case '2': case '3': case '4':
658 case '5': case '6': case '7': case '8': case '9':
659 for (n = 0;; ++fmt) {
660 n = n * 10 + ch - '0';
661 ch = *fmt;
662 if (ch < '0' || ch > '9')
663 break;
665 if (dot)
666 dwidth = n;
667 else
668 width = n;
669 goto reswitch;
670 case 'b':
671 num = (u_int)__va_arg(ap, int);
672 p = __va_arg(ap, char *);
673 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
674 PCHAR(*q--);
676 if (num == 0)
677 break;
679 for (tmp = 0; *p;) {
680 n = *p++;
681 if (num & (1 << (n - 1))) {
682 PCHAR(tmp ? ',' : '<');
683 for (; (n = *p) > ' '; ++p)
684 PCHAR(n);
685 tmp = 1;
686 } else
687 for (; *p > ' '; ++p)
688 continue;
690 if (tmp)
691 PCHAR('>');
692 break;
693 case 'c':
694 PCHAR(__va_arg(ap, int));
695 break;
696 case 'D':
697 up = __va_arg(ap, u_char *);
698 p = __va_arg(ap, char *);
699 if (!width)
700 width = 16;
701 while(width--) {
702 PCHAR(hex2ascii(*up >> 4));
703 PCHAR(hex2ascii(*up & 0x0f));
704 up++;
705 if (width)
706 for (q=p;*q;q++)
707 PCHAR(*q);
709 break;
710 case 'd':
711 case 'i':
712 base = 10;
713 sign = 1;
714 goto handle_sign;
715 case 'h':
716 if (hflag) {
717 hflag = 0;
718 cflag = 1;
719 } else
720 hflag = 1;
721 goto reswitch;
722 case 'j':
723 jflag = 1;
724 goto reswitch;
725 case 'l':
726 if (lflag) {
727 lflag = 0;
728 qflag = 1;
729 } else
730 lflag = 1;
731 goto reswitch;
732 case 'n':
733 if (cflag)
734 *(__va_arg(ap, char *)) = retval;
735 else if (hflag)
736 *(__va_arg(ap, short *)) = retval;
737 else if (jflag)
738 *(__va_arg(ap, intmax_t *)) = retval;
739 else if (lflag)
740 *(__va_arg(ap, long *)) = retval;
741 else if (qflag)
742 *(__va_arg(ap, quad_t *)) = retval;
743 else
744 *(__va_arg(ap, int *)) = retval;
745 break;
746 case 'o':
747 base = 8;
748 goto handle_nosign;
749 case 'p':
750 base = 16;
751 sharpflag = (width == 0);
752 sign = 0;
753 num = (uintptr_t)__va_arg(ap, void *);
754 goto number;
755 case 'q':
756 qflag = 1;
757 goto reswitch;
758 case 'r':
759 base = radix;
760 if (sign)
761 goto handle_sign;
762 goto handle_nosign;
763 case 's':
764 p = __va_arg(ap, char *);
765 if (p == NULL)
766 p = "(null)";
767 if (!dot)
768 n = strlen (p);
769 else
770 for (n = 0; n < dwidth && p[n]; n++)
771 continue;
773 width -= n;
775 if (!ladjust && width > 0)
776 while (width--)
777 PCHAR(padc);
778 while (n--)
779 PCHAR(*p++);
780 if (ladjust && width > 0)
781 while (width--)
782 PCHAR(padc);
783 break;
784 case 't':
785 tflag = 1;
786 goto reswitch;
787 case 'u':
788 base = 10;
789 goto handle_nosign;
790 case 'X':
791 upper = 1;
792 /* FALLTHROUGH */
793 case 'x':
794 base = 16;
795 goto handle_nosign;
796 case 'z':
797 zflag = 1;
798 goto reswitch;
799 handle_nosign:
800 sign = 0;
801 if (cflag)
802 num = (u_char)__va_arg(ap, int);
803 else if (hflag)
804 num = (u_short)__va_arg(ap, int);
805 else if (jflag)
806 num = __va_arg(ap, uintmax_t);
807 else if (lflag)
808 num = __va_arg(ap, u_long);
809 else if (qflag)
810 num = __va_arg(ap, u_quad_t);
811 else if (tflag)
812 num = __va_arg(ap, ptrdiff_t);
813 else if (zflag)
814 num = __va_arg(ap, size_t);
815 else
816 num = __va_arg(ap, u_int);
817 goto number;
818 handle_sign:
819 if (cflag)
820 num = (char)__va_arg(ap, int);
821 else if (hflag)
822 num = (short)__va_arg(ap, int);
823 else if (jflag)
824 num = __va_arg(ap, intmax_t);
825 else if (lflag)
826 num = __va_arg(ap, long);
827 else if (qflag)
828 num = __va_arg(ap, quad_t);
829 else if (tflag)
830 num = __va_arg(ap, ptrdiff_t);
831 else if (zflag)
832 num = __va_arg(ap, ssize_t);
833 else
834 num = __va_arg(ap, int);
835 number:
836 if (sign && (intmax_t)num < 0) {
837 neg = 1;
838 num = -(intmax_t)num;
840 p = ksprintn(nbuf, num, base, &tmp, upper);
841 if (sharpflag && num != 0) {
842 if (base == 8)
843 tmp++;
844 else if (base == 16)
845 tmp += 2;
847 if (neg)
848 tmp++;
850 if (!ladjust && padc != '0' && width &&
851 (width -= tmp) > 0) {
852 while (width--)
853 PCHAR(padc);
855 if (neg)
856 PCHAR('-');
857 if (sharpflag && num != 0) {
858 if (base == 8) {
859 PCHAR('0');
860 } else if (base == 16) {
861 PCHAR('0');
862 PCHAR('x');
865 if (!ladjust && width && (width -= tmp) > 0)
866 while (width--)
867 PCHAR(padc);
869 while (*p)
870 PCHAR(*p--);
872 if (ladjust && width && (width -= tmp) > 0)
873 while (width--)
874 PCHAR(padc);
876 break;
877 default:
878 while (percent < fmt)
879 PCHAR(*percent++);
881 * Since we ignore an formatting argument it is no
882 * longer safe to obey the remaining formatting
883 * arguments as the arguments will no longer match
884 * the format specs.
886 stop = 1;
887 break;
890 done:
892 * Cleanup reentrancy issues.
894 if (func == kputchar)
895 atomic_clear_long(&mycpu->gd_flags, GDF_KPRINTF);
896 if (usespin) {
897 spin_unlock(&cons_spin);
898 crit_exit_hard();
900 return (retval);
903 #undef PCHAR
906 * Called from the panic code to try to get the console working
907 * again in case we paniced inside a kprintf().
909 void
910 kvcreinitspin(void)
912 spin_init(&cons_spin);
913 atomic_clear_long(&mycpu->gd_flags, GDF_KPRINTF);
917 * Console support thread for constty intercepts. This is needed because
918 * console tty intercepts can block. Instead of having kputchar() attempt
919 * to directly write to the console intercept we just force it to log
920 * and wakeup this baby to track and dump the log to constty.
922 static void
923 constty_daemon(void)
925 int rindex = -1;
926 int windex = -1;
927 struct msgbuf *mbp;
928 struct tty *tp;
930 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc,
931 constty_td, SHUTDOWN_PRI_FIRST);
932 constty_td->td_flags |= TDF_SYSTHREAD;
934 for (;;) {
935 kproc_suspend_loop();
937 crit_enter();
938 mbp = msgbufp;
939 if (mbp == NULL || msgbufmapped == 0 ||
940 windex == mbp->msg_bufx) {
941 tsleep(constty_td, 0, "waiting", hz*60);
942 crit_exit();
943 continue;
945 windex = mbp->msg_bufx;
946 crit_exit();
949 * Get message buf FIFO indices. rindex is tracking.
951 if ((tp = constty) == NULL) {
952 rindex = mbp->msg_bufx;
953 continue;
957 * Don't blow up if the message buffer is broken
959 if (windex < 0 || windex >= mbp->msg_size)
960 continue;
961 if (rindex < 0 || rindex >= mbp->msg_size)
962 rindex = windex;
965 * And dump it. If constty gets stuck will give up.
967 while (rindex != windex) {
968 if (tputchar((uint8_t)mbp->msg_ptr[rindex], tp) < 0) {
969 constty = NULL;
970 rindex = mbp->msg_bufx;
971 break;
973 if (++rindex >= mbp->msg_size)
974 rindex = 0;
975 if (tp->t_outq.c_cc >= tp->t_ohiwat) {
976 tsleep(constty_daemon, 0, "blocked", hz / 10);
977 if (tp->t_outq.c_cc >= tp->t_ohiwat) {
978 rindex = windex;
979 break;
986 static struct kproc_desc constty_kp = {
987 "consttyd",
988 constty_daemon,
989 &constty_td
991 SYSINIT(bufdaemon, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY,
992 kproc_start, &constty_kp)
995 * Put character in log buffer with a particular priority.
997 * MPSAFE
999 static void
1000 msglogchar(int c, int pri)
1002 static int lastpri = -1;
1003 static int dangling;
1004 char nbuf[MAXNBUF];
1005 char *p;
1007 if (!msgbufmapped)
1008 return;
1009 if (c == '\0' || c == '\r')
1010 return;
1011 if (pri != -1 && pri != lastpri) {
1012 if (dangling) {
1013 msgaddchar('\n', NULL);
1014 dangling = 0;
1016 msgaddchar('<', NULL);
1017 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
1018 msgaddchar(*p--, NULL);
1019 msgaddchar('>', NULL);
1020 lastpri = pri;
1022 msgaddchar(c, NULL);
1023 if (c == '\n') {
1024 dangling = 0;
1025 lastpri = -1;
1026 } else {
1027 dangling = 1;
1032 * Put char in log buffer. Make sure nothing blows up beyond repair if
1033 * we have an MP race.
1035 * MPSAFE.
1037 static void
1038 msgaddchar(int c, void *dummy)
1040 struct msgbuf *mbp;
1041 int rindex;
1042 int windex;
1044 if (!msgbufmapped)
1045 return;
1046 mbp = msgbufp;
1047 windex = mbp->msg_bufx;
1048 mbp->msg_ptr[windex] = c;
1049 if (++windex >= mbp->msg_size)
1050 windex = 0;
1051 rindex = mbp->msg_bufr;
1052 if (windex == rindex) {
1053 rindex += 32;
1054 if (rindex >= mbp->msg_size)
1055 rindex -= mbp->msg_size;
1056 mbp->msg_bufr = rindex;
1058 mbp->msg_bufx = windex;
1061 static void
1062 msgbufcopy(struct msgbuf *oldp)
1064 int pos;
1066 pos = oldp->msg_bufr;
1067 while (pos != oldp->msg_bufx) {
1068 msglogchar(oldp->msg_ptr[pos], -1);
1069 if (++pos >= oldp->msg_size)
1070 pos = 0;
1074 void
1075 msgbufinit(void *ptr, size_t size)
1077 char *cp;
1078 static struct msgbuf *oldp = NULL;
1080 size -= sizeof(*msgbufp);
1081 cp = (char *)ptr;
1082 msgbufp = (struct msgbuf *) (cp + size);
1083 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
1084 msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) {
1085 bzero(cp, size);
1086 bzero(msgbufp, sizeof(*msgbufp));
1087 msgbufp->msg_magic = MSG_MAGIC;
1088 msgbufp->msg_size = (char *)msgbufp - cp;
1090 msgbufp->msg_ptr = cp;
1091 if (msgbufmapped && oldp != msgbufp)
1092 msgbufcopy(oldp);
1093 msgbufmapped = 1;
1094 oldp = msgbufp;
1097 /* Sysctls for accessing/clearing the msgbuf */
1099 static int
1100 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
1102 struct ucred *cred;
1103 int error;
1106 * Only wheel or root can access the message log.
1108 if (unprivileged_read_msgbuf == 0) {
1109 KKASSERT(req->td->td_proc);
1110 cred = req->td->td_proc->p_ucred;
1112 if ((cred->cr_prison || groupmember(0, cred) == 0) &&
1113 priv_check(req->td, PRIV_ROOT) != 0
1115 return (EPERM);
1120 * Unwind the buffer, so that it's linear (possibly starting with
1121 * some initial nulls).
1123 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
1124 msgbufp->msg_size - msgbufp->msg_bufx, req);
1125 if (error)
1126 return (error);
1127 if (msgbufp->msg_bufx > 0) {
1128 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
1129 msgbufp->msg_bufx, req);
1131 return (error);
1134 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
1135 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
1137 static int msgbuf_clear;
1139 static int
1140 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
1142 int error;
1143 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
1144 if (!error && req->newptr) {
1145 /* Clear the buffer and reset write pointer */
1146 bzero(msgbufp->msg_ptr, msgbufp->msg_size);
1147 msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
1148 msgbuf_clear = 0;
1150 return (error);
1153 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
1154 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
1155 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
1157 #ifdef DDB
1159 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
1161 int i, j;
1163 if (!msgbufmapped) {
1164 db_printf("msgbuf not mapped yet\n");
1165 return;
1167 db_printf("msgbufp = %p\n", msgbufp);
1168 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
1169 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
1170 msgbufp->msg_bufx, msgbufp->msg_ptr);
1171 for (i = 0; i < msgbufp->msg_size; i++) {
1172 j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
1173 db_printf("%c", msgbufp->msg_ptr[j]);
1175 db_printf("\n");
1178 #endif /* DDB */