xref: /freebsd/sys/kern/subr_prf.c (revision d086ded32300bc0f33fb1574d0bcfccfbc60881d)
1 /*-
2  * Copyright (c) 1986, 1988, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_ddb.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/sx.h>
51 #include <sys/kernel.h>
52 #include <sys/msgbuf.h>
53 #include <sys/malloc.h>
54 #include <sys/proc.h>
55 #include <sys/stddef.h>
56 #include <sys/sysctl.h>
57 #include <sys/tty.h>
58 #include <sys/syslog.h>
59 #include <sys/cons.h>
60 #include <sys/uio.h>
61 
62 #ifdef DDB
63 #include <ddb/ddb.h>
64 #endif
65 
66 /*
67  * Note that stdarg.h and the ANSI style va_start macro is used for both
68  * ANSI and traditional C compilers.
69  */
70 #include <machine/stdarg.h>
71 
72 #define TOCONS	0x01
73 #define TOTTY	0x02
74 #define TOLOG	0x04
75 
76 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
77 #define MAXNBUF	(sizeof(intmax_t) * NBBY + 1)
78 
79 struct putchar_arg {
80 	int	flags;
81 	int	pri;
82 	struct	tty *tty;
83 };
84 
85 struct snprintf_arg {
86 	char	*str;
87 	size_t	remain;
88 };
89 
90 extern	int log_open;
91 
92 struct	tty *constty;			/* pointer to console "window" tty */
93 
94 static void (*v_putc)(int) = cnputc;	/* routine to putc on virtual console */
95 static void  msglogchar(int c, int pri);
96 static void  msgaddchar(int c, void *dummy);
97 static u_int msgbufcksum(char *cp, size_t size, u_int cksum);
98 static void  putchar(int ch, void *arg);
99 static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len);
100 static void  snprintf_func(int ch, void *arg);
101 
102 static int consintr = 1;		/* Ok to handle console interrupts? */
103 static int msgbufmapped;		/* Set when safe to use msgbuf */
104 int msgbuftrigger;
105 
106 static int      log_console_output = 1;
107 TUNABLE_INT("kern.log_console_output", &log_console_output);
108 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
109     &log_console_output, 0, "");
110 
111 /*
112  * Warn that a system table is full.
113  */
114 void
115 tablefull(const char *tab)
116 {
117 
118 	log(LOG_ERR, "%s: table is full\n", tab);
119 }
120 
121 /*
122  * Uprintf prints to the controlling terminal for the current process.
123  * It may block if the tty queue is overfull.  No message is printed if
124  * the queue does not clear in a reasonable time.
125  */
126 int
127 uprintf(const char *fmt, ...)
128 {
129 	struct thread *td = curthread;
130 	struct proc *p = td->td_proc;
131 	va_list ap;
132 	struct putchar_arg pca;
133 	int retval;
134 
135 	if (td == NULL || td == PCPU_GET(idlethread))
136 		return (0);
137 
138 	p = td->td_proc;
139 	PROC_LOCK(p);
140 	if ((p->p_flag & P_CONTROLT) == 0) {
141 		PROC_UNLOCK(p);
142 		return (0);
143 	}
144 	SESS_LOCK(p->p_session);
145 	pca.tty = p->p_session->s_ttyp;
146 	SESS_UNLOCK(p->p_session);
147 	PROC_UNLOCK(p);
148 	if (pca.tty == NULL)
149 		return (0);
150 	pca.flags = TOTTY;
151 	va_start(ap, fmt);
152 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
153 	va_end(ap);
154 
155 	return (retval);
156 }
157 
158 /*
159  * tprintf prints on the controlling terminal associated
160  * with the given session, possibly to the log as well.
161  */
162 void
163 tprintf(struct proc *p, int pri, const char *fmt, ...)
164 {
165 	struct tty *tp = NULL;
166 	int flags = 0;
167 	va_list ap;
168 	struct putchar_arg pca;
169 	struct session *sess = NULL;
170 
171 	if (pri != -1)
172 		flags |= TOLOG;
173 	if (p != NULL) {
174 		PROC_LOCK(p);
175 		if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
176 			sess = p->p_session;
177 			SESS_LOCK(sess);
178 			PROC_UNLOCK(p);
179 			SESSHOLD(sess);
180 			tp = sess->s_ttyp;
181 			SESS_UNLOCK(sess);
182 			if (ttycheckoutq(tp, 0))
183 				flags |= TOTTY;
184 			else
185 				tp = NULL;
186 		} else
187 			PROC_UNLOCK(p);
188 	}
189 	pca.pri = pri;
190 	pca.tty = tp;
191 	pca.flags = flags;
192 	va_start(ap, fmt);
193 	kvprintf(fmt, putchar, &pca, 10, ap);
194 	va_end(ap);
195 	if (sess != NULL) {
196 		SESS_LOCK(sess);
197 		SESSRELE(sess);
198 		SESS_UNLOCK(sess);
199 	}
200 	msgbuftrigger = 1;
201 }
202 
203 /*
204  * Ttyprintf displays a message on a tty; it should be used only by
205  * the tty driver, or anything that knows the underlying tty will not
206  * be revoke(2)'d away.  Other callers should use tprintf.
207  */
208 int
209 ttyprintf(struct tty *tp, const char *fmt, ...)
210 {
211 	va_list ap;
212 	struct putchar_arg pca;
213 	int retval;
214 
215 	va_start(ap, fmt);
216 	pca.tty = tp;
217 	pca.flags = TOTTY;
218 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
219 	va_end(ap);
220 	return (retval);
221 }
222 
223 /*
224  * Log writes to the log buffer, and guarantees not to sleep (so can be
225  * called by interrupt routines).  If there is no process reading the
226  * log yet, it writes to the console also.
227  */
228 void
229 log(int level, const char *fmt, ...)
230 {
231 	va_list ap;
232 	struct putchar_arg pca;
233 
234 	pca.tty = NULL;
235 	pca.pri = level;
236 	pca.flags = log_open ? TOLOG : TOCONS;
237 
238 	va_start(ap, fmt);
239 	kvprintf(fmt, putchar, &pca, 10, ap);
240 	va_end(ap);
241 
242 	msgbuftrigger = 1;
243 }
244 
245 #define CONSCHUNK 128
246 
247 void
248 log_console(struct uio *uio)
249 {
250 	int c, i, error, iovlen, nl;
251 	struct uio muio;
252 	struct iovec *miov = NULL;
253 	char *consbuffer;
254 	int pri;
255 
256 	if (!log_console_output)
257 		return;
258 
259 	pri = LOG_INFO | LOG_CONSOLE;
260 	muio = *uio;
261 	iovlen = uio->uio_iovcnt * sizeof (struct iovec);
262 	MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
263 	MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
264 	bcopy(muio.uio_iov, miov, iovlen);
265 	muio.uio_iov = miov;
266 	uio = &muio;
267 
268 	nl = 0;
269 	while (uio->uio_resid > 0) {
270 		c = imin(uio->uio_resid, CONSCHUNK);
271 		error = uiomove(consbuffer, c, uio);
272 		if (error != 0)
273 			break;
274 		for (i = 0; i < c; i++) {
275 			msglogchar(consbuffer[i], pri);
276 			if (consbuffer[i] == '\n')
277 				nl = 1;
278 			else
279 				nl = 0;
280 		}
281 	}
282 	if (!nl)
283 		msglogchar('\n', pri);
284 	msgbuftrigger = 1;
285 	FREE(miov, M_TEMP);
286 	FREE(consbuffer, M_TEMP);
287 	return;
288 }
289 
290 int
291 printf(const char *fmt, ...)
292 {
293 	va_list ap;
294 	int savintr;
295 	struct putchar_arg pca;
296 	int retval;
297 
298 	savintr = consintr;		/* disable interrupts */
299 	consintr = 0;
300 	va_start(ap, fmt);
301 	pca.tty = NULL;
302 	pca.flags = TOCONS | TOLOG;
303 	pca.pri = -1;
304 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
305 	va_end(ap);
306 	if (!panicstr)
307 		msgbuftrigger = 1;
308 	consintr = savintr;		/* reenable interrupts */
309 	return (retval);
310 }
311 
312 int
313 vprintf(const char *fmt, va_list ap)
314 {
315 	int savintr;
316 	struct putchar_arg pca;
317 	int retval;
318 
319 	savintr = consintr;		/* disable interrupts */
320 	consintr = 0;
321 	pca.tty = NULL;
322 	pca.flags = TOCONS | TOLOG;
323 	pca.pri = -1;
324 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
325 	if (!panicstr)
326 		msgbuftrigger = 1;
327 	consintr = savintr;		/* reenable interrupts */
328 	return (retval);
329 }
330 
331 /*
332  * Print a character on console or users terminal.  If destination is
333  * the console then the last bunch of characters are saved in msgbuf for
334  * inspection later.
335  */
336 static void
337 putchar(int c, void *arg)
338 {
339 	struct putchar_arg *ap = (struct putchar_arg*) arg;
340 	int flags = ap->flags;
341 	struct tty *tp = ap->tty;
342 	if (panicstr)
343 		constty = NULL;
344 	if ((flags & TOCONS) && tp == NULL && constty) {
345 		tp = constty;
346 		flags |= TOTTY;
347 	}
348 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
349 	    (flags & TOCONS) && tp == constty)
350 		constty = NULL;
351 	if ((flags & TOLOG))
352 		msglogchar(c, ap->pri);
353 	if ((flags & TOCONS) && constty == NULL && c != '\0')
354 		(*v_putc)(c);
355 }
356 
357 /*
358  * Scaled down version of sprintf(3).
359  */
360 int
361 sprintf(char *buf, const char *cfmt, ...)
362 {
363 	int retval;
364 	va_list ap;
365 
366 	va_start(ap, cfmt);
367 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
368 	buf[retval] = '\0';
369 	va_end(ap);
370 	return (retval);
371 }
372 
373 /*
374  * Scaled down version of vsprintf(3).
375  */
376 int
377 vsprintf(char *buf, const char *cfmt, va_list ap)
378 {
379 	int retval;
380 
381 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
382 	buf[retval] = '\0';
383 	return (retval);
384 }
385 
386 /*
387  * Scaled down version of snprintf(3).
388  */
389 int
390 snprintf(char *str, size_t size, const char *format, ...)
391 {
392 	int retval;
393 	va_list ap;
394 
395 	va_start(ap, format);
396 	retval = vsnprintf(str, size, format, ap);
397 	va_end(ap);
398 	return(retval);
399 }
400 
401 /*
402  * Scaled down version of vsnprintf(3).
403  */
404 int
405 vsnprintf(char *str, size_t size, const char *format, va_list ap)
406 {
407 	struct snprintf_arg info;
408 	int retval;
409 
410 	info.str = str;
411 	info.remain = size;
412 	retval = kvprintf(format, snprintf_func, &info, 10, ap);
413 	if (info.remain >= 1)
414 		*info.str++ = '\0';
415 	return (retval);
416 }
417 
418 /*
419  * Kernel version which takes radix argument vsnprintf(3).
420  */
421 int
422 vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
423 {
424 	struct snprintf_arg info;
425 	int retval;
426 
427 	info.str = str;
428 	info.remain = size;
429 	retval = kvprintf(format, snprintf_func, &info, radix, ap);
430 	if (info.remain >= 1)
431 		*info.str++ = '\0';
432 	return (retval);
433 }
434 
435 static void
436 snprintf_func(int ch, void *arg)
437 {
438 	struct snprintf_arg *const info = arg;
439 
440 	if (info->remain >= 2) {
441 		*info->str++ = ch;
442 		info->remain--;
443 	}
444 }
445 
446 /*
447  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
448  * order; return an optional length and a pointer to the last character
449  * written in the buffer (i.e., the first character of the string).
450  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
451  */
452 static char *
453 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp)
454 {
455 	char *p;
456 
457 	p = nbuf;
458 	*p = '\0';
459 	do {
460 		*++p = hex2ascii(num % base);
461 	} while (num /= base);
462 	if (lenp)
463 		*lenp = p - nbuf;
464 	return (p);
465 }
466 
467 /*
468  * Scaled down version of printf(3).
469  *
470  * Two additional formats:
471  *
472  * The format %b is supported to decode error registers.
473  * Its usage is:
474  *
475  *	printf("reg=%b\n", regval, "<base><arg>*");
476  *
477  * where <base> is the output base expressed as a control character, e.g.
478  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
479  * the first of which gives the bit number to be inspected (origin 1), and
480  * the next characters (up to a control character, i.e. a character <= 32),
481  * give the name of the register.  Thus:
482  *
483  *	kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
484  *
485  * would produce output:
486  *
487  *	reg=3<BITTWO,BITONE>
488  *
489  * XXX:  %D  -- Hexdump, takes pointer and separator string:
490  *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
491  *		("%*D", len, ptr, " " -> XX XX XX XX ...
492  */
493 int
494 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
495 {
496 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
497 	char nbuf[MAXNBUF];
498 	char *d;
499 	const char *p, *percent, *q;
500 	u_char *up;
501 	int ch, n;
502 	uintmax_t num;
503 	int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
504 	int jflag, tflag, zflag;
505 	int dwidth;
506 	char padc;
507 	int retval = 0;
508 
509 	num = 0;
510 	if (!func)
511 		d = (char *) arg;
512 	else
513 		d = NULL;
514 
515 	if (fmt == NULL)
516 		fmt = "(fmt null)\n";
517 
518 	if (radix < 2 || radix > 36)
519 		radix = 10;
520 
521 	for (;;) {
522 		padc = ' ';
523 		width = 0;
524 		while ((ch = (u_char)*fmt++) != '%') {
525 			if (ch == '\0')
526 				return (retval);
527 			PCHAR(ch);
528 		}
529 		percent = fmt - 1;
530 		qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
531 		sign = 0; dot = 0; dwidth = 0;
532 		jflag = 0; tflag = 0; zflag = 0;
533 reswitch:	switch (ch = (u_char)*fmt++) {
534 		case '.':
535 			dot = 1;
536 			goto reswitch;
537 		case '#':
538 			sharpflag = 1;
539 			goto reswitch;
540 		case '+':
541 			sign = 1;
542 			goto reswitch;
543 		case '-':
544 			ladjust = 1;
545 			goto reswitch;
546 		case '%':
547 			PCHAR(ch);
548 			break;
549 		case '*':
550 			if (!dot) {
551 				width = va_arg(ap, int);
552 				if (width < 0) {
553 					ladjust = !ladjust;
554 					width = -width;
555 				}
556 			} else {
557 				dwidth = va_arg(ap, int);
558 			}
559 			goto reswitch;
560 		case '0':
561 			if (!dot) {
562 				padc = '0';
563 				goto reswitch;
564 			}
565 		case '1': case '2': case '3': case '4':
566 		case '5': case '6': case '7': case '8': case '9':
567 				for (n = 0;; ++fmt) {
568 					n = n * 10 + ch - '0';
569 					ch = *fmt;
570 					if (ch < '0' || ch > '9')
571 						break;
572 				}
573 			if (dot)
574 				dwidth = n;
575 			else
576 				width = n;
577 			goto reswitch;
578 		case 'b':
579 			num = (u_int)va_arg(ap, int);
580 			p = va_arg(ap, char *);
581 			for (q = ksprintn(nbuf, num, *p++, NULL); *q;)
582 				PCHAR(*q--);
583 
584 			if (num == 0)
585 				break;
586 
587 			for (tmp = 0; *p;) {
588 				n = *p++;
589 				if (num & (1 << (n - 1))) {
590 					PCHAR(tmp ? ',' : '<');
591 					for (; (n = *p) > ' '; ++p)
592 						PCHAR(n);
593 					tmp = 1;
594 				} else
595 					for (; *p > ' '; ++p)
596 						continue;
597 			}
598 			if (tmp)
599 				PCHAR('>');
600 			break;
601 		case 'c':
602 			PCHAR(va_arg(ap, int));
603 			break;
604 		case 'D':
605 			up = va_arg(ap, u_char *);
606 			p = va_arg(ap, char *);
607 			if (!width)
608 				width = 16;
609 			while(width--) {
610 				PCHAR(hex2ascii(*up >> 4));
611 				PCHAR(hex2ascii(*up & 0x0f));
612 				up++;
613 				if (width)
614 					for (q=p;*q;q++)
615 						PCHAR(*q);
616 			}
617 			break;
618 		case 'd':
619 		case 'i':
620 			base = 10;
621 			sign = 1;
622 			goto handle_sign;
623 		case 'j':
624 			jflag = 1;
625 			goto reswitch;
626 		case 'l':
627 			if (lflag) {
628 				lflag = 0;
629 				qflag = 1;
630 			} else
631 				lflag = 1;
632 			goto reswitch;
633 		case 'n':
634 			if (jflag)
635 				*(va_arg(ap, intmax_t *)) = retval;
636 			else if (qflag)
637 				*(va_arg(ap, quad_t *)) = retval;
638 			else if (lflag)
639 				*(va_arg(ap, long *)) = retval;
640 			else if (zflag)
641 				*(va_arg(ap, size_t *)) = retval;
642 			else
643 				*(va_arg(ap, int *)) = retval;
644 			break;
645 		case 'o':
646 			base = 8;
647 			goto handle_nosign;
648 		case 'p':
649 			base = 16;
650 			sharpflag = (width == 0);
651 			sign = 0;
652 			num = (uintptr_t)va_arg(ap, void *);
653 			goto number;
654 		case 'q':
655 			qflag = 1;
656 			goto reswitch;
657 		case 'r':
658 			base = radix;
659 			if (sign)
660 				goto handle_sign;
661 			goto handle_nosign;
662 		case 's':
663 			p = va_arg(ap, char *);
664 			if (p == NULL)
665 				p = "(null)";
666 			if (!dot)
667 				n = strlen (p);
668 			else
669 				for (n = 0; n < dwidth && p[n]; n++)
670 					continue;
671 
672 			width -= n;
673 
674 			if (!ladjust && width > 0)
675 				while (width--)
676 					PCHAR(padc);
677 			while (n--)
678 				PCHAR(*p++);
679 			if (ladjust && width > 0)
680 				while (width--)
681 					PCHAR(padc);
682 			break;
683 		case 't':
684 			tflag = 1;
685 			goto reswitch;
686 		case 'u':
687 			base = 10;
688 			goto handle_nosign;
689 		case 'x':
690 		case 'X':
691 			base = 16;
692 			goto handle_nosign;
693 		case 'y':
694 			base = 16;
695 			sign = 1;
696 			goto handle_sign;
697 		case 'z':
698 			zflag = 1;
699 			goto reswitch;
700 handle_nosign:
701 			sign = 0;
702 			if (jflag)
703 				num = va_arg(ap, uintmax_t);
704 			else if (qflag)
705 				num = va_arg(ap, u_quad_t);
706 			else if (tflag)
707 				num = va_arg(ap, ptrdiff_t);
708 			else if (lflag)
709 				num = va_arg(ap, u_long);
710 			else if (zflag)
711 				num = va_arg(ap, size_t);
712 			else
713 				num = va_arg(ap, u_int);
714 			goto number;
715 handle_sign:
716 			if (jflag)
717 				num = va_arg(ap, intmax_t);
718 			else if (qflag)
719 				num = va_arg(ap, quad_t);
720 			else if (tflag)
721 				num = va_arg(ap, ptrdiff_t);
722 			else if (lflag)
723 				num = va_arg(ap, long);
724 			else if (zflag)
725 				num = va_arg(ap, size_t);
726 			else
727 				num = va_arg(ap, int);
728 number:
729 			if (sign && (intmax_t)num < 0) {
730 				neg = 1;
731 				num = -(intmax_t)num;
732 			}
733 			p = ksprintn(nbuf, num, base, &tmp);
734 			if (sharpflag && num != 0) {
735 				if (base == 8)
736 					tmp++;
737 				else if (base == 16)
738 					tmp += 2;
739 			}
740 			if (neg)
741 				tmp++;
742 
743 			if (!ladjust && width && (width -= tmp) > 0)
744 				while (width--)
745 					PCHAR(padc);
746 			if (neg)
747 				PCHAR('-');
748 			if (sharpflag && num != 0) {
749 				if (base == 8) {
750 					PCHAR('0');
751 				} else if (base == 16) {
752 					PCHAR('0');
753 					PCHAR('x');
754 				}
755 			}
756 
757 			while (*p)
758 				PCHAR(*p--);
759 
760 			if (ladjust && width && (width -= tmp) > 0)
761 				while (width--)
762 					PCHAR(padc);
763 
764 			break;
765 		default:
766 			while (percent < fmt)
767 				PCHAR(*percent++);
768 			break;
769 		}
770 	}
771 #undef PCHAR
772 }
773 
774 /*
775  * Put character in log buffer with a particular priority.
776  */
777 static void
778 msglogchar(int c, int pri)
779 {
780 	static int lastpri = -1;
781 	static int dangling;
782 	char nbuf[MAXNBUF];
783 	char *p;
784 
785 	if (!msgbufmapped)
786 		return;
787 	if (c == '\0' || c == '\r')
788 		return;
789 	if (pri != -1 && pri != lastpri) {
790 		if (dangling) {
791 			msgaddchar('\n', NULL);
792 			dangling = 0;
793 		}
794 		msgaddchar('<', NULL);
795 		for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL); *p;)
796 			msgaddchar(*p--, NULL);
797 		msgaddchar('>', NULL);
798 		lastpri = pri;
799 	}
800 	msgaddchar(c, NULL);
801 	if (c == '\n') {
802 		dangling = 0;
803 		lastpri = -1;
804 	} else {
805 		dangling = 1;
806 	}
807 }
808 
809 /*
810  * Put char in log buffer
811  */
812 static void
813 msgaddchar(int c, void *dummy)
814 {
815 	struct msgbuf *mbp;
816 
817 	if (!msgbufmapped)
818 		return;
819 	mbp = msgbufp;
820 	mbp->msg_cksum += (u_char)c - (u_char)mbp->msg_ptr[mbp->msg_bufx];
821 	mbp->msg_ptr[mbp->msg_bufx++] = c;
822 	if (mbp->msg_bufx >= mbp->msg_size)
823 		mbp->msg_bufx = 0;
824 	/* If the buffer is full, keep the most recent data. */
825 	if (mbp->msg_bufr == mbp->msg_bufx) {
826 		if (++mbp->msg_bufr >= mbp->msg_size)
827 			mbp->msg_bufr = 0;
828 	}
829 }
830 
831 static void
832 msgbufcopy(struct msgbuf *oldp)
833 {
834 	int pos;
835 
836 	pos = oldp->msg_bufr;
837 	while (pos != oldp->msg_bufx) {
838 		msglogchar(oldp->msg_ptr[pos], -1);
839 		if (++pos >= oldp->msg_size)
840 			pos = 0;
841 	}
842 }
843 
844 void
845 msgbufinit(void *ptr, int size)
846 {
847 	char *cp;
848 	static struct msgbuf *oldp = NULL;
849 
850 	size -= sizeof(*msgbufp);
851 	cp = (char *)ptr;
852 	msgbufp = (struct msgbuf *) (cp + size);
853 	if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
854 	    msgbufp->msg_bufx >= size || msgbufp->msg_bufx < 0 ||
855 	    msgbufp->msg_bufr >= size || msgbufp->msg_bufr < 0 ||
856 	    msgbufcksum(cp, size, msgbufp->msg_cksum) != msgbufp->msg_cksum) {
857 		bzero(cp, size);
858 		bzero(msgbufp, sizeof(*msgbufp));
859 		msgbufp->msg_magic = MSG_MAGIC;
860 		msgbufp->msg_size = size;
861 	}
862 	msgbufp->msg_ptr = cp;
863 	if (msgbufmapped && oldp != msgbufp)
864 		msgbufcopy(oldp);
865 	msgbufmapped = 1;
866 	oldp = msgbufp;
867 }
868 
869 static u_int
870 msgbufcksum(char *cp, size_t size, u_int cksum)
871 {
872 	u_int sum;
873 	int i;
874 
875 	sum = 0;
876 	for (i = 0; i < size; i++)
877 		sum += (u_char)cp[i];
878 	if (sum != cksum)
879 		printf("msgbuf cksum mismatch (read %x, calc %x)\n", cksum,
880 		    sum);
881 
882 	return (sum);
883 }
884 
885 SYSCTL_DECL(_security_bsd);
886 
887 static int unprivileged_read_msgbuf = 1;
888 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf,
889     CTLFLAG_RW, &unprivileged_read_msgbuf, 0,
890     "Unprivileged processes may read the kernel message buffer");
891 
892 /* Sysctls for accessing/clearing the msgbuf */
893 static int
894 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
895 {
896 	int error;
897 
898 	if (!unprivileged_read_msgbuf) {
899 		error = suser(req->td);
900 		if (error)
901 			return (error);
902 	}
903 
904 	/*
905 	 * Unwind the buffer, so that it's linear (possibly starting with
906 	 * some initial nulls).
907 	 */
908 	error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
909 	    msgbufp->msg_size - msgbufp->msg_bufx, req);
910 	if (error)
911 		return (error);
912 	if (msgbufp->msg_bufx > 0) {
913 		error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
914 		    msgbufp->msg_bufx, req);
915 	}
916 	return (error);
917 }
918 
919 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
920     0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
921 
922 static int msgbuf_clear;
923 
924 static int
925 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
926 {
927 	int error;
928 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
929 	if (!error && req->newptr) {
930 		/* Clear the buffer and reset write pointer */
931 		bzero(msgbufp->msg_ptr, msgbufp->msg_size);
932 		msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
933 		msgbufp->msg_cksum = 0;
934 		msgbuf_clear = 0;
935 	}
936 	return (error);
937 }
938 
939 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
940     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
941     sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
942 
943 #ifdef DDB
944 
945 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
946 {
947 	int i, j;
948 
949 	if (!msgbufmapped) {
950 		db_printf("msgbuf not mapped yet\n");
951 		return;
952 	}
953 	db_printf("msgbufp = %p\n", msgbufp);
954 	db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p, cksum= %d\n",
955 	    msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
956 	    msgbufp->msg_bufx, msgbufp->msg_ptr, msgbufp->msg_cksum);
957 	for (i = 0; i < msgbufp->msg_size; i++) {
958 		j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
959 		db_printf("%c", msgbufp->msg_ptr[j]);
960 	}
961 	db_printf("\n");
962 }
963 
964 #endif /* DDB */
965