]>
Commit | Line | Data |
---|---|---|
274a4a44 | 1 | /* |
274a4a44 | 2 | * Logging of zebra |
718e3744 | 3 | * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * GNU Zebra is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 20 | */ |
21 | ||
ab0181ee | 22 | #define FRR_DEFINE_DESC_TABLE |
e0ca5fde | 23 | |
718e3744 | 24 | #include <zebra.h> |
25 | ||
8613585e | 26 | #include "zclient.h" |
718e3744 | 27 | #include "log.h" |
deaa50db | 28 | #include "log_int.h" |
718e3744 | 29 | #include "memory.h" |
30 | #include "command.h" | |
481bc15f DS |
31 | #include "lib_errors.h" |
32 | ||
7d149b8e | 33 | #ifndef SUNOS_5 |
34 | #include <sys/un.h> | |
35 | #endif | |
30a2231a PJ |
36 | /* for printstack on solaris */ |
37 | #ifdef HAVE_UCONTEXT_H | |
38 | #include <ucontext.h> | |
39 | #endif | |
718e3744 | 40 | |
4a1ab8e4 DL |
41 | DEFINE_MTYPE_STATIC(LIB, ZLOG, "Logging") |
42 | ||
d62a17ae | 43 | static int logfile_fd = -1; /* Used in signal handler. */ |
1e221354 | 44 | |
718e3744 | 45 | struct zlog *zlog_default = NULL; |
d34cb7f0 | 46 | bool zlog_startup_stderr = true; |
718e3744 | 47 | |
dd73dca9 QY |
48 | /* lock protecting zlog_default for mt-safe zlog */ |
49 | pthread_mutex_t loglock = PTHREAD_MUTEX_INITIALIZER; | |
50 | ||
d62a17ae | 51 | const char *zlog_priority[] = { |
52 | "emergencies", "alerts", "critical", "errors", "warnings", | |
53 | "notifications", "informational", "debugging", NULL, | |
718e3744 | 54 | }; |
718e3744 | 55 | |
7a49a5b5 DS |
56 | /* |
57 | * write_wrapper | |
58 | * | |
59 | * glibc has declared that the return value from write *must* not be | |
60 | * ignored. | |
61 | * gcc see's this problem and issues a warning for the line. | |
62 | * | |
63 | * Why is this a big deal you say? Because both of them are right | |
64 | * and if you have -Werror enabled then all calls to write | |
65 | * generate a build error and the build stops. | |
66 | * | |
67 | * clang has helpfully allowed this construct: | |
68 | * (void)write(...) | |
69 | * to tell the compiler yeah I know it has a return value | |
70 | * I don't care about it at this time. | |
71 | * gcc doesn't have this ability. | |
72 | * | |
73 | * This code was written such that it didn't care about the | |
74 | * return value from write. At this time do I want | |
75 | * to go through and fix and test this code for correctness. | |
76 | * So just wrapper the bad behavior and move on. | |
77 | */ | |
d62a17ae | 78 | static void write_wrapper(int fd, const void *buf, size_t count) |
7a49a5b5 | 79 | { |
d62a17ae | 80 | if (write(fd, buf, count) <= 0) |
81 | return; | |
7a49a5b5 | 82 | |
d62a17ae | 83 | return; |
7a49a5b5 | 84 | } |
6b0655a2 | 85 | |
56b40679 QY |
86 | /** |
87 | * Looks up a message in a message list by key. | |
88 | * | |
89 | * If the message is not found, returns the provided error message. | |
90 | * | |
91 | * Terminates when it hits a struct message that's all zeros. | |
92 | * | |
93 | * @param mz the message list | |
94 | * @param kz the message key | |
95 | * @param nf the message to return if not found | |
96 | * @return the message | |
97 | */ | |
d62a17ae | 98 | const char *lookup_msg(const struct message *mz, int kz, const char *nf) |
56b40679 | 99 | { |
d62a17ae | 100 | static struct message nt = {0}; |
101 | const char *rz = nf ? nf : "(no message found)"; | |
102 | const struct message *pnt; | |
103 | for (pnt = mz; memcmp(pnt, &nt, sizeof(struct message)); pnt++) | |
104 | if (pnt->key == kz) { | |
105 | rz = pnt->str ? pnt->str : rz; | |
106 | break; | |
107 | } | |
108 | return rz; | |
56b40679 | 109 | } |
718e3744 | 110 | |
56b40679 | 111 | /* For time string format. */ |
d62a17ae | 112 | size_t quagga_timestamp(int timestamp_precision, char *buf, size_t buflen) |
718e3744 | 113 | { |
d62a17ae | 114 | static struct { |
115 | time_t last; | |
116 | size_t len; | |
117 | char buf[28]; | |
118 | } cache; | |
119 | struct timeval clock; | |
120 | ||
121 | gettimeofday(&clock, NULL); | |
122 | ||
123 | /* first, we update the cache if the time has changed */ | |
124 | if (cache.last != clock.tv_sec) { | |
125 | struct tm *tm; | |
126 | cache.last = clock.tv_sec; | |
127 | tm = localtime(&cache.last); | |
128 | cache.len = strftime(cache.buf, sizeof(cache.buf), | |
129 | "%Y/%m/%d %H:%M:%S", tm); | |
130 | } | |
131 | /* note: it's not worth caching the subsecond part, because | |
132 | chances are that back-to-back calls are not sufficiently close | |
133 | together | |
134 | for the clock not to have ticked forward */ | |
135 | ||
136 | if (buflen > cache.len) { | |
137 | memcpy(buf, cache.buf, cache.len); | |
138 | if ((timestamp_precision > 0) | |
139 | && (buflen > cache.len + 1 + timestamp_precision)) { | |
140 | /* should we worry about locale issues? */ | |
141 | static const int divisor[] = {0, 100000, 10000, 1000, | |
142 | 100, 10, 1}; | |
143 | int prec; | |
144 | char *p = buf + cache.len + 1 | |
145 | + (prec = timestamp_precision); | |
146 | *p-- = '\0'; | |
147 | while (prec > 6) | |
148 | /* this is unlikely to happen, but protect anyway */ | |
149 | { | |
150 | *p-- = '0'; | |
151 | prec--; | |
152 | } | |
153 | clock.tv_usec /= divisor[prec]; | |
154 | do { | |
155 | *p-- = '0' + (clock.tv_usec % 10); | |
156 | clock.tv_usec /= 10; | |
157 | } while (--prec > 0); | |
158 | *p = '.'; | |
159 | return cache.len + 1 + timestamp_precision; | |
160 | } | |
161 | buf[cache.len] = '\0'; | |
162 | return cache.len; | |
1ed72e0b | 163 | } |
d62a17ae | 164 | if (buflen > 0) |
165 | buf[0] = '\0'; | |
166 | return 0; | |
1ed72e0b | 167 | } |
718e3744 | 168 | |
1ed72e0b | 169 | /* Utility routine for current time printing. */ |
d62a17ae | 170 | static void time_print(FILE *fp, struct timestamp_control *ctl) |
1ed72e0b | 171 | { |
d62a17ae | 172 | if (!ctl->already_rendered) { |
173 | ctl->len = quagga_timestamp(ctl->precision, ctl->buf, | |
174 | sizeof(ctl->buf)); | |
175 | ctl->already_rendered = 1; | |
176 | } | |
177 | fprintf(fp, "%s ", ctl->buf); | |
718e3744 | 178 | } |
d62a17ae | 179 | |
6b0655a2 | 180 | |
d34cb7f0 | 181 | static void vzlog_file(struct zlog *zl, struct timestamp_control *tsctl, |
996c9314 LB |
182 | const char *proto_str, int record_priority, int priority, |
183 | FILE *fp, const char *format, va_list args) | |
d34cb7f0 DL |
184 | { |
185 | va_list ac; | |
186 | ||
187 | time_print(fp, tsctl); | |
188 | if (record_priority) | |
189 | fprintf(fp, "%s: ", zlog_priority[priority]); | |
190 | ||
191 | fprintf(fp, "%s", proto_str); | |
192 | va_copy(ac, args); | |
193 | vfprintf(fp, format, ac); | |
194 | va_end(ac); | |
195 | fprintf(fp, "\n"); | |
196 | fflush(fp); | |
197 | } | |
198 | ||
718e3744 | 199 | /* va_list version of zlog. */ |
d62a17ae | 200 | void vzlog(int priority, const char *format, va_list args) |
718e3744 | 201 | { |
dd73dca9 QY |
202 | pthread_mutex_lock(&loglock); |
203 | ||
d62a17ae | 204 | char proto_str[32]; |
205 | int original_errno = errno; | |
206 | struct timestamp_control tsctl; | |
207 | tsctl.already_rendered = 0; | |
208 | struct zlog *zl = zlog_default; | |
209 | ||
210 | /* When zlog_default is also NULL, use stderr for logging. */ | |
211 | if (zl == NULL) { | |
212 | tsctl.precision = 0; | |
213 | time_print(stderr, &tsctl); | |
214 | fprintf(stderr, "%s: ", "unknown"); | |
215 | vfprintf(stderr, format, args); | |
216 | fprintf(stderr, "\n"); | |
217 | fflush(stderr); | |
218 | ||
219 | /* In this case we return at here. */ | |
220 | errno = original_errno; | |
dd73dca9 | 221 | pthread_mutex_unlock(&loglock); |
d62a17ae | 222 | return; |
223 | } | |
224 | tsctl.precision = zl->timestamp_precision; | |
225 | ||
226 | /* Syslog output */ | |
227 | if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG]) { | |
228 | va_list ac; | |
229 | va_copy(ac, args); | |
230 | vsyslog(priority | zlog_default->facility, format, ac); | |
231 | va_end(ac); | |
232 | } | |
233 | ||
234 | if (zl->instance) | |
235 | sprintf(proto_str, "%s[%d]: ", zl->protoname, zl->instance); | |
236 | else | |
237 | sprintf(proto_str, "%s: ", zl->protoname); | |
238 | ||
239 | /* File output. */ | |
d34cb7f0 | 240 | if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp) |
996c9314 LB |
241 | vzlog_file(zl, &tsctl, proto_str, zl->record_priority, priority, |
242 | zl->fp, format, args); | |
d62a17ae | 243 | |
d34cb7f0 DL |
244 | /* fixed-config logging to stderr while we're stating up & haven't |
245 | * daemonized / reached mainloop yet | |
246 | * | |
247 | * note the "else" on stdout output -- we don't want to print the same | |
248 | * message to both stderr and stdout. */ | |
249 | if (zlog_startup_stderr && priority <= LOG_WARNING) | |
996c9314 LB |
250 | vzlog_file(zl, &tsctl, proto_str, 1, priority, stderr, format, |
251 | args); | |
d34cb7f0 | 252 | else if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT]) |
996c9314 LB |
253 | vzlog_file(zl, &tsctl, proto_str, zl->record_priority, priority, |
254 | stdout, format, args); | |
d62a17ae | 255 | |
256 | /* Terminal monitor. */ | |
257 | if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR]) | |
258 | vty_log((zl->record_priority ? zlog_priority[priority] : NULL), | |
259 | proto_str, format, &tsctl, args); | |
260 | ||
261 | errno = original_errno; | |
dd73dca9 | 262 | pthread_mutex_unlock(&loglock); |
718e3744 | 263 | } |
264 | ||
d62a17ae | 265 | int vzlog_test(int priority) |
65efcfce | 266 | { |
dd73dca9 QY |
267 | pthread_mutex_lock(&loglock); |
268 | ||
269 | int ret = 0; | |
270 | ||
d62a17ae | 271 | struct zlog *zl = zlog_default; |
272 | ||
273 | /* When zlog_default is also NULL, use stderr for logging. */ | |
dd73dca9 QY |
274 | if (zl == NULL) |
275 | ret = 1; | |
d62a17ae | 276 | /* Syslog output */ |
dd73dca9 QY |
277 | else if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG]) |
278 | ret = 1; | |
d62a17ae | 279 | /* File output. */ |
dd73dca9 QY |
280 | else if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp) |
281 | ret = 1; | |
d62a17ae | 282 | /* stdout output. */ |
dd73dca9 QY |
283 | else if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT]) |
284 | ret = 1; | |
d62a17ae | 285 | /* Terminal monitor. */ |
dd73dca9 QY |
286 | else if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR]) |
287 | ret = 1; | |
d62a17ae | 288 | |
dd73dca9 QY |
289 | pthread_mutex_unlock(&loglock); |
290 | ||
291 | return ret; | |
65efcfce LB |
292 | } |
293 | ||
d62a17ae | 294 | static char *str_append(char *dst, int len, const char *src) |
59a06a91 | 295 | { |
d62a17ae | 296 | while ((len-- > 0) && *src) |
297 | *dst++ = *src++; | |
298 | return dst; | |
59a06a91 | 299 | } |
300 | ||
d7c0a89a | 301 | static char *num_append(char *s, int len, unsigned long x) |
59a06a91 | 302 | { |
d62a17ae | 303 | char buf[30]; |
304 | char *t; | |
305 | ||
306 | if (!x) | |
307 | return str_append(s, len, "0"); | |
308 | *(t = &buf[sizeof(buf) - 1]) = '\0'; | |
309 | while (x && (t > buf)) { | |
310 | *--t = '0' + (x % 10); | |
311 | x /= 10; | |
312 | } | |
313 | return str_append(s, len, t); | |
59a06a91 | 314 | } |
315 | ||
fb66b29c | 316 | #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE) |
d7c0a89a | 317 | static char *hex_append(char *s, int len, unsigned long x) |
7d149b8e | 318 | { |
d62a17ae | 319 | char buf[30]; |
320 | char *t; | |
321 | ||
322 | if (!x) | |
323 | return str_append(s, len, "0"); | |
324 | *(t = &buf[sizeof(buf) - 1]) = '\0'; | |
325 | while (x && (t > buf)) { | |
d7c0a89a | 326 | unsigned int cc = (x % 16); |
d62a17ae | 327 | *--t = ((cc < 10) ? ('0' + cc) : ('a' + cc - 10)); |
328 | x /= 16; | |
329 | } | |
330 | return str_append(s, len, t); | |
7d149b8e | 331 | } |
31364274 | 332 | #endif |
7d149b8e | 333 | |
7d149b8e | 334 | /* Needs to be enhanced to support Solaris. */ |
d62a17ae | 335 | static int syslog_connect(void) |
7d149b8e | 336 | { |
337 | #ifdef SUNOS_5 | |
d62a17ae | 338 | return -1; |
7d149b8e | 339 | #else |
d62a17ae | 340 | int fd; |
341 | char *s; | |
342 | struct sockaddr_un addr; | |
7d149b8e | 343 | |
d62a17ae | 344 | if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) |
345 | return -1; | |
346 | addr.sun_family = AF_UNIX; | |
7d149b8e | 347 | #ifdef _PATH_LOG |
348 | #define SYSLOG_SOCKET_PATH _PATH_LOG | |
349 | #else | |
350 | #define SYSLOG_SOCKET_PATH "/dev/log" | |
351 | #endif | |
d62a17ae | 352 | s = str_append(addr.sun_path, sizeof(addr.sun_path), |
353 | SYSLOG_SOCKET_PATH); | |
7d149b8e | 354 | #undef SYSLOG_SOCKET_PATH |
d62a17ae | 355 | *s = '\0'; |
356 | if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { | |
357 | close(fd); | |
358 | return -1; | |
359 | } | |
360 | return fd; | |
7d149b8e | 361 | #endif |
362 | } | |
363 | ||
d62a17ae | 364 | static void syslog_sigsafe(int priority, const char *msg, size_t msglen) |
7d149b8e | 365 | { |
d62a17ae | 366 | static int syslog_fd = -1; |
367 | char buf[sizeof("<1234567890>ripngd[1234567890]: ") + msglen + 50]; | |
368 | char *s; | |
7d149b8e | 369 | |
d62a17ae | 370 | if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0)) |
371 | return; | |
7d149b8e | 372 | |
373 | #define LOC s,buf+sizeof(buf)-s | |
d62a17ae | 374 | s = buf; |
375 | s = str_append(LOC, "<"); | |
376 | s = num_append(LOC, priority); | |
377 | s = str_append(LOC, ">"); | |
378 | /* forget about the timestamp, too difficult in a signal handler */ | |
379 | s = str_append(LOC, zlog_default->ident); | |
380 | if (zlog_default->syslog_options & LOG_PID) { | |
381 | s = str_append(LOC, "["); | |
382 | s = num_append(LOC, getpid()); | |
383 | s = str_append(LOC, "]"); | |
384 | } | |
385 | s = str_append(LOC, ": "); | |
386 | s = str_append(LOC, msg); | |
387 | write_wrapper(syslog_fd, buf, s - buf); | |
7d149b8e | 388 | #undef LOC |
389 | } | |
390 | ||
d62a17ae | 391 | static int open_crashlog(void) |
1e221354 | 392 | { |
393 | #define CRASHLOG_PREFIX "/var/tmp/quagga." | |
394 | #define CRASHLOG_SUFFIX "crashlog" | |
d62a17ae | 395 | if (zlog_default && zlog_default->ident) { |
396 | /* Avoid strlen since it is not async-signal-safe. */ | |
397 | const char *p; | |
398 | size_t ilen; | |
399 | ||
400 | for (p = zlog_default->ident, ilen = 0; *p; p++) | |
401 | ilen++; | |
402 | { | |
403 | char buf[sizeof(CRASHLOG_PREFIX) + ilen | |
404 | + sizeof(CRASHLOG_SUFFIX) + 3]; | |
405 | char *s = buf; | |
1e221354 | 406 | #define LOC s,buf+sizeof(buf)-s |
d62a17ae | 407 | s = str_append(LOC, CRASHLOG_PREFIX); |
408 | s = str_append(LOC, zlog_default->ident); | |
409 | s = str_append(LOC, "."); | |
410 | s = str_append(LOC, CRASHLOG_SUFFIX); | |
1e221354 | 411 | #undef LOC |
d62a17ae | 412 | *s = '\0'; |
413 | return open(buf, O_WRONLY | O_CREAT | O_EXCL, | |
414 | LOGFILE_MASK); | |
415 | } | |
416 | } | |
417 | return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, | |
418 | O_WRONLY | O_CREAT | O_EXCL, LOGFILE_MASK); | |
1e221354 | 419 | #undef CRASHLOG_SUFFIX |
420 | #undef CRASHLOG_PREFIX | |
421 | } | |
422 | ||
7d149b8e | 423 | /* Note: the goal here is to use only async-signal-safe functions. */ |
d62a17ae | 424 | void zlog_signal(int signo, const char *action |
31364274 | 425 | #ifdef SA_SIGINFO |
d62a17ae | 426 | , |
427 | siginfo_t *siginfo, void *program_counter | |
31364274 | 428 | #endif |
9d303b37 | 429 | ) |
59a06a91 | 430 | { |
d62a17ae | 431 | time_t now; |
432 | char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...") | |
433 | + 100]; | |
434 | char *s = buf; | |
435 | char *msgstart = buf; | |
59a06a91 | 436 | #define LOC s,buf+sizeof(buf)-s |
437 | ||
d62a17ae | 438 | time(&now); |
439 | if (zlog_default) { | |
440 | s = str_append(LOC, zlog_default->protoname); | |
441 | *s++ = ':'; | |
442 | *s++ = ' '; | |
443 | msgstart = s; | |
444 | } | |
445 | s = str_append(LOC, "Received signal "); | |
446 | s = num_append(LOC, signo); | |
447 | s = str_append(LOC, " at "); | |
448 | s = num_append(LOC, now); | |
31364274 | 449 | #ifdef SA_SIGINFO |
d62a17ae | 450 | s = str_append(LOC, " (si_addr 0x"); |
d7c0a89a | 451 | s = hex_append(LOC, (unsigned long)(siginfo->si_addr)); |
d62a17ae | 452 | if (program_counter) { |
453 | s = str_append(LOC, ", PC 0x"); | |
d7c0a89a | 454 | s = hex_append(LOC, (unsigned long)program_counter); |
d62a17ae | 455 | } |
456 | s = str_append(LOC, "); "); | |
457 | #else /* SA_SIGINFO */ | |
458 | s = str_append(LOC, "; "); | |
31364274 | 459 | #endif /* SA_SIGINFO */ |
d62a17ae | 460 | s = str_append(LOC, action); |
461 | if (s < buf + sizeof(buf)) | |
462 | *s++ = '\n'; | |
59a06a91 | 463 | |
d62a17ae | 464 | /* N.B. implicit priority is most severe */ |
1e221354 | 465 | #define PRI LOG_CRIT |
274a4a44 | 466 | |
7a49a5b5 | 467 | #define DUMP(FD) write_wrapper(FD, buf, s-buf); |
d62a17ae | 468 | /* If no file logging configured, try to write to fallback log file. */ |
469 | if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0)) | |
470 | DUMP(logfile_fd) | |
471 | if (!zlog_default) | |
472 | DUMP(STDERR_FILENO) | |
473 | else { | |
474 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) | |
475 | DUMP(STDOUT_FILENO) | |
476 | /* Remove trailing '\n' for monitor and syslog */ | |
477 | *--s = '\0'; | |
478 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) | |
479 | vty_log_fixed(buf, s - buf); | |
480 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) | |
481 | syslog_sigsafe(PRI | zlog_default->facility, msgstart, | |
482 | s - msgstart); | |
483 | } | |
59a06a91 | 484 | #undef DUMP |
485 | ||
d62a17ae | 486 | zlog_backtrace_sigsafe(PRI, |
31364274 | 487 | #ifdef SA_SIGINFO |
d62a17ae | 488 | program_counter |
31364274 | 489 | #else |
d62a17ae | 490 | NULL |
31364274 | 491 | #endif |
9d303b37 | 492 | ); |
d62a17ae | 493 | |
494 | s = buf; | |
495 | struct thread *tc; | |
496 | tc = pthread_getspecific(thread_current); | |
497 | if (!tc) | |
498 | s = str_append(LOC, "no thread information available\n"); | |
499 | else { | |
500 | s = str_append(LOC, "in thread "); | |
501 | s = str_append(LOC, tc->funcname); | |
502 | s = str_append(LOC, " scheduled from "); | |
503 | s = str_append(LOC, tc->schedfrom); | |
504 | s = str_append(LOC, ":"); | |
505 | s = num_append(LOC, tc->schedfrom_line); | |
506 | s = str_append(LOC, "\n"); | |
507 | } | |
d1265948 | 508 | |
69f30024 | 509 | #define DUMP(FD) write_wrapper(FD, buf, s-buf); |
d62a17ae | 510 | /* If no file logging configured, try to write to fallback log file. */ |
511 | if (logfile_fd >= 0) | |
512 | DUMP(logfile_fd) | |
513 | if (!zlog_default) | |
514 | DUMP(STDERR_FILENO) | |
515 | else { | |
516 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) | |
517 | DUMP(STDOUT_FILENO) | |
518 | /* Remove trailing '\n' for monitor and syslog */ | |
519 | *--s = '\0'; | |
520 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) | |
521 | vty_log_fixed(buf, s - buf); | |
522 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) | |
523 | syslog_sigsafe(PRI | zlog_default->facility, msgstart, | |
524 | s - msgstart); | |
525 | } | |
d1265948 DL |
526 | #undef DUMP |
527 | ||
274a4a44 | 528 | #undef PRI |
063ee52a | 529 | #undef LOC |
530 | } | |
531 | ||
532 | /* Log a backtrace using only async-signal-safe functions. | |
533 | Needs to be enhanced to support syslog logging. */ | |
d62a17ae | 534 | void zlog_backtrace_sigsafe(int priority, void *program_counter) |
063ee52a | 535 | { |
fb66b29c | 536 | #ifdef HAVE_STACK_TRACE |
d62a17ae | 537 | static const char pclabel[] = "Program counter: "; |
538 | void *array[64]; | |
539 | int size; | |
540 | char buf[100]; | |
541 | char *s, **bt = NULL; | |
063ee52a | 542 | #define LOC s,buf+sizeof(buf)-s |
59a06a91 | 543 | |
fb66b29c | 544 | #ifdef HAVE_GLIBC_BACKTRACE |
d62a17ae | 545 | size = backtrace(array, array_size(array)); |
546 | if (size <= 0 || (size_t)size > array_size(array)) | |
547 | return; | |
548 | ||
549 | #define DUMP(FD) \ | |
550 | { \ | |
551 | if (program_counter) { \ | |
552 | write_wrapper(FD, pclabel, sizeof(pclabel) - 1); \ | |
553 | backtrace_symbols_fd(&program_counter, 1, FD); \ | |
554 | } \ | |
555 | write_wrapper(FD, buf, s - buf); \ | |
556 | backtrace_symbols_fd(array, size, FD); \ | |
557 | } | |
fb66b29c | 558 | #elif defined(HAVE_PRINTSTACK) |
d62a17ae | 559 | #define DUMP(FD) \ |
560 | { \ | |
561 | if (program_counter) \ | |
562 | write_wrapper((FD), pclabel, sizeof(pclabel) - 1); \ | |
563 | write_wrapper((FD), buf, s - buf); \ | |
564 | printstack((FD)); \ | |
565 | } | |
fb66b29c PJ |
566 | #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */ |
567 | ||
d62a17ae | 568 | s = buf; |
569 | s = str_append(LOC, "Backtrace for "); | |
570 | s = num_append(LOC, size); | |
571 | s = str_append(LOC, " stack frames:\n"); | |
572 | ||
573 | if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0)) | |
574 | DUMP(logfile_fd) | |
575 | if (!zlog_default) | |
576 | DUMP(STDERR_FILENO) | |
577 | else { | |
578 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) | |
579 | DUMP(STDOUT_FILENO) | |
580 | /* Remove trailing '\n' for monitor and syslog */ | |
581 | *--s = '\0'; | |
582 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) | |
583 | vty_log_fixed(buf, s - buf); | |
584 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) | |
585 | syslog_sigsafe(priority | zlog_default->facility, buf, | |
586 | s - buf); | |
587 | { | |
588 | int i; | |
94fc1dd4 | 589 | #ifdef HAVE_GLIBC_BACKTRACE |
d62a17ae | 590 | bt = backtrace_symbols(array, size); |
94fc1dd4 | 591 | #endif |
d62a17ae | 592 | /* Just print the function addresses. */ |
593 | for (i = 0; i < size; i++) { | |
594 | s = buf; | |
595 | if (bt) | |
596 | s = str_append(LOC, bt[i]); | |
597 | else { | |
598 | s = str_append(LOC, "[bt "); | |
599 | s = num_append(LOC, i); | |
600 | s = str_append(LOC, "] 0x"); | |
d7c0a89a QY |
601 | s = hex_append( |
602 | LOC, (unsigned long)(array[i])); | |
d62a17ae | 603 | } |
604 | *s = '\0'; | |
605 | if (priority | |
606 | <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) | |
607 | vty_log_fixed(buf, s - buf); | |
608 | if (priority | |
609 | <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) | |
610 | syslog_sigsafe( | |
611 | priority | |
612 | | zlog_default | |
613 | ->facility, | |
614 | buf, s - buf); | |
615 | } | |
616 | if (bt) | |
617 | free(bt); | |
618 | } | |
619 | } | |
59a06a91 | 620 | #undef DUMP |
59a06a91 | 621 | #undef LOC |
fb66b29c | 622 | #endif /* HAVE_STRACK_TRACE */ |
063ee52a | 623 | } |
624 | ||
d62a17ae | 625 | void zlog_backtrace(int priority) |
063ee52a | 626 | { |
627 | #ifndef HAVE_GLIBC_BACKTRACE | |
d62a17ae | 628 | zlog(priority, "No backtrace available on this platform."); |
063ee52a | 629 | #else |
d62a17ae | 630 | void *array[20]; |
631 | int size, i; | |
632 | char **strings; | |
633 | ||
634 | size = backtrace(array, array_size(array)); | |
635 | if (size <= 0 || (size_t)size > array_size(array)) { | |
09c866e3 QY |
636 | flog_err_sys( |
637 | LIB_ERR_SYSTEM_CALL, | |
638 | "Cannot get backtrace, returned invalid # of frames %d " | |
639 | "(valid range is between 1 and %lu)", | |
640 | size, (unsigned long)(array_size(array))); | |
d62a17ae | 641 | return; |
642 | } | |
643 | zlog(priority, "Backtrace for %d stack frames:", size); | |
644 | if (!(strings = backtrace_symbols(array, size))) { | |
09c866e3 QY |
645 | flog_err_sys(LIB_ERR_SYSTEM_CALL, |
646 | "Cannot get backtrace symbols (out of memory?)"); | |
d62a17ae | 647 | for (i = 0; i < size; i++) |
648 | zlog(priority, "[bt %d] %p", i, array[i]); | |
649 | } else { | |
650 | for (i = 0; i < size; i++) | |
651 | zlog(priority, "[bt %d] %s", i, strings[i]); | |
652 | free(strings); | |
653 | } | |
063ee52a | 654 | #endif /* HAVE_GLIBC_BACKTRACE */ |
59a06a91 | 655 | } |
656 | ||
d62a17ae | 657 | void zlog(int priority, const char *format, ...) |
718e3744 | 658 | { |
d62a17ae | 659 | va_list args; |
718e3744 | 660 | |
d62a17ae | 661 | va_start(args, format); |
662 | vzlog(priority, format, args); | |
663 | va_end(args); | |
718e3744 | 664 | } |
665 | ||
d62a17ae | 666 | #define ZLOG_FUNC(FUNCNAME, PRIORITY) \ |
667 | void FUNCNAME(const char *format, ...) \ | |
668 | { \ | |
669 | va_list args; \ | |
670 | va_start(args, format); \ | |
671 | vzlog(PRIORITY, format, args); \ | |
672 | va_end(args); \ | |
673 | } | |
718e3744 | 674 | |
d246bd96 | 675 | ZLOG_FUNC(zlog_err, LOG_ERR) |
718e3744 | 676 | |
d246bd96 | 677 | ZLOG_FUNC(zlog_warn, LOG_WARNING) |
718e3744 | 678 | |
d246bd96 | 679 | ZLOG_FUNC(zlog_info, LOG_INFO) |
718e3744 | 680 | |
d246bd96 | 681 | ZLOG_FUNC(zlog_notice, LOG_NOTICE) |
718e3744 | 682 | |
d246bd96 | 683 | ZLOG_FUNC(zlog_debug, LOG_DEBUG) |
718e3744 | 684 | |
d246bd96 | 685 | #undef ZLOG_FUNC |
718e3744 | 686 | |
d62a17ae | 687 | void zlog_thread_info(int log_level) |
d1265948 | 688 | { |
d62a17ae | 689 | struct thread *tc; |
690 | tc = pthread_getspecific(thread_current); | |
691 | ||
692 | if (tc) | |
693 | zlog(log_level, | |
694 | "Current thread function %s, scheduled from " | |
695 | "file %s, line %u", | |
696 | tc->funcname, tc->schedfrom, tc->schedfrom_line); | |
697 | else | |
698 | zlog(log_level, "Current thread not known/applicable"); | |
d1265948 DL |
699 | } |
700 | ||
d62a17ae | 701 | void _zlog_assert_failed(const char *assertion, const char *file, |
702 | unsigned int line, const char *function) | |
cee3df1e | 703 | { |
d62a17ae | 704 | /* Force fallback file logging? */ |
705 | if (zlog_default && !zlog_default->fp | |
706 | && ((logfile_fd = open_crashlog()) >= 0) | |
707 | && ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL)) | |
708 | zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR; | |
709 | zlog(LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s", | |
710 | assertion, file, line, (function ? function : "?")); | |
711 | zlog_backtrace(LOG_CRIT); | |
712 | zlog_thread_info(LOG_CRIT); | |
9eed278b | 713 | log_memstats(stderr, "log"); |
d62a17ae | 714 | abort(); |
cee3df1e | 715 | } |
716 | ||
d62a17ae | 717 | void memory_oom(size_t size, const char *name) |
3b4cd783 | 718 | { |
09c866e3 QY |
719 | flog_err_sys(LIB_ERR_SYSTEM_CALL, |
720 | "out of memory: failed to allocate %zu bytes for %s" | |
721 | "object", | |
722 | size, name); | |
3b4cd783 DL |
723 | zlog_backtrace(LOG_ERR); |
724 | abort(); | |
725 | } | |
6b0655a2 | 726 | |
718e3744 | 727 | /* Open log stream */ |
d7c0a89a QY |
728 | void openzlog(const char *progname, const char *protoname, |
729 | unsigned short instance, int syslog_flags, int syslog_facility) | |
718e3744 | 730 | { |
d62a17ae | 731 | struct zlog *zl; |
d7c0a89a | 732 | unsigned int i; |
718e3744 | 733 | |
d62a17ae | 734 | zl = XCALLOC(MTYPE_ZLOG, sizeof(struct zlog)); |
718e3744 | 735 | |
d62a17ae | 736 | zl->ident = progname; |
737 | zl->protoname = protoname; | |
738 | zl->instance = instance; | |
739 | zl->facility = syslog_facility; | |
740 | zl->syslog_options = syslog_flags; | |
718e3744 | 741 | |
d62a17ae | 742 | /* Set default logging levels. */ |
743 | for (i = 0; i < array_size(zl->maxlvl); i++) | |
744 | zl->maxlvl[i] = ZLOG_DISABLED; | |
745 | zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG; | |
746 | zl->default_lvl = LOG_DEBUG; | |
274a4a44 | 747 | |
d62a17ae | 748 | openlog(progname, syslog_flags, zl->facility); |
dd73dca9 QY |
749 | |
750 | pthread_mutex_lock(&loglock); | |
d62a17ae | 751 | zlog_default = zl; |
dd73dca9 | 752 | pthread_mutex_unlock(&loglock); |
3f11a103 DL |
753 | |
754 | #ifdef HAVE_GLIBC_BACKTRACE | |
d62a17ae | 755 | /* work around backtrace() using lazily resolved dynamically linked |
756 | * symbols, which will otherwise cause funny breakage in the SEGV | |
757 | * handler. | |
758 | * (particularly, the dynamic linker can call malloc(), which uses locks | |
759 | * in programs linked with -pthread, thus can deadlock.) */ | |
760 | void *bt[4]; | |
761 | backtrace(bt, array_size(bt)); | |
762 | free(backtrace_symbols(bt, 0)); | |
763 | backtrace_symbols_fd(bt, 0, 0); | |
3f11a103 | 764 | #endif |
718e3744 | 765 | } |
766 | ||
d62a17ae | 767 | void closezlog(void) |
718e3744 | 768 | { |
dd73dca9 | 769 | pthread_mutex_lock(&loglock); |
d62a17ae | 770 | struct zlog *zl = zlog_default; |
bf1013e6 | 771 | |
d62a17ae | 772 | closelog(); |
228da428 | 773 | |
d62a17ae | 774 | if (zl->fp != NULL) |
775 | fclose(zl->fp); | |
718e3744 | 776 | |
d62a17ae | 777 | if (zl->filename != NULL) |
778 | XFREE(MTYPE_ZLOG, zl->filename); | |
7e69d993 | 779 | |
d62a17ae | 780 | XFREE(MTYPE_ZLOG, zl); |
781 | zlog_default = NULL; | |
dd73dca9 | 782 | pthread_mutex_unlock(&loglock); |
718e3744 | 783 | } |
784 | ||
785 | /* Called from command.c. */ | |
d62a17ae | 786 | void zlog_set_level(zlog_dest_t dest, int log_level) |
718e3744 | 787 | { |
dd73dca9 | 788 | pthread_mutex_lock(&loglock); |
d62a17ae | 789 | zlog_default->maxlvl[dest] = log_level; |
dd73dca9 | 790 | pthread_mutex_unlock(&loglock); |
718e3744 | 791 | } |
792 | ||
d62a17ae | 793 | int zlog_set_file(const char *filename, int log_level) |
718e3744 | 794 | { |
dd73dca9 | 795 | struct zlog *zl; |
d62a17ae | 796 | FILE *fp; |
797 | mode_t oldumask; | |
dd73dca9 | 798 | int ret = 1; |
d62a17ae | 799 | |
800 | /* There is opend file. */ | |
801 | zlog_reset_file(); | |
802 | ||
803 | /* Open file. */ | |
804 | oldumask = umask(0777 & ~LOGFILE_MASK); | |
805 | fp = fopen(filename, "a"); | |
806 | umask(oldumask); | |
dd73dca9 QY |
807 | if (fp == NULL) { |
808 | ret = 0; | |
809 | } else { | |
c22fbb9b QY |
810 | pthread_mutex_lock(&loglock); |
811 | zl = zlog_default; | |
812 | ||
dd73dca9 QY |
813 | /* Set flags. */ |
814 | zl->filename = XSTRDUP(MTYPE_ZLOG, filename); | |
815 | zl->maxlvl[ZLOG_DEST_FILE] = log_level; | |
816 | zl->fp = fp; | |
817 | logfile_fd = fileno(fp); | |
c22fbb9b | 818 | pthread_mutex_unlock(&loglock); |
dd73dca9 | 819 | } |
d62a17ae | 820 | |
dd73dca9 | 821 | return ret; |
718e3744 | 822 | } |
823 | ||
824 | /* Reset opend file. */ | |
d62a17ae | 825 | int zlog_reset_file(void) |
718e3744 | 826 | { |
dd73dca9 QY |
827 | pthread_mutex_lock(&loglock); |
828 | ||
d62a17ae | 829 | struct zlog *zl = zlog_default; |
718e3744 | 830 | |
d62a17ae | 831 | if (zl->fp) |
832 | fclose(zl->fp); | |
833 | zl->fp = NULL; | |
834 | logfile_fd = -1; | |
835 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; | |
718e3744 | 836 | |
d62a17ae | 837 | if (zl->filename) |
838 | XFREE(MTYPE_ZLOG, zl->filename); | |
839 | zl->filename = NULL; | |
718e3744 | 840 | |
dd73dca9 QY |
841 | pthread_mutex_unlock(&loglock); |
842 | ||
d62a17ae | 843 | return 1; |
718e3744 | 844 | } |
845 | ||
846 | /* Reopen log file. */ | |
d62a17ae | 847 | int zlog_rotate(void) |
718e3744 | 848 | { |
dd73dca9 QY |
849 | pthread_mutex_lock(&loglock); |
850 | ||
d62a17ae | 851 | struct zlog *zl = zlog_default; |
852 | int level; | |
dd73dca9 | 853 | int ret = 1; |
d62a17ae | 854 | |
855 | if (zl->fp) | |
856 | fclose(zl->fp); | |
857 | zl->fp = NULL; | |
858 | logfile_fd = -1; | |
859 | level = zl->maxlvl[ZLOG_DEST_FILE]; | |
860 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; | |
861 | ||
862 | if (zl->filename) { | |
863 | mode_t oldumask; | |
864 | int save_errno; | |
865 | ||
866 | oldumask = umask(0777 & ~LOGFILE_MASK); | |
867 | zl->fp = fopen(zl->filename, "a"); | |
868 | save_errno = errno; | |
869 | umask(oldumask); | |
870 | if (zl->fp == NULL) { | |
dce2036b QY |
871 | |
872 | pthread_mutex_unlock(&loglock); | |
873 | ||
09c866e3 QY |
874 | flog_err_sys( |
875 | LIB_ERR_SYSTEM_CALL, | |
876 | "Log rotate failed: cannot open file %s for append: %s", | |
877 | zl->filename, safe_strerror(save_errno)); | |
dd73dca9 | 878 | ret = -1; |
dce2036b QY |
879 | |
880 | pthread_mutex_lock(&loglock); | |
dd73dca9 QY |
881 | } else { |
882 | logfile_fd = fileno(zl->fp); | |
883 | zl->maxlvl[ZLOG_DEST_FILE] = level; | |
d62a17ae | 884 | } |
d62a17ae | 885 | } |
886 | ||
dd73dca9 QY |
887 | pthread_mutex_unlock(&loglock); |
888 | ||
889 | return ret; | |
718e3744 | 890 | } |
6b0655a2 | 891 | |
ca359769 | 892 | /* Wrapper around strerror to handle case where it returns NULL. */ |
d62a17ae | 893 | const char *safe_strerror(int errnum) |
ca359769 | 894 | { |
d62a17ae | 895 | const char *s = strerror(errnum); |
896 | return (s != NULL) ? s : "Unknown error"; | |
ca359769 | 897 | } |
f52d13cb | 898 | |
d6d672aa PJ |
899 | #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' } |
900 | static const struct zebra_desc_table command_types[] = { | |
d62a17ae | 901 | DESC_ENTRY(ZEBRA_INTERFACE_ADD), |
902 | DESC_ENTRY(ZEBRA_INTERFACE_DELETE), | |
903 | DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD), | |
904 | DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE), | |
905 | DESC_ENTRY(ZEBRA_INTERFACE_UP), | |
906 | DESC_ENTRY(ZEBRA_INTERFACE_DOWN), | |
e0ae31b8 | 907 | DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER), |
0e51b4a3 RW |
908 | DESC_ENTRY(ZEBRA_ROUTE_ADD), |
909 | DESC_ENTRY(ZEBRA_ROUTE_DELETE), | |
7ea7b86e | 910 | DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER), |
d62a17ae | 911 | DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD), |
912 | DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE), | |
913 | DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD), | |
914 | DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE), | |
915 | DESC_ENTRY(ZEBRA_ROUTER_ID_ADD), | |
916 | DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE), | |
917 | DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE), | |
918 | DESC_ENTRY(ZEBRA_HELLO), | |
919 | DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER), | |
920 | DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER), | |
921 | DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE), | |
922 | DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD), | |
923 | DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE), | |
924 | DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE), | |
925 | DESC_ENTRY(ZEBRA_IMPORT_ROUTE_REGISTER), | |
926 | DESC_ENTRY(ZEBRA_IMPORT_ROUTE_UNREGISTER), | |
927 | DESC_ENTRY(ZEBRA_IMPORT_CHECK_UPDATE), | |
928 | DESC_ENTRY(ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD), | |
929 | DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER), | |
930 | DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER), | |
931 | DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE), | |
932 | DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY), | |
74489921 RW |
933 | DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD), |
934 | DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL), | |
d62a17ae | 935 | DESC_ENTRY(ZEBRA_VRF_UNREGISTER), |
936 | DESC_ENTRY(ZEBRA_VRF_ADD), | |
937 | DESC_ENTRY(ZEBRA_VRF_DELETE), | |
c83c5e44 | 938 | DESC_ENTRY(ZEBRA_VRF_LABEL), |
d62a17ae | 939 | DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE), |
940 | DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER), | |
941 | DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV), | |
942 | DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV), | |
943 | DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB), | |
944 | DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS), | |
945 | DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD), | |
946 | DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE), | |
d62a17ae | 947 | DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS), |
948 | DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT), | |
949 | DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK), | |
950 | DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK), | |
951 | DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI), | |
1a98c087 | 952 | DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW), |
31310b25 | 953 | DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET), |
50f74cf1 | 954 | DESC_ENTRY(ZEBRA_LOCAL_ES_ADD), |
955 | DESC_ENTRY(ZEBRA_LOCAL_ES_DEL), | |
d62a17ae | 956 | DESC_ENTRY(ZEBRA_VNI_ADD), |
957 | DESC_ENTRY(ZEBRA_VNI_DEL), | |
b7cfce93 MK |
958 | DESC_ENTRY(ZEBRA_L3VNI_ADD), |
959 | DESC_ENTRY(ZEBRA_L3VNI_DEL), | |
d62a17ae | 960 | DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD), |
961 | DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL), | |
962 | DESC_ENTRY(ZEBRA_MACIP_ADD), | |
963 | DESC_ENTRY(ZEBRA_MACIP_DEL), | |
31310b25 MK |
964 | DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD), |
965 | DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL), | |
d62a17ae | 966 | DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD), |
967 | DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL), | |
6833ae01 | 968 | DESC_ENTRY(ZEBRA_PW_ADD), |
969 | DESC_ENTRY(ZEBRA_PW_DELETE), | |
970 | DESC_ENTRY(ZEBRA_PW_SET), | |
971 | DESC_ENTRY(ZEBRA_PW_UNSET), | |
972 | DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE), | |
e16abbb3 DS |
973 | DESC_ENTRY(ZEBRA_RULE_ADD), |
974 | DESC_ENTRY(ZEBRA_RULE_DELETE), | |
975 | DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER), | |
75fb51c1 PG |
976 | DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT), |
977 | DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK), | |
978 | DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK), | |
d59c13af PG |
979 | DESC_ENTRY(ZEBRA_IPSET_CREATE), |
980 | DESC_ENTRY(ZEBRA_IPSET_DESTROY), | |
981 | DESC_ENTRY(ZEBRA_IPSET_ENTRY_ADD), | |
982 | DESC_ENTRY(ZEBRA_IPSET_ENTRY_DELETE), | |
d6d672aa PJ |
983 | }; |
984 | #undef DESC_ENTRY | |
985 | ||
d62a17ae | 986 | static const struct zebra_desc_table unknown = {0, "unknown", '?'}; |
d6d672aa | 987 | |
d7c0a89a | 988 | static const struct zebra_desc_table *zroute_lookup(unsigned int zroute) |
f52d13cb | 989 | { |
d7c0a89a | 990 | unsigned int i; |
d62a17ae | 991 | |
992 | if (zroute >= array_size(route_types)) { | |
af4c2728 | 993 | flog_err(LIB_ERR_DEVELOPMENT, "unknown zebra route type: %u", |
472878dc | 994 | zroute); |
d62a17ae | 995 | return &unknown; |
996 | } | |
997 | if (zroute == route_types[zroute].type) | |
998 | return &route_types[zroute]; | |
999 | for (i = 0; i < array_size(route_types); i++) { | |
1000 | if (zroute == route_types[i].type) { | |
1001 | zlog_warn( | |
1002 | "internal error: route type table out of order " | |
1003 | "while searching for %u, please notify developers", | |
1004 | zroute); | |
1005 | return &route_types[i]; | |
1006 | } | |
1007 | } | |
af4c2728 | 1008 | flog_err(LIB_ERR_DEVELOPMENT, |
472878dc DS |
1009 | "internal error: cannot find route type %u in table!", |
1010 | zroute); | |
d62a17ae | 1011 | return &unknown; |
f52d13cb | 1012 | } |
1013 | ||
d7c0a89a | 1014 | const char *zebra_route_string(unsigned int zroute) |
f52d13cb | 1015 | { |
d62a17ae | 1016 | return zroute_lookup(zroute)->string; |
f52d13cb | 1017 | } |
1018 | ||
d7c0a89a | 1019 | char zebra_route_char(unsigned int zroute) |
f52d13cb | 1020 | { |
d62a17ae | 1021 | return zroute_lookup(zroute)->chr; |
f52d13cb | 1022 | } |
d6d672aa | 1023 | |
d62a17ae | 1024 | const char *zserv_command_string(unsigned int command) |
d6d672aa | 1025 | { |
d62a17ae | 1026 | if (command >= array_size(command_types)) { |
af4c2728 | 1027 | flog_err(LIB_ERR_DEVELOPMENT, "unknown zserv command type: %u", |
472878dc | 1028 | command); |
d62a17ae | 1029 | return unknown.string; |
1030 | } | |
1031 | return command_types[command].string; | |
d6d672aa | 1032 | } |
7514fb77 | 1033 | |
d62a17ae | 1034 | int proto_name2num(const char *s) |
7514fb77 | 1035 | { |
d62a17ae | 1036 | unsigned i; |
7514fb77 | 1037 | |
d62a17ae | 1038 | for (i = 0; i < array_size(route_types); ++i) |
1039 | if (strcasecmp(s, route_types[i].string) == 0) | |
1040 | return route_types[i].type; | |
1041 | return -1; | |
7514fb77 | 1042 | } |
e0ca5fde | 1043 | |
d62a17ae | 1044 | int proto_redistnum(int afi, const char *s) |
e0ca5fde | 1045 | { |
d62a17ae | 1046 | if (!s) |
1047 | return -1; | |
1048 | ||
1049 | if (afi == AFI_IP) { | |
1050 | if (strmatch(s, "kernel")) | |
1051 | return ZEBRA_ROUTE_KERNEL; | |
1052 | else if (strmatch(s, "connected")) | |
1053 | return ZEBRA_ROUTE_CONNECT; | |
1054 | else if (strmatch(s, "static")) | |
1055 | return ZEBRA_ROUTE_STATIC; | |
1056 | else if (strmatch(s, "rip")) | |
1057 | return ZEBRA_ROUTE_RIP; | |
1058 | else if (strmatch(s, "eigrp")) | |
1059 | return ZEBRA_ROUTE_EIGRP; | |
1060 | else if (strmatch(s, "ospf")) | |
1061 | return ZEBRA_ROUTE_OSPF; | |
1062 | else if (strmatch(s, "isis")) | |
1063 | return ZEBRA_ROUTE_ISIS; | |
1064 | else if (strmatch(s, "bgp")) | |
1065 | return ZEBRA_ROUTE_BGP; | |
1066 | else if (strmatch(s, "table")) | |
1067 | return ZEBRA_ROUTE_TABLE; | |
1068 | else if (strmatch(s, "vnc")) | |
1069 | return ZEBRA_ROUTE_VNC; | |
1070 | else if (strmatch(s, "vnc-direct")) | |
1071 | return ZEBRA_ROUTE_VNC_DIRECT; | |
1072 | else if (strmatch(s, "nhrp")) | |
1073 | return ZEBRA_ROUTE_NHRP; | |
1074 | else if (strmatch(s, "babel")) | |
1075 | return ZEBRA_ROUTE_BABEL; | |
8a71d93d DS |
1076 | else if (strmatch(s, "sharp")) |
1077 | return ZEBRA_ROUTE_SHARP; | |
d62a17ae | 1078 | } |
1079 | if (afi == AFI_IP6) { | |
1080 | if (strmatch(s, "kernel")) | |
1081 | return ZEBRA_ROUTE_KERNEL; | |
1082 | else if (strmatch(s, "connected")) | |
1083 | return ZEBRA_ROUTE_CONNECT; | |
1084 | else if (strmatch(s, "static")) | |
1085 | return ZEBRA_ROUTE_STATIC; | |
1086 | else if (strmatch(s, "ripng")) | |
1087 | return ZEBRA_ROUTE_RIPNG; | |
1088 | else if (strmatch(s, "ospf6")) | |
1089 | return ZEBRA_ROUTE_OSPF6; | |
1090 | else if (strmatch(s, "isis")) | |
1091 | return ZEBRA_ROUTE_ISIS; | |
1092 | else if (strmatch(s, "bgp")) | |
1093 | return ZEBRA_ROUTE_BGP; | |
1094 | else if (strmatch(s, "table")) | |
1095 | return ZEBRA_ROUTE_TABLE; | |
1096 | else if (strmatch(s, "vnc")) | |
1097 | return ZEBRA_ROUTE_VNC; | |
1098 | else if (strmatch(s, "vnc-direct")) | |
1099 | return ZEBRA_ROUTE_VNC_DIRECT; | |
1100 | else if (strmatch(s, "nhrp")) | |
1101 | return ZEBRA_ROUTE_NHRP; | |
1102 | else if (strmatch(s, "babel")) | |
1103 | return ZEBRA_ROUTE_BABEL; | |
8a71d93d DS |
1104 | else if (strmatch(s, "sharp")) |
1105 | return ZEBRA_ROUTE_SHARP; | |
d62a17ae | 1106 | } |
1107 | return -1; | |
e0ca5fde | 1108 | } |
99a6c6cd | 1109 | |
d62a17ae | 1110 | void zlog_hexdump(const void *mem, unsigned int len) |
1111 | { | |
1112 | unsigned long i = 0; | |
1113 | unsigned int j = 0; | |
1114 | unsigned int columns = 8; | |
abccc775 QY |
1115 | /* |
1116 | * 19 bytes for 0xADDRESS: | |
1117 | * 24 bytes for data; 2 chars plus a space per data byte | |
1118 | * 1 byte for space | |
1119 | * 8 bytes for ASCII representation | |
1120 | * 1 byte for a newline | |
1121 | * ===================== | |
1122 | * 53 bytes per 8 bytes of data | |
1123 | * 1 byte for null term | |
1124 | */ | |
e049c5fc QY |
1125 | size_t bs = ((len / 8) + 1) * 53 + 1; |
1126 | char buf[bs]; | |
d62a17ae | 1127 | char *s = buf; |
1128 | ||
e049c5fc QY |
1129 | memset(buf, 0, sizeof(buf)); |
1130 | ||
d62a17ae | 1131 | for (i = 0; i < len + ((len % columns) ? (columns - len % columns) : 0); |
1132 | i++) { | |
1133 | /* print offset */ | |
1134 | if (i % columns == 0) | |
e049c5fc QY |
1135 | s += snprintf(s, bs - (s - buf), |
1136 | "0x%016lx: ", (unsigned long)mem + i); | |
d62a17ae | 1137 | |
1138 | /* print hex data */ | |
1139 | if (i < len) | |
e049c5fc QY |
1140 | s += snprintf(s, bs - (s - buf), "%02x ", |
1141 | 0xFF & ((const char *)mem)[i]); | |
d62a17ae | 1142 | |
1143 | /* end of block, just aligning for ASCII dump */ | |
1144 | else | |
e049c5fc | 1145 | s += snprintf(s, bs - (s - buf), " "); |
d62a17ae | 1146 | |
1147 | /* print ASCII dump */ | |
1148 | if (i % columns == (columns - 1)) { | |
1149 | for (j = i - (columns - 1); j <= i; j++) { | |
e049c5fc QY |
1150 | /* end of block not really printing */ |
1151 | if (j >= len) | |
1152 | s += snprintf(s, bs - (s - buf), " "); | |
1153 | else if (isprint((int)((const char *)mem)[j])) | |
1154 | s += snprintf( | |
1155 | s, bs - (s - buf), "%c", | |
d62a17ae | 1156 | 0xFF & ((const char *)mem)[j]); |
d62a17ae | 1157 | else /* other char */ |
e049c5fc | 1158 | s += snprintf(s, bs - (s - buf), "."); |
d62a17ae | 1159 | } |
e049c5fc | 1160 | s += snprintf(s, bs - (s - buf), "\n"); |
d62a17ae | 1161 | } |
1162 | } | |
1163 | zlog_debug("\n%s", buf); | |
99a6c6cd | 1164 | } |
1f806fc2 | 1165 | |
d62a17ae | 1166 | const char *zlog_sanitize(char *buf, size_t bufsz, const void *in, size_t inlen) |
1f806fc2 | 1167 | { |
d62a17ae | 1168 | const char *inbuf = in; |
1169 | char *pos = buf, *end = buf + bufsz; | |
1170 | const char *iend = inbuf + inlen; | |
1171 | ||
1172 | memset(buf, 0, bufsz); | |
1173 | for (; inbuf < iend; inbuf++) { | |
1174 | /* don't write partial escape sequence */ | |
1175 | if (end - pos < 5) | |
1176 | break; | |
1177 | ||
1178 | if (*inbuf == '\n') | |
1179 | snprintf(pos, end - pos, "\\n"); | |
1180 | else if (*inbuf == '\r') | |
1181 | snprintf(pos, end - pos, "\\r"); | |
1182 | else if (*inbuf == '\t') | |
1183 | snprintf(pos, end - pos, "\\t"); | |
1184 | else if (*inbuf < ' ' || *inbuf == '"' || *inbuf >= 127) | |
1185 | snprintf(pos, end - pos, "\\x%02hhx", *inbuf); | |
1186 | else | |
1187 | *pos = *inbuf; | |
1188 | ||
1189 | pos += strlen(pos); | |
1190 | } | |
1191 | return buf; | |
1f806fc2 | 1192 | } |