1 |
|
|
/* $OpenBSD: log.c,v 1.8 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 <sys/types.h> |
20 |
|
|
|
21 |
|
|
#include <arpa/inet.h> |
22 |
|
|
#include <errno.h> |
23 |
|
|
#include <netdb.h> |
24 |
|
|
#include <stdarg.h> |
25 |
|
|
#include <stdio.h> |
26 |
|
|
#include <stdlib.h> |
27 |
|
|
#include <string.h> |
28 |
|
|
#include <syslog.h> |
29 |
|
|
#include <time.h> |
30 |
|
|
#include <unistd.h> |
31 |
|
|
#include <arpa/inet.h> |
32 |
|
|
#include <netdb.h> |
33 |
|
|
|
34 |
|
|
#include "eigrpd.h" |
35 |
|
|
#include "log.h" |
36 |
|
|
|
37 |
|
|
int debug; |
38 |
|
|
int verbose; |
39 |
|
|
const char *log_procname; |
40 |
|
|
|
41 |
|
|
void |
42 |
|
|
log_init(int n_debug) |
43 |
|
|
{ |
44 |
|
|
extern char *__progname; |
45 |
|
|
|
46 |
|
|
debug = n_debug; |
47 |
|
|
|
48 |
|
|
if (!debug) |
49 |
|
|
openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); |
50 |
|
|
|
51 |
|
|
tzset(); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
void |
55 |
|
|
log_verbose(int v) |
56 |
|
|
{ |
57 |
|
|
verbose = v; |
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 |
|
|
vlog(int pri, const char *fmt, va_list ap) |
72 |
|
|
{ |
73 |
|
|
char *nfmt; |
74 |
|
|
|
75 |
|
|
if (debug) { |
76 |
|
|
/* best effort in out of mem situations */ |
77 |
|
|
if (asprintf(&nfmt, "%s\n", fmt) == -1) { |
78 |
|
|
vfprintf(stderr, fmt, ap); |
79 |
|
|
fprintf(stderr, "\n"); |
80 |
|
|
} else { |
81 |
|
|
vfprintf(stderr, nfmt, ap); |
82 |
|
|
free(nfmt); |
83 |
|
|
} |
84 |
|
|
fflush(stderr); |
85 |
|
|
} else |
86 |
|
|
vsyslog(pri, fmt, ap); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
void |
90 |
|
|
log_warn(const char *emsg, ...) |
91 |
|
|
{ |
92 |
|
|
char *nfmt; |
93 |
|
|
va_list ap; |
94 |
|
|
|
95 |
|
|
/* best effort to even work in out of memory situations */ |
96 |
|
|
if (emsg == NULL) |
97 |
|
|
logit(LOG_ERR, "%s", strerror(errno)); |
98 |
|
|
else { |
99 |
|
|
va_start(ap, emsg); |
100 |
|
|
|
101 |
|
|
if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) { |
102 |
|
|
/* we tried it... */ |
103 |
|
|
vlog(LOG_ERR, emsg, ap); |
104 |
|
|
logit(LOG_ERR, "%s", strerror(errno)); |
105 |
|
|
} else { |
106 |
|
|
vlog(LOG_ERR, nfmt, ap); |
107 |
|
|
free(nfmt); |
108 |
|
|
} |
109 |
|
|
va_end(ap); |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
void |
114 |
|
|
log_warnx(const char *emsg, ...) |
115 |
|
|
{ |
116 |
|
|
va_list ap; |
117 |
|
|
|
118 |
|
|
va_start(ap, emsg); |
119 |
|
|
vlog(LOG_ERR, emsg, ap); |
120 |
|
|
va_end(ap); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
void |
124 |
|
|
log_info(const char *emsg, ...) |
125 |
|
|
{ |
126 |
|
|
va_list ap; |
127 |
|
|
|
128 |
|
|
va_start(ap, emsg); |
129 |
|
|
vlog(LOG_INFO, emsg, ap); |
130 |
|
|
va_end(ap); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
void |
134 |
|
|
log_debug(const char *emsg, ...) |
135 |
|
|
{ |
136 |
|
|
va_list ap; |
137 |
|
|
|
138 |
|
|
if (verbose) { |
139 |
|
|
va_start(ap, emsg); |
140 |
|
|
vlog(LOG_DEBUG, emsg, ap); |
141 |
|
|
va_end(ap); |
142 |
|
|
} |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
void |
146 |
|
|
fatal(const char *emsg) |
147 |
|
|
{ |
148 |
|
|
if (emsg == NULL) |
149 |
|
|
logit(LOG_CRIT, "fatal in %s: %s", log_procnames[eigrpd_process], |
150 |
|
|
strerror(errno)); |
151 |
|
|
else |
152 |
|
|
if (errno) |
153 |
|
|
logit(LOG_CRIT, "fatal in %s: %s: %s", |
154 |
|
|
log_procnames[eigrpd_process], emsg, strerror(errno)); |
155 |
|
|
else |
156 |
|
|
logit(LOG_CRIT, "fatal in %s: %s", |
157 |
|
|
log_procnames[eigrpd_process], emsg); |
158 |
|
|
|
159 |
|
|
exit(1); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
void |
163 |
|
|
fatalx(const char *emsg) |
164 |
|
|
{ |
165 |
|
|
errno = 0; |
166 |
|
|
fatal(emsg); |
167 |
|
|
} |