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