3 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
22 #define FRR_DEFINE_DESC_TABLE
31 #include "lib_errors.h"
38 /* for printstack on solaris */
39 #ifdef HAVE_UCONTEXT_H
44 #define UNW_LOCAL_ONLY
45 #include <libunwind.h>
49 DEFINE_MTYPE_STATIC(LIB
, ZLOG
, "Logging")
51 /* hook for external logging */
52 DEFINE_HOOK(zebra_ext_log
, (int priority
, const char *format
, va_list args
),
53 (priority
, format
, args
));
55 static int logfile_fd
= -1; /* Used in signal handler. */
57 struct zlog
*zlog_default
= NULL
;
58 bool zlog_startup_stderr
= true;
60 /* lock protecting zlog_default for mt-safe zlog */
61 static pthread_mutex_t loglock
= PTHREAD_MUTEX_INITIALIZER
;
63 const char *zlog_priority
[] = {
64 "emergencies", "alerts", "critical", "errors", "warnings",
65 "notifications", "informational", "debugging", NULL
,
68 static char zlog_filters
[ZLOG_FILTERS_MAX
][ZLOG_FILTER_LENGTH_MAX
+ 1];
69 static uint8_t zlog_filter_count
;
72 * look for a match on the filter in the current filters, loglock must be held
74 static int zlog_filter_lookup(const char *lookup
)
76 for (int i
= 0; i
< zlog_filter_count
; i
++) {
77 if (strncmp(lookup
, zlog_filters
[i
], sizeof(zlog_filters
[0]))
84 void zlog_filter_clear(void)
86 pthread_mutex_lock(&loglock
);
87 zlog_filter_count
= 0;
88 pthread_mutex_unlock(&loglock
);
91 int zlog_filter_add(const char *filter
)
93 pthread_mutex_lock(&loglock
);
97 if (zlog_filter_count
>= ZLOG_FILTERS_MAX
) {
102 if (zlog_filter_lookup(filter
) != -1) {
103 /* Filter already present */
108 strlcpy(zlog_filters
[zlog_filter_count
], filter
,
109 sizeof(zlog_filters
[0]));
111 if (zlog_filters
[zlog_filter_count
][0] == '\0') {
112 /* Filter was either empty or didn't get copied correctly */
120 pthread_mutex_unlock(&loglock
);
124 int zlog_filter_del(const char *filter
)
126 pthread_mutex_lock(&loglock
);
128 int found_idx
= zlog_filter_lookup(filter
);
129 int last_idx
= zlog_filter_count
- 1;
132 if (found_idx
== -1) {
133 /* Didn't find the filter to delete */
138 /* Adjust the filter array */
139 memmove(zlog_filters
[found_idx
], zlog_filters
[found_idx
+ 1],
140 (last_idx
- found_idx
) * sizeof(zlog_filters
[0]));
145 pthread_mutex_unlock(&loglock
);
149 /* Dump all filters to buffer, delimited by new line */
150 int zlog_filter_dump(char *buf
, size_t max_size
)
152 pthread_mutex_lock(&loglock
);
157 for (int i
= 0; i
< zlog_filter_count
; i
++) {
158 ret
= snprintf(buf
+ len
, max_size
- len
, " %s\n",
161 if ((ret
< 0) || ((size_t)len
>= max_size
)) {
168 pthread_mutex_unlock(&loglock
);
175 * glibc has declared that the return value from write *must* not be
177 * gcc see's this problem and issues a warning for the line.
179 * Why is this a big deal you say? Because both of them are right
180 * and if you have -Werror enabled then all calls to write
181 * generate a build error and the build stops.
183 * clang has helpfully allowed this construct:
185 * to tell the compiler yeah I know it has a return value
186 * I don't care about it at this time.
187 * gcc doesn't have this ability.
189 * This code was written such that it didn't care about the
190 * return value from write. At this time do I want
191 * to go through and fix and test this code for correctness.
192 * So just wrapper the bad behavior and move on.
194 static void write_wrapper(int fd
, const void *buf
, size_t count
)
196 if (write(fd
, buf
, count
) <= 0)
203 * Looks up a message in a message list by key.
205 * If the message is not found, returns the provided error message.
207 * Terminates when it hits a struct message that's all zeros.
209 * @param mz the message list
210 * @param kz the message key
211 * @param nf the message to return if not found
212 * @return the message
214 const char *lookup_msg(const struct message
*mz
, int kz
, const char *nf
)
216 static struct message nt
= {0};
217 const char *rz
= nf
? nf
: "(no message found)";
218 const struct message
*pnt
;
219 for (pnt
= mz
; memcmp(pnt
, &nt
, sizeof(struct message
)); pnt
++)
220 if (pnt
->key
== kz
) {
221 rz
= pnt
->str
? pnt
->str
: rz
;
227 /* For time string format. */
228 size_t quagga_timestamp(int timestamp_precision
, char *buf
, size_t buflen
)
235 struct timeval clock
;
237 gettimeofday(&clock
, NULL
);
239 /* first, we update the cache if the time has changed */
240 if (cache
.last
!= clock
.tv_sec
) {
242 cache
.last
= clock
.tv_sec
;
243 tm
= localtime(&cache
.last
);
244 cache
.len
= strftime(cache
.buf
, sizeof(cache
.buf
),
245 "%Y/%m/%d %H:%M:%S", tm
);
247 /* note: it's not worth caching the subsecond part, because
248 chances are that back-to-back calls are not sufficiently close
250 for the clock not to have ticked forward */
252 if (buflen
> cache
.len
) {
253 memcpy(buf
, cache
.buf
, cache
.len
);
254 if ((timestamp_precision
> 0)
255 && (buflen
> cache
.len
+ 1 + timestamp_precision
)) {
256 /* should we worry about locale issues? */
257 static const int divisor
[] = {0, 100000, 10000, 1000,
260 char *p
= buf
+ cache
.len
+ 1
261 + (prec
= timestamp_precision
);
264 /* this is unlikely to happen, but protect anyway */
269 clock
.tv_usec
/= divisor
[prec
];
271 *p
-- = '0' + (clock
.tv_usec
% 10);
273 } while (--prec
> 0);
275 return cache
.len
+ 1 + timestamp_precision
;
277 buf
[cache
.len
] = '\0';
285 static inline void timestamp_control_render(struct timestamp_control
*ctl
)
287 if (!ctl
->already_rendered
) {
288 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
290 ctl
->already_rendered
= 1;
294 /* Utility routine for current time printing. */
295 static void time_print(FILE *fp
, struct timestamp_control
*ctl
)
297 timestamp_control_render(ctl
);
298 fprintf(fp
, "%s ", ctl
->buf
);
301 static int time_print_buf(char *buf
, int len
, int max_size
,
302 struct timestamp_control
*ctl
)
304 timestamp_control_render(ctl
);
306 if (ctl
->len
+ 1 >= (unsigned long)max_size
)
309 return snprintf(buf
+ len
, max_size
- len
, "%s ", ctl
->buf
);
312 static void vzlog_file(struct zlog
*zl
, struct timestamp_control
*tsctl
,
313 const char *proto_str
, int record_priority
, int priority
,
314 FILE *fp
, const char *msg
)
316 time_print(fp
, tsctl
);
318 fprintf(fp
, "%s: ", zlog_priority
[priority
]);
320 fprintf(fp
, "%s%s\n", proto_str
, msg
);
324 /* Search a buf for the filter strings, loglock must be held */
325 static int search_buf(const char *buf
)
329 for (int i
= 0; i
< zlog_filter_count
; i
++) {
330 found
= strstr(buf
, zlog_filters
[i
]);
338 /* Filter out a log */
339 static int vzlog_filter(struct zlog
*zl
, struct timestamp_control
*tsctl
,
340 const char *proto_str
, int priority
, const char *msg
)
346 ret
= time_print_buf(buf
, len
, sizeof(buf
), tsctl
);
349 if ((ret
< 0) || ((size_t)len
>= sizeof(buf
)))
352 if (zl
&& zl
->record_priority
)
353 snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: %s",
354 zlog_priority
[priority
], proto_str
, msg
);
356 snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s", proto_str
,
360 return search_buf(buf
);
363 /* va_list version of zlog. */
364 void vzlog(int priority
, const char *format
, va_list args
)
366 pthread_mutex_lock(&loglock
);
368 char proto_str
[32] = "";
369 int original_errno
= errno
;
370 struct timestamp_control tsctl
= {};
371 tsctl
.already_rendered
= 0;
372 struct zlog
*zl
= zlog_default
;
378 tsctl
.precision
= zl
->timestamp_precision
;
380 sprintf(proto_str
, "%s[%d]: ", zl
->protoname
,
383 sprintf(proto_str
, "%s: ", zl
->protoname
);
386 msg
= vasnprintfrr(MTYPE_TMP
, buf
, sizeof(buf
), format
, args
);
388 /* If it doesn't match on a filter, do nothing with the debug log */
389 if ((priority
== LOG_DEBUG
) && zlog_filter_count
390 && vzlog_filter(zl
, &tsctl
, proto_str
, priority
, msg
))
393 /* call external hook */
394 hook_call(zebra_ext_log
, priority
, format
, args
);
396 /* When zlog_default is also NULL, use stderr for logging. */
398 time_print(stderr
, &tsctl
);
399 fprintf(stderr
, "%s: %s\n", "unknown", msg
);
405 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
406 syslog(priority
| zlog_default
->facility
, "%s", msg
);
409 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
410 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
413 /* fixed-config logging to stderr while we're stating up & haven't
414 * daemonized / reached mainloop yet
416 * note the "else" on stdout output -- we don't want to print the same
417 * message to both stderr and stdout. */
418 if (zlog_startup_stderr
&& priority
<= LOG_WARNING
)
419 vzlog_file(zl
, &tsctl
, proto_str
, 1, priority
, stderr
, msg
);
420 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
421 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
424 /* Terminal monitor. */
425 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
426 vty_log((zl
->record_priority
? zlog_priority
[priority
] : NULL
),
427 proto_str
, msg
, &tsctl
);
431 XFREE(MTYPE_TMP
, msg
);
432 errno
= original_errno
;
433 pthread_mutex_unlock(&loglock
);
436 int vzlog_test(int priority
)
438 pthread_mutex_lock(&loglock
);
442 struct zlog
*zl
= zlog_default
;
444 /* When zlog_default is also NULL, use stderr for logging. */
448 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
451 else if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
454 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
456 /* Terminal monitor. */
457 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
460 pthread_mutex_unlock(&loglock
);
468 * NB: only AS-Safe (async-signal) functions can be used here!
471 /* Needs to be enhanced to support Solaris. */
472 static int syslog_connect(void)
478 struct sockaddr_un addr
;
480 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) < 0)
482 addr
.sun_family
= AF_UNIX
;
484 #define SYSLOG_SOCKET_PATH _PATH_LOG
486 #define SYSLOG_SOCKET_PATH "/dev/log"
488 strlcpy(addr
.sun_path
, SYSLOG_SOCKET_PATH
, sizeof(addr
.sun_path
));
489 #undef SYSLOG_SOCKET_PATH
490 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
498 static void syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
500 static int syslog_fd
= -1;
501 char buf
[sizeof("<1234567890>ripngd[1234567890]: ") + msglen
+ 50];
502 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= sizeof(buf
) };
504 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
507 /* forget about the timestamp, too difficult in a signal handler */
508 bprintfrr(&fb
, "<%d>%s", priority
, zlog_default
->ident
);
509 if (zlog_default
->syslog_options
& LOG_PID
)
510 bprintfrr(&fb
, "[%ld]", (long)getpid());
511 bprintfrr(&fb
, ": %s", msg
);
512 write_wrapper(syslog_fd
, fb
.buf
, fb
.pos
- fb
.buf
);
515 static int open_crashlog(void)
517 char crashlog_buf
[PATH_MAX
];
518 const char *crashlog_default
= "/var/tmp/frr.crashlog", *crashlog
;
520 if (!zlog_default
|| !zlog_default
->ident
)
521 crashlog
= crashlog_default
;
523 snprintfrr(crashlog_buf
, sizeof(crashlog_buf
),
524 "/var/tmp/frr.%s.crashlog", zlog_default
->ident
);
525 crashlog
= crashlog_buf
;
527 return open(crashlog
, O_WRONLY
| O_CREAT
| O_EXCL
, LOGFILE_MASK
);
530 /* N.B. implicit priority is most severe */
533 static void crash_write(struct fbuf
*fb
, char *msgstart
)
535 if (fb
->pos
== fb
->buf
)
540 /* If no file logging configured, try to write to fallback log file. */
541 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
542 write(logfile_fd
, fb
->buf
, fb
->pos
- fb
->buf
);
544 write(STDERR_FILENO
, fb
->buf
, fb
->pos
- fb
->buf
);
546 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
547 write(STDOUT_FILENO
, fb
->buf
, fb
->pos
- fb
->buf
);
548 /* Remove trailing '\n' for monitor and syslog */
550 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
551 vty_log_fixed(fb
->buf
, fb
->pos
- fb
->buf
);
552 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
553 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
558 /* Note: the goal here is to use only async-signal-safe functions. */
559 void zlog_signal(int signo
, const char *action
, void *siginfo_v
,
560 void *program_counter
)
563 siginfo_t
*siginfo
= siginfo_v
;
566 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")
569 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= sizeof(buf
) };
573 bprintfrr(&fb
, "%s: ", zlog_default
->protoname
);
577 bprintfrr(&fb
, "Received signal %d at %lld", signo
, (long long)now
);
580 bprintfrr(&fb
, " (si_addr 0x%tx, PC 0x%tx)",
581 (ptrdiff_t)siginfo
->si_addr
,
582 (ptrdiff_t)program_counter
);
584 bprintfrr(&fb
, " (si_addr 0x%tx)",
585 (ptrdiff_t)siginfo
->si_addr
);
586 #endif /* SA_SIGINFO */
587 bprintfrr(&fb
, "; %s\n", action
);
589 crash_write(&fb
, msgstart
);
591 zlog_backtrace_sigsafe(PRI
, program_counter
);
596 tc
= pthread_getspecific(thread_current
);
599 bprintfrr(&fb
, "no thread information available\n");
601 bprintfrr(&fb
, "in thread %s scheduled from %s:%d\n",
602 tc
->funcname
, tc
->schedfrom
, tc
->schedfrom_line
);
604 crash_write(&fb
, NULL
);
607 /* Log a backtrace using only async-signal-safe functions.
608 Needs to be enhanced to support syslog logging. */
609 void zlog_backtrace_sigsafe(int priority
, void *program_counter
)
611 #ifdef HAVE_LIBUNWIND
613 struct fbuf fb
= { .buf
= buf
, .len
= sizeof(buf
) };
616 unw_word_t ip
, off
, sp
;
620 unw_init_local(&cursor
, &uc
);
621 while (unw_step(&cursor
) > 0) {
622 char name
[128] = "?";
624 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
625 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
627 if (!unw_get_proc_name(&cursor
, buf
, sizeof(buf
), &off
))
628 snprintfrr(name
, sizeof(name
), "%s+%#lx",
632 if (unw_is_signal_frame(&cursor
))
633 bprintfrr(&fb
, " ---- signal ----\n");
634 bprintfrr(&fb
, "%-30s %16lx %16lx", name
, (long)ip
, (long)sp
);
635 if (dladdr((void *)ip
, &dlinfo
))
636 bprintfrr(&fb
, " %s (mapped at %p)",
637 dlinfo
.dli_fname
, dlinfo
.dli_fbase
);
638 bprintfrr(&fb
, "\n");
639 crash_write(&fb
, NULL
);
641 #elif defined(HAVE_GLIBC_BACKTRACE) || defined(HAVE_PRINTSTACK)
642 static const char pclabel
[] = "Program counter: ";
646 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= sizeof(buf
) };
649 #ifdef HAVE_GLIBC_BACKTRACE
650 size
= backtrace(array
, array_size(array
));
651 if (size
<= 0 || (size_t)size
> array_size(array
))
656 if (program_counter) { \
657 write_wrapper(FD, pclabel, sizeof(pclabel) - 1); \
658 backtrace_symbols_fd(&program_counter, 1, FD); \
660 write_wrapper(FD, fb.buf, fb.pos - fb.buf); \
661 backtrace_symbols_fd(array, size, FD); \
663 #elif defined(HAVE_PRINTSTACK)
668 if (program_counter) \
669 write_wrapper((FD), pclabel, sizeof(pclabel) - 1); \
670 write_wrapper((FD), fb.buf, fb.pos - fb.buf); \
673 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
675 bprintfrr(&fb
, "Backtrace for %d stack frames:\n", size
);
677 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
682 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
684 /* Remove trailing '\n' for monitor and syslog */
686 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
687 vty_log_fixed(fb
.buf
, fb
.pos
- fb
.buf
);
688 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
689 syslog_sigsafe(priority
| zlog_default
->facility
,
690 fb
.buf
, fb
.pos
- fb
.buf
);
693 #ifdef HAVE_GLIBC_BACKTRACE
694 bt
= backtrace_symbols(array
, size
);
696 /* Just print the function addresses. */
697 for (i
= 0; i
< size
; i
++) {
700 bprintfrr(&fb
, "%s", bt
[i
]);
702 bprintfrr(&fb
, "[bt %d] 0x%tx", i
,
703 (ptrdiff_t)(array
[i
]));
705 <= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
706 vty_log_fixed(fb
.buf
, fb
.pos
- fb
.buf
);
708 <= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
709 syslog_sigsafe(priority
710 | zlog_default
->facility
,
711 fb
.buf
, fb
.pos
- fb
.buf
);
718 #endif /* HAVE_STRACK_TRACE */
721 void zlog_backtrace(int priority
)
723 #ifdef HAVE_LIBUNWIND
727 unw_word_t ip
, off
, sp
;
731 unw_init_local(&cursor
, &uc
);
732 zlog(priority
, "Backtrace:");
733 while (unw_step(&cursor
) > 0) {
734 char name
[128] = "?";
736 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
737 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
739 if (unw_is_signal_frame(&cursor
))
740 zlog(priority
, " ---- signal ----");
742 if (!unw_get_proc_name(&cursor
, buf
, sizeof(buf
), &off
))
743 snprintf(name
, sizeof(name
), "%s+%#lx",
746 if (dladdr((void *)ip
, &dlinfo
))
747 zlog(priority
, "%-30s %16lx %16lx %s (mapped at %p)",
748 name
, (long)ip
, (long)sp
,
749 dlinfo
.dli_fname
, dlinfo
.dli_fbase
);
751 zlog(priority
, "%-30s %16lx %16lx",
752 name
, (long)ip
, (long)sp
);
754 #elif defined(HAVE_GLIBC_BACKTRACE)
759 size
= backtrace(array
, array_size(array
));
760 if (size
<= 0 || (size_t)size
> array_size(array
)) {
763 "Cannot get backtrace, returned invalid # of frames %d "
764 "(valid range is between 1 and %lu)",
765 size
, (unsigned long)(array_size(array
)));
768 zlog(priority
, "Backtrace for %d stack frames:", size
);
769 if (!(strings
= backtrace_symbols(array
, size
))) {
770 flog_err_sys(EC_LIB_SYSTEM_CALL
,
771 "Cannot get backtrace symbols (out of memory?)");
772 for (i
= 0; i
< size
; i
++)
773 zlog(priority
, "[bt %d] %p", i
, array
[i
]);
775 for (i
= 0; i
< size
; i
++)
776 zlog(priority
, "[bt %d] %s", i
, strings
[i
]);
779 #else /* !HAVE_GLIBC_BACKTRACE && !HAVE_LIBUNWIND */
780 zlog(priority
, "No backtrace available on this platform.");
784 void zlog(int priority
, const char *format
, ...)
788 va_start(args
, format
);
789 vzlog(priority
, format
, args
);
793 #define ZLOG_FUNC(FUNCNAME, PRIORITY) \
794 void FUNCNAME(const char *format, ...) \
797 va_start(args, format); \
798 vzlog(PRIORITY, format, args); \
802 ZLOG_FUNC(zlog_err
, LOG_ERR
)
804 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
806 ZLOG_FUNC(zlog_info
, LOG_INFO
)
808 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
810 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
814 void zlog_thread_info(int log_level
)
817 tc
= pthread_getspecific(thread_current
);
821 "Current thread function %s, scheduled from "
823 tc
->funcname
, tc
->schedfrom
, tc
->schedfrom_line
);
825 zlog(log_level
, "Current thread not known/applicable");
828 void _zlog_assert_failed(const char *assertion
, const char *file
,
829 unsigned int line
, const char *function
)
831 /* Force fallback file logging? */
832 if (zlog_default
&& !zlog_default
->fp
833 && ((logfile_fd
= open_crashlog()) >= 0)
834 && ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
835 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
836 zlog(LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
837 assertion
, file
, line
, (function
? function
: "?"));
838 zlog_backtrace(LOG_CRIT
);
839 zlog_thread_info(LOG_CRIT
);
840 log_memstats(stderr
, "log");
844 void memory_oom(size_t size
, const char *name
)
846 flog_err_sys(EC_LIB_SYSTEM_CALL
,
847 "out of memory: failed to allocate %zu bytes for %s"
850 zlog_backtrace(LOG_ERR
);
854 /* Open log stream */
855 void openzlog(const char *progname
, const char *protoname
,
856 unsigned short instance
, int syslog_flags
, int syslog_facility
)
861 zl
= XCALLOC(MTYPE_ZLOG
, sizeof(struct zlog
));
863 zl
->ident
= progname
;
864 zl
->protoname
= protoname
;
865 zl
->instance
= instance
;
866 zl
->facility
= syslog_facility
;
867 zl
->syslog_options
= syslog_flags
;
869 /* Set default logging levels. */
870 for (i
= 0; i
< array_size(zl
->maxlvl
); i
++)
871 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
872 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
873 zl
->default_lvl
= LOG_DEBUG
;
875 openlog(progname
, syslog_flags
, zl
->facility
);
877 pthread_mutex_lock(&loglock
);
879 pthread_mutex_unlock(&loglock
);
881 #ifdef HAVE_GLIBC_BACKTRACE
882 /* work around backtrace() using lazily resolved dynamically linked
883 * symbols, which will otherwise cause funny breakage in the SEGV
885 * (particularly, the dynamic linker can call malloc(), which uses locks
886 * in programs linked with -pthread, thus can deadlock.) */
888 backtrace(bt
, array_size(bt
));
889 free(backtrace_symbols(bt
, 0));
890 backtrace_symbols_fd(bt
, 0, 0);
896 pthread_mutex_lock(&loglock
);
897 struct zlog
*zl
= zlog_default
;
904 XFREE(MTYPE_ZLOG
, zl
->filename
);
906 XFREE(MTYPE_ZLOG
, zl
);
908 pthread_mutex_unlock(&loglock
);
911 /* Called from command.c. */
912 void zlog_set_level(zlog_dest_t dest
, int log_level
)
914 pthread_mutex_lock(&loglock
);
915 zlog_default
->maxlvl
[dest
] = log_level
;
916 pthread_mutex_unlock(&loglock
);
919 int zlog_set_file(const char *filename
, int log_level
)
926 /* There is opend file. */
930 oldumask
= umask(0777 & ~LOGFILE_MASK
);
931 fp
= fopen(filename
, "a");
936 pthread_mutex_lock(&loglock
);
940 zl
->filename
= XSTRDUP(MTYPE_ZLOG
, filename
);
941 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
943 logfile_fd
= fileno(fp
);
944 pthread_mutex_unlock(&loglock
);
950 /* Reset opend file. */
951 int zlog_reset_file(void)
953 pthread_mutex_lock(&loglock
);
955 struct zlog
*zl
= zlog_default
;
961 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
963 XFREE(MTYPE_ZLOG
, zl
->filename
);
966 pthread_mutex_unlock(&loglock
);
971 /* Reopen log file. */
972 int zlog_rotate(void)
974 pthread_mutex_lock(&loglock
);
976 struct zlog
*zl
= zlog_default
;
984 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
985 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
991 oldumask
= umask(0777 & ~LOGFILE_MASK
);
992 zl
->fp
= fopen(zl
->filename
, "a");
995 if (zl
->fp
== NULL
) {
997 pthread_mutex_unlock(&loglock
);
1001 "Log rotate failed: cannot open file %s for append: %s",
1002 zl
->filename
, safe_strerror(save_errno
));
1005 pthread_mutex_lock(&loglock
);
1007 logfile_fd
= fileno(zl
->fp
);
1008 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
1012 pthread_mutex_unlock(&loglock
);
1017 /* Wrapper around strerror to handle case where it returns NULL. */
1018 const char *safe_strerror(int errnum
)
1020 const char *s
= strerror(errnum
);
1021 return (s
!= NULL
) ? s
: "Unknown error";
1024 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
1025 static const struct zebra_desc_table command_types
[] = {
1026 DESC_ENTRY(ZEBRA_INTERFACE_ADD
),
1027 DESC_ENTRY(ZEBRA_INTERFACE_DELETE
),
1028 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD
),
1029 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE
),
1030 DESC_ENTRY(ZEBRA_INTERFACE_UP
),
1031 DESC_ENTRY(ZEBRA_INTERFACE_DOWN
),
1032 DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER
),
1033 DESC_ENTRY(ZEBRA_ROUTE_ADD
),
1034 DESC_ENTRY(ZEBRA_ROUTE_DELETE
),
1035 DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER
),
1036 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD
),
1037 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE
),
1038 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
1039 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
1040 DESC_ENTRY(ZEBRA_ROUTER_ID_ADD
),
1041 DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE
),
1042 DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE
),
1043 DESC_ENTRY(ZEBRA_HELLO
),
1044 DESC_ENTRY(ZEBRA_CAPABILITIES
),
1045 DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER
),
1046 DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER
),
1047 DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE
),
1048 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
),
1049 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
),
1050 DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE
),
1051 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_REGISTER
),
1052 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_UNREGISTER
),
1053 DESC_ENTRY(ZEBRA_IMPORT_CHECK_UPDATE
),
1054 DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER
),
1055 DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER
),
1056 DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE
),
1057 DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY
),
1058 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD
),
1059 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL
),
1060 DESC_ENTRY(ZEBRA_VRF_UNREGISTER
),
1061 DESC_ENTRY(ZEBRA_VRF_ADD
),
1062 DESC_ENTRY(ZEBRA_VRF_DELETE
),
1063 DESC_ENTRY(ZEBRA_VRF_LABEL
),
1064 DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE
),
1065 DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER
),
1066 DESC_ENTRY(ZEBRA_BFD_CLIENT_DEREGISTER
),
1067 DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV
),
1068 DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV
),
1069 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
),
1070 DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS
),
1071 DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD
),
1072 DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE
),
1073 DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS
),
1074 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT
),
1075 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
),
1076 DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK
),
1077 DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK
),
1078 DESC_ENTRY(ZEBRA_FEC_REGISTER
),
1079 DESC_ENTRY(ZEBRA_FEC_UNREGISTER
),
1080 DESC_ENTRY(ZEBRA_FEC_UPDATE
),
1081 DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI
),
1082 DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW
),
1083 DESC_ENTRY(ZEBRA_ADVERTISE_SVI_MACIP
),
1084 DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET
),
1085 DESC_ENTRY(ZEBRA_LOCAL_ES_ADD
),
1086 DESC_ENTRY(ZEBRA_LOCAL_ES_DEL
),
1087 DESC_ENTRY(ZEBRA_VNI_ADD
),
1088 DESC_ENTRY(ZEBRA_VNI_DEL
),
1089 DESC_ENTRY(ZEBRA_L3VNI_ADD
),
1090 DESC_ENTRY(ZEBRA_L3VNI_DEL
),
1091 DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD
),
1092 DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL
),
1093 DESC_ENTRY(ZEBRA_MACIP_ADD
),
1094 DESC_ENTRY(ZEBRA_MACIP_DEL
),
1095 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD
),
1096 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL
),
1097 DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD
),
1098 DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL
),
1099 DESC_ENTRY(ZEBRA_DUPLICATE_ADDR_DETECTION
),
1100 DESC_ENTRY(ZEBRA_PW_ADD
),
1101 DESC_ENTRY(ZEBRA_PW_DELETE
),
1102 DESC_ENTRY(ZEBRA_PW_SET
),
1103 DESC_ENTRY(ZEBRA_PW_UNSET
),
1104 DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE
),
1105 DESC_ENTRY(ZEBRA_RULE_ADD
),
1106 DESC_ENTRY(ZEBRA_RULE_DELETE
),
1107 DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER
),
1108 DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT
),
1109 DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK
),
1110 DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK
),
1111 DESC_ENTRY(ZEBRA_IPSET_CREATE
),
1112 DESC_ENTRY(ZEBRA_IPSET_DESTROY
),
1113 DESC_ENTRY(ZEBRA_IPSET_ENTRY_ADD
),
1114 DESC_ENTRY(ZEBRA_IPSET_ENTRY_DELETE
),
1115 DESC_ENTRY(ZEBRA_IPSET_NOTIFY_OWNER
),
1116 DESC_ENTRY(ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
),
1117 DESC_ENTRY(ZEBRA_IPTABLE_ADD
),
1118 DESC_ENTRY(ZEBRA_IPTABLE_DELETE
),
1119 DESC_ENTRY(ZEBRA_IPTABLE_NOTIFY_OWNER
),
1120 DESC_ENTRY(ZEBRA_VXLAN_FLOOD_CONTROL
),
1121 DESC_ENTRY(ZEBRA_VXLAN_SG_ADD
),
1122 DESC_ENTRY(ZEBRA_VXLAN_SG_DEL
),
1123 DESC_ENTRY(ZEBRA_VXLAN_SG_REPLAY
),
1127 static const struct zebra_desc_table unknown
= {0, "unknown", '?'};
1129 static const struct zebra_desc_table
*zroute_lookup(unsigned int zroute
)
1133 if (zroute
>= array_size(route_types
)) {
1134 flog_err(EC_LIB_DEVELOPMENT
, "unknown zebra route type: %u",
1138 if (zroute
== route_types
[zroute
].type
)
1139 return &route_types
[zroute
];
1140 for (i
= 0; i
< array_size(route_types
); i
++) {
1141 if (zroute
== route_types
[i
].type
) {
1143 "internal error: route type table out of order "
1144 "while searching for %u, please notify developers",
1146 return &route_types
[i
];
1149 flog_err(EC_LIB_DEVELOPMENT
,
1150 "internal error: cannot find route type %u in table!", zroute
);
1154 const char *zebra_route_string(unsigned int zroute
)
1156 return zroute_lookup(zroute
)->string
;
1159 char zebra_route_char(unsigned int zroute
)
1161 return zroute_lookup(zroute
)->chr
;
1164 const char *zserv_command_string(unsigned int command
)
1166 if (command
>= array_size(command_types
)) {
1167 flog_err(EC_LIB_DEVELOPMENT
, "unknown zserv command type: %u",
1169 return unknown
.string
;
1171 return command_types
[command
].string
;
1174 int proto_name2num(const char *s
)
1178 for (i
= 0; i
< array_size(route_types
); ++i
)
1179 if (strcasecmp(s
, route_types
[i
].string
) == 0)
1180 return route_types
[i
].type
;
1184 int proto_redistnum(int afi
, const char *s
)
1189 if (afi
== AFI_IP
) {
1190 if (strmatch(s
, "kernel"))
1191 return ZEBRA_ROUTE_KERNEL
;
1192 else if (strmatch(s
, "connected"))
1193 return ZEBRA_ROUTE_CONNECT
;
1194 else if (strmatch(s
, "static"))
1195 return ZEBRA_ROUTE_STATIC
;
1196 else if (strmatch(s
, "rip"))
1197 return ZEBRA_ROUTE_RIP
;
1198 else if (strmatch(s
, "eigrp"))
1199 return ZEBRA_ROUTE_EIGRP
;
1200 else if (strmatch(s
, "ospf"))
1201 return ZEBRA_ROUTE_OSPF
;
1202 else if (strmatch(s
, "isis"))
1203 return ZEBRA_ROUTE_ISIS
;
1204 else if (strmatch(s
, "bgp"))
1205 return ZEBRA_ROUTE_BGP
;
1206 else if (strmatch(s
, "table"))
1207 return ZEBRA_ROUTE_TABLE
;
1208 else if (strmatch(s
, "vnc"))
1209 return ZEBRA_ROUTE_VNC
;
1210 else if (strmatch(s
, "vnc-direct"))
1211 return ZEBRA_ROUTE_VNC_DIRECT
;
1212 else if (strmatch(s
, "nhrp"))
1213 return ZEBRA_ROUTE_NHRP
;
1214 else if (strmatch(s
, "babel"))
1215 return ZEBRA_ROUTE_BABEL
;
1216 else if (strmatch(s
, "sharp"))
1217 return ZEBRA_ROUTE_SHARP
;
1218 else if (strmatch(s
, "openfabric"))
1219 return ZEBRA_ROUTE_OPENFABRIC
;
1221 if (afi
== AFI_IP6
) {
1222 if (strmatch(s
, "kernel"))
1223 return ZEBRA_ROUTE_KERNEL
;
1224 else if (strmatch(s
, "connected"))
1225 return ZEBRA_ROUTE_CONNECT
;
1226 else if (strmatch(s
, "static"))
1227 return ZEBRA_ROUTE_STATIC
;
1228 else if (strmatch(s
, "ripng"))
1229 return ZEBRA_ROUTE_RIPNG
;
1230 else if (strmatch(s
, "ospf6"))
1231 return ZEBRA_ROUTE_OSPF6
;
1232 else if (strmatch(s
, "isis"))
1233 return ZEBRA_ROUTE_ISIS
;
1234 else if (strmatch(s
, "bgp"))
1235 return ZEBRA_ROUTE_BGP
;
1236 else if (strmatch(s
, "table"))
1237 return ZEBRA_ROUTE_TABLE
;
1238 else if (strmatch(s
, "vnc"))
1239 return ZEBRA_ROUTE_VNC
;
1240 else if (strmatch(s
, "vnc-direct"))
1241 return ZEBRA_ROUTE_VNC_DIRECT
;
1242 else if (strmatch(s
, "nhrp"))
1243 return ZEBRA_ROUTE_NHRP
;
1244 else if (strmatch(s
, "babel"))
1245 return ZEBRA_ROUTE_BABEL
;
1246 else if (strmatch(s
, "sharp"))
1247 return ZEBRA_ROUTE_SHARP
;
1248 else if (strmatch(s
, "openfabric"))
1249 return ZEBRA_ROUTE_OPENFABRIC
;
1254 void zlog_hexdump(const void *mem
, unsigned int len
)
1256 unsigned long i
= 0;
1258 unsigned int columns
= 8;
1260 * 19 bytes for 0xADDRESS:
1261 * 24 bytes for data; 2 chars plus a space per data byte
1263 * 8 bytes for ASCII representation
1264 * 1 byte for a newline
1265 * =====================
1266 * 53 bytes per 8 bytes of data
1267 * 1 byte for null term
1269 size_t bs
= ((len
/ 8) + 1) * 53 + 1;
1272 const unsigned char *memch
= mem
;
1274 memset(buf
, 0, sizeof(buf
));
1276 for (i
= 0; i
< len
+ ((len
% columns
) ? (columns
- len
% columns
) : 0);
1279 if (i
% columns
== 0)
1280 s
+= snprintf(s
, bs
- (s
- buf
),
1281 "0x%016lx: ", (unsigned long)memch
+ i
);
1283 /* print hex data */
1285 s
+= snprintf(s
, bs
- (s
- buf
), "%02x ", memch
[i
]);
1287 /* end of block, just aligning for ASCII dump */
1289 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1291 /* print ASCII dump */
1292 if (i
% columns
== (columns
- 1)) {
1293 for (j
= i
- (columns
- 1); j
<= i
; j
++) {
1294 /* end of block not really printing */
1296 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1297 else if (isprint(memch
[j
]))
1298 s
+= snprintf(s
, bs
- (s
- buf
), "%c",
1300 else /* other char */
1301 s
+= snprintf(s
, bs
- (s
- buf
), ".");
1303 s
+= snprintf(s
, bs
- (s
- buf
), "\n");
1306 zlog_debug("\n%s", buf
);
1309 const char *zlog_sanitize(char *buf
, size_t bufsz
, const void *in
, size_t inlen
)
1311 const char *inbuf
= in
;
1312 char *pos
= buf
, *end
= buf
+ bufsz
;
1313 const char *iend
= inbuf
+ inlen
;
1315 memset(buf
, 0, bufsz
);
1316 for (; inbuf
< iend
; inbuf
++) {
1317 /* don't write partial escape sequence */
1322 snprintf(pos
, end
- pos
, "\\n");
1323 else if (*inbuf
== '\r')
1324 snprintf(pos
, end
- pos
, "\\r");
1325 else if (*inbuf
== '\t')
1326 snprintf(pos
, end
- pos
, "\\t");
1327 else if (*inbuf
< ' ' || *inbuf
== '"' || *inbuf
>= 127)
1328 snprintf(pos
, end
- pos
, "\\x%02hhx", *inbuf
);