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.20 2008/01/04 12:16:19 matthias 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(_security
, 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 * Limited rate kprintf. The passed rate structure must be initialized
336 * with the desired reporting frequency. A frequency of 0 will result in
340 krateprintf(struct krate
*rate
, const char *fmt
, ...)
344 if (rate
->ticks
!= (int)time_second
) {
345 rate
->ticks
= (int)time_second
;
348 if (rate
->count
< rate
->freq
) {
357 * Print a character on console or users terminal. If destination is
358 * the console then the last bunch of characters are saved in msgbuf for
361 * NOT YET ENTIRELY MPSAFE, EVEN WHEN LOGGING JUST TO THE SYSCONSOLE.
364 kputchar(int c
, void *arg
)
366 struct putchar_arg
*ap
= (struct putchar_arg
*) arg
;
367 int flags
= ap
->flags
;
368 struct tty
*tp
= ap
->tty
;
371 if ((flags
& TOCONS
) && tp
== NULL
&& constty
) {
375 if ((flags
& TOTTY
) && tp
&& tputchar(c
, tp
) < 0 &&
376 (flags
& TOCONS
) && tp
== constty
)
379 msglogchar(c
, ap
->pri
);
380 if ((flags
& TOCONS
) && constty
== NULL
&& c
!= '\0')
385 * Scaled down version of sprintf(3).
388 ksprintf(char *buf
, const char *cfmt
, ...)
393 __va_start(ap
, cfmt
);
394 retval
= kvcprintf(cfmt
, NULL
, (void *)buf
, 10, ap
);
401 * Scaled down version of vsprintf(3).
404 kvsprintf(char *buf
, const char *cfmt
, __va_list ap
)
408 retval
= kvcprintf(cfmt
, NULL
, (void *)buf
, 10, ap
);
414 * Scaled down version of snprintf(3).
417 ksnprintf(char *str
, size_t size
, const char *format
, ...)
422 __va_start(ap
, format
);
423 retval
= kvsnprintf(str
, size
, format
, ap
);
429 * Scaled down version of vsnprintf(3).
432 kvsnprintf(char *str
, size_t size
, const char *format
, __va_list ap
)
434 struct snprintf_arg info
;
439 retval
= kvcprintf(format
, snprintf_func
, &info
, 10, ap
);
440 if (info
.remain
>= 1)
446 snprintf_func(int ch
, void *arg
)
448 struct snprintf_arg
*const info
= arg
;
450 if (info
->remain
>= 2) {
457 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
458 * order; return an optional length and a pointer to the last character
459 * written in the buffer (i.e., the first character of the string).
460 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
463 ksprintn(char *nbuf
, uintmax_t num
, int base
, int *lenp
, int upper
)
470 c
= hex2ascii(num
% base
);
471 *++p
= upper
? toupper(c
) : c
;
472 } while (num
/= base
);
479 * Scaled down version of printf(3).
481 * Two additional formats:
483 * The format %b is supported to decode error registers.
486 * kprintf("reg=%b\n", regval, "<base><arg>*");
488 * where <base> is the output base expressed as a control character, e.g.
489 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
490 * the first of which gives the bit number to be inspected (origin 1), and
491 * the next characters (up to a control character, i.e. a character <= 32),
492 * give the name of the register. Thus:
494 * kvcprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
496 * would produce output:
498 * reg=3<BITTWO,BITONE>
500 * XXX: %D -- Hexdump, takes pointer and separator string:
501 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
502 * ("%*D", len, ptr, " " -> XX XX XX XX ...
505 kvcprintf(char const *fmt
, void (*func
)(int, void*), void *arg
, int radix
, __va_list ap
)
507 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
510 const char *p
, *percent
, *q
;
514 int base
, tmp
, width
, ladjust
, sharpflag
, neg
, sign
, dot
;
515 int jflag
, lflag
, qflag
, tflag
;
518 int retval
= 0, stop
= 0;
527 fmt
= "(fmt null)\n";
529 if (radix
< 2 || radix
> 36)
535 while ((ch
= (u_char
)*fmt
++) != '%' || stop
) {
541 dot
= dwidth
= ladjust
= neg
= sharpflag
= sign
= upper
= 0;
542 jflag
= lflag
= qflag
= tflag
= 0;
545 switch (ch
= (u_char
)*fmt
++) {
563 width
= __va_arg(ap
, int);
569 dwidth
= __va_arg(ap
, int);
577 case '1': case '2': case '3': case '4':
578 case '5': case '6': case '7': case '8': case '9':
579 for (n
= 0;; ++fmt
) {
580 n
= n
* 10 + ch
- '0';
582 if (ch
< '0' || ch
> '9')
591 num
= (u_int
)__va_arg(ap
, int);
592 p
= __va_arg(ap
, char *);
593 for (q
= ksprintn(nbuf
, num
, *p
++, NULL
, 0); *q
;)
601 if (num
& (1 << (n
- 1))) {
602 PCHAR(tmp
? ',' : '<');
603 for (; (n
= *p
) > ' '; ++p
)
607 for (; *p
> ' '; ++p
)
614 PCHAR(__va_arg(ap
, int));
617 up
= __va_arg(ap
, u_char
*);
618 p
= __va_arg(ap
, char *);
622 PCHAR(hex2ascii(*up
>> 4));
623 PCHAR(hex2ascii(*up
& 0x0f));
647 *(__va_arg(ap
, intmax_t *)) = retval
;
649 *(__va_arg(ap
, long *)) = retval
;
651 *(__va_arg(ap
, quad_t
*)) = retval
;
653 *(__va_arg(ap
, int *)) = retval
;
660 sharpflag
= (width
== 0);
662 num
= (uintptr_t)__va_arg(ap
, void *);
673 p
= __va_arg(ap
, char *);
679 for (n
= 0; n
< dwidth
&& p
[n
]; n
++)
684 if (!ladjust
&& width
> 0)
689 if (ladjust
&& width
> 0)
712 num
= __va_arg(ap
, uintmax_t);
714 num
= __va_arg(ap
, u_long
);
716 num
= __va_arg(ap
, u_quad_t
);
718 num
= __va_arg(ap
, ptrdiff_t);
720 num
= __va_arg(ap
, u_int
);
724 num
= __va_arg(ap
, intmax_t);
726 num
= __va_arg(ap
, long);
728 num
= __va_arg(ap
, quad_t
);
730 num
= __va_arg(ap
, ptrdiff_t);
732 num
= __va_arg(ap
, int);
734 if (sign
&& (intmax_t)num
< 0) {
736 num
= -(intmax_t)num
;
738 p
= ksprintn(nbuf
, num
, base
, &tmp
, upper
);
739 if (sharpflag
&& num
!= 0) {
748 if (!ladjust
&& padc
!= '0' && width
&&
749 (width
-= tmp
) > 0) {
755 if (sharpflag
&& num
!= 0) {
758 } else if (base
== 16) {
763 if (!ladjust
&& width
&& (width
-= tmp
) > 0)
770 if (ladjust
&& width
&& (width
-= tmp
) > 0)
776 while (percent
< fmt
)
779 * Since we ignore an formatting argument it is no
780 * longer safe to obey the remaining formatting
781 * arguments as the arguments will no longer match
792 * Put character in log buffer with a particular priority.
797 msglogchar(int c
, int pri
)
799 static int lastpri
= -1;
806 if (c
== '\0' || c
== '\r')
808 if (pri
!= -1 && pri
!= lastpri
) {
810 msgaddchar('\n', NULL
);
813 msgaddchar('<', NULL
);
814 for (p
= ksprintn(nbuf
, (uintmax_t)pri
, 10, NULL
, 0); *p
;)
815 msgaddchar(*p
--, NULL
);
816 msgaddchar('>', NULL
);
829 * Put char in log buffer. Make sure nothing blows up beyond repair if
830 * we have an MP race.
835 msgaddchar(int c
, void *dummy
)
844 windex
= mbp
->msg_bufx
;
845 mbp
->msg_ptr
[windex
] = c
;
846 if (++windex
>= mbp
->msg_size
)
848 rindex
= mbp
->msg_bufr
;
849 if (windex
== rindex
) {
851 if (rindex
>= mbp
->msg_size
)
852 rindex
-= mbp
->msg_size
;
853 mbp
->msg_bufr
= rindex
;
855 mbp
->msg_bufx
= windex
;
859 msgbufcopy(struct msgbuf
*oldp
)
863 pos
= oldp
->msg_bufr
;
864 while (pos
!= oldp
->msg_bufx
) {
865 msglogchar(oldp
->msg_ptr
[pos
], -1);
866 if (++pos
>= oldp
->msg_size
)
872 msgbufinit(void *ptr
, size_t size
)
875 static struct msgbuf
*oldp
= NULL
;
877 size
-= sizeof(*msgbufp
);
879 msgbufp
= (struct msgbuf
*) (cp
+ size
);
880 if (msgbufp
->msg_magic
!= MSG_MAGIC
|| msgbufp
->msg_size
!= size
||
881 msgbufp
->msg_bufx
>= size
|| msgbufp
->msg_bufr
>= size
) {
883 bzero(msgbufp
, sizeof(*msgbufp
));
884 msgbufp
->msg_magic
= MSG_MAGIC
;
885 msgbufp
->msg_size
= (char *)msgbufp
- cp
;
887 msgbufp
->msg_ptr
= cp
;
888 if (msgbufmapped
&& oldp
!= msgbufp
)
894 /* Sysctls for accessing/clearing the msgbuf */
897 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS
)
903 * Only wheel or root can access the message log.
905 if (unprivileged_read_msgbuf
== 0) {
906 KKASSERT(req
->td
->td_proc
);
907 cred
= req
->td
->td_proc
->p_ucred
;
909 if ((cred
->cr_prison
|| groupmember(0, cred
) == 0) &&
917 * Unwind the buffer, so that it's linear (possibly starting with
918 * some initial nulls).
920 error
= sysctl_handle_opaque(oidp
, msgbufp
->msg_ptr
+ msgbufp
->msg_bufx
,
921 msgbufp
->msg_size
- msgbufp
->msg_bufx
, req
);
924 if (msgbufp
->msg_bufx
> 0) {
925 error
= sysctl_handle_opaque(oidp
, msgbufp
->msg_ptr
,
926 msgbufp
->msg_bufx
, req
);
931 SYSCTL_PROC(_kern
, OID_AUTO
, msgbuf
, CTLTYPE_STRING
| CTLFLAG_RD
,
932 0, 0, sysctl_kern_msgbuf
, "A", "Contents of kernel message buffer");
934 static int msgbuf_clear
;
937 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS
)
940 error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
941 if (!error
&& req
->newptr
) {
942 /* Clear the buffer and reset write pointer */
943 bzero(msgbufp
->msg_ptr
, msgbufp
->msg_size
);
944 msgbufp
->msg_bufr
= msgbufp
->msg_bufx
= 0;
950 SYSCTL_PROC(_kern
, OID_AUTO
, msgbuf_clear
,
951 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_SECURE
, &msgbuf_clear
, 0,
952 sysctl_kern_msgbuf_clear
, "I", "Clear kernel message buffer");
956 DB_SHOW_COMMAND(msgbuf
, db_show_msgbuf
)
961 db_printf("msgbuf not mapped yet\n");
964 db_printf("msgbufp = %p\n", msgbufp
);
965 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
966 msgbufp
->msg_magic
, msgbufp
->msg_size
, msgbufp
->msg_bufr
,
967 msgbufp
->msg_bufx
, msgbufp
->msg_ptr
);
968 for (i
= 0; i
< msgbufp
->msg_size
; i
++) {
969 j
= (i
+ msgbufp
->msg_bufr
) % msgbufp
->msg_size
;
970 db_printf("%c", msgbufp
->msg_ptr
[j
]);