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.9 2005/09/29 20:43:56 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/msgbuf.h>
47 #include <sys/malloc.h>
50 #include <sys/tprintf.h>
51 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
58 * Note that stdarg.h and the ANSI style va_start macro is used for both
59 * ANSI and traditional C compilers. We use the __ machine version to stay
60 * within the kernel header file set.
62 #include <machine/stdarg.h>
68 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
69 #define MAXNBUF (sizeof(quad_t) * NBBY + 1)
84 struct tty *constty; /* pointer to console "window" tty */
86 static void (*v_putc)(int) = cnputc; /* routine to putc on virtual console */
87 static void msglogchar(int c, int pri);
88 static void msgaddchar(int c, void *dummy);
89 static void putchar (int ch, void *arg);
90 static char *ksprintn (char *nbuf, u_long num, int base, int *len);
91 static char *ksprintqn (char *nbuf, u_quad_t num, int base, int *len);
92 static void snprintf_func (int ch, void *arg);
94 static int consintr = 1; /* Ok to handle console interrupts? */
95 static int msgbufmapped; /* Set when safe to use msgbuf */
98 static int log_console_output = 1;
99 TUNABLE_INT("kern.log_console_output", &log_console_output);
100 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
101 &log_console_output, 0, "");
103 static int unprivileged_read_msgbuf = 1;
104 SYSCTL_INT(_kern, OID_AUTO, unprivileged_read_msgbuf, CTLFLAG_RW,
105 &unprivileged_read_msgbuf, 0,
106 "Unprivileged processes may read the kernel message buffer");
109 * Warn that a system table is full.
112 tablefull(const char *tab)
115 log(LOG_ERR, "%s: table is full\n", tab);
119 * Uprintf prints to the controlling terminal for the current process.
120 * It may block if the tty queue is overfull. No message is printed if
121 * the queue does not clear in a reasonable time.
124 uprintf(const char *fmt, ...)
126 struct proc *p = curproc;
128 struct putchar_arg pca;
131 if (p && p->p_flag & P_CONTROLT &&
132 p->p_session->s_ttyvp) {
134 pca.tty = p->p_session->s_ttyp;
137 retval = kvprintf(fmt, putchar, &pca, 10, ap);
144 tprintf_open(struct proc *p)
147 if ((p->p_flag & P_CONTROLT) && p->p_session->s_ttyvp) {
148 sess_hold(p->p_session);
149 return ((tpr_t) p->p_session);
151 return ((tpr_t) NULL);
155 tprintf_close(tpr_t sess)
158 sess_rele((struct session *) sess);
162 * tprintf prints on the controlling terminal associated
163 * with the given session.
166 tprintf(tpr_t tpr, const char *fmt, ...)
168 struct session *sess = (struct session *)tpr;
169 struct tty *tp = NULL;
172 struct putchar_arg pca;
175 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
183 retval = kvprintf(fmt, putchar, &pca, 10, ap);
190 * Ttyprintf displays a message on a tty; it should be used only by
191 * the tty driver, or anything that knows the underlying tty will not
192 * be revoke(2)'d away. Other callers should use tprintf.
195 ttyprintf(struct tty *tp, const char *fmt, ...)
198 struct putchar_arg pca;
204 retval = kvprintf(fmt, putchar, &pca, 10, ap);
210 * Log writes to the log buffer, and guarantees not to sleep (so can be
211 * called by interrupt routines). If there is no process reading the
212 * log yet, it writes to the console also.
215 log(int level, const char *fmt, ...)
219 struct putchar_arg pca;
223 pca.flags = log_open ? TOLOG : TOCONS;
226 retval = kvprintf(fmt, putchar, &pca, 10, ap);
234 addlog(const char *fmt, ...)
238 struct putchar_arg pca;
242 pca.flags = log_open ? TOLOG : TOCONS;
245 retval = kvprintf(fmt, putchar, &pca, 10, ap);
252 #define CONSCHUNK 128
255 log_console(struct uio *uio)
257 int c, i, error, iovlen, nl;
259 struct iovec *miov = NULL;
263 if (!log_console_output)
266 pri = LOG_INFO | LOG_CONSOLE;
268 iovlen = uio->uio_iovcnt * sizeof (struct iovec);
269 MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
270 MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
271 bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen);
276 while (uio->uio_resid > 0) {
277 c = imin(uio->uio_resid, CONSCHUNK);
278 error = uiomove(consbuffer, c, uio);
281 for (i = 0; i < c; i++) {
282 msglogchar(consbuffer[i], pri);
283 if (consbuffer[i] == '\n')
290 msglogchar('\n', pri);
293 FREE(consbuffer, M_TEMP);
298 printf(const char *fmt, ...)
302 struct putchar_arg pca;
305 savintr = consintr; /* disable interrupts */
309 pca.flags = TOCONS | TOLOG;
312 retval = kvprintf(fmt, putchar, &pca, 10, ap);
317 consintr = savintr; /* reenable interrupts */
322 vprintf(const char *fmt, __va_list ap)
325 struct putchar_arg pca;
328 savintr = consintr; /* disable interrupts */
331 pca.flags = TOCONS | TOLOG;
334 retval = kvprintf(fmt, putchar, &pca, 10, ap);
338 consintr = savintr; /* reenable interrupts */
343 * Print a character on console or users terminal. If destination is
344 * the console then the last bunch of characters are saved in msgbuf for
348 putchar(int c, void *arg)
350 struct putchar_arg *ap = (struct putchar_arg*) arg;
351 int flags = ap->flags;
352 struct tty *tp = ap->tty;
355 if ((flags & TOCONS) && tp == NULL && constty) {
359 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
360 (flags & TOCONS) && tp == constty)
363 msglogchar(c, ap->pri);
364 if ((flags & TOCONS) && constty == NULL && c != '\0')
369 * Scaled down version of sprintf(3).
372 sprintf(char *buf, const char *cfmt, ...)
377 __va_start(ap, cfmt);
378 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
385 * Scaled down version of vsprintf(3).
388 vsprintf(char *buf, const char *cfmt, __va_list ap)
392 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
398 * Scaled down version of snprintf(3).
401 snprintf(char *str, size_t size, const char *format, ...)
406 __va_start(ap, format);
407 retval = vsnprintf(str, size, format, ap);
413 * Scaled down version of vsnprintf(3).
416 vsnprintf(char *str, size_t size, const char *format, __va_list ap)
418 struct snprintf_arg info;
423 retval = kvprintf(format, snprintf_func, &info, 10, ap);
424 if (info.remain >= 1)
430 snprintf_func(int ch, void *arg)
432 struct snprintf_arg *const info = arg;
434 if (info->remain >= 2) {
441 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
442 * order; return an optional length and a pointer to the last character
443 * written in the buffer (i.e., the first character of the string).
444 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
447 ksprintn(nbuf, ul, base, lenp)
457 *++p = hex2ascii(ul % base);
458 } while (ul /= base);
463 /* ksprintn, but for a quad_t. */
465 ksprintqn(nbuf, uq, base, lenp)
475 *++p = hex2ascii(uq % base);
476 } while (uq /= base);
483 * Scaled down version of printf(3).
485 * Two additional formats:
487 * The format %b is supported to decode error registers.
490 * printf("reg=%b\n", regval, "<base><arg>*");
492 * where <base> is the output base expressed as a control character, e.g.
493 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
494 * the first of which gives the bit number to be inspected (origin 1), and
495 * the next characters (up to a control character, i.e. a character <= 32),
496 * give the name of the register. Thus:
498 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
500 * would produce output:
502 * reg=3<BITTWO,BITONE>
504 * XXX: %D -- Hexdump, takes pointer and separator string:
505 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
506 * ("%*D", len, ptr, " " -> XX XX XX XX ...
509 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, __va_list ap)
511 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
518 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
531 fmt = "(fmt null)\n";
533 if (radix < 2 || radix > 36)
539 while ((ch = (u_char)*fmt++) != '%') {
544 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
545 sign = 0; dot = 0; dwidth = 0;
546 reswitch: switch (ch = (u_char)*fmt++) {
564 width = __va_arg(ap, int);
570 dwidth = __va_arg(ap, int);
578 case '1': case '2': case '3': case '4':
579 case '5': case '6': case '7': case '8': case '9':
580 for (n = 0;; ++fmt) {
581 n = n * 10 + ch - '0';
583 if (ch < '0' || ch > '9')
592 ul = __va_arg(ap, int);
593 p = __va_arg(ap, char *);
594 for (q = ksprintn(nbuf, ul, *p++, NULL); *q;)
602 if (ul & (1 << (n - 1))) {
603 PCHAR(tmp ? ',' : '<');
604 for (; (n = *p) > ' '; ++p)
608 for (; *p > ' '; ++p)
615 PCHAR(__va_arg(ap, int));
618 up = __va_arg(ap, u_char *);
619 p = __va_arg(ap, char *);
623 PCHAR(hex2ascii(*up >> 4));
624 PCHAR(hex2ascii(*up & 0x0f));
633 uq = __va_arg(ap, quad_t);
635 ul = __va_arg(ap, long);
637 ul = __va_arg(ap, int);
650 uq = __va_arg(ap, u_quad_t);
652 ul = __va_arg(ap, u_long);
654 ul = __va_arg(ap, u_int);
658 ul = (uintptr_t)__va_arg(ap, void *);
660 sharpflag = (width == 0);
668 uq = __va_arg(ap, u_quad_t);
670 ul = __va_arg(ap, u_long);
673 (u_long)__va_arg(ap, int) : __va_arg(ap, u_int);
677 p = __va_arg(ap, char *);
683 for (n = 0; n < dwidth && p[n]; n++)
688 if (!ladjust && width > 0)
693 if (ladjust && width > 0)
699 uq = __va_arg(ap, u_quad_t);
701 ul = __va_arg(ap, u_long);
703 ul = __va_arg(ap, u_int);
709 uq = __va_arg(ap, u_quad_t);
711 ul = __va_arg(ap, u_long);
713 ul = __va_arg(ap, u_int);
718 uq = __va_arg(ap, u_quad_t);
720 ul = __va_arg(ap, u_long);
723 (u_long)__va_arg(ap, int) : __va_arg(ap, u_int);
729 if (sign && (quad_t)uq < 0) {
733 p = ksprintqn(nbuf, uq, base, &tmp);
735 if (sign && (long)ul < 0) {
739 p = ksprintn(nbuf, ul, base, &tmp);
741 if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
750 if (!ladjust && width && (width -= tmp) > 0)
755 if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
758 } else if (base == 16) {
767 if (ladjust && width && (width -= tmp) > 0)
784 * Put character in log buffer with a particular priority.
787 msglogchar(int c, int pri)
789 static int lastpri = -1;
796 if (c == '\0' || c == '\r')
798 if (pri != -1 && pri != lastpri) {
800 msgaddchar('\n', NULL);
803 msgaddchar('<', NULL);
804 for (p = ksprintn(nbuf, (u_long)pri, 10, NULL); *p;)
805 msgaddchar(*p--, NULL);
806 msgaddchar('>', NULL);
819 * Put char in log buffer
822 msgaddchar(int c, void *dummy)
829 mbp->msg_ptr[mbp->msg_bufx++] = c;
830 if (mbp->msg_bufx >= mbp->msg_size)
832 /* If the buffer is full, keep the most recent data. */
833 if (mbp->msg_bufr == mbp->msg_bufx) {
834 if (++mbp->msg_bufr >= mbp->msg_size)
840 msgbufcopy(struct msgbuf *oldp)
844 pos = oldp->msg_bufr;
845 while (pos != oldp->msg_bufx) {
846 msglogchar(oldp->msg_ptr[pos], -1);
847 if (++pos >= oldp->msg_size)
853 msgbufinit(void *ptr, size_t size)
856 static struct msgbuf *oldp = NULL;
858 size -= sizeof(*msgbufp);
860 msgbufp = (struct msgbuf *) (cp + size);
861 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
862 msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) {
864 bzero(msgbufp, sizeof(*msgbufp));
865 msgbufp->msg_magic = MSG_MAGIC;
866 msgbufp->msg_size = (char *)msgbufp - cp;
868 msgbufp->msg_ptr = cp;
869 if (msgbufmapped && oldp != msgbufp)
875 /* Sysctls for accessing/clearing the msgbuf */
878 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
884 * Only wheel or root can access the message log.
886 if (unprivileged_read_msgbuf == 0) {
887 KKASSERT(req->td->td_proc);
888 cred = req->td->td_proc->p_ucred;
890 if ((cred->cr_prison || groupmember(0, cred) == 0) &&
898 * Unwind the buffer, so that it's linear (possibly starting with
899 * some initial nulls).
901 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
902 msgbufp->msg_size - msgbufp->msg_bufx, req);
905 if (msgbufp->msg_bufx > 0) {
906 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
907 msgbufp->msg_bufx, req);
912 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
913 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
915 static int msgbuf_clear;
918 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
921 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
922 if (!error && req->newptr) {
923 /* Clear the buffer and reset write pointer */
924 bzero(msgbufp->msg_ptr, msgbufp->msg_size);
925 msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
931 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
932 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
933 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
939 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
944 db_printf("msgbuf not mapped yet\n");
947 db_printf("msgbufp = %p\n", msgbufp);
948 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
949 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
950 msgbufp->msg_bufx, msgbufp->msg_ptr);
951 for (i = 0; i < msgbufp->msg_size; i++) {
952 j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
953 db_printf("%c", msgbufp->msg_ptr[j]);