xref: /freebsd/sys/kern/subr_prf.c (revision 4784a46912c7d3d627fea960067f4e04831346c7)
1df8bae1dSRodney W. Grimes /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1986, 1988, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41677b542eSDavid E. O'Brien #include <sys/cdefs.h>
42677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
43677b542eSDavid E. O'Brien 
443ae59505SPoul-Henning Kamp #include "opt_ddb.h"
453ae59505SPoul-Henning Kamp 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
47df8bae1dSRodney W. Grimes #include <sys/systm.h>
48f591779bSSeigo Tanimura #include <sys/lock.h>
49f591779bSSeigo Tanimura #include <sys/mutex.h>
50f591779bSSeigo Tanimura #include <sys/sx.h>
51e796e00dSPoul-Henning Kamp #include <sys/kernel.h>
52df8bae1dSRodney W. Grimes #include <sys/msgbuf.h>
53a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
54df8bae1dSRodney W. Grimes #include <sys/proc.h>
552bb95458SMaxime Henrion #include <sys/stddef.h>
5658a24f79SPoul-Henning Kamp #include <sys/sysctl.h>
57df8bae1dSRodney W. Grimes #include <sys/tty.h>
58df8bae1dSRodney W. Grimes #include <sys/syslog.h>
59ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
60e2a09b26SPoul-Henning Kamp #include <sys/uio.h>
61df8bae1dSRodney W. Grimes 
623ae59505SPoul-Henning Kamp #ifdef DDB
633ae59505SPoul-Henning Kamp #include <ddb/ddb.h>
643ae59505SPoul-Henning Kamp #endif
653ae59505SPoul-Henning Kamp 
66df8bae1dSRodney W. Grimes /*
67df8bae1dSRodney W. Grimes  * Note that stdarg.h and the ANSI style va_start macro is used for both
68df8bae1dSRodney W. Grimes  * ANSI and traditional C compilers.
69df8bae1dSRodney W. Grimes  */
70df8bae1dSRodney W. Grimes #include <machine/stdarg.h>
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes #define TOCONS	0x01
73df8bae1dSRodney W. Grimes #define TOTTY	0x02
74df8bae1dSRodney W. Grimes #define TOLOG	0x04
75df8bae1dSRodney W. Grimes 
7682941964SPeter Wemm /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
773b1f7e7dSDag-Erling Smørgrav #define MAXNBUF	(sizeof(intmax_t) * NBBY + 1)
78df8bae1dSRodney W. Grimes 
798245f3f5SArchie Cobbs struct putchar_arg {
808245f3f5SArchie Cobbs 	int	flags;
81e2a09b26SPoul-Henning Kamp 	int	pri;
828245f3f5SArchie Cobbs 	struct	tty *tty;
838245f3f5SArchie Cobbs };
848245f3f5SArchie Cobbs 
858245f3f5SArchie Cobbs struct snprintf_arg {
868245f3f5SArchie Cobbs 	char	*str;
878245f3f5SArchie Cobbs 	size_t	remain;
888245f3f5SArchie Cobbs };
898245f3f5SArchie Cobbs 
90e2a09b26SPoul-Henning Kamp extern	int log_open;
91e2a09b26SPoul-Henning Kamp 
9205292ba2SArchie Cobbs struct	tty *constty;			/* pointer to console "window" tty */
9305292ba2SArchie Cobbs 
9487b6de2bSPoul-Henning Kamp static void (*v_putc)(int) = cnputc;	/* routine to putc on virtual console */
95e2a09b26SPoul-Henning Kamp static void  msglogchar(int c, int pri);
964d77a549SAlfred Perlstein static void  putchar(int ch, void *arg);
973b1f7e7dSDag-Erling Smørgrav static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len);
984d77a549SAlfred Perlstein static void  snprintf_func(int ch, void *arg);
99df8bae1dSRodney W. Grimes 
10087b6de2bSPoul-Henning Kamp static int consintr = 1;		/* Ok to handle console interrupts? */
101e796e00dSPoul-Henning Kamp static int msgbufmapped;		/* Set when safe to use msgbuf */
102e2a09b26SPoul-Henning Kamp int msgbuftrigger;
103df8bae1dSRodney W. Grimes 
104dbe620d3SDavid Malone static int      log_console_output = 1;
105af338beaSDavid Malone TUNABLE_INT("kern.log_console_output", &log_console_output);
106dbe620d3SDavid Malone SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
107dbe620d3SDavid Malone     &log_console_output, 0, "");
108dbe620d3SDavid Malone 
109df8bae1dSRodney W. Grimes /*
110df8bae1dSRodney W. Grimes  * Warn that a system table is full.
111df8bae1dSRodney W. Grimes  */
112df8bae1dSRodney W. Grimes void
113e2a09b26SPoul-Henning Kamp tablefull(const char *tab)
114df8bae1dSRodney W. Grimes {
115df8bae1dSRodney W. Grimes 
116df8bae1dSRodney W. Grimes 	log(LOG_ERR, "%s: table is full\n", tab);
117df8bae1dSRodney W. Grimes }
118df8bae1dSRodney W. Grimes 
119df8bae1dSRodney W. Grimes /*
120df8bae1dSRodney W. Grimes  * Uprintf prints to the controlling terminal for the current process.
121df8bae1dSRodney W. Grimes  * It may block if the tty queue is overfull.  No message is printed if
122df8bae1dSRodney W. Grimes  * the queue does not clear in a reasonable time.
123df8bae1dSRodney W. Grimes  */
124f1550d9dSDoug Rabson int
125df8bae1dSRodney W. Grimes uprintf(const char *fmt, ...)
126df8bae1dSRodney W. Grimes {
127b40ce416SJulian Elischer 	struct thread *td = curthread;
128b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
129df8bae1dSRodney W. Grimes 	va_list ap;
130791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
131f591779bSSeigo Tanimura 	int retval;
132df8bae1dSRodney W. Grimes 
133f591779bSSeigo Tanimura 	if (td == NULL || td == PCPU_GET(idlethread))
134f591779bSSeigo Tanimura 		return (0);
135f591779bSSeigo Tanimura 
136f591779bSSeigo Tanimura 	p = td->td_proc;
137f591779bSSeigo Tanimura 	PROC_LOCK(p);
138f591779bSSeigo Tanimura 	if ((p->p_flag & P_CONTROLT) == 0) {
139f591779bSSeigo Tanimura 		PROC_UNLOCK(p);
140f591779bSSeigo Tanimura 		return (0);
141f591779bSSeigo Tanimura 	}
142f591779bSSeigo Tanimura 	SESS_LOCK(p->p_session);
143791d77e0SPoul-Henning Kamp 	pca.tty = p->p_session->s_ttyp;
144f591779bSSeigo Tanimura 	SESS_UNLOCK(p->p_session);
145f591779bSSeigo Tanimura 	PROC_UNLOCK(p);
146f591779bSSeigo Tanimura 	if (pca.tty == NULL)
147f591779bSSeigo Tanimura 		return (0);
148791d77e0SPoul-Henning Kamp 	pca.flags = TOTTY;
149f591779bSSeigo Tanimura 	va_start(ap, fmt);
150f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
151df8bae1dSRodney W. Grimes 	va_end(ap);
152f591779bSSeigo Tanimura 
15358a24f79SPoul-Henning Kamp 	return (retval);
154df8bae1dSRodney W. Grimes }
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes /*
157df8bae1dSRodney W. Grimes  * tprintf prints on the controlling terminal associated
158a52585d7SPoul-Henning Kamp  * with the given session, possibly to the log as well.
159df8bae1dSRodney W. Grimes  */
160a52585d7SPoul-Henning Kamp void
161a52585d7SPoul-Henning Kamp tprintf(struct proc *p, int pri, const char *fmt, ...)
162df8bae1dSRodney W. Grimes {
163df8bae1dSRodney W. Grimes 	struct tty *tp = NULL;
164b5a2bad1SJohn Baldwin 	int flags = 0;
165df8bae1dSRodney W. Grimes 	va_list ap;
166791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
167b5a2bad1SJohn Baldwin 	struct session *sess = NULL;
168df8bae1dSRodney W. Grimes 
169e2a09b26SPoul-Henning Kamp 	if (pri != -1)
170a52585d7SPoul-Henning Kamp 		flags |= TOLOG;
171f591779bSSeigo Tanimura 	if (p != NULL) {
172f591779bSSeigo Tanimura 		PROC_LOCK(p);
173f591779bSSeigo Tanimura 		if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
174b5a2bad1SJohn Baldwin 			sess = p->p_session;
175b5a2bad1SJohn Baldwin 			SESS_LOCK(sess);
176f591779bSSeigo Tanimura 			PROC_UNLOCK(p);
177b5a2bad1SJohn Baldwin 			SESSHOLD(sess);
178b5a2bad1SJohn Baldwin 			tp = sess->s_ttyp;
179b5a2bad1SJohn Baldwin 			SESS_UNLOCK(sess);
180f591779bSSeigo Tanimura 			if (ttycheckoutq(tp, 0))
181f591779bSSeigo Tanimura 				flags |= TOTTY;
182f591779bSSeigo Tanimura 			else
183f591779bSSeigo Tanimura 				tp = NULL;
184f591779bSSeigo Tanimura 		} else
185f591779bSSeigo Tanimura 			PROC_UNLOCK(p);
186a52585d7SPoul-Henning Kamp 	}
187e2a09b26SPoul-Henning Kamp 	pca.pri = pri;
188791d77e0SPoul-Henning Kamp 	pca.tty = tp;
189791d77e0SPoul-Henning Kamp 	pca.flags = flags;
190a52585d7SPoul-Henning Kamp 	va_start(ap, fmt);
19174f1af01SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
192df8bae1dSRodney W. Grimes 	va_end(ap);
193b5a2bad1SJohn Baldwin 	if (sess != NULL) {
194b5a2bad1SJohn Baldwin 		SESS_LOCK(sess);
195b5a2bad1SJohn Baldwin 		SESSRELE(sess);
196b5a2bad1SJohn Baldwin 		SESS_UNLOCK(sess);
197f591779bSSeigo Tanimura 	}
198e2a09b26SPoul-Henning Kamp 	msgbuftrigger = 1;
199df8bae1dSRodney W. Grimes }
200df8bae1dSRodney W. Grimes 
201df8bae1dSRodney W. Grimes /*
202df8bae1dSRodney W. Grimes  * Ttyprintf displays a message on a tty; it should be used only by
203df8bae1dSRodney W. Grimes  * the tty driver, or anything that knows the underlying tty will not
204df8bae1dSRodney W. Grimes  * be revoke(2)'d away.  Other callers should use tprintf.
205df8bae1dSRodney W. Grimes  */
206f1550d9dSDoug Rabson int
207df8bae1dSRodney W. Grimes ttyprintf(struct tty *tp, const char *fmt, ...)
208df8bae1dSRodney W. Grimes {
209df8bae1dSRodney W. Grimes 	va_list ap;
210791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
211f1550d9dSDoug Rabson 	int retval;
212f1550d9dSDoug Rabson 
213df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
214791d77e0SPoul-Henning Kamp 	pca.tty = tp;
215791d77e0SPoul-Henning Kamp 	pca.flags = TOTTY;
216f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
217df8bae1dSRodney W. Grimes 	va_end(ap);
21858a24f79SPoul-Henning Kamp 	return (retval);
219df8bae1dSRodney W. Grimes }
220df8bae1dSRodney W. Grimes 
221df8bae1dSRodney W. Grimes /*
222df8bae1dSRodney W. Grimes  * Log writes to the log buffer, and guarantees not to sleep (so can be
223df8bae1dSRodney W. Grimes  * called by interrupt routines).  If there is no process reading the
224df8bae1dSRodney W. Grimes  * log yet, it writes to the console also.
225df8bae1dSRodney W. Grimes  */
2264d88c459SPoul-Henning Kamp void
227df8bae1dSRodney W. Grimes log(int level, const char *fmt, ...)
228df8bae1dSRodney W. Grimes {
229df8bae1dSRodney W. Grimes 	va_list ap;
230791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
231e2a09b26SPoul-Henning Kamp 
232791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
233e2a09b26SPoul-Henning Kamp 	pca.pri = level;
234e2a09b26SPoul-Henning Kamp 	pca.flags = log_open ? TOLOG : TOCONS;
235e2a09b26SPoul-Henning Kamp 
236e2a09b26SPoul-Henning Kamp 	va_start(ap, fmt);
23774f1af01SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
238df8bae1dSRodney W. Grimes 	va_end(ap);
239e2a09b26SPoul-Henning Kamp 
240b80e3b41SPoul-Henning Kamp 	msgbuftrigger = 1;
241df8bae1dSRodney W. Grimes }
242df8bae1dSRodney W. Grimes 
243e2a09b26SPoul-Henning Kamp #define CONSCHUNK 128
244e2a09b26SPoul-Henning Kamp 
245e2a09b26SPoul-Henning Kamp void
246e2a09b26SPoul-Henning Kamp log_console(struct uio *uio)
247df8bae1dSRodney W. Grimes {
248e2a09b26SPoul-Henning Kamp 	int c, i, error, iovlen, nl;
249e2a09b26SPoul-Henning Kamp 	struct uio muio;
250e2a09b26SPoul-Henning Kamp 	struct iovec *miov = NULL;
251e2a09b26SPoul-Henning Kamp 	char *consbuffer;
252e2a09b26SPoul-Henning Kamp 	int pri;
253df8bae1dSRodney W. Grimes 
254dbe620d3SDavid Malone 	if (!log_console_output)
255dbe620d3SDavid Malone 		return;
256dbe620d3SDavid Malone 
257e2a09b26SPoul-Henning Kamp 	pri = LOG_INFO | LOG_CONSOLE;
258e2a09b26SPoul-Henning Kamp 	muio = *uio;
259e2a09b26SPoul-Henning Kamp 	iovlen = uio->uio_iovcnt * sizeof (struct iovec);
260a163d034SWarner Losh 	MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
261a163d034SWarner Losh 	MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
26280208239SAlfred Perlstein 	bcopy(muio.uio_iov, miov, iovlen);
263e2a09b26SPoul-Henning Kamp 	muio.uio_iov = miov;
264e2a09b26SPoul-Henning Kamp 	uio = &muio;
265e2a09b26SPoul-Henning Kamp 
266e2a09b26SPoul-Henning Kamp 	nl = 0;
267e2a09b26SPoul-Henning Kamp 	while (uio->uio_resid > 0) {
268e2a09b26SPoul-Henning Kamp 		c = imin(uio->uio_resid, CONSCHUNK);
269e2a09b26SPoul-Henning Kamp 		error = uiomove(consbuffer, c, uio);
270e2a09b26SPoul-Henning Kamp 		if (error != 0)
2712f9752e9SPoul-Henning Kamp 			break;
272e2a09b26SPoul-Henning Kamp 		for (i = 0; i < c; i++) {
273e2a09b26SPoul-Henning Kamp 			msglogchar(consbuffer[i], pri);
274e2a09b26SPoul-Henning Kamp 			if (consbuffer[i] == '\n')
275e2a09b26SPoul-Henning Kamp 				nl = 1;
276e2a09b26SPoul-Henning Kamp 			else
277e2a09b26SPoul-Henning Kamp 				nl = 0;
278e2a09b26SPoul-Henning Kamp 		}
279e2a09b26SPoul-Henning Kamp 	}
280e2a09b26SPoul-Henning Kamp 	if (!nl)
281e2a09b26SPoul-Henning Kamp 		msglogchar('\n', pri);
282b80e3b41SPoul-Henning Kamp 	msgbuftrigger = 1;
283e2a09b26SPoul-Henning Kamp 	FREE(miov, M_TEMP);
284e2a09b26SPoul-Henning Kamp 	FREE(consbuffer, M_TEMP);
285e2a09b26SPoul-Henning Kamp 	return;
286df8bae1dSRodney W. Grimes }
287df8bae1dSRodney W. Grimes 
2886ddbf1e2SGary Palmer int
289df8bae1dSRodney W. Grimes printf(const char *fmt, ...)
290df8bae1dSRodney W. Grimes {
291df8bae1dSRodney W. Grimes 	va_list ap;
2924d88c459SPoul-Henning Kamp 	int savintr;
293791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
29465ed8cbdSJustin T. Gibbs 	int retval;
295df8bae1dSRodney W. Grimes 
296df8bae1dSRodney W. Grimes 	savintr = consintr;		/* disable interrupts */
297df8bae1dSRodney W. Grimes 	consintr = 0;
298df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
299791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
300791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
301e2a09b26SPoul-Henning Kamp 	pca.pri = -1;
30265ed8cbdSJustin T. Gibbs 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
303df8bae1dSRodney W. Grimes 	va_end(ap);
304df8bae1dSRodney W. Grimes 	if (!panicstr)
305b80e3b41SPoul-Henning Kamp 		msgbuftrigger = 1;
306df8bae1dSRodney W. Grimes 	consintr = savintr;		/* reenable interrupts */
30758a24f79SPoul-Henning Kamp 	return (retval);
308df8bae1dSRodney W. Grimes }
309df8bae1dSRodney W. Grimes 
310f1550d9dSDoug Rabson int
311791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap)
312791d77e0SPoul-Henning Kamp {
3134d88c459SPoul-Henning Kamp 	int savintr;
314791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
315f1550d9dSDoug Rabson 	int retval;
316791d77e0SPoul-Henning Kamp 
317791d77e0SPoul-Henning Kamp 	savintr = consintr;		/* disable interrupts */
318791d77e0SPoul-Henning Kamp 	consintr = 0;
319791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
320791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
321e2a09b26SPoul-Henning Kamp 	pca.pri = -1;
322f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
323791d77e0SPoul-Henning Kamp 	if (!panicstr)
324b80e3b41SPoul-Henning Kamp 		msgbuftrigger = 1;
325791d77e0SPoul-Henning Kamp 	consintr = savintr;		/* reenable interrupts */
32658a24f79SPoul-Henning Kamp 	return (retval);
327791d77e0SPoul-Henning Kamp }
328791d77e0SPoul-Henning Kamp 
329791d77e0SPoul-Henning Kamp /*
330791d77e0SPoul-Henning Kamp  * Print a character on console or users terminal.  If destination is
331e796e00dSPoul-Henning Kamp  * the console then the last bunch of characters are saved in msgbuf for
332791d77e0SPoul-Henning Kamp  * inspection later.
333791d77e0SPoul-Henning Kamp  */
334791d77e0SPoul-Henning Kamp static void
335791d77e0SPoul-Henning Kamp putchar(int c, void *arg)
336791d77e0SPoul-Henning Kamp {
337791d77e0SPoul-Henning Kamp 	struct putchar_arg *ap = (struct putchar_arg*) arg;
338791d77e0SPoul-Henning Kamp 	int flags = ap->flags;
339791d77e0SPoul-Henning Kamp 	struct tty *tp = ap->tty;
340791d77e0SPoul-Henning Kamp 	if (panicstr)
341791d77e0SPoul-Henning Kamp 		constty = NULL;
342791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && tp == NULL && constty) {
343791d77e0SPoul-Henning Kamp 		tp = constty;
344791d77e0SPoul-Henning Kamp 		flags |= TOTTY;
345791d77e0SPoul-Henning Kamp 	}
346791d77e0SPoul-Henning Kamp 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
347791d77e0SPoul-Henning Kamp 	    (flags & TOCONS) && tp == constty)
348791d77e0SPoul-Henning Kamp 		constty = NULL;
349791d77e0SPoul-Henning Kamp 	if ((flags & TOLOG))
350e2a09b26SPoul-Henning Kamp 		msglogchar(c, ap->pri);
351791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && constty == NULL && c != '\0')
352791d77e0SPoul-Henning Kamp 		(*v_putc)(c);
353791d77e0SPoul-Henning Kamp }
354791d77e0SPoul-Henning Kamp 
355791d77e0SPoul-Henning Kamp /*
356791d77e0SPoul-Henning Kamp  * Scaled down version of sprintf(3).
357791d77e0SPoul-Henning Kamp  */
358791d77e0SPoul-Henning Kamp int
359791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...)
360791d77e0SPoul-Henning Kamp {
361791d77e0SPoul-Henning Kamp 	int retval;
362791d77e0SPoul-Henning Kamp 	va_list ap;
363791d77e0SPoul-Henning Kamp 
364791d77e0SPoul-Henning Kamp 	va_start(ap, cfmt);
365791d77e0SPoul-Henning Kamp 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
366fe96d47dSPoul-Henning Kamp 	buf[retval] = '\0';
367791d77e0SPoul-Henning Kamp 	va_end(ap);
36858a24f79SPoul-Henning Kamp 	return (retval);
369791d77e0SPoul-Henning Kamp }
370791d77e0SPoul-Henning Kamp 
371791d77e0SPoul-Henning Kamp /*
37299237364SAndrey A. Chernov  * Scaled down version of vsprintf(3).
37399237364SAndrey A. Chernov  */
37499237364SAndrey A. Chernov int
37599237364SAndrey A. Chernov vsprintf(char *buf, const char *cfmt, va_list ap)
37699237364SAndrey A. Chernov {
37799237364SAndrey A. Chernov 	int retval;
37899237364SAndrey A. Chernov 
37999237364SAndrey A. Chernov 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
38099237364SAndrey A. Chernov 	buf[retval] = '\0';
38158a24f79SPoul-Henning Kamp 	return (retval);
38299237364SAndrey A. Chernov }
38399237364SAndrey A. Chernov 
38499237364SAndrey A. Chernov /*
3858245f3f5SArchie Cobbs  * Scaled down version of snprintf(3).
3868245f3f5SArchie Cobbs  */
3878245f3f5SArchie Cobbs int
3888245f3f5SArchie Cobbs snprintf(char *str, size_t size, const char *format, ...)
3898245f3f5SArchie Cobbs {
3908245f3f5SArchie Cobbs 	int retval;
3918245f3f5SArchie Cobbs 	va_list ap;
3928245f3f5SArchie Cobbs 
3938245f3f5SArchie Cobbs 	va_start(ap, format);
3948245f3f5SArchie Cobbs 	retval = vsnprintf(str, size, format, ap);
3958245f3f5SArchie Cobbs 	va_end(ap);
3968245f3f5SArchie Cobbs 	return(retval);
3978245f3f5SArchie Cobbs }
3988245f3f5SArchie Cobbs 
3998245f3f5SArchie Cobbs /*
4008245f3f5SArchie Cobbs  * Scaled down version of vsnprintf(3).
4018245f3f5SArchie Cobbs  */
4028245f3f5SArchie Cobbs int
4038245f3f5SArchie Cobbs vsnprintf(char *str, size_t size, const char *format, va_list ap)
4048245f3f5SArchie Cobbs {
4058245f3f5SArchie Cobbs 	struct snprintf_arg info;
4068245f3f5SArchie Cobbs 	int retval;
4078245f3f5SArchie Cobbs 
4088245f3f5SArchie Cobbs 	info.str = str;
4098245f3f5SArchie Cobbs 	info.remain = size;
4108245f3f5SArchie Cobbs 	retval = kvprintf(format, snprintf_func, &info, 10, ap);
4118245f3f5SArchie Cobbs 	if (info.remain >= 1)
4128245f3f5SArchie Cobbs 		*info.str++ = '\0';
41358a24f79SPoul-Henning Kamp 	return (retval);
4148245f3f5SArchie Cobbs }
4158245f3f5SArchie Cobbs 
4168751a8c7SPoul-Henning Kamp /*
4178751a8c7SPoul-Henning Kamp  * Kernel version which takes radix argument vsnprintf(3).
4188751a8c7SPoul-Henning Kamp  */
4198751a8c7SPoul-Henning Kamp int
4208751a8c7SPoul-Henning Kamp vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
4218751a8c7SPoul-Henning Kamp {
4228751a8c7SPoul-Henning Kamp 	struct snprintf_arg info;
4238751a8c7SPoul-Henning Kamp 	int retval;
4248751a8c7SPoul-Henning Kamp 
4258751a8c7SPoul-Henning Kamp 	info.str = str;
4268751a8c7SPoul-Henning Kamp 	info.remain = size;
4278751a8c7SPoul-Henning Kamp 	retval = kvprintf(format, snprintf_func, &info, radix, ap);
4288751a8c7SPoul-Henning Kamp 	if (info.remain >= 1)
4298751a8c7SPoul-Henning Kamp 		*info.str++ = '\0';
4308751a8c7SPoul-Henning Kamp 	return (retval);
4318751a8c7SPoul-Henning Kamp }
4328751a8c7SPoul-Henning Kamp 
4338245f3f5SArchie Cobbs static void
4348245f3f5SArchie Cobbs snprintf_func(int ch, void *arg)
4358245f3f5SArchie Cobbs {
4368245f3f5SArchie Cobbs 	struct snprintf_arg *const info = arg;
4378245f3f5SArchie Cobbs 
4388245f3f5SArchie Cobbs 	if (info->remain >= 2) {
4398245f3f5SArchie Cobbs 		*info->str++ = ch;
4408245f3f5SArchie Cobbs 		info->remain--;
4418245f3f5SArchie Cobbs 	}
4428245f3f5SArchie Cobbs }
4438245f3f5SArchie Cobbs 
4448245f3f5SArchie Cobbs /*
44582941964SPeter Wemm  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
44605292ba2SArchie Cobbs  * order; return an optional length and a pointer to the last character
44705292ba2SArchie Cobbs  * written in the buffer (i.e., the first character of the string).
44805292ba2SArchie Cobbs  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
449791d77e0SPoul-Henning Kamp  */
450791d77e0SPoul-Henning Kamp static char *
451f16a5176SDag-Erling Smørgrav ksprintn(char *nbuf, uintmax_t num, int base, int *lenp)
45205292ba2SArchie Cobbs {
4534d88c459SPoul-Henning Kamp 	char *p;
454791d77e0SPoul-Henning Kamp 
45505292ba2SArchie Cobbs 	p = nbuf;
456ad4f8dbdSArchie Cobbs 	*p = '\0';
457791d77e0SPoul-Henning Kamp 	do {
4583b1f7e7dSDag-Erling Smørgrav 		*++p = hex2ascii(num % base);
4593b1f7e7dSDag-Erling Smørgrav 	} while (num /= base);
4607d921a01SPeter Wemm 	if (lenp)
4617d921a01SPeter Wemm 		*lenp = p - nbuf;
4627d921a01SPeter Wemm 	return (p);
4637d921a01SPeter Wemm }
464791d77e0SPoul-Henning Kamp 
465df8bae1dSRodney W. Grimes /*
466df8bae1dSRodney W. Grimes  * Scaled down version of printf(3).
467df8bae1dSRodney W. Grimes  *
468df8bae1dSRodney W. Grimes  * Two additional formats:
469df8bae1dSRodney W. Grimes  *
470df8bae1dSRodney W. Grimes  * The format %b is supported to decode error registers.
471df8bae1dSRodney W. Grimes  * Its usage is:
472df8bae1dSRodney W. Grimes  *
473df8bae1dSRodney W. Grimes  *	printf("reg=%b\n", regval, "<base><arg>*");
474df8bae1dSRodney W. Grimes  *
475df8bae1dSRodney W. Grimes  * where <base> is the output base expressed as a control character, e.g.
476df8bae1dSRodney W. Grimes  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
477df8bae1dSRodney W. Grimes  * the first of which gives the bit number to be inspected (origin 1), and
478df8bae1dSRodney W. Grimes  * the next characters (up to a control character, i.e. a character <= 32),
479df8bae1dSRodney W. Grimes  * give the name of the register.  Thus:
480df8bae1dSRodney W. Grimes  *
4814e31a37bSGary Palmer  *	kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
482df8bae1dSRodney W. Grimes  *
483df8bae1dSRodney W. Grimes  * would produce output:
484df8bae1dSRodney W. Grimes  *
485df8bae1dSRodney W. Grimes  *	reg=3<BITTWO,BITONE>
486df8bae1dSRodney W. Grimes  *
487120f0783SPoul-Henning Kamp  * XXX:  %D  -- Hexdump, takes pointer and separator string:
488120f0783SPoul-Henning Kamp  *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
489120f0783SPoul-Henning Kamp  *		("%*D", len, ptr, " " -> XX XX XX XX ...
490df8bae1dSRodney W. Grimes  */
491791d77e0SPoul-Henning Kamp int
492791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
493df8bae1dSRodney W. Grimes {
494791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
495ad4f8dbdSArchie Cobbs 	char nbuf[MAXNBUF];
4963b1f7e7dSDag-Erling Smørgrav 	char *d;
4973b1f7e7dSDag-Erling Smørgrav 	const char *p, *percent, *q;
498120f0783SPoul-Henning Kamp 	u_char *up;
499791d77e0SPoul-Henning Kamp 	int ch, n;
5003b1f7e7dSDag-Erling Smørgrav 	uintmax_t num;
5017d921a01SPeter Wemm 	int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
5022bb95458SMaxime Henrion 	int jflag, tflag, zflag;
5034f20e4b1SPoul-Henning Kamp 	int dwidth;
504df8bae1dSRodney W. Grimes 	char padc;
505791d77e0SPoul-Henning Kamp 	int retval = 0;
506791d77e0SPoul-Henning Kamp 
5073b1f7e7dSDag-Erling Smørgrav 	num = 0;
508fe96d47dSPoul-Henning Kamp 	if (!func)
509791d77e0SPoul-Henning Kamp 		d = (char *) arg;
510791d77e0SPoul-Henning Kamp 	else
511fe96d47dSPoul-Henning Kamp 		d = NULL;
5128f5067baSDavid Greenman 
5138f5067baSDavid Greenman 	if (fmt == NULL)
5142336b9d7SBruce Evans 		fmt = "(fmt null)\n";
515b4b2f81eSPoul-Henning Kamp 
516120f0783SPoul-Henning Kamp 	if (radix < 2 || radix > 36)
517b4b2f81eSPoul-Henning Kamp 		radix = 10;
518b4b2f81eSPoul-Henning Kamp 
519df8bae1dSRodney W. Grimes 	for (;;) {
520df8bae1dSRodney W. Grimes 		padc = ' ';
521df8bae1dSRodney W. Grimes 		width = 0;
522e0c95ed9SBruce Evans 		while ((ch = (u_char)*fmt++) != '%') {
523df8bae1dSRodney W. Grimes 			if (ch == '\0')
52458a24f79SPoul-Henning Kamp 				return (retval);
525791d77e0SPoul-Henning Kamp 			PCHAR(ch);
526df8bae1dSRodney W. Grimes 		}
5273b1f7e7dSDag-Erling Smørgrav 		percent = fmt - 1;
5287d921a01SPeter Wemm 		qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
5294f20e4b1SPoul-Henning Kamp 		sign = 0; dot = 0; dwidth = 0;
5302bb95458SMaxime Henrion 		jflag = 0; tflag = 0; zflag = 0;
531e0c95ed9SBruce Evans reswitch:	switch (ch = (u_char)*fmt++) {
5324830092aSPoul-Henning Kamp 		case '.':
5334830092aSPoul-Henning Kamp 			dot = 1;
5344830092aSPoul-Henning Kamp 			goto reswitch;
535791d77e0SPoul-Henning Kamp 		case '#':
536791d77e0SPoul-Henning Kamp 			sharpflag = 1;
537791d77e0SPoul-Henning Kamp 			goto reswitch;
538791d77e0SPoul-Henning Kamp 		case '+':
539791d77e0SPoul-Henning Kamp 			sign = 1;
540791d77e0SPoul-Henning Kamp 			goto reswitch;
541791d77e0SPoul-Henning Kamp 		case '-':
542791d77e0SPoul-Henning Kamp 			ladjust = 1;
543791d77e0SPoul-Henning Kamp 			goto reswitch;
544791d77e0SPoul-Henning Kamp 		case '%':
545791d77e0SPoul-Henning Kamp 			PCHAR(ch);
546791d77e0SPoul-Henning Kamp 			break;
547791d77e0SPoul-Henning Kamp 		case '*':
548ed71c342SPoul-Henning Kamp 			if (!dot) {
549791d77e0SPoul-Henning Kamp 				width = va_arg(ap, int);
550791d77e0SPoul-Henning Kamp 				if (width < 0) {
551791d77e0SPoul-Henning Kamp 					ladjust = !ladjust;
552791d77e0SPoul-Henning Kamp 					width = -width;
553791d77e0SPoul-Henning Kamp 				}
554ed71c342SPoul-Henning Kamp 			} else {
555ed71c342SPoul-Henning Kamp 				dwidth = va_arg(ap, int);
556ed71c342SPoul-Henning Kamp 			}
557791d77e0SPoul-Henning Kamp 			goto reswitch;
558df8bae1dSRodney W. Grimes 		case '0':
5594f20e4b1SPoul-Henning Kamp 			if (!dot) {
560df8bae1dSRodney W. Grimes 				padc = '0';
561df8bae1dSRodney W. Grimes 				goto reswitch;
5624f20e4b1SPoul-Henning Kamp 			}
563df8bae1dSRodney W. Grimes 		case '1': case '2': case '3': case '4':
564df8bae1dSRodney W. Grimes 		case '5': case '6': case '7': case '8': case '9':
5654f20e4b1SPoul-Henning Kamp 				for (n = 0;; ++fmt) {
5664f20e4b1SPoul-Henning Kamp 					n = n * 10 + ch - '0';
567df8bae1dSRodney W. Grimes 					ch = *fmt;
568df8bae1dSRodney W. Grimes 					if (ch < '0' || ch > '9')
569df8bae1dSRodney W. Grimes 						break;
570df8bae1dSRodney W. Grimes 				}
5714f20e4b1SPoul-Henning Kamp 			if (dot)
5724f20e4b1SPoul-Henning Kamp 				dwidth = n;
5734f20e4b1SPoul-Henning Kamp 			else
5744f20e4b1SPoul-Henning Kamp 				width = n;
575df8bae1dSRodney W. Grimes 			goto reswitch;
576df8bae1dSRodney W. Grimes 		case 'b':
577377a66bcSJohn Baldwin 			num = (u_int)va_arg(ap, int);
578df8bae1dSRodney W. Grimes 			p = va_arg(ap, char *);
5793b1f7e7dSDag-Erling Smørgrav 			for (q = ksprintn(nbuf, num, *p++, NULL); *q;)
580791d77e0SPoul-Henning Kamp 				PCHAR(*q--);
581df8bae1dSRodney W. Grimes 
5823b1f7e7dSDag-Erling Smørgrav 			if (num == 0)
583df8bae1dSRodney W. Grimes 				break;
584df8bae1dSRodney W. Grimes 
585797f2d22SPoul-Henning Kamp 			for (tmp = 0; *p;) {
586797f2d22SPoul-Henning Kamp 				n = *p++;
5873b1f7e7dSDag-Erling Smørgrav 				if (num & (1 << (n - 1))) {
588791d77e0SPoul-Henning Kamp 					PCHAR(tmp ? ',' : '<');
589df8bae1dSRodney W. Grimes 					for (; (n = *p) > ' '; ++p)
590791d77e0SPoul-Henning Kamp 						PCHAR(n);
591df8bae1dSRodney W. Grimes 					tmp = 1;
592df8bae1dSRodney W. Grimes 				} else
593df8bae1dSRodney W. Grimes 					for (; *p > ' '; ++p)
594df8bae1dSRodney W. Grimes 						continue;
595df8bae1dSRodney W. Grimes 			}
596df8bae1dSRodney W. Grimes 			if (tmp)
597791d77e0SPoul-Henning Kamp 				PCHAR('>');
598df8bae1dSRodney W. Grimes 			break;
599df8bae1dSRodney W. Grimes 		case 'c':
600791d77e0SPoul-Henning Kamp 			PCHAR(va_arg(ap, int));
601df8bae1dSRodney W. Grimes 			break;
602120f0783SPoul-Henning Kamp 		case 'D':
603120f0783SPoul-Henning Kamp 			up = va_arg(ap, u_char *);
604120f0783SPoul-Henning Kamp 			p = va_arg(ap, char *);
605120f0783SPoul-Henning Kamp 			if (!width)
606120f0783SPoul-Henning Kamp 				width = 16;
607120f0783SPoul-Henning Kamp 			while(width--) {
608120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up >> 4));
609120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up & 0x0f));
610120f0783SPoul-Henning Kamp 				up++;
611120f0783SPoul-Henning Kamp 				if (width)
612120f0783SPoul-Henning Kamp 					for (q=p;*q;q++)
613120f0783SPoul-Henning Kamp 						PCHAR(*q);
614120f0783SPoul-Henning Kamp 			}
615120f0783SPoul-Henning Kamp 			break;
616df8bae1dSRodney W. Grimes 		case 'd':
617e0b74464SWarner Losh 		case 'i':
618df8bae1dSRodney W. Grimes 			base = 10;
6193b1f7e7dSDag-Erling Smørgrav 			sign = 1;
6203b1f7e7dSDag-Erling Smørgrav 			goto handle_sign;
6213b1f7e7dSDag-Erling Smørgrav 		case 'j':
6223b1f7e7dSDag-Erling Smørgrav 			jflag = 1;
6233b1f7e7dSDag-Erling Smørgrav 			goto reswitch;
624791d77e0SPoul-Henning Kamp 		case 'l':
625301ca4ffSBrian Feldman 			if (lflag) {
626301ca4ffSBrian Feldman 				lflag = 0;
627301ca4ffSBrian Feldman 				qflag = 1;
628301ca4ffSBrian Feldman 			} else
629791d77e0SPoul-Henning Kamp 				lflag = 1;
630791d77e0SPoul-Henning Kamp 			goto reswitch;
6313b1f7e7dSDag-Erling Smørgrav 		case 'n':
6323b1f7e7dSDag-Erling Smørgrav 			if (jflag)
6333b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, intmax_t *)) = retval;
6343b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
6353b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, quad_t *)) = retval;
6367d921a01SPeter Wemm 			else if (lflag)
6373b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, long *)) = retval;
6384578a2e6SMaxime Henrion 			else if (zflag)
6394578a2e6SMaxime Henrion 				*(va_arg(ap, size_t *)) = retval;
6407d921a01SPeter Wemm 			else
6413b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, int *)) = retval;
6423b1f7e7dSDag-Erling Smørgrav 			break;
6433b1f7e7dSDag-Erling Smørgrav 		case 'o':
644df8bae1dSRodney W. Grimes 			base = 8;
6453b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
64612d17f65SPoul-Henning Kamp 		case 'p':
64712d17f65SPoul-Henning Kamp 			base = 16;
648c41141b0SBruce Evans 			sharpflag = (width == 0);
6493b1f7e7dSDag-Erling Smørgrav 			sign = 0;
6503b1f7e7dSDag-Erling Smørgrav 			num = (uintptr_t)va_arg(ap, void *);
6513b1f7e7dSDag-Erling Smørgrav 			goto number;
6527d921a01SPeter Wemm 		case 'q':
6537d921a01SPeter Wemm 			qflag = 1;
6547d921a01SPeter Wemm 			goto reswitch;
655e0c38587SBruce Evans 		case 'r':
656e0c38587SBruce Evans 			base = radix;
6573b1f7e7dSDag-Erling Smørgrav 			if (sign)
6583b1f7e7dSDag-Erling Smørgrav 				goto handle_sign;
6593b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
660791d77e0SPoul-Henning Kamp 		case 's':
661791d77e0SPoul-Henning Kamp 			p = va_arg(ap, char *);
662791d77e0SPoul-Henning Kamp 			if (p == NULL)
663791d77e0SPoul-Henning Kamp 				p = "(null)";
6644830092aSPoul-Henning Kamp 			if (!dot)
6654830092aSPoul-Henning Kamp 				n = strlen (p);
6664830092aSPoul-Henning Kamp 			else
6674f20e4b1SPoul-Henning Kamp 				for (n = 0; n < dwidth && p[n]; n++)
6684830092aSPoul-Henning Kamp 					continue;
6694f20e4b1SPoul-Henning Kamp 
6704830092aSPoul-Henning Kamp 			width -= n;
6714f20e4b1SPoul-Henning Kamp 
672791d77e0SPoul-Henning Kamp 			if (!ladjust && width > 0)
673791d77e0SPoul-Henning Kamp 				while (width--)
674791d77e0SPoul-Henning Kamp 					PCHAR(padc);
6754830092aSPoul-Henning Kamp 			while (n--)
676791d77e0SPoul-Henning Kamp 				PCHAR(*p++);
677791d77e0SPoul-Henning Kamp 			if (ladjust && width > 0)
678791d77e0SPoul-Henning Kamp 				while (width--)
679791d77e0SPoul-Henning Kamp 					PCHAR(padc);
680791d77e0SPoul-Henning Kamp 			break;
6812bb95458SMaxime Henrion 		case 't':
6822bb95458SMaxime Henrion 			tflag = 1;
6832bb95458SMaxime Henrion 			goto reswitch;
684f53dbe97SBruce Evans 		case 'u':
685f53dbe97SBruce Evans 			base = 10;
6863b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
687df8bae1dSRodney W. Grimes 		case 'x':
688aa998012SMike Smith 		case 'X':
689df8bae1dSRodney W. Grimes 			base = 16;
6903b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
6914578a2e6SMaxime Henrion 		case 'y':
692e0c38587SBruce Evans 			base = 16;
69385594430SJohn Baldwin 			sign = 1;
6943b1f7e7dSDag-Erling Smørgrav 			goto handle_sign;
6954578a2e6SMaxime Henrion 		case 'z':
6964578a2e6SMaxime Henrion 			zflag = 1;
6974578a2e6SMaxime Henrion 			goto reswitch;
6983b1f7e7dSDag-Erling Smørgrav handle_nosign:
6993b1f7e7dSDag-Erling Smørgrav 			sign = 0;
7003b1f7e7dSDag-Erling Smørgrav 			if (jflag)
7013b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, uintmax_t);
7023b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
7033b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_quad_t);
7042bb95458SMaxime Henrion 			else if (tflag)
7052bb95458SMaxime Henrion 				num = va_arg(ap, ptrdiff_t);
7063b1f7e7dSDag-Erling Smørgrav 			else if (lflag)
7073b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_long);
7084578a2e6SMaxime Henrion 			else if (zflag)
7094578a2e6SMaxime Henrion 				num = va_arg(ap, size_t);
7103b1f7e7dSDag-Erling Smørgrav 			else
7113b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_int);
712e0c38587SBruce Evans 			goto number;
7133b1f7e7dSDag-Erling Smørgrav handle_sign:
7143b1f7e7dSDag-Erling Smørgrav 			if (jflag)
7153b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, intmax_t);
7163b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
7173b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, quad_t);
7182bb95458SMaxime Henrion 			else if (tflag)
7192bb95458SMaxime Henrion 				num = va_arg(ap, ptrdiff_t);
7203b1f7e7dSDag-Erling Smørgrav 			else if (lflag)
7213b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, long);
7224578a2e6SMaxime Henrion 			else if (zflag)
7234578a2e6SMaxime Henrion 				num = va_arg(ap, size_t);
7243b1f7e7dSDag-Erling Smørgrav 			else
7253b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, int);
7267d921a01SPeter Wemm number:
7273b1f7e7dSDag-Erling Smørgrav 			if (sign && (intmax_t)num < 0) {
7287d921a01SPeter Wemm 				neg = 1;
7293b1f7e7dSDag-Erling Smørgrav 				num = -(intmax_t)num;
7307d921a01SPeter Wemm 			}
7313b1f7e7dSDag-Erling Smørgrav 			p = ksprintn(nbuf, num, base, &tmp);
7323b1f7e7dSDag-Erling Smørgrav 			if (sharpflag && num != 0) {
733791d77e0SPoul-Henning Kamp 				if (base == 8)
734791d77e0SPoul-Henning Kamp 					tmp++;
735791d77e0SPoul-Henning Kamp 				else if (base == 16)
736791d77e0SPoul-Henning Kamp 					tmp += 2;
737791d77e0SPoul-Henning Kamp 			}
738791d77e0SPoul-Henning Kamp 			if (neg)
739791d77e0SPoul-Henning Kamp 				tmp++;
740791d77e0SPoul-Henning Kamp 
741791d77e0SPoul-Henning Kamp 			if (!ladjust && width && (width -= tmp) > 0)
742df8bae1dSRodney W. Grimes 				while (width--)
743791d77e0SPoul-Henning Kamp 					PCHAR(padc);
744791d77e0SPoul-Henning Kamp 			if (neg)
745791d77e0SPoul-Henning Kamp 				PCHAR('-');
7463b1f7e7dSDag-Erling Smørgrav 			if (sharpflag && num != 0) {
747791d77e0SPoul-Henning Kamp 				if (base == 8) {
748791d77e0SPoul-Henning Kamp 					PCHAR('0');
749791d77e0SPoul-Henning Kamp 				} else if (base == 16) {
750791d77e0SPoul-Henning Kamp 					PCHAR('0');
751791d77e0SPoul-Henning Kamp 					PCHAR('x');
752791d77e0SPoul-Henning Kamp 				}
753791d77e0SPoul-Henning Kamp 			}
754791d77e0SPoul-Henning Kamp 
755797f2d22SPoul-Henning Kamp 			while (*p)
756791d77e0SPoul-Henning Kamp 				PCHAR(*p--);
757791d77e0SPoul-Henning Kamp 
758791d77e0SPoul-Henning Kamp 			if (ladjust && width && (width -= tmp) > 0)
759791d77e0SPoul-Henning Kamp 				while (width--)
760791d77e0SPoul-Henning Kamp 					PCHAR(padc);
761791d77e0SPoul-Henning Kamp 
762df8bae1dSRodney W. Grimes 			break;
763df8bae1dSRodney W. Grimes 		default:
7643b1f7e7dSDag-Erling Smørgrav 			while (percent < fmt)
7653b1f7e7dSDag-Erling Smørgrav 				PCHAR(*percent++);
766791d77e0SPoul-Henning Kamp 			break;
767df8bae1dSRodney W. Grimes 		}
768df8bae1dSRodney W. Grimes 	}
769791d77e0SPoul-Henning Kamp #undef PCHAR
770df8bae1dSRodney W. Grimes }
771df8bae1dSRodney W. Grimes 
772df8bae1dSRodney W. Grimes /*
773e2a09b26SPoul-Henning Kamp  * Put character in log buffer with a particular priority.
774df8bae1dSRodney W. Grimes  */
775df8bae1dSRodney W. Grimes static void
776e2a09b26SPoul-Henning Kamp msglogchar(int c, int pri)
777e2a09b26SPoul-Henning Kamp {
778e2a09b26SPoul-Henning Kamp 	static int lastpri = -1;
779e2a09b26SPoul-Henning Kamp 	static int dangling;
780e2a09b26SPoul-Henning Kamp 	char nbuf[MAXNBUF];
781e2a09b26SPoul-Henning Kamp 	char *p;
782e2a09b26SPoul-Henning Kamp 
783e2a09b26SPoul-Henning Kamp 	if (!msgbufmapped)
784e2a09b26SPoul-Henning Kamp 		return;
785e2a09b26SPoul-Henning Kamp 	if (c == '\0' || c == '\r')
786e2a09b26SPoul-Henning Kamp 		return;
787e2a09b26SPoul-Henning Kamp 	if (pri != -1 && pri != lastpri) {
788e2a09b26SPoul-Henning Kamp 		if (dangling) {
7894784a469SIan Dowse 			msgbuf_addchar(msgbufp, '\n');
790e2a09b26SPoul-Henning Kamp 			dangling = 0;
791e2a09b26SPoul-Henning Kamp 		}
7924784a469SIan Dowse 		msgbuf_addchar(msgbufp, '<');
7933b1f7e7dSDag-Erling Smørgrav 		for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL); *p;)
7944784a469SIan Dowse 			msgbuf_addchar(msgbufp, *p--);
7954784a469SIan Dowse 		msgbuf_addchar(msgbufp, '>');
796e2a09b26SPoul-Henning Kamp 		lastpri = pri;
797e2a09b26SPoul-Henning Kamp 	}
7984784a469SIan Dowse 	msgbuf_addchar(msgbufp, c);
799e2a09b26SPoul-Henning Kamp 	if (c == '\n') {
800e2a09b26SPoul-Henning Kamp 		dangling = 0;
801e2a09b26SPoul-Henning Kamp 		lastpri = -1;
802e2a09b26SPoul-Henning Kamp 	} else {
803e2a09b26SPoul-Henning Kamp 		dangling = 1;
804e2a09b26SPoul-Henning Kamp 	}
805e2a09b26SPoul-Henning Kamp }
806e2a09b26SPoul-Henning Kamp 
807e796e00dSPoul-Henning Kamp void
80801ee4395SThomas Moestl msgbufinit(void *ptr, int size)
809e796e00dSPoul-Henning Kamp {
810e796e00dSPoul-Henning Kamp 	char *cp;
811eb9d435aSJonathan Lemon 	static struct msgbuf *oldp = NULL;
812e796e00dSPoul-Henning Kamp 
813cca8f980SIan Dowse 	size -= sizeof(*msgbufp);
814e796e00dSPoul-Henning Kamp 	cp = (char *)ptr;
815cca8f980SIan Dowse 	msgbufp = (struct msgbuf *)(cp + size);
8164784a469SIan Dowse 	msgbuf_reinit(msgbufp, cp, size);
817eb9d435aSJonathan Lemon 	if (msgbufmapped && oldp != msgbufp)
8184784a469SIan Dowse 		msgbuf_copy(oldp, msgbufp);
819e796e00dSPoul-Henning Kamp 	msgbufmapped = 1;
820eb9d435aSJonathan Lemon 	oldp = msgbufp;
821e796e00dSPoul-Henning Kamp }
822e796e00dSPoul-Henning Kamp 
823d0615c64SAndrew R. Reiter SYSCTL_DECL(_security_bsd);
8246f3933faSRobert Watson 
8256f3933faSRobert Watson static int unprivileged_read_msgbuf = 1;
826d0615c64SAndrew R. Reiter SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf,
8276f3933faSRobert Watson     CTLFLAG_RW, &unprivileged_read_msgbuf, 0,
8286f3933faSRobert Watson     "Unprivileged processes may read the kernel message buffer");
8296f3933faSRobert Watson 
830948d3d94SThomas Moestl /* Sysctls for accessing/clearing the msgbuf */
831948d3d94SThomas Moestl static int
832948d3d94SThomas Moestl sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
833948d3d94SThomas Moestl {
8344784a469SIan Dowse 	char buf[128];
8354784a469SIan Dowse 	u_int seq;
8364784a469SIan Dowse 	int error, len;
837948d3d94SThomas Moestl 
8386f3933faSRobert Watson 	if (!unprivileged_read_msgbuf) {
83944731cabSJohn Baldwin 		error = suser(req->td);
8406f3933faSRobert Watson 		if (error)
8416f3933faSRobert Watson 			return (error);
8426f3933faSRobert Watson 	}
8436f3933faSRobert Watson 
8444784a469SIan Dowse 	/* Read the whole buffer, one chunk at a time. */
8454784a469SIan Dowse 	msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
8464784a469SIan Dowse 	while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) {
8474784a469SIan Dowse 		error = sysctl_handle_opaque(oidp, buf, len, req);
848948d3d94SThomas Moestl 		if (error)
849948d3d94SThomas Moestl 			return (error);
850948d3d94SThomas Moestl 	}
8514784a469SIan Dowse 	return (0);
852948d3d94SThomas Moestl }
853948d3d94SThomas Moestl 
854948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
855948d3d94SThomas Moestl     0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
856948d3d94SThomas Moestl 
8574784a469SIan Dowse static int msgbuf_clearflag;
858948d3d94SThomas Moestl 
859948d3d94SThomas Moestl static int
860948d3d94SThomas Moestl sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
861948d3d94SThomas Moestl {
862948d3d94SThomas Moestl 	int error;
863948d3d94SThomas Moestl 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
864948d3d94SThomas Moestl 	if (!error && req->newptr) {
8654784a469SIan Dowse 		msgbuf_clear(msgbufp);
8664784a469SIan Dowse 		msgbuf_clearflag = 0;
867948d3d94SThomas Moestl 	}
868948d3d94SThomas Moestl 	return (error);
869948d3d94SThomas Moestl }
870948d3d94SThomas Moestl 
871948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
8724784a469SIan Dowse     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clearflag, 0,
873948d3d94SThomas Moestl     sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
874948d3d94SThomas Moestl 
875e796e00dSPoul-Henning Kamp #ifdef DDB
876e796e00dSPoul-Henning Kamp 
877e796e00dSPoul-Henning Kamp DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
878e796e00dSPoul-Henning Kamp {
879e796e00dSPoul-Henning Kamp 	int i, j;
880e796e00dSPoul-Henning Kamp 
881e796e00dSPoul-Henning Kamp 	if (!msgbufmapped) {
882e796e00dSPoul-Henning Kamp 		db_printf("msgbuf not mapped yet\n");
883e796e00dSPoul-Henning Kamp 		return;
884e796e00dSPoul-Henning Kamp 	}
885e796e00dSPoul-Henning Kamp 	db_printf("msgbufp = %p\n", msgbufp);
8864784a469SIan Dowse 	db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n",
8874784a469SIan Dowse 	    msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq,
8884784a469SIan Dowse 	    msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum);
889e796e00dSPoul-Henning Kamp 	for (i = 0; i < msgbufp->msg_size; i++) {
8904784a469SIan Dowse 		j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq);
891e796e00dSPoul-Henning Kamp 		db_printf("%c", msgbufp->msg_ptr[j]);
892e796e00dSPoul-Henning Kamp 	}
893e796e00dSPoul-Henning Kamp 	db_printf("\n");
894e796e00dSPoul-Henning Kamp }
895e796e00dSPoul-Henning Kamp 
896e796e00dSPoul-Henning Kamp #endif /* DDB */
897