Home | History | Annotate | Line # | Download | only in libwrap
diag.c revision 1.8
      1  1.8       jrf /*	$NetBSD: diag.c,v 1.8 2004/09/07 13:20:40 jrf 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.8       jrf __RCSID("$NetBSD: diag.c,v 1.8 2004/09/07 13:20:40 jrf 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.1       mrg #include <stdio.h>
     27  1.1       mrg #include <setjmp.h>
     28  1.7    atatat #include <string.h>
     29  1.7    atatat #include <errno.h>
     30  1.1       mrg 
     31  1.1       mrg /* Local stuff */
     32  1.1       mrg 
     33  1.1       mrg #include "tcpd.h"
     34  1.1       mrg #include "mystdarg.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.6  sommerfe static void tcpd_diag __P((int, char *, 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.1       mrg static void tcpd_diag(severity, tag, format, ap)
     45  1.1       mrg int     severity;
     46  1.1       mrg char   *tag;
     47  1.1       mrg char   *format;
     48  1.1       mrg va_list ap;
     49  1.1       mrg {
     50  1.1       mrg     char    fmt[BUFSIZ];
     51  1.6  sommerfe     char    buf[BUFSIZ];
     52  1.7    atatat     int     i, o, oerrno;
     53  1.1       mrg 
     54  1.7    atatat     /* save errno in case we need it */
     55  1.7    atatat     oerrno = errno;
     56  1.7    atatat 
     57  1.7    atatat     /* contruct the tag for the log entry */
     58  1.1       mrg     if (tcpd_context.file)
     59  1.7    atatat 	(void)snprintf(buf, sizeof buf, "%s: %s, line %d: ",
     60  1.6  sommerfe 		tag, tcpd_context.file, tcpd_context.line);
     61  1.1       mrg     else
     62  1.7    atatat 	(void)snprintf(buf, sizeof buf, "%s: ", tag);
     63  1.7    atatat 
     64  1.7    atatat     /* change % to %% in tag before appending the format */
     65  1.7    atatat     for (i = 0, o = 0; buf[i] != '\0'; ) {
     66  1.7    atatat 	if (buf[i] == '%') {
     67  1.7    atatat 	    fmt[o] = '%';
     68  1.7    atatat 	    if (o < sizeof(fmt) - 1)
     69  1.7    atatat 		o++;
     70  1.7    atatat 	}
     71  1.7    atatat 	fmt[o] = buf[i++];
     72  1.7    atatat 	if (o < sizeof(fmt) - 1)
     73  1.7    atatat 	    o++;
     74  1.7    atatat     }
     75  1.7    atatat 
     76  1.7    atatat     /* append format and force null termination */
     77  1.7    atatat     fmt[o] = '\0';
     78  1.8       jrf     (void)strlcat(fmt, format, sizeof(fmt) - o);
     79  1.7    atatat 
     80  1.7    atatat     errno = oerrno;
     81  1.7    atatat     vsyslog(severity, fmt, ap);
     82  1.1       mrg }
     83  1.1       mrg 
     84  1.1       mrg /* tcpd_warn - report problem of some sort and proceed */
     85  1.1       mrg 
     86  1.1       mrg void    VARARGS(tcpd_warn, char *, format)
     87  1.1       mrg {
     88  1.1       mrg     va_list ap;
     89  1.1       mrg 
     90  1.1       mrg     VASTART(ap, char *, format);
     91  1.1       mrg     tcpd_diag(LOG_ERR, "warning", format, ap);
     92  1.1       mrg     VAEND(ap);
     93  1.1       mrg }
     94  1.1       mrg 
     95  1.1       mrg /* tcpd_jump - report serious problem and jump */
     96  1.1       mrg 
     97  1.1       mrg void    VARARGS(tcpd_jump, char *, format)
     98  1.1       mrg {
     99  1.1       mrg     va_list ap;
    100  1.1       mrg 
    101  1.1       mrg     VASTART(ap, char *, format);
    102  1.1       mrg     tcpd_diag(LOG_ERR, "error", format, ap);
    103  1.1       mrg     VAEND(ap);
    104  1.1       mrg     longjmp(tcpd_buf, AC_ERROR);
    105  1.1       mrg }
    106