GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/route6d/log.c Lines: 0 66 0.0 %
Date: 2017-11-13 Branches: 0 18 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: log.c,v 1.4 2017/07/28 13:05:21 florian 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 <err.h>
20
#include <errno.h>
21
#include <netdb.h>
22
#include <stdarg.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <syslog.h>
27
#include <time.h>
28
#include <unistd.h>
29
30
#include "log.h"
31
32
static int debug;
33
34
static char	*logqueue;
35
36
#define LOGQUEUE_SIZE 1024
37
38
void	logit(int, const char *, ...);
39
void	vlog(int pri, const char *fmt, va_list ap);
40
41
void
42
log_init(int n_debug)
43
{
44
	extern char	*__progname;
45
46
	if (logqueue == NULL) {
47
		logqueue = calloc(LOGQUEUE_SIZE, 1);
48
		if (logqueue == NULL)
49
			err(1, NULL);
50
	}
51
52
	debug = n_debug;
53
54
	if (!debug)
55
		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
56
57
	tzset();
58
}
59
60
void
61
logit(int pri, const char *fmt, ...)
62
{
63
	va_list	ap;
64
65
	va_start(ap, fmt);
66
	vlog(pri, fmt, ap);
67
	va_end(ap);
68
}
69
70
void
71
log_enqueue(const char *fmt, ...)
72
{
73
	char tmpbuf[LOGQUEUE_SIZE];
74
	va_list ap;
75
76
	va_start(ap, fmt);
77
	(void)vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
78
	va_end(ap);
79
	(void)strlcat(logqueue, tmpbuf, LOGQUEUE_SIZE);
80
}
81
82
83
void
84
vlog(int pri, const char *fmt, va_list ap)
85
{
86
	char	tmpbuf[1024];
87
	char	logbuf[LOGQUEUE_SIZE + sizeof(tmpbuf)];
88
89
	if (pri == LOG_DEBUG && !debug) {
90
		logqueue[0] = '\0';
91
		return;
92
	}
93
94
	(void)vsnprintf(tmpbuf, sizeof(tmpbuf), fmt, ap);
95
	(void)strlcpy(logbuf, logqueue, sizeof(logbuf));
96
	(void)strlcat(logbuf, tmpbuf, sizeof(logbuf));
97
98
	logqueue[0] = '\0';
99
100
	if (debug) {
101
		fprintf(stderr, "%s\n", logbuf);
102
		fflush(stderr);
103
	} else
104
		syslog(pri, "%s", logbuf);
105
}
106
107
void
108
log_warn(const char *emsg, ...)
109
{
110
	char	*nfmt;
111
	va_list	 ap;
112
113
	if (emsg == NULL)
114
		logit(LOG_ERR, "%s", strerror(errno));
115
	else {
116
		va_start(ap, emsg);
117
118
		/* best effort to even work in out of memory situations */
119
		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
120
			/* we tried it... */
121
			vlog(LOG_ERR, emsg, ap);
122
			logit(LOG_ERR, "%s", strerror(errno));
123
		} else {
124
			vlog(LOG_ERR, nfmt, ap);
125
			free(nfmt);
126
		}
127
		va_end(ap);
128
	}
129
}
130
131
void
132
log_warnx(const char *emsg, ...)
133
{
134
	va_list	 ap;
135
136
	va_start(ap, emsg);
137
	vlog(LOG_ERR, emsg, ap);
138
	va_end(ap);
139
}
140
141
void
142
log_info(const char *emsg, ...)
143
{
144
	va_list	 ap;
145
146
	va_start(ap, emsg);
147
	vlog(LOG_INFO, emsg, ap);
148
	va_end(ap);
149
}
150
151
void
152
log_debug(const char *emsg, ...)
153
{
154
	va_list	 ap;
155
156
	va_start(ap, emsg);
157
	vlog(LOG_DEBUG, emsg, ap);
158
	va_end(ap);
159
}
160
161
void
162
fatal(const char *emsg)
163
{
164
	extern char	*__progname;
165
	extern __dead void rtdexit(void);
166
167
	if (emsg == NULL)
168
		logit(LOG_CRIT, "fatal in %s: %s", __progname,
169
		    strerror(errno));
170
	else
171
		if (errno)
172
			logit(LOG_CRIT, "fatal in %s: %s: %s",
173
			    __progname, emsg, strerror(errno));
174
		else
175
			logit(LOG_CRIT, "fatal in %s: %s",
176
			    __progname, emsg);
177
178
	rtdexit();
179
}
180
181
void
182
fatalx(const char *emsg)
183
{
184
	errno = 0;
185
	fatal(emsg);
186
}