xref: /freebsd/sys/kern/subr_prf.c (revision ad4f8dbd233631f3fead91e9f6587231d5208236)
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
39ad4f8dbdSArchie Cobbs  * $Id: subr_prf.c,v 1.52 1999/06/01 18:20:29 jlemon Exp $
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44e796e00dSPoul-Henning Kamp #include <sys/kernel.h>
45df8bae1dSRodney W. Grimes #include <sys/msgbuf.h>
46a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
47df8bae1dSRodney W. Grimes #include <sys/proc.h>
48df8bae1dSRodney W. Grimes #include <sys/tty.h>
49df8bae1dSRodney W. Grimes #include <sys/tprintf.h>
50df8bae1dSRodney W. Grimes #include <sys/syslog.h>
514fb0b0deSJoerg Wunsch #include <machine/cons.h>
52df8bae1dSRodney W. Grimes 
53df8bae1dSRodney W. Grimes /*
54df8bae1dSRodney W. Grimes  * Note that stdarg.h and the ANSI style va_start macro is used for both
55df8bae1dSRodney W. Grimes  * ANSI and traditional C compilers.
56df8bae1dSRodney W. Grimes  */
57df8bae1dSRodney W. Grimes #include <machine/stdarg.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #define TOCONS	0x01
60df8bae1dSRodney W. Grimes #define TOTTY	0x02
61df8bae1dSRodney W. Grimes #define TOLOG	0x04
62df8bae1dSRodney W. Grimes 
63ad4f8dbdSArchie Cobbs /* Max number conversion buffer length: a long in base 8, plus NUL byte */
64ad4f8dbdSArchie Cobbs #define MAXNBUF	(sizeof(long) * NBBY / 3 + 2)
65ad4f8dbdSArchie Cobbs 
66df8bae1dSRodney W. Grimes struct	tty *constty;			/* pointer to console "window" tty */
67df8bae1dSRodney W. Grimes 
688245f3f5SArchie Cobbs struct putchar_arg {
698245f3f5SArchie Cobbs 	int flags;
708245f3f5SArchie Cobbs 	struct tty *tty;
718245f3f5SArchie Cobbs };
728245f3f5SArchie Cobbs 
738245f3f5SArchie Cobbs struct snprintf_arg {
748245f3f5SArchie Cobbs 	char *str;
758245f3f5SArchie Cobbs 	size_t remain;
768245f3f5SArchie Cobbs };
778245f3f5SArchie Cobbs 
7887b6de2bSPoul-Henning Kamp static void (*v_putc)(int) = cnputc;	/* routine to putc on virtual console */
7987b6de2bSPoul-Henning Kamp static void  logpri __P((int level));
80791d77e0SPoul-Henning Kamp static void  msglogchar(int c, void *dummyarg);
81791d77e0SPoul-Henning Kamp static void  putchar __P((int ch, void *arg));
82ad4f8dbdSArchie Cobbs static char *ksprintn __P((char *nbuf, u_long num, int base, int *len));
838245f3f5SArchie Cobbs static void  snprintf_func __P((int ch, void *arg));
84df8bae1dSRodney W. Grimes 
8587b6de2bSPoul-Henning Kamp static int consintr = 1;		/* Ok to handle console interrupts? */
86e796e00dSPoul-Henning Kamp static int msgbufmapped;		/* Set when safe to use msgbuf */
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes /*
89df8bae1dSRodney W. Grimes  * Warn that a system table is full.
90df8bae1dSRodney W. Grimes  */
91df8bae1dSRodney W. Grimes void
92df8bae1dSRodney W. Grimes tablefull(tab)
93df8bae1dSRodney W. Grimes 	const char *tab;
94df8bae1dSRodney W. Grimes {
95df8bae1dSRodney W. Grimes 
96df8bae1dSRodney W. Grimes 	log(LOG_ERR, "%s: table is full\n", tab);
97df8bae1dSRodney W. Grimes }
98df8bae1dSRodney W. Grimes 
99df8bae1dSRodney W. Grimes /*
100df8bae1dSRodney W. Grimes  * Uprintf prints to the controlling terminal for the current process.
101df8bae1dSRodney W. Grimes  * It may block if the tty queue is overfull.  No message is printed if
102df8bae1dSRodney W. Grimes  * the queue does not clear in a reasonable time.
103df8bae1dSRodney W. Grimes  */
104df8bae1dSRodney W. Grimes void
105df8bae1dSRodney W. Grimes uprintf(const char *fmt, ...)
106df8bae1dSRodney W. Grimes {
107791d77e0SPoul-Henning Kamp 	struct proc *p = curproc;
108df8bae1dSRodney W. Grimes 	va_list ap;
109791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
110df8bae1dSRodney W. Grimes 
111df8bae1dSRodney W. Grimes 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
112df8bae1dSRodney W. Grimes 		va_start(ap, fmt);
113791d77e0SPoul-Henning Kamp 		pca.tty = p->p_session->s_ttyp;
114791d77e0SPoul-Henning Kamp 		pca.flags = TOTTY;
115791d77e0SPoul-Henning Kamp 		kvprintf(fmt, putchar, &pca, 10, ap);
116df8bae1dSRodney W. Grimes 		va_end(ap);
117df8bae1dSRodney W. Grimes 	}
118df8bae1dSRodney W. Grimes }
119df8bae1dSRodney W. Grimes 
120df8bae1dSRodney W. Grimes tpr_t
121df8bae1dSRodney W. Grimes tprintf_open(p)
122df8bae1dSRodney W. Grimes 	register struct proc *p;
123df8bae1dSRodney W. Grimes {
124df8bae1dSRodney W. Grimes 
125df8bae1dSRodney W. Grimes 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
126df8bae1dSRodney W. Grimes 		SESSHOLD(p->p_session);
127df8bae1dSRodney W. Grimes 		return ((tpr_t) p->p_session);
128df8bae1dSRodney W. Grimes 	}
129df8bae1dSRodney W. Grimes 	return ((tpr_t) NULL);
130df8bae1dSRodney W. Grimes }
131df8bae1dSRodney W. Grimes 
132df8bae1dSRodney W. Grimes void
133df8bae1dSRodney W. Grimes tprintf_close(sess)
134df8bae1dSRodney W. Grimes 	tpr_t sess;
135df8bae1dSRodney W. Grimes {
136df8bae1dSRodney W. Grimes 
137df8bae1dSRodney W. Grimes 	if (sess)
138df8bae1dSRodney W. Grimes 		SESSRELE((struct session *) sess);
139df8bae1dSRodney W. Grimes }
140df8bae1dSRodney W. Grimes 
141df8bae1dSRodney W. Grimes /*
142df8bae1dSRodney W. Grimes  * tprintf prints on the controlling terminal associated
143df8bae1dSRodney W. Grimes  * with the given session.
144df8bae1dSRodney W. Grimes  */
145df8bae1dSRodney W. Grimes void
146df8bae1dSRodney W. Grimes tprintf(tpr_t tpr, const char *fmt, ...)
147df8bae1dSRodney W. Grimes {
148df8bae1dSRodney W. Grimes 	register struct session *sess = (struct session *)tpr;
149df8bae1dSRodney W. Grimes 	struct tty *tp = NULL;
150df8bae1dSRodney W. Grimes 	int flags = TOLOG;
151df8bae1dSRodney W. Grimes 	va_list ap;
152791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
153df8bae1dSRodney W. Grimes 
154df8bae1dSRodney W. Grimes 	logpri(LOG_INFO);
155df8bae1dSRodney W. Grimes 	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
156df8bae1dSRodney W. Grimes 		flags |= TOTTY;
157df8bae1dSRodney W. Grimes 		tp = sess->s_ttyp;
158df8bae1dSRodney W. Grimes 	}
159df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
160791d77e0SPoul-Henning Kamp 	pca.tty = tp;
161791d77e0SPoul-Henning Kamp 	pca.flags = flags;
162791d77e0SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
163df8bae1dSRodney W. Grimes 	va_end(ap);
164df8bae1dSRodney W. Grimes 	logwakeup();
165df8bae1dSRodney W. Grimes }
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes /*
168df8bae1dSRodney W. Grimes  * Ttyprintf displays a message on a tty; it should be used only by
169df8bae1dSRodney W. Grimes  * the tty driver, or anything that knows the underlying tty will not
170df8bae1dSRodney W. Grimes  * be revoke(2)'d away.  Other callers should use tprintf.
171df8bae1dSRodney W. Grimes  */
172df8bae1dSRodney W. Grimes void
173df8bae1dSRodney W. Grimes ttyprintf(struct tty *tp, const char *fmt, ...)
174df8bae1dSRodney W. Grimes {
175df8bae1dSRodney W. Grimes 	va_list ap;
176791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
177df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
178791d77e0SPoul-Henning Kamp 	pca.tty = tp;
179791d77e0SPoul-Henning Kamp 	pca.flags = TOTTY;
180791d77e0SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
181df8bae1dSRodney W. Grimes 	va_end(ap);
182df8bae1dSRodney W. Grimes }
183df8bae1dSRodney W. Grimes 
184df8bae1dSRodney W. Grimes extern	int log_open;
185df8bae1dSRodney W. Grimes 
186df8bae1dSRodney W. Grimes /*
187df8bae1dSRodney W. Grimes  * Log writes to the log buffer, and guarantees not to sleep (so can be
188df8bae1dSRodney W. Grimes  * called by interrupt routines).  If there is no process reading the
189df8bae1dSRodney W. Grimes  * log yet, it writes to the console also.
190df8bae1dSRodney W. Grimes  */
191df8bae1dSRodney W. Grimes void
192df8bae1dSRodney W. Grimes log(int level, const char *fmt, ...)
193df8bae1dSRodney W. Grimes {
194df8bae1dSRodney W. Grimes 	register int s;
195df8bae1dSRodney W. Grimes 	va_list ap;
196df8bae1dSRodney W. Grimes 
197df8bae1dSRodney W. Grimes 	s = splhigh();
198df8bae1dSRodney W. Grimes 	logpri(level);
199df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
200791d77e0SPoul-Henning Kamp 
201791d77e0SPoul-Henning Kamp 	kvprintf(fmt, msglogchar, NULL, 10, ap);
202df8bae1dSRodney W. Grimes 	va_end(ap);
203791d77e0SPoul-Henning Kamp 
204791d77e0SPoul-Henning Kamp 	splx(s);
205df8bae1dSRodney W. Grimes 	if (!log_open) {
206791d77e0SPoul-Henning Kamp 		struct putchar_arg pca;
207df8bae1dSRodney W. Grimes 		va_start(ap, fmt);
208791d77e0SPoul-Henning Kamp 		pca.tty = NULL;
209791d77e0SPoul-Henning Kamp 		pca.flags = TOCONS;
210791d77e0SPoul-Henning Kamp 		kvprintf(fmt, putchar, &pca, 10, ap);
211df8bae1dSRodney W. Grimes 		va_end(ap);
212df8bae1dSRodney W. Grimes 	}
213df8bae1dSRodney W. Grimes 	logwakeup();
214df8bae1dSRodney W. Grimes }
215df8bae1dSRodney W. Grimes 
21687b6de2bSPoul-Henning Kamp static void
217df8bae1dSRodney W. Grimes logpri(level)
218df8bae1dSRodney W. Grimes 	int level;
219df8bae1dSRodney W. Grimes {
220df8bae1dSRodney W. Grimes 	register char *p;
221ad4f8dbdSArchie Cobbs 	char nbuf[MAXNBUF];
222df8bae1dSRodney W. Grimes 
223791d77e0SPoul-Henning Kamp 	msglogchar('<', NULL);
224ad4f8dbdSArchie Cobbs 	for (p = ksprintn(nbuf, (u_long)level, 10, NULL); *p;)
225791d77e0SPoul-Henning Kamp 		msglogchar(*p--, NULL);
226791d77e0SPoul-Henning Kamp 	msglogchar('>', NULL);
227df8bae1dSRodney W. Grimes }
228df8bae1dSRodney W. Grimes 
2296ddbf1e2SGary Palmer int
230df8bae1dSRodney W. Grimes addlog(const char *fmt, ...)
231df8bae1dSRodney W. Grimes {
232df8bae1dSRodney W. Grimes 	register int s;
233df8bae1dSRodney W. Grimes 	va_list ap;
2346ddbf1e2SGary Palmer 	int retval;
235df8bae1dSRodney W. Grimes 
236df8bae1dSRodney W. Grimes 	s = splhigh();
237df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
2386ddbf1e2SGary Palmer 	retval = kvprintf(fmt, msglogchar, NULL, 10, ap);
239df8bae1dSRodney W. Grimes 	splx(s);
240df8bae1dSRodney W. Grimes 	va_end(ap);
241df8bae1dSRodney W. Grimes 	if (!log_open) {
242791d77e0SPoul-Henning Kamp 		struct putchar_arg pca;
243df8bae1dSRodney W. Grimes 		va_start(ap, fmt);
244791d77e0SPoul-Henning Kamp 		pca.tty = NULL;
245791d77e0SPoul-Henning Kamp 		pca.flags = TOCONS;
246791d77e0SPoul-Henning Kamp 		kvprintf(fmt, putchar, &pca, 10, ap);
247df8bae1dSRodney W. Grimes 		va_end(ap);
248df8bae1dSRodney W. Grimes 	}
249df8bae1dSRodney W. Grimes 	logwakeup();
2506ddbf1e2SGary Palmer 	return (retval);
251df8bae1dSRodney W. Grimes }
252df8bae1dSRodney W. Grimes 
25365ed8cbdSJustin T. Gibbs int
254df8bae1dSRodney W. Grimes printf(const char *fmt, ...)
255df8bae1dSRodney W. Grimes {
256df8bae1dSRodney W. Grimes 	va_list ap;
257df8bae1dSRodney W. Grimes 	register int savintr;
258791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
25965ed8cbdSJustin T. Gibbs 	int retval;
260df8bae1dSRodney W. Grimes 
261df8bae1dSRodney W. Grimes 	savintr = consintr;		/* disable interrupts */
262df8bae1dSRodney W. Grimes 	consintr = 0;
263df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
264791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
265791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
26665ed8cbdSJustin T. Gibbs 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
267df8bae1dSRodney W. Grimes 	va_end(ap);
268df8bae1dSRodney W. Grimes 	if (!panicstr)
269df8bae1dSRodney W. Grimes 		logwakeup();
270df8bae1dSRodney W. Grimes 	consintr = savintr;		/* reenable interrupts */
27165ed8cbdSJustin T. Gibbs 	return retval;
272df8bae1dSRodney W. Grimes }
273df8bae1dSRodney W. Grimes 
274791d77e0SPoul-Henning Kamp void
275791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap)
276791d77e0SPoul-Henning Kamp {
277791d77e0SPoul-Henning Kamp 	register int savintr;
278791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
279791d77e0SPoul-Henning Kamp 
280791d77e0SPoul-Henning Kamp 	savintr = consintr;		/* disable interrupts */
281791d77e0SPoul-Henning Kamp 	consintr = 0;
282791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
283791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
284791d77e0SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
285791d77e0SPoul-Henning Kamp 	if (!panicstr)
286791d77e0SPoul-Henning Kamp 		logwakeup();
287791d77e0SPoul-Henning Kamp 	consintr = savintr;		/* reenable interrupts */
288791d77e0SPoul-Henning Kamp }
289791d77e0SPoul-Henning Kamp 
290791d77e0SPoul-Henning Kamp /*
291791d77e0SPoul-Henning Kamp  * Print a character on console or users terminal.  If destination is
292e796e00dSPoul-Henning Kamp  * the console then the last bunch of characters are saved in msgbuf for
293791d77e0SPoul-Henning Kamp  * inspection later.
294791d77e0SPoul-Henning Kamp  */
295791d77e0SPoul-Henning Kamp static void
296791d77e0SPoul-Henning Kamp putchar(int c, void *arg)
297791d77e0SPoul-Henning Kamp {
298791d77e0SPoul-Henning Kamp 	struct putchar_arg *ap = (struct putchar_arg*) arg;
299791d77e0SPoul-Henning Kamp 	int flags = ap->flags;
300791d77e0SPoul-Henning Kamp 	struct tty *tp = ap->tty;
301791d77e0SPoul-Henning Kamp 	if (panicstr)
302791d77e0SPoul-Henning Kamp 		constty = NULL;
303791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && tp == NULL && constty) {
304791d77e0SPoul-Henning Kamp 		tp = constty;
305791d77e0SPoul-Henning Kamp 		flags |= TOTTY;
306791d77e0SPoul-Henning Kamp 	}
307791d77e0SPoul-Henning Kamp 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
308791d77e0SPoul-Henning Kamp 	    (flags & TOCONS) && tp == constty)
309791d77e0SPoul-Henning Kamp 		constty = NULL;
310791d77e0SPoul-Henning Kamp 	if ((flags & TOLOG))
311791d77e0SPoul-Henning Kamp 		msglogchar(c, NULL);
312791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && constty == NULL && c != '\0')
313791d77e0SPoul-Henning Kamp 		(*v_putc)(c);
314791d77e0SPoul-Henning Kamp }
315791d77e0SPoul-Henning Kamp 
316791d77e0SPoul-Henning Kamp /*
317791d77e0SPoul-Henning Kamp  * Scaled down version of sprintf(3).
318791d77e0SPoul-Henning Kamp  */
319791d77e0SPoul-Henning Kamp int
320791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...)
321791d77e0SPoul-Henning Kamp {
322791d77e0SPoul-Henning Kamp 	int retval;
323791d77e0SPoul-Henning Kamp 	va_list ap;
324791d77e0SPoul-Henning Kamp 
325791d77e0SPoul-Henning Kamp 	va_start(ap, cfmt);
326791d77e0SPoul-Henning Kamp 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
327fe96d47dSPoul-Henning Kamp 	buf[retval] = '\0';
328791d77e0SPoul-Henning Kamp 	va_end(ap);
329791d77e0SPoul-Henning Kamp 	return retval;
330791d77e0SPoul-Henning Kamp }
331791d77e0SPoul-Henning Kamp 
332791d77e0SPoul-Henning Kamp /*
33399237364SAndrey A. Chernov  * Scaled down version of vsprintf(3).
33499237364SAndrey A. Chernov  */
33599237364SAndrey A. Chernov int
33699237364SAndrey A. Chernov vsprintf(char *buf, const char *cfmt, va_list ap)
33799237364SAndrey A. Chernov {
33899237364SAndrey A. Chernov 	int retval;
33999237364SAndrey A. Chernov 
34099237364SAndrey A. Chernov 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
34199237364SAndrey A. Chernov 	buf[retval] = '\0';
34299237364SAndrey A. Chernov 	return retval;
34399237364SAndrey A. Chernov }
34499237364SAndrey A. Chernov 
34599237364SAndrey A. Chernov /*
3468245f3f5SArchie Cobbs  * Scaled down version of snprintf(3).
3478245f3f5SArchie Cobbs  */
3488245f3f5SArchie Cobbs int
3498245f3f5SArchie Cobbs snprintf(char *str, size_t size, const char *format, ...)
3508245f3f5SArchie Cobbs {
3518245f3f5SArchie Cobbs 	int retval;
3528245f3f5SArchie Cobbs 	va_list ap;
3538245f3f5SArchie Cobbs 
3548245f3f5SArchie Cobbs 	va_start(ap, format);
3558245f3f5SArchie Cobbs 	retval = vsnprintf(str, size, format, ap);
3568245f3f5SArchie Cobbs 	va_end(ap);
3578245f3f5SArchie Cobbs 	return(retval);
3588245f3f5SArchie Cobbs }
3598245f3f5SArchie Cobbs 
3608245f3f5SArchie Cobbs /*
3618245f3f5SArchie Cobbs  * Scaled down version of vsnprintf(3).
3628245f3f5SArchie Cobbs  */
3638245f3f5SArchie Cobbs int
3648245f3f5SArchie Cobbs vsnprintf(char *str, size_t size, const char *format, va_list ap)
3658245f3f5SArchie Cobbs {
3668245f3f5SArchie Cobbs 	struct snprintf_arg info;
3678245f3f5SArchie Cobbs 	int retval;
3688245f3f5SArchie Cobbs 
3698245f3f5SArchie Cobbs 	info.str = str;
3708245f3f5SArchie Cobbs 	info.remain = size;
3718245f3f5SArchie Cobbs 	retval = kvprintf(format, snprintf_func, &info, 10, ap);
3728245f3f5SArchie Cobbs 	if (info.remain >= 1)
3738245f3f5SArchie Cobbs 		*info.str++ = '\0';
3748245f3f5SArchie Cobbs 	return retval;
3758245f3f5SArchie Cobbs }
3768245f3f5SArchie Cobbs 
3778245f3f5SArchie Cobbs static void
3788245f3f5SArchie Cobbs snprintf_func(int ch, void *arg)
3798245f3f5SArchie Cobbs {
3808245f3f5SArchie Cobbs 	struct snprintf_arg *const info = arg;
3818245f3f5SArchie Cobbs 
3828245f3f5SArchie Cobbs 	if (info->remain >= 2) {
3838245f3f5SArchie Cobbs 		*info->str++ = ch;
3848245f3f5SArchie Cobbs 		info->remain--;
3858245f3f5SArchie Cobbs 	}
3868245f3f5SArchie Cobbs }
3878245f3f5SArchie Cobbs 
3888245f3f5SArchie Cobbs /*
389791d77e0SPoul-Henning Kamp  * Put a number (base <= 16) in a buffer in reverse order; return an
390791d77e0SPoul-Henning Kamp  * optional length and a pointer to the NULL terminated (preceded?)
391ad4f8dbdSArchie Cobbs  * buffer. The buffer pointed to by "buf" must have length >= MAXNBUF.
392791d77e0SPoul-Henning Kamp  */
393791d77e0SPoul-Henning Kamp static char *
394ad4f8dbdSArchie Cobbs ksprintn(buf, ul, base, lenp)
395ad4f8dbdSArchie Cobbs 	char *buf;
396791d77e0SPoul-Henning Kamp 	register u_long ul;
397791d77e0SPoul-Henning Kamp 	register int base, *lenp;
398791d77e0SPoul-Henning Kamp {					/* A long in base 8, plus NULL. */
399791d77e0SPoul-Henning Kamp 	register char *p;
400791d77e0SPoul-Henning Kamp 
401791d77e0SPoul-Henning Kamp 	p = buf;
402ad4f8dbdSArchie Cobbs 	*p = '\0';
403791d77e0SPoul-Henning Kamp 	do {
404791d77e0SPoul-Henning Kamp 		*++p = hex2ascii(ul % base);
405791d77e0SPoul-Henning Kamp 	} while (ul /= base);
406791d77e0SPoul-Henning Kamp 	if (lenp)
407791d77e0SPoul-Henning Kamp 		*lenp = p - buf;
408791d77e0SPoul-Henning Kamp 	return (p);
409791d77e0SPoul-Henning Kamp }
410791d77e0SPoul-Henning Kamp 
411df8bae1dSRodney W. Grimes /*
412df8bae1dSRodney W. Grimes  * Scaled down version of printf(3).
413df8bae1dSRodney W. Grimes  *
414df8bae1dSRodney W. Grimes  * Two additional formats:
415df8bae1dSRodney W. Grimes  *
416df8bae1dSRodney W. Grimes  * The format %b is supported to decode error registers.
417df8bae1dSRodney W. Grimes  * Its usage is:
418df8bae1dSRodney W. Grimes  *
419df8bae1dSRodney W. Grimes  *	printf("reg=%b\n", regval, "<base><arg>*");
420df8bae1dSRodney W. Grimes  *
421df8bae1dSRodney W. Grimes  * where <base> is the output base expressed as a control character, e.g.
422df8bae1dSRodney W. Grimes  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
423df8bae1dSRodney W. Grimes  * the first of which gives the bit number to be inspected (origin 1), and
424df8bae1dSRodney W. Grimes  * the next characters (up to a control character, i.e. a character <= 32),
425df8bae1dSRodney W. Grimes  * give the name of the register.  Thus:
426df8bae1dSRodney W. Grimes  *
4274e31a37bSGary Palmer  *	kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
428df8bae1dSRodney W. Grimes  *
429df8bae1dSRodney W. Grimes  * would produce output:
430df8bae1dSRodney W. Grimes  *
431df8bae1dSRodney W. Grimes  *	reg=3<BITTWO,BITONE>
432df8bae1dSRodney W. Grimes  *
433120f0783SPoul-Henning Kamp  * XXX:  %D  -- Hexdump, takes pointer and separator string:
434120f0783SPoul-Henning Kamp  *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
435120f0783SPoul-Henning Kamp  *		("%*D", len, ptr, " " -> XX XX XX XX ...
436df8bae1dSRodney W. Grimes  */
437791d77e0SPoul-Henning Kamp int
438791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
439df8bae1dSRodney W. Grimes {
440791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
441ad4f8dbdSArchie Cobbs 	char nbuf[MAXNBUF];
442791d77e0SPoul-Henning Kamp 	char *p, *q, *d;
443120f0783SPoul-Henning Kamp 	u_char *up;
444791d77e0SPoul-Henning Kamp 	int ch, n;
445df8bae1dSRodney W. Grimes 	u_long ul;
4464830092aSPoul-Henning Kamp 	int base, lflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
4474f20e4b1SPoul-Henning Kamp 	int dwidth;
448df8bae1dSRodney W. Grimes 	char padc;
449791d77e0SPoul-Henning Kamp 	int retval = 0;
450791d77e0SPoul-Henning Kamp 
451fe96d47dSPoul-Henning Kamp 	if (!func)
452791d77e0SPoul-Henning Kamp 		d = (char *) arg;
453791d77e0SPoul-Henning Kamp 	else
454fe96d47dSPoul-Henning Kamp 		d = NULL;
4558f5067baSDavid Greenman 
4568f5067baSDavid Greenman 	if (fmt == NULL)
4572336b9d7SBruce Evans 		fmt = "(fmt null)\n";
458b4b2f81eSPoul-Henning Kamp 
459120f0783SPoul-Henning Kamp 	if (radix < 2 || radix > 36)
460b4b2f81eSPoul-Henning Kamp 		radix = 10;
461b4b2f81eSPoul-Henning Kamp 
462df8bae1dSRodney W. Grimes 	for (;;) {
463df8bae1dSRodney W. Grimes 		padc = ' ';
464df8bae1dSRodney W. Grimes 		width = 0;
465e0c95ed9SBruce Evans 		while ((ch = (u_char)*fmt++) != '%') {
466df8bae1dSRodney W. Grimes 			if (ch == '\0')
467791d77e0SPoul-Henning Kamp 				return retval;
468791d77e0SPoul-Henning Kamp 			PCHAR(ch);
469df8bae1dSRodney W. Grimes 		}
4704f20e4b1SPoul-Henning Kamp 		lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
4714f20e4b1SPoul-Henning Kamp 		sign = 0; dot = 0; dwidth = 0;
472e0c95ed9SBruce Evans reswitch:	switch (ch = (u_char)*fmt++) {
4734830092aSPoul-Henning Kamp 		case '.':
4744830092aSPoul-Henning Kamp 			dot = 1;
4754830092aSPoul-Henning Kamp 			goto reswitch;
476791d77e0SPoul-Henning Kamp 		case '#':
477791d77e0SPoul-Henning Kamp 			sharpflag = 1;
478791d77e0SPoul-Henning Kamp 			goto reswitch;
479791d77e0SPoul-Henning Kamp 		case '+':
480791d77e0SPoul-Henning Kamp 			sign = 1;
481791d77e0SPoul-Henning Kamp 			goto reswitch;
482791d77e0SPoul-Henning Kamp 		case '-':
483791d77e0SPoul-Henning Kamp 			ladjust = 1;
484791d77e0SPoul-Henning Kamp 			goto reswitch;
485791d77e0SPoul-Henning Kamp 		case '%':
486791d77e0SPoul-Henning Kamp 			PCHAR(ch);
487791d77e0SPoul-Henning Kamp 			break;
488791d77e0SPoul-Henning Kamp 		case '*':
489ed71c342SPoul-Henning Kamp 			if (!dot) {
490791d77e0SPoul-Henning Kamp 				width = va_arg(ap, int);
491791d77e0SPoul-Henning Kamp 				if (width < 0) {
492791d77e0SPoul-Henning Kamp 					ladjust = !ladjust;
493791d77e0SPoul-Henning Kamp 					width = -width;
494791d77e0SPoul-Henning Kamp 				}
495ed71c342SPoul-Henning Kamp 			} else {
496ed71c342SPoul-Henning Kamp 				dwidth = va_arg(ap, int);
497ed71c342SPoul-Henning Kamp 			}
498791d77e0SPoul-Henning Kamp 			goto reswitch;
499df8bae1dSRodney W. Grimes 		case '0':
5004f20e4b1SPoul-Henning Kamp 			if (!dot) {
501df8bae1dSRodney W. Grimes 				padc = '0';
502df8bae1dSRodney W. Grimes 				goto reswitch;
5034f20e4b1SPoul-Henning Kamp 			}
504df8bae1dSRodney W. Grimes 		case '1': case '2': case '3': case '4':
505df8bae1dSRodney W. Grimes 		case '5': case '6': case '7': case '8': case '9':
5064f20e4b1SPoul-Henning Kamp 				for (n = 0;; ++fmt) {
5074f20e4b1SPoul-Henning Kamp 					n = n * 10 + ch - '0';
508df8bae1dSRodney W. Grimes 					ch = *fmt;
509df8bae1dSRodney W. Grimes 					if (ch < '0' || ch > '9')
510df8bae1dSRodney W. Grimes 						break;
511df8bae1dSRodney W. Grimes 				}
5124f20e4b1SPoul-Henning Kamp 			if (dot)
5134f20e4b1SPoul-Henning Kamp 				dwidth = n;
5144f20e4b1SPoul-Henning Kamp 			else
5154f20e4b1SPoul-Henning Kamp 				width = n;
516df8bae1dSRodney W. Grimes 			goto reswitch;
517df8bae1dSRodney W. Grimes 		case 'b':
518df8bae1dSRodney W. Grimes 			ul = va_arg(ap, int);
519df8bae1dSRodney W. Grimes 			p = va_arg(ap, char *);
520ad4f8dbdSArchie Cobbs 			for (q = ksprintn(nbuf, ul, *p++, NULL); *q;)
521791d77e0SPoul-Henning Kamp 				PCHAR(*q--);
522df8bae1dSRodney W. Grimes 
523df8bae1dSRodney W. Grimes 			if (!ul)
524df8bae1dSRodney W. Grimes 				break;
525df8bae1dSRodney W. Grimes 
526797f2d22SPoul-Henning Kamp 			for (tmp = 0; *p;) {
527797f2d22SPoul-Henning Kamp 				n = *p++;
528df8bae1dSRodney W. Grimes 				if (ul & (1 << (n - 1))) {
529791d77e0SPoul-Henning Kamp 					PCHAR(tmp ? ',' : '<');
530df8bae1dSRodney W. Grimes 					for (; (n = *p) > ' '; ++p)
531791d77e0SPoul-Henning Kamp 						PCHAR(n);
532df8bae1dSRodney W. Grimes 					tmp = 1;
533df8bae1dSRodney W. Grimes 				} else
534df8bae1dSRodney W. Grimes 					for (; *p > ' '; ++p)
535df8bae1dSRodney W. Grimes 						continue;
536df8bae1dSRodney W. Grimes 			}
537df8bae1dSRodney W. Grimes 			if (tmp)
538791d77e0SPoul-Henning Kamp 				PCHAR('>');
539df8bae1dSRodney W. Grimes 			break;
540df8bae1dSRodney W. Grimes 		case 'c':
541791d77e0SPoul-Henning Kamp 			PCHAR(va_arg(ap, int));
542df8bae1dSRodney W. Grimes 			break;
543120f0783SPoul-Henning Kamp 		case 'D':
544120f0783SPoul-Henning Kamp 			up = va_arg(ap, u_char *);
545120f0783SPoul-Henning Kamp 			p = va_arg(ap, char *);
546120f0783SPoul-Henning Kamp 			if (!width)
547120f0783SPoul-Henning Kamp 				width = 16;
548120f0783SPoul-Henning Kamp 			while(width--) {
549120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up >> 4));
550120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up & 0x0f));
551120f0783SPoul-Henning Kamp 				up++;
552120f0783SPoul-Henning Kamp 				if (width)
553120f0783SPoul-Henning Kamp 					for (q=p;*q;q++)
554120f0783SPoul-Henning Kamp 						PCHAR(*q);
555120f0783SPoul-Henning Kamp 			}
556120f0783SPoul-Henning Kamp 			break;
557df8bae1dSRodney W. Grimes 		case 'd':
558df8bae1dSRodney W. Grimes 			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
559791d77e0SPoul-Henning Kamp 			sign = 1;
560df8bae1dSRodney W. Grimes 			base = 10;
561df8bae1dSRodney W. Grimes 			goto number;
562791d77e0SPoul-Henning Kamp 		case 'l':
563791d77e0SPoul-Henning Kamp 			lflag = 1;
564791d77e0SPoul-Henning Kamp 			goto reswitch;
565df8bae1dSRodney W. Grimes 		case 'o':
566df8bae1dSRodney W. Grimes 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
567df8bae1dSRodney W. Grimes 			base = 8;
568e0c38587SBruce Evans 			goto nosign;
56912d17f65SPoul-Henning Kamp 		case 'p':
570a23d65bfSBruce Evans 			ul = (uintptr_t)va_arg(ap, void *);
57112d17f65SPoul-Henning Kamp 			base = 16;
572c41141b0SBruce Evans 			sharpflag = (width == 0);
573e0c38587SBruce Evans 			goto nosign;
574e0c38587SBruce Evans 		case 'n':
575e0c38587SBruce Evans 		case 'r':
576e0c38587SBruce Evans 			ul = lflag ? va_arg(ap, u_long) :
577e0c38587SBruce Evans 			    sign ? (u_long)va_arg(ap, int) : va_arg(ap, u_int);
578e0c38587SBruce Evans 			base = radix;
57912d17f65SPoul-Henning Kamp 			goto number;
580791d77e0SPoul-Henning Kamp 		case 's':
581791d77e0SPoul-Henning Kamp 			p = va_arg(ap, char *);
582791d77e0SPoul-Henning Kamp 			if (p == NULL)
583791d77e0SPoul-Henning Kamp 				p = "(null)";
5844830092aSPoul-Henning Kamp 			if (!dot)
5854830092aSPoul-Henning Kamp 				n = strlen (p);
5864830092aSPoul-Henning Kamp 			else
5874f20e4b1SPoul-Henning Kamp 				for (n = 0; n < dwidth && p[n]; n++)
5884830092aSPoul-Henning Kamp 					continue;
5894f20e4b1SPoul-Henning Kamp 
5904830092aSPoul-Henning Kamp 			width -= n;
5914f20e4b1SPoul-Henning Kamp 
592791d77e0SPoul-Henning Kamp 			if (!ladjust && width > 0)
593791d77e0SPoul-Henning Kamp 				while (width--)
594791d77e0SPoul-Henning Kamp 					PCHAR(padc);
5954830092aSPoul-Henning Kamp 			while (n--)
596791d77e0SPoul-Henning Kamp 				PCHAR(*p++);
597791d77e0SPoul-Henning Kamp 			if (ladjust && width > 0)
598791d77e0SPoul-Henning Kamp 				while (width--)
599791d77e0SPoul-Henning Kamp 					PCHAR(padc);
600791d77e0SPoul-Henning Kamp 			break;
601f53dbe97SBruce Evans 		case 'u':
602f53dbe97SBruce Evans 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
603f53dbe97SBruce Evans 			base = 10;
604e0c38587SBruce Evans 			goto nosign;
605df8bae1dSRodney W. Grimes 		case 'x':
606df8bae1dSRodney W. Grimes 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
607df8bae1dSRodney W. Grimes 			base = 16;
608e0c38587SBruce Evans 			goto nosign;
609e0c38587SBruce Evans 		case 'z':
610e0c38587SBruce Evans 			ul = lflag ? va_arg(ap, u_long) :
611e0c38587SBruce Evans 			    sign ? (u_long)va_arg(ap, int) : va_arg(ap, u_int);
612e0c38587SBruce Evans 			base = 16;
613e0c38587SBruce Evans 			goto number;
614e0c38587SBruce Evans nosign:			sign = 0;
615791d77e0SPoul-Henning Kamp number:			if (sign && (long)ul < 0L) {
616791d77e0SPoul-Henning Kamp 				neg = 1;
617791d77e0SPoul-Henning Kamp 				ul = -(long)ul;
618791d77e0SPoul-Henning Kamp 			}
619ad4f8dbdSArchie Cobbs 			p = ksprintn(nbuf, ul, base, &tmp);
620791d77e0SPoul-Henning Kamp 			if (sharpflag && ul != 0) {
621791d77e0SPoul-Henning Kamp 				if (base == 8)
622791d77e0SPoul-Henning Kamp 					tmp++;
623791d77e0SPoul-Henning Kamp 				else if (base == 16)
624791d77e0SPoul-Henning Kamp 					tmp += 2;
625791d77e0SPoul-Henning Kamp 			}
626791d77e0SPoul-Henning Kamp 			if (neg)
627791d77e0SPoul-Henning Kamp 				tmp++;
628791d77e0SPoul-Henning Kamp 
629791d77e0SPoul-Henning Kamp 			if (!ladjust && width && (width -= tmp) > 0)
630df8bae1dSRodney W. Grimes 				while (width--)
631791d77e0SPoul-Henning Kamp 					PCHAR(padc);
632791d77e0SPoul-Henning Kamp 			if (neg)
633791d77e0SPoul-Henning Kamp 				PCHAR('-');
634791d77e0SPoul-Henning Kamp 			if (sharpflag && ul != 0) {
635791d77e0SPoul-Henning Kamp 				if (base == 8) {
636791d77e0SPoul-Henning Kamp 					PCHAR('0');
637791d77e0SPoul-Henning Kamp 				} else if (base == 16) {
638791d77e0SPoul-Henning Kamp 					PCHAR('0');
639791d77e0SPoul-Henning Kamp 					PCHAR('x');
640791d77e0SPoul-Henning Kamp 				}
641791d77e0SPoul-Henning Kamp 			}
642791d77e0SPoul-Henning Kamp 
643797f2d22SPoul-Henning Kamp 			while (*p)
644791d77e0SPoul-Henning Kamp 				PCHAR(*p--);
645791d77e0SPoul-Henning Kamp 
646791d77e0SPoul-Henning Kamp 			if (ladjust && width && (width -= tmp) > 0)
647791d77e0SPoul-Henning Kamp 				while (width--)
648791d77e0SPoul-Henning Kamp 					PCHAR(padc);
649791d77e0SPoul-Henning Kamp 
650df8bae1dSRodney W. Grimes 			break;
651df8bae1dSRodney W. Grimes 		default:
652791d77e0SPoul-Henning Kamp 			PCHAR('%');
653df8bae1dSRodney W. Grimes 			if (lflag)
654791d77e0SPoul-Henning Kamp 				PCHAR('l');
655791d77e0SPoul-Henning Kamp 			PCHAR(ch);
656791d77e0SPoul-Henning Kamp 			break;
657df8bae1dSRodney W. Grimes 		}
658df8bae1dSRodney W. Grimes 	}
659791d77e0SPoul-Henning Kamp #undef PCHAR
660df8bae1dSRodney W. Grimes }
661df8bae1dSRodney W. Grimes 
662df8bae1dSRodney W. Grimes /*
663791d77e0SPoul-Henning Kamp  * Put character in log buffer.
664df8bae1dSRodney W. Grimes  */
665df8bae1dSRodney W. Grimes static void
666791d77e0SPoul-Henning Kamp msglogchar(int c, void *dummyarg)
667df8bae1dSRodney W. Grimes {
668791d77e0SPoul-Henning Kamp 	struct msgbuf *mbp;
669df8bae1dSRodney W. Grimes 
670791d77e0SPoul-Henning Kamp 	if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
671df8bae1dSRodney W. Grimes 		mbp = msgbufp;
67258067a99SPoul-Henning Kamp 		mbp->msg_ptr[mbp->msg_bufx++] = c;
67358067a99SPoul-Henning Kamp 		if (mbp->msg_bufx >= mbp->msg_size)
674df8bae1dSRodney W. Grimes 			mbp->msg_bufx = 0;
67502d5c7b1SDavid Greenman 		/* If the buffer is full, keep the most recent data. */
676a3f4faceSBruce Evans 		if (mbp->msg_bufr == mbp->msg_bufx) {
67758067a99SPoul-Henning Kamp 			if (++mbp->msg_bufr >= mbp->msg_size)
678a3f4faceSBruce Evans 				mbp->msg_bufr = 0;
679a3f4faceSBruce Evans 		}
680df8bae1dSRodney W. Grimes 	}
681df8bae1dSRodney W. Grimes }
682e796e00dSPoul-Henning Kamp 
683eb9d435aSJonathan Lemon static void
684eb9d435aSJonathan Lemon msgbufcopy(struct msgbuf *oldp)
685eb9d435aSJonathan Lemon {
686eb9d435aSJonathan Lemon 	int pos;
687eb9d435aSJonathan Lemon 
688eb9d435aSJonathan Lemon 	pos = oldp->msg_bufr;
689eb9d435aSJonathan Lemon 	while (pos != oldp->msg_bufx) {
690eb9d435aSJonathan Lemon 		msglogchar(oldp->msg_ptr[pos], NULL);
691eb9d435aSJonathan Lemon 		if (++pos >= oldp->msg_size)
692eb9d435aSJonathan Lemon 			pos = 0;
693eb9d435aSJonathan Lemon 	}
694eb9d435aSJonathan Lemon }
695eb9d435aSJonathan Lemon 
696e796e00dSPoul-Henning Kamp void
697e796e00dSPoul-Henning Kamp msgbufinit(void *ptr, size_t size)
698e796e00dSPoul-Henning Kamp {
699e796e00dSPoul-Henning Kamp 	char *cp;
700eb9d435aSJonathan Lemon 	static struct msgbuf *oldp = NULL;
701e796e00dSPoul-Henning Kamp 
702e796e00dSPoul-Henning Kamp 	cp = (char *)ptr;
703e796e00dSPoul-Henning Kamp 	msgbufp = (struct msgbuf *) (cp + size - sizeof(*msgbufp));
704e796e00dSPoul-Henning Kamp 	if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_ptr != cp) {
705e796e00dSPoul-Henning Kamp 		bzero(cp, size);
706e796e00dSPoul-Henning Kamp 		msgbufp->msg_magic = MSG_MAGIC;
707e796e00dSPoul-Henning Kamp 		msgbufp->msg_size = (char *)msgbufp - cp;
708e796e00dSPoul-Henning Kamp 		msgbufp->msg_ptr = cp;
709e796e00dSPoul-Henning Kamp 	}
710eb9d435aSJonathan Lemon 	if (msgbufmapped && oldp != msgbufp)
711eb9d435aSJonathan Lemon 		msgbufcopy(oldp);
712e796e00dSPoul-Henning Kamp 	msgbufmapped = 1;
713eb9d435aSJonathan Lemon 	oldp = msgbufp;
714e796e00dSPoul-Henning Kamp }
715e796e00dSPoul-Henning Kamp 
716e796e00dSPoul-Henning Kamp #include "opt_ddb.h"
717e796e00dSPoul-Henning Kamp #ifdef DDB
718e796e00dSPoul-Henning Kamp #include <ddb/ddb.h>
719e796e00dSPoul-Henning Kamp 
720e796e00dSPoul-Henning Kamp DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
721e796e00dSPoul-Henning Kamp {
722e796e00dSPoul-Henning Kamp 	int i, j;
723e796e00dSPoul-Henning Kamp 
724e796e00dSPoul-Henning Kamp 	if (!msgbufmapped) {
725e796e00dSPoul-Henning Kamp 		db_printf("msgbuf not mapped yet\n");
726e796e00dSPoul-Henning Kamp 		return;
727e796e00dSPoul-Henning Kamp 	}
728e796e00dSPoul-Henning Kamp 	db_printf("msgbufp = %p\n", msgbufp);
729e796e00dSPoul-Henning Kamp 	db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
730e796e00dSPoul-Henning Kamp 	    msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
731e796e00dSPoul-Henning Kamp 	    msgbufp->msg_bufx, msgbufp->msg_ptr);
732e796e00dSPoul-Henning Kamp 	for (i = 0; i < msgbufp->msg_size; i++) {
733e796e00dSPoul-Henning Kamp 		j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
734e796e00dSPoul-Henning Kamp 		db_printf("%c", msgbufp->msg_ptr[j]);
735e796e00dSPoul-Henning Kamp 	}
736e796e00dSPoul-Henning Kamp 	db_printf("\n");
737e796e00dSPoul-Henning Kamp }
738e796e00dSPoul-Henning Kamp 
739e796e00dSPoul-Henning Kamp #endif /* DDB */
740