diag.c revision 1.12 1 1.12 christos /* $NetBSD: diag.c,v 1.12 2019/01/10 13:53:26 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.12 christos __RCSID("$NetBSD: diag.c,v 1.12 2019/01/10 13:53:26 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.12 christos char *buf, *buf2, *ptr;
49 1.12 christos
50 1.12 christos if ((ptr = strstr(fmt, "%m")) != NULL) {
51 1.12 christos if (asprintf(&buf, "%.*s%s%s", (int)(ptr - fmt), fmt, strerror(errno),
52 1.12 christos ptr + 2) == -1)
53 1.12 christos buf = __UNCONST(fmt);
54 1.12 christos } else {
55 1.12 christos buf = __UNCONST(fmt);
56 1.12 christos }
57 1.1 mrg
58 1.7 atatat
59 1.12 christos if (vasprintf(&buf2, buf, ap) == -1)
60 1.12 christos buf2 = buf;
61 1.10 joerg
62 1.10 joerg errno = oerrno;
63 1.10 joerg
64 1.7 atatat /* contruct the tag for the log entry */
65 1.1 mrg if (tcpd_context.file)
66 1.10 joerg syslog(severity, "%s: %s, line %d: %s",
67 1.12 christos tag, tcpd_context.file, tcpd_context.line, buf2);
68 1.1 mrg else
69 1.12 christos syslog(severity, "%s: %s", tag, buf2);
70 1.7 atatat
71 1.10 joerg if (buf != fmt)
72 1.10 joerg free(buf);
73 1.12 christos if (buf2 != buf)
74 1.12 christos free(buf2);
75 1.1 mrg }
76 1.1 mrg
77 1.1 mrg /* tcpd_warn - report problem of some sort and proceed */
78 1.1 mrg
79 1.9 matt void
80 1.9 matt tcpd_warn(const char *format, ...)
81 1.1 mrg {
82 1.1 mrg va_list ap;
83 1.1 mrg
84 1.9 matt va_start(ap, format);
85 1.1 mrg tcpd_diag(LOG_ERR, "warning", format, ap);
86 1.9 matt va_end(ap);
87 1.1 mrg }
88 1.1 mrg
89 1.1 mrg /* tcpd_jump - report serious problem and jump */
90 1.1 mrg
91 1.9 matt void
92 1.9 matt tcpd_jump(const char *format, ...)
93 1.1 mrg {
94 1.1 mrg va_list ap;
95 1.1 mrg
96 1.9 matt va_start(ap, format);
97 1.1 mrg tcpd_diag(LOG_ERR, "error", format, ap);
98 1.9 matt va_end(ap);
99 1.1 mrg longjmp(tcpd_buf, AC_ERROR);
100 1.1 mrg }
101