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