1 /* $NetBSD: diag.c,v 1.7 2001/09/24 17:55:47 atatat 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.7 2001/09/24 17:55:47 atatat Exp $"); 20 #endif 21 #endif 22 23 /* System libraries */ 24 25 #include <syslog.h> 26 #include <stdio.h> 27 #include <setjmp.h> 28 #include <string.h> 29 #include <errno.h> 30 31 /* Local stuff */ 32 33 #include "tcpd.h" 34 #include "mystdarg.h" 35 36 struct tcpd_context tcpd_context; 37 jmp_buf tcpd_buf; 38 39 static void tcpd_diag __P((int, char *, char *, va_list)) 40 __attribute__((__format__(__printf__, 3, 0))); 41 42 /* tcpd_diag - centralize error reporter */ 43 44 static void tcpd_diag(severity, tag, format, ap) 45 int severity; 46 char *tag; 47 char *format; 48 va_list ap; 49 { 50 char fmt[BUFSIZ]; 51 char buf[BUFSIZ]; 52 int i, o, oerrno; 53 54 /* save errno in case we need it */ 55 oerrno = errno; 56 57 /* contruct the tag for the log entry */ 58 if (tcpd_context.file) 59 (void)snprintf(buf, sizeof buf, "%s: %s, line %d: ", 60 tag, tcpd_context.file, tcpd_context.line); 61 else 62 (void)snprintf(buf, sizeof buf, "%s: ", tag); 63 64 /* change % to %% in tag before appending the format */ 65 for (i = 0, o = 0; buf[i] != '\0'; ) { 66 if (buf[i] == '%') { 67 fmt[o] = '%'; 68 if (o < sizeof(fmt) - 1) 69 o++; 70 } 71 fmt[o] = buf[i++]; 72 if (o < sizeof(fmt) - 1) 73 o++; 74 } 75 76 /* append format and force null termination */ 77 fmt[o] = '\0'; 78 strncat(fmt, format, sizeof(fmt) - o); 79 fmt[sizeof(fmt) - 1] = '\0'; 80 81 errno = oerrno; 82 vsyslog(severity, fmt, ap); 83 } 84 85 /* tcpd_warn - report problem of some sort and proceed */ 86 87 void VARARGS(tcpd_warn, char *, format) 88 { 89 va_list ap; 90 91 VASTART(ap, char *, format); 92 tcpd_diag(LOG_ERR, "warning", format, ap); 93 VAEND(ap); 94 } 95 96 /* tcpd_jump - report serious problem and jump */ 97 98 void VARARGS(tcpd_jump, char *, format) 99 { 100 va_list ap; 101 102 VASTART(ap, char *, format); 103 tcpd_diag(LOG_ERR, "error", format, ap); 104 VAEND(ap); 105 longjmp(tcpd_buf, AC_ERROR); 106 } 107