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"
34 #include "frr_pthread.h"
39 /* for printstack on solaris */
40 #ifdef HAVE_UCONTEXT_H
45 #define UNW_LOCAL_ONLY
46 #include <libunwind.h>
50 DEFINE_MTYPE_STATIC(LIB
, ZLOG
, "Logging")
52 /* hook for external logging */
53 DEFINE_HOOK(zebra_ext_log
, (int priority
, const char *format
, va_list args
),
54 (priority
, format
, args
));
56 static int logfile_fd
= -1; /* Used in signal handler. */
58 struct zlog
*zlog_default
= NULL
;
59 bool zlog_startup_stderr
= true;
61 /* lock protecting zlog_default for mt-safe zlog */
62 static pthread_mutex_t loglock
= PTHREAD_MUTEX_INITIALIZER
;
64 const char *zlog_priority
[] = {
65 "emergencies", "alerts", "critical", "errors", "warnings",
66 "notifications", "informational", "debugging", NULL
,
69 static char zlog_filters
[ZLOG_FILTERS_MAX
][ZLOG_FILTER_LENGTH_MAX
+ 1];
70 static uint8_t zlog_filter_count
;
73 * look for a match on the filter in the current filters, loglock must be held
75 static int zlog_filter_lookup(const char *lookup
)
77 for (int i
= 0; i
< zlog_filter_count
; i
++) {
78 if (strncmp(lookup
, zlog_filters
[i
], sizeof(zlog_filters
[0]))
85 void zlog_filter_clear(void)
87 frr_with_mutex(&loglock
) {
88 zlog_filter_count
= 0;
92 int zlog_filter_add(const char *filter
)
94 frr_with_mutex(&loglock
) {
95 if (zlog_filter_count
>= ZLOG_FILTERS_MAX
)
98 if (zlog_filter_lookup(filter
) != -1)
99 /* Filter already present */
102 strlcpy(zlog_filters
[zlog_filter_count
], filter
,
103 sizeof(zlog_filters
[0]));
105 if (zlog_filters
[zlog_filter_count
][0] == '\0')
106 /* Filter was either empty or didn't get copied
116 int zlog_filter_del(const char *filter
)
118 frr_with_mutex(&loglock
) {
119 int found_idx
= zlog_filter_lookup(filter
);
120 int last_idx
= zlog_filter_count
- 1;
123 /* Didn't find the filter to delete */
126 /* Adjust the filter array */
127 memmove(zlog_filters
[found_idx
], zlog_filters
[found_idx
+ 1],
128 (last_idx
- found_idx
) * sizeof(zlog_filters
[0]));
135 /* Dump all filters to buffer, delimited by new line */
136 int zlog_filter_dump(char *buf
, size_t max_size
)
140 frr_with_mutex(&loglock
) {
141 for (int i
= 0; i
< zlog_filter_count
; i
++) {
143 ret
= snprintf(buf
+ len
, max_size
- len
, " %s\n",
146 if ((ret
< 0) || ((size_t)len
>= max_size
))
157 * glibc has declared that the return value from write *must* not be
159 * gcc see's this problem and issues a warning for the line.
161 * Why is this a big deal you say? Because both of them are right
162 * and if you have -Werror enabled then all calls to write
163 * generate a build error and the build stops.
165 * clang has helpfully allowed this construct:
167 * to tell the compiler yeah I know it has a return value
168 * I don't care about it at this time.
169 * gcc doesn't have this ability.
171 * This code was written such that it didn't care about the
172 * return value from write. At this time do I want
173 * to go through and fix and test this code for correctness.
174 * So just wrapper the bad behavior and move on.
176 static void write_wrapper(int fd
, const void *buf
, size_t count
)
178 if (write(fd
, buf
, count
) <= 0)
185 * Looks up a message in a message list by key.
187 * If the message is not found, returns the provided error message.
189 * Terminates when it hits a struct message that's all zeros.
191 * @param mz the message list
192 * @param kz the message key
193 * @param nf the message to return if not found
194 * @return the message
196 const char *lookup_msg(const struct message
*mz
, int kz
, const char *nf
)
198 static struct message nt
= {0};
199 const char *rz
= nf
? nf
: "(no message found)";
200 const struct message
*pnt
;
201 for (pnt
= mz
; memcmp(pnt
, &nt
, sizeof(struct message
)); pnt
++)
202 if (pnt
->key
== kz
) {
203 rz
= pnt
->str
? pnt
->str
: rz
;
209 /* For time string format. */
210 size_t quagga_timestamp(int timestamp_precision
, char *buf
, size_t buflen
)
217 struct timeval clock
;
219 gettimeofday(&clock
, NULL
);
221 /* first, we update the cache if the time has changed */
222 if (cache
.last
!= clock
.tv_sec
) {
224 cache
.last
= clock
.tv_sec
;
225 tm
= localtime(&cache
.last
);
226 cache
.len
= strftime(cache
.buf
, sizeof(cache
.buf
),
227 "%Y/%m/%d %H:%M:%S", tm
);
229 /* note: it's not worth caching the subsecond part, because
230 chances are that back-to-back calls are not sufficiently close
232 for the clock not to have ticked forward */
234 if (buflen
> cache
.len
) {
235 memcpy(buf
, cache
.buf
, cache
.len
);
236 if ((timestamp_precision
> 0)
237 && (buflen
> cache
.len
+ 1 + timestamp_precision
)) {
238 /* should we worry about locale issues? */
239 static const int divisor
[] = {0, 100000, 10000, 1000,
242 char *p
= buf
+ cache
.len
+ 1
243 + (prec
= timestamp_precision
);
246 /* this is unlikely to happen, but protect anyway */
251 clock
.tv_usec
/= divisor
[prec
];
253 *p
-- = '0' + (clock
.tv_usec
% 10);
255 } while (--prec
> 0);
257 return cache
.len
+ 1 + timestamp_precision
;
259 buf
[cache
.len
] = '\0';
267 static inline void timestamp_control_render(struct timestamp_control
*ctl
)
269 if (!ctl
->already_rendered
) {
270 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
272 ctl
->already_rendered
= 1;
276 /* Utility routine for current time printing. */
277 static void time_print(FILE *fp
, struct timestamp_control
*ctl
)
279 timestamp_control_render(ctl
);
280 fprintf(fp
, "%s ", ctl
->buf
);
283 static int time_print_buf(char *buf
, int len
, int max_size
,
284 struct timestamp_control
*ctl
)
286 timestamp_control_render(ctl
);
288 if (ctl
->len
+ 1 >= (unsigned long)max_size
)
291 return snprintf(buf
+ len
, max_size
- len
, "%s ", ctl
->buf
);
294 static void vzlog_file(struct zlog
*zl
, struct timestamp_control
*tsctl
,
295 const char *proto_str
, int record_priority
, int priority
,
296 FILE *fp
, const char *msg
)
298 time_print(fp
, tsctl
);
300 fprintf(fp
, "%s: ", zlog_priority
[priority
]);
302 fprintf(fp
, "%s%s\n", proto_str
, msg
);
306 /* Search a buf for the filter strings, loglock must be held */
307 static int search_buf(const char *buf
)
311 for (int i
= 0; i
< zlog_filter_count
; i
++) {
312 found
= strstr(buf
, zlog_filters
[i
]);
320 /* Filter out a log */
321 static int vzlog_filter(struct zlog
*zl
, struct timestamp_control
*tsctl
,
322 const char *proto_str
, int priority
, const char *msg
)
328 ret
= time_print_buf(buf
, len
, sizeof(buf
), tsctl
);
331 if ((ret
< 0) || ((size_t)len
>= sizeof(buf
)))
334 if (zl
&& zl
->record_priority
)
335 snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s: %s",
336 zlog_priority
[priority
], proto_str
, msg
);
338 snprintf(buf
+ len
, sizeof(buf
) - len
, "%s: %s", proto_str
,
342 return search_buf(buf
);
345 /* va_list version of zlog. */
346 void vzlog(int priority
, const char *format
, va_list args
)
348 frr_mutex_lock_autounlock(&loglock
);
350 char proto_str
[32] = "";
351 int original_errno
= errno
;
352 struct timestamp_control tsctl
= {};
353 tsctl
.already_rendered
= 0;
354 struct zlog
*zl
= zlog_default
;
360 tsctl
.precision
= zl
->timestamp_precision
;
362 sprintf(proto_str
, "%s[%d]: ", zl
->protoname
,
365 sprintf(proto_str
, "%s: ", zl
->protoname
);
368 msg
= vasnprintfrr(MTYPE_TMP
, buf
, sizeof(buf
), format
, args
);
370 /* If it doesn't match on a filter, do nothing with the debug log */
371 if ((priority
== LOG_DEBUG
) && zlog_filter_count
372 && vzlog_filter(zl
, &tsctl
, proto_str
, priority
, msg
))
375 /* call external hook */
376 hook_call(zebra_ext_log
, priority
, format
, args
);
378 /* When zlog_default is also NULL, use stderr for logging. */
380 time_print(stderr
, &tsctl
);
381 fprintf(stderr
, "%s: %s\n", "unknown", msg
);
387 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
388 syslog(priority
| zlog_default
->facility
, "%s", msg
);
391 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
392 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
395 /* fixed-config logging to stderr while we're stating up & haven't
396 * daemonized / reached mainloop yet
398 * note the "else" on stdout output -- we don't want to print the same
399 * message to both stderr and stdout. */
400 if (zlog_startup_stderr
&& priority
<= LOG_WARNING
)
401 vzlog_file(zl
, &tsctl
, proto_str
, 1, priority
, stderr
, msg
);
402 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
403 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
406 /* Terminal monitor. */
407 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
408 vty_log((zl
->record_priority
? zlog_priority
[priority
] : NULL
),
409 proto_str
, msg
, &tsctl
);
413 XFREE(MTYPE_TMP
, msg
);
414 errno
= original_errno
;
417 int vzlog_test(int priority
)
419 frr_mutex_lock_autounlock(&loglock
);
421 struct zlog
*zl
= zlog_default
;
423 /* When zlog_default is also NULL, use stderr for logging. */
427 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
430 else if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
433 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
435 /* Terminal monitor. */
436 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
445 * NB: only AS-Safe (async-signal) functions can be used here!
448 /* Needs to be enhanced to support Solaris. */
449 static int syslog_connect(void)
455 struct sockaddr_un addr
;
457 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) < 0)
459 addr
.sun_family
= AF_UNIX
;
461 #define SYSLOG_SOCKET_PATH _PATH_LOG
463 #define SYSLOG_SOCKET_PATH "/dev/log"
465 strlcpy(addr
.sun_path
, SYSLOG_SOCKET_PATH
, sizeof(addr
.sun_path
));
466 #undef SYSLOG_SOCKET_PATH
467 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
475 static void syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
477 static int syslog_fd
= -1;
478 char buf
[sizeof("<1234567890>ripngd[1234567890]: ") + msglen
+ 50];
479 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= sizeof(buf
) };
481 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
484 /* forget about the timestamp, too difficult in a signal handler */
485 bprintfrr(&fb
, "<%d>%s", priority
, zlog_default
->ident
);
486 if (zlog_default
->syslog_options
& LOG_PID
)
487 bprintfrr(&fb
, "[%ld]", (long)getpid());
488 bprintfrr(&fb
, ": %s", msg
);
489 write_wrapper(syslog_fd
, fb
.buf
, fb
.pos
- fb
.buf
);
492 static int open_crashlog(void)
494 char crashlog_buf
[PATH_MAX
];
495 const char *crashlog_default
= "/var/tmp/frr.crashlog", *crashlog
;
497 if (!zlog_default
|| !zlog_default
->ident
)
498 crashlog
= crashlog_default
;
500 snprintfrr(crashlog_buf
, sizeof(crashlog_buf
),
501 "/var/tmp/frr.%s.crashlog", zlog_default
->ident
);
502 crashlog
= crashlog_buf
;
504 return open(crashlog
, O_WRONLY
| O_CREAT
| O_EXCL
, LOGFILE_MASK
);
507 /* N.B. implicit priority is most severe */
510 static void crash_write(struct fbuf
*fb
, char *msgstart
)
512 if (fb
->pos
== fb
->buf
)
517 /* If no file logging configured, try to write to fallback log file. */
518 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
519 write(logfile_fd
, fb
->buf
, fb
->pos
- fb
->buf
);
521 write(STDERR_FILENO
, fb
->buf
, fb
->pos
- fb
->buf
);
523 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
524 write(STDOUT_FILENO
, fb
->buf
, fb
->pos
- fb
->buf
);
525 /* Remove trailing '\n' for monitor and syslog */
527 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
528 vty_log_fixed(fb
->buf
, fb
->pos
- fb
->buf
);
529 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
530 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
535 /* Note: the goal here is to use only async-signal-safe functions. */
536 void zlog_signal(int signo
, const char *action
, void *siginfo_v
,
537 void *program_counter
)
539 siginfo_t
*siginfo
= siginfo_v
;
541 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")
544 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= sizeof(buf
) };
548 bprintfrr(&fb
, "%s: ", zlog_default
->protoname
);
552 bprintfrr(&fb
, "Received signal %d at %lld", signo
, (long long)now
);
554 bprintfrr(&fb
, " (si_addr 0x%tx, PC 0x%tx)",
555 (ptrdiff_t)siginfo
->si_addr
,
556 (ptrdiff_t)program_counter
);
558 bprintfrr(&fb
, " (si_addr 0x%tx)",
559 (ptrdiff_t)siginfo
->si_addr
);
560 bprintfrr(&fb
, "; %s\n", action
);
562 crash_write(&fb
, msgstart
);
564 zlog_backtrace_sigsafe(PRI
, program_counter
);
569 tc
= pthread_getspecific(thread_current
);
572 bprintfrr(&fb
, "no thread information available\n");
574 bprintfrr(&fb
, "in thread %s scheduled from %s:%d\n",
575 tc
->funcname
, tc
->schedfrom
, tc
->schedfrom_line
);
577 crash_write(&fb
, NULL
);
580 /* Log a backtrace using only async-signal-safe functions.
581 Needs to be enhanced to support syslog logging. */
582 void zlog_backtrace_sigsafe(int priority
, void *program_counter
)
584 #ifdef HAVE_LIBUNWIND
586 struct fbuf fb
= { .buf
= buf
, .len
= sizeof(buf
) };
589 unw_word_t ip
, off
, sp
;
593 unw_init_local(&cursor
, &uc
);
594 while (unw_step(&cursor
) > 0) {
595 char name
[128] = "?";
597 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
598 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
600 if (!unw_get_proc_name(&cursor
, buf
, sizeof(buf
), &off
))
601 snprintfrr(name
, sizeof(name
), "%s+%#lx",
605 if (unw_is_signal_frame(&cursor
))
606 bprintfrr(&fb
, " ---- signal ----\n");
607 bprintfrr(&fb
, "%-30s %16lx %16lx", name
, (long)ip
, (long)sp
);
608 if (dladdr((void *)ip
, &dlinfo
))
609 bprintfrr(&fb
, " %s (mapped at %p)",
610 dlinfo
.dli_fname
, dlinfo
.dli_fbase
);
611 bprintfrr(&fb
, "\n");
612 crash_write(&fb
, NULL
);
614 #elif defined(HAVE_GLIBC_BACKTRACE) || defined(HAVE_PRINTSTACK)
615 static const char pclabel
[] = "Program counter: ";
619 struct fbuf fb
= { .buf
= buf
, .pos
= buf
, .len
= sizeof(buf
) };
622 #ifdef HAVE_GLIBC_BACKTRACE
623 size
= backtrace(array
, array_size(array
));
624 if (size
<= 0 || (size_t)size
> array_size(array
))
629 if (program_counter) { \
630 write_wrapper(FD, pclabel, sizeof(pclabel) - 1); \
631 backtrace_symbols_fd(&program_counter, 1, FD); \
633 write_wrapper(FD, fb.buf, fb.pos - fb.buf); \
634 backtrace_symbols_fd(array, size, FD); \
636 #elif defined(HAVE_PRINTSTACK)
641 if (program_counter) \
642 write_wrapper((FD), pclabel, sizeof(pclabel) - 1); \
643 write_wrapper((FD), fb.buf, fb.pos - fb.buf); \
646 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
648 bprintfrr(&fb
, "Backtrace for %d stack frames:\n", size
);
650 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
655 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
657 /* Remove trailing '\n' for monitor and syslog */
659 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
660 vty_log_fixed(fb
.buf
, fb
.pos
- fb
.buf
);
661 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
662 syslog_sigsafe(priority
| zlog_default
->facility
,
663 fb
.buf
, fb
.pos
- fb
.buf
);
666 #ifdef HAVE_GLIBC_BACKTRACE
667 bt
= backtrace_symbols(array
, size
);
669 /* Just print the function addresses. */
670 for (i
= 0; i
< size
; i
++) {
673 bprintfrr(&fb
, "%s", bt
[i
]);
675 bprintfrr(&fb
, "[bt %d] 0x%tx", i
,
676 (ptrdiff_t)(array
[i
]));
678 <= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
679 vty_log_fixed(fb
.buf
, fb
.pos
- fb
.buf
);
681 <= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
682 syslog_sigsafe(priority
683 | zlog_default
->facility
,
684 fb
.buf
, fb
.pos
- fb
.buf
);
691 #endif /* HAVE_STRACK_TRACE */
694 void zlog_backtrace(int priority
)
696 #ifdef HAVE_LIBUNWIND
700 unw_word_t ip
, off
, sp
;
704 unw_init_local(&cursor
, &uc
);
705 zlog(priority
, "Backtrace:");
706 while (unw_step(&cursor
) > 0) {
707 char name
[128] = "?";
709 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
710 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
712 if (unw_is_signal_frame(&cursor
))
713 zlog(priority
, " ---- signal ----");
715 if (!unw_get_proc_name(&cursor
, buf
, sizeof(buf
), &off
))
716 snprintf(name
, sizeof(name
), "%s+%#lx",
719 if (dladdr((void *)ip
, &dlinfo
))
720 zlog(priority
, "%-30s %16lx %16lx %s (mapped at %p)",
721 name
, (long)ip
, (long)sp
,
722 dlinfo
.dli_fname
, dlinfo
.dli_fbase
);
724 zlog(priority
, "%-30s %16lx %16lx",
725 name
, (long)ip
, (long)sp
);
727 #elif defined(HAVE_GLIBC_BACKTRACE)
732 size
= backtrace(array
, array_size(array
));
733 if (size
<= 0 || (size_t)size
> array_size(array
)) {
736 "Cannot get backtrace, returned invalid # of frames %d "
737 "(valid range is between 1 and %lu)",
738 size
, (unsigned long)(array_size(array
)));
741 zlog(priority
, "Backtrace for %d stack frames:", size
);
742 if (!(strings
= backtrace_symbols(array
, size
))) {
743 flog_err_sys(EC_LIB_SYSTEM_CALL
,
744 "Cannot get backtrace symbols (out of memory?)");
745 for (i
= 0; i
< size
; i
++)
746 zlog(priority
, "[bt %d] %p", i
, array
[i
]);
748 for (i
= 0; i
< size
; i
++)
749 zlog(priority
, "[bt %d] %s", i
, strings
[i
]);
752 #else /* !HAVE_GLIBC_BACKTRACE && !HAVE_LIBUNWIND */
753 zlog(priority
, "No backtrace available on this platform.");
757 void zlog(int priority
, const char *format
, ...)
761 va_start(args
, format
);
762 vzlog(priority
, format
, args
);
766 #define ZLOG_FUNC(FUNCNAME, PRIORITY) \
767 void FUNCNAME(const char *format, ...) \
770 va_start(args, format); \
771 vzlog(PRIORITY, format, args); \
775 ZLOG_FUNC(zlog_err
, LOG_ERR
)
777 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
779 ZLOG_FUNC(zlog_info
, LOG_INFO
)
781 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
783 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
787 void zlog_thread_info(int log_level
)
790 tc
= pthread_getspecific(thread_current
);
794 "Current thread function %s, scheduled from "
796 tc
->funcname
, tc
->schedfrom
, tc
->schedfrom_line
);
798 zlog(log_level
, "Current thread not known/applicable");
801 void _zlog_assert_failed(const char *assertion
, const char *file
,
802 unsigned int line
, const char *function
)
804 /* Force fallback file logging? */
805 if (zlog_default
&& !zlog_default
->fp
806 && ((logfile_fd
= open_crashlog()) >= 0)
807 && ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
808 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
809 zlog(LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
810 assertion
, file
, line
, (function
? function
: "?"));
811 zlog_backtrace(LOG_CRIT
);
812 zlog_thread_info(LOG_CRIT
);
813 log_memstats(stderr
, "log");
817 void memory_oom(size_t size
, const char *name
)
819 flog_err_sys(EC_LIB_SYSTEM_CALL
,
820 "out of memory: failed to allocate %zu bytes for %s"
823 zlog_backtrace(LOG_ERR
);
827 /* Open log stream */
828 void openzlog(const char *progname
, const char *protoname
,
829 unsigned short instance
, int syslog_flags
, int syslog_facility
)
834 zl
= XCALLOC(MTYPE_ZLOG
, sizeof(struct zlog
));
836 zl
->ident
= progname
;
837 zl
->protoname
= protoname
;
838 zl
->instance
= instance
;
839 zl
->facility
= syslog_facility
;
840 zl
->syslog_options
= syslog_flags
;
842 /* Set default logging levels. */
843 for (i
= 0; i
< array_size(zl
->maxlvl
); i
++)
844 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
845 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
846 zl
->default_lvl
= LOG_DEBUG
;
848 openlog(progname
, syslog_flags
, zl
->facility
);
850 frr_with_mutex(&loglock
) {
854 #ifdef HAVE_GLIBC_BACKTRACE
855 /* work around backtrace() using lazily resolved dynamically linked
856 * symbols, which will otherwise cause funny breakage in the SEGV
858 * (particularly, the dynamic linker can call malloc(), which uses locks
859 * in programs linked with -pthread, thus can deadlock.) */
861 backtrace(bt
, array_size(bt
));
862 free(backtrace_symbols(bt
, 0));
863 backtrace_symbols_fd(bt
, 0, 0);
869 frr_mutex_lock_autounlock(&loglock
);
871 struct zlog
*zl
= zlog_default
;
878 XFREE(MTYPE_ZLOG
, zl
->filename
);
880 XFREE(MTYPE_ZLOG
, zl
);
884 /* Called from command.c. */
885 void zlog_set_level(zlog_dest_t dest
, int log_level
)
887 frr_with_mutex(&loglock
) {
888 zlog_default
->maxlvl
[dest
] = log_level
;
892 int zlog_set_file(const char *filename
, int log_level
)
899 /* There is opend file. */
903 oldumask
= umask(0777 & ~LOGFILE_MASK
);
904 fp
= fopen(filename
, "a");
909 frr_with_mutex(&loglock
) {
913 zl
->filename
= XSTRDUP(MTYPE_ZLOG
, filename
);
914 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
916 logfile_fd
= fileno(fp
);
923 /* Reset opend file. */
924 int zlog_reset_file(void)
926 frr_mutex_lock_autounlock(&loglock
);
928 struct zlog
*zl
= zlog_default
;
934 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
936 XFREE(MTYPE_ZLOG
, zl
->filename
);
941 /* Reopen log file. */
942 int zlog_rotate(void)
944 pthread_mutex_lock(&loglock
);
946 struct zlog
*zl
= zlog_default
;
954 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
955 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
961 oldumask
= umask(0777 & ~LOGFILE_MASK
);
962 zl
->fp
= fopen(zl
->filename
, "a");
965 if (zl
->fp
== NULL
) {
967 pthread_mutex_unlock(&loglock
);
971 "Log rotate failed: cannot open file %s for append: %s",
972 zl
->filename
, safe_strerror(save_errno
));
975 pthread_mutex_lock(&loglock
);
977 logfile_fd
= fileno(zl
->fp
);
978 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
982 pthread_mutex_unlock(&loglock
);
987 /* Wrapper around strerror to handle case where it returns NULL. */
988 const char *safe_strerror(int errnum
)
990 const char *s
= strerror(errnum
);
991 return (s
!= NULL
) ? s
: "Unknown error";
994 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
995 static const struct zebra_desc_table command_types
[] = {
996 DESC_ENTRY(ZEBRA_INTERFACE_ADD
),
997 DESC_ENTRY(ZEBRA_INTERFACE_DELETE
),
998 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD
),
999 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE
),
1000 DESC_ENTRY(ZEBRA_INTERFACE_UP
),
1001 DESC_ENTRY(ZEBRA_INTERFACE_DOWN
),
1002 DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER
),
1003 DESC_ENTRY(ZEBRA_ROUTE_ADD
),
1004 DESC_ENTRY(ZEBRA_ROUTE_DELETE
),
1005 DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER
),
1006 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD
),
1007 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE
),
1008 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
1009 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
1010 DESC_ENTRY(ZEBRA_ROUTER_ID_ADD
),
1011 DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE
),
1012 DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE
),
1013 DESC_ENTRY(ZEBRA_HELLO
),
1014 DESC_ENTRY(ZEBRA_CAPABILITIES
),
1015 DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER
),
1016 DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER
),
1017 DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE
),
1018 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
),
1019 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
),
1020 DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE
),
1021 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_REGISTER
),
1022 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_UNREGISTER
),
1023 DESC_ENTRY(ZEBRA_IMPORT_CHECK_UPDATE
),
1024 DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER
),
1025 DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER
),
1026 DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE
),
1027 DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY
),
1028 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD
),
1029 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL
),
1030 DESC_ENTRY(ZEBRA_VRF_UNREGISTER
),
1031 DESC_ENTRY(ZEBRA_VRF_ADD
),
1032 DESC_ENTRY(ZEBRA_VRF_DELETE
),
1033 DESC_ENTRY(ZEBRA_VRF_LABEL
),
1034 DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE
),
1035 DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER
),
1036 DESC_ENTRY(ZEBRA_BFD_CLIENT_DEREGISTER
),
1037 DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV
),
1038 DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV
),
1039 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
),
1040 DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS
),
1041 DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD
),
1042 DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE
),
1043 DESC_ENTRY(ZEBRA_MPLS_LABELS_REPLACE
),
1044 DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS
),
1045 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT
),
1046 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
),
1047 DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK
),
1048 DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK
),
1049 DESC_ENTRY(ZEBRA_FEC_REGISTER
),
1050 DESC_ENTRY(ZEBRA_FEC_UNREGISTER
),
1051 DESC_ENTRY(ZEBRA_FEC_UPDATE
),
1052 DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI
),
1053 DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW
),
1054 DESC_ENTRY(ZEBRA_ADVERTISE_SVI_MACIP
),
1055 DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET
),
1056 DESC_ENTRY(ZEBRA_LOCAL_ES_ADD
),
1057 DESC_ENTRY(ZEBRA_LOCAL_ES_DEL
),
1058 DESC_ENTRY(ZEBRA_VNI_ADD
),
1059 DESC_ENTRY(ZEBRA_VNI_DEL
),
1060 DESC_ENTRY(ZEBRA_L3VNI_ADD
),
1061 DESC_ENTRY(ZEBRA_L3VNI_DEL
),
1062 DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD
),
1063 DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL
),
1064 DESC_ENTRY(ZEBRA_MACIP_ADD
),
1065 DESC_ENTRY(ZEBRA_MACIP_DEL
),
1066 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD
),
1067 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL
),
1068 DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD
),
1069 DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL
),
1070 DESC_ENTRY(ZEBRA_DUPLICATE_ADDR_DETECTION
),
1071 DESC_ENTRY(ZEBRA_PW_ADD
),
1072 DESC_ENTRY(ZEBRA_PW_DELETE
),
1073 DESC_ENTRY(ZEBRA_PW_SET
),
1074 DESC_ENTRY(ZEBRA_PW_UNSET
),
1075 DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE
),
1076 DESC_ENTRY(ZEBRA_RULE_ADD
),
1077 DESC_ENTRY(ZEBRA_RULE_DELETE
),
1078 DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER
),
1079 DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT
),
1080 DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK
),
1081 DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK
),
1082 DESC_ENTRY(ZEBRA_IPSET_CREATE
),
1083 DESC_ENTRY(ZEBRA_IPSET_DESTROY
),
1084 DESC_ENTRY(ZEBRA_IPSET_ENTRY_ADD
),
1085 DESC_ENTRY(ZEBRA_IPSET_ENTRY_DELETE
),
1086 DESC_ENTRY(ZEBRA_IPSET_NOTIFY_OWNER
),
1087 DESC_ENTRY(ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
),
1088 DESC_ENTRY(ZEBRA_IPTABLE_ADD
),
1089 DESC_ENTRY(ZEBRA_IPTABLE_DELETE
),
1090 DESC_ENTRY(ZEBRA_IPTABLE_NOTIFY_OWNER
),
1091 DESC_ENTRY(ZEBRA_VXLAN_FLOOD_CONTROL
),
1092 DESC_ENTRY(ZEBRA_VXLAN_SG_ADD
),
1093 DESC_ENTRY(ZEBRA_VXLAN_SG_DEL
),
1094 DESC_ENTRY(ZEBRA_VXLAN_SG_REPLAY
),
1095 DESC_ENTRY(ZEBRA_MLAG_PROCESS_UP
),
1096 DESC_ENTRY(ZEBRA_MLAG_PROCESS_DOWN
),
1097 DESC_ENTRY(ZEBRA_MLAG_CLIENT_REGISTER
),
1098 DESC_ENTRY(ZEBRA_MLAG_CLIENT_UNREGISTER
),
1099 DESC_ENTRY(ZEBRA_MLAG_FORWARD_MSG
),
1100 DESC_ENTRY(ZEBRA_ERROR
),
1101 DESC_ENTRY(ZEBRA_CLIENT_CAPABILITIES
)};
1104 static const struct zebra_desc_table unknown
= {0, "unknown", '?'};
1106 static const struct zebra_desc_table
*zroute_lookup(unsigned int zroute
)
1110 if (zroute
>= array_size(route_types
)) {
1111 flog_err(EC_LIB_DEVELOPMENT
, "unknown zebra route type: %u",
1115 if (zroute
== route_types
[zroute
].type
)
1116 return &route_types
[zroute
];
1117 for (i
= 0; i
< array_size(route_types
); i
++) {
1118 if (zroute
== route_types
[i
].type
) {
1120 "internal error: route type table out of order "
1121 "while searching for %u, please notify developers",
1123 return &route_types
[i
];
1126 flog_err(EC_LIB_DEVELOPMENT
,
1127 "internal error: cannot find route type %u in table!", zroute
);
1131 const char *zebra_route_string(unsigned int zroute
)
1133 return zroute_lookup(zroute
)->string
;
1136 char zebra_route_char(unsigned int zroute
)
1138 return zroute_lookup(zroute
)->chr
;
1141 const char *zserv_command_string(unsigned int command
)
1143 if (command
>= array_size(command_types
)) {
1144 flog_err(EC_LIB_DEVELOPMENT
, "unknown zserv command type: %u",
1146 return unknown
.string
;
1148 return command_types
[command
].string
;
1151 int proto_name2num(const char *s
)
1155 for (i
= 0; i
< array_size(route_types
); ++i
)
1156 if (strcasecmp(s
, route_types
[i
].string
) == 0)
1157 return route_types
[i
].type
;
1161 int proto_redistnum(int afi
, const char *s
)
1166 if (afi
== AFI_IP
) {
1167 if (strmatch(s
, "kernel"))
1168 return ZEBRA_ROUTE_KERNEL
;
1169 else if (strmatch(s
, "connected"))
1170 return ZEBRA_ROUTE_CONNECT
;
1171 else if (strmatch(s
, "static"))
1172 return ZEBRA_ROUTE_STATIC
;
1173 else if (strmatch(s
, "rip"))
1174 return ZEBRA_ROUTE_RIP
;
1175 else if (strmatch(s
, "eigrp"))
1176 return ZEBRA_ROUTE_EIGRP
;
1177 else if (strmatch(s
, "ospf"))
1178 return ZEBRA_ROUTE_OSPF
;
1179 else if (strmatch(s
, "isis"))
1180 return ZEBRA_ROUTE_ISIS
;
1181 else if (strmatch(s
, "bgp"))
1182 return ZEBRA_ROUTE_BGP
;
1183 else if (strmatch(s
, "table"))
1184 return ZEBRA_ROUTE_TABLE
;
1185 else if (strmatch(s
, "vnc"))
1186 return ZEBRA_ROUTE_VNC
;
1187 else if (strmatch(s
, "vnc-direct"))
1188 return ZEBRA_ROUTE_VNC_DIRECT
;
1189 else if (strmatch(s
, "nhrp"))
1190 return ZEBRA_ROUTE_NHRP
;
1191 else if (strmatch(s
, "babel"))
1192 return ZEBRA_ROUTE_BABEL
;
1193 else if (strmatch(s
, "sharp"))
1194 return ZEBRA_ROUTE_SHARP
;
1195 else if (strmatch(s
, "openfabric"))
1196 return ZEBRA_ROUTE_OPENFABRIC
;
1198 if (afi
== AFI_IP6
) {
1199 if (strmatch(s
, "kernel"))
1200 return ZEBRA_ROUTE_KERNEL
;
1201 else if (strmatch(s
, "connected"))
1202 return ZEBRA_ROUTE_CONNECT
;
1203 else if (strmatch(s
, "static"))
1204 return ZEBRA_ROUTE_STATIC
;
1205 else if (strmatch(s
, "ripng"))
1206 return ZEBRA_ROUTE_RIPNG
;
1207 else if (strmatch(s
, "ospf6"))
1208 return ZEBRA_ROUTE_OSPF6
;
1209 else if (strmatch(s
, "isis"))
1210 return ZEBRA_ROUTE_ISIS
;
1211 else if (strmatch(s
, "bgp"))
1212 return ZEBRA_ROUTE_BGP
;
1213 else if (strmatch(s
, "table"))
1214 return ZEBRA_ROUTE_TABLE
;
1215 else if (strmatch(s
, "vnc"))
1216 return ZEBRA_ROUTE_VNC
;
1217 else if (strmatch(s
, "vnc-direct"))
1218 return ZEBRA_ROUTE_VNC_DIRECT
;
1219 else if (strmatch(s
, "nhrp"))
1220 return ZEBRA_ROUTE_NHRP
;
1221 else if (strmatch(s
, "babel"))
1222 return ZEBRA_ROUTE_BABEL
;
1223 else if (strmatch(s
, "sharp"))
1224 return ZEBRA_ROUTE_SHARP
;
1225 else if (strmatch(s
, "openfabric"))
1226 return ZEBRA_ROUTE_OPENFABRIC
;
1231 void zlog_hexdump(const void *mem
, unsigned int len
)
1233 unsigned long i
= 0;
1235 unsigned int columns
= 8;
1237 * 19 bytes for 0xADDRESS:
1238 * 24 bytes for data; 2 chars plus a space per data byte
1240 * 8 bytes for ASCII representation
1241 * 1 byte for a newline
1242 * =====================
1243 * 53 bytes per 8 bytes of data
1244 * 1 byte for null term
1246 size_t bs
= ((len
/ 8) + 1) * 53 + 1;
1249 const unsigned char *memch
= mem
;
1251 memset(buf
, 0, sizeof(buf
));
1253 for (i
= 0; i
< len
+ ((len
% columns
) ? (columns
- len
% columns
) : 0);
1256 if (i
% columns
== 0)
1257 s
+= snprintf(s
, bs
- (s
- buf
),
1258 "0x%016lx: ", (unsigned long)memch
+ i
);
1260 /* print hex data */
1262 s
+= snprintf(s
, bs
- (s
- buf
), "%02x ", memch
[i
]);
1264 /* end of block, just aligning for ASCII dump */
1266 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1268 /* print ASCII dump */
1269 if (i
% columns
== (columns
- 1)) {
1270 for (j
= i
- (columns
- 1); j
<= i
; j
++) {
1271 /* end of block not really printing */
1273 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1274 else if (isprint(memch
[j
]))
1275 s
+= snprintf(s
, bs
- (s
- buf
), "%c",
1277 else /* other char */
1278 s
+= snprintf(s
, bs
- (s
- buf
), ".");
1280 s
+= snprintf(s
, bs
- (s
- buf
), "\n");
1283 zlog_debug("\n%s", buf
);
1286 const char *zlog_sanitize(char *buf
, size_t bufsz
, const void *in
, size_t inlen
)
1288 const char *inbuf
= in
;
1289 char *pos
= buf
, *end
= buf
+ bufsz
;
1290 const char *iend
= inbuf
+ inlen
;
1292 memset(buf
, 0, bufsz
);
1293 for (; inbuf
< iend
; inbuf
++) {
1294 /* don't write partial escape sequence */
1299 snprintf(pos
, end
- pos
, "\\n");
1300 else if (*inbuf
== '\r')
1301 snprintf(pos
, end
- pos
, "\\r");
1302 else if (*inbuf
== '\t')
1303 snprintf(pos
, end
- pos
, "\\t");
1304 else if (*inbuf
< ' ' || *inbuf
== '"' || *inbuf
>= 127)
1305 snprintf(pos
, end
- pos
, "\\x%02hhx", *inbuf
);