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