File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / mpd / src / log.c
Revision 1.1.1.4 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 00:39:23 2021 UTC (3 years, 4 months ago) by misho
Branches: mpd, MAIN
CVS tags: v5_9p16, v5_9, HEAD
mpd 5.9

    1: 
    2: /*
    3:  * log.c
    4:  *
    5:  * Written by Toshiharu OHNO <tony-o@iij.ad.jp>
    6:  * Copyright (c) 1993, Internet Initiative Japan, Inc. All rights reserved.
    7:  * See ``COPYRIGHT.iij''
    8:  * 
    9:  * Rewritten by Archie Cobbs <archie@freebsd.org>
   10:  * Copyright (c) 1995-1999 Whistle Communications, Inc. All rights reserved.
   11:  * See ``COPYRIGHT.whistle''
   12:  */
   13: 
   14: #include "ppp.h"
   15: #ifdef SYSLOG_FACILITY
   16: #include <syslog.h>
   17: #endif
   18: 
   19: /*
   20:  * DEFINITIONS
   21:  */
   22: 
   23:   #define DUMP_BYTES_PER_LINE	16
   24:   #define ROUNDUP(x,r)		(((x)%(r))?((x)+((r)-((x)%(r)))):(x))
   25:   #define MAX_LOG_LINE		500
   26: 
   27: /* Log option descriptor */
   28: 
   29:   struct logopt
   30:   {
   31:     int		mask;
   32:     const char	*name;
   33:     const char	*desc;
   34:   };
   35: 
   36: /*
   37:  * GLOBAL VARIABLES
   38:  */
   39: 
   40:   int	gLogOptions = LG_DEFAULT_OPT | LG_ALWAYS;
   41: #ifdef SYSLOG_FACILITY
   42:   char	gSysLogIdent[32];
   43: #endif
   44: 
   45: /*
   46:  * INTERNAL VARIABLES
   47:  */
   48: 
   49:   #define ADD_OPT(x,d)	{ LG_ ##x, #x, d },
   50: 
   51:   static struct logopt	LogOptionList[] =
   52:   {
   53: #ifdef LG_BUND
   54:     ADD_OPT(BUND,	"Bundle events")
   55: #endif
   56: #ifdef LG_BUND2
   57:     ADD_OPT(BUND2,	"Detailed bundle events")
   58: #endif
   59: #ifdef LG_LINK
   60:     ADD_OPT(LINK,	"Link events")
   61: #endif
   62: #ifdef LG_REP
   63:     ADD_OPT(REP,	"Repeater events")
   64: #endif
   65: #ifdef LG_LCP
   66:     ADD_OPT(LCP,	"LCP negotiation")
   67: #endif
   68: #ifdef LG_LCP2
   69:     ADD_OPT(LCP2,	"LCP events and debugging")
   70: #endif
   71: #ifdef LG_AUTH
   72:     ADD_OPT(AUTH,	"Link authentication events")
   73: #endif
   74: #ifdef LG_AUTH2
   75:     ADD_OPT(AUTH2,	"Link authentication details")
   76: #endif
   77: #ifdef LG_IPCP
   78:     ADD_OPT(IPCP,	"IPCP negotiation")
   79: #endif
   80: #ifdef LG_IPCP2
   81:     ADD_OPT(IPCP2,	"IPCP events and debugging")
   82: #endif
   83: #ifdef LG_IPV6CP
   84:     ADD_OPT(IPV6CP,	"IPV6CP negotiation")
   85: #endif
   86: #ifdef LG_IPV6CP2
   87:     ADD_OPT(IPV6CP2,	"IPV6CP events and debugging")
   88: #endif
   89: #ifdef LG_CCP
   90:     ADD_OPT(CCP,	"CCP negotiation")
   91: #endif
   92: #ifdef LG_CCP2
   93:     ADD_OPT(CCP2,	"CCP events and debugging")
   94: #endif
   95: #ifdef LG_ECP
   96:     ADD_OPT(ECP,	"ECP negotiation")
   97: #endif
   98: #ifdef LG_ECP2
   99:     ADD_OPT(ECP2,	"ECP events and debugging")
  100: #endif
  101: #ifdef LG_FSM
  102:     ADD_OPT(FSM,	"All FSM events (except echo & reset)")
  103: #endif
  104: #ifdef LG_ECHO
  105:     ADD_OPT(ECHO,	"Echo/reply events for all automata")
  106: #endif
  107: #ifdef LG_PHYS
  108:     ADD_OPT(PHYS,	"Physical layer events")
  109: #endif
  110: #ifdef LG_PHYS2
  111:     ADD_OPT(PHYS2,	"Physical layer debug")
  112: #endif
  113: #ifdef LG_PHYS3
  114:     ADD_OPT(PHYS3,	"Physical layer control packet dump")
  115: #endif
  116: #ifdef LG_CHAT
  117:     ADD_OPT(CHAT,	"Modem chat script")
  118: #endif
  119: #ifdef LG_CHAT2
  120:     ADD_OPT(CHAT2,	"Chat script extra debugging output")
  121: #endif
  122: #ifdef LG_IFACE
  123:     ADD_OPT(IFACE,	"IP interface and route management")
  124: #endif
  125: #ifdef LG_IFACE2
  126:     ADD_OPT(IFACE2,	"IP interface and route management debug")
  127: #endif
  128: #ifdef LG_FRAME
  129:     ADD_OPT(FRAME,	"Dump all incoming & outgoing frames")
  130: #endif
  131: #ifdef LG_RADIUS
  132:     ADD_OPT(RADIUS,	"Radius authentication events")
  133: #endif
  134: #ifdef LG_RADIUS2
  135:     ADD_OPT(RADIUS2,	"Radius authentication debug")
  136: #endif
  137: #ifdef LG_CONSOLE
  138:     ADD_OPT(CONSOLE,	"Console events")
  139: #endif
  140: #ifdef LG_EVENTS
  141:     ADD_OPT(EVENTS,	"Daemon events debug")
  142: #endif
  143:   };
  144: 
  145:   #define NUM_LOG_LEVELS (sizeof(LogOptionList) / sizeof(*LogOptionList))
  146: 
  147: /*
  148:  * LogOpen()
  149:  */
  150: 
  151: int
  152: LogOpen(void)
  153: {
  154: #ifdef SYSLOG_FACILITY
  155:     if (!*gSysLogIdent)
  156: 	strcpy(gSysLogIdent, "mpd");
  157:     openlog(gSysLogIdent, 0, LOG_DAEMON);
  158: #endif
  159:     return(0);
  160: }
  161: 
  162: /*
  163:  * LogClose()
  164:  */
  165: 
  166: void
  167: LogClose(void)
  168: {
  169: #ifdef SYSLOG_FACILITY
  170:     closelog();
  171: #endif
  172: }
  173: 
  174: /*
  175:  * LogCommand()
  176:  */
  177: 
  178: int
  179: LogCommand(Context ctx, int ac, const char *const av[], const void *arg)
  180: {
  181:     u_int	k;
  182:     int		bits, add;
  183:     const char	*s;
  184: 
  185:     (void)arg;
  186:     if (ac == 0) {
  187: #define LG_FMT	"    %-12s  %-10s  %s\r\n"
  188: 
  189: 	Printf(LG_FMT, "Log Option", "Enabled", "Description");
  190: 	Printf(LG_FMT, "----------", "-------", "-----------");
  191: 	for (k = 0; k < NUM_LOG_LEVELS; k++) {
  192:     	    Printf("  " LG_FMT, LogOptionList[k].name,
  193: 		(gLogOptions & LogOptionList[k].mask) ? "Yes" : "No",
  194: 		LogOptionList[k].desc);
  195: 	}
  196: 	return(0);
  197:     }
  198: 
  199:     while (ac--) {
  200: 	s = *av;
  201: 	switch (*s) {
  202:     	    case '+':
  203: 		s++;
  204:     	    default:
  205: 		add = TRUE;
  206: 		break;
  207:     	    case '-':
  208: 		add = FALSE;
  209: 		s++;
  210: 	    break;
  211: 	}
  212: 	for (k = 0;
  213:     	    k < NUM_LOG_LEVELS && strcasecmp(s, LogOptionList[k].name);
  214:     	    k++);
  215: 	if (k < NUM_LOG_LEVELS)
  216:     	    bits = LogOptionList[k].mask;
  217: 	else {
  218:     	    if (!strcasecmp(s, "all")) {
  219: 		for (bits = k = 0; k < NUM_LOG_LEVELS; k++)
  220: 		    bits |= LogOptionList[k].mask;
  221:     	    } else {
  222: 		Printf("\"%s\" is unknown. Enter \"log\" for list.\r\n", s);
  223: 		bits = 0;
  224:     	    }
  225: 	}
  226: 	if (add)
  227:     	    gLogOptions |= bits;
  228: 	else
  229:     	    gLogOptions &= ~bits;
  230: 	av++;
  231:     }
  232:     return(0);
  233: }
  234: 
  235: /*
  236:  * LogPrintf()
  237:  *
  238:  * The way to print something to the log
  239:  */
  240: 
  241: void
  242: LogPrintf(const char *fmt, ...)
  243: {
  244:     va_list       args;
  245: 
  246:     va_start(args, fmt);
  247:     vLogPrintf(fmt, args);
  248:     va_end(args);
  249: }
  250: 
  251: void
  252: vLogPrintf(const char *fmt, va_list args) NO_THREAD_SAFETY_ANALYSIS
  253: {
  254:     if (!SLIST_EMPTY(&gConsole.sessions)) {
  255: 	char		buf[1000];
  256: 	ConsoleSession	s;
  257: 
  258:         vsnprintf(buf, sizeof(buf), fmt, args);
  259: #ifdef SYSLOG_FACILITY
  260:         syslog(LOG_INFO, "%s", buf);
  261: #endif
  262: 	pthread_cleanup_push(ConsoleCancelCleanup, gConsole.lock);
  263: 	RWLOCK_RDLOCK(gConsole.lock);
  264: 	SLIST_FOREACH(s, &gConsole.sessions, next) {
  265: 	    if (Enabled(&s->options, CONSOLE_LOGGING))
  266: 		s->write(s, "%s\r\n", buf);
  267: 	}
  268: 	pthread_cleanup_pop(1);
  269: #ifdef SYSLOG_FACILITY
  270:     } else {
  271:         vsyslog(LOG_INFO, fmt, args);
  272: #endif
  273:     }
  274: }
  275: 
  276: /*
  277:  * LogPrintf2()
  278:  *
  279:  * The way to print something to the log
  280:  */
  281: 
  282: void
  283: LogPrintf2(const char *fmt, ...)
  284: {
  285:     va_list       args;
  286: 
  287:     va_start(args, fmt);
  288: #ifdef SYSLOG_FACILITY
  289:     vsyslog(LOG_INFO, fmt, args);
  290: #endif
  291:     va_end(args);
  292: }
  293: 
  294: /*
  295:  * LogDumpBp2()
  296:  *
  297:  * Dump the contents of an Mbuf to the log
  298:  */
  299: 
  300: void
  301: LogDumpBp2(Mbuf bp, const char *fmt, ...)
  302: {
  303:     int		k;
  304:     unsigned	total;
  305:     u_char	bytes[DUMP_BYTES_PER_LINE];
  306:     char	line[128];
  307:     int		linelen;
  308:     va_list	ap;
  309: 
  310: 	/* Do header */
  311: 	va_start(ap, fmt);
  312: 	vLogPrintf(fmt, ap);
  313: 	va_end(ap);
  314: 
  315: 	/* Do data */
  316: 	line[0]=' ';
  317: 	line[1]=' ';
  318:         line[2]=' ';
  319:         line[3]=0;
  320:         linelen=3;
  321:   
  322:         total = 0;
  323: 	if (bp) {
  324:     	    int	start, last = 0;
  325:     	    unsigned stop;
  326: 
  327:     	    stop = ROUNDUP(total + MBLEN(bp), DUMP_BYTES_PER_LINE);
  328:     	    for (start = total; total < stop; ) {
  329:     		u_int	const byte = (MBDATAU(bp))[total - start];
  330: 
  331:     		if (total < start + MBLEN(bp)) {
  332: 		    sprintf(line+linelen, " %02x", byte);
  333: 		    last = total % DUMP_BYTES_PER_LINE;
  334:     		} else
  335: 		    sprintf(line+linelen, "   ");
  336:     		linelen+=3;
  337:       
  338:     		bytes[total % DUMP_BYTES_PER_LINE] = byte;
  339:     		total++;
  340:       
  341:     		if (total % DUMP_BYTES_PER_LINE == 0) {
  342: 		    snprintf(line+linelen, sizeof(line), "  ");
  343:         	    linelen+=2;
  344: 		    for (k = 0; k <= last; k++) {
  345: 			line[linelen++] = isgraph(bytes[k]) ? bytes[k] : '.';
  346: 			line[linelen] = 0;
  347: 		    }
  348: 		    LogPrintf("%s",line);
  349: 		    line[0]=' ';
  350: 		    line[1]=' ';
  351: 		    line[2]=' ';
  352: 		    line[3]=0;
  353: 		    linelen=3;
  354:     		}
  355:     	    }
  356: 	}
  357: }
  358: 
  359: /*
  360:  * LogDumpBuf2()
  361:  *
  362:  * Dump the contents of a buffer to the log
  363:  */
  364: 
  365: void
  366: LogDumpBuf2(const u_char *buf, int count, const char *fmt, ...)
  367: {
  368:     int		k, stop, total;
  369:     char	line[128];
  370:     int		linelen;
  371:     va_list	ap;
  372: 
  373: 	/* Do header */
  374: 	va_start(ap, fmt);
  375:         vLogPrintf(fmt, ap);
  376:         va_end(ap);
  377: 
  378: 	/* Do data */
  379:         line[0]=' ';
  380:         line[1]=' ';
  381:         line[2]=' ';
  382:         line[3]=0;
  383:         linelen=3;
  384: 
  385:         stop = ROUNDUP(count, DUMP_BYTES_PER_LINE);
  386:         for (total = 0; total < stop; ) {
  387: 	    if (total < count)
  388: 		sprintf(line+linelen, " %02x", buf[total]);
  389: 	    else
  390: 		sprintf(line+linelen, "   ");
  391:             linelen+=3;
  392:     	    total++;
  393: 	    if (total % DUMP_BYTES_PER_LINE == 0) {
  394: 		snprintf(line+linelen, sizeof(line), "  ");
  395:     		linelen+=2;
  396:     		for (k = total - DUMP_BYTES_PER_LINE; k < total && k < count; k++) {
  397: 		    line[linelen++] = isgraph(buf[k]) ? buf[k] : '.';
  398: 		    line[linelen] = 0;
  399: 		}
  400: 		LogPrintf("%s",line);
  401: 		line[0]=' ';
  402: 		line[1]=' ';
  403: 		line[2]=' ';
  404: 		line[3]=0;
  405: 		linelen=3;
  406: 	    }
  407: 	}
  408: }
  409: 
  410: /*
  411:  * Perror()
  412:  */
  413: 
  414: void
  415: Perror(const char *fmt, ...)
  416: {
  417:     va_list	args;
  418:     char	buf[200];
  419: 
  420:     va_start(args, fmt);
  421:     vsnprintf(buf, sizeof(buf), fmt, args);
  422:     va_end(args);
  423:     snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
  424: 	": %s", strerror(errno));
  425:     Log(LG_ERR, ("%s", buf));
  426: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>