The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_prf.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      @(#)subr_prf.c  8.3 (Berkeley) 1/21/94
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD: releng/9.0/sys/kern/subr_prf.c 222804 2011-06-07 05:04:37Z ken $");
   39 
   40 #include "opt_ddb.h"
   41 #include "opt_printf.h"
   42 
   43 #include <sys/param.h>
   44 #include <sys/systm.h>
   45 #include <sys/lock.h>
   46 #include <sys/kdb.h>
   47 #include <sys/mutex.h>
   48 #include <sys/sx.h>
   49 #include <sys/kernel.h>
   50 #include <sys/msgbuf.h>
   51 #include <sys/malloc.h>
   52 #include <sys/priv.h>
   53 #include <sys/proc.h>
   54 #include <sys/stddef.h>
   55 #include <sys/sysctl.h>
   56 #include <sys/tty.h>
   57 #include <sys/syslog.h>
   58 #include <sys/cons.h>
   59 #include <sys/uio.h>
   60 #include <sys/ctype.h>
   61 
   62 #ifdef DDB
   63 #include <ddb/ddb.h>
   64 #endif
   65 
   66 /*
   67  * Note that stdarg.h and the ANSI style va_start macro is used for both
   68  * ANSI and traditional C compilers.
   69  */
   70 #include <machine/stdarg.h>
   71 
   72 #define TOCONS  0x01
   73 #define TOTTY   0x02
   74 #define TOLOG   0x04
   75 
   76 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
   77 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
   78 
   79 struct putchar_arg {
   80         int     flags;
   81         int     pri;
   82         struct  tty *tty;
   83         char    *p_bufr;
   84         size_t  n_bufr;
   85         char    *p_next;
   86         size_t  remain;
   87 };
   88 
   89 struct snprintf_arg {
   90         char    *str;
   91         size_t  remain;
   92 };
   93 
   94 extern  int log_open;
   95 
   96 static void  msglogchar(int c, int pri);
   97 static void  msglogstr(char *str, int pri, int filter_cr);
   98 static void  putchar(int ch, void *arg);
   99 static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len, int upper);
  100 static void  snprintf_func(int ch, void *arg);
  101 
  102 static int msgbufmapped;                /* Set when safe to use msgbuf */
  103 int msgbuftrigger;
  104 
  105 static int      log_console_output = 1;
  106 TUNABLE_INT("kern.log_console_output", &log_console_output);
  107 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
  108     &log_console_output, 0, "Duplicate console output to the syslog.");
  109 
  110 /*
  111  * See the comment in log_console() below for more explanation of this.
  112  */
  113 static int log_console_add_linefeed = 0;
  114 TUNABLE_INT("kern.log_console_add_linefeed", &log_console_add_linefeed);
  115 SYSCTL_INT(_kern, OID_AUTO, log_console_add_linefeed, CTLFLAG_RW,
  116     &log_console_add_linefeed, 0, "log_console() adds extra newlines.");
  117 
  118 static int      always_console_output = 0;
  119 TUNABLE_INT("kern.always_console_output", &always_console_output);
  120 SYSCTL_INT(_kern, OID_AUTO, always_console_output, CTLFLAG_RW,
  121     &always_console_output, 0, "Always output to console despite TIOCCONS.");
  122 
  123 /*
  124  * Warn that a system table is full.
  125  */
  126 void
  127 tablefull(const char *tab)
  128 {
  129 
  130         log(LOG_ERR, "%s: table is full\n", tab);
  131 }
  132 
  133 /*
  134  * Uprintf prints to the controlling terminal for the current process.
  135  */
  136 int
  137 uprintf(const char *fmt, ...)
  138 {
  139         va_list ap;
  140         struct putchar_arg pca;
  141         struct proc *p;
  142         struct thread *td;
  143         int retval;
  144 
  145         td = curthread;
  146         if (TD_IS_IDLETHREAD(td))
  147                 return (0);
  148 
  149         sx_slock(&proctree_lock);
  150         p = td->td_proc;
  151         PROC_LOCK(p);
  152         if ((p->p_flag & P_CONTROLT) == 0) {
  153                 PROC_UNLOCK(p);
  154                 retval = 0;
  155                 goto out;
  156         }
  157         SESS_LOCK(p->p_session);
  158         pca.tty = p->p_session->s_ttyp;
  159         SESS_UNLOCK(p->p_session);
  160         PROC_UNLOCK(p);
  161         if (pca.tty == NULL) {
  162                 retval = 0;
  163                 goto out;
  164         }
  165         pca.flags = TOTTY;
  166         pca.p_bufr = NULL;
  167         va_start(ap, fmt);
  168         tty_lock(pca.tty);
  169         retval = kvprintf(fmt, putchar, &pca, 10, ap);
  170         tty_unlock(pca.tty);
  171         va_end(ap);
  172 out:
  173         sx_sunlock(&proctree_lock);
  174         return (retval);
  175 }
  176 
  177 /*
  178  * tprintf prints on the controlling terminal associated with the given
  179  * session, possibly to the log as well.
  180  */
  181 void
  182 tprintf(struct proc *p, int pri, const char *fmt, ...)
  183 {
  184         struct tty *tp = NULL;
  185         int flags = 0;
  186         va_list ap;
  187         struct putchar_arg pca;
  188         struct session *sess = NULL;
  189 
  190         sx_slock(&proctree_lock);
  191         if (pri != -1)
  192                 flags |= TOLOG;
  193         if (p != NULL) {
  194                 PROC_LOCK(p);
  195                 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
  196                         sess = p->p_session;
  197                         sess_hold(sess);
  198                         PROC_UNLOCK(p);
  199                         tp = sess->s_ttyp;
  200                         if (tp != NULL && tty_checkoutq(tp))
  201                                 flags |= TOTTY;
  202                         else
  203                                 tp = NULL;
  204                 } else
  205                         PROC_UNLOCK(p);
  206         }
  207         pca.pri = pri;
  208         pca.tty = tp;
  209         pca.flags = flags;
  210         pca.p_bufr = NULL;
  211         va_start(ap, fmt);
  212         if (pca.tty != NULL)
  213                 tty_lock(pca.tty);
  214         kvprintf(fmt, putchar, &pca, 10, ap);
  215         if (pca.tty != NULL)
  216                 tty_unlock(pca.tty);
  217         va_end(ap);
  218         if (sess != NULL)
  219                 sess_release(sess);
  220         msgbuftrigger = 1;
  221         sx_sunlock(&proctree_lock);
  222 }
  223 
  224 /*
  225  * Ttyprintf displays a message on a tty; it should be used only by
  226  * the tty driver, or anything that knows the underlying tty will not
  227  * be revoke(2)'d away.  Other callers should use tprintf.
  228  */
  229 int
  230 ttyprintf(struct tty *tp, const char *fmt, ...)
  231 {
  232         va_list ap;
  233         struct putchar_arg pca;
  234         int retval;
  235 
  236         va_start(ap, fmt);
  237         pca.tty = tp;
  238         pca.flags = TOTTY;
  239         pca.p_bufr = NULL;
  240         retval = kvprintf(fmt, putchar, &pca, 10, ap);
  241         va_end(ap);
  242         return (retval);
  243 }
  244 
  245 /*
  246  * Log writes to the log buffer, and guarantees not to sleep (so can be
  247  * called by interrupt routines).  If there is no process reading the
  248  * log yet, it writes to the console also.
  249  */
  250 void
  251 log(int level, const char *fmt, ...)
  252 {
  253         va_list ap;
  254         struct putchar_arg pca;
  255 #ifdef PRINTF_BUFR_SIZE
  256         char bufr[PRINTF_BUFR_SIZE];
  257 #endif
  258 
  259         pca.tty = NULL;
  260         pca.pri = level;
  261         pca.flags = log_open ? TOLOG : TOCONS;
  262 #ifdef PRINTF_BUFR_SIZE
  263         pca.p_bufr = bufr;
  264         pca.p_next = pca.p_bufr;
  265         pca.n_bufr = sizeof(bufr);
  266         pca.remain = sizeof(bufr);
  267         *pca.p_next = '\0';
  268 #else
  269         pca.p_bufr = NULL;
  270 #endif
  271 
  272         va_start(ap, fmt);
  273         kvprintf(fmt, putchar, &pca, 10, ap);
  274         va_end(ap);
  275 
  276 #ifdef PRINTF_BUFR_SIZE
  277         /* Write any buffered console/log output: */
  278         if (*pca.p_bufr != '\0') {
  279                 if (pca.flags & TOLOG)
  280                         msglogstr(pca.p_bufr, level, /*filter_cr*/1);
  281 
  282                 if (pca.flags & TOCONS)
  283                         cnputs(pca.p_bufr);
  284         }
  285 #endif
  286         msgbuftrigger = 1;
  287 }
  288 
  289 #define CONSCHUNK 128
  290 
  291 void
  292 log_console(struct uio *uio)
  293 {
  294         int c, error, nl;
  295         char *consbuffer;
  296         int pri;
  297 
  298         if (!log_console_output)
  299                 return;
  300 
  301         pri = LOG_INFO | LOG_CONSOLE;
  302         uio = cloneuio(uio);
  303         consbuffer = malloc(CONSCHUNK, M_TEMP, M_WAITOK);
  304 
  305         nl = 0;
  306         while (uio->uio_resid > 0) {
  307                 c = imin(uio->uio_resid, CONSCHUNK - 1);
  308                 error = uiomove(consbuffer, c, uio);
  309                 if (error != 0)
  310                         break;
  311                 /* Make sure we're NUL-terminated */
  312                 consbuffer[c] = '\0';
  313                 if (consbuffer[c - 1] == '\n')
  314                         nl = 1;
  315                 else
  316                         nl = 0;
  317                 msglogstr(consbuffer, pri, /*filter_cr*/ 1);
  318         }
  319         /*
  320          * The previous behavior in log_console() is preserved when
  321          * log_console_add_linefeed is non-zero.  For that behavior, if an
  322          * individual console write came in that was not terminated with a
  323          * line feed, it would add a line feed.
  324          *
  325          * This results in different data in the message buffer than
  326          * appears on the system console (which doesn't add extra line feed
  327          * characters).
  328          *
  329          * A number of programs and rc scripts write a line feed, or a period
  330          * and a line feed when they have completed their operation.  On
  331          * the console, this looks seamless, but when displayed with
  332          * 'dmesg -a', you wind up with output that looks like this:
  333          *
  334          * Updating motd:
  335          * .
  336          *
  337          * On the console, it looks like this:
  338          * Updating motd:.
  339          *
  340          * We could add logic to detect that situation, or just not insert
  341          * the extra newlines.  Set the kern.log_console_add_linefeed
  342          * sysctl/tunable variable to get the old behavior.
  343          */
  344         if (!nl && log_console_add_linefeed) {
  345                 consbuffer[0] = '\n';
  346                 consbuffer[1] = '\0';
  347                 msglogstr(consbuffer, pri, /*filter_cr*/ 1);
  348         }
  349         msgbuftrigger = 1;
  350         free(uio, M_IOV);
  351         free(consbuffer, M_TEMP);
  352         return;
  353 }
  354 
  355 int
  356 printf(const char *fmt, ...)
  357 {
  358         va_list ap;
  359         int retval;
  360 
  361         va_start(ap, fmt);
  362         retval = vprintf(fmt, ap);
  363         va_end(ap);
  364 
  365         return (retval);
  366 }
  367 
  368 int
  369 vprintf(const char *fmt, va_list ap)
  370 {
  371         struct putchar_arg pca;
  372         int retval;
  373 #ifdef PRINTF_BUFR_SIZE
  374         char bufr[PRINTF_BUFR_SIZE];
  375 #endif
  376 
  377         pca.tty = NULL;
  378         pca.flags = TOCONS | TOLOG;
  379         pca.pri = -1;
  380 #ifdef PRINTF_BUFR_SIZE
  381         pca.p_bufr = bufr;
  382         pca.p_next = pca.p_bufr;
  383         pca.n_bufr = sizeof(bufr);
  384         pca.remain = sizeof(bufr);
  385         *pca.p_next = '\0';
  386 #else
  387         /* Don't buffer console output. */
  388         pca.p_bufr = NULL;
  389 #endif
  390 
  391         retval = kvprintf(fmt, putchar, &pca, 10, ap);
  392 
  393 #ifdef PRINTF_BUFR_SIZE
  394         /* Write any buffered console/log output: */
  395         if (*pca.p_bufr != '\0') {
  396                 cnputs(pca.p_bufr);
  397                 msglogstr(pca.p_bufr, pca.pri, /*filter_cr*/ 1);
  398         }
  399 #endif
  400 
  401         if (!panicstr)
  402                 msgbuftrigger = 1;
  403 
  404         return (retval);
  405 }
  406 
  407 static void
  408 putbuf(int c, struct putchar_arg *ap)
  409 {
  410         /* Check if no console output buffer was provided. */
  411         if (ap->p_bufr == NULL) {
  412                 /* Output direct to the console. */
  413                 if (ap->flags & TOCONS)
  414                         cnputc(c);
  415 
  416                 if (ap->flags & TOLOG)
  417                         msglogchar(c, ap->pri);
  418         } else {
  419                 /* Buffer the character: */
  420                 *ap->p_next++ = c;
  421                 ap->remain--;
  422 
  423                 /* Always leave the buffer zero terminated. */
  424                 *ap->p_next = '\0';
  425 
  426                 /* Check if the buffer needs to be flushed. */
  427                 if (ap->remain == 2 || c == '\n') {
  428 
  429                         if (ap->flags & TOLOG)
  430                                 msglogstr(ap->p_bufr, ap->pri, /*filter_cr*/1);
  431 
  432                         if (ap->flags & TOCONS) {
  433                                 if ((panicstr == NULL) && (constty != NULL))
  434                                         msgbuf_addstr(&consmsgbuf, -1,
  435                                             ap->p_bufr, /*filter_cr*/ 0);
  436 
  437                                 if ((constty == NULL) ||(always_console_output))
  438                                         cnputs(ap->p_bufr);
  439                         }
  440 
  441                         ap->p_next = ap->p_bufr;
  442                         ap->remain = ap->n_bufr;
  443                         *ap->p_next = '\0';
  444                 }
  445 
  446                 /*
  447                  * Since we fill the buffer up one character at a time,
  448                  * this should not happen.  We should always catch it when
  449                  * ap->remain == 2 (if not sooner due to a newline), flush
  450                  * the buffer and move on.  One way this could happen is
  451                  * if someone sets PRINTF_BUFR_SIZE to 1 or something
  452                  * similarly silly.
  453                  */
  454                 KASSERT(ap->remain > 2, ("Bad buffer logic, remain = %zd",
  455                     ap->remain));
  456         }
  457 }
  458 
  459 /*
  460  * Print a character on console or users terminal.  If destination is
  461  * the console then the last bunch of characters are saved in msgbuf for
  462  * inspection later.
  463  */
  464 static void
  465 putchar(int c, void *arg)
  466 {
  467         struct putchar_arg *ap = (struct putchar_arg*) arg;
  468         struct tty *tp = ap->tty;
  469         int flags = ap->flags;
  470         int putbuf_done = 0;
  471 
  472         /* Don't use the tty code after a panic or while in ddb. */
  473         if (kdb_active) {
  474                 if (c != '\0')
  475                         cnputc(c);
  476         } else {
  477                 if ((panicstr == NULL) && (flags & TOTTY) && (tp != NULL))
  478                         tty_putchar(tp, c);
  479 
  480                 if (flags & TOCONS) {
  481                         putbuf(c, ap);
  482                         putbuf_done = 1;
  483                 }
  484         }
  485         if ((flags & TOLOG) && (putbuf_done == 0)) {
  486                 if (c != '\0')
  487                         putbuf(c, ap);
  488         }
  489 }
  490 
  491 /*
  492  * Scaled down version of sprintf(3).
  493  */
  494 int
  495 sprintf(char *buf, const char *cfmt, ...)
  496 {
  497         int retval;
  498         va_list ap;
  499 
  500         va_start(ap, cfmt);
  501         retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
  502         buf[retval] = '\0';
  503         va_end(ap);
  504         return (retval);
  505 }
  506 
  507 /*
  508  * Scaled down version of vsprintf(3).
  509  */
  510 int
  511 vsprintf(char *buf, const char *cfmt, va_list ap)
  512 {
  513         int retval;
  514 
  515         retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
  516         buf[retval] = '\0';
  517         return (retval);
  518 }
  519 
  520 /*
  521  * Scaled down version of snprintf(3).
  522  */
  523 int
  524 snprintf(char *str, size_t size, const char *format, ...)
  525 {
  526         int retval;
  527         va_list ap;
  528 
  529         va_start(ap, format);
  530         retval = vsnprintf(str, size, format, ap);
  531         va_end(ap);
  532         return(retval);
  533 }
  534 
  535 /*
  536  * Scaled down version of vsnprintf(3).
  537  */
  538 int
  539 vsnprintf(char *str, size_t size, const char *format, va_list ap)
  540 {
  541         struct snprintf_arg info;
  542         int retval;
  543 
  544         info.str = str;
  545         info.remain = size;
  546         retval = kvprintf(format, snprintf_func, &info, 10, ap);
  547         if (info.remain >= 1)
  548                 *info.str++ = '\0';
  549         return (retval);
  550 }
  551 
  552 /*
  553  * Kernel version which takes radix argument vsnprintf(3).
  554  */
  555 int
  556 vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
  557 {
  558         struct snprintf_arg info;
  559         int retval;
  560 
  561         info.str = str;
  562         info.remain = size;
  563         retval = kvprintf(format, snprintf_func, &info, radix, ap);
  564         if (info.remain >= 1)
  565                 *info.str++ = '\0';
  566         return (retval);
  567 }
  568 
  569 static void
  570 snprintf_func(int ch, void *arg)
  571 {
  572         struct snprintf_arg *const info = arg;
  573 
  574         if (info->remain >= 2) {
  575                 *info->str++ = ch;
  576                 info->remain--;
  577         }
  578 }
  579 
  580 /*
  581  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
  582  * order; return an optional length and a pointer to the last character
  583  * written in the buffer (i.e., the first character of the string).
  584  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
  585  */
  586 static char *
  587 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
  588 {
  589         char *p, c;
  590 
  591         p = nbuf;
  592         *p = '\0';
  593         do {
  594                 c = hex2ascii(num % base);
  595                 *++p = upper ? toupper(c) : c;
  596         } while (num /= base);
  597         if (lenp)
  598                 *lenp = p - nbuf;
  599         return (p);
  600 }
  601 
  602 /*
  603  * Scaled down version of printf(3).
  604  *
  605  * Two additional formats:
  606  *
  607  * The format %b is supported to decode error registers.
  608  * Its usage is:
  609  *
  610  *      printf("reg=%b\n", regval, "<base><arg>*");
  611  *
  612  * where <base> is the output base expressed as a control character, e.g.
  613  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
  614  * the first of which gives the bit number to be inspected (origin 1), and
  615  * the next characters (up to a control character, i.e. a character <= 32),
  616  * give the name of the register.  Thus:
  617  *
  618  *      kvprintf("reg=%b\n", 3, "\1\2BITTWO\1BITONE\n");
  619  *
  620  * would produce output:
  621  *
  622  *      reg=3<BITTWO,BITONE>
  623  *
  624  * XXX:  %D  -- Hexdump, takes pointer and separator string:
  625  *              ("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
  626  *              ("%*D", len, ptr, " " -> XX XX XX XX ...
  627  */
  628 int
  629 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
  630 {
  631 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
  632         char nbuf[MAXNBUF];
  633         char *d;
  634         const char *p, *percent, *q;
  635         u_char *up;
  636         int ch, n;
  637         uintmax_t num;
  638         int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
  639         int cflag, hflag, jflag, tflag, zflag;
  640         int dwidth, upper;
  641         char padc;
  642         int stop = 0, retval = 0;
  643 
  644         num = 0;
  645         if (!func)
  646                 d = (char *) arg;
  647         else
  648                 d = NULL;
  649 
  650         if (fmt == NULL)
  651                 fmt = "(fmt null)\n";
  652 
  653         if (radix < 2 || radix > 36)
  654                 radix = 10;
  655 
  656         for (;;) {
  657                 padc = ' ';
  658                 width = 0;
  659                 while ((ch = (u_char)*fmt++) != '%' || stop) {
  660                         if (ch == '\0')
  661                                 return (retval);
  662                         PCHAR(ch);
  663                 }
  664                 percent = fmt - 1;
  665                 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
  666                 sign = 0; dot = 0; dwidth = 0; upper = 0;
  667                 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
  668 reswitch:       switch (ch = (u_char)*fmt++) {
  669                 case '.':
  670                         dot = 1;
  671                         goto reswitch;
  672                 case '#':
  673                         sharpflag = 1;
  674                         goto reswitch;
  675                 case '+':
  676                         sign = 1;
  677                         goto reswitch;
  678                 case '-':
  679                         ladjust = 1;
  680                         goto reswitch;
  681                 case '%':
  682                         PCHAR(ch);
  683                         break;
  684                 case '*':
  685                         if (!dot) {
  686                                 width = va_arg(ap, int);
  687                                 if (width < 0) {
  688                                         ladjust = !ladjust;
  689                                         width = -width;
  690                                 }
  691                         } else {
  692                                 dwidth = va_arg(ap, int);
  693                         }
  694                         goto reswitch;
  695                 case '':
  696                         if (!dot) {
  697                                 padc = '';
  698                                 goto reswitch;
  699                         }
  700                 case '1': case '2': case '3': case '4':
  701                 case '5': case '6': case '7': case '8': case '9':
  702                                 for (n = 0;; ++fmt) {
  703                                         n = n * 10 + ch - '';
  704                                         ch = *fmt;
  705                                         if (ch < '' || ch > '9')
  706                                                 break;
  707                                 }
  708                         if (dot)
  709                                 dwidth = n;
  710                         else
  711                                 width = n;
  712                         goto reswitch;
  713                 case 'b':
  714                         num = (u_int)va_arg(ap, int);
  715                         p = va_arg(ap, char *);
  716                         for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
  717                                 PCHAR(*q--);
  718 
  719                         if (num == 0)
  720                                 break;
  721 
  722                         for (tmp = 0; *p;) {
  723                                 n = *p++;
  724                                 if (num & (1 << (n - 1))) {
  725                                         PCHAR(tmp ? ',' : '<');
  726                                         for (; (n = *p) > ' '; ++p)
  727                                                 PCHAR(n);
  728                                         tmp = 1;
  729                                 } else
  730                                         for (; *p > ' '; ++p)
  731                                                 continue;
  732                         }
  733                         if (tmp)
  734                                 PCHAR('>');
  735                         break;
  736                 case 'c':
  737                         PCHAR(va_arg(ap, int));
  738                         break;
  739                 case 'D':
  740                         up = va_arg(ap, u_char *);
  741                         p = va_arg(ap, char *);
  742                         if (!width)
  743                                 width = 16;
  744                         while(width--) {
  745                                 PCHAR(hex2ascii(*up >> 4));
  746                                 PCHAR(hex2ascii(*up & 0x0f));
  747                                 up++;
  748                                 if (width)
  749                                         for (q=p;*q;q++)
  750                                                 PCHAR(*q);
  751                         }
  752                         break;
  753                 case 'd':
  754                 case 'i':
  755                         base = 10;
  756                         sign = 1;
  757                         goto handle_sign;
  758                 case 'h':
  759                         if (hflag) {
  760                                 hflag = 0;
  761                                 cflag = 1;
  762                         } else
  763                                 hflag = 1;
  764                         goto reswitch;
  765                 case 'j':
  766                         jflag = 1;
  767                         goto reswitch;
  768                 case 'l':
  769                         if (lflag) {
  770                                 lflag = 0;
  771                                 qflag = 1;
  772                         } else
  773                                 lflag = 1;
  774                         goto reswitch;
  775                 case 'n':
  776                         if (jflag)
  777                                 *(va_arg(ap, intmax_t *)) = retval;
  778                         else if (qflag)
  779                                 *(va_arg(ap, quad_t *)) = retval;
  780                         else if (lflag)
  781                                 *(va_arg(ap, long *)) = retval;
  782                         else if (zflag)
  783                                 *(va_arg(ap, size_t *)) = retval;
  784                         else if (hflag)
  785                                 *(va_arg(ap, short *)) = retval;
  786                         else if (cflag)
  787                                 *(va_arg(ap, char *)) = retval;
  788                         else
  789                                 *(va_arg(ap, int *)) = retval;
  790                         break;
  791                 case 'o':
  792                         base = 8;
  793                         goto handle_nosign;
  794                 case 'p':
  795                         base = 16;
  796                         sharpflag = (width == 0);
  797                         sign = 0;
  798                         num = (uintptr_t)va_arg(ap, void *);
  799                         goto number;
  800                 case 'q':
  801                         qflag = 1;
  802                         goto reswitch;
  803                 case 'r':
  804                         base = radix;
  805                         if (sign)
  806                                 goto handle_sign;
  807                         goto handle_nosign;
  808                 case 's':
  809                         p = va_arg(ap, char *);
  810                         if (p == NULL)
  811                                 p = "(null)";
  812                         if (!dot)
  813                                 n = strlen (p);
  814                         else
  815                                 for (n = 0; n < dwidth && p[n]; n++)
  816                                         continue;
  817 
  818                         width -= n;
  819 
  820                         if (!ladjust && width > 0)
  821                                 while (width--)
  822                                         PCHAR(padc);
  823                         while (n--)
  824                                 PCHAR(*p++);
  825                         if (ladjust && width > 0)
  826                                 while (width--)
  827                                         PCHAR(padc);
  828                         break;
  829                 case 't':
  830                         tflag = 1;
  831                         goto reswitch;
  832                 case 'u':
  833                         base = 10;
  834                         goto handle_nosign;
  835                 case 'X':
  836                         upper = 1;
  837                 case 'x':
  838                         base = 16;
  839                         goto handle_nosign;
  840                 case 'y':
  841                         base = 16;
  842                         sign = 1;
  843                         goto handle_sign;
  844                 case 'z':
  845                         zflag = 1;
  846                         goto reswitch;
  847 handle_nosign:
  848                         sign = 0;
  849                         if (jflag)
  850                                 num = va_arg(ap, uintmax_t);
  851                         else if (qflag)
  852                                 num = va_arg(ap, u_quad_t);
  853                         else if (tflag)
  854                                 num = va_arg(ap, ptrdiff_t);
  855                         else if (lflag)
  856                                 num = va_arg(ap, u_long);
  857                         else if (zflag)
  858                                 num = va_arg(ap, size_t);
  859                         else if (hflag)
  860                                 num = (u_short)va_arg(ap, int);
  861                         else if (cflag)
  862                                 num = (u_char)va_arg(ap, int);
  863                         else
  864                                 num = va_arg(ap, u_int);
  865                         goto number;
  866 handle_sign:
  867                         if (jflag)
  868                                 num = va_arg(ap, intmax_t);
  869                         else if (qflag)
  870                                 num = va_arg(ap, quad_t);
  871                         else if (tflag)
  872                                 num = va_arg(ap, ptrdiff_t);
  873                         else if (lflag)
  874                                 num = va_arg(ap, long);
  875                         else if (zflag)
  876                                 num = va_arg(ap, ssize_t);
  877                         else if (hflag)
  878                                 num = (short)va_arg(ap, int);
  879                         else if (cflag)
  880                                 num = (char)va_arg(ap, int);
  881                         else
  882                                 num = va_arg(ap, int);
  883 number:
  884                         if (sign && (intmax_t)num < 0) {
  885                                 neg = 1;
  886                                 num = -(intmax_t)num;
  887                         }
  888                         p = ksprintn(nbuf, num, base, &n, upper);
  889                         tmp = 0;
  890                         if (sharpflag && num != 0) {
  891                                 if (base == 8)
  892                                         tmp++;
  893                                 else if (base == 16)
  894                                         tmp += 2;
  895                         }
  896                         if (neg)
  897                                 tmp++;
  898 
  899                         if (!ladjust && padc == '')
  900                                 dwidth = width - tmp;
  901                         width -= tmp + imax(dwidth, n);
  902                         dwidth -= n;
  903                         if (!ladjust)
  904                                 while (width-- > 0)
  905                                         PCHAR(' ');
  906                         if (neg)
  907                                 PCHAR('-');
  908                         if (sharpflag && num != 0) {
  909                                 if (base == 8) {
  910                                         PCHAR('');
  911                                 } else if (base == 16) {
  912                                         PCHAR('');
  913                                         PCHAR('x');
  914                                 }
  915                         }
  916                         while (dwidth-- > 0)
  917                                 PCHAR('');
  918 
  919                         while (*p)
  920                                 PCHAR(*p--);
  921 
  922                         if (ladjust)
  923                                 while (width-- > 0)
  924                                         PCHAR(' ');
  925 
  926                         break;
  927                 default:
  928                         while (percent < fmt)
  929                                 PCHAR(*percent++);
  930                         /*
  931                          * Since we ignore an formatting argument it is no 
  932                          * longer safe to obey the remaining formatting
  933                          * arguments as the arguments will no longer match
  934                          * the format specs.
  935                          */
  936                         stop = 1;
  937                         break;
  938                 }
  939         }
  940 #undef PCHAR
  941 }
  942 
  943 /*
  944  * Put character in log buffer with a particular priority.
  945  */
  946 static void
  947 msglogchar(int c, int pri)
  948 {
  949         static int lastpri = -1;
  950         static int dangling;
  951         char nbuf[MAXNBUF];
  952         char *p;
  953 
  954         if (!msgbufmapped)
  955                 return;
  956         if (c == '\0' || c == '\r')
  957                 return;
  958         if (pri != -1 && pri != lastpri) {
  959                 if (dangling) {
  960                         msgbuf_addchar(msgbufp, '\n');
  961                         dangling = 0;
  962                 }
  963                 msgbuf_addchar(msgbufp, '<');
  964                 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
  965                         msgbuf_addchar(msgbufp, *p--);
  966                 msgbuf_addchar(msgbufp, '>');
  967                 lastpri = pri;
  968         }
  969         msgbuf_addchar(msgbufp, c);
  970         if (c == '\n') {
  971                 dangling = 0;
  972                 lastpri = -1;
  973         } else {
  974                 dangling = 1;
  975         }
  976 }
  977 
  978 static void
  979 msglogstr(char *str, int pri, int filter_cr)
  980 {
  981         if (!msgbufmapped)
  982                 return;
  983 
  984         msgbuf_addstr(msgbufp, pri, str, filter_cr);
  985 }
  986 
  987 void
  988 msgbufinit(void *ptr, int size)
  989 {
  990         char *cp;
  991         static struct msgbuf *oldp = NULL;
  992 
  993         size -= sizeof(*msgbufp);
  994         cp = (char *)ptr;
  995         msgbufp = (struct msgbuf *)(cp + size);
  996         msgbuf_reinit(msgbufp, cp, size);
  997         if (msgbufmapped && oldp != msgbufp)
  998                 msgbuf_copy(oldp, msgbufp);
  999         msgbufmapped = 1;
 1000         oldp = msgbufp;
 1001 }
 1002 
 1003 static int unprivileged_read_msgbuf = 1;
 1004 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf,
 1005     CTLFLAG_RW, &unprivileged_read_msgbuf, 0,
 1006     "Unprivileged processes may read the kernel message buffer");
 1007 
 1008 /* Sysctls for accessing/clearing the msgbuf */
 1009 static int
 1010 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
 1011 {
 1012         char buf[128];
 1013         u_int seq;
 1014         int error, len;
 1015 
 1016         if (!unprivileged_read_msgbuf) {
 1017                 error = priv_check(req->td, PRIV_MSGBUF);
 1018                 if (error)
 1019                         return (error);
 1020         }
 1021 
 1022         /* Read the whole buffer, one chunk at a time. */
 1023         mtx_lock(&msgbuf_lock);
 1024         msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
 1025         for (;;) {
 1026                 len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
 1027                 mtx_unlock(&msgbuf_lock);
 1028                 if (len == 0)
 1029                         return (0);
 1030 
 1031                 error = sysctl_handle_opaque(oidp, buf, len, req);
 1032                 if (error)
 1033                         return (error);
 1034 
 1035                 mtx_lock(&msgbuf_lock);
 1036         }
 1037 }
 1038 
 1039 SYSCTL_PROC(_kern, OID_AUTO, msgbuf,
 1040     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
 1041     NULL, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
 1042 
 1043 static int msgbuf_clearflag;
 1044 
 1045 static int
 1046 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
 1047 {
 1048         int error;
 1049         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
 1050         if (!error && req->newptr) {
 1051                 mtx_lock(&msgbuf_lock);
 1052                 msgbuf_clear(msgbufp);
 1053                 mtx_unlock(&msgbuf_lock);
 1054                 msgbuf_clearflag = 0;
 1055         }
 1056         return (error);
 1057 }
 1058 
 1059 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
 1060     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE,
 1061     &msgbuf_clearflag, 0, sysctl_kern_msgbuf_clear, "I",
 1062     "Clear kernel message buffer");
 1063 
 1064 #ifdef DDB
 1065 
 1066 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
 1067 {
 1068         int i, j;
 1069 
 1070         if (!msgbufmapped) {
 1071                 db_printf("msgbuf not mapped yet\n");
 1072                 return;
 1073         }
 1074         db_printf("msgbufp = %p\n", msgbufp);
 1075         db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n",
 1076             msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq,
 1077             msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum);
 1078         for (i = 0; i < msgbufp->msg_size && !db_pager_quit; i++) {
 1079                 j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq);
 1080                 db_printf("%c", msgbufp->msg_ptr[j]);
 1081         }
 1082         db_printf("\n");
 1083 }
 1084 
 1085 #endif /* DDB */
 1086 
 1087 void
 1088 hexdump(const void *ptr, int length, const char *hdr, int flags)
 1089 {
 1090         int i, j, k;
 1091         int cols;
 1092         const unsigned char *cp;
 1093         char delim;
 1094 
 1095         if ((flags & HD_DELIM_MASK) != 0)
 1096                 delim = (flags & HD_DELIM_MASK) >> 8;
 1097         else
 1098                 delim = ' ';
 1099 
 1100         if ((flags & HD_COLUMN_MASK) != 0)
 1101                 cols = flags & HD_COLUMN_MASK;
 1102         else
 1103                 cols = 16;
 1104 
 1105         cp = ptr;
 1106         for (i = 0; i < length; i+= cols) {
 1107                 if (hdr != NULL)
 1108                         printf("%s", hdr);
 1109 
 1110                 if ((flags & HD_OMIT_COUNT) == 0)
 1111                         printf("%04x  ", i);
 1112 
 1113                 if ((flags & HD_OMIT_HEX) == 0) {
 1114                         for (j = 0; j < cols; j++) {
 1115                                 k = i + j;
 1116                                 if (k < length)
 1117                                         printf("%c%02x", delim, cp[k]);
 1118                                 else
 1119                                         printf("   ");
 1120                         }
 1121                 }
 1122 
 1123                 if ((flags & HD_OMIT_CHARS) == 0) {
 1124                         printf("  |");
 1125                         for (j = 0; j < cols; j++) {
 1126                                 k = i + j;
 1127                                 if (k >= length)
 1128                                         printf(" ");
 1129                                 else if (cp[k] >= ' ' && cp[k] <= '~')
 1130                                         printf("%c", cp[k]);
 1131                                 else
 1132                                         printf(".");
 1133                         }
 1134                         printf("|");
 1135                 }
 1136                 printf("\n");
 1137         }
 1138 }
 1139 

Cache object: 0bccc67234036bb0b74f6159bf0a6811


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.