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