GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ldpd/log.c Lines: 0 55 0.0 %
Date: 2017-11-07 Branches: 0 16 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: log.c,v 1.34 2017/03/21 12:06:55 bluhm Exp $ */
2
3
/*
4
 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <errno.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
#include <syslog.h>
24
#include <unistd.h>
25
#include <netdb.h>
26
#include <limits.h>
27
28
#include "log.h"
29
#include "ldpd.h"
30
31
int		 debug;
32
int		 verbose;
33
const char	*log_procname;
34
35
void
36
log_init(int n_debug)
37
{
38
	extern char	*__progname;
39
40
	debug = n_debug;
41
42
	if (!debug)
43
		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
44
45
	tzset();
46
}
47
48
void
49
log_verbose(int v)
50
{
51
	verbose = v;
52
}
53
54
void
55
logit(int pri, const char *fmt, ...)
56
{
57
	va_list	ap;
58
59
	va_start(ap, fmt);
60
	vlog(pri, fmt, ap);
61
	va_end(ap);
62
}
63
64
void
65
vlog(int pri, const char *fmt, va_list ap)
66
{
67
	char	*nfmt;
68
69
	if (debug) {
70
		/* best effort in out of mem situations */
71
		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
72
			vfprintf(stderr, fmt, ap);
73
			fprintf(stderr, "\n");
74
		} else {
75
			vfprintf(stderr, nfmt, ap);
76
			free(nfmt);
77
		}
78
		fflush(stderr);
79
	} else
80
		vsyslog(pri, fmt, ap);
81
}
82
83
void
84
log_warn(const char *emsg, ...)
85
{
86
	char	*nfmt;
87
	va_list	 ap;
88
89
	/* best effort to even work in out of memory situations */
90
	if (emsg == NULL)
91
		logit(LOG_ERR, "%s", strerror(errno));
92
	else {
93
		va_start(ap, emsg);
94
95
		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
96
			/* we tried it... */
97
			vlog(LOG_ERR, emsg, ap);
98
			logit(LOG_ERR, "%s", strerror(errno));
99
		} else {
100
			vlog(LOG_ERR, nfmt, ap);
101
			free(nfmt);
102
		}
103
		va_end(ap);
104
	}
105
}
106
107
void
108
log_warnx(const char *emsg, ...)
109
{
110
	va_list	 ap;
111
112
	va_start(ap, emsg);
113
	vlog(LOG_ERR, emsg, ap);
114
	va_end(ap);
115
}
116
117
void
118
log_info(const char *emsg, ...)
119
{
120
	va_list	 ap;
121
122
	va_start(ap, emsg);
123
	vlog(LOG_INFO, emsg, ap);
124
	va_end(ap);
125
}
126
127
void
128
log_debug(const char *emsg, ...)
129
{
130
	va_list	 ap;
131
132
	if (verbose & LDPD_OPT_VERBOSE) {
133
		va_start(ap, emsg);
134
		vlog(LOG_DEBUG, emsg, ap);
135
		va_end(ap);
136
	}
137
}
138
139
void
140
fatal(const char *emsg)
141
{
142
	if (emsg == NULL)
143
		logit(LOG_CRIT, "fatal in %s: %s", log_procname,
144
		    strerror(errno));
145
	else
146
		if (errno)
147
			logit(LOG_CRIT, "fatal in %s: %s: %s",
148
			    log_procname, emsg, strerror(errno));
149
		else
150
			logit(LOG_CRIT, "fatal in %s: %s",
151
			    log_procname, emsg);
152
153
	exit(1);
154
}
155
156
void
157
fatalx(const char *emsg)
158
{
159
	errno = 0;
160
	fatal(emsg);
161
}