Home | History | Annotate | Line # | Download | only in libwrap
diag.c revision 1.15
      1  1.15  christos /*	$NetBSD: diag.c,v 1.15 2019/01/11 16:15:20 christos 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.15  christos __RCSID("$NetBSD: diag.c,v 1.15 2019/01/11 16:15:20 christos 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.10     joerg #include <stdlib.h>
     29   1.1       mrg #include <setjmp.h>
     30   1.7    atatat #include <string.h>
     31   1.7    atatat #include <errno.h>
     32   1.1       mrg 
     33   1.1       mrg /* Local stuff */
     34   1.1       mrg 
     35   1.1       mrg #include "tcpd.h"
     36   1.1       mrg 
     37   1.1       mrg struct tcpd_context tcpd_context;
     38   1.1       mrg jmp_buf tcpd_buf;
     39   1.3  christos 
     40   1.9      matt static void tcpd_diag(int, const char *, const char *, va_list)
     41  1.11  christos     __sysloglike(3,0);
     42   1.1       mrg 
     43   1.1       mrg /* tcpd_diag - centralize error reporter */
     44   1.1       mrg 
     45   1.9      matt static void
     46  1.10     joerg tcpd_diag(int severity, const char *tag, const char *fmt, va_list ap)
     47   1.1       mrg {
     48  1.15  christos     char *buf;
     49  1.15  christos     int e, oerrno = errno;
     50  1.12  christos 
     51  1.15  christos     /* contruct the tag for the log entry */
     52  1.15  christos     if (tcpd_context.file)
     53  1.15  christos 	e = asprintf(&buf, "%s: %s, line %d: %s",
     54  1.15  christos 	    tag, tcpd_context.file, tcpd_context.line, fmt);
     55  1.15  christos     else
     56  1.15  christos 	e = asprintf(&buf, "%s: %s", tag, fmt);
     57  1.15  christos 
     58  1.15  christos     if (e == -1)
     59  1.12  christos 	buf = __UNCONST(fmt);
     60  1.10     joerg 
     61  1.14  christos     errno = oerrno;
     62  1.14  christos 
     63  1.15  christos     vsyslog(severity, buf, ap);
     64   1.7    atatat 
     65  1.10     joerg     if (buf != fmt)
     66  1.10     joerg         free(buf);
     67   1.1       mrg }
     68   1.1       mrg 
     69   1.1       mrg /* tcpd_warn - report problem of some sort and proceed */
     70   1.1       mrg 
     71   1.9      matt void
     72   1.9      matt tcpd_warn(const char *format, ...)
     73   1.1       mrg {
     74   1.1       mrg     va_list ap;
     75   1.1       mrg 
     76   1.9      matt     va_start(ap, format);
     77   1.1       mrg     tcpd_diag(LOG_ERR, "warning", format, ap);
     78   1.9      matt     va_end(ap);
     79   1.1       mrg }
     80   1.1       mrg 
     81   1.1       mrg /* tcpd_jump - report serious problem and jump */
     82   1.1       mrg 
     83   1.9      matt void
     84   1.9      matt tcpd_jump(const char *format, ...)
     85   1.1       mrg {
     86   1.1       mrg     va_list ap;
     87   1.1       mrg 
     88   1.9      matt     va_start(ap, format);
     89   1.1       mrg     tcpd_diag(LOG_ERR, "error", format, ap);
     90   1.9      matt     va_end(ap);
     91   1.1       mrg     longjmp(tcpd_buf, AC_ERROR);
     92   1.1       mrg }
     93