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 * 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: releng/10.2/sys/kern/subr_prf.c 284435 2015-06-16 02:31:11Z ken $");
39
40 #ifdef _KERNEL
41 #include "opt_ddb.h"
42 #include "opt_printf.h"
43 #endif /* _KERNEL */
44
45 #include <sys/param.h>
46 #ifdef _KERNEL
47 #include <sys/systm.h>
48 #include <sys/lock.h>
49 #include <sys/kdb.h>
50 #include <sys/mutex.h>
51 #include <sys/sx.h>
52 #include <sys/kernel.h>
53 #include <sys/msgbuf.h>
54 #include <sys/malloc.h>
55 #include <sys/priv.h>
56 #include <sys/proc.h>
57 #include <sys/stddef.h>
58 #include <sys/sysctl.h>
59 #include <sys/tty.h>
60 #include <sys/syslog.h>
61 #include <sys/cons.h>
62 #include <sys/uio.h>
63 #endif
64 #include <sys/ctype.h>
65 #include <sys/sbuf.h>
66
67 #ifdef DDB
68 #include <ddb/ddb.h>
69 #endif
70
71 /*
72 * Note that stdarg.h and the ANSI style va_start macro is used for both
73 * ANSI and traditional C compilers.
74 */
75 #include <machine/stdarg.h>
76
77 #ifdef _KERNEL
78
79 #define TOCONS 0x01
80 #define TOTTY 0x02
81 #define TOLOG 0x04
82
83 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
84 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
85
86 struct putchar_arg {
87 int flags;
88 int pri;
89 struct tty *tty;
90 char *p_bufr;
91 size_t n_bufr;
92 char *p_next;
93 size_t remain;
94 };
95
96 struct snprintf_arg {
97 char *str;
98 size_t remain;
99 };
100
101 extern int log_open;
102
103 static void msglogchar(int c, int pri);
104 static void msglogstr(char *str, int pri, int filter_cr);
105 static void putchar(int ch, void *arg);
106 static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len, int upper);
107 static void snprintf_func(int ch, void *arg);
108
109 static int msgbufmapped; /* Set when safe to use msgbuf */
110 int msgbuftrigger;
111
112 static int log_console_output = 1;
113 TUNABLE_INT("kern.log_console_output", &log_console_output);
114 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
115 &log_console_output, 0, "Duplicate console output to the syslog.");
116
117 /*
118 * See the comment in log_console() below for more explanation of this.
119 */
120 static int log_console_add_linefeed = 0;
121 TUNABLE_INT("kern.log_console_add_linefeed", &log_console_add_linefeed);
122 SYSCTL_INT(_kern, OID_AUTO, log_console_add_linefeed, CTLFLAG_RW,
123 &log_console_add_linefeed, 0, "log_console() adds extra newlines.");
124
125 static int always_console_output = 0;
126 TUNABLE_INT("kern.always_console_output", &always_console_output);
127 SYSCTL_INT(_kern, OID_AUTO, always_console_output, CTLFLAG_RW,
128 &always_console_output, 0, "Always output to console despite TIOCCONS.");
129
130 /*
131 * Warn that a system table is full.
132 */
133 void
134 tablefull(const char *tab)
135 {
136
137 log(LOG_ERR, "%s: table is full\n", tab);
138 }
139
140 /*
141 * Uprintf prints to the controlling terminal for the current process.
142 */
143 int
144 uprintf(const char *fmt, ...)
145 {
146 va_list ap;
147 struct putchar_arg pca;
148 struct proc *p;
149 struct thread *td;
150 int retval;
151
152 td = curthread;
153 if (TD_IS_IDLETHREAD(td))
154 return (0);
155
156 sx_slock(&proctree_lock);
157 p = td->td_proc;
158 PROC_LOCK(p);
159 if ((p->p_flag & P_CONTROLT) == 0) {
160 PROC_UNLOCK(p);
161 sx_sunlock(&proctree_lock);
162 return (0);
163 }
164 SESS_LOCK(p->p_session);
165 pca.tty = p->p_session->s_ttyp;
166 SESS_UNLOCK(p->p_session);
167 PROC_UNLOCK(p);
168 if (pca.tty == NULL) {
169 sx_sunlock(&proctree_lock);
170 return (0);
171 }
172 pca.flags = TOTTY;
173 pca.p_bufr = NULL;
174 va_start(ap, fmt);
175 tty_lock(pca.tty);
176 sx_sunlock(&proctree_lock);
177 retval = kvprintf(fmt, putchar, &pca, 10, ap);
178 tty_unlock(pca.tty);
179 va_end(ap);
180 return (retval);
181 }
182
183 /*
184 * tprintf and vtprintf print on the controlling terminal associated with the
185 * given session, possibly to the log as well.
186 */
187 void
188 tprintf(struct proc *p, int pri, const char *fmt, ...)
189 {
190 va_list ap;
191
192 va_start(ap, fmt);
193 vtprintf(p, pri, fmt, ap);
194 va_end(ap);
195 }
196
197 void
198 vtprintf(struct proc *p, int pri, const char *fmt, va_list ap)
199 {
200 struct tty *tp = NULL;
201 int flags = 0;
202 struct putchar_arg pca;
203 struct session *sess = NULL;
204
205 sx_slock(&proctree_lock);
206 if (pri != -1)
207 flags |= TOLOG;
208 if (p != NULL) {
209 PROC_LOCK(p);
210 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
211 sess = p->p_session;
212 sess_hold(sess);
213 PROC_UNLOCK(p);
214 tp = sess->s_ttyp;
215 if (tp != NULL && tty_checkoutq(tp))
216 flags |= TOTTY;
217 else
218 tp = NULL;
219 } else
220 PROC_UNLOCK(p);
221 }
222 pca.pri = pri;
223 pca.tty = tp;
224 pca.flags = flags;
225 pca.p_bufr = NULL;
226 if (pca.tty != NULL)
227 tty_lock(pca.tty);
228 sx_sunlock(&proctree_lock);
229 kvprintf(fmt, putchar, &pca, 10, ap);
230 if (pca.tty != NULL)
231 tty_unlock(pca.tty);
232 if (sess != NULL)
233 sess_release(sess);
234 msgbuftrigger = 1;
235 }
236
237 /*
238 * Ttyprintf displays a message on a tty; it should be used only by
239 * the tty driver, or anything that knows the underlying tty will not
240 * be revoke(2)'d away. Other callers should use tprintf.
241 */
242 int
243 ttyprintf(struct tty *tp, const char *fmt, ...)
244 {
245 va_list ap;
246 struct putchar_arg pca;
247 int retval;
248
249 va_start(ap, fmt);
250 pca.tty = tp;
251 pca.flags = TOTTY;
252 pca.p_bufr = NULL;
253 retval = kvprintf(fmt, putchar, &pca, 10, ap);
254 va_end(ap);
255 return (retval);
256 }
257
258 static int
259 _vprintf(int level, int flags, const char *fmt, va_list ap)
260 {
261 struct putchar_arg pca;
262 int retval;
263 #ifdef PRINTF_BUFR_SIZE
264 char bufr[PRINTF_BUFR_SIZE];
265 #endif
266
267 pca.tty = NULL;
268 pca.pri = level;
269 pca.flags = flags;
270 #ifdef PRINTF_BUFR_SIZE
271 pca.p_bufr = bufr;
272 pca.p_next = pca.p_bufr;
273 pca.n_bufr = sizeof(bufr);
274 pca.remain = sizeof(bufr);
275 *pca.p_next = '\0';
276 #else
277 /* Don't buffer console output. */
278 pca.p_bufr = NULL;
279 #endif
280
281 retval = kvprintf(fmt, putchar, &pca, 10, ap);
282
283 #ifdef PRINTF_BUFR_SIZE
284 /* Write any buffered console/log output: */
285 if (*pca.p_bufr != '\0') {
286 if (pca.flags & TOLOG)
287 msglogstr(pca.p_bufr, level, /*filter_cr*/1);
288
289 if (pca.flags & TOCONS)
290 cnputs(pca.p_bufr);
291 }
292 #endif
293
294 return (retval);
295 }
296
297 /*
298 * Log writes to the log buffer, and guarantees not to sleep (so can be
299 * called by interrupt routines). If there is no process reading the
300 * log yet, it writes to the console also.
301 */
302 void
303 log(int level, const char *fmt, ...)
304 {
305 va_list ap;
306
307 va_start(ap, fmt);
308 (void)_vprintf(level, log_open ? TOLOG : TOCONS, fmt, ap);
309 va_end(ap);
310
311 msgbuftrigger = 1;
312 }
313
314 #define CONSCHUNK 128
315
316 void
317 log_console(struct uio *uio)
318 {
319 int c, error, nl;
320 char *consbuffer;
321 int pri;
322
323 if (!log_console_output)
324 return;
325
326 pri = LOG_INFO | LOG_CONSOLE;
327 uio = cloneuio(uio);
328 consbuffer = malloc(CONSCHUNK, M_TEMP, M_WAITOK);
329
330 nl = 0;
331 while (uio->uio_resid > 0) {
332 c = imin(uio->uio_resid, CONSCHUNK - 1);
333 error = uiomove(consbuffer, c, uio);
334 if (error != 0)
335 break;
336 /* Make sure we're NUL-terminated */
337 consbuffer[c] = '\0';
338 if (consbuffer[c - 1] == '\n')
339 nl = 1;
340 else
341 nl = 0;
342 msglogstr(consbuffer, pri, /*filter_cr*/ 1);
343 }
344 /*
345 * The previous behavior in log_console() is preserved when
346 * log_console_add_linefeed is non-zero. For that behavior, if an
347 * individual console write came in that was not terminated with a
348 * line feed, it would add a line feed.
349 *
350 * This results in different data in the message buffer than
351 * appears on the system console (which doesn't add extra line feed
352 * characters).
353 *
354 * A number of programs and rc scripts write a line feed, or a period
355 * and a line feed when they have completed their operation. On
356 * the console, this looks seamless, but when displayed with
357 * 'dmesg -a', you wind up with output that looks like this:
358 *
359 * Updating motd:
360 * .
361 *
362 * On the console, it looks like this:
363 * Updating motd:.
364 *
365 * We could add logic to detect that situation, or just not insert
366 * the extra newlines. Set the kern.log_console_add_linefeed
367 * sysctl/tunable variable to get the old behavior.
368 */
369 if (!nl && log_console_add_linefeed) {
370 consbuffer[0] = '\n';
371 consbuffer[1] = '\0';
372 msglogstr(consbuffer, pri, /*filter_cr*/ 1);
373 }
374 msgbuftrigger = 1;
375 free(uio, M_IOV);
376 free(consbuffer, M_TEMP);
377 return;
378 }
379
380 int
381 printf(const char *fmt, ...)
382 {
383 va_list ap;
384 int retval;
385
386 va_start(ap, fmt);
387 retval = vprintf(fmt, ap);
388 va_end(ap);
389
390 return (retval);
391 }
392
393 int
394 vprintf(const char *fmt, va_list ap)
395 {
396 int retval;
397
398 retval = _vprintf(-1, TOCONS | TOLOG, fmt, ap);
399
400 if (!panicstr)
401 msgbuftrigger = 1;
402
403 return (retval);
404 }
405
406 static void
407 putbuf(int c, struct putchar_arg *ap)
408 {
409 /* Check if no console output buffer was provided. */
410 if (ap->p_bufr == NULL) {
411 /* Output direct to the console. */
412 if (ap->flags & TOCONS)
413 cnputc(c);
414
415 if (ap->flags & TOLOG)
416 msglogchar(c, ap->pri);
417 } else {
418 /* Buffer the character: */
419 *ap->p_next++ = c;
420 ap->remain--;
421
422 /* Always leave the buffer zero terminated. */
423 *ap->p_next = '\0';
424
425 /* Check if the buffer needs to be flushed. */
426 if (ap->remain == 2 || c == '\n') {
427
428 if (ap->flags & TOLOG)
429 msglogstr(ap->p_bufr, ap->pri, /*filter_cr*/1);
430
431 if (ap->flags & TOCONS) {
432 if ((panicstr == NULL) && (constty != NULL))
433 msgbuf_addstr(&consmsgbuf, -1,
434 ap->p_bufr, /*filter_cr*/ 0);
435
436 if ((constty == NULL) ||(always_console_output))
437 cnputs(ap->p_bufr);
438 }
439
440 ap->p_next = ap->p_bufr;
441 ap->remain = ap->n_bufr;
442 *ap->p_next = '\0';
443 }
444
445 /*
446 * Since we fill the buffer up one character at a time,
447 * this should not happen. We should always catch it when
448 * ap->remain == 2 (if not sooner due to a newline), flush
449 * the buffer and move on. One way this could happen is
450 * if someone sets PRINTF_BUFR_SIZE to 1 or something
451 * similarly silly.
452 */
453 KASSERT(ap->remain > 2, ("Bad buffer logic, remain = %zd",
454 ap->remain));
455 }
456 }
457
458 /*
459 * Print a character on console or users terminal. If destination is
460 * the console then the last bunch of characters are saved in msgbuf for
461 * inspection later.
462 */
463 static void
464 putchar(int c, void *arg)
465 {
466 struct putchar_arg *ap = (struct putchar_arg*) arg;
467 struct tty *tp = ap->tty;
468 int flags = ap->flags;
469
470 /* Don't use the tty code after a panic or while in ddb. */
471 if (kdb_active) {
472 if (c != '\0')
473 cnputc(c);
474 return;
475 }
476
477 if ((flags & TOTTY) && tp != NULL && panicstr == NULL)
478 tty_putchar(tp, c);
479
480 if ((flags & (TOCONS | TOLOG)) && c != '\0')
481 putbuf(c, ap);
482 }
483
484 /*
485 * Scaled down version of sprintf(3).
486 */
487 int
488 sprintf(char *buf, const char *cfmt, ...)
489 {
490 int retval;
491 va_list ap;
492
493 va_start(ap, cfmt);
494 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
495 buf[retval] = '\0';
496 va_end(ap);
497 return (retval);
498 }
499
500 /*
501 * Scaled down version of vsprintf(3).
502 */
503 int
504 vsprintf(char *buf, const char *cfmt, va_list ap)
505 {
506 int retval;
507
508 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
509 buf[retval] = '\0';
510 return (retval);
511 }
512
513 /*
514 * Scaled down version of snprintf(3).
515 */
516 int
517 snprintf(char *str, size_t size, const char *format, ...)
518 {
519 int retval;
520 va_list ap;
521
522 va_start(ap, format);
523 retval = vsnprintf(str, size, format, ap);
524 va_end(ap);
525 return(retval);
526 }
527
528 /*
529 * Scaled down version of vsnprintf(3).
530 */
531 int
532 vsnprintf(char *str, size_t size, const char *format, va_list ap)
533 {
534 struct snprintf_arg info;
535 int retval;
536
537 info.str = str;
538 info.remain = size;
539 retval = kvprintf(format, snprintf_func, &info, 10, ap);
540 if (info.remain >= 1)
541 *info.str++ = '\0';
542 return (retval);
543 }
544
545 /*
546 * Kernel version which takes radix argument vsnprintf(3).
547 */
548 int
549 vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
550 {
551 struct snprintf_arg info;
552 int retval;
553
554 info.str = str;
555 info.remain = size;
556 retval = kvprintf(format, snprintf_func, &info, radix, ap);
557 if (info.remain >= 1)
558 *info.str++ = '\0';
559 return (retval);
560 }
561
562 static void
563 snprintf_func(int ch, void *arg)
564 {
565 struct snprintf_arg *const info = arg;
566
567 if (info->remain >= 2) {
568 *info->str++ = ch;
569 info->remain--;
570 }
571 }
572
573 /*
574 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
575 * order; return an optional length and a pointer to the last character
576 * written in the buffer (i.e., the first character of the string).
577 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
578 */
579 static char *
580 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
581 {
582 char *p, c;
583
584 p = nbuf;
585 *p = '\0';
586 do {
587 c = hex2ascii(num % base);
588 *++p = upper ? toupper(c) : c;
589 } while (num /= base);
590 if (lenp)
591 *lenp = p - nbuf;
592 return (p);
593 }
594
595 /*
596 * Scaled down version of printf(3).
597 *
598 * Two additional formats:
599 *
600 * The format %b is supported to decode error registers.
601 * Its usage is:
602 *
603 * printf("reg=%b\n", regval, "<base><arg>*");
604 *
605 * where <base> is the output base expressed as a control character, e.g.
606 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
607 * the first of which gives the bit number to be inspected (origin 1), and
608 * the next characters (up to a control character, i.e. a character <= 32),
609 * give the name of the register. Thus:
610 *
611 * kvprintf("reg=%b\n", 3, "\1\2BITTWO\1BITONE\n");
612 *
613 * would produce output:
614 *
615 * reg=3<BITTWO,BITONE>
616 *
617 * XXX: %D -- Hexdump, takes pointer and separator string:
618 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
619 * ("%*D", len, ptr, " " -> XX XX XX XX ...
620 */
621 int
622 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
623 {
624 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
625 char nbuf[MAXNBUF];
626 char *d;
627 const char *p, *percent, *q;
628 u_char *up;
629 int ch, n;
630 uintmax_t num;
631 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
632 int cflag, hflag, jflag, tflag, zflag;
633 int dwidth, upper;
634 char padc;
635 int stop = 0, retval = 0;
636
637 num = 0;
638 if (!func)
639 d = (char *) arg;
640 else
641 d = NULL;
642
643 if (fmt == NULL)
644 fmt = "(fmt null)\n";
645
646 if (radix < 2 || radix > 36)
647 radix = 10;
648
649 for (;;) {
650 padc = ' ';
651 width = 0;
652 while ((ch = (u_char)*fmt++) != '%' || stop) {
653 if (ch == '\0')
654 return (retval);
655 PCHAR(ch);
656 }
657 percent = fmt - 1;
658 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
659 sign = 0; dot = 0; dwidth = 0; upper = 0;
660 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
661 reswitch: switch (ch = (u_char)*fmt++) {
662 case '.':
663 dot = 1;
664 goto reswitch;
665 case '#':
666 sharpflag = 1;
667 goto reswitch;
668 case '+':
669 sign = 1;
670 goto reswitch;
671 case '-':
672 ladjust = 1;
673 goto reswitch;
674 case '%':
675 PCHAR(ch);
676 break;
677 case '*':
678 if (!dot) {
679 width = va_arg(ap, int);
680 if (width < 0) {
681 ladjust = !ladjust;
682 width = -width;
683 }
684 } else {
685 dwidth = va_arg(ap, int);
686 }
687 goto reswitch;
688 case '':
689 if (!dot) {
690 padc = '';
691 goto reswitch;
692 }
693 case '1': case '2': case '3': case '4':
694 case '5': case '6': case '7': case '8': case '9':
695 for (n = 0;; ++fmt) {
696 n = n * 10 + ch - '';
697 ch = *fmt;
698 if (ch < '' || ch > '9')
699 break;
700 }
701 if (dot)
702 dwidth = n;
703 else
704 width = n;
705 goto reswitch;
706 case 'b':
707 num = (u_int)va_arg(ap, int);
708 p = va_arg(ap, char *);
709 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
710 PCHAR(*q--);
711
712 if (num == 0)
713 break;
714
715 for (tmp = 0; *p;) {
716 n = *p++;
717 if (num & (1 << (n - 1))) {
718 PCHAR(tmp ? ',' : '<');
719 for (; (n = *p) > ' '; ++p)
720 PCHAR(n);
721 tmp = 1;
722 } else
723 for (; *p > ' '; ++p)
724 continue;
725 }
726 if (tmp)
727 PCHAR('>');
728 break;
729 case 'c':
730 PCHAR(va_arg(ap, int));
731 break;
732 case 'D':
733 up = va_arg(ap, u_char *);
734 p = va_arg(ap, char *);
735 if (!width)
736 width = 16;
737 while(width--) {
738 PCHAR(hex2ascii(*up >> 4));
739 PCHAR(hex2ascii(*up & 0x0f));
740 up++;
741 if (width)
742 for (q=p;*q;q++)
743 PCHAR(*q);
744 }
745 break;
746 case 'd':
747 case 'i':
748 base = 10;
749 sign = 1;
750 goto handle_sign;
751 case 'h':
752 if (hflag) {
753 hflag = 0;
754 cflag = 1;
755 } else
756 hflag = 1;
757 goto reswitch;
758 case 'j':
759 jflag = 1;
760 goto reswitch;
761 case 'l':
762 if (lflag) {
763 lflag = 0;
764 qflag = 1;
765 } else
766 lflag = 1;
767 goto reswitch;
768 case 'n':
769 if (jflag)
770 *(va_arg(ap, intmax_t *)) = retval;
771 else if (qflag)
772 *(va_arg(ap, quad_t *)) = retval;
773 else if (lflag)
774 *(va_arg(ap, long *)) = retval;
775 else if (zflag)
776 *(va_arg(ap, size_t *)) = retval;
777 else if (hflag)
778 *(va_arg(ap, short *)) = retval;
779 else if (cflag)
780 *(va_arg(ap, char *)) = retval;
781 else
782 *(va_arg(ap, int *)) = retval;
783 break;
784 case 'o':
785 base = 8;
786 goto handle_nosign;
787 case 'p':
788 base = 16;
789 sharpflag = (width == 0);
790 sign = 0;
791 num = (uintptr_t)va_arg(ap, void *);
792 goto number;
793 case 'q':
794 qflag = 1;
795 goto reswitch;
796 case 'r':
797 base = radix;
798 if (sign)
799 goto handle_sign;
800 goto handle_nosign;
801 case 's':
802 p = va_arg(ap, char *);
803 if (p == NULL)
804 p = "(null)";
805 if (!dot)
806 n = strlen (p);
807 else
808 for (n = 0; n < dwidth && p[n]; n++)
809 continue;
810
811 width -= n;
812
813 if (!ladjust && width > 0)
814 while (width--)
815 PCHAR(padc);
816 while (n--)
817 PCHAR(*p++);
818 if (ladjust && width > 0)
819 while (width--)
820 PCHAR(padc);
821 break;
822 case 't':
823 tflag = 1;
824 goto reswitch;
825 case 'u':
826 base = 10;
827 goto handle_nosign;
828 case 'X':
829 upper = 1;
830 case 'x':
831 base = 16;
832 goto handle_nosign;
833 case 'y':
834 base = 16;
835 sign = 1;
836 goto handle_sign;
837 case 'z':
838 zflag = 1;
839 goto reswitch;
840 handle_nosign:
841 sign = 0;
842 if (jflag)
843 num = va_arg(ap, uintmax_t);
844 else if (qflag)
845 num = va_arg(ap, u_quad_t);
846 else if (tflag)
847 num = va_arg(ap, ptrdiff_t);
848 else if (lflag)
849 num = va_arg(ap, u_long);
850 else if (zflag)
851 num = va_arg(ap, size_t);
852 else if (hflag)
853 num = (u_short)va_arg(ap, int);
854 else if (cflag)
855 num = (u_char)va_arg(ap, int);
856 else
857 num = va_arg(ap, u_int);
858 goto number;
859 handle_sign:
860 if (jflag)
861 num = va_arg(ap, intmax_t);
862 else if (qflag)
863 num = va_arg(ap, quad_t);
864 else if (tflag)
865 num = va_arg(ap, ptrdiff_t);
866 else if (lflag)
867 num = va_arg(ap, long);
868 else if (zflag)
869 num = va_arg(ap, ssize_t);
870 else if (hflag)
871 num = (short)va_arg(ap, int);
872 else if (cflag)
873 num = (char)va_arg(ap, int);
874 else
875 num = va_arg(ap, int);
876 number:
877 if (sign && (intmax_t)num < 0) {
878 neg = 1;
879 num = -(intmax_t)num;
880 }
881 p = ksprintn(nbuf, num, base, &n, upper);
882 tmp = 0;
883 if (sharpflag && num != 0) {
884 if (base == 8)
885 tmp++;
886 else if (base == 16)
887 tmp += 2;
888 }
889 if (neg)
890 tmp++;
891
892 if (!ladjust && padc == '')
893 dwidth = width - tmp;
894 width -= tmp + imax(dwidth, n);
895 dwidth -= n;
896 if (!ladjust)
897 while (width-- > 0)
898 PCHAR(' ');
899 if (neg)
900 PCHAR('-');
901 if (sharpflag && num != 0) {
902 if (base == 8) {
903 PCHAR('');
904 } else if (base == 16) {
905 PCHAR('');
906 PCHAR('x');
907 }
908 }
909 while (dwidth-- > 0)
910 PCHAR('');
911
912 while (*p)
913 PCHAR(*p--);
914
915 if (ladjust)
916 while (width-- > 0)
917 PCHAR(' ');
918
919 break;
920 default:
921 while (percent < fmt)
922 PCHAR(*percent++);
923 /*
924 * Since we ignore a formatting argument it is no
925 * longer safe to obey the remaining formatting
926 * arguments as the arguments will no longer match
927 * the format specs.
928 */
929 stop = 1;
930 break;
931 }
932 }
933 #undef PCHAR
934 }
935
936 /*
937 * Put character in log buffer with a particular priority.
938 */
939 static void
940 msglogchar(int c, int pri)
941 {
942 static int lastpri = -1;
943 static int dangling;
944 char nbuf[MAXNBUF];
945 char *p;
946
947 if (!msgbufmapped)
948 return;
949 if (c == '\0' || c == '\r')
950 return;
951 if (pri != -1 && pri != lastpri) {
952 if (dangling) {
953 msgbuf_addchar(msgbufp, '\n');
954 dangling = 0;
955 }
956 msgbuf_addchar(msgbufp, '<');
957 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
958 msgbuf_addchar(msgbufp, *p--);
959 msgbuf_addchar(msgbufp, '>');
960 lastpri = pri;
961 }
962 msgbuf_addchar(msgbufp, c);
963 if (c == '\n') {
964 dangling = 0;
965 lastpri = -1;
966 } else {
967 dangling = 1;
968 }
969 }
970
971 static void
972 msglogstr(char *str, int pri, int filter_cr)
973 {
974 if (!msgbufmapped)
975 return;
976
977 msgbuf_addstr(msgbufp, pri, str, filter_cr);
978 }
979
980 void
981 msgbufinit(void *ptr, int size)
982 {
983 char *cp;
984 static struct msgbuf *oldp = NULL;
985
986 size -= sizeof(*msgbufp);
987 cp = (char *)ptr;
988 msgbufp = (struct msgbuf *)(cp + size);
989 msgbuf_reinit(msgbufp, cp, size);
990 if (msgbufmapped && oldp != msgbufp)
991 msgbuf_copy(oldp, msgbufp);
992 msgbufmapped = 1;
993 oldp = msgbufp;
994 }
995
996 static int unprivileged_read_msgbuf = 1;
997 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf,
998 CTLFLAG_RW, &unprivileged_read_msgbuf, 0,
999 "Unprivileged processes may read the kernel message buffer");
1000
1001 /* Sysctls for accessing/clearing the msgbuf */
1002 static int
1003 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
1004 {
1005 char buf[128];
1006 u_int seq;
1007 int error, len;
1008
1009 if (!unprivileged_read_msgbuf) {
1010 error = priv_check(req->td, PRIV_MSGBUF);
1011 if (error)
1012 return (error);
1013 }
1014
1015 /* Read the whole buffer, one chunk at a time. */
1016 mtx_lock(&msgbuf_lock);
1017 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
1018 for (;;) {
1019 len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
1020 mtx_unlock(&msgbuf_lock);
1021 if (len == 0)
1022 return (0);
1023
1024 error = sysctl_handle_opaque(oidp, buf, len, req);
1025 if (error)
1026 return (error);
1027
1028 mtx_lock(&msgbuf_lock);
1029 }
1030 }
1031
1032 SYSCTL_PROC(_kern, OID_AUTO, msgbuf,
1033 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1034 NULL, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
1035
1036 static int msgbuf_clearflag;
1037
1038 static int
1039 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
1040 {
1041 int error;
1042 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
1043 if (!error && req->newptr) {
1044 mtx_lock(&msgbuf_lock);
1045 msgbuf_clear(msgbufp);
1046 mtx_unlock(&msgbuf_lock);
1047 msgbuf_clearflag = 0;
1048 }
1049 return (error);
1050 }
1051
1052 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
1053 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE,
1054 &msgbuf_clearflag, 0, sysctl_kern_msgbuf_clear, "I",
1055 "Clear kernel message buffer");
1056
1057 #ifdef DDB
1058
1059 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
1060 {
1061 int i, j;
1062
1063 if (!msgbufmapped) {
1064 db_printf("msgbuf not mapped yet\n");
1065 return;
1066 }
1067 db_printf("msgbufp = %p\n", msgbufp);
1068 db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n",
1069 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq,
1070 msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum);
1071 for (i = 0; i < msgbufp->msg_size && !db_pager_quit; i++) {
1072 j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq);
1073 db_printf("%c", msgbufp->msg_ptr[j]);
1074 }
1075 db_printf("\n");
1076 }
1077
1078 #endif /* DDB */
1079
1080 void
1081 hexdump(const void *ptr, int length, const char *hdr, int flags)
1082 {
1083 int i, j, k;
1084 int cols;
1085 const unsigned char *cp;
1086 char delim;
1087
1088 if ((flags & HD_DELIM_MASK) != 0)
1089 delim = (flags & HD_DELIM_MASK) >> 8;
1090 else
1091 delim = ' ';
1092
1093 if ((flags & HD_COLUMN_MASK) != 0)
1094 cols = flags & HD_COLUMN_MASK;
1095 else
1096 cols = 16;
1097
1098 cp = ptr;
1099 for (i = 0; i < length; i+= cols) {
1100 if (hdr != NULL)
1101 printf("%s", hdr);
1102
1103 if ((flags & HD_OMIT_COUNT) == 0)
1104 printf("%04x ", i);
1105
1106 if ((flags & HD_OMIT_HEX) == 0) {
1107 for (j = 0; j < cols; j++) {
1108 k = i + j;
1109 if (k < length)
1110 printf("%c%02x", delim, cp[k]);
1111 else
1112 printf(" ");
1113 }
1114 }
1115
1116 if ((flags & HD_OMIT_CHARS) == 0) {
1117 printf(" |");
1118 for (j = 0; j < cols; j++) {
1119 k = i + j;
1120 if (k >= length)
1121 printf(" ");
1122 else if (cp[k] >= ' ' && cp[k] <= '~')
1123 printf("%c", cp[k]);
1124 else
1125 printf(".");
1126 }
1127 printf("|");
1128 }
1129 printf("\n");
1130 }
1131 }
1132 #endif /* _KERNEL */
1133
1134 void
1135 sbuf_hexdump(struct sbuf *sb, const void *ptr, int length, const char *hdr,
1136 int flags)
1137 {
1138 int i, j, k;
1139 int cols;
1140 const unsigned char *cp;
1141 char delim;
1142
1143 if ((flags & HD_DELIM_MASK) != 0)
1144 delim = (flags & HD_DELIM_MASK) >> 8;
1145 else
1146 delim = ' ';
1147
1148 if ((flags & HD_COLUMN_MASK) != 0)
1149 cols = flags & HD_COLUMN_MASK;
1150 else
1151 cols = 16;
1152
1153 cp = ptr;
1154 for (i = 0; i < length; i+= cols) {
1155 if (hdr != NULL)
1156 sbuf_printf(sb, "%s", hdr);
1157
1158 if ((flags & HD_OMIT_COUNT) == 0)
1159 sbuf_printf(sb, "%04x ", i);
1160
1161 if ((flags & HD_OMIT_HEX) == 0) {
1162 for (j = 0; j < cols; j++) {
1163 k = i + j;
1164 if (k < length)
1165 sbuf_printf(sb, "%c%02x", delim, cp[k]);
1166 else
1167 sbuf_printf(sb, " ");
1168 }
1169 }
1170
1171 if ((flags & HD_OMIT_CHARS) == 0) {
1172 sbuf_printf(sb, " |");
1173 for (j = 0; j < cols; j++) {
1174 k = i + j;
1175 if (k >= length)
1176 sbuf_printf(sb, " ");
1177 else if (cp[k] >= ' ' && cp[k] <= '~')
1178 sbuf_printf(sb, "%c", cp[k]);
1179 else
1180 sbuf_printf(sb, ".");
1181 }
1182 sbuf_printf(sb, "|");
1183 }
1184 sbuf_printf(sb, "\n");
1185 }
1186 }
1187
Cache object: e5ecb9f4655f71a77303c45cb0d1c768
|