diag.c revision 1.13
1/*	$NetBSD: diag.c,v 1.13 2019/01/10 13:53:58 christos 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
17static char sccsid[] = "@(#) diag.c 1.1 94/12/28 17:42:20";
18#else
19__RCSID("$NetBSD: diag.c,v 1.13 2019/01/10 13:53:58 christos Exp $");
20#endif
21#endif
22
23/* System libraries */
24
25#include <syslog.h>
26#include <stdarg.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <setjmp.h>
30#include <string.h>
31#include <errno.h>
32
33/* Local stuff */
34
35#include "tcpd.h"
36
37struct tcpd_context tcpd_context;
38jmp_buf tcpd_buf;
39
40static void tcpd_diag(int, const char *, const char *, va_list)
41    __sysloglike(3,0);
42
43/* tcpd_diag - centralize error reporter */
44
45static void
46tcpd_diag(int severity, const char *tag, const char *fmt, va_list ap)
47{
48    char *buf, *buf2, *ptr;
49
50    if ((ptr = strstr(fmt, "%m")) != NULL) {
51	if (asprintf(&buf, "%.*s%s%s", (int)(ptr - fmt), fmt, strerror(errno),
52	    ptr + 2) == -1)
53	    buf = __UNCONST(fmt);
54    } else {
55	buf = __UNCONST(fmt);
56    }
57
58
59    if (vasprintf(&buf2, buf, ap) == -1)
60	buf2 = buf;
61
62    /* contruct the tag for the log entry */
63    if (tcpd_context.file)
64	syslog(severity, "%s: %s, line %d: %s",
65	    tag, tcpd_context.file, tcpd_context.line, buf2);
66    else
67	syslog(severity, "%s: %s", tag, buf2);
68
69    if (buf != fmt)
70        free(buf);
71    if (buf2 != buf)
72	free(buf2);
73}
74
75/* tcpd_warn - report problem of some sort and proceed */
76
77void
78tcpd_warn(const char *format, ...)
79{
80    va_list ap;
81
82    va_start(ap, format);
83    tcpd_diag(LOG_ERR, "warning", format, ap);
84    va_end(ap);
85}
86
87/* tcpd_jump - report serious problem and jump */
88
89void
90tcpd_jump(const char *format, ...)
91{
92    va_list ap;
93
94    va_start(ap, format);
95    tcpd_diag(LOG_ERR, "error", format, ap);
96    va_end(ap);
97    longjmp(tcpd_buf, AC_ERROR);
98}
99