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

Cache object: 65fe9ec8cab869d3b74fc080dcdbe2dc


[ 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.