xref: /freebsd/sys/kern/subr_prf.c (revision 07d90ee0a62110e5161bb0b8a3a0b1b9d2beabad)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1986, 1988, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 #ifdef _KERNEL
39 #include "opt_ddb.h"
40 #include "opt_printf.h"
41 #endif  /* _KERNEL */
42 
43 #include <sys/param.h>
44 #ifdef _KERNEL
45 #include <sys/systm.h>
46 #include <sys/lock.h>
47 #include <sys/kdb.h>
48 #include <sys/mutex.h>
49 #include <sys/sx.h>
50 #include <sys/kernel.h>
51 #include <sys/msgbuf.h>
52 #include <sys/malloc.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/stddef.h>
56 #include <sys/sysctl.h>
57 #include <sys/tslog.h>
58 #include <sys/tty.h>
59 #include <sys/syslog.h>
60 #include <sys/cons.h>
61 #include <sys/uio.h>
62 #else /* !_KERNEL */
63 #include <errno.h>
64 #endif
65 #include <sys/ctype.h>
66 #include <sys/sbuf.h>
67 
68 #ifdef DDB
69 #include <ddb/ddb.h>
70 #endif
71 
72 /*
73  * Note that stdarg.h and the ANSI style va_start macro is used for both
74  * ANSI and traditional C compilers.
75  */
76 #ifdef _KERNEL
77 #include <machine/stdarg.h>
78 #else
79 #include <stdarg.h>
80 #endif
81 
82 /*
83  * This is needed for sbuf_putbuf() when compiled into userland.  Due to the
84  * shared nature of this file, it's the only place to put it.
85  */
86 #ifndef _KERNEL
87 #include <stdio.h>
88 #endif
89 
90 #ifdef _KERNEL
91 
92 #define TOCONS	0x01
93 #define TOTTY	0x02
94 #define TOLOG	0x04
95 
96 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
97 #define MAXNBUF	(sizeof(intmax_t) * NBBY + 1)
98 
99 struct putchar_arg {
100 	int	flags;
101 	int	pri;
102 	struct	tty *tty;
103 	char	*p_bufr;
104 	size_t	n_bufr;
105 	char	*p_next;
106 	size_t	remain;
107 };
108 
109 struct snprintf_arg {
110 	char	*str;
111 	size_t	remain;
112 };
113 
114 extern	int log_open;
115 
116 static void  msglogchar(int c, int pri);
117 static void  msglogstr(char *str, int pri, int filter_cr);
118 static void  prf_putbuf(char *bufr, int flags, int pri);
119 static void  putchar(int ch, void *arg);
120 static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len, int upper);
121 static void  snprintf_func(int ch, void *arg);
122 
123 static bool msgbufmapped;		/* Set when safe to use msgbuf */
124 int msgbuftrigger;
125 struct msgbuf *msgbufp;
126 
127 #ifndef BOOT_TAG_SZ
128 #define	BOOT_TAG_SZ	32
129 #endif
130 #ifndef BOOT_TAG
131 /* Tag used to mark the start of a boot in dmesg */
132 #define	BOOT_TAG	"---<<BOOT>>---"
133 #endif
134 
135 static char current_boot_tag[BOOT_TAG_SZ + 1] = BOOT_TAG;
136 SYSCTL_STRING(_kern, OID_AUTO, boot_tag, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
137     current_boot_tag, 0, "Tag added to dmesg at start of boot");
138 
139 static int log_console_output = 1;
140 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RWTUN,
141     &log_console_output, 0, "Duplicate console output to the syslog");
142 
143 /*
144  * See the comment in log_console() below for more explanation of this.
145  */
146 static int log_console_add_linefeed;
147 SYSCTL_INT(_kern, OID_AUTO, log_console_add_linefeed, CTLFLAG_RWTUN,
148     &log_console_add_linefeed, 0, "log_console() adds extra newlines");
149 
150 static int always_console_output;
151 SYSCTL_INT(_kern, OID_AUTO, always_console_output, CTLFLAG_RWTUN,
152     &always_console_output, 0, "Always output to console despite TIOCCONS");
153 
154 /*
155  * Warn that a system table is full.
156  */
157 void
tablefull(const char * tab)158 tablefull(const char *tab)
159 {
160 
161 	log(LOG_ERR, "%s: table is full\n", tab);
162 }
163 
164 /*
165  * Uprintf prints to the controlling terminal for the current process.
166  */
167 int
uprintf(const char * fmt,...)168 uprintf(const char *fmt, ...)
169 {
170 	va_list ap;
171 	struct putchar_arg pca;
172 	struct proc *p;
173 	struct thread *td;
174 	int retval;
175 
176 	td = curthread;
177 	if (TD_IS_IDLETHREAD(td))
178 		return (0);
179 
180 	if (td->td_proc == initproc) {
181 		/* Produce output when we fail to load /sbin/init: */
182 		va_start(ap, fmt);
183 		retval = vprintf(fmt, ap);
184 		va_end(ap);
185 		return (retval);
186 	}
187 
188 	sx_slock(&proctree_lock);
189 	p = td->td_proc;
190 	PROC_LOCK(p);
191 	if ((p->p_flag & P_CONTROLT) == 0) {
192 		PROC_UNLOCK(p);
193 		sx_sunlock(&proctree_lock);
194 		return (0);
195 	}
196 	SESS_LOCK(p->p_session);
197 	pca.tty = p->p_session->s_ttyp;
198 	SESS_UNLOCK(p->p_session);
199 	PROC_UNLOCK(p);
200 	if (pca.tty == NULL) {
201 		sx_sunlock(&proctree_lock);
202 		return (0);
203 	}
204 	pca.flags = TOTTY;
205 	pca.p_bufr = NULL;
206 	va_start(ap, fmt);
207 	tty_lock(pca.tty);
208 	sx_sunlock(&proctree_lock);
209 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
210 	tty_unlock(pca.tty);
211 	va_end(ap);
212 	return (retval);
213 }
214 
215 /*
216  * tprintf and vtprintf print on the controlling terminal associated with the
217  * given session, possibly to the log as well.
218  */
219 void
tprintf(struct proc * p,int pri,const char * fmt,...)220 tprintf(struct proc *p, int pri, const char *fmt, ...)
221 {
222 	va_list ap;
223 
224 	va_start(ap, fmt);
225 	vtprintf(p, pri, fmt, ap);
226 	va_end(ap);
227 }
228 
229 void
vtprintf(struct proc * p,int pri,const char * fmt,va_list ap)230 vtprintf(struct proc *p, int pri, const char *fmt, va_list ap)
231 {
232 	struct tty *tp = NULL;
233 	int flags = 0;
234 	struct putchar_arg pca;
235 	struct session *sess = NULL;
236 
237 	sx_slock(&proctree_lock);
238 	if (pri != -1)
239 		flags |= TOLOG;
240 	if (p != NULL) {
241 		PROC_LOCK(p);
242 		if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
243 			sess = p->p_session;
244 			sess_hold(sess);
245 			PROC_UNLOCK(p);
246 			tp = sess->s_ttyp;
247 			if (tp != NULL && tty_checkoutq(tp))
248 				flags |= TOTTY;
249 			else
250 				tp = NULL;
251 		} else
252 			PROC_UNLOCK(p);
253 	}
254 	pca.pri = pri;
255 	pca.tty = tp;
256 	pca.flags = flags;
257 	pca.p_bufr = NULL;
258 	if (pca.tty != NULL)
259 		tty_lock(pca.tty);
260 	sx_sunlock(&proctree_lock);
261 	kvprintf(fmt, putchar, &pca, 10, ap);
262 	if (pca.tty != NULL)
263 		tty_unlock(pca.tty);
264 	if (sess != NULL)
265 		sess_release(sess);
266 	msgbuftrigger = 1;
267 }
268 
269 static int
_vprintf(int level,int flags,const char * fmt,va_list ap)270 _vprintf(int level, int flags, const char *fmt, va_list ap)
271 {
272 	struct putchar_arg pca;
273 	int retval;
274 #ifdef PRINTF_BUFR_SIZE
275 	char bufr[PRINTF_BUFR_SIZE];
276 #endif
277 
278 	TSENTER();
279 	pca.tty = NULL;
280 	pca.pri = level;
281 	pca.flags = flags;
282 #ifdef PRINTF_BUFR_SIZE
283 	pca.p_bufr = bufr;
284 	pca.p_next = pca.p_bufr;
285 	pca.n_bufr = sizeof(bufr);
286 	pca.remain = sizeof(bufr);
287 	*pca.p_next = '\0';
288 #else
289 	/* Don't buffer console output. */
290 	pca.p_bufr = NULL;
291 #endif
292 
293 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
294 
295 #ifdef PRINTF_BUFR_SIZE
296 	/* Write any buffered console/log output: */
297 	if (*pca.p_bufr != '\0')
298 		prf_putbuf(pca.p_bufr, flags, level);
299 #endif
300 
301 	TSEXIT();
302 	return (retval);
303 }
304 
305 /*
306  * Log writes to the log buffer, and guarantees not to sleep (so can be
307  * called by interrupt routines).  If there is no process reading the
308  * log yet, it writes to the console also.
309  */
310 void
log(int level,const char * fmt,...)311 log(int level, const char *fmt, ...)
312 {
313 	va_list ap;
314 
315 	va_start(ap, fmt);
316 	vlog(level, fmt, ap);
317 	va_end(ap);
318 }
319 
320 void
vlog(int level,const char * fmt,va_list ap)321 vlog(int level, const char *fmt, va_list ap)
322 {
323 
324 	(void)_vprintf(level, log_open ? TOLOG : TOCONS | TOLOG, fmt, ap);
325 	msgbuftrigger = 1;
326 }
327 
328 #define CONSCHUNK 128
329 
330 void
log_console(struct uio * uio)331 log_console(struct uio *uio)
332 {
333 	int c, error, nl;
334 	char *consbuffer;
335 	int pri;
336 
337 	if (!log_console_output)
338 		return;
339 
340 	pri = LOG_INFO | LOG_CONSOLE;
341 	uio = cloneuio(uio);
342 	consbuffer = malloc(CONSCHUNK, M_TEMP, M_WAITOK);
343 
344 	nl = 0;
345 	while (uio->uio_resid > 0) {
346 		c = imin(uio->uio_resid, CONSCHUNK - 1);
347 		error = uiomove(consbuffer, c, uio);
348 		if (error != 0)
349 			break;
350 		/* Make sure we're NUL-terminated */
351 		consbuffer[c] = '\0';
352 		if (consbuffer[c - 1] == '\n')
353 			nl = 1;
354 		else
355 			nl = 0;
356 		msglogstr(consbuffer, pri, /*filter_cr*/ 1);
357 	}
358 	/*
359 	 * The previous behavior in log_console() is preserved when
360 	 * log_console_add_linefeed is non-zero.  For that behavior, if an
361 	 * individual console write came in that was not terminated with a
362 	 * line feed, it would add a line feed.
363 	 *
364 	 * This results in different data in the message buffer than
365 	 * appears on the system console (which doesn't add extra line feed
366 	 * characters).
367 	 *
368 	 * A number of programs and rc scripts write a line feed, or a period
369 	 * and a line feed when they have completed their operation.  On
370 	 * the console, this looks seamless, but when displayed with
371 	 * 'dmesg -a', you wind up with output that looks like this:
372 	 *
373 	 * Updating motd:
374 	 * .
375 	 *
376 	 * On the console, it looks like this:
377 	 * Updating motd:.
378 	 *
379 	 * We could add logic to detect that situation, or just not insert
380 	 * the extra newlines.  Set the kern.log_console_add_linefeed
381 	 * sysctl/tunable variable to get the old behavior.
382 	 */
383 	if (!nl && log_console_add_linefeed) {
384 		consbuffer[0] = '\n';
385 		consbuffer[1] = '\0';
386 		msglogstr(consbuffer, pri, /*filter_cr*/ 1);
387 	}
388 	msgbuftrigger = 1;
389 	freeuio(uio);
390 	free(consbuffer, M_TEMP);
391 }
392 
393 int
printf(const char * fmt,...)394 printf(const char *fmt, ...)
395 {
396 	va_list ap;
397 	int retval;
398 
399 	va_start(ap, fmt);
400 	retval = vprintf(fmt, ap);
401 	va_end(ap);
402 
403 	return (retval);
404 }
405 
406 int
vprintf(const char * fmt,va_list ap)407 vprintf(const char *fmt, va_list ap)
408 {
409 	int retval;
410 
411 	retval = _vprintf(-1, TOCONS | TOLOG, fmt, ap);
412 
413 	if (!KERNEL_PANICKED())
414 		msgbuftrigger = 1;
415 
416 	return (retval);
417 }
418 
419 static void
prf_putchar(int c,int flags,int pri)420 prf_putchar(int c, int flags, int pri)
421 {
422 
423 	if (flags & TOLOG) {
424 		msglogchar(c, pri);
425 		msgbuftrigger = 1;
426 	}
427 
428 	if (flags & TOCONS) {
429 		if ((!KERNEL_PANICKED()) && (constty != NULL))
430 			msgbuf_addchar(&consmsgbuf, c);
431 
432 		if ((constty == NULL) || always_console_output)
433 			cnputc(c);
434 	}
435 }
436 
437 static void
prf_putbuf(char * bufr,int flags,int pri)438 prf_putbuf(char *bufr, int flags, int pri)
439 {
440 
441 	if (flags & TOLOG) {
442 		msglogstr(bufr, pri, /*filter_cr*/1);
443 		msgbuftrigger = 1;
444 	}
445 
446 	if (flags & TOCONS) {
447 		if ((!KERNEL_PANICKED()) && (constty != NULL))
448 			msgbuf_addstr(&consmsgbuf, -1,
449 			    bufr, /*filter_cr*/ 0);
450 
451 		if ((constty == NULL) || always_console_output)
452 			cnputs(bufr);
453 	}
454 }
455 
456 static void
putbuf(int c,struct putchar_arg * ap)457 putbuf(int c, struct putchar_arg *ap)
458 {
459 	/* Check if no console output buffer was provided. */
460 	if (ap->p_bufr == NULL) {
461 		prf_putchar(c, ap->flags, ap->pri);
462 	} else {
463 		/* Buffer the character: */
464 		*ap->p_next++ = c;
465 		ap->remain--;
466 
467 		/* Always leave the buffer zero terminated. */
468 		*ap->p_next = '\0';
469 
470 		/* Check if the buffer needs to be flushed. */
471 		if (ap->remain == 2 || c == '\n') {
472 			prf_putbuf(ap->p_bufr, ap->flags, ap->pri);
473 
474 			ap->p_next = ap->p_bufr;
475 			ap->remain = ap->n_bufr;
476 			*ap->p_next = '\0';
477 		}
478 
479 		/*
480 		 * Since we fill the buffer up one character at a time,
481 		 * this should not happen.  We should always catch it when
482 		 * ap->remain == 2 (if not sooner due to a newline), flush
483 		 * the buffer and move on.  One way this could happen is
484 		 * if someone sets PRINTF_BUFR_SIZE to 1 or something
485 		 * similarly silly.
486 		 */
487 		KASSERT(ap->remain > 2, ("Bad buffer logic, remain = %zd",
488 		    ap->remain));
489 	}
490 }
491 
492 /*
493  * Print a character on console or users terminal.  If destination is
494  * the console then the last bunch of characters are saved in msgbuf for
495  * inspection later.
496  */
497 static void
putchar(int c,void * arg)498 putchar(int c, void *arg)
499 {
500 	struct putchar_arg *ap = (struct putchar_arg*) arg;
501 	struct tty *tp = ap->tty;
502 	int flags = ap->flags;
503 
504 	/* Don't use the tty code after a panic or while in ddb. */
505 	if (kdb_active) {
506 		if (c != '\0')
507 			cnputc(c);
508 		return;
509 	}
510 
511 	if ((flags & TOTTY) && tp != NULL && !KERNEL_PANICKED())
512 		tty_putchar(tp, c);
513 
514 	if ((flags & (TOCONS | TOLOG)) && c != '\0')
515 		putbuf(c, ap);
516 }
517 
518 /*
519  * Scaled down version of sprintf(3).
520  */
521 int
sprintf(char * buf,const char * cfmt,...)522 sprintf(char *buf, const char *cfmt, ...)
523 {
524 	int retval;
525 	va_list ap;
526 
527 	va_start(ap, cfmt);
528 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
529 	buf[retval] = '\0';
530 	va_end(ap);
531 	return (retval);
532 }
533 
534 /*
535  * Scaled down version of vsprintf(3).
536  */
537 int
vsprintf(char * buf,const char * cfmt,va_list ap)538 vsprintf(char *buf, const char *cfmt, va_list ap)
539 {
540 	int retval;
541 
542 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
543 	buf[retval] = '\0';
544 	return (retval);
545 }
546 
547 /*
548  * Scaled down version of snprintf(3).
549  */
550 int
snprintf(char * str,size_t size,const char * format,...)551 snprintf(char *str, size_t size, const char *format, ...)
552 {
553 	int retval;
554 	va_list ap;
555 
556 	va_start(ap, format);
557 	retval = vsnprintf(str, size, format, ap);
558 	va_end(ap);
559 	return(retval);
560 }
561 
562 /*
563  * Scaled down version of vsnprintf(3).
564  */
565 int
vsnprintf(char * str,size_t size,const char * format,va_list ap)566 vsnprintf(char *str, size_t size, const char *format, va_list ap)
567 {
568 	struct snprintf_arg info;
569 	int retval;
570 
571 	info.str = str;
572 	info.remain = size;
573 	retval = kvprintf(format, snprintf_func, &info, 10, ap);
574 	if (info.remain >= 1)
575 		*info.str++ = '\0';
576 	return (retval);
577 }
578 
579 /*
580  * Kernel version which takes radix argument vsnprintf(3).
581  */
582 int
vsnrprintf(char * str,size_t size,int radix,const char * format,va_list ap)583 vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
584 {
585 	struct snprintf_arg info;
586 	int retval;
587 
588 	info.str = str;
589 	info.remain = size;
590 	retval = kvprintf(format, snprintf_func, &info, radix, ap);
591 	if (info.remain >= 1)
592 		*info.str++ = '\0';
593 	return (retval);
594 }
595 
596 static void
snprintf_func(int ch,void * arg)597 snprintf_func(int ch, void *arg)
598 {
599 	struct snprintf_arg *const info = arg;
600 
601 	if (info->remain >= 2) {
602 		*info->str++ = ch;
603 		info->remain--;
604 	}
605 }
606 
607 /*
608  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
609  * order; return an optional length and a pointer to the last character
610  * written in the buffer (i.e., the first character of the string).
611  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
612  */
613 static char *
ksprintn(char * nbuf,uintmax_t num,int base,int * lenp,int upper)614 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
615 {
616 	char *p, c;
617 
618 	p = nbuf;
619 	*p = '\0';
620 	do {
621 		c = hex2ascii(num % base);
622 		*++p = upper ? toupper(c) : c;
623 	} while (num /= base);
624 	if (lenp)
625 		*lenp = p - nbuf;
626 	return (p);
627 }
628 
629 /*
630  * Scaled down version of printf(3).
631  *
632  * Two additional formats:
633  *
634  * The format %b is supported to decode error registers.
635  * Its usage is:
636  *
637  *	printf("reg=%b\n", regval, "<base><arg>*");
638  *
639  * where <base> is the output base expressed as a control character, e.g.
640  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
641  * the first of which gives the bit number to be inspected (origin 1), and
642  * the next characters (up to a control character, i.e. a character <= 32),
643  * give the name of the register.  Thus:
644  *
645  *	kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE");
646  *
647  * would produce output:
648  *
649  *	reg=3<BITTWO,BITONE>
650  *
651  * XXX:  %D  -- Hexdump, takes pointer and separator string:
652  *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
653  *		("%*D", len, ptr, " " -> XX XX XX XX ...
654  */
655 int
kvprintf(char const * fmt,void (* func)(int,void *),void * arg,int radix,va_list ap)656 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
657 {
658 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
659 	char nbuf[MAXNBUF];
660 	char *d;
661 	const char *p, *percent, *q;
662 	u_char *up;
663 	int ch, n, sign;
664 	uintmax_t num;
665 	int base, lflag, qflag, tmp, width, ladjust, sharpflag, dot;
666 	int cflag, hflag, jflag, tflag, zflag;
667 	int bconv, dwidth, upper;
668 	char padc;
669 	int stop = 0, retval = 0;
670 
671 	num = 0;
672 	q = NULL;
673 	if (!func)
674 		d = (char *) arg;
675 	else
676 		d = NULL;
677 
678 	if (fmt == NULL)
679 		fmt = "(fmt null)\n";
680 
681 	if (radix < 2 || radix > 36)
682 		radix = 10;
683 
684 	for (;;) {
685 		padc = ' ';
686 		width = 0;
687 		while ((ch = (u_char)*fmt++) != '%' || stop) {
688 			if (ch == '\0')
689 				return (retval);
690 			PCHAR(ch);
691 		}
692 		percent = fmt - 1;
693 		qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0;
694 		sign = 0; dot = 0; bconv = 0; dwidth = 0; upper = 0;
695 		cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
696 reswitch:	switch (ch = (u_char)*fmt++) {
697 		case '.':
698 			dot = 1;
699 			goto reswitch;
700 		case '#':
701 			sharpflag = 1;
702 			goto reswitch;
703 		case '+':
704 			sign = '+';
705 			goto reswitch;
706 		case '-':
707 			ladjust = 1;
708 			goto reswitch;
709 		case '%':
710 			PCHAR(ch);
711 			break;
712 		case '*':
713 			if (!dot) {
714 				width = va_arg(ap, int);
715 				if (width < 0) {
716 					ladjust = !ladjust;
717 					width = -width;
718 				}
719 			} else {
720 				dwidth = va_arg(ap, int);
721 			}
722 			goto reswitch;
723 		case '0':
724 			if (!dot) {
725 				padc = '0';
726 				goto reswitch;
727 			}
728 			/* FALLTHROUGH */
729 		case '1': case '2': case '3': case '4':
730 		case '5': case '6': case '7': case '8': case '9':
731 				for (n = 0;; ++fmt) {
732 					n = n * 10 + ch - '0';
733 					ch = *fmt;
734 					if (ch < '0' || ch > '9')
735 						break;
736 				}
737 			if (dot)
738 				dwidth = n;
739 			else
740 				width = n;
741 			goto reswitch;
742 		case 'b':
743 			ladjust = 1;
744 			bconv = 1;
745 			goto handle_nosign;
746 		case 'c':
747 			width -= 1;
748 
749 			if (!ladjust && width > 0)
750 				while (width--)
751 					PCHAR(padc);
752 			PCHAR(va_arg(ap, int));
753 			if (ladjust && width > 0)
754 				while (width--)
755 					PCHAR(padc);
756 			break;
757 		case 'D':
758 			up = va_arg(ap, u_char *);
759 			p = va_arg(ap, char *);
760 			if (!width)
761 				width = 16;
762 			while(width--) {
763 				PCHAR(hex2ascii(*up >> 4));
764 				PCHAR(hex2ascii(*up & 0x0f));
765 				up++;
766 				if (width)
767 					for (q=p;*q;q++)
768 						PCHAR(*q);
769 			}
770 			break;
771 		case 'd':
772 		case 'i':
773 			base = 10;
774 			goto handle_sign;
775 		case 'h':
776 			if (hflag) {
777 				hflag = 0;
778 				cflag = 1;
779 			} else
780 				hflag = 1;
781 			goto reswitch;
782 		case 'j':
783 			jflag = 1;
784 			goto reswitch;
785 		case 'l':
786 			if (lflag) {
787 				lflag = 0;
788 				qflag = 1;
789 			} else
790 				lflag = 1;
791 			goto reswitch;
792 		case 'n':
793 			/*
794 			 * We do not support %n in kernel, but consume the
795 			 * argument.
796 			 */
797 			if (jflag)
798 				(void)va_arg(ap, intmax_t *);
799 			else if (qflag)
800 				(void)va_arg(ap, quad_t *);
801 			else if (lflag)
802 				(void)va_arg(ap, long *);
803 			else if (zflag)
804 				(void)va_arg(ap, size_t *);
805 			else if (hflag)
806 				(void)va_arg(ap, short *);
807 			else if (cflag)
808 				(void)va_arg(ap, char *);
809 			else
810 				(void)va_arg(ap, int *);
811 			break;
812 		case 'o':
813 			base = 8;
814 			goto handle_nosign;
815 		case 'p':
816 			base = 16;
817 			sharpflag = (width == 0);
818 			sign = 0;
819 			num = (uintptr_t)va_arg(ap, void *);
820 			goto number;
821 		case 'q':
822 			qflag = 1;
823 			goto reswitch;
824 		case 'r':
825 			base = radix;
826 			if (sign) {
827 				sign = 0;
828 				goto handle_sign;
829 			}
830 			goto handle_nosign;
831 		case 's':
832 			p = va_arg(ap, char *);
833 			if (p == NULL)
834 				p = "(null)";
835 			if (!dot)
836 				n = strlen (p);
837 			else
838 				for (n = 0; n < dwidth && p[n]; n++)
839 					continue;
840 
841 			width -= n;
842 
843 			if (!ladjust && width > 0)
844 				while (width--)
845 					PCHAR(padc);
846 			while (n--)
847 				PCHAR(*p++);
848 			if (ladjust && width > 0)
849 				while (width--)
850 					PCHAR(padc);
851 			break;
852 		case 't':
853 			tflag = 1;
854 			goto reswitch;
855 		case 'u':
856 			base = 10;
857 			goto handle_nosign;
858 		case 'X':
859 			upper = 1;
860 			/* FALLTHROUGH */
861 		case 'x':
862 			base = 16;
863 			goto handle_nosign;
864 		case 'y':
865 			base = 16;
866 			goto handle_sign;
867 		case 'z':
868 			zflag = 1;
869 			goto reswitch;
870 handle_nosign:
871 			if (jflag)
872 				num = va_arg(ap, uintmax_t);
873 			else if (qflag)
874 				num = va_arg(ap, u_quad_t);
875 			else if (tflag)
876 				num = va_arg(ap, ptrdiff_t);
877 			else if (lflag)
878 				num = va_arg(ap, u_long);
879 			else if (zflag)
880 				num = va_arg(ap, size_t);
881 			else if (hflag)
882 				num = (u_short)va_arg(ap, int);
883 			else if (cflag)
884 				num = (u_char)va_arg(ap, int);
885 			else
886 				num = va_arg(ap, u_int);
887 			if (bconv) {
888 				q = va_arg(ap, char *);
889 				base = *q++;
890 			}
891 			goto number;
892 handle_sign:
893 			if (jflag)
894 				num = va_arg(ap, intmax_t);
895 			else if (qflag)
896 				num = va_arg(ap, quad_t);
897 			else if (tflag)
898 				num = va_arg(ap, ptrdiff_t);
899 			else if (lflag)
900 				num = va_arg(ap, long);
901 			else if (zflag)
902 				num = va_arg(ap, ssize_t);
903 			else if (hflag)
904 				num = (short)va_arg(ap, int);
905 			else if (cflag)
906 				num = (signed char)va_arg(ap, int);
907 			else
908 				num = va_arg(ap, int);
909 			if ((intmax_t)num < 0) {
910 				sign = '-';
911 				num = -(intmax_t)num;
912 			}
913 number:
914 			p = ksprintn(nbuf, num, base, &n, upper);
915 			tmp = 0;
916 			if (sharpflag && num != 0) {
917 				if (base == 8)
918 					tmp++;
919 				else if (base == 16)
920 					tmp += 2;
921 			}
922 			if (sign)
923 				tmp++;
924 
925 			if (!ladjust && padc == '0')
926 				dwidth = width - tmp;
927 			width -= tmp + imax(dwidth, n);
928 			dwidth -= n;
929 			if (!ladjust)
930 				while (width-- > 0)
931 					PCHAR(' ');
932 			if (sign)
933 				PCHAR(sign);
934 			if (sharpflag && num != 0) {
935 				if (base == 8) {
936 					PCHAR('0');
937 				} else if (base == 16) {
938 					PCHAR('0');
939 					PCHAR('x');
940 				}
941 			}
942 			while (dwidth-- > 0)
943 				PCHAR('0');
944 
945 			while (*p)
946 				PCHAR(*p--);
947 
948 			if (bconv && num != 0) {
949 				/* %b conversion flag format. */
950 				tmp = retval;
951 				while (*q) {
952 					n = *q++;
953 					if (num & (1 << (n - 1))) {
954 						PCHAR(retval != tmp ?
955 						    ',' : '<');
956 						for (; (n = *q) > ' '; ++q)
957 							PCHAR(n);
958 					} else
959 						for (; *q > ' '; ++q)
960 							continue;
961 				}
962 				if (retval != tmp) {
963 					PCHAR('>');
964 					width -= retval - tmp;
965 				}
966 			}
967 
968 			if (ladjust)
969 				while (width-- > 0)
970 					PCHAR(' ');
971 
972 			break;
973 		default:
974 			while (percent < fmt)
975 				PCHAR(*percent++);
976 			/*
977 			 * Since we ignore a formatting argument it is no
978 			 * longer safe to obey the remaining formatting
979 			 * arguments as the arguments will no longer match
980 			 * the format specs.
981 			 */
982 			stop = 1;
983 			break;
984 		}
985 	}
986 #undef PCHAR
987 }
988 
989 /*
990  * Put character in log buffer with a particular priority.
991  */
992 static void
msglogchar(int c,int pri)993 msglogchar(int c, int pri)
994 {
995 	static int lastpri = -1;
996 	static int dangling;
997 	char nbuf[MAXNBUF];
998 	char *p;
999 
1000 	if (!msgbufmapped)
1001 		return;
1002 	if (c == '\0' || c == '\r')
1003 		return;
1004 	if (pri != -1 && pri != lastpri) {
1005 		if (dangling) {
1006 			msgbuf_addchar(msgbufp, '\n');
1007 			dangling = 0;
1008 		}
1009 		msgbuf_addchar(msgbufp, '<');
1010 		for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
1011 			msgbuf_addchar(msgbufp, *p--);
1012 		msgbuf_addchar(msgbufp, '>');
1013 		lastpri = pri;
1014 	}
1015 	msgbuf_addchar(msgbufp, c);
1016 	if (c == '\n') {
1017 		dangling = 0;
1018 		lastpri = -1;
1019 	} else {
1020 		dangling = 1;
1021 	}
1022 }
1023 
1024 static void
msglogstr(char * str,int pri,int filter_cr)1025 msglogstr(char *str, int pri, int filter_cr)
1026 {
1027 	if (!msgbufmapped)
1028 		return;
1029 
1030 	msgbuf_addstr(msgbufp, pri, str, filter_cr);
1031 }
1032 
1033 void
msgbufinit(void * ptr,int size)1034 msgbufinit(void *ptr, int size)
1035 {
1036 	char *cp;
1037 	static struct msgbuf *oldp = NULL;
1038 	bool print_boot_tag;
1039 
1040 	TSENTER();
1041 	size -= sizeof(*msgbufp);
1042 	cp = (char *)ptr;
1043 	print_boot_tag = !msgbufmapped;
1044 	/* Attempt to fetch kern.boot_tag tunable on first mapping */
1045 	if (!msgbufmapped)
1046 		TUNABLE_STR_FETCH("kern.boot_tag", current_boot_tag,
1047 		    sizeof(current_boot_tag));
1048 	msgbufp = (struct msgbuf *)(cp + size);
1049 	msgbuf_reinit(msgbufp, cp, size);
1050 	if (msgbufmapped && oldp != msgbufp)
1051 		msgbuf_copy(oldp, msgbufp);
1052 	msgbufmapped = true;
1053 	if (print_boot_tag && *current_boot_tag != '\0')
1054 		printf("%s\n", current_boot_tag);
1055 	oldp = msgbufp;
1056 	TSEXIT();
1057 }
1058 
1059 /* Sysctls for accessing/clearing the msgbuf */
1060 static int
sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)1061 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
1062 {
1063 	char buf[128], *bp;
1064 	u_int seq;
1065 	int error, len;
1066 	bool wrap;
1067 
1068 	error = priv_check(req->td, PRIV_MSGBUF);
1069 	if (error)
1070 		return (error);
1071 
1072 	/* Read the whole buffer, one chunk at a time. */
1073 	mtx_lock(&msgbuf_lock);
1074 	msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
1075 	wrap = (seq != 0);
1076 	for (;;) {
1077 		len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
1078 		mtx_unlock(&msgbuf_lock);
1079 		if (len == 0)
1080 			return (SYSCTL_OUT(req, "", 1)); /* add nulterm */
1081 		if (wrap) {
1082 			/* Skip the first line, as it is probably incomplete. */
1083 			bp = memchr(buf, '\n', len);
1084 			if (bp == NULL) {
1085 				mtx_lock(&msgbuf_lock);
1086 				continue;
1087 			}
1088 			wrap = false;
1089 			bp++;
1090 			len -= bp - buf;
1091 			if (len == 0) {
1092 				mtx_lock(&msgbuf_lock);
1093 				continue;
1094 			}
1095 		} else
1096 			bp = buf;
1097 		error = sysctl_handle_opaque(oidp, bp, len, req);
1098 		if (error)
1099 			return (error);
1100 
1101 		mtx_lock(&msgbuf_lock);
1102 	}
1103 }
1104 
1105 SYSCTL_PROC(_kern, OID_AUTO, msgbuf,
1106     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1107     NULL, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
1108 
1109 static int msgbuf_clearflag;
1110 
1111 static int
sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)1112 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
1113 {
1114 	int error;
1115 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
1116 	if (!error && req->newptr) {
1117 		mtx_lock(&msgbuf_lock);
1118 		msgbuf_clear(msgbufp);
1119 		mtx_unlock(&msgbuf_lock);
1120 		msgbuf_clearflag = 0;
1121 	}
1122 	return (error);
1123 }
1124 
1125 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
1126     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE,
1127     &msgbuf_clearflag, 0, sysctl_kern_msgbuf_clear, "I",
1128     "Clear kernel message buffer");
1129 
1130 #ifdef DDB
1131 
DB_SHOW_COMMAND_FLAGS(msgbuf,db_show_msgbuf,DB_CMD_MEMSAFE)1132 DB_SHOW_COMMAND_FLAGS(msgbuf, db_show_msgbuf, DB_CMD_MEMSAFE)
1133 {
1134 	int i, j;
1135 
1136 	if (!msgbufmapped) {
1137 		db_printf("msgbuf not mapped yet\n");
1138 		return;
1139 	}
1140 	db_printf("msgbufp = %p\n", msgbufp);
1141 	db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n",
1142 	    msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq,
1143 	    msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum);
1144 	for (i = 0; i < msgbufp->msg_size && !db_pager_quit; i++) {
1145 		j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq);
1146 		db_printf("%c", msgbufp->msg_ptr[j]);
1147 	}
1148 	db_printf("\n");
1149 }
1150 
1151 #endif /* DDB */
1152 
1153 void
hexdump(const void * ptr,int length,const char * hdr,int flags)1154 hexdump(const void *ptr, int length, const char *hdr, int flags)
1155 {
1156 	int i, j, k;
1157 	int cols;
1158 	const unsigned char *cp;
1159 	char delim;
1160 
1161 	if ((flags & HD_DELIM_MASK) != 0)
1162 		delim = (flags & HD_DELIM_MASK) >> 8;
1163 	else
1164 		delim = ' ';
1165 
1166 	if ((flags & HD_COLUMN_MASK) != 0)
1167 		cols = flags & HD_COLUMN_MASK;
1168 	else
1169 		cols = 16;
1170 
1171 	cp = ptr;
1172 	for (i = 0; i < length; i+= cols) {
1173 		if (hdr != NULL)
1174 			printf("%s", hdr);
1175 
1176 		if ((flags & HD_OMIT_COUNT) == 0)
1177 			printf("%04x  ", i);
1178 
1179 		if ((flags & HD_OMIT_HEX) == 0) {
1180 			for (j = 0; j < cols; j++) {
1181 				k = i + j;
1182 				if (k < length)
1183 					printf("%c%02x", delim, cp[k]);
1184 				else
1185 					printf("   ");
1186 			}
1187 		}
1188 
1189 		if ((flags & HD_OMIT_CHARS) == 0) {
1190 			printf("  |");
1191 			for (j = 0; j < cols; j++) {
1192 				k = i + j;
1193 				if (k >= length)
1194 					printf(" ");
1195 				else if (cp[k] >= ' ' && cp[k] <= '~')
1196 					printf("%c", cp[k]);
1197 				else
1198 					printf(".");
1199 			}
1200 			printf("|");
1201 		}
1202 		printf("\n");
1203 	}
1204 }
1205 #endif /* _KERNEL */
1206 
1207 void
sbuf_hexdump(struct sbuf * sb,const void * ptr,int length,const char * hdr,int flags)1208 sbuf_hexdump(struct sbuf *sb, const void *ptr, int length, const char *hdr,
1209 	     int flags)
1210 {
1211 	int i, j, k;
1212 	int cols;
1213 	const unsigned char *cp;
1214 	char delim;
1215 
1216 	if ((flags & HD_DELIM_MASK) != 0)
1217 		delim = (flags & HD_DELIM_MASK) >> 8;
1218 	else
1219 		delim = ' ';
1220 
1221 	if ((flags & HD_COLUMN_MASK) != 0)
1222 		cols = flags & HD_COLUMN_MASK;
1223 	else
1224 		cols = 16;
1225 
1226 	cp = ptr;
1227 	for (i = 0; i < length; i+= cols) {
1228 		if (hdr != NULL)
1229 			sbuf_printf(sb, "%s", hdr);
1230 
1231 		if ((flags & HD_OMIT_COUNT) == 0)
1232 			sbuf_printf(sb, "%04x  ", i);
1233 
1234 		if ((flags & HD_OMIT_HEX) == 0) {
1235 			for (j = 0; j < cols; j++) {
1236 				k = i + j;
1237 				if (k < length)
1238 					sbuf_printf(sb, "%c%02x", delim, cp[k]);
1239 				else
1240 					sbuf_cat(sb, "   ");
1241 			}
1242 		}
1243 
1244 		if ((flags & HD_OMIT_CHARS) == 0) {
1245 			sbuf_cat(sb, "  |");
1246 			for (j = 0; j < cols; j++) {
1247 				k = i + j;
1248 				if (k >= length)
1249 					sbuf_putc(sb, ' ');
1250 				else if (cp[k] >= ' ' && cp[k] <= '~')
1251 					sbuf_putc(sb, cp[k]);
1252 				else
1253 					sbuf_putc(sb, '.');
1254 			}
1255 			sbuf_putc(sb, '|');
1256 		}
1257 		sbuf_putc(sb, '\n');
1258 	}
1259 }
1260 
1261 #ifdef _KERNEL
1262 void
counted_warning(unsigned * counter,const char * msg)1263 counted_warning(unsigned *counter, const char *msg)
1264 {
1265 	struct thread *td;
1266 	unsigned c;
1267 
1268 	for (;;) {
1269 		c = *counter;
1270 		if (c == 0)
1271 			break;
1272 		if (atomic_cmpset_int(counter, c, c - 1)) {
1273 			td = curthread;
1274 			log(LOG_INFO, "pid %d (%s) %s%s\n",
1275 			    td->td_proc->p_pid, td->td_name, msg,
1276 			    c > 1 ? "" : " - not logging anymore");
1277 			break;
1278 		}
1279 	}
1280 }
1281 #endif
1282 
1283 #ifdef _KERNEL
1284 void
sbuf_putbuf(struct sbuf * sb)1285 sbuf_putbuf(struct sbuf *sb)
1286 {
1287 
1288 	prf_putbuf(sbuf_data(sb), TOLOG | TOCONS, -1);
1289 }
1290 #else
1291 void
sbuf_putbuf(struct sbuf * sb)1292 sbuf_putbuf(struct sbuf *sb)
1293 {
1294 
1295 	printf("%s", sbuf_data(sb));
1296 }
1297 #endif
1298 
1299 int
sbuf_printf_drain(void * arg,const char * data,int len)1300 sbuf_printf_drain(void *arg, const char *data, int len)
1301 {
1302 	size_t *retvalptr;
1303 	int r;
1304 #ifdef _KERNEL
1305 	char *dataptr;
1306 	char oldchr;
1307 
1308 	/*
1309 	 * This is allowed as an extra byte is always resvered for
1310 	 * terminating NUL byte.  Save and restore the byte because
1311 	 * we might be flushing a record, and there may be valid
1312 	 * data after the buffer.
1313 	 */
1314 	oldchr = data[len];
1315 	dataptr = __DECONST(char *, data);
1316 	dataptr[len] = '\0';
1317 
1318 	prf_putbuf(dataptr, TOLOG | TOCONS, -1);
1319 	r = len;
1320 
1321 	dataptr[len] = oldchr;
1322 
1323 #else /* !_KERNEL */
1324 
1325 	r = printf("%.*s", len, data);
1326 	if (r < 0)
1327 		return (-errno);
1328 
1329 #endif
1330 
1331 	retvalptr = arg;
1332 	if (retvalptr != NULL)
1333 		*retvalptr += r;
1334 
1335 	return (r);
1336 }
1337