Home | History | Annotate | Line # | Download | only in libwrap
diag.c revision 1.9
      1 /*	$NetBSD: diag.c,v 1.9 2012/03/21 10:10:37 matt Exp $	*/
      2 
      3  /*
      4   * Routines to report various classes of problems. Each report is decorated
      5   * with the current context (file name and line number), if available.
      6   *
      7   * tcpd_warn() reports a problem and proceeds.
      8   *
      9   * tcpd_jump() reports a problem and jumps.
     10   *
     11   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
     12   */
     13 
     14 #include <sys/cdefs.h>
     15 #ifndef lint
     16 #if 0
     17 static char sccsid[] = "@(#) diag.c 1.1 94/12/28 17:42:20";
     18 #else
     19 __RCSID("$NetBSD: diag.c,v 1.9 2012/03/21 10:10:37 matt Exp $");
     20 #endif
     21 #endif
     22 
     23 /* System libraries */
     24 
     25 #include <syslog.h>
     26 #include <stdarg.h>
     27 #include <stdio.h>
     28 #include <setjmp.h>
     29 #include <string.h>
     30 #include <errno.h>
     31 
     32 /* Local stuff */
     33 
     34 #include "tcpd.h"
     35 
     36 struct tcpd_context tcpd_context;
     37 jmp_buf tcpd_buf;
     38 
     39 static void tcpd_diag(int, const char *, const char *, va_list)
     40 	__attribute__((__format__(__printf__, 3, 0)));
     41 
     42 /* tcpd_diag - centralize error reporter */
     43 
     44 static void
     45 tcpd_diag(int severity, const char *tag, const char *format, va_list ap)
     46 {
     47     char    fmt[BUFSIZ];
     48     char    buf[BUFSIZ];
     49     size_t  i, o;
     50     int     oerrno;
     51 
     52     /* save errno in case we need it */
     53     oerrno = errno;
     54 
     55     /* contruct the tag for the log entry */
     56     if (tcpd_context.file)
     57 	(void)snprintf(buf, sizeof buf, "%s: %s, line %d: ",
     58 		tag, tcpd_context.file, tcpd_context.line);
     59     else
     60 	(void)snprintf(buf, sizeof buf, "%s: ", tag);
     61 
     62     /* change % to %% in tag before appending the format */
     63     for (i = 0, o = 0; buf[i] != '\0'; ) {
     64 	if (buf[i] == '%') {
     65 	    fmt[o] = '%';
     66 	    if (o < sizeof(fmt) - 1)
     67 		o++;
     68 	}
     69 	fmt[o] = buf[i++];
     70 	if (o < sizeof(fmt) - 1)
     71 	    o++;
     72     }
     73 
     74     /* append format and force null termination */
     75     fmt[o] = '\0';
     76     (void)strlcat(fmt, format, sizeof(fmt) - o);
     77 
     78     errno = oerrno;
     79     vsyslog(severity, fmt, ap);
     80 }
     81 
     82 /* tcpd_warn - report problem of some sort and proceed */
     83 
     84 void
     85 tcpd_warn(const char *format, ...)
     86 {
     87     va_list ap;
     88 
     89     va_start(ap, format);
     90     tcpd_diag(LOG_ERR, "warning", format, ap);
     91     va_end(ap);
     92 }
     93 
     94 /* tcpd_jump - report serious problem and jump */
     95 
     96 void
     97 tcpd_jump(const char *format, ...)
     98 {
     99     va_list ap;
    100 
    101     va_start(ap, format);
    102     tcpd_diag(LOG_ERR, "error", format, ap);
    103     va_end(ap);
    104     longjmp(tcpd_buf, AC_ERROR);
    105 }
    106