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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/subr_prf.c,v 1.61.2.5 2002/08/31 18:22:08 dwmalone Exp $
40 * $DragonFly: src/sys/kern/subr_prf.c,v 1.17 2006/12/26 11:01:07 swildner Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/msgbuf.h>
49 #include <sys/malloc.h>
52 #include <sys/tprintf.h>
53 #include <sys/stdint.h>
54 #include <sys/syslog.h>
57 #include <sys/sysctl.h>
59 #include <sys/ctype.h>
66 * Note that stdarg.h and the ANSI style va_start macro is used for both
67 * ANSI and traditional C compilers. We use the __ machine version to stay
68 * within the kernel header file set.
70 #include <machine/stdarg.h>
76 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
77 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
92 struct tty *constty; /* pointer to console "window" tty */
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 void kputchar (int ch, void *arg);
98 static char *ksprintn (char *nbuf, uintmax_t num, int base, int *lenp,
100 static void snprintf_func (int ch, void *arg);
102 static int consintr = 1; /* Ok to handle console interrupts? */
103 static int msgbufmapped; /* Set when safe to use msgbuf */
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, "");
111 static int unprivileged_read_msgbuf = 1;
112 SYSCTL_INT(_kern, OID_AUTO, unprivileged_read_msgbuf, CTLFLAG_RW,
113 &unprivileged_read_msgbuf, 0,
114 "Unprivileged processes may read the kernel message buffer");
117 * Warn that a system table is full.
120 tablefull(const char *tab)
123 log(LOG_ERR, "%s: table is full\n", tab);
127 * Uprintf prints to the controlling terminal for the current process.
130 uprintf(const char *fmt, ...)
132 struct proc *p = curproc;
134 struct putchar_arg pca;
137 if (p && p->p_flag & P_CONTROLT &&
138 p->p_session->s_ttyvp) {
140 pca.tty = p->p_session->s_ttyp;
143 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
150 tprintf_open(struct proc *p)
153 if ((p->p_flag & P_CONTROLT) && p->p_session->s_ttyvp) {
154 sess_hold(p->p_session);
155 return ((tpr_t) p->p_session);
157 return ((tpr_t) NULL);
161 tprintf_close(tpr_t sess)
164 sess_rele((struct session *) sess);
168 * tprintf prints on the controlling terminal associated
169 * with the given session.
172 tprintf(tpr_t tpr, const char *fmt, ...)
174 struct session *sess = (struct session *)tpr;
175 struct tty *tp = NULL;
178 struct putchar_arg pca;
181 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
189 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
196 * Ttyprintf displays a message on a tty; it should be used only by
197 * the tty driver, or anything that knows the underlying tty will not
198 * be revoke(2)'d away. Other callers should use tprintf.
201 ttyprintf(struct tty *tp, const char *fmt, ...)
204 struct putchar_arg pca;
210 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
216 * Log writes to the log buffer, and guarantees not to sleep (so can be
217 * called by interrupt routines). If there is no process reading the
218 * log yet, it writes to the console also.
221 log(int level, const char *fmt, ...)
225 struct putchar_arg pca;
229 pca.flags = log_open ? TOLOG : TOCONS;
232 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
239 #define CONSCHUNK 128
242 log_console(struct uio *uio)
244 int c, i, error, iovlen, nl;
246 struct iovec *miov = NULL;
250 if (!log_console_output)
253 pri = LOG_INFO | LOG_CONSOLE;
255 iovlen = uio->uio_iovcnt * sizeof (struct iovec);
256 MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
257 MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
258 bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen);
263 while (uio->uio_resid > 0) {
264 c = imin(uio->uio_resid, CONSCHUNK);
265 error = uiomove(consbuffer, c, uio);
268 for (i = 0; i < c; i++) {
269 msglogchar(consbuffer[i], pri);
270 if (consbuffer[i] == '\n')
277 msglogchar('\n', pri);
280 FREE(consbuffer, M_TEMP);
285 * Output to the console.
287 * NOT YET ENTIRELY MPSAFE
290 kprintf(const char *fmt, ...)
294 struct putchar_arg pca;
297 savintr = consintr; /* disable interrupts */
301 pca.flags = TOCONS | TOLOG;
304 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
309 consintr = savintr; /* reenable interrupts */
314 kvprintf(const char *fmt, __va_list ap)
317 struct putchar_arg pca;
320 savintr = consintr; /* disable interrupts */
323 pca.flags = TOCONS | TOLOG;
326 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
330 consintr = savintr; /* reenable interrupts */
335 * Print a character on console or users terminal. If destination is
336 * the console then the last bunch of characters are saved in msgbuf for
339 * NOT YET ENTIRELY MPSAFE, EVEN WHEN LOGGING JUST TO THE SYSCONSOLE.
342 kputchar(int c, void *arg)
344 struct putchar_arg *ap = (struct putchar_arg*) arg;
345 int flags = ap->flags;
346 struct tty *tp = ap->tty;
349 if ((flags & TOCONS) && tp == NULL && constty) {
353 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
354 (flags & TOCONS) && tp == constty)
357 msglogchar(c, ap->pri);
358 if ((flags & TOCONS) && constty == NULL && c != '\0')
363 * Scaled down version of sprintf(3).
366 ksprintf(char *buf, const char *cfmt, ...)
371 __va_start(ap, cfmt);
372 retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap);
379 * Scaled down version of vsprintf(3).
382 kvsprintf(char *buf, const char *cfmt, __va_list ap)
386 retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap);
392 * Scaled down version of snprintf(3).
395 ksnprintf(char *str, size_t size, const char *format, ...)
400 __va_start(ap, format);
401 retval = kvsnprintf(str, size, format, ap);
407 * Scaled down version of vsnprintf(3).
410 kvsnprintf(char *str, size_t size, const char *format, __va_list ap)
412 struct snprintf_arg info;
417 retval = kvcprintf(format, snprintf_func, &info, 10, ap);
418 if (info.remain >= 1)
424 snprintf_func(int ch, void *arg)
426 struct snprintf_arg *const info = arg;
428 if (info->remain >= 2) {
435 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
436 * order; return an optional length and a pointer to the last character
437 * written in the buffer (i.e., the first character of the string).
438 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
441 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
448 c = hex2ascii(num % base);
449 *++p = upper ? toupper(c) : c;
450 } while (num /= base);
457 * Scaled down version of printf(3).
459 * Two additional formats:
461 * The format %b is supported to decode error registers.
464 * kprintf("reg=%b\n", regval, "<base><arg>*");
466 * where <base> is the output base expressed as a control character, e.g.
467 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
468 * the first of which gives the bit number to be inspected (origin 1), and
469 * the next characters (up to a control character, i.e. a character <= 32),
470 * give the name of the register. Thus:
472 * kvcprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
474 * would produce output:
476 * reg=3<BITTWO,BITONE>
478 * XXX: %D -- Hexdump, takes pointer and separator string:
479 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
480 * ("%*D", len, ptr, " " -> XX XX XX XX ...
483 kvcprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, __va_list ap)
485 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
488 const char *p, *percent, *q;
492 int base, tmp, width, ladjust, sharpflag, neg, sign, dot;
493 int jflag, lflag, qflag, tflag;
496 int retval = 0, stop = 0;
505 fmt = "(fmt null)\n";
507 if (radix < 2 || radix > 36)
513 while ((ch = (u_char)*fmt++) != '%' || stop) {
519 dot = dwidth = ladjust = neg = sharpflag = sign = upper = 0;
520 jflag = lflag = qflag = tflag = 0;
523 switch (ch = (u_char)*fmt++) {
541 width = __va_arg(ap, int);
547 dwidth = __va_arg(ap, int);
555 case '1': case '2': case '3': case '4':
556 case '5': case '6': case '7': case '8': case '9':
557 for (n = 0;; ++fmt) {
558 n = n * 10 + ch - '0';
560 if (ch < '0' || ch > '9')
569 num = (u_int)__va_arg(ap, int);
570 p = __va_arg(ap, char *);
571 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
579 if (num & (1 << (n - 1))) {
580 PCHAR(tmp ? ',' : '<');
581 for (; (n = *p) > ' '; ++p)
585 for (; *p > ' '; ++p)
592 PCHAR(__va_arg(ap, int));
595 up = __va_arg(ap, u_char *);
596 p = __va_arg(ap, char *);
600 PCHAR(hex2ascii(*up >> 4));
601 PCHAR(hex2ascii(*up & 0x0f));
625 *(__va_arg(ap, intmax_t *)) = retval;
627 *(__va_arg(ap, long *)) = retval;
629 *(__va_arg(ap, quad_t *)) = retval;
631 *(__va_arg(ap, int *)) = retval;
638 sharpflag = (width == 0);
640 num = (uintptr_t)__va_arg(ap, void *);
651 p = __va_arg(ap, char *);
657 for (n = 0; n < dwidth && p[n]; n++)
662 if (!ladjust && width > 0)
667 if (ladjust && width > 0)
690 num = __va_arg(ap, uintmax_t);
692 num = __va_arg(ap, u_long);
694 num = __va_arg(ap, u_quad_t);
696 num = __va_arg(ap, ptrdiff_t);
698 num = __va_arg(ap, u_int);
702 num = __va_arg(ap, intmax_t);
704 num = __va_arg(ap, long);
706 num = __va_arg(ap, quad_t);
708 num = __va_arg(ap, ptrdiff_t);
710 num = __va_arg(ap, int);
712 if (sign && (intmax_t)num < 0) {
714 num = -(intmax_t)num;
716 p = ksprintn(nbuf, num, base, &tmp, upper);
717 if (sharpflag && num != 0) {
726 if (!ladjust && padc != '0' && width &&
727 (width -= tmp) > 0) {
733 if (sharpflag && num != 0) {
736 } else if (base == 16) {
741 if (!ladjust && width && (width -= tmp) > 0)
748 if (ladjust && width && (width -= tmp) > 0)
754 while (percent < fmt)
757 * Since we ignore an formatting argument it is no
758 * longer safe to obey the remaining formatting
759 * arguments as the arguments will no longer match
770 * Put character in log buffer with a particular priority.
775 msglogchar(int c, int pri)
777 static int lastpri = -1;
784 if (c == '\0' || c == '\r')
786 if (pri != -1 && pri != lastpri) {
788 msgaddchar('\n', NULL);
791 msgaddchar('<', NULL);
792 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
793 msgaddchar(*p--, NULL);
794 msgaddchar('>', NULL);
807 * Put char in log buffer. Make sure nothing blows up beyond repair if
808 * we have an MP race.
813 msgaddchar(int c, void *dummy)
822 windex = mbp->msg_bufx;
823 mbp->msg_ptr[windex] = c;
824 if (++windex >= mbp->msg_size)
826 rindex = mbp->msg_bufr;
827 if (windex == rindex) {
829 if (rindex >= mbp->msg_size)
830 rindex -= mbp->msg_size;
831 mbp->msg_bufr = rindex;
833 mbp->msg_bufx = windex;
837 msgbufcopy(struct msgbuf *oldp)
841 pos = oldp->msg_bufr;
842 while (pos != oldp->msg_bufx) {
843 msglogchar(oldp->msg_ptr[pos], -1);
844 if (++pos >= oldp->msg_size)
850 msgbufinit(void *ptr, size_t size)
853 static struct msgbuf *oldp = NULL;
855 size -= sizeof(*msgbufp);
857 msgbufp = (struct msgbuf *) (cp + size);
858 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
859 msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) {
861 bzero(msgbufp, sizeof(*msgbufp));
862 msgbufp->msg_magic = MSG_MAGIC;
863 msgbufp->msg_size = (char *)msgbufp - cp;
865 msgbufp->msg_ptr = cp;
866 if (msgbufmapped && oldp != msgbufp)
872 /* Sysctls for accessing/clearing the msgbuf */
875 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
881 * Only wheel or root can access the message log.
883 if (unprivileged_read_msgbuf == 0) {
884 KKASSERT(req->td->td_proc);
885 cred = req->td->td_proc->p_ucred;
887 if ((cred->cr_prison || groupmember(0, cred) == 0) &&
895 * Unwind the buffer, so that it's linear (possibly starting with
896 * some initial nulls).
898 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
899 msgbufp->msg_size - msgbufp->msg_bufx, req);
902 if (msgbufp->msg_bufx > 0) {
903 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
904 msgbufp->msg_bufx, req);
909 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
910 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
912 static int msgbuf_clear;
915 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
918 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
919 if (!error && req->newptr) {
920 /* Clear the buffer and reset write pointer */
921 bzero(msgbufp->msg_ptr, msgbufp->msg_size);
922 msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
928 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
929 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
930 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
934 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
939 db_printf("msgbuf not mapped yet\n");
942 db_printf("msgbufp = %p\n", msgbufp);
943 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
944 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
945 msgbufp->msg_bufx, msgbufp->msg_ptr);
946 for (i = 0; i < msgbufp->msg_size; i++) {
947 j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
948 db_printf("%c", msgbufp->msg_ptr[j]);