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