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