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