xref: /freebsd/sys/kern/subr_prf.c (revision af338bea6461ad28a5ff96a9558bc051fc7c2ade)
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
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44f591779bSSeigo Tanimura #include <sys/lock.h>
45f591779bSSeigo Tanimura #include <sys/mutex.h>
46f591779bSSeigo Tanimura #include <sys/sx.h>
47e796e00dSPoul-Henning Kamp #include <sys/kernel.h>
48df8bae1dSRodney W. Grimes #include <sys/msgbuf.h>
49a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/proc.h>
513b1f7e7dSDag-Erling Smørgrav #include <sys/stdint.h>
5258a24f79SPoul-Henning Kamp #include <sys/sysctl.h>
53df8bae1dSRodney W. Grimes #include <sys/tty.h>
54df8bae1dSRodney W. Grimes #include <sys/syslog.h>
55ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
56e2a09b26SPoul-Henning Kamp #include <sys/uio.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes /*
59df8bae1dSRodney W. Grimes  * Note that stdarg.h and the ANSI style va_start macro is used for both
60df8bae1dSRodney W. Grimes  * ANSI and traditional C compilers.
61df8bae1dSRodney W. Grimes  */
62df8bae1dSRodney W. Grimes #include <machine/stdarg.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #define TOCONS	0x01
65df8bae1dSRodney W. Grimes #define TOTTY	0x02
66df8bae1dSRodney W. Grimes #define TOLOG	0x04
67df8bae1dSRodney W. Grimes 
6882941964SPeter Wemm /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
693b1f7e7dSDag-Erling Smørgrav #define MAXNBUF	(sizeof(intmax_t) * NBBY + 1)
70df8bae1dSRodney W. Grimes 
718245f3f5SArchie Cobbs struct putchar_arg {
728245f3f5SArchie Cobbs 	int	flags;
73e2a09b26SPoul-Henning Kamp 	int	pri;
748245f3f5SArchie Cobbs 	struct	tty *tty;
758245f3f5SArchie Cobbs };
768245f3f5SArchie Cobbs 
778245f3f5SArchie Cobbs struct snprintf_arg {
788245f3f5SArchie Cobbs 	char	*str;
798245f3f5SArchie Cobbs 	size_t	remain;
808245f3f5SArchie Cobbs };
818245f3f5SArchie Cobbs 
82e2a09b26SPoul-Henning Kamp extern	int log_open;
83e2a09b26SPoul-Henning Kamp 
8405292ba2SArchie Cobbs struct	tty *constty;			/* pointer to console "window" tty */
8505292ba2SArchie Cobbs 
8687b6de2bSPoul-Henning Kamp static void (*v_putc)(int) = cnputc;	/* routine to putc on virtual console */
87e2a09b26SPoul-Henning Kamp static void  msglogchar(int c, int pri);
88e2a09b26SPoul-Henning Kamp static void  msgaddchar(int c, void *dummy);
894d77a549SAlfred Perlstein static void  putchar(int ch, void *arg);
903b1f7e7dSDag-Erling Smørgrav static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len);
914d77a549SAlfred Perlstein static void  snprintf_func(int ch, void *arg);
92df8bae1dSRodney W. Grimes 
9387b6de2bSPoul-Henning Kamp static int consintr = 1;		/* Ok to handle console interrupts? */
94e796e00dSPoul-Henning Kamp static int msgbufmapped;		/* Set when safe to use msgbuf */
95e2a09b26SPoul-Henning Kamp int msgbuftrigger;
96df8bae1dSRodney W. Grimes 
97dbe620d3SDavid Malone static int      log_console_output = 1;
98af338beaSDavid Malone TUNABLE_INT("kern.log_console_output", &log_console_output);
99dbe620d3SDavid Malone SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
100dbe620d3SDavid Malone     &log_console_output, 0, "");
101dbe620d3SDavid Malone 
102df8bae1dSRodney W. Grimes /*
103df8bae1dSRodney W. Grimes  * Warn that a system table is full.
104df8bae1dSRodney W. Grimes  */
105df8bae1dSRodney W. Grimes void
106e2a09b26SPoul-Henning Kamp tablefull(const char *tab)
107df8bae1dSRodney W. Grimes {
108df8bae1dSRodney W. Grimes 
109df8bae1dSRodney W. Grimes 	log(LOG_ERR, "%s: table is full\n", tab);
110df8bae1dSRodney W. Grimes }
111df8bae1dSRodney W. Grimes 
112df8bae1dSRodney W. Grimes /*
113df8bae1dSRodney W. Grimes  * Uprintf prints to the controlling terminal for the current process.
114df8bae1dSRodney W. Grimes  * It may block if the tty queue is overfull.  No message is printed if
115df8bae1dSRodney W. Grimes  * the queue does not clear in a reasonable time.
116df8bae1dSRodney W. Grimes  */
117f1550d9dSDoug Rabson int
118df8bae1dSRodney W. Grimes uprintf(const char *fmt, ...)
119df8bae1dSRodney W. Grimes {
120b40ce416SJulian Elischer 	struct thread *td = curthread;
121b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
122df8bae1dSRodney W. Grimes 	va_list ap;
123791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
124f591779bSSeigo Tanimura 	int retval;
125df8bae1dSRodney W. Grimes 
126f591779bSSeigo Tanimura 	if (td == NULL || td == PCPU_GET(idlethread))
127f591779bSSeigo Tanimura 		return (0);
128f591779bSSeigo Tanimura 
129f591779bSSeigo Tanimura 	p = td->td_proc;
130f591779bSSeigo Tanimura 	PROC_LOCK(p);
131f591779bSSeigo Tanimura 	if ((p->p_flag & P_CONTROLT) == 0) {
132f591779bSSeigo Tanimura 		PROC_UNLOCK(p);
133f591779bSSeigo Tanimura 		return (0);
134f591779bSSeigo Tanimura 	}
135f591779bSSeigo Tanimura 	SESS_LOCK(p->p_session);
136791d77e0SPoul-Henning Kamp 	pca.tty = p->p_session->s_ttyp;
137f591779bSSeigo Tanimura 	SESS_UNLOCK(p->p_session);
138f591779bSSeigo Tanimura 	PROC_UNLOCK(p);
139f591779bSSeigo Tanimura 	if (pca.tty == NULL)
140f591779bSSeigo Tanimura 		return (0);
141791d77e0SPoul-Henning Kamp 	pca.flags = TOTTY;
142f591779bSSeigo Tanimura 	va_start(ap, fmt);
143f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
144df8bae1dSRodney W. Grimes 	va_end(ap);
145f591779bSSeigo Tanimura 
14658a24f79SPoul-Henning Kamp 	return (retval);
147df8bae1dSRodney W. Grimes }
148df8bae1dSRodney W. Grimes 
149df8bae1dSRodney W. Grimes /*
150df8bae1dSRodney W. Grimes  * tprintf prints on the controlling terminal associated
151a52585d7SPoul-Henning Kamp  * with the given session, possibly to the log as well.
152df8bae1dSRodney W. Grimes  */
153a52585d7SPoul-Henning Kamp void
154a52585d7SPoul-Henning Kamp tprintf(struct proc *p, int pri, const char *fmt, ...)
155df8bae1dSRodney W. Grimes {
156df8bae1dSRodney W. Grimes 	struct tty *tp = NULL;
157a52585d7SPoul-Henning Kamp 	int flags = 0, shld = 0;
158df8bae1dSRodney W. Grimes 	va_list ap;
159791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
160f1550d9dSDoug Rabson 	int retval;
161df8bae1dSRodney W. Grimes 
162e2a09b26SPoul-Henning Kamp 	if (pri != -1)
163a52585d7SPoul-Henning Kamp 		flags |= TOLOG;
164f591779bSSeigo Tanimura 	if (p != NULL) {
165f591779bSSeigo Tanimura 		PROC_LOCK(p);
166f591779bSSeigo Tanimura 		if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
167f591779bSSeigo Tanimura 			SESS_LOCK(p->p_session);
168a52585d7SPoul-Henning Kamp 			SESSHOLD(p->p_session);
169a52585d7SPoul-Henning Kamp 			tp = p->p_session->s_ttyp;
170f591779bSSeigo Tanimura 			SESS_UNLOCK(p->p_session);
171f591779bSSeigo Tanimura 			PROC_UNLOCK(p);
172f591779bSSeigo Tanimura 			shld++;
173f591779bSSeigo Tanimura 			if (ttycheckoutq(tp, 0))
174f591779bSSeigo Tanimura 				flags |= TOTTY;
175f591779bSSeigo Tanimura 			else
176f591779bSSeigo Tanimura 				tp = NULL;
177f591779bSSeigo Tanimura 		} else
178f591779bSSeigo Tanimura 			PROC_UNLOCK(p);
179a52585d7SPoul-Henning Kamp 	}
180e2a09b26SPoul-Henning Kamp 	pca.pri = pri;
181791d77e0SPoul-Henning Kamp 	pca.tty = tp;
182791d77e0SPoul-Henning Kamp 	pca.flags = flags;
183a52585d7SPoul-Henning Kamp 	va_start(ap, fmt);
184f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
185df8bae1dSRodney W. Grimes 	va_end(ap);
186f591779bSSeigo Tanimura 	if (shld) {
187183ccde6SSeigo Tanimura 		PROC_LOCK(p);
188f591779bSSeigo Tanimura 		SESS_LOCK(p->p_session);
189a52585d7SPoul-Henning Kamp 		SESSRELE(p->p_session);
190f591779bSSeigo Tanimura 		SESS_UNLOCK(p->p_session);
191183ccde6SSeigo Tanimura 		PROC_UNLOCK(p);
192f591779bSSeigo Tanimura 	}
193e2a09b26SPoul-Henning Kamp 	msgbuftrigger = 1;
194df8bae1dSRodney W. Grimes }
195df8bae1dSRodney W. Grimes 
196df8bae1dSRodney W. Grimes /*
197df8bae1dSRodney W. Grimes  * Ttyprintf displays a message on a tty; it should be used only by
198df8bae1dSRodney W. Grimes  * the tty driver, or anything that knows the underlying tty will not
199df8bae1dSRodney W. Grimes  * be revoke(2)'d away.  Other callers should use tprintf.
200df8bae1dSRodney W. Grimes  */
201f1550d9dSDoug Rabson int
202df8bae1dSRodney W. Grimes ttyprintf(struct tty *tp, const char *fmt, ...)
203df8bae1dSRodney W. Grimes {
204df8bae1dSRodney W. Grimes 	va_list ap;
205791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
206f1550d9dSDoug Rabson 	int retval;
207f1550d9dSDoug Rabson 
208df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
209791d77e0SPoul-Henning Kamp 	pca.tty = tp;
210791d77e0SPoul-Henning Kamp 	pca.flags = TOTTY;
211f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
212df8bae1dSRodney W. Grimes 	va_end(ap);
21358a24f79SPoul-Henning Kamp 	return (retval);
214df8bae1dSRodney W. Grimes }
215df8bae1dSRodney W. Grimes 
216df8bae1dSRodney W. Grimes /*
217df8bae1dSRodney W. Grimes  * Log writes to the log buffer, and guarantees not to sleep (so can be
218df8bae1dSRodney W. Grimes  * called by interrupt routines).  If there is no process reading the
219df8bae1dSRodney W. Grimes  * log yet, it writes to the console also.
220df8bae1dSRodney W. Grimes  */
2214d88c459SPoul-Henning Kamp void
222df8bae1dSRodney W. Grimes log(int level, const char *fmt, ...)
223df8bae1dSRodney W. Grimes {
224df8bae1dSRodney W. Grimes 	va_list ap;
225f1550d9dSDoug Rabson 	int retval;
226791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
227e2a09b26SPoul-Henning Kamp 
228791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
229e2a09b26SPoul-Henning Kamp 	pca.pri = level;
230e2a09b26SPoul-Henning Kamp 	pca.flags = log_open ? TOLOG : TOCONS;
231e2a09b26SPoul-Henning Kamp 
232e2a09b26SPoul-Henning Kamp 	va_start(ap, fmt);
233e2a09b26SPoul-Henning Kamp 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
234df8bae1dSRodney W. Grimes 	va_end(ap);
235e2a09b26SPoul-Henning Kamp 
236b80e3b41SPoul-Henning Kamp 	msgbuftrigger = 1;
237df8bae1dSRodney W. Grimes }
238df8bae1dSRodney W. Grimes 
239e2a09b26SPoul-Henning Kamp #define CONSCHUNK 128
240e2a09b26SPoul-Henning Kamp 
241e2a09b26SPoul-Henning Kamp void
242e2a09b26SPoul-Henning Kamp log_console(struct uio *uio)
243df8bae1dSRodney W. Grimes {
244e2a09b26SPoul-Henning Kamp 	int c, i, error, iovlen, nl;
245e2a09b26SPoul-Henning Kamp 	struct uio muio;
246e2a09b26SPoul-Henning Kamp 	struct iovec *miov = NULL;
247e2a09b26SPoul-Henning Kamp 	char *consbuffer;
248e2a09b26SPoul-Henning Kamp 	int pri;
249df8bae1dSRodney W. Grimes 
250dbe620d3SDavid Malone 	if (!log_console_output)
251dbe620d3SDavid Malone 		return;
252dbe620d3SDavid Malone 
253e2a09b26SPoul-Henning Kamp 	pri = LOG_INFO | LOG_CONSOLE;
254e2a09b26SPoul-Henning Kamp 	muio = *uio;
255e2a09b26SPoul-Henning Kamp 	iovlen = uio->uio_iovcnt * sizeof (struct iovec);
256e2a09b26SPoul-Henning Kamp 	MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
257e2a09b26SPoul-Henning Kamp 	MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
25880208239SAlfred Perlstein 	bcopy(muio.uio_iov, miov, iovlen);
259e2a09b26SPoul-Henning Kamp 	muio.uio_iov = miov;
260e2a09b26SPoul-Henning Kamp 	uio = &muio;
261e2a09b26SPoul-Henning Kamp 
262e2a09b26SPoul-Henning Kamp 	nl = 0;
263e2a09b26SPoul-Henning Kamp 	while (uio->uio_resid > 0) {
264e2a09b26SPoul-Henning Kamp 		c = imin(uio->uio_resid, CONSCHUNK);
265e2a09b26SPoul-Henning Kamp 		error = uiomove(consbuffer, c, uio);
266e2a09b26SPoul-Henning Kamp 		if (error != 0)
267e2a09b26SPoul-Henning Kamp 			return;
268e2a09b26SPoul-Henning Kamp 		for (i = 0; i < c; i++) {
269e2a09b26SPoul-Henning Kamp 			msglogchar(consbuffer[i], pri);
270e2a09b26SPoul-Henning Kamp 			if (consbuffer[i] == '\n')
271e2a09b26SPoul-Henning Kamp 				nl = 1;
272e2a09b26SPoul-Henning Kamp 			else
273e2a09b26SPoul-Henning Kamp 				nl = 0;
274e2a09b26SPoul-Henning Kamp 		}
275e2a09b26SPoul-Henning Kamp 	}
276e2a09b26SPoul-Henning Kamp 	if (!nl)
277e2a09b26SPoul-Henning Kamp 		msglogchar('\n', pri);
278b80e3b41SPoul-Henning Kamp 	msgbuftrigger = 1;
279e2a09b26SPoul-Henning Kamp 	FREE(miov, M_TEMP);
280e2a09b26SPoul-Henning Kamp 	FREE(consbuffer, M_TEMP);
281e2a09b26SPoul-Henning Kamp 	return;
282df8bae1dSRodney W. Grimes }
283df8bae1dSRodney W. Grimes 
2846ddbf1e2SGary Palmer int
285df8bae1dSRodney W. Grimes printf(const char *fmt, ...)
286df8bae1dSRodney W. Grimes {
287df8bae1dSRodney W. Grimes 	va_list ap;
2884d88c459SPoul-Henning Kamp 	int savintr;
289791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
29065ed8cbdSJustin T. Gibbs 	int retval;
291df8bae1dSRodney W. Grimes 
292df8bae1dSRodney W. Grimes 	savintr = consintr;		/* disable interrupts */
293df8bae1dSRodney W. Grimes 	consintr = 0;
294df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
295791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
296791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
297e2a09b26SPoul-Henning Kamp 	pca.pri = -1;
29865ed8cbdSJustin T. Gibbs 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
299df8bae1dSRodney W. Grimes 	va_end(ap);
300df8bae1dSRodney W. Grimes 	if (!panicstr)
301b80e3b41SPoul-Henning Kamp 		msgbuftrigger = 1;
302df8bae1dSRodney W. Grimes 	consintr = savintr;		/* reenable interrupts */
30358a24f79SPoul-Henning Kamp 	return (retval);
304df8bae1dSRodney W. Grimes }
305df8bae1dSRodney W. Grimes 
306f1550d9dSDoug Rabson int
307791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap)
308791d77e0SPoul-Henning Kamp {
3094d88c459SPoul-Henning Kamp 	int savintr;
310791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
311f1550d9dSDoug Rabson 	int retval;
312791d77e0SPoul-Henning Kamp 
313791d77e0SPoul-Henning Kamp 	savintr = consintr;		/* disable interrupts */
314791d77e0SPoul-Henning Kamp 	consintr = 0;
315791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
316791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
317e2a09b26SPoul-Henning Kamp 	pca.pri = -1;
318f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
319791d77e0SPoul-Henning Kamp 	if (!panicstr)
320b80e3b41SPoul-Henning Kamp 		msgbuftrigger = 1;
321791d77e0SPoul-Henning Kamp 	consintr = savintr;		/* reenable interrupts */
32258a24f79SPoul-Henning Kamp 	return (retval);
323791d77e0SPoul-Henning Kamp }
324791d77e0SPoul-Henning Kamp 
325791d77e0SPoul-Henning Kamp /*
326791d77e0SPoul-Henning Kamp  * Print a character on console or users terminal.  If destination is
327e796e00dSPoul-Henning Kamp  * the console then the last bunch of characters are saved in msgbuf for
328791d77e0SPoul-Henning Kamp  * inspection later.
329791d77e0SPoul-Henning Kamp  */
330791d77e0SPoul-Henning Kamp static void
331791d77e0SPoul-Henning Kamp putchar(int c, void *arg)
332791d77e0SPoul-Henning Kamp {
333791d77e0SPoul-Henning Kamp 	struct putchar_arg *ap = (struct putchar_arg*) arg;
334791d77e0SPoul-Henning Kamp 	int flags = ap->flags;
335791d77e0SPoul-Henning Kamp 	struct tty *tp = ap->tty;
336791d77e0SPoul-Henning Kamp 	if (panicstr)
337791d77e0SPoul-Henning Kamp 		constty = NULL;
338791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && tp == NULL && constty) {
339791d77e0SPoul-Henning Kamp 		tp = constty;
340791d77e0SPoul-Henning Kamp 		flags |= TOTTY;
341791d77e0SPoul-Henning Kamp 	}
342791d77e0SPoul-Henning Kamp 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
343791d77e0SPoul-Henning Kamp 	    (flags & TOCONS) && tp == constty)
344791d77e0SPoul-Henning Kamp 		constty = NULL;
345791d77e0SPoul-Henning Kamp 	if ((flags & TOLOG))
346e2a09b26SPoul-Henning Kamp 		msglogchar(c, ap->pri);
347791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && constty == NULL && c != '\0')
348791d77e0SPoul-Henning Kamp 		(*v_putc)(c);
349791d77e0SPoul-Henning Kamp }
350791d77e0SPoul-Henning Kamp 
351791d77e0SPoul-Henning Kamp /*
352791d77e0SPoul-Henning Kamp  * Scaled down version of sprintf(3).
353791d77e0SPoul-Henning Kamp  */
354791d77e0SPoul-Henning Kamp int
355791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...)
356791d77e0SPoul-Henning Kamp {
357791d77e0SPoul-Henning Kamp 	int retval;
358791d77e0SPoul-Henning Kamp 	va_list ap;
359791d77e0SPoul-Henning Kamp 
360791d77e0SPoul-Henning Kamp 	va_start(ap, cfmt);
361791d77e0SPoul-Henning Kamp 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
362fe96d47dSPoul-Henning Kamp 	buf[retval] = '\0';
363791d77e0SPoul-Henning Kamp 	va_end(ap);
36458a24f79SPoul-Henning Kamp 	return (retval);
365791d77e0SPoul-Henning Kamp }
366791d77e0SPoul-Henning Kamp 
367791d77e0SPoul-Henning Kamp /*
36899237364SAndrey A. Chernov  * Scaled down version of vsprintf(3).
36999237364SAndrey A. Chernov  */
37099237364SAndrey A. Chernov int
37199237364SAndrey A. Chernov vsprintf(char *buf, const char *cfmt, va_list ap)
37299237364SAndrey A. Chernov {
37399237364SAndrey A. Chernov 	int retval;
37499237364SAndrey A. Chernov 
37599237364SAndrey A. Chernov 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
37699237364SAndrey A. Chernov 	buf[retval] = '\0';
37758a24f79SPoul-Henning Kamp 	return (retval);
37899237364SAndrey A. Chernov }
37999237364SAndrey A. Chernov 
38099237364SAndrey A. Chernov /*
3818245f3f5SArchie Cobbs  * Scaled down version of snprintf(3).
3828245f3f5SArchie Cobbs  */
3838245f3f5SArchie Cobbs int
3848245f3f5SArchie Cobbs snprintf(char *str, size_t size, const char *format, ...)
3858245f3f5SArchie Cobbs {
3868245f3f5SArchie Cobbs 	int retval;
3878245f3f5SArchie Cobbs 	va_list ap;
3888245f3f5SArchie Cobbs 
3898245f3f5SArchie Cobbs 	va_start(ap, format);
3908245f3f5SArchie Cobbs 	retval = vsnprintf(str, size, format, ap);
3918245f3f5SArchie Cobbs 	va_end(ap);
3928245f3f5SArchie Cobbs 	return(retval);
3938245f3f5SArchie Cobbs }
3948245f3f5SArchie Cobbs 
3958245f3f5SArchie Cobbs /*
3968245f3f5SArchie Cobbs  * Scaled down version of vsnprintf(3).
3978245f3f5SArchie Cobbs  */
3988245f3f5SArchie Cobbs int
3998245f3f5SArchie Cobbs vsnprintf(char *str, size_t size, const char *format, va_list ap)
4008245f3f5SArchie Cobbs {
4018245f3f5SArchie Cobbs 	struct snprintf_arg info;
4028245f3f5SArchie Cobbs 	int retval;
4038245f3f5SArchie Cobbs 
4048245f3f5SArchie Cobbs 	info.str = str;
4058245f3f5SArchie Cobbs 	info.remain = size;
4068245f3f5SArchie Cobbs 	retval = kvprintf(format, snprintf_func, &info, 10, ap);
4078245f3f5SArchie Cobbs 	if (info.remain >= 1)
4088245f3f5SArchie Cobbs 		*info.str++ = '\0';
40958a24f79SPoul-Henning Kamp 	return (retval);
4108245f3f5SArchie Cobbs }
4118245f3f5SArchie Cobbs 
4128245f3f5SArchie Cobbs static void
4138245f3f5SArchie Cobbs snprintf_func(int ch, void *arg)
4148245f3f5SArchie Cobbs {
4158245f3f5SArchie Cobbs 	struct snprintf_arg *const info = arg;
4168245f3f5SArchie Cobbs 
4178245f3f5SArchie Cobbs 	if (info->remain >= 2) {
4188245f3f5SArchie Cobbs 		*info->str++ = ch;
4198245f3f5SArchie Cobbs 		info->remain--;
4208245f3f5SArchie Cobbs 	}
4218245f3f5SArchie Cobbs }
4228245f3f5SArchie Cobbs 
4238245f3f5SArchie Cobbs /*
42482941964SPeter Wemm  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
42505292ba2SArchie Cobbs  * order; return an optional length and a pointer to the last character
42605292ba2SArchie Cobbs  * written in the buffer (i.e., the first character of the string).
42705292ba2SArchie Cobbs  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
428791d77e0SPoul-Henning Kamp  */
429791d77e0SPoul-Henning Kamp static char *
430f16a5176SDag-Erling Smørgrav ksprintn(char *nbuf, uintmax_t num, int base, int *lenp)
43105292ba2SArchie Cobbs {
4324d88c459SPoul-Henning Kamp 	char *p;
433791d77e0SPoul-Henning Kamp 
43405292ba2SArchie Cobbs 	p = nbuf;
435ad4f8dbdSArchie Cobbs 	*p = '\0';
436791d77e0SPoul-Henning Kamp 	do {
4373b1f7e7dSDag-Erling Smørgrav 		*++p = hex2ascii(num % base);
4383b1f7e7dSDag-Erling Smørgrav 	} while (num /= base);
4397d921a01SPeter Wemm 	if (lenp)
4407d921a01SPeter Wemm 		*lenp = p - nbuf;
4417d921a01SPeter Wemm 	return (p);
4427d921a01SPeter Wemm }
443791d77e0SPoul-Henning Kamp 
444df8bae1dSRodney W. Grimes /*
445df8bae1dSRodney W. Grimes  * Scaled down version of printf(3).
446df8bae1dSRodney W. Grimes  *
447df8bae1dSRodney W. Grimes  * Two additional formats:
448df8bae1dSRodney W. Grimes  *
449df8bae1dSRodney W. Grimes  * The format %b is supported to decode error registers.
450df8bae1dSRodney W. Grimes  * Its usage is:
451df8bae1dSRodney W. Grimes  *
452df8bae1dSRodney W. Grimes  *	printf("reg=%b\n", regval, "<base><arg>*");
453df8bae1dSRodney W. Grimes  *
454df8bae1dSRodney W. Grimes  * where <base> is the output base expressed as a control character, e.g.
455df8bae1dSRodney W. Grimes  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
456df8bae1dSRodney W. Grimes  * the first of which gives the bit number to be inspected (origin 1), and
457df8bae1dSRodney W. Grimes  * the next characters (up to a control character, i.e. a character <= 32),
458df8bae1dSRodney W. Grimes  * give the name of the register.  Thus:
459df8bae1dSRodney W. Grimes  *
4604e31a37bSGary Palmer  *	kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
461df8bae1dSRodney W. Grimes  *
462df8bae1dSRodney W. Grimes  * would produce output:
463df8bae1dSRodney W. Grimes  *
464df8bae1dSRodney W. Grimes  *	reg=3<BITTWO,BITONE>
465df8bae1dSRodney W. Grimes  *
466120f0783SPoul-Henning Kamp  * XXX:  %D  -- Hexdump, takes pointer and separator string:
467120f0783SPoul-Henning Kamp  *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
468120f0783SPoul-Henning Kamp  *		("%*D", len, ptr, " " -> XX XX XX XX ...
469df8bae1dSRodney W. Grimes  */
470791d77e0SPoul-Henning Kamp int
471791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
472df8bae1dSRodney W. Grimes {
473791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
474ad4f8dbdSArchie Cobbs 	char nbuf[MAXNBUF];
4753b1f7e7dSDag-Erling Smørgrav 	char *d;
4763b1f7e7dSDag-Erling Smørgrav 	const char *p, *percent, *q;
477120f0783SPoul-Henning Kamp 	u_char *up;
478791d77e0SPoul-Henning Kamp 	int ch, n;
4793b1f7e7dSDag-Erling Smørgrav 	uintmax_t num;
4807d921a01SPeter Wemm 	int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
4813b1f7e7dSDag-Erling Smørgrav 	int jflag;
4824f20e4b1SPoul-Henning Kamp 	int dwidth;
483df8bae1dSRodney W. Grimes 	char padc;
484791d77e0SPoul-Henning Kamp 	int retval = 0;
485791d77e0SPoul-Henning Kamp 
4863b1f7e7dSDag-Erling Smørgrav 	num = 0;
487fe96d47dSPoul-Henning Kamp 	if (!func)
488791d77e0SPoul-Henning Kamp 		d = (char *) arg;
489791d77e0SPoul-Henning Kamp 	else
490fe96d47dSPoul-Henning Kamp 		d = NULL;
4918f5067baSDavid Greenman 
4928f5067baSDavid Greenman 	if (fmt == NULL)
4932336b9d7SBruce Evans 		fmt = "(fmt null)\n";
494b4b2f81eSPoul-Henning Kamp 
495120f0783SPoul-Henning Kamp 	if (radix < 2 || radix > 36)
496b4b2f81eSPoul-Henning Kamp 		radix = 10;
497b4b2f81eSPoul-Henning Kamp 
498df8bae1dSRodney W. Grimes 	for (;;) {
499df8bae1dSRodney W. Grimes 		padc = ' ';
500df8bae1dSRodney W. Grimes 		width = 0;
501e0c95ed9SBruce Evans 		while ((ch = (u_char)*fmt++) != '%') {
502df8bae1dSRodney W. Grimes 			if (ch == '\0')
50358a24f79SPoul-Henning Kamp 				return (retval);
504791d77e0SPoul-Henning Kamp 			PCHAR(ch);
505df8bae1dSRodney W. Grimes 		}
5063b1f7e7dSDag-Erling Smørgrav 		percent = fmt - 1;
5077d921a01SPeter Wemm 		qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
5084f20e4b1SPoul-Henning Kamp 		sign = 0; dot = 0; dwidth = 0;
5093b1f7e7dSDag-Erling Smørgrav 		jflag = 0;
510e0c95ed9SBruce Evans reswitch:	switch (ch = (u_char)*fmt++) {
5114830092aSPoul-Henning Kamp 		case '.':
5124830092aSPoul-Henning Kamp 			dot = 1;
5134830092aSPoul-Henning Kamp 			goto reswitch;
514791d77e0SPoul-Henning Kamp 		case '#':
515791d77e0SPoul-Henning Kamp 			sharpflag = 1;
516791d77e0SPoul-Henning Kamp 			goto reswitch;
517791d77e0SPoul-Henning Kamp 		case '+':
518791d77e0SPoul-Henning Kamp 			sign = 1;
519791d77e0SPoul-Henning Kamp 			goto reswitch;
520791d77e0SPoul-Henning Kamp 		case '-':
521791d77e0SPoul-Henning Kamp 			ladjust = 1;
522791d77e0SPoul-Henning Kamp 			goto reswitch;
523791d77e0SPoul-Henning Kamp 		case '%':
524791d77e0SPoul-Henning Kamp 			PCHAR(ch);
525791d77e0SPoul-Henning Kamp 			break;
526791d77e0SPoul-Henning Kamp 		case '*':
527ed71c342SPoul-Henning Kamp 			if (!dot) {
528791d77e0SPoul-Henning Kamp 				width = va_arg(ap, int);
529791d77e0SPoul-Henning Kamp 				if (width < 0) {
530791d77e0SPoul-Henning Kamp 					ladjust = !ladjust;
531791d77e0SPoul-Henning Kamp 					width = -width;
532791d77e0SPoul-Henning Kamp 				}
533ed71c342SPoul-Henning Kamp 			} else {
534ed71c342SPoul-Henning Kamp 				dwidth = va_arg(ap, int);
535ed71c342SPoul-Henning Kamp 			}
536791d77e0SPoul-Henning Kamp 			goto reswitch;
537df8bae1dSRodney W. Grimes 		case '0':
5384f20e4b1SPoul-Henning Kamp 			if (!dot) {
539df8bae1dSRodney W. Grimes 				padc = '0';
540df8bae1dSRodney W. Grimes 				goto reswitch;
5414f20e4b1SPoul-Henning Kamp 			}
542df8bae1dSRodney W. Grimes 		case '1': case '2': case '3': case '4':
543df8bae1dSRodney W. Grimes 		case '5': case '6': case '7': case '8': case '9':
5444f20e4b1SPoul-Henning Kamp 				for (n = 0;; ++fmt) {
5454f20e4b1SPoul-Henning Kamp 					n = n * 10 + ch - '0';
546df8bae1dSRodney W. Grimes 					ch = *fmt;
547df8bae1dSRodney W. Grimes 					if (ch < '0' || ch > '9')
548df8bae1dSRodney W. Grimes 						break;
549df8bae1dSRodney W. Grimes 				}
5504f20e4b1SPoul-Henning Kamp 			if (dot)
5514f20e4b1SPoul-Henning Kamp 				dwidth = n;
5524f20e4b1SPoul-Henning Kamp 			else
5534f20e4b1SPoul-Henning Kamp 				width = n;
554df8bae1dSRodney W. Grimes 			goto reswitch;
555df8bae1dSRodney W. Grimes 		case 'b':
5563b1f7e7dSDag-Erling Smørgrav 			num = va_arg(ap, int);
557df8bae1dSRodney W. Grimes 			p = va_arg(ap, char *);
5583b1f7e7dSDag-Erling Smørgrav 			for (q = ksprintn(nbuf, num, *p++, NULL); *q;)
559791d77e0SPoul-Henning Kamp 				PCHAR(*q--);
560df8bae1dSRodney W. Grimes 
5613b1f7e7dSDag-Erling Smørgrav 			if (num == 0)
562df8bae1dSRodney W. Grimes 				break;
563df8bae1dSRodney W. Grimes 
564797f2d22SPoul-Henning Kamp 			for (tmp = 0; *p;) {
565797f2d22SPoul-Henning Kamp 				n = *p++;
5663b1f7e7dSDag-Erling Smørgrav 				if (num & (1 << (n - 1))) {
567791d77e0SPoul-Henning Kamp 					PCHAR(tmp ? ',' : '<');
568df8bae1dSRodney W. Grimes 					for (; (n = *p) > ' '; ++p)
569791d77e0SPoul-Henning Kamp 						PCHAR(n);
570df8bae1dSRodney W. Grimes 					tmp = 1;
571df8bae1dSRodney W. Grimes 				} else
572df8bae1dSRodney W. Grimes 					for (; *p > ' '; ++p)
573df8bae1dSRodney W. Grimes 						continue;
574df8bae1dSRodney W. Grimes 			}
575df8bae1dSRodney W. Grimes 			if (tmp)
576791d77e0SPoul-Henning Kamp 				PCHAR('>');
577df8bae1dSRodney W. Grimes 			break;
578df8bae1dSRodney W. Grimes 		case 'c':
579791d77e0SPoul-Henning Kamp 			PCHAR(va_arg(ap, int));
580df8bae1dSRodney W. Grimes 			break;
581120f0783SPoul-Henning Kamp 		case 'D':
582120f0783SPoul-Henning Kamp 			up = va_arg(ap, u_char *);
583120f0783SPoul-Henning Kamp 			p = va_arg(ap, char *);
584120f0783SPoul-Henning Kamp 			if (!width)
585120f0783SPoul-Henning Kamp 				width = 16;
586120f0783SPoul-Henning Kamp 			while(width--) {
587120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up >> 4));
588120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up & 0x0f));
589120f0783SPoul-Henning Kamp 				up++;
590120f0783SPoul-Henning Kamp 				if (width)
591120f0783SPoul-Henning Kamp 					for (q=p;*q;q++)
592120f0783SPoul-Henning Kamp 						PCHAR(*q);
593120f0783SPoul-Henning Kamp 			}
594120f0783SPoul-Henning Kamp 			break;
595df8bae1dSRodney W. Grimes 		case 'd':
596e0b74464SWarner Losh 		case 'i':
597df8bae1dSRodney W. Grimes 			base = 10;
5983b1f7e7dSDag-Erling Smørgrav 			sign = 1;
5993b1f7e7dSDag-Erling Smørgrav 			goto handle_sign;
6003b1f7e7dSDag-Erling Smørgrav 		case 'j':
6013b1f7e7dSDag-Erling Smørgrav 			jflag = 1;
6023b1f7e7dSDag-Erling Smørgrav 			goto reswitch;
603791d77e0SPoul-Henning Kamp 		case 'l':
604301ca4ffSBrian Feldman 			if (lflag) {
605301ca4ffSBrian Feldman 				lflag = 0;
606301ca4ffSBrian Feldman 				qflag = 1;
607301ca4ffSBrian Feldman 			} else
608791d77e0SPoul-Henning Kamp 				lflag = 1;
609791d77e0SPoul-Henning Kamp 			goto reswitch;
6103b1f7e7dSDag-Erling Smørgrav 		case 'n':
6113b1f7e7dSDag-Erling Smørgrav 			if (jflag)
6123b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, intmax_t *)) = retval;
6133b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
6143b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, quad_t *)) = retval;
6157d921a01SPeter Wemm 			else if (lflag)
6163b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, long *)) = retval;
6177d921a01SPeter Wemm 			else
6183b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, int *)) = retval;
6193b1f7e7dSDag-Erling Smørgrav 			break;
6203b1f7e7dSDag-Erling Smørgrav 		case 'o':
621df8bae1dSRodney W. Grimes 			base = 8;
6223b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
62312d17f65SPoul-Henning Kamp 		case 'p':
62412d17f65SPoul-Henning Kamp 			base = 16;
625c41141b0SBruce Evans 			sharpflag = (width == 0);
6263b1f7e7dSDag-Erling Smørgrav 			sign = 0;
6273b1f7e7dSDag-Erling Smørgrav 			num = (uintptr_t)va_arg(ap, void *);
6283b1f7e7dSDag-Erling Smørgrav 			goto number;
6297d921a01SPeter Wemm 		case 'q':
6307d921a01SPeter Wemm 			qflag = 1;
6317d921a01SPeter Wemm 			goto reswitch;
632e0c38587SBruce Evans 		case 'r':
633e0c38587SBruce Evans 			base = radix;
6343b1f7e7dSDag-Erling Smørgrav 			if (sign)
6353b1f7e7dSDag-Erling Smørgrav 				goto handle_sign;
6363b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
637791d77e0SPoul-Henning Kamp 		case 's':
638791d77e0SPoul-Henning Kamp 			p = va_arg(ap, char *);
639791d77e0SPoul-Henning Kamp 			if (p == NULL)
640791d77e0SPoul-Henning Kamp 				p = "(null)";
6414830092aSPoul-Henning Kamp 			if (!dot)
6424830092aSPoul-Henning Kamp 				n = strlen (p);
6434830092aSPoul-Henning Kamp 			else
6444f20e4b1SPoul-Henning Kamp 				for (n = 0; n < dwidth && p[n]; n++)
6454830092aSPoul-Henning Kamp 					continue;
6464f20e4b1SPoul-Henning Kamp 
6474830092aSPoul-Henning Kamp 			width -= n;
6484f20e4b1SPoul-Henning Kamp 
649791d77e0SPoul-Henning Kamp 			if (!ladjust && width > 0)
650791d77e0SPoul-Henning Kamp 				while (width--)
651791d77e0SPoul-Henning Kamp 					PCHAR(padc);
6524830092aSPoul-Henning Kamp 			while (n--)
653791d77e0SPoul-Henning Kamp 				PCHAR(*p++);
654791d77e0SPoul-Henning Kamp 			if (ladjust && width > 0)
655791d77e0SPoul-Henning Kamp 				while (width--)
656791d77e0SPoul-Henning Kamp 					PCHAR(padc);
657791d77e0SPoul-Henning Kamp 			break;
658f53dbe97SBruce Evans 		case 'u':
659f53dbe97SBruce Evans 			base = 10;
6603b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
661df8bae1dSRodney W. Grimes 		case 'x':
662aa998012SMike Smith 		case 'X':
663df8bae1dSRodney W. Grimes 			base = 16;
6643b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
665e0c38587SBruce Evans 		case 'z':
666e0c38587SBruce Evans 			base = 16;
6673b1f7e7dSDag-Erling Smørgrav 			if (sign)
6683b1f7e7dSDag-Erling Smørgrav 				goto handle_sign;
6693b1f7e7dSDag-Erling Smørgrav handle_nosign:
6703b1f7e7dSDag-Erling Smørgrav 			sign = 0;
6713b1f7e7dSDag-Erling Smørgrav 			if (jflag)
6723b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, uintmax_t);
6733b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
6743b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_quad_t);
6753b1f7e7dSDag-Erling Smørgrav 			else if (lflag)
6763b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_long);
6773b1f7e7dSDag-Erling Smørgrav 			else
6783b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_int);
679e0c38587SBruce Evans 			goto number;
6803b1f7e7dSDag-Erling Smørgrav handle_sign:
6813b1f7e7dSDag-Erling Smørgrav 			if (jflag)
6823b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, intmax_t);
6833b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
6843b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, quad_t);
6853b1f7e7dSDag-Erling Smørgrav 			else if (lflag)
6863b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, long);
6873b1f7e7dSDag-Erling Smørgrav 			else
6883b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, int);
6897d921a01SPeter Wemm number:
6903b1f7e7dSDag-Erling Smørgrav 			if (sign && (intmax_t)num < 0) {
6917d921a01SPeter Wemm 				neg = 1;
6923b1f7e7dSDag-Erling Smørgrav 				num = -(intmax_t)num;
6937d921a01SPeter Wemm 			}
6943b1f7e7dSDag-Erling Smørgrav 			p = ksprintn(nbuf, num, base, &tmp);
6953b1f7e7dSDag-Erling Smørgrav 			if (sharpflag && num != 0) {
696791d77e0SPoul-Henning Kamp 				if (base == 8)
697791d77e0SPoul-Henning Kamp 					tmp++;
698791d77e0SPoul-Henning Kamp 				else if (base == 16)
699791d77e0SPoul-Henning Kamp 					tmp += 2;
700791d77e0SPoul-Henning Kamp 			}
701791d77e0SPoul-Henning Kamp 			if (neg)
702791d77e0SPoul-Henning Kamp 				tmp++;
703791d77e0SPoul-Henning Kamp 
704791d77e0SPoul-Henning Kamp 			if (!ladjust && width && (width -= tmp) > 0)
705df8bae1dSRodney W. Grimes 				while (width--)
706791d77e0SPoul-Henning Kamp 					PCHAR(padc);
707791d77e0SPoul-Henning Kamp 			if (neg)
708791d77e0SPoul-Henning Kamp 				PCHAR('-');
7093b1f7e7dSDag-Erling Smørgrav 			if (sharpflag && num != 0) {
710791d77e0SPoul-Henning Kamp 				if (base == 8) {
711791d77e0SPoul-Henning Kamp 					PCHAR('0');
712791d77e0SPoul-Henning Kamp 				} else if (base == 16) {
713791d77e0SPoul-Henning Kamp 					PCHAR('0');
714791d77e0SPoul-Henning Kamp 					PCHAR('x');
715791d77e0SPoul-Henning Kamp 				}
716791d77e0SPoul-Henning Kamp 			}
717791d77e0SPoul-Henning Kamp 
718797f2d22SPoul-Henning Kamp 			while (*p)
719791d77e0SPoul-Henning Kamp 				PCHAR(*p--);
720791d77e0SPoul-Henning Kamp 
721791d77e0SPoul-Henning Kamp 			if (ladjust && width && (width -= tmp) > 0)
722791d77e0SPoul-Henning Kamp 				while (width--)
723791d77e0SPoul-Henning Kamp 					PCHAR(padc);
724791d77e0SPoul-Henning Kamp 
725df8bae1dSRodney W. Grimes 			break;
726df8bae1dSRodney W. Grimes 		default:
7273b1f7e7dSDag-Erling Smørgrav 			while (percent < fmt)
7283b1f7e7dSDag-Erling Smørgrav 				PCHAR(*percent++);
729791d77e0SPoul-Henning Kamp 			break;
730df8bae1dSRodney W. Grimes 		}
731df8bae1dSRodney W. Grimes 	}
732791d77e0SPoul-Henning Kamp #undef PCHAR
733df8bae1dSRodney W. Grimes }
734df8bae1dSRodney W. Grimes 
735df8bae1dSRodney W. Grimes /*
736e2a09b26SPoul-Henning Kamp  * Put character in log buffer with a particular priority.
737df8bae1dSRodney W. Grimes  */
738df8bae1dSRodney W. Grimes static void
739e2a09b26SPoul-Henning Kamp msglogchar(int c, int pri)
740e2a09b26SPoul-Henning Kamp {
741e2a09b26SPoul-Henning Kamp 	static int lastpri = -1;
742e2a09b26SPoul-Henning Kamp 	static int dangling;
743e2a09b26SPoul-Henning Kamp 	char nbuf[MAXNBUF];
744e2a09b26SPoul-Henning Kamp 	char *p;
745e2a09b26SPoul-Henning Kamp 
746e2a09b26SPoul-Henning Kamp 	if (!msgbufmapped)
747e2a09b26SPoul-Henning Kamp 		return;
748e2a09b26SPoul-Henning Kamp 	if (c == '\0' || c == '\r')
749e2a09b26SPoul-Henning Kamp 		return;
750e2a09b26SPoul-Henning Kamp 	if (pri != -1 && pri != lastpri) {
751e2a09b26SPoul-Henning Kamp 		if (dangling) {
752e2a09b26SPoul-Henning Kamp 			msgaddchar('\n', NULL);
753e2a09b26SPoul-Henning Kamp 			dangling = 0;
754e2a09b26SPoul-Henning Kamp 		}
755e2a09b26SPoul-Henning Kamp 		msgaddchar('<', NULL);
7563b1f7e7dSDag-Erling Smørgrav 		for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL); *p;)
757e2a09b26SPoul-Henning Kamp 			msgaddchar(*p--, NULL);
758e2a09b26SPoul-Henning Kamp 		msgaddchar('>', NULL);
759e2a09b26SPoul-Henning Kamp 		lastpri = pri;
760e2a09b26SPoul-Henning Kamp 	}
761e2a09b26SPoul-Henning Kamp 	msgaddchar(c, NULL);
762e2a09b26SPoul-Henning Kamp 	if (c == '\n') {
763e2a09b26SPoul-Henning Kamp 		dangling = 0;
764e2a09b26SPoul-Henning Kamp 		lastpri = -1;
765e2a09b26SPoul-Henning Kamp 	} else {
766e2a09b26SPoul-Henning Kamp 		dangling = 1;
767e2a09b26SPoul-Henning Kamp 	}
768e2a09b26SPoul-Henning Kamp }
769e2a09b26SPoul-Henning Kamp 
770e2a09b26SPoul-Henning Kamp /*
771e2a09b26SPoul-Henning Kamp  * Put char in log buffer
772e2a09b26SPoul-Henning Kamp  */
773e2a09b26SPoul-Henning Kamp static void
774e2a09b26SPoul-Henning Kamp msgaddchar(int c, void *dummy)
775df8bae1dSRodney W. Grimes {
776791d77e0SPoul-Henning Kamp 	struct msgbuf *mbp;
777df8bae1dSRodney W. Grimes 
778e2a09b26SPoul-Henning Kamp 	if (!msgbufmapped)
779e2a09b26SPoul-Henning Kamp 		return;
780df8bae1dSRodney W. Grimes 	mbp = msgbufp;
78158067a99SPoul-Henning Kamp 	mbp->msg_ptr[mbp->msg_bufx++] = c;
78258067a99SPoul-Henning Kamp 	if (mbp->msg_bufx >= mbp->msg_size)
783df8bae1dSRodney W. Grimes 		mbp->msg_bufx = 0;
78402d5c7b1SDavid Greenman 	/* If the buffer is full, keep the most recent data. */
785a3f4faceSBruce Evans 	if (mbp->msg_bufr == mbp->msg_bufx) {
78658067a99SPoul-Henning Kamp 		if (++mbp->msg_bufr >= mbp->msg_size)
787a3f4faceSBruce Evans 			mbp->msg_bufr = 0;
788a3f4faceSBruce Evans 	}
789df8bae1dSRodney W. Grimes }
790e796e00dSPoul-Henning Kamp 
791eb9d435aSJonathan Lemon static void
792eb9d435aSJonathan Lemon msgbufcopy(struct msgbuf *oldp)
793eb9d435aSJonathan Lemon {
794eb9d435aSJonathan Lemon 	int pos;
795eb9d435aSJonathan Lemon 
796eb9d435aSJonathan Lemon 	pos = oldp->msg_bufr;
797eb9d435aSJonathan Lemon 	while (pos != oldp->msg_bufx) {
798e2a09b26SPoul-Henning Kamp 		msglogchar(oldp->msg_ptr[pos], -1);
799eb9d435aSJonathan Lemon 		if (++pos >= oldp->msg_size)
800eb9d435aSJonathan Lemon 			pos = 0;
801eb9d435aSJonathan Lemon 	}
802eb9d435aSJonathan Lemon }
803eb9d435aSJonathan Lemon 
804e796e00dSPoul-Henning Kamp void
805e796e00dSPoul-Henning Kamp msgbufinit(void *ptr, size_t size)
806e796e00dSPoul-Henning Kamp {
807e796e00dSPoul-Henning Kamp 	char *cp;
808eb9d435aSJonathan Lemon 	static struct msgbuf *oldp = NULL;
809e796e00dSPoul-Henning Kamp 
810cca8f980SIan Dowse 	size -= sizeof(*msgbufp);
811e796e00dSPoul-Henning Kamp 	cp = (char *)ptr;
812cca8f980SIan Dowse 	msgbufp = (struct msgbuf *) (cp + size);
813cca8f980SIan Dowse 	if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
814cca8f980SIan Dowse 	    msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) {
815e796e00dSPoul-Henning Kamp 		bzero(cp, size);
816cca8f980SIan Dowse 		bzero(msgbufp, sizeof(*msgbufp));
817e796e00dSPoul-Henning Kamp 		msgbufp->msg_magic = MSG_MAGIC;
818e796e00dSPoul-Henning Kamp 		msgbufp->msg_size = (char *)msgbufp - cp;
819e796e00dSPoul-Henning Kamp 	}
820cca8f980SIan Dowse 	msgbufp->msg_ptr = cp;
821eb9d435aSJonathan Lemon 	if (msgbufmapped && oldp != msgbufp)
822eb9d435aSJonathan Lemon 		msgbufcopy(oldp);
823e796e00dSPoul-Henning Kamp 	msgbufmapped = 1;
824eb9d435aSJonathan Lemon 	oldp = msgbufp;
825e796e00dSPoul-Henning Kamp }
826e796e00dSPoul-Henning Kamp 
827d0615c64SAndrew R. Reiter SYSCTL_DECL(_security_bsd);
8286f3933faSRobert Watson 
8296f3933faSRobert Watson static int unprivileged_read_msgbuf = 1;
830d0615c64SAndrew R. Reiter SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf,
8316f3933faSRobert Watson     CTLFLAG_RW, &unprivileged_read_msgbuf, 0,
8326f3933faSRobert Watson     "Unprivileged processes may read the kernel message buffer");
8336f3933faSRobert Watson 
834948d3d94SThomas Moestl /* Sysctls for accessing/clearing the msgbuf */
835948d3d94SThomas Moestl static int
836948d3d94SThomas Moestl sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
837948d3d94SThomas Moestl {
838948d3d94SThomas Moestl 	int error;
839948d3d94SThomas Moestl 
8406f3933faSRobert Watson 	if (!unprivileged_read_msgbuf) {
84144731cabSJohn Baldwin 		error = suser(req->td);
8426f3933faSRobert Watson 		if (error)
8436f3933faSRobert Watson 			return (error);
8446f3933faSRobert Watson 	}
8456f3933faSRobert Watson 
846948d3d94SThomas Moestl 	/*
847948d3d94SThomas Moestl 	 * Unwind the buffer, so that it's linear (possibly starting with
848948d3d94SThomas Moestl 	 * some initial nulls).
849948d3d94SThomas Moestl 	 */
850948d3d94SThomas Moestl 	error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
851948d3d94SThomas Moestl 	    msgbufp->msg_size - msgbufp->msg_bufx, req);
852948d3d94SThomas Moestl 	if (error)
853948d3d94SThomas Moestl 		return (error);
854948d3d94SThomas Moestl 	if (msgbufp->msg_bufx > 0) {
855948d3d94SThomas Moestl 		error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
856948d3d94SThomas Moestl 		    msgbufp->msg_bufx, req);
857948d3d94SThomas Moestl 	}
858948d3d94SThomas Moestl 	return (error);
859948d3d94SThomas Moestl }
860948d3d94SThomas Moestl 
861948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
862948d3d94SThomas Moestl     0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
863948d3d94SThomas Moestl 
864948d3d94SThomas Moestl static int msgbuf_clear;
865948d3d94SThomas Moestl 
866948d3d94SThomas Moestl static int
867948d3d94SThomas Moestl sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
868948d3d94SThomas Moestl {
869948d3d94SThomas Moestl 	int error;
870948d3d94SThomas Moestl 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
871948d3d94SThomas Moestl 	if (!error && req->newptr) {
872948d3d94SThomas Moestl 		/* Clear the buffer and reset write pointer */
873948d3d94SThomas Moestl 		bzero(msgbufp->msg_ptr, msgbufp->msg_size);
874948d3d94SThomas Moestl 		msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
875948d3d94SThomas Moestl 		msgbuf_clear = 0;
876948d3d94SThomas Moestl 	}
877948d3d94SThomas Moestl 	return (error);
878948d3d94SThomas Moestl }
879948d3d94SThomas Moestl 
880948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
881948d3d94SThomas Moestl     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
882948d3d94SThomas Moestl     sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
883948d3d94SThomas Moestl 
884e796e00dSPoul-Henning Kamp #include "opt_ddb.h"
885e796e00dSPoul-Henning Kamp #ifdef DDB
886e796e00dSPoul-Henning Kamp #include <ddb/ddb.h>
887e796e00dSPoul-Henning Kamp 
888e796e00dSPoul-Henning Kamp DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
889e796e00dSPoul-Henning Kamp {
890e796e00dSPoul-Henning Kamp 	int i, j;
891e796e00dSPoul-Henning Kamp 
892e796e00dSPoul-Henning Kamp 	if (!msgbufmapped) {
893e796e00dSPoul-Henning Kamp 		db_printf("msgbuf not mapped yet\n");
894e796e00dSPoul-Henning Kamp 		return;
895e796e00dSPoul-Henning Kamp 	}
896e796e00dSPoul-Henning Kamp 	db_printf("msgbufp = %p\n", msgbufp);
897e796e00dSPoul-Henning Kamp 	db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
898e796e00dSPoul-Henning Kamp 	    msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
899e796e00dSPoul-Henning Kamp 	    msgbufp->msg_bufx, msgbufp->msg_ptr);
900e796e00dSPoul-Henning Kamp 	for (i = 0; i < msgbufp->msg_size; i++) {
901e796e00dSPoul-Henning Kamp 		j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
902e796e00dSPoul-Henning Kamp 		db_printf("%c", msgbufp->msg_ptr[j]);
903e796e00dSPoul-Henning Kamp 	}
904e796e00dSPoul-Henning Kamp 	db_printf("\n");
905e796e00dSPoul-Henning Kamp }
906e796e00dSPoul-Henning Kamp 
907e796e00dSPoul-Henning Kamp #endif /* DDB */
908