xref: /freebsd/sys/kern/subr_prf.c (revision 677b542ea243380af64822b30e4ef5f7a6d978ee)
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);
96e2a09b26SPoul-Henning Kamp static void  msgaddchar(int c, void *dummy);
976205bf31SIan Dowse static u_int msgbufcksum(char *cp, size_t size, u_int cksum);
984d77a549SAlfred Perlstein static void  putchar(int ch, void *arg);
993b1f7e7dSDag-Erling Smørgrav static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len);
1004d77a549SAlfred Perlstein static void  snprintf_func(int ch, void *arg);
101df8bae1dSRodney W. Grimes 
10287b6de2bSPoul-Henning Kamp static int consintr = 1;		/* Ok to handle console interrupts? */
103e796e00dSPoul-Henning Kamp static int msgbufmapped;		/* Set when safe to use msgbuf */
104e2a09b26SPoul-Henning Kamp int msgbuftrigger;
105df8bae1dSRodney W. Grimes 
106dbe620d3SDavid Malone static int      log_console_output = 1;
107af338beaSDavid Malone TUNABLE_INT("kern.log_console_output", &log_console_output);
108dbe620d3SDavid Malone SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
109dbe620d3SDavid Malone     &log_console_output, 0, "");
110dbe620d3SDavid Malone 
111df8bae1dSRodney W. Grimes /*
112df8bae1dSRodney W. Grimes  * Warn that a system table is full.
113df8bae1dSRodney W. Grimes  */
114df8bae1dSRodney W. Grimes void
115e2a09b26SPoul-Henning Kamp tablefull(const char *tab)
116df8bae1dSRodney W. Grimes {
117df8bae1dSRodney W. Grimes 
118df8bae1dSRodney W. Grimes 	log(LOG_ERR, "%s: table is full\n", tab);
119df8bae1dSRodney W. Grimes }
120df8bae1dSRodney W. Grimes 
121df8bae1dSRodney W. Grimes /*
122df8bae1dSRodney W. Grimes  * Uprintf prints to the controlling terminal for the current process.
123df8bae1dSRodney W. Grimes  * It may block if the tty queue is overfull.  No message is printed if
124df8bae1dSRodney W. Grimes  * the queue does not clear in a reasonable time.
125df8bae1dSRodney W. Grimes  */
126f1550d9dSDoug Rabson int
127df8bae1dSRodney W. Grimes uprintf(const char *fmt, ...)
128df8bae1dSRodney W. Grimes {
129b40ce416SJulian Elischer 	struct thread *td = curthread;
130b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
131df8bae1dSRodney W. Grimes 	va_list ap;
132791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
133f591779bSSeigo Tanimura 	int retval;
134df8bae1dSRodney W. Grimes 
135f591779bSSeigo Tanimura 	if (td == NULL || td == PCPU_GET(idlethread))
136f591779bSSeigo Tanimura 		return (0);
137f591779bSSeigo Tanimura 
138f591779bSSeigo Tanimura 	p = td->td_proc;
139f591779bSSeigo Tanimura 	PROC_LOCK(p);
140f591779bSSeigo Tanimura 	if ((p->p_flag & P_CONTROLT) == 0) {
141f591779bSSeigo Tanimura 		PROC_UNLOCK(p);
142f591779bSSeigo Tanimura 		return (0);
143f591779bSSeigo Tanimura 	}
144f591779bSSeigo Tanimura 	SESS_LOCK(p->p_session);
145791d77e0SPoul-Henning Kamp 	pca.tty = p->p_session->s_ttyp;
146f591779bSSeigo Tanimura 	SESS_UNLOCK(p->p_session);
147f591779bSSeigo Tanimura 	PROC_UNLOCK(p);
148f591779bSSeigo Tanimura 	if (pca.tty == NULL)
149f591779bSSeigo Tanimura 		return (0);
150791d77e0SPoul-Henning Kamp 	pca.flags = TOTTY;
151f591779bSSeigo Tanimura 	va_start(ap, fmt);
152f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
153df8bae1dSRodney W. Grimes 	va_end(ap);
154f591779bSSeigo Tanimura 
15558a24f79SPoul-Henning Kamp 	return (retval);
156df8bae1dSRodney W. Grimes }
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes /*
159df8bae1dSRodney W. Grimes  * tprintf prints on the controlling terminal associated
160a52585d7SPoul-Henning Kamp  * with the given session, possibly to the log as well.
161df8bae1dSRodney W. Grimes  */
162a52585d7SPoul-Henning Kamp void
163a52585d7SPoul-Henning Kamp tprintf(struct proc *p, int pri, const char *fmt, ...)
164df8bae1dSRodney W. Grimes {
165df8bae1dSRodney W. Grimes 	struct tty *tp = NULL;
166b5a2bad1SJohn Baldwin 	int flags = 0;
167df8bae1dSRodney W. Grimes 	va_list ap;
168791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
169b5a2bad1SJohn Baldwin 	struct session *sess = NULL;
170df8bae1dSRodney W. Grimes 
171e2a09b26SPoul-Henning Kamp 	if (pri != -1)
172a52585d7SPoul-Henning Kamp 		flags |= TOLOG;
173f591779bSSeigo Tanimura 	if (p != NULL) {
174f591779bSSeigo Tanimura 		PROC_LOCK(p);
175f591779bSSeigo Tanimura 		if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
176b5a2bad1SJohn Baldwin 			sess = p->p_session;
177b5a2bad1SJohn Baldwin 			SESS_LOCK(sess);
178f591779bSSeigo Tanimura 			PROC_UNLOCK(p);
179b5a2bad1SJohn Baldwin 			SESSHOLD(sess);
180b5a2bad1SJohn Baldwin 			tp = sess->s_ttyp;
181b5a2bad1SJohn Baldwin 			SESS_UNLOCK(sess);
182f591779bSSeigo Tanimura 			if (ttycheckoutq(tp, 0))
183f591779bSSeigo Tanimura 				flags |= TOTTY;
184f591779bSSeigo Tanimura 			else
185f591779bSSeigo Tanimura 				tp = NULL;
186f591779bSSeigo Tanimura 		} else
187f591779bSSeigo Tanimura 			PROC_UNLOCK(p);
188a52585d7SPoul-Henning Kamp 	}
189e2a09b26SPoul-Henning Kamp 	pca.pri = pri;
190791d77e0SPoul-Henning Kamp 	pca.tty = tp;
191791d77e0SPoul-Henning Kamp 	pca.flags = flags;
192a52585d7SPoul-Henning Kamp 	va_start(ap, fmt);
19374f1af01SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
194df8bae1dSRodney W. Grimes 	va_end(ap);
195b5a2bad1SJohn Baldwin 	if (sess != NULL) {
196b5a2bad1SJohn Baldwin 		SESS_LOCK(sess);
197b5a2bad1SJohn Baldwin 		SESSRELE(sess);
198b5a2bad1SJohn Baldwin 		SESS_UNLOCK(sess);
199f591779bSSeigo Tanimura 	}
200e2a09b26SPoul-Henning Kamp 	msgbuftrigger = 1;
201df8bae1dSRodney W. Grimes }
202df8bae1dSRodney W. Grimes 
203df8bae1dSRodney W. Grimes /*
204df8bae1dSRodney W. Grimes  * Ttyprintf displays a message on a tty; it should be used only by
205df8bae1dSRodney W. Grimes  * the tty driver, or anything that knows the underlying tty will not
206df8bae1dSRodney W. Grimes  * be revoke(2)'d away.  Other callers should use tprintf.
207df8bae1dSRodney W. Grimes  */
208f1550d9dSDoug Rabson int
209df8bae1dSRodney W. Grimes ttyprintf(struct tty *tp, const char *fmt, ...)
210df8bae1dSRodney W. Grimes {
211df8bae1dSRodney W. Grimes 	va_list ap;
212791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
213f1550d9dSDoug Rabson 	int retval;
214f1550d9dSDoug Rabson 
215df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
216791d77e0SPoul-Henning Kamp 	pca.tty = tp;
217791d77e0SPoul-Henning Kamp 	pca.flags = TOTTY;
218f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
219df8bae1dSRodney W. Grimes 	va_end(ap);
22058a24f79SPoul-Henning Kamp 	return (retval);
221df8bae1dSRodney W. Grimes }
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes /*
224df8bae1dSRodney W. Grimes  * Log writes to the log buffer, and guarantees not to sleep (so can be
225df8bae1dSRodney W. Grimes  * called by interrupt routines).  If there is no process reading the
226df8bae1dSRodney W. Grimes  * log yet, it writes to the console also.
227df8bae1dSRodney W. Grimes  */
2284d88c459SPoul-Henning Kamp void
229df8bae1dSRodney W. Grimes log(int level, const char *fmt, ...)
230df8bae1dSRodney W. Grimes {
231df8bae1dSRodney W. Grimes 	va_list ap;
232791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
233e2a09b26SPoul-Henning Kamp 
234791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
235e2a09b26SPoul-Henning Kamp 	pca.pri = level;
236e2a09b26SPoul-Henning Kamp 	pca.flags = log_open ? TOLOG : TOCONS;
237e2a09b26SPoul-Henning Kamp 
238e2a09b26SPoul-Henning Kamp 	va_start(ap, fmt);
23974f1af01SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
240df8bae1dSRodney W. Grimes 	va_end(ap);
241e2a09b26SPoul-Henning Kamp 
242b80e3b41SPoul-Henning Kamp 	msgbuftrigger = 1;
243df8bae1dSRodney W. Grimes }
244df8bae1dSRodney W. Grimes 
245e2a09b26SPoul-Henning Kamp #define CONSCHUNK 128
246e2a09b26SPoul-Henning Kamp 
247e2a09b26SPoul-Henning Kamp void
248e2a09b26SPoul-Henning Kamp log_console(struct uio *uio)
249df8bae1dSRodney W. Grimes {
250e2a09b26SPoul-Henning Kamp 	int c, i, error, iovlen, nl;
251e2a09b26SPoul-Henning Kamp 	struct uio muio;
252e2a09b26SPoul-Henning Kamp 	struct iovec *miov = NULL;
253e2a09b26SPoul-Henning Kamp 	char *consbuffer;
254e2a09b26SPoul-Henning Kamp 	int pri;
255df8bae1dSRodney W. Grimes 
256dbe620d3SDavid Malone 	if (!log_console_output)
257dbe620d3SDavid Malone 		return;
258dbe620d3SDavid Malone 
259e2a09b26SPoul-Henning Kamp 	pri = LOG_INFO | LOG_CONSOLE;
260e2a09b26SPoul-Henning Kamp 	muio = *uio;
261e2a09b26SPoul-Henning Kamp 	iovlen = uio->uio_iovcnt * sizeof (struct iovec);
262a163d034SWarner Losh 	MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
263a163d034SWarner Losh 	MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
26480208239SAlfred Perlstein 	bcopy(muio.uio_iov, miov, iovlen);
265e2a09b26SPoul-Henning Kamp 	muio.uio_iov = miov;
266e2a09b26SPoul-Henning Kamp 	uio = &muio;
267e2a09b26SPoul-Henning Kamp 
268e2a09b26SPoul-Henning Kamp 	nl = 0;
269e2a09b26SPoul-Henning Kamp 	while (uio->uio_resid > 0) {
270e2a09b26SPoul-Henning Kamp 		c = imin(uio->uio_resid, CONSCHUNK);
271e2a09b26SPoul-Henning Kamp 		error = uiomove(consbuffer, c, uio);
272e2a09b26SPoul-Henning Kamp 		if (error != 0)
2732f9752e9SPoul-Henning Kamp 			break;
274e2a09b26SPoul-Henning Kamp 		for (i = 0; i < c; i++) {
275e2a09b26SPoul-Henning Kamp 			msglogchar(consbuffer[i], pri);
276e2a09b26SPoul-Henning Kamp 			if (consbuffer[i] == '\n')
277e2a09b26SPoul-Henning Kamp 				nl = 1;
278e2a09b26SPoul-Henning Kamp 			else
279e2a09b26SPoul-Henning Kamp 				nl = 0;
280e2a09b26SPoul-Henning Kamp 		}
281e2a09b26SPoul-Henning Kamp 	}
282e2a09b26SPoul-Henning Kamp 	if (!nl)
283e2a09b26SPoul-Henning Kamp 		msglogchar('\n', pri);
284b80e3b41SPoul-Henning Kamp 	msgbuftrigger = 1;
285e2a09b26SPoul-Henning Kamp 	FREE(miov, M_TEMP);
286e2a09b26SPoul-Henning Kamp 	FREE(consbuffer, M_TEMP);
287e2a09b26SPoul-Henning Kamp 	return;
288df8bae1dSRodney W. Grimes }
289df8bae1dSRodney W. Grimes 
2906ddbf1e2SGary Palmer int
291df8bae1dSRodney W. Grimes printf(const char *fmt, ...)
292df8bae1dSRodney W. Grimes {
293df8bae1dSRodney W. Grimes 	va_list ap;
2944d88c459SPoul-Henning Kamp 	int savintr;
295791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
29665ed8cbdSJustin T. Gibbs 	int retval;
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes 	savintr = consintr;		/* disable interrupts */
299df8bae1dSRodney W. Grimes 	consintr = 0;
300df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
301791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
302791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
303e2a09b26SPoul-Henning Kamp 	pca.pri = -1;
30465ed8cbdSJustin T. Gibbs 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
305df8bae1dSRodney W. Grimes 	va_end(ap);
306df8bae1dSRodney W. Grimes 	if (!panicstr)
307b80e3b41SPoul-Henning Kamp 		msgbuftrigger = 1;
308df8bae1dSRodney W. Grimes 	consintr = savintr;		/* reenable interrupts */
30958a24f79SPoul-Henning Kamp 	return (retval);
310df8bae1dSRodney W. Grimes }
311df8bae1dSRodney W. Grimes 
312f1550d9dSDoug Rabson int
313791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap)
314791d77e0SPoul-Henning Kamp {
3154d88c459SPoul-Henning Kamp 	int savintr;
316791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
317f1550d9dSDoug Rabson 	int retval;
318791d77e0SPoul-Henning Kamp 
319791d77e0SPoul-Henning Kamp 	savintr = consintr;		/* disable interrupts */
320791d77e0SPoul-Henning Kamp 	consintr = 0;
321791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
322791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
323e2a09b26SPoul-Henning Kamp 	pca.pri = -1;
324f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
325791d77e0SPoul-Henning Kamp 	if (!panicstr)
326b80e3b41SPoul-Henning Kamp 		msgbuftrigger = 1;
327791d77e0SPoul-Henning Kamp 	consintr = savintr;		/* reenable interrupts */
32858a24f79SPoul-Henning Kamp 	return (retval);
329791d77e0SPoul-Henning Kamp }
330791d77e0SPoul-Henning Kamp 
331791d77e0SPoul-Henning Kamp /*
332791d77e0SPoul-Henning Kamp  * Print a character on console or users terminal.  If destination is
333e796e00dSPoul-Henning Kamp  * the console then the last bunch of characters are saved in msgbuf for
334791d77e0SPoul-Henning Kamp  * inspection later.
335791d77e0SPoul-Henning Kamp  */
336791d77e0SPoul-Henning Kamp static void
337791d77e0SPoul-Henning Kamp putchar(int c, void *arg)
338791d77e0SPoul-Henning Kamp {
339791d77e0SPoul-Henning Kamp 	struct putchar_arg *ap = (struct putchar_arg*) arg;
340791d77e0SPoul-Henning Kamp 	int flags = ap->flags;
341791d77e0SPoul-Henning Kamp 	struct tty *tp = ap->tty;
342791d77e0SPoul-Henning Kamp 	if (panicstr)
343791d77e0SPoul-Henning Kamp 		constty = NULL;
344791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && tp == NULL && constty) {
345791d77e0SPoul-Henning Kamp 		tp = constty;
346791d77e0SPoul-Henning Kamp 		flags |= TOTTY;
347791d77e0SPoul-Henning Kamp 	}
348791d77e0SPoul-Henning Kamp 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
349791d77e0SPoul-Henning Kamp 	    (flags & TOCONS) && tp == constty)
350791d77e0SPoul-Henning Kamp 		constty = NULL;
351791d77e0SPoul-Henning Kamp 	if ((flags & TOLOG))
352e2a09b26SPoul-Henning Kamp 		msglogchar(c, ap->pri);
353791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && constty == NULL && c != '\0')
354791d77e0SPoul-Henning Kamp 		(*v_putc)(c);
355791d77e0SPoul-Henning Kamp }
356791d77e0SPoul-Henning Kamp 
357791d77e0SPoul-Henning Kamp /*
358791d77e0SPoul-Henning Kamp  * Scaled down version of sprintf(3).
359791d77e0SPoul-Henning Kamp  */
360791d77e0SPoul-Henning Kamp int
361791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...)
362791d77e0SPoul-Henning Kamp {
363791d77e0SPoul-Henning Kamp 	int retval;
364791d77e0SPoul-Henning Kamp 	va_list ap;
365791d77e0SPoul-Henning Kamp 
366791d77e0SPoul-Henning Kamp 	va_start(ap, cfmt);
367791d77e0SPoul-Henning Kamp 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
368fe96d47dSPoul-Henning Kamp 	buf[retval] = '\0';
369791d77e0SPoul-Henning Kamp 	va_end(ap);
37058a24f79SPoul-Henning Kamp 	return (retval);
371791d77e0SPoul-Henning Kamp }
372791d77e0SPoul-Henning Kamp 
373791d77e0SPoul-Henning Kamp /*
37499237364SAndrey A. Chernov  * Scaled down version of vsprintf(3).
37599237364SAndrey A. Chernov  */
37699237364SAndrey A. Chernov int
37799237364SAndrey A. Chernov vsprintf(char *buf, const char *cfmt, va_list ap)
37899237364SAndrey A. Chernov {
37999237364SAndrey A. Chernov 	int retval;
38099237364SAndrey A. Chernov 
38199237364SAndrey A. Chernov 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
38299237364SAndrey A. Chernov 	buf[retval] = '\0';
38358a24f79SPoul-Henning Kamp 	return (retval);
38499237364SAndrey A. Chernov }
38599237364SAndrey A. Chernov 
38699237364SAndrey A. Chernov /*
3878245f3f5SArchie Cobbs  * Scaled down version of snprintf(3).
3888245f3f5SArchie Cobbs  */
3898245f3f5SArchie Cobbs int
3908245f3f5SArchie Cobbs snprintf(char *str, size_t size, const char *format, ...)
3918245f3f5SArchie Cobbs {
3928245f3f5SArchie Cobbs 	int retval;
3938245f3f5SArchie Cobbs 	va_list ap;
3948245f3f5SArchie Cobbs 
3958245f3f5SArchie Cobbs 	va_start(ap, format);
3968245f3f5SArchie Cobbs 	retval = vsnprintf(str, size, format, ap);
3978245f3f5SArchie Cobbs 	va_end(ap);
3988245f3f5SArchie Cobbs 	return(retval);
3998245f3f5SArchie Cobbs }
4008245f3f5SArchie Cobbs 
4018245f3f5SArchie Cobbs /*
4028245f3f5SArchie Cobbs  * Scaled down version of vsnprintf(3).
4038245f3f5SArchie Cobbs  */
4048245f3f5SArchie Cobbs int
4058245f3f5SArchie Cobbs vsnprintf(char *str, size_t size, const char *format, va_list ap)
4068245f3f5SArchie Cobbs {
4078245f3f5SArchie Cobbs 	struct snprintf_arg info;
4088245f3f5SArchie Cobbs 	int retval;
4098245f3f5SArchie Cobbs 
4108245f3f5SArchie Cobbs 	info.str = str;
4118245f3f5SArchie Cobbs 	info.remain = size;
4128245f3f5SArchie Cobbs 	retval = kvprintf(format, snprintf_func, &info, 10, ap);
4138245f3f5SArchie Cobbs 	if (info.remain >= 1)
4148245f3f5SArchie Cobbs 		*info.str++ = '\0';
41558a24f79SPoul-Henning Kamp 	return (retval);
4168245f3f5SArchie Cobbs }
4178245f3f5SArchie Cobbs 
4188751a8c7SPoul-Henning Kamp /*
4198751a8c7SPoul-Henning Kamp  * Kernel version which takes radix argument vsnprintf(3).
4208751a8c7SPoul-Henning Kamp  */
4218751a8c7SPoul-Henning Kamp int
4228751a8c7SPoul-Henning Kamp vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
4238751a8c7SPoul-Henning Kamp {
4248751a8c7SPoul-Henning Kamp 	struct snprintf_arg info;
4258751a8c7SPoul-Henning Kamp 	int retval;
4268751a8c7SPoul-Henning Kamp 
4278751a8c7SPoul-Henning Kamp 	info.str = str;
4288751a8c7SPoul-Henning Kamp 	info.remain = size;
4298751a8c7SPoul-Henning Kamp 	retval = kvprintf(format, snprintf_func, &info, radix, ap);
4308751a8c7SPoul-Henning Kamp 	if (info.remain >= 1)
4318751a8c7SPoul-Henning Kamp 		*info.str++ = '\0';
4328751a8c7SPoul-Henning Kamp 	return (retval);
4338751a8c7SPoul-Henning Kamp }
4348751a8c7SPoul-Henning Kamp 
4358245f3f5SArchie Cobbs static void
4368245f3f5SArchie Cobbs snprintf_func(int ch, void *arg)
4378245f3f5SArchie Cobbs {
4388245f3f5SArchie Cobbs 	struct snprintf_arg *const info = arg;
4398245f3f5SArchie Cobbs 
4408245f3f5SArchie Cobbs 	if (info->remain >= 2) {
4418245f3f5SArchie Cobbs 		*info->str++ = ch;
4428245f3f5SArchie Cobbs 		info->remain--;
4438245f3f5SArchie Cobbs 	}
4448245f3f5SArchie Cobbs }
4458245f3f5SArchie Cobbs 
4468245f3f5SArchie Cobbs /*
44782941964SPeter Wemm  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
44805292ba2SArchie Cobbs  * order; return an optional length and a pointer to the last character
44905292ba2SArchie Cobbs  * written in the buffer (i.e., the first character of the string).
45005292ba2SArchie Cobbs  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
451791d77e0SPoul-Henning Kamp  */
452791d77e0SPoul-Henning Kamp static char *
453f16a5176SDag-Erling Smørgrav ksprintn(char *nbuf, uintmax_t num, int base, int *lenp)
45405292ba2SArchie Cobbs {
4554d88c459SPoul-Henning Kamp 	char *p;
456791d77e0SPoul-Henning Kamp 
45705292ba2SArchie Cobbs 	p = nbuf;
458ad4f8dbdSArchie Cobbs 	*p = '\0';
459791d77e0SPoul-Henning Kamp 	do {
4603b1f7e7dSDag-Erling Smørgrav 		*++p = hex2ascii(num % base);
4613b1f7e7dSDag-Erling Smørgrav 	} while (num /= base);
4627d921a01SPeter Wemm 	if (lenp)
4637d921a01SPeter Wemm 		*lenp = p - nbuf;
4647d921a01SPeter Wemm 	return (p);
4657d921a01SPeter Wemm }
466791d77e0SPoul-Henning Kamp 
467df8bae1dSRodney W. Grimes /*
468df8bae1dSRodney W. Grimes  * Scaled down version of printf(3).
469df8bae1dSRodney W. Grimes  *
470df8bae1dSRodney W. Grimes  * Two additional formats:
471df8bae1dSRodney W. Grimes  *
472df8bae1dSRodney W. Grimes  * The format %b is supported to decode error registers.
473df8bae1dSRodney W. Grimes  * Its usage is:
474df8bae1dSRodney W. Grimes  *
475df8bae1dSRodney W. Grimes  *	printf("reg=%b\n", regval, "<base><arg>*");
476df8bae1dSRodney W. Grimes  *
477df8bae1dSRodney W. Grimes  * where <base> is the output base expressed as a control character, e.g.
478df8bae1dSRodney W. Grimes  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
479df8bae1dSRodney W. Grimes  * the first of which gives the bit number to be inspected (origin 1), and
480df8bae1dSRodney W. Grimes  * the next characters (up to a control character, i.e. a character <= 32),
481df8bae1dSRodney W. Grimes  * give the name of the register.  Thus:
482df8bae1dSRodney W. Grimes  *
4834e31a37bSGary Palmer  *	kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
484df8bae1dSRodney W. Grimes  *
485df8bae1dSRodney W. Grimes  * would produce output:
486df8bae1dSRodney W. Grimes  *
487df8bae1dSRodney W. Grimes  *	reg=3<BITTWO,BITONE>
488df8bae1dSRodney W. Grimes  *
489120f0783SPoul-Henning Kamp  * XXX:  %D  -- Hexdump, takes pointer and separator string:
490120f0783SPoul-Henning Kamp  *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
491120f0783SPoul-Henning Kamp  *		("%*D", len, ptr, " " -> XX XX XX XX ...
492df8bae1dSRodney W. Grimes  */
493791d77e0SPoul-Henning Kamp int
494791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
495df8bae1dSRodney W. Grimes {
496791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
497ad4f8dbdSArchie Cobbs 	char nbuf[MAXNBUF];
4983b1f7e7dSDag-Erling Smørgrav 	char *d;
4993b1f7e7dSDag-Erling Smørgrav 	const char *p, *percent, *q;
500120f0783SPoul-Henning Kamp 	u_char *up;
501791d77e0SPoul-Henning Kamp 	int ch, n;
5023b1f7e7dSDag-Erling Smørgrav 	uintmax_t num;
5037d921a01SPeter Wemm 	int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
5042bb95458SMaxime Henrion 	int jflag, tflag, zflag;
5054f20e4b1SPoul-Henning Kamp 	int dwidth;
506df8bae1dSRodney W. Grimes 	char padc;
507791d77e0SPoul-Henning Kamp 	int retval = 0;
508791d77e0SPoul-Henning Kamp 
5093b1f7e7dSDag-Erling Smørgrav 	num = 0;
510fe96d47dSPoul-Henning Kamp 	if (!func)
511791d77e0SPoul-Henning Kamp 		d = (char *) arg;
512791d77e0SPoul-Henning Kamp 	else
513fe96d47dSPoul-Henning Kamp 		d = NULL;
5148f5067baSDavid Greenman 
5158f5067baSDavid Greenman 	if (fmt == NULL)
5162336b9d7SBruce Evans 		fmt = "(fmt null)\n";
517b4b2f81eSPoul-Henning Kamp 
518120f0783SPoul-Henning Kamp 	if (radix < 2 || radix > 36)
519b4b2f81eSPoul-Henning Kamp 		radix = 10;
520b4b2f81eSPoul-Henning Kamp 
521df8bae1dSRodney W. Grimes 	for (;;) {
522df8bae1dSRodney W. Grimes 		padc = ' ';
523df8bae1dSRodney W. Grimes 		width = 0;
524e0c95ed9SBruce Evans 		while ((ch = (u_char)*fmt++) != '%') {
525df8bae1dSRodney W. Grimes 			if (ch == '\0')
52658a24f79SPoul-Henning Kamp 				return (retval);
527791d77e0SPoul-Henning Kamp 			PCHAR(ch);
528df8bae1dSRodney W. Grimes 		}
5293b1f7e7dSDag-Erling Smørgrav 		percent = fmt - 1;
5307d921a01SPeter Wemm 		qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
5314f20e4b1SPoul-Henning Kamp 		sign = 0; dot = 0; dwidth = 0;
5322bb95458SMaxime Henrion 		jflag = 0; tflag = 0; zflag = 0;
533e0c95ed9SBruce Evans reswitch:	switch (ch = (u_char)*fmt++) {
5344830092aSPoul-Henning Kamp 		case '.':
5354830092aSPoul-Henning Kamp 			dot = 1;
5364830092aSPoul-Henning Kamp 			goto reswitch;
537791d77e0SPoul-Henning Kamp 		case '#':
538791d77e0SPoul-Henning Kamp 			sharpflag = 1;
539791d77e0SPoul-Henning Kamp 			goto reswitch;
540791d77e0SPoul-Henning Kamp 		case '+':
541791d77e0SPoul-Henning Kamp 			sign = 1;
542791d77e0SPoul-Henning Kamp 			goto reswitch;
543791d77e0SPoul-Henning Kamp 		case '-':
544791d77e0SPoul-Henning Kamp 			ladjust = 1;
545791d77e0SPoul-Henning Kamp 			goto reswitch;
546791d77e0SPoul-Henning Kamp 		case '%':
547791d77e0SPoul-Henning Kamp 			PCHAR(ch);
548791d77e0SPoul-Henning Kamp 			break;
549791d77e0SPoul-Henning Kamp 		case '*':
550ed71c342SPoul-Henning Kamp 			if (!dot) {
551791d77e0SPoul-Henning Kamp 				width = va_arg(ap, int);
552791d77e0SPoul-Henning Kamp 				if (width < 0) {
553791d77e0SPoul-Henning Kamp 					ladjust = !ladjust;
554791d77e0SPoul-Henning Kamp 					width = -width;
555791d77e0SPoul-Henning Kamp 				}
556ed71c342SPoul-Henning Kamp 			} else {
557ed71c342SPoul-Henning Kamp 				dwidth = va_arg(ap, int);
558ed71c342SPoul-Henning Kamp 			}
559791d77e0SPoul-Henning Kamp 			goto reswitch;
560df8bae1dSRodney W. Grimes 		case '0':
5614f20e4b1SPoul-Henning Kamp 			if (!dot) {
562df8bae1dSRodney W. Grimes 				padc = '0';
563df8bae1dSRodney W. Grimes 				goto reswitch;
5644f20e4b1SPoul-Henning Kamp 			}
565df8bae1dSRodney W. Grimes 		case '1': case '2': case '3': case '4':
566df8bae1dSRodney W. Grimes 		case '5': case '6': case '7': case '8': case '9':
5674f20e4b1SPoul-Henning Kamp 				for (n = 0;; ++fmt) {
5684f20e4b1SPoul-Henning Kamp 					n = n * 10 + ch - '0';
569df8bae1dSRodney W. Grimes 					ch = *fmt;
570df8bae1dSRodney W. Grimes 					if (ch < '0' || ch > '9')
571df8bae1dSRodney W. Grimes 						break;
572df8bae1dSRodney W. Grimes 				}
5734f20e4b1SPoul-Henning Kamp 			if (dot)
5744f20e4b1SPoul-Henning Kamp 				dwidth = n;
5754f20e4b1SPoul-Henning Kamp 			else
5764f20e4b1SPoul-Henning Kamp 				width = n;
577df8bae1dSRodney W. Grimes 			goto reswitch;
578df8bae1dSRodney W. Grimes 		case 'b':
579377a66bcSJohn Baldwin 			num = (u_int)va_arg(ap, int);
580df8bae1dSRodney W. Grimes 			p = va_arg(ap, char *);
5813b1f7e7dSDag-Erling Smørgrav 			for (q = ksprintn(nbuf, num, *p++, NULL); *q;)
582791d77e0SPoul-Henning Kamp 				PCHAR(*q--);
583df8bae1dSRodney W. Grimes 
5843b1f7e7dSDag-Erling Smørgrav 			if (num == 0)
585df8bae1dSRodney W. Grimes 				break;
586df8bae1dSRodney W. Grimes 
587797f2d22SPoul-Henning Kamp 			for (tmp = 0; *p;) {
588797f2d22SPoul-Henning Kamp 				n = *p++;
5893b1f7e7dSDag-Erling Smørgrav 				if (num & (1 << (n - 1))) {
590791d77e0SPoul-Henning Kamp 					PCHAR(tmp ? ',' : '<');
591df8bae1dSRodney W. Grimes 					for (; (n = *p) > ' '; ++p)
592791d77e0SPoul-Henning Kamp 						PCHAR(n);
593df8bae1dSRodney W. Grimes 					tmp = 1;
594df8bae1dSRodney W. Grimes 				} else
595df8bae1dSRodney W. Grimes 					for (; *p > ' '; ++p)
596df8bae1dSRodney W. Grimes 						continue;
597df8bae1dSRodney W. Grimes 			}
598df8bae1dSRodney W. Grimes 			if (tmp)
599791d77e0SPoul-Henning Kamp 				PCHAR('>');
600df8bae1dSRodney W. Grimes 			break;
601df8bae1dSRodney W. Grimes 		case 'c':
602791d77e0SPoul-Henning Kamp 			PCHAR(va_arg(ap, int));
603df8bae1dSRodney W. Grimes 			break;
604120f0783SPoul-Henning Kamp 		case 'D':
605120f0783SPoul-Henning Kamp 			up = va_arg(ap, u_char *);
606120f0783SPoul-Henning Kamp 			p = va_arg(ap, char *);
607120f0783SPoul-Henning Kamp 			if (!width)
608120f0783SPoul-Henning Kamp 				width = 16;
609120f0783SPoul-Henning Kamp 			while(width--) {
610120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up >> 4));
611120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up & 0x0f));
612120f0783SPoul-Henning Kamp 				up++;
613120f0783SPoul-Henning Kamp 				if (width)
614120f0783SPoul-Henning Kamp 					for (q=p;*q;q++)
615120f0783SPoul-Henning Kamp 						PCHAR(*q);
616120f0783SPoul-Henning Kamp 			}
617120f0783SPoul-Henning Kamp 			break;
618df8bae1dSRodney W. Grimes 		case 'd':
619e0b74464SWarner Losh 		case 'i':
620df8bae1dSRodney W. Grimes 			base = 10;
6213b1f7e7dSDag-Erling Smørgrav 			sign = 1;
6223b1f7e7dSDag-Erling Smørgrav 			goto handle_sign;
6233b1f7e7dSDag-Erling Smørgrav 		case 'j':
6243b1f7e7dSDag-Erling Smørgrav 			jflag = 1;
6253b1f7e7dSDag-Erling Smørgrav 			goto reswitch;
626791d77e0SPoul-Henning Kamp 		case 'l':
627301ca4ffSBrian Feldman 			if (lflag) {
628301ca4ffSBrian Feldman 				lflag = 0;
629301ca4ffSBrian Feldman 				qflag = 1;
630301ca4ffSBrian Feldman 			} else
631791d77e0SPoul-Henning Kamp 				lflag = 1;
632791d77e0SPoul-Henning Kamp 			goto reswitch;
6333b1f7e7dSDag-Erling Smørgrav 		case 'n':
6343b1f7e7dSDag-Erling Smørgrav 			if (jflag)
6353b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, intmax_t *)) = retval;
6363b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
6373b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, quad_t *)) = retval;
6387d921a01SPeter Wemm 			else if (lflag)
6393b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, long *)) = retval;
6404578a2e6SMaxime Henrion 			else if (zflag)
6414578a2e6SMaxime Henrion 				*(va_arg(ap, size_t *)) = retval;
6427d921a01SPeter Wemm 			else
6433b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, int *)) = retval;
6443b1f7e7dSDag-Erling Smørgrav 			break;
6453b1f7e7dSDag-Erling Smørgrav 		case 'o':
646df8bae1dSRodney W. Grimes 			base = 8;
6473b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
64812d17f65SPoul-Henning Kamp 		case 'p':
64912d17f65SPoul-Henning Kamp 			base = 16;
650c41141b0SBruce Evans 			sharpflag = (width == 0);
6513b1f7e7dSDag-Erling Smørgrav 			sign = 0;
6523b1f7e7dSDag-Erling Smørgrav 			num = (uintptr_t)va_arg(ap, void *);
6533b1f7e7dSDag-Erling Smørgrav 			goto number;
6547d921a01SPeter Wemm 		case 'q':
6557d921a01SPeter Wemm 			qflag = 1;
6567d921a01SPeter Wemm 			goto reswitch;
657e0c38587SBruce Evans 		case 'r':
658e0c38587SBruce Evans 			base = radix;
6593b1f7e7dSDag-Erling Smørgrav 			if (sign)
6603b1f7e7dSDag-Erling Smørgrav 				goto handle_sign;
6613b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
662791d77e0SPoul-Henning Kamp 		case 's':
663791d77e0SPoul-Henning Kamp 			p = va_arg(ap, char *);
664791d77e0SPoul-Henning Kamp 			if (p == NULL)
665791d77e0SPoul-Henning Kamp 				p = "(null)";
6664830092aSPoul-Henning Kamp 			if (!dot)
6674830092aSPoul-Henning Kamp 				n = strlen (p);
6684830092aSPoul-Henning Kamp 			else
6694f20e4b1SPoul-Henning Kamp 				for (n = 0; n < dwidth && p[n]; n++)
6704830092aSPoul-Henning Kamp 					continue;
6714f20e4b1SPoul-Henning Kamp 
6724830092aSPoul-Henning Kamp 			width -= n;
6734f20e4b1SPoul-Henning Kamp 
674791d77e0SPoul-Henning Kamp 			if (!ladjust && width > 0)
675791d77e0SPoul-Henning Kamp 				while (width--)
676791d77e0SPoul-Henning Kamp 					PCHAR(padc);
6774830092aSPoul-Henning Kamp 			while (n--)
678791d77e0SPoul-Henning Kamp 				PCHAR(*p++);
679791d77e0SPoul-Henning Kamp 			if (ladjust && width > 0)
680791d77e0SPoul-Henning Kamp 				while (width--)
681791d77e0SPoul-Henning Kamp 					PCHAR(padc);
682791d77e0SPoul-Henning Kamp 			break;
6832bb95458SMaxime Henrion 		case 't':
6842bb95458SMaxime Henrion 			tflag = 1;
6852bb95458SMaxime Henrion 			goto reswitch;
686f53dbe97SBruce Evans 		case 'u':
687f53dbe97SBruce Evans 			base = 10;
6883b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
689df8bae1dSRodney W. Grimes 		case 'x':
690aa998012SMike Smith 		case 'X':
691df8bae1dSRodney W. Grimes 			base = 16;
6923b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
6934578a2e6SMaxime Henrion 		case 'y':
694e0c38587SBruce Evans 			base = 16;
69585594430SJohn Baldwin 			sign = 1;
6963b1f7e7dSDag-Erling Smørgrav 			goto handle_sign;
6974578a2e6SMaxime Henrion 		case 'z':
6984578a2e6SMaxime Henrion 			zflag = 1;
6994578a2e6SMaxime Henrion 			goto reswitch;
7003b1f7e7dSDag-Erling Smørgrav handle_nosign:
7013b1f7e7dSDag-Erling Smørgrav 			sign = 0;
7023b1f7e7dSDag-Erling Smørgrav 			if (jflag)
7033b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, uintmax_t);
7043b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
7053b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_quad_t);
7062bb95458SMaxime Henrion 			else if (tflag)
7072bb95458SMaxime Henrion 				num = va_arg(ap, ptrdiff_t);
7083b1f7e7dSDag-Erling Smørgrav 			else if (lflag)
7093b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_long);
7104578a2e6SMaxime Henrion 			else if (zflag)
7114578a2e6SMaxime Henrion 				num = va_arg(ap, size_t);
7123b1f7e7dSDag-Erling Smørgrav 			else
7133b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_int);
714e0c38587SBruce Evans 			goto number;
7153b1f7e7dSDag-Erling Smørgrav handle_sign:
7163b1f7e7dSDag-Erling Smørgrav 			if (jflag)
7173b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, intmax_t);
7183b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
7193b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, quad_t);
7202bb95458SMaxime Henrion 			else if (tflag)
7212bb95458SMaxime Henrion 				num = va_arg(ap, ptrdiff_t);
7223b1f7e7dSDag-Erling Smørgrav 			else if (lflag)
7233b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, long);
7244578a2e6SMaxime Henrion 			else if (zflag)
7254578a2e6SMaxime Henrion 				num = va_arg(ap, size_t);
7263b1f7e7dSDag-Erling Smørgrav 			else
7273b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, int);
7287d921a01SPeter Wemm number:
7293b1f7e7dSDag-Erling Smørgrav 			if (sign && (intmax_t)num < 0) {
7307d921a01SPeter Wemm 				neg = 1;
7313b1f7e7dSDag-Erling Smørgrav 				num = -(intmax_t)num;
7327d921a01SPeter Wemm 			}
7333b1f7e7dSDag-Erling Smørgrav 			p = ksprintn(nbuf, num, base, &tmp);
7343b1f7e7dSDag-Erling Smørgrav 			if (sharpflag && num != 0) {
735791d77e0SPoul-Henning Kamp 				if (base == 8)
736791d77e0SPoul-Henning Kamp 					tmp++;
737791d77e0SPoul-Henning Kamp 				else if (base == 16)
738791d77e0SPoul-Henning Kamp 					tmp += 2;
739791d77e0SPoul-Henning Kamp 			}
740791d77e0SPoul-Henning Kamp 			if (neg)
741791d77e0SPoul-Henning Kamp 				tmp++;
742791d77e0SPoul-Henning Kamp 
743791d77e0SPoul-Henning Kamp 			if (!ladjust && width && (width -= tmp) > 0)
744df8bae1dSRodney W. Grimes 				while (width--)
745791d77e0SPoul-Henning Kamp 					PCHAR(padc);
746791d77e0SPoul-Henning Kamp 			if (neg)
747791d77e0SPoul-Henning Kamp 				PCHAR('-');
7483b1f7e7dSDag-Erling Smørgrav 			if (sharpflag && num != 0) {
749791d77e0SPoul-Henning Kamp 				if (base == 8) {
750791d77e0SPoul-Henning Kamp 					PCHAR('0');
751791d77e0SPoul-Henning Kamp 				} else if (base == 16) {
752791d77e0SPoul-Henning Kamp 					PCHAR('0');
753791d77e0SPoul-Henning Kamp 					PCHAR('x');
754791d77e0SPoul-Henning Kamp 				}
755791d77e0SPoul-Henning Kamp 			}
756791d77e0SPoul-Henning Kamp 
757797f2d22SPoul-Henning Kamp 			while (*p)
758791d77e0SPoul-Henning Kamp 				PCHAR(*p--);
759791d77e0SPoul-Henning Kamp 
760791d77e0SPoul-Henning Kamp 			if (ladjust && width && (width -= tmp) > 0)
761791d77e0SPoul-Henning Kamp 				while (width--)
762791d77e0SPoul-Henning Kamp 					PCHAR(padc);
763791d77e0SPoul-Henning Kamp 
764df8bae1dSRodney W. Grimes 			break;
765df8bae1dSRodney W. Grimes 		default:
7663b1f7e7dSDag-Erling Smørgrav 			while (percent < fmt)
7673b1f7e7dSDag-Erling Smørgrav 				PCHAR(*percent++);
768791d77e0SPoul-Henning Kamp 			break;
769df8bae1dSRodney W. Grimes 		}
770df8bae1dSRodney W. Grimes 	}
771791d77e0SPoul-Henning Kamp #undef PCHAR
772df8bae1dSRodney W. Grimes }
773df8bae1dSRodney W. Grimes 
774df8bae1dSRodney W. Grimes /*
775e2a09b26SPoul-Henning Kamp  * Put character in log buffer with a particular priority.
776df8bae1dSRodney W. Grimes  */
777df8bae1dSRodney W. Grimes static void
778e2a09b26SPoul-Henning Kamp msglogchar(int c, int pri)
779e2a09b26SPoul-Henning Kamp {
780e2a09b26SPoul-Henning Kamp 	static int lastpri = -1;
781e2a09b26SPoul-Henning Kamp 	static int dangling;
782e2a09b26SPoul-Henning Kamp 	char nbuf[MAXNBUF];
783e2a09b26SPoul-Henning Kamp 	char *p;
784e2a09b26SPoul-Henning Kamp 
785e2a09b26SPoul-Henning Kamp 	if (!msgbufmapped)
786e2a09b26SPoul-Henning Kamp 		return;
787e2a09b26SPoul-Henning Kamp 	if (c == '\0' || c == '\r')
788e2a09b26SPoul-Henning Kamp 		return;
789e2a09b26SPoul-Henning Kamp 	if (pri != -1 && pri != lastpri) {
790e2a09b26SPoul-Henning Kamp 		if (dangling) {
791e2a09b26SPoul-Henning Kamp 			msgaddchar('\n', NULL);
792e2a09b26SPoul-Henning Kamp 			dangling = 0;
793e2a09b26SPoul-Henning Kamp 		}
794e2a09b26SPoul-Henning Kamp 		msgaddchar('<', NULL);
7953b1f7e7dSDag-Erling Smørgrav 		for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL); *p;)
796e2a09b26SPoul-Henning Kamp 			msgaddchar(*p--, NULL);
797e2a09b26SPoul-Henning Kamp 		msgaddchar('>', NULL);
798e2a09b26SPoul-Henning Kamp 		lastpri = pri;
799e2a09b26SPoul-Henning Kamp 	}
800e2a09b26SPoul-Henning Kamp 	msgaddchar(c, NULL);
801e2a09b26SPoul-Henning Kamp 	if (c == '\n') {
802e2a09b26SPoul-Henning Kamp 		dangling = 0;
803e2a09b26SPoul-Henning Kamp 		lastpri = -1;
804e2a09b26SPoul-Henning Kamp 	} else {
805e2a09b26SPoul-Henning Kamp 		dangling = 1;
806e2a09b26SPoul-Henning Kamp 	}
807e2a09b26SPoul-Henning Kamp }
808e2a09b26SPoul-Henning Kamp 
809e2a09b26SPoul-Henning Kamp /*
810e2a09b26SPoul-Henning Kamp  * Put char in log buffer
811e2a09b26SPoul-Henning Kamp  */
812e2a09b26SPoul-Henning Kamp static void
813e2a09b26SPoul-Henning Kamp msgaddchar(int c, void *dummy)
814df8bae1dSRodney W. Grimes {
815791d77e0SPoul-Henning Kamp 	struct msgbuf *mbp;
816df8bae1dSRodney W. Grimes 
817e2a09b26SPoul-Henning Kamp 	if (!msgbufmapped)
818e2a09b26SPoul-Henning Kamp 		return;
819df8bae1dSRodney W. Grimes 	mbp = msgbufp;
8206205bf31SIan Dowse 	mbp->msg_cksum += (u_char)c - (u_char)mbp->msg_ptr[mbp->msg_bufx];
82158067a99SPoul-Henning Kamp 	mbp->msg_ptr[mbp->msg_bufx++] = c;
82258067a99SPoul-Henning Kamp 	if (mbp->msg_bufx >= mbp->msg_size)
823df8bae1dSRodney W. Grimes 		mbp->msg_bufx = 0;
82402d5c7b1SDavid Greenman 	/* If the buffer is full, keep the most recent data. */
825a3f4faceSBruce Evans 	if (mbp->msg_bufr == mbp->msg_bufx) {
82658067a99SPoul-Henning Kamp 		if (++mbp->msg_bufr >= mbp->msg_size)
827a3f4faceSBruce Evans 			mbp->msg_bufr = 0;
828a3f4faceSBruce Evans 	}
829df8bae1dSRodney W. Grimes }
830e796e00dSPoul-Henning Kamp 
831eb9d435aSJonathan Lemon static void
832eb9d435aSJonathan Lemon msgbufcopy(struct msgbuf *oldp)
833eb9d435aSJonathan Lemon {
834eb9d435aSJonathan Lemon 	int pos;
835eb9d435aSJonathan Lemon 
836eb9d435aSJonathan Lemon 	pos = oldp->msg_bufr;
837eb9d435aSJonathan Lemon 	while (pos != oldp->msg_bufx) {
838e2a09b26SPoul-Henning Kamp 		msglogchar(oldp->msg_ptr[pos], -1);
839eb9d435aSJonathan Lemon 		if (++pos >= oldp->msg_size)
840eb9d435aSJonathan Lemon 			pos = 0;
841eb9d435aSJonathan Lemon 	}
842eb9d435aSJonathan Lemon }
843eb9d435aSJonathan Lemon 
844e796e00dSPoul-Henning Kamp void
84501ee4395SThomas Moestl msgbufinit(void *ptr, int size)
846e796e00dSPoul-Henning Kamp {
847e796e00dSPoul-Henning Kamp 	char *cp;
848eb9d435aSJonathan Lemon 	static struct msgbuf *oldp = NULL;
849e796e00dSPoul-Henning Kamp 
850cca8f980SIan Dowse 	size -= sizeof(*msgbufp);
851e796e00dSPoul-Henning Kamp 	cp = (char *)ptr;
852cca8f980SIan Dowse 	msgbufp = (struct msgbuf *) (cp + size);
853cca8f980SIan Dowse 	if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
85401ee4395SThomas Moestl 	    msgbufp->msg_bufx >= size || msgbufp->msg_bufx < 0 ||
8556205bf31SIan Dowse 	    msgbufp->msg_bufr >= size || msgbufp->msg_bufr < 0 ||
8566205bf31SIan Dowse 	    msgbufcksum(cp, size, msgbufp->msg_cksum) != msgbufp->msg_cksum) {
857e796e00dSPoul-Henning Kamp 		bzero(cp, size);
858cca8f980SIan Dowse 		bzero(msgbufp, sizeof(*msgbufp));
859e796e00dSPoul-Henning Kamp 		msgbufp->msg_magic = MSG_MAGIC;
8606205bf31SIan Dowse 		msgbufp->msg_size = size;
861e796e00dSPoul-Henning Kamp 	}
862cca8f980SIan Dowse 	msgbufp->msg_ptr = cp;
863eb9d435aSJonathan Lemon 	if (msgbufmapped && oldp != msgbufp)
864eb9d435aSJonathan Lemon 		msgbufcopy(oldp);
865e796e00dSPoul-Henning Kamp 	msgbufmapped = 1;
866eb9d435aSJonathan Lemon 	oldp = msgbufp;
867e796e00dSPoul-Henning Kamp }
868e796e00dSPoul-Henning Kamp 
8696205bf31SIan Dowse static u_int
8706205bf31SIan Dowse msgbufcksum(char *cp, size_t size, u_int cksum)
8716205bf31SIan Dowse {
8726205bf31SIan Dowse 	u_int sum;
8736205bf31SIan Dowse 	int i;
8746205bf31SIan Dowse 
8756205bf31SIan Dowse 	sum = 0;
8766205bf31SIan Dowse 	for (i = 0; i < size; i++)
8776205bf31SIan Dowse 		sum += (u_char)cp[i];
8786205bf31SIan Dowse 	if (sum != cksum)
8796205bf31SIan Dowse 		printf("msgbuf cksum mismatch (read %x, calc %x)\n", cksum,
8806205bf31SIan Dowse 		    sum);
8816205bf31SIan Dowse 
8826205bf31SIan Dowse 	return (sum);
8836205bf31SIan Dowse }
8846205bf31SIan Dowse 
885d0615c64SAndrew R. Reiter SYSCTL_DECL(_security_bsd);
8866f3933faSRobert Watson 
8876f3933faSRobert Watson static int unprivileged_read_msgbuf = 1;
888d0615c64SAndrew R. Reiter SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf,
8896f3933faSRobert Watson     CTLFLAG_RW, &unprivileged_read_msgbuf, 0,
8906f3933faSRobert Watson     "Unprivileged processes may read the kernel message buffer");
8916f3933faSRobert Watson 
892948d3d94SThomas Moestl /* Sysctls for accessing/clearing the msgbuf */
893948d3d94SThomas Moestl static int
894948d3d94SThomas Moestl sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
895948d3d94SThomas Moestl {
896948d3d94SThomas Moestl 	int error;
897948d3d94SThomas Moestl 
8986f3933faSRobert Watson 	if (!unprivileged_read_msgbuf) {
89944731cabSJohn Baldwin 		error = suser(req->td);
9006f3933faSRobert Watson 		if (error)
9016f3933faSRobert Watson 			return (error);
9026f3933faSRobert Watson 	}
9036f3933faSRobert Watson 
904948d3d94SThomas Moestl 	/*
905948d3d94SThomas Moestl 	 * Unwind the buffer, so that it's linear (possibly starting with
906948d3d94SThomas Moestl 	 * some initial nulls).
907948d3d94SThomas Moestl 	 */
908948d3d94SThomas Moestl 	error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
909948d3d94SThomas Moestl 	    msgbufp->msg_size - msgbufp->msg_bufx, req);
910948d3d94SThomas Moestl 	if (error)
911948d3d94SThomas Moestl 		return (error);
912948d3d94SThomas Moestl 	if (msgbufp->msg_bufx > 0) {
913948d3d94SThomas Moestl 		error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
914948d3d94SThomas Moestl 		    msgbufp->msg_bufx, req);
915948d3d94SThomas Moestl 	}
916948d3d94SThomas Moestl 	return (error);
917948d3d94SThomas Moestl }
918948d3d94SThomas Moestl 
919948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
920948d3d94SThomas Moestl     0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
921948d3d94SThomas Moestl 
922948d3d94SThomas Moestl static int msgbuf_clear;
923948d3d94SThomas Moestl 
924948d3d94SThomas Moestl static int
925948d3d94SThomas Moestl sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
926948d3d94SThomas Moestl {
927948d3d94SThomas Moestl 	int error;
928948d3d94SThomas Moestl 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
929948d3d94SThomas Moestl 	if (!error && req->newptr) {
930948d3d94SThomas Moestl 		/* Clear the buffer and reset write pointer */
931948d3d94SThomas Moestl 		bzero(msgbufp->msg_ptr, msgbufp->msg_size);
932948d3d94SThomas Moestl 		msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
9336205bf31SIan Dowse 		msgbufp->msg_cksum = 0;
934948d3d94SThomas Moestl 		msgbuf_clear = 0;
935948d3d94SThomas Moestl 	}
936948d3d94SThomas Moestl 	return (error);
937948d3d94SThomas Moestl }
938948d3d94SThomas Moestl 
939948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
940948d3d94SThomas Moestl     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
941948d3d94SThomas Moestl     sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
942948d3d94SThomas Moestl 
943e796e00dSPoul-Henning Kamp #ifdef DDB
944e796e00dSPoul-Henning Kamp 
945e796e00dSPoul-Henning Kamp DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
946e796e00dSPoul-Henning Kamp {
947e796e00dSPoul-Henning Kamp 	int i, j;
948e796e00dSPoul-Henning Kamp 
949e796e00dSPoul-Henning Kamp 	if (!msgbufmapped) {
950e796e00dSPoul-Henning Kamp 		db_printf("msgbuf not mapped yet\n");
951e796e00dSPoul-Henning Kamp 		return;
952e796e00dSPoul-Henning Kamp 	}
953e796e00dSPoul-Henning Kamp 	db_printf("msgbufp = %p\n", msgbufp);
9546205bf31SIan Dowse 	db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p, cksum= %d\n",
955e796e00dSPoul-Henning Kamp 	    msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
9566205bf31SIan Dowse 	    msgbufp->msg_bufx, msgbufp->msg_ptr, msgbufp->msg_cksum);
957e796e00dSPoul-Henning Kamp 	for (i = 0; i < msgbufp->msg_size; i++) {
958e796e00dSPoul-Henning Kamp 		j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
959e796e00dSPoul-Henning Kamp 		db_printf("%c", msgbufp->msg_ptr[j]);
960e796e00dSPoul-Henning Kamp 	}
961e796e00dSPoul-Henning Kamp 	db_printf("\n");
962e796e00dSPoul-Henning Kamp }
963e796e00dSPoul-Henning Kamp 
964e796e00dSPoul-Henning Kamp #endif /* DDB */
965