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
)) {
391 pthread_mutex_unlock(&loglock
);
395 /* call external hook */
396 hook_call(zebra_ext_log
, priority
, format
, args
);
398 /* When zlog_default is also NULL, use stderr for logging. */
400 time_print(stderr
, &tsctl
);
401 fprintf(stderr
, "%s: %s\n", "unknown", msg
);
407 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
408 syslog(priority
| zlog_default
->facility
, "%s", msg
);
411 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
412 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
415 /* fixed-config logging to stderr while we're stating up & haven't
416 * daemonized / reached mainloop yet
418 * note the "else" on stdout output -- we don't want to print the same
419 * message to both stderr and stdout. */
420 if (zlog_startup_stderr
&& priority
<= LOG_WARNING
)
421 vzlog_file(zl
, &tsctl
, proto_str
, 1, priority
, stderr
, msg
);
422 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
423 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
426 /* Terminal monitor. */
427 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
428 vty_log((zl
->record_priority
? zlog_priority
[priority
] : NULL
),
429 proto_str
, msg
, &tsctl
);
433 XFREE(MTYPE_TMP
, msg
);
434 errno
= original_errno
;
435 pthread_mutex_unlock(&loglock
);
438 int vzlog_test(int priority
)
440 pthread_mutex_lock(&loglock
);
444 struct zlog
*zl
= zlog_default
;
446 /* When zlog_default is also NULL, use stderr for logging. */
450 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
453 else if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
456 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
458 /* Terminal monitor. */
459 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
462 pthread_mutex_unlock(&loglock
);
470 * NB: only AS-Safe (async-signal) functions can be used here!
473 /* Needs to be enhanced to support Solaris. */
474 static int syslog_connect(void)
480 struct sockaddr_un addr
;
482 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) < 0)
484 addr
.sun_family
= AF_UNIX
;
486 #define SYSLOG_SOCKET_PATH _PATH_LOG
488 #define SYSLOG_SOCKET_PATH "/dev/log"
490 strlcpy(addr
.sun_path
, SYSLOG_SOCKET_PATH
, sizeof(addr
.sun_path
));
491 #undef SYSLOG_SOCKET_PATH
492 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
500 static void syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
502 static int syslog_fd
= -1;
503 char buf
[sizeof("<1234567890>ripngd[1234567890]: ") + msglen
+ 50];
504 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= sizeof(buf
) };
506 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
509 /* forget about the timestamp, too difficult in a signal handler */
510 bprintfrr(&fb
, "<%d>%s", priority
, zlog_default
->ident
);
511 if (zlog_default
->syslog_options
& LOG_PID
)
512 bprintfrr(&fb
, "[%ld]", (long)getpid());
513 bprintfrr(&fb
, ": %s", msg
);
514 write_wrapper(syslog_fd
, fb
.buf
, fb
.pos
- fb
.buf
);
517 static int open_crashlog(void)
519 char crashlog_buf
[PATH_MAX
];
520 const char *crashlog_default
= "/var/tmp/frr.crashlog", *crashlog
;
522 if (!zlog_default
|| !zlog_default
->ident
)
523 crashlog
= crashlog_default
;
525 snprintfrr(crashlog_buf
, sizeof(crashlog_buf
),
526 "/var/tmp/frr.%s.crashlog", zlog_default
->ident
);
527 crashlog
= crashlog_buf
;
529 return open(crashlog
, O_WRONLY
| O_CREAT
| O_EXCL
, LOGFILE_MASK
);
532 /* N.B. implicit priority is most severe */
535 static void crash_write(struct fbuf
*fb
, char *msgstart
)
537 if (fb
->pos
== fb
->buf
)
542 /* If no file logging configured, try to write to fallback log file. */
543 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
544 write(logfile_fd
, fb
->buf
, fb
->pos
- fb
->buf
);
546 write(STDERR_FILENO
, fb
->buf
, fb
->pos
- fb
->buf
);
548 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
549 write(STDOUT_FILENO
, fb
->buf
, fb
->pos
- fb
->buf
);
550 /* Remove trailing '\n' for monitor and syslog */
552 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
553 vty_log_fixed(fb
->buf
, fb
->pos
- fb
->buf
);
554 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
555 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
560 /* Note: the goal here is to use only async-signal-safe functions. */
561 void zlog_signal(int signo
, const char *action
, void *siginfo_v
,
562 void *program_counter
)
565 siginfo_t
*siginfo
= siginfo_v
;
568 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")
571 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= sizeof(buf
) };
575 bprintfrr(&fb
, "%s: ", zlog_default
->protoname
);
579 bprintfrr(&fb
, "Received signal %d at %lld", signo
, (long long)now
);
582 bprintfrr(&fb
, " (si_addr 0x%tx, PC 0x%tx)",
583 (ptrdiff_t)siginfo
->si_addr
,
584 (ptrdiff_t)program_counter
);
586 bprintfrr(&fb
, " (si_addr 0x%tx)",
587 (ptrdiff_t)siginfo
->si_addr
);
588 #endif /* SA_SIGINFO */
589 bprintfrr(&fb
, "; %s\n", action
);
591 crash_write(&fb
, msgstart
);
593 zlog_backtrace_sigsafe(PRI
, program_counter
);
598 tc
= pthread_getspecific(thread_current
);
601 bprintfrr(&fb
, "no thread information available\n");
603 bprintfrr(&fb
, "in thread %s scheduled from %s:%d\n",
604 tc
->funcname
, tc
->schedfrom
, tc
->schedfrom_line
);
606 crash_write(&fb
, NULL
);
609 /* Log a backtrace using only async-signal-safe functions.
610 Needs to be enhanced to support syslog logging. */
611 void zlog_backtrace_sigsafe(int priority
, void *program_counter
)
613 #ifdef HAVE_LIBUNWIND
615 struct fbuf fb
= { .buf
= buf
, .len
= sizeof(buf
) };
618 unw_word_t ip
, off
, sp
;
622 unw_init_local(&cursor
, &uc
);
623 while (unw_step(&cursor
) > 0) {
624 char name
[128] = "?";
626 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
627 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
629 if (!unw_get_proc_name(&cursor
, buf
, sizeof(buf
), &off
))
630 snprintfrr(name
, sizeof(name
), "%s+%#lx",
634 if (unw_is_signal_frame(&cursor
))
635 bprintfrr(&fb
, " ---- signal ----\n");
636 bprintfrr(&fb
, "%-30s %16lx %16lx", name
, (long)ip
, (long)sp
);
637 if (dladdr((void *)ip
, &dlinfo
))
638 bprintfrr(&fb
, " %s (mapped at %p)",
639 dlinfo
.dli_fname
, dlinfo
.dli_fbase
);
640 bprintfrr(&fb
, "\n");
641 crash_write(&fb
, NULL
);
643 #elif defined(HAVE_GLIBC_BACKTRACE) || defined(HAVE_PRINTSTACK)
644 static const char pclabel
[] = "Program counter: ";
648 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= sizeof(buf
) };
651 #ifdef HAVE_GLIBC_BACKTRACE
652 size
= backtrace(array
, array_size(array
));
653 if (size
<= 0 || (size_t)size
> array_size(array
))
658 if (program_counter) { \
659 write_wrapper(FD, pclabel, sizeof(pclabel) - 1); \
660 backtrace_symbols_fd(&program_counter, 1, FD); \
662 write_wrapper(FD, fb.buf, fb.pos - fb.buf); \
663 backtrace_symbols_fd(array, size, FD); \
665 #elif defined(HAVE_PRINTSTACK)
670 if (program_counter) \
671 write_wrapper((FD), pclabel, sizeof(pclabel) - 1); \
672 write_wrapper((FD), fb.buf, fb.pos - fb.buf); \
675 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
677 bprintfrr(&fb
, "Backtrace for %d stack frames:\n", size
);
679 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
684 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
686 /* Remove trailing '\n' for monitor and syslog */
688 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
689 vty_log_fixed(fb
.buf
, fb
.pos
- fb
.buf
);
690 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
691 syslog_sigsafe(priority
| zlog_default
->facility
,
692 fb
.buf
, fb
.pos
- fb
.buf
);
695 #ifdef HAVE_GLIBC_BACKTRACE
696 bt
= backtrace_symbols(array
, size
);
698 /* Just print the function addresses. */
699 for (i
= 0; i
< size
; i
++) {
702 bprintfrr(&fb
, "%s", bt
[i
]);
704 bprintfrr(&fb
, "[bt %d] 0x%tx", i
,
705 (ptrdiff_t)(array
[i
]));
707 <= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
708 vty_log_fixed(fb
.buf
, fb
.pos
- fb
.buf
);
710 <= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
711 syslog_sigsafe(priority
712 | zlog_default
->facility
,
713 fb
.buf
, fb
.pos
- fb
.buf
);
720 #endif /* HAVE_STRACK_TRACE */
723 void zlog_backtrace(int priority
)
725 #ifdef HAVE_LIBUNWIND
729 unw_word_t ip
, off
, sp
;
733 unw_init_local(&cursor
, &uc
);
734 zlog(priority
, "Backtrace:");
735 while (unw_step(&cursor
) > 0) {
736 char name
[128] = "?";
738 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
739 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
741 if (unw_is_signal_frame(&cursor
))
742 zlog(priority
, " ---- signal ----");
744 if (!unw_get_proc_name(&cursor
, buf
, sizeof(buf
), &off
))
745 snprintf(name
, sizeof(name
), "%s+%#lx",
748 if (dladdr((void *)ip
, &dlinfo
))
749 zlog(priority
, "%-30s %16lx %16lx %s (mapped at %p)",
750 name
, (long)ip
, (long)sp
,
751 dlinfo
.dli_fname
, dlinfo
.dli_fbase
);
753 zlog(priority
, "%-30s %16lx %16lx",
754 name
, (long)ip
, (long)sp
);
756 #elif defined(HAVE_GLIBC_BACKTRACE)
761 size
= backtrace(array
, array_size(array
));
762 if (size
<= 0 || (size_t)size
> array_size(array
)) {
765 "Cannot get backtrace, returned invalid # of frames %d "
766 "(valid range is between 1 and %lu)",
767 size
, (unsigned long)(array_size(array
)));
770 zlog(priority
, "Backtrace for %d stack frames:", size
);
771 if (!(strings
= backtrace_symbols(array
, size
))) {
772 flog_err_sys(EC_LIB_SYSTEM_CALL
,
773 "Cannot get backtrace symbols (out of memory?)");
774 for (i
= 0; i
< size
; i
++)
775 zlog(priority
, "[bt %d] %p", i
, array
[i
]);
777 for (i
= 0; i
< size
; i
++)
778 zlog(priority
, "[bt %d] %s", i
, strings
[i
]);
781 #else /* !HAVE_GLIBC_BACKTRACE && !HAVE_LIBUNWIND */
782 zlog(priority
, "No backtrace available on this platform.");
786 void zlog(int priority
, const char *format
, ...)
790 va_start(args
, format
);
791 vzlog(priority
, format
, args
);
795 #define ZLOG_FUNC(FUNCNAME, PRIORITY) \
796 void FUNCNAME(const char *format, ...) \
799 va_start(args, format); \
800 vzlog(PRIORITY, format, args); \
804 ZLOG_FUNC(zlog_err
, LOG_ERR
)
806 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
808 ZLOG_FUNC(zlog_info
, LOG_INFO
)
810 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
812 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
816 void zlog_thread_info(int log_level
)
819 tc
= pthread_getspecific(thread_current
);
823 "Current thread function %s, scheduled from "
825 tc
->funcname
, tc
->schedfrom
, tc
->schedfrom_line
);
827 zlog(log_level
, "Current thread not known/applicable");
830 void _zlog_assert_failed(const char *assertion
, const char *file
,
831 unsigned int line
, const char *function
)
833 /* Force fallback file logging? */
834 if (zlog_default
&& !zlog_default
->fp
835 && ((logfile_fd
= open_crashlog()) >= 0)
836 && ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
837 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
838 zlog(LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
839 assertion
, file
, line
, (function
? function
: "?"));
840 zlog_backtrace(LOG_CRIT
);
841 zlog_thread_info(LOG_CRIT
);
842 log_memstats(stderr
, "log");
846 void memory_oom(size_t size
, const char *name
)
848 flog_err_sys(EC_LIB_SYSTEM_CALL
,
849 "out of memory: failed to allocate %zu bytes for %s"
852 zlog_backtrace(LOG_ERR
);
856 /* Open log stream */
857 void openzlog(const char *progname
, const char *protoname
,
858 unsigned short instance
, int syslog_flags
, int syslog_facility
)
863 zl
= XCALLOC(MTYPE_ZLOG
, sizeof(struct zlog
));
865 zl
->ident
= progname
;
866 zl
->protoname
= protoname
;
867 zl
->instance
= instance
;
868 zl
->facility
= syslog_facility
;
869 zl
->syslog_options
= syslog_flags
;
871 /* Set default logging levels. */
872 for (i
= 0; i
< array_size(zl
->maxlvl
); i
++)
873 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
874 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
875 zl
->default_lvl
= LOG_DEBUG
;
877 openlog(progname
, syslog_flags
, zl
->facility
);
879 pthread_mutex_lock(&loglock
);
881 pthread_mutex_unlock(&loglock
);
883 #ifdef HAVE_GLIBC_BACKTRACE
884 /* work around backtrace() using lazily resolved dynamically linked
885 * symbols, which will otherwise cause funny breakage in the SEGV
887 * (particularly, the dynamic linker can call malloc(), which uses locks
888 * in programs linked with -pthread, thus can deadlock.) */
890 backtrace(bt
, array_size(bt
));
891 free(backtrace_symbols(bt
, 0));
892 backtrace_symbols_fd(bt
, 0, 0);
898 pthread_mutex_lock(&loglock
);
899 struct zlog
*zl
= zlog_default
;
906 XFREE(MTYPE_ZLOG
, zl
->filename
);
908 XFREE(MTYPE_ZLOG
, zl
);
910 pthread_mutex_unlock(&loglock
);
913 /* Called from command.c. */
914 void zlog_set_level(zlog_dest_t dest
, int log_level
)
916 pthread_mutex_lock(&loglock
);
917 zlog_default
->maxlvl
[dest
] = log_level
;
918 pthread_mutex_unlock(&loglock
);
921 int zlog_set_file(const char *filename
, int log_level
)
928 /* There is opend file. */
932 oldumask
= umask(0777 & ~LOGFILE_MASK
);
933 fp
= fopen(filename
, "a");
938 pthread_mutex_lock(&loglock
);
942 zl
->filename
= XSTRDUP(MTYPE_ZLOG
, filename
);
943 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
945 logfile_fd
= fileno(fp
);
946 pthread_mutex_unlock(&loglock
);
952 /* Reset opend file. */
953 int zlog_reset_file(void)
955 pthread_mutex_lock(&loglock
);
957 struct zlog
*zl
= zlog_default
;
963 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
965 XFREE(MTYPE_ZLOG
, zl
->filename
);
968 pthread_mutex_unlock(&loglock
);
973 /* Reopen log file. */
974 int zlog_rotate(void)
976 pthread_mutex_lock(&loglock
);
978 struct zlog
*zl
= zlog_default
;
986 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
987 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
993 oldumask
= umask(0777 & ~LOGFILE_MASK
);
994 zl
->fp
= fopen(zl
->filename
, "a");
997 if (zl
->fp
== NULL
) {
999 pthread_mutex_unlock(&loglock
);
1003 "Log rotate failed: cannot open file %s for append: %s",
1004 zl
->filename
, safe_strerror(save_errno
));
1007 pthread_mutex_lock(&loglock
);
1009 logfile_fd
= fileno(zl
->fp
);
1010 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
1014 pthread_mutex_unlock(&loglock
);
1019 /* Wrapper around strerror to handle case where it returns NULL. */
1020 const char *safe_strerror(int errnum
)
1022 const char *s
= strerror(errnum
);
1023 return (s
!= NULL
) ? s
: "Unknown error";
1026 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
1027 static const struct zebra_desc_table command_types
[] = {
1028 DESC_ENTRY(ZEBRA_INTERFACE_ADD
),
1029 DESC_ENTRY(ZEBRA_INTERFACE_DELETE
),
1030 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD
),
1031 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE
),
1032 DESC_ENTRY(ZEBRA_INTERFACE_UP
),
1033 DESC_ENTRY(ZEBRA_INTERFACE_DOWN
),
1034 DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER
),
1035 DESC_ENTRY(ZEBRA_ROUTE_ADD
),
1036 DESC_ENTRY(ZEBRA_ROUTE_DELETE
),
1037 DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER
),
1038 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD
),
1039 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE
),
1040 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
1041 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
1042 DESC_ENTRY(ZEBRA_ROUTER_ID_ADD
),
1043 DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE
),
1044 DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE
),
1045 DESC_ENTRY(ZEBRA_HELLO
),
1046 DESC_ENTRY(ZEBRA_CAPABILITIES
),
1047 DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER
),
1048 DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER
),
1049 DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE
),
1050 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
),
1051 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
),
1052 DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE
),
1053 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_REGISTER
),
1054 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_UNREGISTER
),
1055 DESC_ENTRY(ZEBRA_IMPORT_CHECK_UPDATE
),
1056 DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER
),
1057 DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER
),
1058 DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE
),
1059 DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY
),
1060 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD
),
1061 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL
),
1062 DESC_ENTRY(ZEBRA_VRF_UNREGISTER
),
1063 DESC_ENTRY(ZEBRA_VRF_ADD
),
1064 DESC_ENTRY(ZEBRA_VRF_DELETE
),
1065 DESC_ENTRY(ZEBRA_VRF_LABEL
),
1066 DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE
),
1067 DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER
),
1068 DESC_ENTRY(ZEBRA_BFD_CLIENT_DEREGISTER
),
1069 DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV
),
1070 DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV
),
1071 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
),
1072 DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS
),
1073 DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD
),
1074 DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE
),
1075 DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS
),
1076 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT
),
1077 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
),
1078 DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK
),
1079 DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK
),
1080 DESC_ENTRY(ZEBRA_FEC_REGISTER
),
1081 DESC_ENTRY(ZEBRA_FEC_UNREGISTER
),
1082 DESC_ENTRY(ZEBRA_FEC_UPDATE
),
1083 DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI
),
1084 DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW
),
1085 DESC_ENTRY(ZEBRA_ADVERTISE_SVI_MACIP
),
1086 DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET
),
1087 DESC_ENTRY(ZEBRA_LOCAL_ES_ADD
),
1088 DESC_ENTRY(ZEBRA_LOCAL_ES_DEL
),
1089 DESC_ENTRY(ZEBRA_VNI_ADD
),
1090 DESC_ENTRY(ZEBRA_VNI_DEL
),
1091 DESC_ENTRY(ZEBRA_L3VNI_ADD
),
1092 DESC_ENTRY(ZEBRA_L3VNI_DEL
),
1093 DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD
),
1094 DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL
),
1095 DESC_ENTRY(ZEBRA_MACIP_ADD
),
1096 DESC_ENTRY(ZEBRA_MACIP_DEL
),
1097 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD
),
1098 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL
),
1099 DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD
),
1100 DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL
),
1101 DESC_ENTRY(ZEBRA_DUPLICATE_ADDR_DETECTION
),
1102 DESC_ENTRY(ZEBRA_PW_ADD
),
1103 DESC_ENTRY(ZEBRA_PW_DELETE
),
1104 DESC_ENTRY(ZEBRA_PW_SET
),
1105 DESC_ENTRY(ZEBRA_PW_UNSET
),
1106 DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE
),
1107 DESC_ENTRY(ZEBRA_RULE_ADD
),
1108 DESC_ENTRY(ZEBRA_RULE_DELETE
),
1109 DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER
),
1110 DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT
),
1111 DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK
),
1112 DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK
),
1113 DESC_ENTRY(ZEBRA_IPSET_CREATE
),
1114 DESC_ENTRY(ZEBRA_IPSET_DESTROY
),
1115 DESC_ENTRY(ZEBRA_IPSET_ENTRY_ADD
),
1116 DESC_ENTRY(ZEBRA_IPSET_ENTRY_DELETE
),
1117 DESC_ENTRY(ZEBRA_IPSET_NOTIFY_OWNER
),
1118 DESC_ENTRY(ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
),
1119 DESC_ENTRY(ZEBRA_IPTABLE_ADD
),
1120 DESC_ENTRY(ZEBRA_IPTABLE_DELETE
),
1121 DESC_ENTRY(ZEBRA_IPTABLE_NOTIFY_OWNER
),
1122 DESC_ENTRY(ZEBRA_VXLAN_FLOOD_CONTROL
),
1123 DESC_ENTRY(ZEBRA_VXLAN_SG_ADD
),
1124 DESC_ENTRY(ZEBRA_VXLAN_SG_DEL
),
1125 DESC_ENTRY(ZEBRA_VXLAN_SG_REPLAY
),
1129 static const struct zebra_desc_table unknown
= {0, "unknown", '?'};
1131 static const struct zebra_desc_table
*zroute_lookup(unsigned int zroute
)
1135 if (zroute
>= array_size(route_types
)) {
1136 flog_err(EC_LIB_DEVELOPMENT
, "unknown zebra route type: %u",
1140 if (zroute
== route_types
[zroute
].type
)
1141 return &route_types
[zroute
];
1142 for (i
= 0; i
< array_size(route_types
); i
++) {
1143 if (zroute
== route_types
[i
].type
) {
1145 "internal error: route type table out of order "
1146 "while searching for %u, please notify developers",
1148 return &route_types
[i
];
1151 flog_err(EC_LIB_DEVELOPMENT
,
1152 "internal error: cannot find route type %u in table!", zroute
);
1156 const char *zebra_route_string(unsigned int zroute
)
1158 return zroute_lookup(zroute
)->string
;
1161 char zebra_route_char(unsigned int zroute
)
1163 return zroute_lookup(zroute
)->chr
;
1166 const char *zserv_command_string(unsigned int command
)
1168 if (command
>= array_size(command_types
)) {
1169 flog_err(EC_LIB_DEVELOPMENT
, "unknown zserv command type: %u",
1171 return unknown
.string
;
1173 return command_types
[command
].string
;
1176 int proto_name2num(const char *s
)
1180 for (i
= 0; i
< array_size(route_types
); ++i
)
1181 if (strcasecmp(s
, route_types
[i
].string
) == 0)
1182 return route_types
[i
].type
;
1186 int proto_redistnum(int afi
, const char *s
)
1191 if (afi
== AFI_IP
) {
1192 if (strmatch(s
, "kernel"))
1193 return ZEBRA_ROUTE_KERNEL
;
1194 else if (strmatch(s
, "connected"))
1195 return ZEBRA_ROUTE_CONNECT
;
1196 else if (strmatch(s
, "static"))
1197 return ZEBRA_ROUTE_STATIC
;
1198 else if (strmatch(s
, "rip"))
1199 return ZEBRA_ROUTE_RIP
;
1200 else if (strmatch(s
, "eigrp"))
1201 return ZEBRA_ROUTE_EIGRP
;
1202 else if (strmatch(s
, "ospf"))
1203 return ZEBRA_ROUTE_OSPF
;
1204 else if (strmatch(s
, "isis"))
1205 return ZEBRA_ROUTE_ISIS
;
1206 else if (strmatch(s
, "bgp"))
1207 return ZEBRA_ROUTE_BGP
;
1208 else if (strmatch(s
, "table"))
1209 return ZEBRA_ROUTE_TABLE
;
1210 else if (strmatch(s
, "vnc"))
1211 return ZEBRA_ROUTE_VNC
;
1212 else if (strmatch(s
, "vnc-direct"))
1213 return ZEBRA_ROUTE_VNC_DIRECT
;
1214 else if (strmatch(s
, "nhrp"))
1215 return ZEBRA_ROUTE_NHRP
;
1216 else if (strmatch(s
, "babel"))
1217 return ZEBRA_ROUTE_BABEL
;
1218 else if (strmatch(s
, "sharp"))
1219 return ZEBRA_ROUTE_SHARP
;
1220 else if (strmatch(s
, "openfabric"))
1221 return ZEBRA_ROUTE_OPENFABRIC
;
1223 if (afi
== AFI_IP6
) {
1224 if (strmatch(s
, "kernel"))
1225 return ZEBRA_ROUTE_KERNEL
;
1226 else if (strmatch(s
, "connected"))
1227 return ZEBRA_ROUTE_CONNECT
;
1228 else if (strmatch(s
, "static"))
1229 return ZEBRA_ROUTE_STATIC
;
1230 else if (strmatch(s
, "ripng"))
1231 return ZEBRA_ROUTE_RIPNG
;
1232 else if (strmatch(s
, "ospf6"))
1233 return ZEBRA_ROUTE_OSPF6
;
1234 else if (strmatch(s
, "isis"))
1235 return ZEBRA_ROUTE_ISIS
;
1236 else if (strmatch(s
, "bgp"))
1237 return ZEBRA_ROUTE_BGP
;
1238 else if (strmatch(s
, "table"))
1239 return ZEBRA_ROUTE_TABLE
;
1240 else if (strmatch(s
, "vnc"))
1241 return ZEBRA_ROUTE_VNC
;
1242 else if (strmatch(s
, "vnc-direct"))
1243 return ZEBRA_ROUTE_VNC_DIRECT
;
1244 else if (strmatch(s
, "nhrp"))
1245 return ZEBRA_ROUTE_NHRP
;
1246 else if (strmatch(s
, "babel"))
1247 return ZEBRA_ROUTE_BABEL
;
1248 else if (strmatch(s
, "sharp"))
1249 return ZEBRA_ROUTE_SHARP
;
1250 else if (strmatch(s
, "openfabric"))
1251 return ZEBRA_ROUTE_OPENFABRIC
;
1256 void zlog_hexdump(const void *mem
, unsigned int len
)
1258 unsigned long i
= 0;
1260 unsigned int columns
= 8;
1262 * 19 bytes for 0xADDRESS:
1263 * 24 bytes for data; 2 chars plus a space per data byte
1265 * 8 bytes for ASCII representation
1266 * 1 byte for a newline
1267 * =====================
1268 * 53 bytes per 8 bytes of data
1269 * 1 byte for null term
1271 size_t bs
= ((len
/ 8) + 1) * 53 + 1;
1275 memset(buf
, 0, sizeof(buf
));
1277 for (i
= 0; i
< len
+ ((len
% columns
) ? (columns
- len
% columns
) : 0);
1280 if (i
% columns
== 0)
1281 s
+= snprintf(s
, bs
- (s
- buf
),
1282 "0x%016lx: ", (unsigned long)mem
+ i
);
1284 /* print hex data */
1286 s
+= snprintf(s
, bs
- (s
- buf
), "%02x ",
1287 0xFF & ((const char *)mem
)[i
]);
1289 /* end of block, just aligning for ASCII dump */
1291 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1293 /* print ASCII dump */
1294 if (i
% columns
== (columns
- 1)) {
1295 for (j
= i
- (columns
- 1); j
<= i
; j
++) {
1296 /* end of block not really printing */
1298 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1299 else if (isprint((int)((const char *)mem
)[j
]))
1301 s
, bs
- (s
- buf
), "%c",
1302 0xFF & ((const char *)mem
)[j
]);
1303 else /* other char */
1304 s
+= snprintf(s
, bs
- (s
- buf
), ".");
1306 s
+= snprintf(s
, bs
- (s
- buf
), "\n");
1309 zlog_debug("\n%s", buf
);
1312 const char *zlog_sanitize(char *buf
, size_t bufsz
, const void *in
, size_t inlen
)
1314 const char *inbuf
= in
;
1315 char *pos
= buf
, *end
= buf
+ bufsz
;
1316 const char *iend
= inbuf
+ inlen
;
1318 memset(buf
, 0, bufsz
);
1319 for (; inbuf
< iend
; inbuf
++) {
1320 /* don't write partial escape sequence */
1325 snprintf(pos
, end
- pos
, "\\n");
1326 else if (*inbuf
== '\r')
1327 snprintf(pos
, end
- pos
, "\\r");
1328 else if (*inbuf
== '\t')
1329 snprintf(pos
, end
- pos
, "\\t");
1330 else if (*inbuf
< ' ' || *inbuf
== '"' || *inbuf
>= 127)
1331 snprintf(pos
, end
- pos
, "\\x%02hhx", *inbuf
);