proc->thread stage 2: MAJOR revamping of system calls, ucred, jail API,
[dragonfly.git] / sys / kern / subr_prf.c
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 $
40  * $DragonFly: src/sys/kern/subr_prf.c,v 1.2 2003/06/17 04:28:41 dillon Exp $
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/msgbuf.h>
47 #include <sys/malloc.h>
48 #include <sys/proc.h>
49 #include <sys/tty.h>
50 #include <sys/tprintf.h>
51 #include <sys/syslog.h>
52 #include <sys/cons.h>
53 #include <sys/uio.h>
54 #include <sys/sysctl.h>
55
56 /*
57  * Note that stdarg.h and the ANSI style va_start macro is used for both
58  * ANSI and traditional C compilers.
59  */
60 #include <machine/stdarg.h>
61
62 #define TOCONS  0x01
63 #define TOTTY   0x02
64 #define TOLOG   0x04
65
66 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
67 #define MAXNBUF (sizeof(quad_t) * NBBY + 1)
68
69 struct putchar_arg {
70         int     flags;
71         int     pri;
72         struct  tty *tty;
73 };
74
75 struct snprintf_arg {
76         char    *str;
77         size_t  remain;
78 };
79
80 extern  int log_open;
81
82 struct  tty *constty;                   /* pointer to console "window" tty */
83
84 static void (*v_putc)(int) = cnputc;    /* routine to putc on virtual console */
85 static void  msglogchar(int c, int pri);
86 static void  msgaddchar(int c, void *dummy);
87 static void  putchar __P((int ch, void *arg));
88 static char *ksprintn __P((char *nbuf, u_long num, int base, int *len));
89 static char *ksprintqn __P((char *nbuf, u_quad_t num, int base, int *len));
90 static void  snprintf_func __P((int ch, void *arg));
91
92 static int consintr = 1;                /* Ok to handle console interrupts? */
93 static int msgbufmapped;                /* Set when safe to use msgbuf */
94 int msgbuftrigger;
95
96 static int      log_console_output = 1;
97 TUNABLE_INT("kern.log_console_output", &log_console_output);
98 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
99     &log_console_output, 0, "");
100
101 /*
102  * Warn that a system table is full.
103  */
104 void
105 tablefull(const char *tab)
106 {
107
108         log(LOG_ERR, "%s: table is full\n", tab);
109 }
110
111 /*
112  * Uprintf prints to the controlling terminal for the current process.
113  * It may block if the tty queue is overfull.  No message is printed if
114  * the queue does not clear in a reasonable time.
115  */
116 int
117 uprintf(const char *fmt, ...)
118 {
119         struct proc *p = curproc;
120         va_list ap;
121         struct putchar_arg pca;
122         int retval = 0;
123
124         if (p && p->p_flag & P_CONTROLT &&
125             p->p_session->s_ttyvp) {
126                 va_start(ap, fmt);
127                 pca.tty = p->p_session->s_ttyp;
128                 pca.flags = TOTTY;
129                 retval = kvprintf(fmt, putchar, &pca, 10, ap);
130                 va_end(ap);
131         }
132         return retval;
133 }
134
135 tpr_t
136 tprintf_open(p)
137         register struct proc *p;
138 {
139
140         if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
141                 SESSHOLD(p->p_session);
142                 return ((tpr_t) p->p_session);
143         }
144         return ((tpr_t) NULL);
145 }
146
147 void
148 tprintf_close(sess)
149         tpr_t sess;
150 {
151
152         if (sess)
153                 SESSRELE((struct session *) sess);
154 }
155
156 /*
157  * tprintf prints on the controlling terminal associated
158  * with the given session.
159  */
160 int
161 tprintf(tpr_t tpr, const char *fmt, ...)
162 {
163         register struct session *sess = (struct session *)tpr;
164         struct tty *tp = NULL;
165         int flags = TOLOG;
166         va_list ap;
167         struct putchar_arg pca;
168         int retval;
169
170         if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
171                 flags |= TOTTY;
172                 tp = sess->s_ttyp;
173         }
174         va_start(ap, fmt);
175         pca.tty = tp;
176         pca.flags = flags;
177         pca.pri = LOG_INFO;
178         retval = kvprintf(fmt, putchar, &pca, 10, ap);
179         va_end(ap);
180         msgbuftrigger = 1;
181         return retval;
182 }
183
184 /*
185  * Ttyprintf displays a message on a tty; it should be used only by
186  * the tty driver, or anything that knows the underlying tty will not
187  * be revoke(2)'d away.  Other callers should use tprintf.
188  */
189 int
190 ttyprintf(struct tty *tp, const char *fmt, ...)
191 {
192         va_list ap;
193         struct putchar_arg pca;
194         int retval;
195
196         va_start(ap, fmt);
197         pca.tty = tp;
198         pca.flags = TOTTY;
199         retval = kvprintf(fmt, putchar, &pca, 10, ap);
200         va_end(ap);
201         return retval;
202 }
203
204 /*
205  * Log writes to the log buffer, and guarantees not to sleep (so can be
206  * called by interrupt routines).  If there is no process reading the
207  * log yet, it writes to the console also.
208  */
209 int
210 log(int level, const char *fmt, ...)
211 {
212         va_list ap;
213         int retval;
214         struct putchar_arg pca;
215
216         pca.tty = NULL;
217         pca.pri = level;
218         pca.flags = log_open ? TOLOG : TOCONS;
219
220         va_start(ap, fmt);
221         retval = kvprintf(fmt, putchar, &pca, 10, ap);
222         va_end(ap);
223
224         msgbuftrigger = 1;
225         return (retval);
226 }
227
228 int
229 addlog(const char *fmt, ...)
230 {
231         va_list ap;
232         int retval;
233         struct putchar_arg pca;
234
235         pca.tty = NULL;
236         pca.pri = -1;
237         pca.flags = log_open ? TOLOG : TOCONS;
238
239         va_start(ap, fmt);
240         retval = kvprintf(fmt, putchar, &pca, 10, ap);
241         va_end(ap);
242
243         msgbuftrigger = 1;
244         return (retval);
245 }
246
247 #define CONSCHUNK 128
248
249 void
250 log_console(struct uio *uio)
251 {
252         int c, i, error, iovlen, nl;
253         struct uio muio;
254         struct iovec *miov = NULL;
255         char *consbuffer;
256         int pri;
257
258         if (!log_console_output)
259                 return;
260
261         pri = LOG_INFO | LOG_CONSOLE;
262         muio = *uio;
263         iovlen = uio->uio_iovcnt * sizeof (struct iovec);
264         MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
265         MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
266         bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen);
267         muio.uio_iov = miov;
268         uio = &muio;
269
270         nl = 0;
271         while (uio->uio_resid > 0) {
272                 c = imin(uio->uio_resid, CONSCHUNK);
273                 error = uiomove(consbuffer, c, uio);
274                 if (error != 0)
275                         return;
276                 for (i = 0; i < c; i++) {
277                         msglogchar(consbuffer[i], pri);
278                         if (consbuffer[i] == '\n')
279                                 nl = 1;
280                         else
281                                 nl = 0;
282                 }
283         }
284         if (!nl)
285                 msglogchar('\n', pri);
286         msgbuftrigger = 1;
287         FREE(miov, M_TEMP);
288         FREE(consbuffer, M_TEMP);
289         return;
290 }
291
292 int
293 printf(const char *fmt, ...)
294 {
295         va_list ap;
296         int savintr;
297         struct putchar_arg pca;
298         int retval;
299
300         savintr = consintr;             /* disable interrupts */
301         consintr = 0;
302         va_start(ap, fmt);
303         pca.tty = NULL;
304         pca.flags = TOCONS | TOLOG;
305         pca.pri = -1;
306         retval = kvprintf(fmt, putchar, &pca, 10, ap);
307         va_end(ap);
308         if (!panicstr)
309                 msgbuftrigger = 1;
310         consintr = savintr;             /* reenable interrupts */
311         return retval;
312 }
313
314 int
315 vprintf(const char *fmt, va_list ap)
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;
326         retval = kvprintf(fmt, putchar, &pca, 10, ap);
327         if (!panicstr)
328                 msgbuftrigger = 1;
329         consintr = savintr;             /* reenable interrupts */
330         return retval;
331 }
332
333 /*
334  * Print a character on console or users terminal.  If destination is
335  * the console then the last bunch of characters are saved in msgbuf for
336  * inspection later.
337  */
338 static void
339 putchar(int c, void *arg)
340 {
341         struct putchar_arg *ap = (struct putchar_arg*) arg;
342         int flags = ap->flags;
343         struct tty *tp = ap->tty;
344         if (panicstr)
345                 constty = NULL;
346         if ((flags & TOCONS) && tp == NULL && constty) {
347                 tp = constty;
348                 flags |= TOTTY;
349         }
350         if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
351             (flags & TOCONS) && tp == constty)
352                 constty = NULL;
353         if ((flags & TOLOG))
354                 msglogchar(c, ap->pri);
355         if ((flags & TOCONS) && constty == NULL && c != '\0')
356                 (*v_putc)(c);
357 }
358
359 /*
360  * Scaled down version of sprintf(3).
361  */
362 int
363 sprintf(char *buf, const char *cfmt, ...)
364 {
365         int retval;
366         va_list ap;
367
368         va_start(ap, cfmt);
369         retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
370         buf[retval] = '\0';
371         va_end(ap);
372         return retval;
373 }
374
375 /*
376  * Scaled down version of vsprintf(3).
377  */
378 int
379 vsprintf(char *buf, const char *cfmt, va_list ap)
380 {
381         int retval;
382
383         retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
384         buf[retval] = '\0';
385         return retval;
386 }
387
388 /*
389  * Scaled down version of snprintf(3).
390  */
391 int
392 snprintf(char *str, size_t size, const char *format, ...)
393 {
394         int retval;
395         va_list ap;
396
397         va_start(ap, format);
398         retval = vsnprintf(str, size, format, ap);
399         va_end(ap);
400         return(retval);
401 }
402
403 /*
404  * Scaled down version of vsnprintf(3).
405  */
406 int
407 vsnprintf(char *str, size_t size, const char *format, va_list ap)
408 {
409         struct snprintf_arg info;
410         int retval;
411
412         info.str = str;
413         info.remain = size;
414         retval = kvprintf(format, snprintf_func, &info, 10, ap);
415         if (info.remain >= 1)
416                 *info.str++ = '\0';
417         return retval;
418 }
419
420 static void
421 snprintf_func(int ch, void *arg)
422 {
423         struct snprintf_arg *const info = arg;
424
425         if (info->remain >= 2) {
426                 *info->str++ = ch;
427                 info->remain--;
428         }
429 }
430
431 /*
432  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
433  * order; return an optional length and a pointer to the last character
434  * written in the buffer (i.e., the first character of the string).
435  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
436  */
437 static char *
438 ksprintn(nbuf, ul, base, lenp)
439         char *nbuf;
440         u_long ul;
441         int base, *lenp;
442 {
443         char *p;
444
445         p = nbuf;
446         *p = '\0';
447         do {
448                 *++p = hex2ascii(ul % base);
449         } while (ul /= base);
450         if (lenp)
451                 *lenp = p - nbuf;
452         return (p);
453 }
454 /* ksprintn, but for a quad_t. */
455 static char *
456 ksprintqn(nbuf, uq, base, lenp)
457         char *nbuf;
458         u_quad_t uq;
459         int base, *lenp;
460 {
461         char *p;
462
463         p = nbuf;
464         *p = '\0';
465         do {
466                 *++p = hex2ascii(uq % base);
467         } while (uq /= base);
468         if (lenp)
469                 *lenp = p - nbuf;
470         return (p);
471 }
472
473 /*
474  * Scaled down version of printf(3).
475  *
476  * Two additional formats:
477  *
478  * The format %b is supported to decode error registers.
479  * Its usage is:
480  *
481  *      printf("reg=%b\n", regval, "<base><arg>*");
482  *
483  * where <base> is the output base expressed as a control character, e.g.
484  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
485  * the first of which gives the bit number to be inspected (origin 1), and
486  * the next characters (up to a control character, i.e. a character <= 32),
487  * give the name of the register.  Thus:
488  *
489  *      kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
490  *
491  * would produce output:
492  *
493  *      reg=3<BITTWO,BITONE>
494  *
495  * XXX:  %D  -- Hexdump, takes pointer and separator string:
496  *              ("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
497  *              ("%*D", len, ptr, " " -> XX XX XX XX ...
498  */
499 int
500 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
501 {
502 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
503         char nbuf[MAXNBUF];
504         char *p, *q, *d;
505         u_char *up;
506         int ch, n;
507         u_long ul;
508         u_quad_t uq;
509         int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
510         int dwidth;
511         char padc;
512         int retval = 0;
513
514         ul = 0;
515         uq = 0;
516         if (!func)
517                 d = (char *) arg;
518         else
519                 d = NULL;
520
521         if (fmt == NULL)
522                 fmt = "(fmt null)\n";
523
524         if (radix < 2 || radix > 36)
525                 radix = 10;
526
527         for (;;) {
528                 padc = ' ';
529                 width = 0;
530                 while ((ch = (u_char)*fmt++) != '%') {
531                         if (ch == '\0') 
532                                 return retval;
533                         PCHAR(ch);
534                 }
535                 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
536                 sign = 0; dot = 0; dwidth = 0;
537 reswitch:       switch (ch = (u_char)*fmt++) {
538                 case '.':
539                         dot = 1;
540                         goto reswitch;
541                 case '#':
542                         sharpflag = 1;
543                         goto reswitch;
544                 case '+':
545                         sign = 1;
546                         goto reswitch;
547                 case '-':
548                         ladjust = 1;
549                         goto reswitch;
550                 case '%':
551                         PCHAR(ch);
552                         break;
553                 case '*':
554                         if (!dot) {
555                                 width = va_arg(ap, int);
556                                 if (width < 0) {
557                                         ladjust = !ladjust;
558                                         width = -width;
559                                 }
560                         } else {
561                                 dwidth = va_arg(ap, int);
562                         }
563                         goto reswitch;
564                 case '0':
565                         if (!dot) {
566                                 padc = '0';
567                                 goto reswitch;
568                         }
569                 case '1': case '2': case '3': case '4':
570                 case '5': case '6': case '7': case '8': case '9':
571                                 for (n = 0;; ++fmt) {
572                                         n = n * 10 + ch - '0';
573                                         ch = *fmt;
574                                         if (ch < '0' || ch > '9')
575                                                 break;
576                                 }
577                         if (dot)
578                                 dwidth = n;
579                         else
580                                 width = n;
581                         goto reswitch;
582                 case 'b':
583                         ul = va_arg(ap, int);
584                         p = va_arg(ap, char *);
585                         for (q = ksprintn(nbuf, ul, *p++, NULL); *q;)
586                                 PCHAR(*q--);
587
588                         if (!ul)
589                                 break;
590
591                         for (tmp = 0; *p;) {
592                                 n = *p++;
593                                 if (ul & (1 << (n - 1))) {
594                                         PCHAR(tmp ? ',' : '<');
595                                         for (; (n = *p) > ' '; ++p)
596                                                 PCHAR(n);
597                                         tmp = 1;
598                                 } else
599                                         for (; *p > ' '; ++p)
600                                                 continue;
601                         }
602                         if (tmp)
603                                 PCHAR('>');
604                         break;
605                 case 'c':
606                         PCHAR(va_arg(ap, int));
607                         break;
608                 case 'D':
609                         up = va_arg(ap, u_char *);
610                         p = va_arg(ap, char *);
611                         if (!width)
612                                 width = 16;
613                         while(width--) {
614                                 PCHAR(hex2ascii(*up >> 4));
615                                 PCHAR(hex2ascii(*up & 0x0f));
616                                 up++;
617                                 if (width)
618                                         for (q=p;*q;q++)
619                                                 PCHAR(*q);
620                         }
621                         break;
622                 case 'd':
623                         if (qflag)
624                                 uq = va_arg(ap, quad_t);
625                         else if (lflag)
626                                 ul = va_arg(ap, long);
627                         else
628                                 ul = va_arg(ap, int);
629                         sign = 1;
630                         base = 10;
631                         goto number;
632                 case 'l':
633                         if (lflag) {
634                                 lflag = 0;
635                                 qflag = 1;
636                         } else
637                                 lflag = 1;
638                         goto reswitch;
639                 case 'o':
640                         if (qflag)
641                                 uq = va_arg(ap, u_quad_t);
642                         else if (lflag)
643                                 ul = va_arg(ap, u_long);
644                         else
645                                 ul = va_arg(ap, u_int);
646                         base = 8;
647                         goto nosign;
648                 case 'p':
649                         ul = (uintptr_t)va_arg(ap, void *);
650                         base = 16;
651                         sharpflag = (width == 0);
652                         goto nosign;
653                 case 'q':
654                         qflag = 1;
655                         goto reswitch;
656                 case 'n':
657                 case 'r':
658                         if (qflag)
659                                 uq = va_arg(ap, u_quad_t);
660                         else if (lflag)
661                                 ul = va_arg(ap, u_long);
662                         else
663                                 ul = sign ?
664                                     (u_long)va_arg(ap, int) : va_arg(ap, u_int);
665                         base = radix;
666                         goto number;
667                 case 's':
668                         p = va_arg(ap, char *);
669                         if (p == NULL)
670                                 p = "(null)";
671                         if (!dot)
672                                 n = strlen (p);
673                         else
674                                 for (n = 0; n < dwidth && p[n]; n++)
675                                         continue;
676
677                         width -= n;
678
679                         if (!ladjust && width > 0)
680                                 while (width--)
681                                         PCHAR(padc);
682                         while (n--)
683                                 PCHAR(*p++);
684                         if (ladjust && width > 0)
685                                 while (width--)
686                                         PCHAR(padc);
687                         break;
688                 case 'u':
689                         if (qflag)
690                                 uq = va_arg(ap, u_quad_t);
691                         else if (lflag)
692                                 ul = va_arg(ap, u_long);
693                         else
694                                 ul = va_arg(ap, u_int);
695                         base = 10;
696                         goto nosign;
697                 case 'x':
698                 case 'X':
699                         if (qflag)
700                                 uq = va_arg(ap, u_quad_t);
701                         else if (lflag)
702                                 ul = va_arg(ap, u_long);
703                         else
704                                 ul = va_arg(ap, u_int);
705                         base = 16;
706                         goto nosign;
707                 case 'z':
708                         if (qflag)
709                                 uq = va_arg(ap, u_quad_t);
710                         else if (lflag)
711                                 ul = va_arg(ap, u_long);
712                         else
713                                 ul = sign ?
714                                     (u_long)va_arg(ap, int) : va_arg(ap, u_int);
715                         base = 16;
716                         goto number;
717 nosign:                 sign = 0;
718 number:                 
719                         if (qflag) {
720                                 if (sign && (quad_t)uq < 0) {
721                                         neg = 1;
722                                         uq = -(quad_t)uq;
723                                 }
724                                 p = ksprintqn(nbuf, uq, base, &tmp);
725                         } else {
726                                 if (sign && (long)ul < 0) {
727                                         neg = 1;
728                                         ul = -(long)ul;
729                                 }
730                                 p = ksprintn(nbuf, ul, base, &tmp);
731                         }
732                         if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
733                                 if (base == 8)
734                                         tmp++;
735                                 else if (base == 16)
736                                         tmp += 2;
737                         }
738                         if (neg)
739                                 tmp++;
740
741                         if (!ladjust && width && (width -= tmp) > 0)
742                                 while (width--)
743                                         PCHAR(padc);
744                         if (neg)
745                                 PCHAR('-');
746                         if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
747                                 if (base == 8) {
748                                         PCHAR('0');
749                                 } else if (base == 16) {
750                                         PCHAR('0');
751                                         PCHAR('x');
752                                 }
753                         }
754
755                         while (*p)
756                                 PCHAR(*p--);
757
758                         if (ladjust && width && (width -= tmp) > 0)
759                                 while (width--)
760                                         PCHAR(padc);
761
762                         break;
763                 default:
764                         PCHAR('%');
765                         if (lflag)
766                                 PCHAR('l');
767                         PCHAR(ch);
768                         break;
769                 }
770         }
771 #undef PCHAR
772 }
773
774 /*
775  * Put character in log buffer with a particular priority.
776  */
777 static void
778 msglogchar(int c, int pri)
779 {
780         static int lastpri = -1;
781         static int dangling;
782         char nbuf[MAXNBUF];
783         char *p;
784
785         if (!msgbufmapped)
786                 return;
787         if (c == '\0' || c == '\r')
788                 return;
789         if (pri != -1 && pri != lastpri) {
790                 if (dangling) {
791                         msgaddchar('\n', NULL);
792                         dangling = 0;
793                 }
794                 msgaddchar('<', NULL);
795                 for (p = ksprintn(nbuf, (u_long)pri, 10, NULL); *p;)
796                         msgaddchar(*p--, NULL);
797                 msgaddchar('>', NULL);
798                 lastpri = pri;
799         }
800         msgaddchar(c, NULL);
801         if (c == '\n') {
802                 dangling = 0;
803                 lastpri = -1;
804         } else {
805                 dangling = 1;
806         }
807 }
808
809 /*
810  * Put char in log buffer
811  */
812 static void
813 msgaddchar(int c, void *dummy)
814 {
815         struct msgbuf *mbp;
816
817         if (!msgbufmapped)
818                 return;
819         mbp = msgbufp;
820         mbp->msg_ptr[mbp->msg_bufx++] = c;
821         if (mbp->msg_bufx >= mbp->msg_size)
822                 mbp->msg_bufx = 0;
823         /* If the buffer is full, keep the most recent data. */
824         if (mbp->msg_bufr == mbp->msg_bufx) {
825                 if (++mbp->msg_bufr >= mbp->msg_size)
826                         mbp->msg_bufr = 0;
827         }
828 }
829
830 static void
831 msgbufcopy(struct msgbuf *oldp)
832 {
833         int pos;
834
835         pos = oldp->msg_bufr;
836         while (pos != oldp->msg_bufx) {
837                 msglogchar(oldp->msg_ptr[pos], -1);
838                 if (++pos >= oldp->msg_size)
839                         pos = 0;
840         }
841 }
842
843 void
844 msgbufinit(void *ptr, size_t size)
845 {
846         char *cp;
847         static struct msgbuf *oldp = NULL;
848
849         size -= sizeof(*msgbufp);
850         cp = (char *)ptr;
851         msgbufp = (struct msgbuf *) (cp + size);
852         if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
853             msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) {
854                 bzero(cp, size);
855                 bzero(msgbufp, sizeof(*msgbufp));
856                 msgbufp->msg_magic = MSG_MAGIC;
857                 msgbufp->msg_size = (char *)msgbufp - cp;
858         }
859         msgbufp->msg_ptr = cp;
860         if (msgbufmapped && oldp != msgbufp)
861                 msgbufcopy(oldp);
862         msgbufmapped = 1;
863         oldp = msgbufp;
864 }
865
866 /* Sysctls for accessing/clearing the msgbuf */
867 static int
868 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
869 {
870         int error;
871
872         /*
873          * Unwind the buffer, so that it's linear (possibly starting with
874          * some initial nulls).
875          */
876         error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
877             msgbufp->msg_size - msgbufp->msg_bufx, req);
878         if (error)
879                 return (error);
880         if (msgbufp->msg_bufx > 0) {
881                 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
882                     msgbufp->msg_bufx, req);
883         }
884         return (error);
885 }
886
887 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
888     0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
889
890 static int msgbuf_clear;
891
892 static int
893 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
894 {
895         int error;
896         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
897         if (!error && req->newptr) {
898                 /* Clear the buffer and reset write pointer */
899                 bzero(msgbufp->msg_ptr, msgbufp->msg_size);
900                 msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
901                 msgbuf_clear = 0;
902         }
903         return (error);
904 }
905
906 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
907     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0, 
908     sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
909
910 #include "opt_ddb.h"
911 #ifdef DDB
912 #include <ddb/ddb.h>
913
914 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
915 {
916         int i, j;
917
918         if (!msgbufmapped) {
919                 db_printf("msgbuf not mapped yet\n");
920                 return;
921         }
922         db_printf("msgbufp = %p\n", msgbufp);
923         db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
924             msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
925             msgbufp->msg_bufx, msgbufp->msg_ptr);
926         for (i = 0; i < msgbufp->msg_size; i++) {
927                 j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
928                 db_printf("%c", msgbufp->msg_ptr[j]);
929         }
930         db_printf("\n");
931 }
932
933 #endif /* DDB */