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 $ 40 * $DragonFly: src/sys/kern/subr_prf.c,v 1.21 2008/07/17 23:56:23 dillon Exp $ 41 */ 42 43 #include "opt_ddb.h" 44 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> 60 61 #ifdef DDB 62 #include <ddb/ddb.h> 63 #endif 64 65 /* 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. 69 */ 70 #include <machine/stdarg.h> 71 72 #define TOCONS 0x01 73 #define TOTTY 0x02 74 #define TOLOG 0x04 75 76 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */ 77 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1) 78 79 struct putchar_arg { 80 int flags; 81 int pri; 82 struct tty *tty; 83 }; 84 85 struct snprintf_arg { 86 char *str; 87 size_t remain; 88 }; 89 90 extern int log_open; 91 92 struct tty *constty; /* pointer to console "window" tty */ 93 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); 101 102 static int consintr = 1; /* Ok to handle console interrupts? */ 103 static int msgbufmapped; /* Set when safe to use msgbuf */ 104 int msgbuftrigger; 105 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, ""); 110 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"); 115 116 /* 117 * Warn that a system table is full. 118 */ 119 void 120 tablefull(const char *tab) 121 { 122 123 log(LOG_ERR, "%s: table is full\n", tab); 124 } 125 126 /* 127 * Uprintf prints to the controlling terminal for the current process. 128 */ 129 int 130 uprintf(const char *fmt, ...) 131 { 132 struct proc *p = curproc; 133 __va_list ap; 134 struct putchar_arg pca; 135 int retval = 0; 136 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; 142 143 retval = kvcprintf(fmt, kputchar, &pca, 10, ap); 144 __va_end(ap); 145 } 146 return (retval); 147 } 148 149 tpr_t 150 tprintf_open(struct proc *p) 151 { 152 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); 156 } 157 return ((tpr_t) NULL); 158 } 159 160 void 161 tprintf_close(tpr_t sess) 162 { 163 if (sess) 164 sess_rele((struct session *) sess); 165 } 166 167 /* 168 * tprintf prints on the controlling terminal associated 169 * with the given session. 170 */ 171 int 172 tprintf(tpr_t tpr, const char *fmt, ...) 173 { 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; 180 181 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 182 flags |= TOTTY; 183 tp = sess->s_ttyp; 184 } 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); 193 } 194 195 /* 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. 199 */ 200 int 201 ttyprintf(struct tty *tp, const char *fmt, ...) 202 { 203 __va_list ap; 204 struct putchar_arg pca; 205 int retval; 206 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); 213 } 214 215 /* 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. 219 */ 220 int 221 log(int level, const char *fmt, ...) 222 { 223 __va_list ap; 224 int retval; 225 struct putchar_arg pca; 226 227 pca.tty = NULL; 228 pca.pri = level; 229 pca.flags = log_open ? TOLOG : TOCONS; 230 231 __va_start(ap, fmt); 232 retval = kvcprintf(fmt, kputchar, &pca, 10, ap); 233 __va_end(ap); 234 235 msgbuftrigger = 1; 236 return (retval); 237 } 238 239 #define CONSCHUNK 128 240 241 void 242 log_console(struct uio *uio) 243 { 244 int c, i, error, iovlen, nl; 245 struct uio muio; 246 struct iovec *miov = NULL; 247 char *consbuffer; 248 int pri; 249 250 if (!log_console_output) 251 return; 252 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; 261 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; 274 } 275 } 276 if (!nl) 277 msglogchar('\n', pri); 278 msgbuftrigger = 1; 279 FREE(miov, M_TEMP); 280 FREE(consbuffer, M_TEMP); 281 return; 282 } 283 284 /* 285 * Output to the console. 286 * 287 * NOT YET ENTIRELY MPSAFE 288 */ 289 int 290 kprintf(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 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); 311 } 312 313 int 314 kvprintf(const char *fmt, __va_list ap) 315 { 316 int savintr; 317 struct putchar_arg pca; 318 int retval; 319 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); 332 } 333 334 /* 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. 338 * 339 * count may be initialized to a negative number to allow an initial 340 * burst. 341 */ 342 void 343 krateprintf(struct krate *rate, const char *fmt, ...) 344 { 345 __va_list ap; 346 347 if (rate->ticks != (int)time_second) { 348 rate->ticks = (int)time_second; 349 if (rate->count > 0) 350 rate->count = 0; 351 } 352 if (rate->count < rate->freq) { 353 ++rate->count; 354 __va_start(ap, fmt); 355 kvprintf(fmt, ap); 356 __va_end(ap); 357 } 358 } 359 360 /* 361 * Print a character on console or users terminal. If destination is 362 * the console then the last bunch of characters are saved in msgbuf for 363 * inspection later. 364 * 365 * NOT YET ENTIRELY MPSAFE, EVEN WHEN LOGGING JUST TO THE SYSCONSOLE. 366 */ 367 static void 368 kputchar(int c, void *arg) 369 { 370 struct putchar_arg *ap = (struct putchar_arg*) arg; 371 int flags = ap->flags; 372 struct tty *tp = ap->tty; 373 if (panicstr) 374 constty = NULL; 375 if ((flags & TOCONS) && tp == NULL && constty) { 376 tp = constty; 377 flags |= TOTTY; 378 } 379 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 && 380 (flags & TOCONS) && tp == constty) 381 constty = NULL; 382 if ((flags & TOLOG)) 383 msglogchar(c, ap->pri); 384 if ((flags & TOCONS) && constty == NULL && c != '\0') 385 (*v_putc)(c); 386 } 387 388 /* 389 * Scaled down version of sprintf(3). 390 */ 391 int 392 ksprintf(char *buf, const char *cfmt, ...) 393 { 394 int retval; 395 __va_list ap; 396 397 __va_start(ap, cfmt); 398 retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap); 399 buf[retval] = '\0'; 400 __va_end(ap); 401 return (retval); 402 } 403 404 /* 405 * Scaled down version of vsprintf(3). 406 */ 407 int 408 kvsprintf(char *buf, const char *cfmt, __va_list ap) 409 { 410 int retval; 411 412 retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap); 413 buf[retval] = '\0'; 414 return (retval); 415 } 416 417 /* 418 * Scaled down version of snprintf(3). 419 */ 420 int 421 ksnprintf(char *str, size_t size, const char *format, ...) 422 { 423 int retval; 424 __va_list ap; 425 426 __va_start(ap, format); 427 retval = kvsnprintf(str, size, format, ap); 428 __va_end(ap); 429 return(retval); 430 } 431 432 /* 433 * Scaled down version of vsnprintf(3). 434 */ 435 int 436 kvsnprintf(char *str, size_t size, const char *format, __va_list ap) 437 { 438 struct snprintf_arg info; 439 int retval; 440 441 info.str = str; 442 info.remain = size; 443 retval = kvcprintf(format, snprintf_func, &info, 10, ap); 444 if (info.remain >= 1) 445 *info.str++ = '\0'; 446 return (retval); 447 } 448 449 static void 450 snprintf_func(int ch, void *arg) 451 { 452 struct snprintf_arg *const info = arg; 453 454 if (info->remain >= 2) { 455 *info->str++ = ch; 456 info->remain--; 457 } 458 } 459 460 /* 461 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse 462 * order; return an optional length and a pointer to the last character 463 * written in the buffer (i.e., the first character of the string). 464 * The buffer pointed to by `nbuf' must have length >= MAXNBUF. 465 */ 466 static char * 467 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper) 468 { 469 char *p, c; 470 471 p = nbuf; 472 *p = '\0'; 473 do { 474 c = hex2ascii(num % base); 475 *++p = upper ? toupper(c) : c; 476 } while (num /= base); 477 if (lenp) 478 *lenp = p - nbuf; 479 return (p); 480 } 481 482 /* 483 * Scaled down version of printf(3). 484 * 485 * Two additional formats: 486 * 487 * The format %b is supported to decode error registers. 488 * Its usage is: 489 * 490 * kprintf("reg=%b\n", regval, "<base><arg>*"); 491 * 492 * where <base> is the output base expressed as a control character, e.g. 493 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 494 * the first of which gives the bit number to be inspected (origin 1), and 495 * the next characters (up to a control character, i.e. a character <= 32), 496 * give the name of the register. Thus: 497 * 498 * kvcprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 499 * 500 * would produce output: 501 * 502 * reg=3<BITTWO,BITONE> 503 * 504 * XXX: %D -- Hexdump, takes pointer and separator string: 505 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX 506 * ("%*D", len, ptr, " " -> XX XX XX XX ... 507 */ 508 int 509 kvcprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, __va_list ap) 510 { 511 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } 512 char nbuf[MAXNBUF]; 513 char *d; 514 const char *p, *percent, *q; 515 u_char *up; 516 int ch, n; 517 uintmax_t num; 518 int base, tmp, width, ladjust, sharpflag, neg, sign, dot; 519 int jflag, lflag, qflag, tflag; 520 int dwidth, upper; 521 char padc; 522 int retval = 0, stop = 0; 523 524 num = 0; 525 if (!func) 526 d = (char *) arg; 527 else 528 d = NULL; 529 530 if (fmt == NULL) 531 fmt = "(fmt null)\n"; 532 533 if (radix < 2 || radix > 36) 534 radix = 10; 535 536 for (;;) { 537 padc = ' '; 538 width = 0; 539 while ((ch = (u_char)*fmt++) != '%' || stop) { 540 if (ch == '\0') 541 return (retval); 542 PCHAR(ch); 543 } 544 percent = fmt - 1; 545 dot = dwidth = ladjust = neg = sharpflag = sign = upper = 0; 546 jflag = lflag = qflag = tflag = 0; 547 548 reswitch: 549 switch (ch = (u_char)*fmt++) { 550 case '.': 551 dot = 1; 552 goto reswitch; 553 case '#': 554 sharpflag = 1; 555 goto reswitch; 556 case '+': 557 sign = 1; 558 goto reswitch; 559 case '-': 560 ladjust = 1; 561 goto reswitch; 562 case '%': 563 PCHAR(ch); 564 break; 565 case '*': 566 if (!dot) { 567 width = __va_arg(ap, int); 568 if (width < 0) { 569 ladjust = !ladjust; 570 width = -width; 571 } 572 } else { 573 dwidth = __va_arg(ap, int); 574 } 575 goto reswitch; 576 case '0': 577 if (!dot) { 578 padc = '0'; 579 goto reswitch; 580 } 581 case '1': case '2': case '3': case '4': 582 case '5': case '6': case '7': case '8': case '9': 583 for (n = 0;; ++fmt) { 584 n = n * 10 + ch - '0'; 585 ch = *fmt; 586 if (ch < '0' || ch > '9') 587 break; 588 } 589 if (dot) 590 dwidth = n; 591 else 592 width = n; 593 goto reswitch; 594 case 'b': 595 num = (u_int)__va_arg(ap, int); 596 p = __va_arg(ap, char *); 597 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;) 598 PCHAR(*q--); 599 600 if (num == 0) 601 break; 602 603 for (tmp = 0; *p;) { 604 n = *p++; 605 if (num & (1 << (n - 1))) { 606 PCHAR(tmp ? ',' : '<'); 607 for (; (n = *p) > ' '; ++p) 608 PCHAR(n); 609 tmp = 1; 610 } else 611 for (; *p > ' '; ++p) 612 continue; 613 } 614 if (tmp) 615 PCHAR('>'); 616 break; 617 case 'c': 618 PCHAR(__va_arg(ap, int)); 619 break; 620 case 'D': 621 up = __va_arg(ap, u_char *); 622 p = __va_arg(ap, char *); 623 if (!width) 624 width = 16; 625 while(width--) { 626 PCHAR(hex2ascii(*up >> 4)); 627 PCHAR(hex2ascii(*up & 0x0f)); 628 up++; 629 if (width) 630 for (q=p;*q;q++) 631 PCHAR(*q); 632 } 633 break; 634 case 'd': 635 case 'i': 636 base = 10; 637 sign = 1; 638 goto handle_sign; 639 case 'j': 640 jflag = 1; 641 goto reswitch; 642 case 'l': 643 if (lflag) { 644 lflag = 0; 645 qflag = 1; 646 } else 647 lflag = 1; 648 goto reswitch; 649 case 'n': 650 if (jflag) 651 *(__va_arg(ap, intmax_t *)) = retval; 652 else if (lflag) 653 *(__va_arg(ap, long *)) = retval; 654 else if (qflag) 655 *(__va_arg(ap, quad_t *)) = retval; 656 else 657 *(__va_arg(ap, int *)) = retval; 658 break; 659 case 'o': 660 base = 8; 661 goto handle_nosign; 662 case 'p': 663 base = 16; 664 sharpflag = (width == 0); 665 sign = 0; 666 num = (uintptr_t)__va_arg(ap, void *); 667 goto number; 668 case 'q': 669 qflag = 1; 670 goto reswitch; 671 case 'r': 672 base = radix; 673 if (sign) 674 goto handle_sign; 675 goto handle_nosign; 676 case 's': 677 p = __va_arg(ap, char *); 678 if (p == NULL) 679 p = "(null)"; 680 if (!dot) 681 n = strlen (p); 682 else 683 for (n = 0; n < dwidth && p[n]; n++) 684 continue; 685 686 width -= n; 687 688 if (!ladjust && width > 0) 689 while (width--) 690 PCHAR(padc); 691 while (n--) 692 PCHAR(*p++); 693 if (ladjust && width > 0) 694 while (width--) 695 PCHAR(padc); 696 break; 697 case 't': 698 tflag = 1; 699 goto reswitch; 700 case 'u': 701 base = 10; 702 goto handle_nosign; 703 case 'X': 704 upper = 1; 705 /* FALLTHROUGH */ 706 case 'x': 707 base = 16; 708 goto handle_nosign; 709 case 'z': 710 base = 16; 711 sign = 1; 712 goto handle_sign; 713 handle_nosign: 714 sign = 0; 715 if (jflag) 716 num = __va_arg(ap, uintmax_t); 717 else if (lflag) 718 num = __va_arg(ap, u_long); 719 else if (qflag) 720 num = __va_arg(ap, u_quad_t); 721 else if (tflag) 722 num = __va_arg(ap, ptrdiff_t); 723 else 724 num = __va_arg(ap, u_int); 725 goto number; 726 handle_sign: 727 if (jflag) 728 num = __va_arg(ap, intmax_t); 729 else if (lflag) 730 num = __va_arg(ap, long); 731 else if (qflag) 732 num = __va_arg(ap, quad_t); 733 else if (tflag) 734 num = __va_arg(ap, ptrdiff_t); 735 else 736 num = __va_arg(ap, int); 737 number: 738 if (sign && (intmax_t)num < 0) { 739 neg = 1; 740 num = -(intmax_t)num; 741 } 742 p = ksprintn(nbuf, num, base, &tmp, upper); 743 if (sharpflag && num != 0) { 744 if (base == 8) 745 tmp++; 746 else if (base == 16) 747 tmp += 2; 748 } 749 if (neg) 750 tmp++; 751 752 if (!ladjust && padc != '0' && width && 753 (width -= tmp) > 0) { 754 while (width--) 755 PCHAR(padc); 756 } 757 if (neg) 758 PCHAR('-'); 759 if (sharpflag && num != 0) { 760 if (base == 8) { 761 PCHAR('0'); 762 } else if (base == 16) { 763 PCHAR('0'); 764 PCHAR('x'); 765 } 766 } 767 if (!ladjust && width && (width -= tmp) > 0) 768 while (width--) 769 PCHAR(padc); 770 771 while (*p) 772 PCHAR(*p--); 773 774 if (ladjust && width && (width -= tmp) > 0) 775 while (width--) 776 PCHAR(padc); 777 778 break; 779 default: 780 while (percent < fmt) 781 PCHAR(*percent++); 782 /* 783 * Since we ignore an formatting argument it is no 784 * longer safe to obey the remaining formatting 785 * arguments as the arguments will no longer match 786 * the format specs. 787 */ 788 stop = 1; 789 break; 790 } 791 } 792 #undef PCHAR 793 } 794 795 /* 796 * Put character in log buffer with a particular priority. 797 * 798 * MPSAFE 799 */ 800 static void 801 msglogchar(int c, int pri) 802 { 803 static int lastpri = -1; 804 static int dangling; 805 char nbuf[MAXNBUF]; 806 char *p; 807 808 if (!msgbufmapped) 809 return; 810 if (c == '\0' || c == '\r') 811 return; 812 if (pri != -1 && pri != lastpri) { 813 if (dangling) { 814 msgaddchar('\n', NULL); 815 dangling = 0; 816 } 817 msgaddchar('<', NULL); 818 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;) 819 msgaddchar(*p--, NULL); 820 msgaddchar('>', NULL); 821 lastpri = pri; 822 } 823 msgaddchar(c, NULL); 824 if (c == '\n') { 825 dangling = 0; 826 lastpri = -1; 827 } else { 828 dangling = 1; 829 } 830 } 831 832 /* 833 * Put char in log buffer. Make sure nothing blows up beyond repair if 834 * we have an MP race. 835 * 836 * MPSAFE. 837 */ 838 static void 839 msgaddchar(int c, void *dummy) 840 { 841 struct msgbuf *mbp; 842 int rindex; 843 int windex; 844 845 if (!msgbufmapped) 846 return; 847 mbp = msgbufp; 848 windex = mbp->msg_bufx; 849 mbp->msg_ptr[windex] = c; 850 if (++windex >= mbp->msg_size) 851 windex = 0; 852 rindex = mbp->msg_bufr; 853 if (windex == rindex) { 854 rindex += 32; 855 if (rindex >= mbp->msg_size) 856 rindex -= mbp->msg_size; 857 mbp->msg_bufr = rindex; 858 } 859 mbp->msg_bufx = windex; 860 } 861 862 static void 863 msgbufcopy(struct msgbuf *oldp) 864 { 865 int pos; 866 867 pos = oldp->msg_bufr; 868 while (pos != oldp->msg_bufx) { 869 msglogchar(oldp->msg_ptr[pos], -1); 870 if (++pos >= oldp->msg_size) 871 pos = 0; 872 } 873 } 874 875 void 876 msgbufinit(void *ptr, size_t size) 877 { 878 char *cp; 879 static struct msgbuf *oldp = NULL; 880 881 size -= sizeof(*msgbufp); 882 cp = (char *)ptr; 883 msgbufp = (struct msgbuf *) (cp + size); 884 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size || 885 msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) { 886 bzero(cp, size); 887 bzero(msgbufp, sizeof(*msgbufp)); 888 msgbufp->msg_magic = MSG_MAGIC; 889 msgbufp->msg_size = (char *)msgbufp - cp; 890 } 891 msgbufp->msg_ptr = cp; 892 if (msgbufmapped && oldp != msgbufp) 893 msgbufcopy(oldp); 894 msgbufmapped = 1; 895 oldp = msgbufp; 896 } 897 898 /* Sysctls for accessing/clearing the msgbuf */ 899 900 static int 901 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS) 902 { 903 struct ucred *cred; 904 int error; 905 906 /* 907 * Only wheel or root can access the message log. 908 */ 909 if (unprivileged_read_msgbuf == 0) { 910 KKASSERT(req->td->td_proc); 911 cred = req->td->td_proc->p_ucred; 912 913 if ((cred->cr_prison || groupmember(0, cred) == 0) && 914 suser(req->td) != 0 915 ) { 916 return (EPERM); 917 } 918 } 919 920 /* 921 * Unwind the buffer, so that it's linear (possibly starting with 922 * some initial nulls). 923 */ 924 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx, 925 msgbufp->msg_size - msgbufp->msg_bufx, req); 926 if (error) 927 return (error); 928 if (msgbufp->msg_bufx > 0) { 929 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr, 930 msgbufp->msg_bufx, req); 931 } 932 return (error); 933 } 934 935 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD, 936 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer"); 937 938 static int msgbuf_clear; 939 940 static int 941 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS) 942 { 943 int error; 944 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 945 if (!error && req->newptr) { 946 /* Clear the buffer and reset write pointer */ 947 bzero(msgbufp->msg_ptr, msgbufp->msg_size); 948 msgbufp->msg_bufr = msgbufp->msg_bufx = 0; 949 msgbuf_clear = 0; 950 } 951 return (error); 952 } 953 954 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear, 955 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0, 956 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer"); 957 958 #ifdef DDB 959 960 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf) 961 { 962 int i, j; 963 964 if (!msgbufmapped) { 965 db_printf("msgbuf not mapped yet\n"); 966 return; 967 } 968 db_printf("msgbufp = %p\n", msgbufp); 969 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n", 970 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr, 971 msgbufp->msg_bufx, msgbufp->msg_ptr); 972 for (i = 0; i < msgbufp->msg_size; i++) { 973 j = (i + msgbufp->msg_bufr) % msgbufp->msg_size; 974 db_printf("%c", msgbufp->msg_ptr[j]); 975 } 976 db_printf("\n"); 977 } 978 979 #endif /* DDB */ 980