1/*	$NetBSD: diag.c,v 1.18 2024/02/09 22:08:31 andvar 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.18 2024/02/09 22:08:31 andvar 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#include "expandm.h"
37
38struct tcpd_context tcpd_context;
39jmp_buf tcpd_buf;
40
41static void tcpd_diag(int, const char *, const char *, va_list)
42    __sysloglike(3,0);
43
44/* tcpd_diag - centralize error reporter */
45
46static void
47tcpd_diag(int severity, const char *tag, const char *fmt, va_list ap)
48{
49    char *buf, *bufx;
50
51    if (vasprintf(&buf, expandm(fmt, NULL, &bufx), ap) == -1)
52	buf = __UNCONST(fmt);
53    free(bufx);
54
55    /* construct the tag for the log entry */
56    if (tcpd_context.file)
57	syslog(severity, "%s: %s, line %d: %s",
58	    tag, tcpd_context.file, tcpd_context.line, buf);
59    else
60	syslog(severity, "%s: %s", tag, buf);
61
62    if (buf != fmt)
63	free(buf);
64}
65
66/* tcpd_warn - report problem of some sort and proceed */
67
68void
69tcpd_warn(const char *format, ...)
70{
71    va_list ap;
72
73    va_start(ap, format);
74    tcpd_diag(LOG_ERR, "warning", format, ap);
75    va_end(ap);
76}
77
78/* tcpd_jump - report serious problem and jump */
79
80void
81tcpd_jump(const char *format, ...)
82{
83    va_list ap;
84
85    va_start(ap, format);
86    tcpd_diag(LOG_ERR, "error", format, ap);
87    va_end(ap);
88    longjmp(tcpd_buf, AC_ERROR);
89}
90