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; 98dbe620d3SDavid Malone SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW, 99dbe620d3SDavid Malone &log_console_output, 0, ""); 100dbe620d3SDavid Malone 101df8bae1dSRodney W. Grimes /* 102df8bae1dSRodney W. Grimes * Warn that a system table is full. 103df8bae1dSRodney W. Grimes */ 104df8bae1dSRodney W. Grimes void 105e2a09b26SPoul-Henning Kamp tablefull(const char *tab) 106df8bae1dSRodney W. Grimes { 107df8bae1dSRodney W. Grimes 108df8bae1dSRodney W. Grimes log(LOG_ERR, "%s: table is full\n", tab); 109df8bae1dSRodney W. Grimes } 110df8bae1dSRodney W. Grimes 111df8bae1dSRodney W. Grimes /* 112df8bae1dSRodney W. Grimes * Uprintf prints to the controlling terminal for the current process. 113df8bae1dSRodney W. Grimes * It may block if the tty queue is overfull. No message is printed if 114df8bae1dSRodney W. Grimes * the queue does not clear in a reasonable time. 115df8bae1dSRodney W. Grimes */ 116f1550d9dSDoug Rabson int 117df8bae1dSRodney W. Grimes uprintf(const char *fmt, ...) 118df8bae1dSRodney W. Grimes { 119b40ce416SJulian Elischer struct thread *td = curthread; 120b40ce416SJulian Elischer struct proc *p = td->td_proc; 121df8bae1dSRodney W. Grimes va_list ap; 122791d77e0SPoul-Henning Kamp struct putchar_arg pca; 123f591779bSSeigo Tanimura int retval; 124df8bae1dSRodney W. Grimes 125f591779bSSeigo Tanimura if (td == NULL || td == PCPU_GET(idlethread)) 126f591779bSSeigo Tanimura return (0); 127f591779bSSeigo Tanimura 128f591779bSSeigo Tanimura p = td->td_proc; 129f591779bSSeigo Tanimura PROC_LOCK(p); 130f591779bSSeigo Tanimura if ((p->p_flag & P_CONTROLT) == 0) { 131f591779bSSeigo Tanimura PROC_UNLOCK(p); 132f591779bSSeigo Tanimura return (0); 133f591779bSSeigo Tanimura } 134f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 135791d77e0SPoul-Henning Kamp pca.tty = p->p_session->s_ttyp; 136f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 137f591779bSSeigo Tanimura PROC_UNLOCK(p); 138f591779bSSeigo Tanimura if (pca.tty == NULL) 139f591779bSSeigo Tanimura return (0); 140791d77e0SPoul-Henning Kamp pca.flags = TOTTY; 141f591779bSSeigo Tanimura va_start(ap, fmt); 142f1550d9dSDoug Rabson retval = kvprintf(fmt, putchar, &pca, 10, ap); 143df8bae1dSRodney W. Grimes va_end(ap); 144f591779bSSeigo Tanimura 14558a24f79SPoul-Henning Kamp return (retval); 146df8bae1dSRodney W. Grimes } 147df8bae1dSRodney W. Grimes 148df8bae1dSRodney W. Grimes /* 149df8bae1dSRodney W. Grimes * tprintf prints on the controlling terminal associated 150a52585d7SPoul-Henning Kamp * with the given session, possibly to the log as well. 151df8bae1dSRodney W. Grimes */ 152a52585d7SPoul-Henning Kamp void 153a52585d7SPoul-Henning Kamp tprintf(struct proc *p, int pri, const char *fmt, ...) 154df8bae1dSRodney W. Grimes { 155df8bae1dSRodney W. Grimes struct tty *tp = NULL; 156a52585d7SPoul-Henning Kamp int flags = 0, shld = 0; 157df8bae1dSRodney W. Grimes va_list ap; 158791d77e0SPoul-Henning Kamp struct putchar_arg pca; 159f1550d9dSDoug Rabson int retval; 160df8bae1dSRodney W. Grimes 161e2a09b26SPoul-Henning Kamp if (pri != -1) 162a52585d7SPoul-Henning Kamp flags |= TOLOG; 163f591779bSSeigo Tanimura if (p != NULL) { 164f591779bSSeigo Tanimura PROC_LOCK(p); 165f591779bSSeigo Tanimura if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 166f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 167a52585d7SPoul-Henning Kamp SESSHOLD(p->p_session); 168a52585d7SPoul-Henning Kamp tp = p->p_session->s_ttyp; 169f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 170f591779bSSeigo Tanimura PROC_UNLOCK(p); 171f591779bSSeigo Tanimura shld++; 172f591779bSSeigo Tanimura if (ttycheckoutq(tp, 0)) 173f591779bSSeigo Tanimura flags |= TOTTY; 174f591779bSSeigo Tanimura else 175f591779bSSeigo Tanimura tp = NULL; 176f591779bSSeigo Tanimura } else 177f591779bSSeigo Tanimura PROC_UNLOCK(p); 178a52585d7SPoul-Henning Kamp } 179e2a09b26SPoul-Henning Kamp pca.pri = pri; 180791d77e0SPoul-Henning Kamp pca.tty = tp; 181791d77e0SPoul-Henning Kamp pca.flags = flags; 182a52585d7SPoul-Henning Kamp va_start(ap, fmt); 183f1550d9dSDoug Rabson retval = kvprintf(fmt, putchar, &pca, 10, ap); 184df8bae1dSRodney W. Grimes va_end(ap); 185f591779bSSeigo Tanimura if (shld) { 186183ccde6SSeigo Tanimura PROC_LOCK(p); 187f591779bSSeigo Tanimura SESS_LOCK(p->p_session); 188a52585d7SPoul-Henning Kamp SESSRELE(p->p_session); 189f591779bSSeigo Tanimura SESS_UNLOCK(p->p_session); 190183ccde6SSeigo Tanimura PROC_UNLOCK(p); 191f591779bSSeigo Tanimura } 192e2a09b26SPoul-Henning Kamp msgbuftrigger = 1; 193df8bae1dSRodney W. Grimes } 194df8bae1dSRodney W. Grimes 195df8bae1dSRodney W. Grimes /* 196df8bae1dSRodney W. Grimes * Ttyprintf displays a message on a tty; it should be used only by 197df8bae1dSRodney W. Grimes * the tty driver, or anything that knows the underlying tty will not 198df8bae1dSRodney W. Grimes * be revoke(2)'d away. Other callers should use tprintf. 199df8bae1dSRodney W. Grimes */ 200f1550d9dSDoug Rabson int 201df8bae1dSRodney W. Grimes ttyprintf(struct tty *tp, const char *fmt, ...) 202df8bae1dSRodney W. Grimes { 203df8bae1dSRodney W. Grimes va_list ap; 204791d77e0SPoul-Henning Kamp struct putchar_arg pca; 205f1550d9dSDoug Rabson int retval; 206f1550d9dSDoug Rabson 207df8bae1dSRodney W. Grimes va_start(ap, fmt); 208791d77e0SPoul-Henning Kamp pca.tty = tp; 209791d77e0SPoul-Henning Kamp pca.flags = TOTTY; 210f1550d9dSDoug Rabson retval = kvprintf(fmt, putchar, &pca, 10, ap); 211df8bae1dSRodney W. Grimes va_end(ap); 21258a24f79SPoul-Henning Kamp return (retval); 213df8bae1dSRodney W. Grimes } 214df8bae1dSRodney W. Grimes 215df8bae1dSRodney W. Grimes /* 216df8bae1dSRodney W. Grimes * Log writes to the log buffer, and guarantees not to sleep (so can be 217df8bae1dSRodney W. Grimes * called by interrupt routines). If there is no process reading the 218df8bae1dSRodney W. Grimes * log yet, it writes to the console also. 219df8bae1dSRodney W. Grimes */ 2204d88c459SPoul-Henning Kamp void 221df8bae1dSRodney W. Grimes log(int level, const char *fmt, ...) 222df8bae1dSRodney W. Grimes { 223df8bae1dSRodney W. Grimes va_list ap; 224f1550d9dSDoug Rabson int retval; 225791d77e0SPoul-Henning Kamp struct putchar_arg pca; 226e2a09b26SPoul-Henning Kamp 227791d77e0SPoul-Henning Kamp pca.tty = NULL; 228e2a09b26SPoul-Henning Kamp pca.pri = level; 229e2a09b26SPoul-Henning Kamp pca.flags = log_open ? TOLOG : TOCONS; 230e2a09b26SPoul-Henning Kamp 231e2a09b26SPoul-Henning Kamp va_start(ap, fmt); 232e2a09b26SPoul-Henning Kamp retval = kvprintf(fmt, putchar, &pca, 10, ap); 233df8bae1dSRodney W. Grimes va_end(ap); 234e2a09b26SPoul-Henning Kamp 235b80e3b41SPoul-Henning Kamp msgbuftrigger = 1; 236df8bae1dSRodney W. Grimes } 237df8bae1dSRodney W. Grimes 238e2a09b26SPoul-Henning Kamp #define CONSCHUNK 128 239e2a09b26SPoul-Henning Kamp 240e2a09b26SPoul-Henning Kamp void 241e2a09b26SPoul-Henning Kamp log_console(struct uio *uio) 242df8bae1dSRodney W. Grimes { 243e2a09b26SPoul-Henning Kamp int c, i, error, iovlen, nl; 244e2a09b26SPoul-Henning Kamp struct uio muio; 245e2a09b26SPoul-Henning Kamp struct iovec *miov = NULL; 246e2a09b26SPoul-Henning Kamp char *consbuffer; 247e2a09b26SPoul-Henning Kamp int pri; 248df8bae1dSRodney W. Grimes 249dbe620d3SDavid Malone if (!log_console_output) 250dbe620d3SDavid Malone return; 251dbe620d3SDavid Malone 252e2a09b26SPoul-Henning Kamp pri = LOG_INFO | LOG_CONSOLE; 253e2a09b26SPoul-Henning Kamp muio = *uio; 254e2a09b26SPoul-Henning Kamp iovlen = uio->uio_iovcnt * sizeof (struct iovec); 255e2a09b26SPoul-Henning Kamp MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK); 256e2a09b26SPoul-Henning Kamp MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK); 25780208239SAlfred Perlstein bcopy(muio.uio_iov, miov, iovlen); 258e2a09b26SPoul-Henning Kamp muio.uio_iov = miov; 259e2a09b26SPoul-Henning Kamp uio = &muio; 260e2a09b26SPoul-Henning Kamp 261e2a09b26SPoul-Henning Kamp nl = 0; 262e2a09b26SPoul-Henning Kamp while (uio->uio_resid > 0) { 263e2a09b26SPoul-Henning Kamp c = imin(uio->uio_resid, CONSCHUNK); 264e2a09b26SPoul-Henning Kamp error = uiomove(consbuffer, c, uio); 265e2a09b26SPoul-Henning Kamp if (error != 0) 266e2a09b26SPoul-Henning Kamp return; 267e2a09b26SPoul-Henning Kamp for (i = 0; i < c; i++) { 268e2a09b26SPoul-Henning Kamp msglogchar(consbuffer[i], pri); 269e2a09b26SPoul-Henning Kamp if (consbuffer[i] == '\n') 270e2a09b26SPoul-Henning Kamp nl = 1; 271e2a09b26SPoul-Henning Kamp else 272e2a09b26SPoul-Henning Kamp nl = 0; 273e2a09b26SPoul-Henning Kamp } 274e2a09b26SPoul-Henning Kamp } 275e2a09b26SPoul-Henning Kamp if (!nl) 276e2a09b26SPoul-Henning Kamp msglogchar('\n', pri); 277b80e3b41SPoul-Henning Kamp msgbuftrigger = 1; 278e2a09b26SPoul-Henning Kamp FREE(miov, M_TEMP); 279e2a09b26SPoul-Henning Kamp FREE(consbuffer, M_TEMP); 280e2a09b26SPoul-Henning Kamp return; 281df8bae1dSRodney W. Grimes } 282df8bae1dSRodney W. Grimes 2836ddbf1e2SGary Palmer int 284df8bae1dSRodney W. Grimes printf(const char *fmt, ...) 285df8bae1dSRodney W. Grimes { 286df8bae1dSRodney W. Grimes va_list ap; 2874d88c459SPoul-Henning Kamp int savintr; 288791d77e0SPoul-Henning Kamp struct putchar_arg pca; 28965ed8cbdSJustin T. Gibbs int retval; 290df8bae1dSRodney W. Grimes 291df8bae1dSRodney W. Grimes savintr = consintr; /* disable interrupts */ 292df8bae1dSRodney W. Grimes consintr = 0; 293df8bae1dSRodney W. Grimes va_start(ap, fmt); 294791d77e0SPoul-Henning Kamp pca.tty = NULL; 295791d77e0SPoul-Henning Kamp pca.flags = TOCONS | TOLOG; 296e2a09b26SPoul-Henning Kamp pca.pri = -1; 29765ed8cbdSJustin T. Gibbs retval = kvprintf(fmt, putchar, &pca, 10, ap); 298df8bae1dSRodney W. Grimes va_end(ap); 299df8bae1dSRodney W. Grimes if (!panicstr) 300b80e3b41SPoul-Henning Kamp msgbuftrigger = 1; 301df8bae1dSRodney W. Grimes consintr = savintr; /* reenable interrupts */ 30258a24f79SPoul-Henning Kamp return (retval); 303df8bae1dSRodney W. Grimes } 304df8bae1dSRodney W. Grimes 305f1550d9dSDoug Rabson int 306791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap) 307791d77e0SPoul-Henning Kamp { 3084d88c459SPoul-Henning Kamp int savintr; 309791d77e0SPoul-Henning Kamp struct putchar_arg pca; 310f1550d9dSDoug Rabson int retval; 311791d77e0SPoul-Henning Kamp 312791d77e0SPoul-Henning Kamp savintr = consintr; /* disable interrupts */ 313791d77e0SPoul-Henning Kamp consintr = 0; 314791d77e0SPoul-Henning Kamp pca.tty = NULL; 315791d77e0SPoul-Henning Kamp pca.flags = TOCONS | TOLOG; 316e2a09b26SPoul-Henning Kamp pca.pri = -1; 317f1550d9dSDoug Rabson retval = kvprintf(fmt, putchar, &pca, 10, ap); 318791d77e0SPoul-Henning Kamp if (!panicstr) 319b80e3b41SPoul-Henning Kamp msgbuftrigger = 1; 320791d77e0SPoul-Henning Kamp consintr = savintr; /* reenable interrupts */ 32158a24f79SPoul-Henning Kamp return (retval); 322791d77e0SPoul-Henning Kamp } 323791d77e0SPoul-Henning Kamp 324791d77e0SPoul-Henning Kamp /* 325791d77e0SPoul-Henning Kamp * Print a character on console or users terminal. If destination is 326e796e00dSPoul-Henning Kamp * the console then the last bunch of characters are saved in msgbuf for 327791d77e0SPoul-Henning Kamp * inspection later. 328791d77e0SPoul-Henning Kamp */ 329791d77e0SPoul-Henning Kamp static void 330791d77e0SPoul-Henning Kamp putchar(int c, void *arg) 331791d77e0SPoul-Henning Kamp { 332791d77e0SPoul-Henning Kamp struct putchar_arg *ap = (struct putchar_arg*) arg; 333791d77e0SPoul-Henning Kamp int flags = ap->flags; 334791d77e0SPoul-Henning Kamp struct tty *tp = ap->tty; 335791d77e0SPoul-Henning Kamp if (panicstr) 336791d77e0SPoul-Henning Kamp constty = NULL; 337791d77e0SPoul-Henning Kamp if ((flags & TOCONS) && tp == NULL && constty) { 338791d77e0SPoul-Henning Kamp tp = constty; 339791d77e0SPoul-Henning Kamp flags |= TOTTY; 340791d77e0SPoul-Henning Kamp } 341791d77e0SPoul-Henning Kamp if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 && 342791d77e0SPoul-Henning Kamp (flags & TOCONS) && tp == constty) 343791d77e0SPoul-Henning Kamp constty = NULL; 344791d77e0SPoul-Henning Kamp if ((flags & TOLOG)) 345e2a09b26SPoul-Henning Kamp msglogchar(c, ap->pri); 346791d77e0SPoul-Henning Kamp if ((flags & TOCONS) && constty == NULL && c != '\0') 347791d77e0SPoul-Henning Kamp (*v_putc)(c); 348791d77e0SPoul-Henning Kamp } 349791d77e0SPoul-Henning Kamp 350791d77e0SPoul-Henning Kamp /* 351791d77e0SPoul-Henning Kamp * Scaled down version of sprintf(3). 352791d77e0SPoul-Henning Kamp */ 353791d77e0SPoul-Henning Kamp int 354791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...) 355791d77e0SPoul-Henning Kamp { 356791d77e0SPoul-Henning Kamp int retval; 357791d77e0SPoul-Henning Kamp va_list ap; 358791d77e0SPoul-Henning Kamp 359791d77e0SPoul-Henning Kamp va_start(ap, cfmt); 360791d77e0SPoul-Henning Kamp retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 361fe96d47dSPoul-Henning Kamp buf[retval] = '\0'; 362791d77e0SPoul-Henning Kamp va_end(ap); 36358a24f79SPoul-Henning Kamp return (retval); 364791d77e0SPoul-Henning Kamp } 365791d77e0SPoul-Henning Kamp 366791d77e0SPoul-Henning Kamp /* 36799237364SAndrey A. Chernov * Scaled down version of vsprintf(3). 36899237364SAndrey A. Chernov */ 36999237364SAndrey A. Chernov int 37099237364SAndrey A. Chernov vsprintf(char *buf, const char *cfmt, va_list ap) 37199237364SAndrey A. Chernov { 37299237364SAndrey A. Chernov int retval; 37399237364SAndrey A. Chernov 37499237364SAndrey A. Chernov retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 37599237364SAndrey A. Chernov buf[retval] = '\0'; 37658a24f79SPoul-Henning Kamp return (retval); 37799237364SAndrey A. Chernov } 37899237364SAndrey A. Chernov 37999237364SAndrey A. Chernov /* 3808245f3f5SArchie Cobbs * Scaled down version of snprintf(3). 3818245f3f5SArchie Cobbs */ 3828245f3f5SArchie Cobbs int 3838245f3f5SArchie Cobbs snprintf(char *str, size_t size, const char *format, ...) 3848245f3f5SArchie Cobbs { 3858245f3f5SArchie Cobbs int retval; 3868245f3f5SArchie Cobbs va_list ap; 3878245f3f5SArchie Cobbs 3888245f3f5SArchie Cobbs va_start(ap, format); 3898245f3f5SArchie Cobbs retval = vsnprintf(str, size, format, ap); 3908245f3f5SArchie Cobbs va_end(ap); 3918245f3f5SArchie Cobbs return(retval); 3928245f3f5SArchie Cobbs } 3938245f3f5SArchie Cobbs 3948245f3f5SArchie Cobbs /* 3958245f3f5SArchie Cobbs * Scaled down version of vsnprintf(3). 3968245f3f5SArchie Cobbs */ 3978245f3f5SArchie Cobbs int 3988245f3f5SArchie Cobbs vsnprintf(char *str, size_t size, const char *format, va_list ap) 3998245f3f5SArchie Cobbs { 4008245f3f5SArchie Cobbs struct snprintf_arg info; 4018245f3f5SArchie Cobbs int retval; 4028245f3f5SArchie Cobbs 4038245f3f5SArchie Cobbs info.str = str; 4048245f3f5SArchie Cobbs info.remain = size; 4058245f3f5SArchie Cobbs retval = kvprintf(format, snprintf_func, &info, 10, ap); 4068245f3f5SArchie Cobbs if (info.remain >= 1) 4078245f3f5SArchie Cobbs *info.str++ = '\0'; 40858a24f79SPoul-Henning Kamp return (retval); 4098245f3f5SArchie Cobbs } 4108245f3f5SArchie Cobbs 4118245f3f5SArchie Cobbs static void 4128245f3f5SArchie Cobbs snprintf_func(int ch, void *arg) 4138245f3f5SArchie Cobbs { 4148245f3f5SArchie Cobbs struct snprintf_arg *const info = arg; 4158245f3f5SArchie Cobbs 4168245f3f5SArchie Cobbs if (info->remain >= 2) { 4178245f3f5SArchie Cobbs *info->str++ = ch; 4188245f3f5SArchie Cobbs info->remain--; 4198245f3f5SArchie Cobbs } 4208245f3f5SArchie Cobbs } 4218245f3f5SArchie Cobbs 4228245f3f5SArchie Cobbs /* 42382941964SPeter Wemm * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse 42405292ba2SArchie Cobbs * order; return an optional length and a pointer to the last character 42505292ba2SArchie Cobbs * written in the buffer (i.e., the first character of the string). 42605292ba2SArchie Cobbs * The buffer pointed to by `nbuf' must have length >= MAXNBUF. 427791d77e0SPoul-Henning Kamp */ 428791d77e0SPoul-Henning Kamp static char * 429f16a5176SDag-Erling Smørgrav ksprintn(char *nbuf, uintmax_t num, int base, int *lenp) 43005292ba2SArchie Cobbs { 4314d88c459SPoul-Henning Kamp char *p; 432791d77e0SPoul-Henning Kamp 43305292ba2SArchie Cobbs p = nbuf; 434ad4f8dbdSArchie Cobbs *p = '\0'; 435791d77e0SPoul-Henning Kamp do { 4363b1f7e7dSDag-Erling Smørgrav *++p = hex2ascii(num % base); 4373b1f7e7dSDag-Erling Smørgrav } while (num /= base); 4387d921a01SPeter Wemm if (lenp) 4397d921a01SPeter Wemm *lenp = p - nbuf; 4407d921a01SPeter Wemm return (p); 4417d921a01SPeter Wemm } 442791d77e0SPoul-Henning Kamp 443df8bae1dSRodney W. Grimes /* 444df8bae1dSRodney W. Grimes * Scaled down version of printf(3). 445df8bae1dSRodney W. Grimes * 446df8bae1dSRodney W. Grimes * Two additional formats: 447df8bae1dSRodney W. Grimes * 448df8bae1dSRodney W. Grimes * The format %b is supported to decode error registers. 449df8bae1dSRodney W. Grimes * Its usage is: 450df8bae1dSRodney W. Grimes * 451df8bae1dSRodney W. Grimes * printf("reg=%b\n", regval, "<base><arg>*"); 452df8bae1dSRodney W. Grimes * 453df8bae1dSRodney W. Grimes * where <base> is the output base expressed as a control character, e.g. 454df8bae1dSRodney W. Grimes * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 455df8bae1dSRodney W. Grimes * the first of which gives the bit number to be inspected (origin 1), and 456df8bae1dSRodney W. Grimes * the next characters (up to a control character, i.e. a character <= 32), 457df8bae1dSRodney W. Grimes * give the name of the register. Thus: 458df8bae1dSRodney W. Grimes * 4594e31a37bSGary Palmer * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 460df8bae1dSRodney W. Grimes * 461df8bae1dSRodney W. Grimes * would produce output: 462df8bae1dSRodney W. Grimes * 463df8bae1dSRodney W. Grimes * reg=3<BITTWO,BITONE> 464df8bae1dSRodney W. Grimes * 465120f0783SPoul-Henning Kamp * XXX: %D -- Hexdump, takes pointer and separator string: 466120f0783SPoul-Henning Kamp * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX 467120f0783SPoul-Henning Kamp * ("%*D", len, ptr, " " -> XX XX XX XX ... 468df8bae1dSRodney W. Grimes */ 469791d77e0SPoul-Henning Kamp int 470791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) 471df8bae1dSRodney W. Grimes { 472791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } 473ad4f8dbdSArchie Cobbs char nbuf[MAXNBUF]; 4743b1f7e7dSDag-Erling Smørgrav char *d; 4753b1f7e7dSDag-Erling Smørgrav const char *p, *percent, *q; 476120f0783SPoul-Henning Kamp u_char *up; 477791d77e0SPoul-Henning Kamp int ch, n; 4783b1f7e7dSDag-Erling Smørgrav uintmax_t num; 4797d921a01SPeter Wemm int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot; 4803b1f7e7dSDag-Erling Smørgrav int jflag; 4814f20e4b1SPoul-Henning Kamp int dwidth; 482df8bae1dSRodney W. Grimes char padc; 483791d77e0SPoul-Henning Kamp int retval = 0; 484791d77e0SPoul-Henning Kamp 4853b1f7e7dSDag-Erling Smørgrav num = 0; 486fe96d47dSPoul-Henning Kamp if (!func) 487791d77e0SPoul-Henning Kamp d = (char *) arg; 488791d77e0SPoul-Henning Kamp else 489fe96d47dSPoul-Henning Kamp d = NULL; 4908f5067baSDavid Greenman 4918f5067baSDavid Greenman if (fmt == NULL) 4922336b9d7SBruce Evans fmt = "(fmt null)\n"; 493b4b2f81eSPoul-Henning Kamp 494120f0783SPoul-Henning Kamp if (radix < 2 || radix > 36) 495b4b2f81eSPoul-Henning Kamp radix = 10; 496b4b2f81eSPoul-Henning Kamp 497df8bae1dSRodney W. Grimes for (;;) { 498df8bae1dSRodney W. Grimes padc = ' '; 499df8bae1dSRodney W. Grimes width = 0; 500e0c95ed9SBruce Evans while ((ch = (u_char)*fmt++) != '%') { 501df8bae1dSRodney W. Grimes if (ch == '\0') 50258a24f79SPoul-Henning Kamp return (retval); 503791d77e0SPoul-Henning Kamp PCHAR(ch); 504df8bae1dSRodney W. Grimes } 5053b1f7e7dSDag-Erling Smørgrav percent = fmt - 1; 5067d921a01SPeter Wemm qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; 5074f20e4b1SPoul-Henning Kamp sign = 0; dot = 0; dwidth = 0; 5083b1f7e7dSDag-Erling Smørgrav jflag = 0; 509e0c95ed9SBruce Evans reswitch: switch (ch = (u_char)*fmt++) { 5104830092aSPoul-Henning Kamp case '.': 5114830092aSPoul-Henning Kamp dot = 1; 5124830092aSPoul-Henning Kamp goto reswitch; 513791d77e0SPoul-Henning Kamp case '#': 514791d77e0SPoul-Henning Kamp sharpflag = 1; 515791d77e0SPoul-Henning Kamp goto reswitch; 516791d77e0SPoul-Henning Kamp case '+': 517791d77e0SPoul-Henning Kamp sign = 1; 518791d77e0SPoul-Henning Kamp goto reswitch; 519791d77e0SPoul-Henning Kamp case '-': 520791d77e0SPoul-Henning Kamp ladjust = 1; 521791d77e0SPoul-Henning Kamp goto reswitch; 522791d77e0SPoul-Henning Kamp case '%': 523791d77e0SPoul-Henning Kamp PCHAR(ch); 524791d77e0SPoul-Henning Kamp break; 525791d77e0SPoul-Henning Kamp case '*': 526ed71c342SPoul-Henning Kamp if (!dot) { 527791d77e0SPoul-Henning Kamp width = va_arg(ap, int); 528791d77e0SPoul-Henning Kamp if (width < 0) { 529791d77e0SPoul-Henning Kamp ladjust = !ladjust; 530791d77e0SPoul-Henning Kamp width = -width; 531791d77e0SPoul-Henning Kamp } 532ed71c342SPoul-Henning Kamp } else { 533ed71c342SPoul-Henning Kamp dwidth = va_arg(ap, int); 534ed71c342SPoul-Henning Kamp } 535791d77e0SPoul-Henning Kamp goto reswitch; 536df8bae1dSRodney W. Grimes case '0': 5374f20e4b1SPoul-Henning Kamp if (!dot) { 538df8bae1dSRodney W. Grimes padc = '0'; 539df8bae1dSRodney W. Grimes goto reswitch; 5404f20e4b1SPoul-Henning Kamp } 541df8bae1dSRodney W. Grimes case '1': case '2': case '3': case '4': 542df8bae1dSRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 5434f20e4b1SPoul-Henning Kamp for (n = 0;; ++fmt) { 5444f20e4b1SPoul-Henning Kamp n = n * 10 + ch - '0'; 545df8bae1dSRodney W. Grimes ch = *fmt; 546df8bae1dSRodney W. Grimes if (ch < '0' || ch > '9') 547df8bae1dSRodney W. Grimes break; 548df8bae1dSRodney W. Grimes } 5494f20e4b1SPoul-Henning Kamp if (dot) 5504f20e4b1SPoul-Henning Kamp dwidth = n; 5514f20e4b1SPoul-Henning Kamp else 5524f20e4b1SPoul-Henning Kamp width = n; 553df8bae1dSRodney W. Grimes goto reswitch; 554df8bae1dSRodney W. Grimes case 'b': 5553b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, int); 556df8bae1dSRodney W. Grimes p = va_arg(ap, char *); 5573b1f7e7dSDag-Erling Smørgrav for (q = ksprintn(nbuf, num, *p++, NULL); *q;) 558791d77e0SPoul-Henning Kamp PCHAR(*q--); 559df8bae1dSRodney W. Grimes 5603b1f7e7dSDag-Erling Smørgrav if (num == 0) 561df8bae1dSRodney W. Grimes break; 562df8bae1dSRodney W. Grimes 563797f2d22SPoul-Henning Kamp for (tmp = 0; *p;) { 564797f2d22SPoul-Henning Kamp n = *p++; 5653b1f7e7dSDag-Erling Smørgrav if (num & (1 << (n - 1))) { 566791d77e0SPoul-Henning Kamp PCHAR(tmp ? ',' : '<'); 567df8bae1dSRodney W. Grimes for (; (n = *p) > ' '; ++p) 568791d77e0SPoul-Henning Kamp PCHAR(n); 569df8bae1dSRodney W. Grimes tmp = 1; 570df8bae1dSRodney W. Grimes } else 571df8bae1dSRodney W. Grimes for (; *p > ' '; ++p) 572df8bae1dSRodney W. Grimes continue; 573df8bae1dSRodney W. Grimes } 574df8bae1dSRodney W. Grimes if (tmp) 575791d77e0SPoul-Henning Kamp PCHAR('>'); 576df8bae1dSRodney W. Grimes break; 577df8bae1dSRodney W. Grimes case 'c': 578791d77e0SPoul-Henning Kamp PCHAR(va_arg(ap, int)); 579df8bae1dSRodney W. Grimes break; 580120f0783SPoul-Henning Kamp case 'D': 581120f0783SPoul-Henning Kamp up = va_arg(ap, u_char *); 582120f0783SPoul-Henning Kamp p = va_arg(ap, char *); 583120f0783SPoul-Henning Kamp if (!width) 584120f0783SPoul-Henning Kamp width = 16; 585120f0783SPoul-Henning Kamp while(width--) { 586120f0783SPoul-Henning Kamp PCHAR(hex2ascii(*up >> 4)); 587120f0783SPoul-Henning Kamp PCHAR(hex2ascii(*up & 0x0f)); 588120f0783SPoul-Henning Kamp up++; 589120f0783SPoul-Henning Kamp if (width) 590120f0783SPoul-Henning Kamp for (q=p;*q;q++) 591120f0783SPoul-Henning Kamp PCHAR(*q); 592120f0783SPoul-Henning Kamp } 593120f0783SPoul-Henning Kamp break; 594df8bae1dSRodney W. Grimes case 'd': 595df8bae1dSRodney W. Grimes base = 10; 5963b1f7e7dSDag-Erling Smørgrav sign = 1; 5973b1f7e7dSDag-Erling Smørgrav goto handle_sign; 5983b1f7e7dSDag-Erling Smørgrav case 'j': 5993b1f7e7dSDag-Erling Smørgrav jflag = 1; 6003b1f7e7dSDag-Erling Smørgrav goto reswitch; 601791d77e0SPoul-Henning Kamp case 'l': 602301ca4ffSBrian Feldman if (lflag) { 603301ca4ffSBrian Feldman lflag = 0; 604301ca4ffSBrian Feldman qflag = 1; 605301ca4ffSBrian Feldman } else 606791d77e0SPoul-Henning Kamp lflag = 1; 607791d77e0SPoul-Henning Kamp goto reswitch; 6083b1f7e7dSDag-Erling Smørgrav case 'n': 6093b1f7e7dSDag-Erling Smørgrav if (jflag) 6103b1f7e7dSDag-Erling Smørgrav *(va_arg(ap, intmax_t *)) = retval; 6113b1f7e7dSDag-Erling Smørgrav else if (qflag) 6123b1f7e7dSDag-Erling Smørgrav *(va_arg(ap, quad_t *)) = retval; 6137d921a01SPeter Wemm else if (lflag) 6143b1f7e7dSDag-Erling Smørgrav *(va_arg(ap, long *)) = retval; 6157d921a01SPeter Wemm else 6163b1f7e7dSDag-Erling Smørgrav *(va_arg(ap, int *)) = retval; 6173b1f7e7dSDag-Erling Smørgrav break; 6183b1f7e7dSDag-Erling Smørgrav case 'o': 619df8bae1dSRodney W. Grimes base = 8; 6203b1f7e7dSDag-Erling Smørgrav goto handle_nosign; 62112d17f65SPoul-Henning Kamp case 'p': 62212d17f65SPoul-Henning Kamp base = 16; 623c41141b0SBruce Evans sharpflag = (width == 0); 6243b1f7e7dSDag-Erling Smørgrav sign = 0; 6253b1f7e7dSDag-Erling Smørgrav num = (uintptr_t)va_arg(ap, void *); 6263b1f7e7dSDag-Erling Smørgrav goto number; 6277d921a01SPeter Wemm case 'q': 6287d921a01SPeter Wemm qflag = 1; 6297d921a01SPeter Wemm goto reswitch; 630e0c38587SBruce Evans case 'r': 631e0c38587SBruce Evans base = radix; 6323b1f7e7dSDag-Erling Smørgrav if (sign) 6333b1f7e7dSDag-Erling Smørgrav goto handle_sign; 6343b1f7e7dSDag-Erling Smørgrav goto handle_nosign; 635791d77e0SPoul-Henning Kamp case 's': 636791d77e0SPoul-Henning Kamp p = va_arg(ap, char *); 637791d77e0SPoul-Henning Kamp if (p == NULL) 638791d77e0SPoul-Henning Kamp p = "(null)"; 6394830092aSPoul-Henning Kamp if (!dot) 6404830092aSPoul-Henning Kamp n = strlen (p); 6414830092aSPoul-Henning Kamp else 6424f20e4b1SPoul-Henning Kamp for (n = 0; n < dwidth && p[n]; n++) 6434830092aSPoul-Henning Kamp continue; 6444f20e4b1SPoul-Henning Kamp 6454830092aSPoul-Henning Kamp width -= n; 6464f20e4b1SPoul-Henning Kamp 647791d77e0SPoul-Henning Kamp if (!ladjust && width > 0) 648791d77e0SPoul-Henning Kamp while (width--) 649791d77e0SPoul-Henning Kamp PCHAR(padc); 6504830092aSPoul-Henning Kamp while (n--) 651791d77e0SPoul-Henning Kamp PCHAR(*p++); 652791d77e0SPoul-Henning Kamp if (ladjust && width > 0) 653791d77e0SPoul-Henning Kamp while (width--) 654791d77e0SPoul-Henning Kamp PCHAR(padc); 655791d77e0SPoul-Henning Kamp break; 656f53dbe97SBruce Evans case 'u': 657f53dbe97SBruce Evans base = 10; 6583b1f7e7dSDag-Erling Smørgrav goto handle_nosign; 659df8bae1dSRodney W. Grimes case 'x': 660aa998012SMike Smith case 'X': 661df8bae1dSRodney W. Grimes base = 16; 6623b1f7e7dSDag-Erling Smørgrav goto handle_nosign; 663e0c38587SBruce Evans case 'z': 664e0c38587SBruce Evans base = 16; 6653b1f7e7dSDag-Erling Smørgrav if (sign) 6663b1f7e7dSDag-Erling Smørgrav goto handle_sign; 6673b1f7e7dSDag-Erling Smørgrav handle_nosign: 6683b1f7e7dSDag-Erling Smørgrav sign = 0; 6693b1f7e7dSDag-Erling Smørgrav if (jflag) 6703b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, uintmax_t); 6713b1f7e7dSDag-Erling Smørgrav else if (qflag) 6723b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, u_quad_t); 6733b1f7e7dSDag-Erling Smørgrav else if (lflag) 6743b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, u_long); 6753b1f7e7dSDag-Erling Smørgrav else 6763b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, u_int); 677e0c38587SBruce Evans goto number; 6783b1f7e7dSDag-Erling Smørgrav handle_sign: 6793b1f7e7dSDag-Erling Smørgrav if (jflag) 6803b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, intmax_t); 6813b1f7e7dSDag-Erling Smørgrav else if (qflag) 6823b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, quad_t); 6833b1f7e7dSDag-Erling Smørgrav else if (lflag) 6843b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, long); 6853b1f7e7dSDag-Erling Smørgrav else 6863b1f7e7dSDag-Erling Smørgrav num = va_arg(ap, int); 6877d921a01SPeter Wemm number: 6883b1f7e7dSDag-Erling Smørgrav if (sign && (intmax_t)num < 0) { 6897d921a01SPeter Wemm neg = 1; 6903b1f7e7dSDag-Erling Smørgrav num = -(intmax_t)num; 6917d921a01SPeter Wemm } 6923b1f7e7dSDag-Erling Smørgrav p = ksprintn(nbuf, num, base, &tmp); 6933b1f7e7dSDag-Erling Smørgrav if (sharpflag && num != 0) { 694791d77e0SPoul-Henning Kamp if (base == 8) 695791d77e0SPoul-Henning Kamp tmp++; 696791d77e0SPoul-Henning Kamp else if (base == 16) 697791d77e0SPoul-Henning Kamp tmp += 2; 698791d77e0SPoul-Henning Kamp } 699791d77e0SPoul-Henning Kamp if (neg) 700791d77e0SPoul-Henning Kamp tmp++; 701791d77e0SPoul-Henning Kamp 702791d77e0SPoul-Henning Kamp if (!ladjust && width && (width -= tmp) > 0) 703df8bae1dSRodney W. Grimes while (width--) 704791d77e0SPoul-Henning Kamp PCHAR(padc); 705791d77e0SPoul-Henning Kamp if (neg) 706791d77e0SPoul-Henning Kamp PCHAR('-'); 7073b1f7e7dSDag-Erling Smørgrav if (sharpflag && num != 0) { 708791d77e0SPoul-Henning Kamp if (base == 8) { 709791d77e0SPoul-Henning Kamp PCHAR('0'); 710791d77e0SPoul-Henning Kamp } else if (base == 16) { 711791d77e0SPoul-Henning Kamp PCHAR('0'); 712791d77e0SPoul-Henning Kamp PCHAR('x'); 713791d77e0SPoul-Henning Kamp } 714791d77e0SPoul-Henning Kamp } 715791d77e0SPoul-Henning Kamp 716797f2d22SPoul-Henning Kamp while (*p) 717791d77e0SPoul-Henning Kamp PCHAR(*p--); 718791d77e0SPoul-Henning Kamp 719791d77e0SPoul-Henning Kamp if (ladjust && width && (width -= tmp) > 0) 720791d77e0SPoul-Henning Kamp while (width--) 721791d77e0SPoul-Henning Kamp PCHAR(padc); 722791d77e0SPoul-Henning Kamp 723df8bae1dSRodney W. Grimes break; 724df8bae1dSRodney W. Grimes default: 7253b1f7e7dSDag-Erling Smørgrav while (percent < fmt) 7263b1f7e7dSDag-Erling Smørgrav PCHAR(*percent++); 727791d77e0SPoul-Henning Kamp break; 728df8bae1dSRodney W. Grimes } 729df8bae1dSRodney W. Grimes } 730791d77e0SPoul-Henning Kamp #undef PCHAR 731df8bae1dSRodney W. Grimes } 732df8bae1dSRodney W. Grimes 733df8bae1dSRodney W. Grimes /* 734e2a09b26SPoul-Henning Kamp * Put character in log buffer with a particular priority. 735df8bae1dSRodney W. Grimes */ 736df8bae1dSRodney W. Grimes static void 737e2a09b26SPoul-Henning Kamp msglogchar(int c, int pri) 738e2a09b26SPoul-Henning Kamp { 739e2a09b26SPoul-Henning Kamp static int lastpri = -1; 740e2a09b26SPoul-Henning Kamp static int dangling; 741e2a09b26SPoul-Henning Kamp char nbuf[MAXNBUF]; 742e2a09b26SPoul-Henning Kamp char *p; 743e2a09b26SPoul-Henning Kamp 744e2a09b26SPoul-Henning Kamp if (!msgbufmapped) 745e2a09b26SPoul-Henning Kamp return; 746e2a09b26SPoul-Henning Kamp if (c == '\0' || c == '\r') 747e2a09b26SPoul-Henning Kamp return; 748e2a09b26SPoul-Henning Kamp if (pri != -1 && pri != lastpri) { 749e2a09b26SPoul-Henning Kamp if (dangling) { 750e2a09b26SPoul-Henning Kamp msgaddchar('\n', NULL); 751e2a09b26SPoul-Henning Kamp dangling = 0; 752e2a09b26SPoul-Henning Kamp } 753e2a09b26SPoul-Henning Kamp msgaddchar('<', NULL); 7543b1f7e7dSDag-Erling Smørgrav for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL); *p;) 755e2a09b26SPoul-Henning Kamp msgaddchar(*p--, NULL); 756e2a09b26SPoul-Henning Kamp msgaddchar('>', NULL); 757e2a09b26SPoul-Henning Kamp lastpri = pri; 758e2a09b26SPoul-Henning Kamp } 759e2a09b26SPoul-Henning Kamp msgaddchar(c, NULL); 760e2a09b26SPoul-Henning Kamp if (c == '\n') { 761e2a09b26SPoul-Henning Kamp dangling = 0; 762e2a09b26SPoul-Henning Kamp lastpri = -1; 763e2a09b26SPoul-Henning Kamp } else { 764e2a09b26SPoul-Henning Kamp dangling = 1; 765e2a09b26SPoul-Henning Kamp } 766e2a09b26SPoul-Henning Kamp } 767e2a09b26SPoul-Henning Kamp 768e2a09b26SPoul-Henning Kamp /* 769e2a09b26SPoul-Henning Kamp * Put char in log buffer 770e2a09b26SPoul-Henning Kamp */ 771e2a09b26SPoul-Henning Kamp static void 772e2a09b26SPoul-Henning Kamp msgaddchar(int c, void *dummy) 773df8bae1dSRodney W. Grimes { 774791d77e0SPoul-Henning Kamp struct msgbuf *mbp; 775df8bae1dSRodney W. Grimes 776e2a09b26SPoul-Henning Kamp if (!msgbufmapped) 777e2a09b26SPoul-Henning Kamp return; 778df8bae1dSRodney W. Grimes mbp = msgbufp; 77958067a99SPoul-Henning Kamp mbp->msg_ptr[mbp->msg_bufx++] = c; 78058067a99SPoul-Henning Kamp if (mbp->msg_bufx >= mbp->msg_size) 781df8bae1dSRodney W. Grimes mbp->msg_bufx = 0; 78202d5c7b1SDavid Greenman /* If the buffer is full, keep the most recent data. */ 783a3f4faceSBruce Evans if (mbp->msg_bufr == mbp->msg_bufx) { 78458067a99SPoul-Henning Kamp if (++mbp->msg_bufr >= mbp->msg_size) 785a3f4faceSBruce Evans mbp->msg_bufr = 0; 786a3f4faceSBruce Evans } 787df8bae1dSRodney W. Grimes } 788e796e00dSPoul-Henning Kamp 789eb9d435aSJonathan Lemon static void 790eb9d435aSJonathan Lemon msgbufcopy(struct msgbuf *oldp) 791eb9d435aSJonathan Lemon { 792eb9d435aSJonathan Lemon int pos; 793eb9d435aSJonathan Lemon 794eb9d435aSJonathan Lemon pos = oldp->msg_bufr; 795eb9d435aSJonathan Lemon while (pos != oldp->msg_bufx) { 796e2a09b26SPoul-Henning Kamp msglogchar(oldp->msg_ptr[pos], -1); 797eb9d435aSJonathan Lemon if (++pos >= oldp->msg_size) 798eb9d435aSJonathan Lemon pos = 0; 799eb9d435aSJonathan Lemon } 800eb9d435aSJonathan Lemon } 801eb9d435aSJonathan Lemon 802e796e00dSPoul-Henning Kamp void 803e796e00dSPoul-Henning Kamp msgbufinit(void *ptr, size_t size) 804e796e00dSPoul-Henning Kamp { 805e796e00dSPoul-Henning Kamp char *cp; 806eb9d435aSJonathan Lemon static struct msgbuf *oldp = NULL; 807e796e00dSPoul-Henning Kamp 808cca8f980SIan Dowse size -= sizeof(*msgbufp); 809e796e00dSPoul-Henning Kamp cp = (char *)ptr; 810cca8f980SIan Dowse msgbufp = (struct msgbuf *) (cp + size); 811cca8f980SIan Dowse if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size || 812cca8f980SIan Dowse msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) { 813e796e00dSPoul-Henning Kamp bzero(cp, size); 814cca8f980SIan Dowse bzero(msgbufp, sizeof(*msgbufp)); 815e796e00dSPoul-Henning Kamp msgbufp->msg_magic = MSG_MAGIC; 816e796e00dSPoul-Henning Kamp msgbufp->msg_size = (char *)msgbufp - cp; 817e796e00dSPoul-Henning Kamp } 818cca8f980SIan Dowse msgbufp->msg_ptr = cp; 819eb9d435aSJonathan Lemon if (msgbufmapped && oldp != msgbufp) 820eb9d435aSJonathan Lemon msgbufcopy(oldp); 821e796e00dSPoul-Henning Kamp msgbufmapped = 1; 822eb9d435aSJonathan Lemon oldp = msgbufp; 823e796e00dSPoul-Henning Kamp } 824e796e00dSPoul-Henning Kamp 825d0615c64SAndrew R. Reiter SYSCTL_DECL(_security_bsd); 8266f3933faSRobert Watson 8276f3933faSRobert Watson static int unprivileged_read_msgbuf = 1; 828d0615c64SAndrew R. Reiter SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf, 8296f3933faSRobert Watson CTLFLAG_RW, &unprivileged_read_msgbuf, 0, 8306f3933faSRobert Watson "Unprivileged processes may read the kernel message buffer"); 8316f3933faSRobert Watson 832948d3d94SThomas Moestl /* Sysctls for accessing/clearing the msgbuf */ 833948d3d94SThomas Moestl static int 834948d3d94SThomas Moestl sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS) 835948d3d94SThomas Moestl { 836948d3d94SThomas Moestl int error; 837948d3d94SThomas Moestl 8386f3933faSRobert Watson if (!unprivileged_read_msgbuf) { 83944731cabSJohn Baldwin error = suser(req->td); 8406f3933faSRobert Watson if (error) 8416f3933faSRobert Watson return (error); 8426f3933faSRobert Watson } 8436f3933faSRobert Watson 844948d3d94SThomas Moestl /* 845948d3d94SThomas Moestl * Unwind the buffer, so that it's linear (possibly starting with 846948d3d94SThomas Moestl * some initial nulls). 847948d3d94SThomas Moestl */ 848948d3d94SThomas Moestl error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx, 849948d3d94SThomas Moestl msgbufp->msg_size - msgbufp->msg_bufx, req); 850948d3d94SThomas Moestl if (error) 851948d3d94SThomas Moestl return (error); 852948d3d94SThomas Moestl if (msgbufp->msg_bufx > 0) { 853948d3d94SThomas Moestl error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr, 854948d3d94SThomas Moestl msgbufp->msg_bufx, req); 855948d3d94SThomas Moestl } 856948d3d94SThomas Moestl return (error); 857948d3d94SThomas Moestl } 858948d3d94SThomas Moestl 859948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD, 860948d3d94SThomas Moestl 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer"); 861948d3d94SThomas Moestl 862948d3d94SThomas Moestl static int msgbuf_clear; 863948d3d94SThomas Moestl 864948d3d94SThomas Moestl static int 865948d3d94SThomas Moestl sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS) 866948d3d94SThomas Moestl { 867948d3d94SThomas Moestl int error; 868948d3d94SThomas Moestl error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); 869948d3d94SThomas Moestl if (!error && req->newptr) { 870948d3d94SThomas Moestl /* Clear the buffer and reset write pointer */ 871948d3d94SThomas Moestl bzero(msgbufp->msg_ptr, msgbufp->msg_size); 872948d3d94SThomas Moestl msgbufp->msg_bufr = msgbufp->msg_bufx = 0; 873948d3d94SThomas Moestl msgbuf_clear = 0; 874948d3d94SThomas Moestl } 875948d3d94SThomas Moestl return (error); 876948d3d94SThomas Moestl } 877948d3d94SThomas Moestl 878948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear, 879948d3d94SThomas Moestl CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0, 880948d3d94SThomas Moestl sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer"); 881948d3d94SThomas Moestl 882e796e00dSPoul-Henning Kamp #include "opt_ddb.h" 883e796e00dSPoul-Henning Kamp #ifdef DDB 884e796e00dSPoul-Henning Kamp #include <ddb/ddb.h> 885e796e00dSPoul-Henning Kamp 886e796e00dSPoul-Henning Kamp DB_SHOW_COMMAND(msgbuf, db_show_msgbuf) 887e796e00dSPoul-Henning Kamp { 888e796e00dSPoul-Henning Kamp int i, j; 889e796e00dSPoul-Henning Kamp 890e796e00dSPoul-Henning Kamp if (!msgbufmapped) { 891e796e00dSPoul-Henning Kamp db_printf("msgbuf not mapped yet\n"); 892e796e00dSPoul-Henning Kamp return; 893e796e00dSPoul-Henning Kamp } 894e796e00dSPoul-Henning Kamp db_printf("msgbufp = %p\n", msgbufp); 895e796e00dSPoul-Henning Kamp db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n", 896e796e00dSPoul-Henning Kamp msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr, 897e796e00dSPoul-Henning Kamp msgbufp->msg_bufx, msgbufp->msg_ptr); 898e796e00dSPoul-Henning Kamp for (i = 0; i < msgbufp->msg_size; i++) { 899e796e00dSPoul-Henning Kamp j = (i + msgbufp->msg_bufr) % msgbufp->msg_size; 900e796e00dSPoul-Henning Kamp db_printf("%c", msgbufp->msg_ptr[j]); 901e796e00dSPoul-Henning Kamp } 902e796e00dSPoul-Henning Kamp db_printf("\n"); 903e796e00dSPoul-Henning Kamp } 904e796e00dSPoul-Henning Kamp 905e796e00dSPoul-Henning Kamp #endif /* DDB */ 906