kernel - Add support for %z[d,x,etc].
[dragonfly.git] / sys / kern / subr_prf.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/subr_prf.c,v 1.61.2.5 2002/08/31 18:22:08 dwmalone Exp $
448f8e02 40 * $DragonFly: src/sys/kern/subr_prf.c,v 1.21 2008/07/17 23:56:23 dillon Exp $
984263bc
MD
41 */
42
a0a36cfd
SW
43#include "opt_ddb.h"
44
984263bc
MD
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/msgbuf.h>
49#include <sys/malloc.h>
50#include <sys/proc.h>
895c1f85 51#include <sys/priv.h>
984263bc
MD
52#include <sys/tty.h>
53#include <sys/tprintf.h>
a0a36cfd 54#include <sys/stdint.h>
984263bc
MD
55#include <sys/syslog.h>
56#include <sys/cons.h>
57#include <sys/uio.h>
58#include <sys/sysctl.h>
8a8d5d85 59#include <sys/lock.h>
a0a36cfd
SW
60#include <sys/ctype.h>
61
62#ifdef DDB
63#include <ddb/ddb.h>
64#endif
984263bc
MD
65
66/*
67 * Note that stdarg.h and the ANSI style va_start macro is used for both
e2565a42
MD
68 * ANSI and traditional C compilers. We use the __ machine version to stay
69 * within the kernel header file set.
984263bc
MD
70 */
71#include <machine/stdarg.h>
72
73#define TOCONS 0x01
74#define TOTTY 0x02
75#define TOLOG 0x04
76
77/* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
a0a36cfd 78#define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
984263bc
MD
79
80struct putchar_arg {
81 int flags;
82 int pri;
83 struct tty *tty;
84};
85
86struct snprintf_arg {
87 char *str;
88 size_t remain;
89};
90
91extern int log_open;
92
93struct tty *constty; /* pointer to console "window" tty */
94
95static void (*v_putc)(int) = cnputc; /* routine to putc on virtual console */
96static void msglogchar(int c, int pri);
97static void msgaddchar(int c, void *dummy);
0ced1954 98static void kputchar (int ch, void *arg);
a0a36cfd
SW
99static char *ksprintn (char *nbuf, uintmax_t num, int base, int *lenp,
100 int upper);
402ed7e1 101static void snprintf_func (int ch, void *arg);
984263bc
MD
102
103static int consintr = 1; /* Ok to handle console interrupts? */
104static int msgbufmapped; /* Set when safe to use msgbuf */
105int msgbuftrigger;
106
107static int log_console_output = 1;
108TUNABLE_INT("kern.log_console_output", &log_console_output);
109SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
110 &log_console_output, 0, "");
111
d41cbec3 112static int unprivileged_read_msgbuf = 1;
43a0f7ae 113SYSCTL_INT(_security, OID_AUTO, unprivileged_read_msgbuf, CTLFLAG_RW,
d41cbec3
MD
114 &unprivileged_read_msgbuf, 0,
115 "Unprivileged processes may read the kernel message buffer");
116
984263bc
MD
117/*
118 * Warn that a system table is full.
119 */
120void
121tablefull(const char *tab)
122{
123
124 log(LOG_ERR, "%s: table is full\n", tab);
125}
126
127/*
128 * Uprintf prints to the controlling terminal for the current process.
984263bc
MD
129 */
130int
131uprintf(const char *fmt, ...)
132{
133 struct proc *p = curproc;
e2565a42 134 __va_list ap;
984263bc
MD
135 struct putchar_arg pca;
136 int retval = 0;
137
138 if (p && p->p_flag & P_CONTROLT &&
139 p->p_session->s_ttyvp) {
e2565a42 140 __va_start(ap, fmt);
984263bc
MD
141 pca.tty = p->p_session->s_ttyp;
142 pca.flags = TOTTY;
8a8d5d85 143
379210cb 144 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
e2565a42 145 __va_end(ap);
984263bc 146 }
a0a36cfd 147 return (retval);
984263bc
MD
148}
149
150tpr_t
dadab5e9 151tprintf_open(struct proc *p)
984263bc
MD
152{
153
dadab5e9 154 if ((p->p_flag & P_CONTROLT) && p->p_session->s_ttyvp) {
8b90699b 155 sess_hold(p->p_session);
984263bc
MD
156 return ((tpr_t) p->p_session);
157 }
158 return ((tpr_t) NULL);
159}
160
161void
dadab5e9 162tprintf_close(tpr_t sess)
984263bc 163{
984263bc 164 if (sess)
8b90699b 165 sess_rele((struct session *) sess);
984263bc
MD
166}
167
168/*
169 * tprintf prints on the controlling terminal associated
170 * with the given session.
171 */
172int
173tprintf(tpr_t tpr, const char *fmt, ...)
174{
1fd87d54 175 struct session *sess = (struct session *)tpr;
984263bc
MD
176 struct tty *tp = NULL;
177 int flags = TOLOG;
e2565a42 178 __va_list ap;
984263bc
MD
179 struct putchar_arg pca;
180 int retval;
181
182 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
183 flags |= TOTTY;
184 tp = sess->s_ttyp;
185 }
e2565a42 186 __va_start(ap, fmt);
984263bc
MD
187 pca.tty = tp;
188 pca.flags = flags;
189 pca.pri = LOG_INFO;
379210cb 190 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
e2565a42 191 __va_end(ap);
984263bc 192 msgbuftrigger = 1;
a0a36cfd 193 return (retval);
984263bc
MD
194}
195
196/*
197 * Ttyprintf displays a message on a tty; it should be used only by
198 * the tty driver, or anything that knows the underlying tty will not
199 * be revoke(2)'d away. Other callers should use tprintf.
200 */
201int
202ttyprintf(struct tty *tp, const char *fmt, ...)
203{
e2565a42 204 __va_list ap;
984263bc
MD
205 struct putchar_arg pca;
206 int retval;
207
e2565a42 208 __va_start(ap, fmt);
984263bc
MD
209 pca.tty = tp;
210 pca.flags = TOTTY;
379210cb 211 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
e2565a42 212 __va_end(ap);
a0a36cfd 213 return (retval);
984263bc
MD
214}
215
216/*
217 * Log writes to the log buffer, and guarantees not to sleep (so can be
218 * called by interrupt routines). If there is no process reading the
219 * log yet, it writes to the console also.
220 */
221int
222log(int level, const char *fmt, ...)
223{
e2565a42 224 __va_list ap;
984263bc
MD
225 int retval;
226 struct putchar_arg pca;
227
228 pca.tty = NULL;
229 pca.pri = level;
230 pca.flags = log_open ? TOLOG : TOCONS;
231
e2565a42 232 __va_start(ap, fmt);
379210cb 233 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
e2565a42 234 __va_end(ap);
984263bc
MD
235
236 msgbuftrigger = 1;
237 return (retval);
238}
239
984263bc
MD
240#define CONSCHUNK 128
241
242void
243log_console(struct uio *uio)
244{
245 int c, i, error, iovlen, nl;
246 struct uio muio;
247 struct iovec *miov = NULL;
248 char *consbuffer;
249 int pri;
250
251 if (!log_console_output)
252 return;
253
254 pri = LOG_INFO | LOG_CONSOLE;
255 muio = *uio;
256 iovlen = uio->uio_iovcnt * sizeof (struct iovec);
257 MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
258 MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
259 bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen);
260 muio.uio_iov = miov;
261 uio = &muio;
262
263 nl = 0;
264 while (uio->uio_resid > 0) {
e54488bb
MD
265 c = (int)szmin(uio->uio_resid, CONSCHUNK);
266 error = uiomove(consbuffer, (size_t)c, uio);
984263bc 267 if (error != 0)
a0a36cfd 268 break;
984263bc
MD
269 for (i = 0; i < c; i++) {
270 msglogchar(consbuffer[i], pri);
271 if (consbuffer[i] == '\n')
272 nl = 1;
273 else
274 nl = 0;
275 }
276 }
277 if (!nl)
278 msglogchar('\n', pri);
279 msgbuftrigger = 1;
280 FREE(miov, M_TEMP);
281 FREE(consbuffer, M_TEMP);
282 return;
283}
284
e02941a7
MD
285/*
286 * Output to the console.
287 *
288 * NOT YET ENTIRELY MPSAFE
289 */
984263bc 290int
169276ba
MD
291kprintf(const char *fmt, ...)
292{
293 __va_list ap;
294 int savintr;
295 struct putchar_arg pca;
296 int retval;
297
298 savintr = consintr; /* disable interrupts */
299 consintr = 0;
300 __va_start(ap, fmt);
301 pca.tty = NULL;
302 pca.flags = TOCONS | TOLOG;
303 pca.pri = -1;
304 cons_lock();
305 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
306 cons_unlock();
307 __va_end(ap);
308 if (!panicstr)
309 msgbuftrigger = 1;
310 consintr = savintr; /* reenable interrupts */
a0a36cfd 311 return (retval);
169276ba
MD
312}
313
984263bc 314int
379210cb 315kvprintf(const char *fmt, __va_list ap)
984263bc
MD
316{
317 int savintr;
318 struct putchar_arg pca;
319 int retval;
320
321 savintr = consintr; /* disable interrupts */
322 consintr = 0;
323 pca.tty = NULL;
324 pca.flags = TOCONS | TOLOG;
325 pca.pri = -1;
8a8d5d85 326 cons_lock();
379210cb 327 retval = kvcprintf(fmt, kputchar, &pca, 10, ap);
8a8d5d85 328 cons_unlock();
984263bc
MD
329 if (!panicstr)
330 msgbuftrigger = 1;
331 consintr = savintr; /* reenable interrupts */
a0a36cfd 332 return (retval);
984263bc
MD
333}
334
335/*
c8dfd00d
MD
336 * Limited rate kprintf. The passed rate structure must be initialized
337 * with the desired reporting frequency. A frequency of 0 will result in
338 * no output.
448f8e02
MD
339 *
340 * count may be initialized to a negative number to allow an initial
341 * burst.
c8dfd00d
MD
342 */
343void
344krateprintf(struct krate *rate, const char *fmt, ...)
345{
346 __va_list ap;
347
1c9c514a
MD
348 if (rate->ticks != (int)time_second) {
349 rate->ticks = (int)time_second;
448f8e02
MD
350 if (rate->count > 0)
351 rate->count = 0;
c8dfd00d
MD
352 }
353 if (rate->count < rate->freq) {
354 ++rate->count;
355 __va_start(ap, fmt);
356 kvprintf(fmt, ap);
357 __va_end(ap);
358 }
359}
360
361/*
984263bc
MD
362 * Print a character on console or users terminal. If destination is
363 * the console then the last bunch of characters are saved in msgbuf for
364 * inspection later.
e02941a7
MD
365 *
366 * NOT YET ENTIRELY MPSAFE, EVEN WHEN LOGGING JUST TO THE SYSCONSOLE.
984263bc
MD
367 */
368static void
0ced1954 369kputchar(int c, void *arg)
984263bc
MD
370{
371 struct putchar_arg *ap = (struct putchar_arg*) arg;
372 int flags = ap->flags;
373 struct tty *tp = ap->tty;
374 if (panicstr)
375 constty = NULL;
376 if ((flags & TOCONS) && tp == NULL && constty) {
377 tp = constty;
378 flags |= TOTTY;
379 }
380 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
381 (flags & TOCONS) && tp == constty)
382 constty = NULL;
383 if ((flags & TOLOG))
384 msglogchar(c, ap->pri);
385 if ((flags & TOCONS) && constty == NULL && c != '\0')
386 (*v_putc)(c);
387}
388
389/*
390 * Scaled down version of sprintf(3).
391 */
392int
169276ba
MD
393ksprintf(char *buf, const char *cfmt, ...)
394{
395 int retval;
396 __va_list ap;
397
398 __va_start(ap, cfmt);
399 retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap);
400 buf[retval] = '\0';
401 __va_end(ap);
a0a36cfd 402 return (retval);
169276ba
MD
403}
404
405/*
984263bc
MD
406 * Scaled down version of vsprintf(3).
407 */
408int
379210cb 409kvsprintf(char *buf, const char *cfmt, __va_list ap)
984263bc
MD
410{
411 int retval;
412
379210cb 413 retval = kvcprintf(cfmt, NULL, (void *)buf, 10, ap);
984263bc 414 buf[retval] = '\0';
a0a36cfd 415 return (retval);
984263bc
MD
416}
417
418/*
419 * Scaled down version of snprintf(3).
420 */
421int
169276ba
MD
422ksnprintf(char *str, size_t size, const char *format, ...)
423{
424 int retval;
425 __va_list ap;
984263bc 426
e2565a42 427 __va_start(ap, format);
379210cb 428 retval = kvsnprintf(str, size, format, ap);
e2565a42 429 __va_end(ap);
984263bc
MD
430 return(retval);
431}
432
433/*
434 * Scaled down version of vsnprintf(3).
435 */
436int
379210cb 437kvsnprintf(char *str, size_t size, const char *format, __va_list ap)
984263bc
MD
438{
439 struct snprintf_arg info;
440 int retval;
441
442 info.str = str;
443 info.remain = size;
379210cb 444 retval = kvcprintf(format, snprintf_func, &info, 10, ap);
984263bc
MD
445 if (info.remain >= 1)
446 *info.str++ = '\0';
a0a36cfd 447 return (retval);
984263bc
MD
448}
449
450static void
451snprintf_func(int ch, void *arg)
452{
453 struct snprintf_arg *const info = arg;
454
455 if (info->remain >= 2) {
456 *info->str++ = ch;
457 info->remain--;
458 }
459}
460
461/*
462 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
463 * order; return an optional length and a pointer to the last character
464 * written in the buffer (i.e., the first character of the string).
465 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
466 */
467static char *
a0a36cfd 468ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
984263bc 469{
a0a36cfd 470 char *p, c;
984263bc
MD
471
472 p = nbuf;
473 *p = '\0';
474 do {
a0a36cfd
SW
475 c = hex2ascii(num % base);
476 *++p = upper ? toupper(c) : c;
477 } while (num /= base);
984263bc
MD
478 if (lenp)
479 *lenp = p - nbuf;
480 return (p);
481}
482
483/*
484 * Scaled down version of printf(3).
485 *
486 * Two additional formats:
487 *
488 * The format %b is supported to decode error registers.
489 * Its usage is:
490 *
71681fd7 491 * kprintf("reg=%b\n", regval, "<base><arg>*");
984263bc
MD
492 *
493 * where <base> is the output base expressed as a control character, e.g.
494 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
495 * the first of which gives the bit number to be inspected (origin 1), and
496 * the next characters (up to a control character, i.e. a character <= 32),
497 * give the name of the register. Thus:
498 *
379210cb 499 * kvcprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
984263bc
MD
500 *
501 * would produce output:
502 *
503 * reg=3<BITTWO,BITONE>
504 *
505 * XXX: %D -- Hexdump, takes pointer and separator string:
506 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
507 * ("%*D", len, ptr, " " -> XX XX XX XX ...
508 */
509int
379210cb 510kvcprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, __va_list ap)
984263bc
MD
511{
512#define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
513 char nbuf[MAXNBUF];
a0a36cfd
SW
514 char *d;
515 const char *p, *percent, *q;
984263bc
MD
516 u_char *up;
517 int ch, n;
a0a36cfd
SW
518 uintmax_t num;
519 int base, tmp, width, ladjust, sharpflag, neg, sign, dot;
9c7517ab 520 int jflag, lflag, qflag, tflag, zflag;
a0a36cfd 521 int dwidth, upper;
984263bc 522 char padc;
a0a36cfd 523 int retval = 0, stop = 0;
984263bc 524
a0a36cfd 525 num = 0;
984263bc
MD
526 if (!func)
527 d = (char *) arg;
528 else
529 d = NULL;
530
531 if (fmt == NULL)
532 fmt = "(fmt null)\n";
533
534 if (radix < 2 || radix > 36)
535 radix = 10;
536
537 for (;;) {
538 padc = ' ';
539 width = 0;
a0a36cfd
SW
540 while ((ch = (u_char)*fmt++) != '%' || stop) {
541 if (ch == '\0')
542 return (retval);
984263bc
MD
543 PCHAR(ch);
544 }
a0a36cfd
SW
545 percent = fmt - 1;
546 dot = dwidth = ladjust = neg = sharpflag = sign = upper = 0;
9c7517ab 547 jflag = lflag = qflag = tflag = zflag = 0;
a0a36cfd
SW
548
549reswitch:
550 switch (ch = (u_char)*fmt++) {
984263bc
MD
551 case '.':
552 dot = 1;
553 goto reswitch;
554 case '#':
555 sharpflag = 1;
556 goto reswitch;
557 case '+':
558 sign = 1;
559 goto reswitch;
560 case '-':
561 ladjust = 1;
562 goto reswitch;
563 case '%':
564 PCHAR(ch);
565 break;
566 case '*':
567 if (!dot) {
e2565a42 568 width = __va_arg(ap, int);
984263bc
MD
569 if (width < 0) {
570 ladjust = !ladjust;
571 width = -width;
572 }
573 } else {
e2565a42 574 dwidth = __va_arg(ap, int);
984263bc
MD
575 }
576 goto reswitch;
577 case '0':
578 if (!dot) {
579 padc = '0';
580 goto reswitch;
581 }
582 case '1': case '2': case '3': case '4':
583 case '5': case '6': case '7': case '8': case '9':
584 for (n = 0;; ++fmt) {
585 n = n * 10 + ch - '0';
586 ch = *fmt;
587 if (ch < '0' || ch > '9')
588 break;
589 }
590 if (dot)
591 dwidth = n;
592 else
593 width = n;
594 goto reswitch;
595 case 'b':
a0a36cfd 596 num = (u_int)__va_arg(ap, int);
e2565a42 597 p = __va_arg(ap, char *);
a0a36cfd 598 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
984263bc
MD
599 PCHAR(*q--);
600
a0a36cfd 601 if (num == 0)
984263bc
MD
602 break;
603
604 for (tmp = 0; *p;) {
605 n = *p++;
a0a36cfd 606 if (num & (1 << (n - 1))) {
984263bc
MD
607 PCHAR(tmp ? ',' : '<');
608 for (; (n = *p) > ' '; ++p)
609 PCHAR(n);
610 tmp = 1;
611 } else
612 for (; *p > ' '; ++p)
613 continue;
614 }
615 if (tmp)
616 PCHAR('>');
617 break;
618 case 'c':
e2565a42 619 PCHAR(__va_arg(ap, int));
984263bc
MD
620 break;
621 case 'D':
e2565a42
MD
622 up = __va_arg(ap, u_char *);
623 p = __va_arg(ap, char *);
984263bc
MD
624 if (!width)
625 width = 16;
626 while(width--) {
627 PCHAR(hex2ascii(*up >> 4));
628 PCHAR(hex2ascii(*up & 0x0f));
629 up++;
630 if (width)
631 for (q=p;*q;q++)
632 PCHAR(*q);
633 }
634 break;
635 case 'd':
a0a36cfd 636 case 'i':
984263bc 637 base = 10;
a0a36cfd
SW
638 sign = 1;
639 goto handle_sign;
640 case 'j':
641 jflag = 1;
642 goto reswitch;
984263bc
MD
643 case 'l':
644 if (lflag) {
645 lflag = 0;
646 qflag = 1;
647 } else
648 lflag = 1;
649 goto reswitch;
a0a36cfd
SW
650 case 'n':
651 if (jflag)
652 *(__va_arg(ap, intmax_t *)) = retval;
984263bc 653 else if (lflag)
a0a36cfd
SW
654 *(__va_arg(ap, long *)) = retval;
655 else if (qflag)
656 *(__va_arg(ap, quad_t *)) = retval;
984263bc 657 else
a0a36cfd
SW
658 *(__va_arg(ap, int *)) = retval;
659 break;
660 case 'o':
984263bc 661 base = 8;
a0a36cfd 662 goto handle_nosign;
984263bc 663 case 'p':
984263bc
MD
664 base = 16;
665 sharpflag = (width == 0);
a0a36cfd
SW
666 sign = 0;
667 num = (uintptr_t)__va_arg(ap, void *);
668 goto number;
984263bc
MD
669 case 'q':
670 qflag = 1;
671 goto reswitch;
984263bc 672 case 'r':
984263bc 673 base = radix;
a0a36cfd
SW
674 if (sign)
675 goto handle_sign;
676 goto handle_nosign;
984263bc 677 case 's':
e2565a42 678 p = __va_arg(ap, char *);
984263bc
MD
679 if (p == NULL)
680 p = "(null)";
681 if (!dot)
682 n = strlen (p);
683 else
684 for (n = 0; n < dwidth && p[n]; n++)
685 continue;
686
687 width -= n;
688
689 if (!ladjust && width > 0)
690 while (width--)
691 PCHAR(padc);
692 while (n--)
693 PCHAR(*p++);
694 if (ladjust && width > 0)
695 while (width--)
696 PCHAR(padc);
697 break;
a0a36cfd
SW
698 case 't':
699 tflag = 1;
700 goto reswitch;
984263bc 701 case 'u':
984263bc 702 base = 10;
a0a36cfd 703 goto handle_nosign;
984263bc 704 case 'X':
a0a36cfd
SW
705 upper = 1;
706 /* FALLTHROUGH */
707 case 'x':
984263bc 708 base = 16;
a0a36cfd 709 goto handle_nosign;
984263bc 710 case 'z':
9c7517ab
MD
711 zflag = 1;
712 goto reswitch;
a0a36cfd
SW
713handle_nosign:
714 sign = 0;
715 if (jflag)
716 num = __va_arg(ap, uintmax_t);
984263bc 717 else if (lflag)
a0a36cfd
SW
718 num = __va_arg(ap, u_long);
719 else if (qflag)
720 num = __va_arg(ap, u_quad_t);
721 else if (tflag)
722 num = __va_arg(ap, ptrdiff_t);
9c7517ab
MD
723 else if (zflag)
724 num = __va_arg(ap, size_t);
984263bc 725 else
a0a36cfd 726 num = __va_arg(ap, u_int);
984263bc 727 goto number;
a0a36cfd
SW
728handle_sign:
729 if (jflag)
730 num = __va_arg(ap, intmax_t);
731 else if (lflag)
732 num = __va_arg(ap, long);
733 else if (qflag)
734 num = __va_arg(ap, quad_t);
735 else if (tflag)
736 num = __va_arg(ap, ptrdiff_t);
9c7517ab
MD
737 else if (zflag)
738 num = __va_arg(ap, ssize_t);
a0a36cfd
SW
739 else
740 num = __va_arg(ap, int);
741number:
742 if (sign && (intmax_t)num < 0) {
743 neg = 1;
744 num = -(intmax_t)num;
984263bc 745 }
a0a36cfd
SW
746 p = ksprintn(nbuf, num, base, &tmp, upper);
747 if (sharpflag && num != 0) {
984263bc
MD
748 if (base == 8)
749 tmp++;
750 else if (base == 16)
751 tmp += 2;
752 }
753 if (neg)
754 tmp++;
755
a0a36cfd
SW
756 if (!ladjust && padc != '0' && width &&
757 (width -= tmp) > 0) {
984263bc
MD
758 while (width--)
759 PCHAR(padc);
a0a36cfd 760 }
984263bc
MD
761 if (neg)
762 PCHAR('-');
a0a36cfd 763 if (sharpflag && num != 0) {
984263bc
MD
764 if (base == 8) {
765 PCHAR('0');
766 } else if (base == 16) {
767 PCHAR('0');
768 PCHAR('x');
769 }
770 }
a0a36cfd
SW
771 if (!ladjust && width && (width -= tmp) > 0)
772 while (width--)
773 PCHAR(padc);
984263bc
MD
774
775 while (*p)
776 PCHAR(*p--);
777
778 if (ladjust && width && (width -= tmp) > 0)
779 while (width--)
780 PCHAR(padc);
781
782 break;
783 default:
a0a36cfd
SW
784 while (percent < fmt)
785 PCHAR(*percent++);
786 /*
787 * Since we ignore an formatting argument it is no
788 * longer safe to obey the remaining formatting
789 * arguments as the arguments will no longer match
790 * the format specs.
791 */
792 stop = 1;
984263bc
MD
793 break;
794 }
795 }
796#undef PCHAR
797}
798
799/*
800 * Put character in log buffer with a particular priority.
e02941a7
MD
801 *
802 * MPSAFE
984263bc
MD
803 */
804static void
805msglogchar(int c, int pri)
806{
807 static int lastpri = -1;
808 static int dangling;
809 char nbuf[MAXNBUF];
810 char *p;
811
812 if (!msgbufmapped)
813 return;
814 if (c == '\0' || c == '\r')
815 return;
816 if (pri != -1 && pri != lastpri) {
817 if (dangling) {
818 msgaddchar('\n', NULL);
819 dangling = 0;
820 }
821 msgaddchar('<', NULL);
a0a36cfd 822 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
984263bc
MD
823 msgaddchar(*p--, NULL);
824 msgaddchar('>', NULL);
825 lastpri = pri;
826 }
827 msgaddchar(c, NULL);
828 if (c == '\n') {
829 dangling = 0;
830 lastpri = -1;
831 } else {
832 dangling = 1;
833 }
834}
835
836/*
e02941a7
MD
837 * Put char in log buffer. Make sure nothing blows up beyond repair if
838 * we have an MP race.
839 *
840 * MPSAFE.
984263bc
MD
841 */
842static void
843msgaddchar(int c, void *dummy)
844{
845 struct msgbuf *mbp;
e02941a7
MD
846 int rindex;
847 int windex;
984263bc
MD
848
849 if (!msgbufmapped)
850 return;
851 mbp = msgbufp;
e02941a7
MD
852 windex = mbp->msg_bufx;
853 mbp->msg_ptr[windex] = c;
854 if (++windex >= mbp->msg_size)
855 windex = 0;
856 rindex = mbp->msg_bufr;
857 if (windex == rindex) {
858 rindex += 32;
859 if (rindex >= mbp->msg_size)
860 rindex -= mbp->msg_size;
861 mbp->msg_bufr = rindex;
984263bc 862 }
e02941a7 863 mbp->msg_bufx = windex;
984263bc
MD
864}
865
866static void
867msgbufcopy(struct msgbuf *oldp)
868{
869 int pos;
870
871 pos = oldp->msg_bufr;
872 while (pos != oldp->msg_bufx) {
873 msglogchar(oldp->msg_ptr[pos], -1);
874 if (++pos >= oldp->msg_size)
875 pos = 0;
876 }
877}
878
879void
880msgbufinit(void *ptr, size_t size)
881{
882 char *cp;
883 static struct msgbuf *oldp = NULL;
884
885 size -= sizeof(*msgbufp);
886 cp = (char *)ptr;
887 msgbufp = (struct msgbuf *) (cp + size);
888 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
889 msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) {
890 bzero(cp, size);
891 bzero(msgbufp, sizeof(*msgbufp));
892 msgbufp->msg_magic = MSG_MAGIC;
893 msgbufp->msg_size = (char *)msgbufp - cp;
894 }
895 msgbufp->msg_ptr = cp;
896 if (msgbufmapped && oldp != msgbufp)
897 msgbufcopy(oldp);
898 msgbufmapped = 1;
899 oldp = msgbufp;
900}
901
902/* Sysctls for accessing/clearing the msgbuf */
d41cbec3 903
984263bc
MD
904static int
905sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
906{
d41cbec3 907 struct ucred *cred;
984263bc
MD
908 int error;
909
910 /*
d41cbec3
MD
911 * Only wheel or root can access the message log.
912 */
913 if (unprivileged_read_msgbuf == 0) {
914 KKASSERT(req->td->td_proc);
915 cred = req->td->td_proc->p_ucred;
916
917 if ((cred->cr_prison || groupmember(0, cred) == 0) &&
895c1f85 918 priv_check(req->td, PRIV_ROOT) != 0
d41cbec3
MD
919 ) {
920 return (EPERM);
921 }
922 }
923
924 /*
984263bc
MD
925 * Unwind the buffer, so that it's linear (possibly starting with
926 * some initial nulls).
927 */
928 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
929 msgbufp->msg_size - msgbufp->msg_bufx, req);
930 if (error)
931 return (error);
932 if (msgbufp->msg_bufx > 0) {
933 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
934 msgbufp->msg_bufx, req);
935 }
936 return (error);
937}
938
939SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
940 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
941
942static int msgbuf_clear;
943
944static int
945sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
946{
947 int error;
948 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
949 if (!error && req->newptr) {
950 /* Clear the buffer and reset write pointer */
951 bzero(msgbufp->msg_ptr, msgbufp->msg_size);
952 msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
953 msgbuf_clear = 0;
954 }
955 return (error);
956}
957
958SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
a0a36cfd 959 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
984263bc
MD
960 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
961
984263bc 962#ifdef DDB
984263bc
MD
963
964DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
965{
966 int i, j;
967
968 if (!msgbufmapped) {
969 db_printf("msgbuf not mapped yet\n");
970 return;
971 }
972 db_printf("msgbufp = %p\n", msgbufp);
973 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
974 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
975 msgbufp->msg_bufx, msgbufp->msg_ptr);
976 for (i = 0; i < msgbufp->msg_size; i++) {
977 j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
978 db_printf("%c", msgbufp->msg_ptr[j]);
979 }
980 db_printf("\n");
981}
982
983#endif /* DDB */