diag.c revision 1.10 1 1.10 joerg /* $NetBSD: diag.c,v 1.10 2012/03/22 22:58:15 joerg 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.10 joerg __RCSID("$NetBSD: diag.c,v 1.10 2012/03/22 22:58:15 joerg 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.10 joerg __printflike(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.10 joerg char *buf;
49 1.9 matt int oerrno;
50 1.1 mrg
51 1.7 atatat /* save errno in case we need it */
52 1.7 atatat oerrno = errno;
53 1.7 atatat
54 1.10 joerg if (vasprintf(&buf, fmt, ap) == -1)
55 1.10 joerg buf = __UNCONST(fmt);
56 1.10 joerg
57 1.10 joerg errno = oerrno;
58 1.10 joerg
59 1.7 atatat /* contruct the tag for the log entry */
60 1.1 mrg if (tcpd_context.file)
61 1.10 joerg syslog(severity, "%s: %s, line %d: %s",
62 1.10 joerg tag, tcpd_context.file, tcpd_context.line, buf);
63 1.1 mrg else
64 1.10 joerg syslog(severity, "%s: %s", tag, buf);
65 1.7 atatat
66 1.10 joerg if (buf != fmt)
67 1.10 joerg free(buf);
68 1.1 mrg }
69 1.1 mrg
70 1.1 mrg /* tcpd_warn - report problem of some sort and proceed */
71 1.1 mrg
72 1.9 matt void
73 1.9 matt tcpd_warn(const char *format, ...)
74 1.1 mrg {
75 1.1 mrg va_list ap;
76 1.1 mrg
77 1.9 matt va_start(ap, format);
78 1.1 mrg tcpd_diag(LOG_ERR, "warning", format, ap);
79 1.9 matt va_end(ap);
80 1.1 mrg }
81 1.1 mrg
82 1.1 mrg /* tcpd_jump - report serious problem and jump */
83 1.1 mrg
84 1.9 matt void
85 1.9 matt tcpd_jump(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, "error", format, ap);
91 1.9 matt va_end(ap);
92 1.1 mrg longjmp(tcpd_buf, AC_ERROR);
93 1.1 mrg }
94