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