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"
37 /* for printstack on solaris */
38 #ifdef HAVE_UCONTEXT_H
43 #define UNW_LOCAL_ONLY
44 #include <libunwind.h>
48 DEFINE_MTYPE_STATIC(LIB
, ZLOG
, "Logging")
50 /* hook for external logging */
51 DEFINE_HOOK(zebra_ext_log
, (int priority
, const char *format
, va_list args
),
52 (priority
, format
, args
));
54 static int logfile_fd
= -1; /* Used in signal handler. */
56 struct zlog
*zlog_default
= NULL
;
57 bool zlog_startup_stderr
= true;
59 /* lock protecting zlog_default for mt-safe zlog */
60 static pthread_mutex_t loglock
= PTHREAD_MUTEX_INITIALIZER
;
62 const char *zlog_priority
[] = {
63 "emergencies", "alerts", "critical", "errors", "warnings",
64 "notifications", "informational", "debugging", NULL
,
70 * glibc has declared that the return value from write *must* not be
72 * gcc see's this problem and issues a warning for the line.
74 * Why is this a big deal you say? Because both of them are right
75 * and if you have -Werror enabled then all calls to write
76 * generate a build error and the build stops.
78 * clang has helpfully allowed this construct:
80 * to tell the compiler yeah I know it has a return value
81 * I don't care about it at this time.
82 * gcc doesn't have this ability.
84 * This code was written such that it didn't care about the
85 * return value from write. At this time do I want
86 * to go through and fix and test this code for correctness.
87 * So just wrapper the bad behavior and move on.
89 static void write_wrapper(int fd
, const void *buf
, size_t count
)
91 if (write(fd
, buf
, count
) <= 0)
98 * Looks up a message in a message list by key.
100 * If the message is not found, returns the provided error message.
102 * Terminates when it hits a struct message that's all zeros.
104 * @param mz the message list
105 * @param kz the message key
106 * @param nf the message to return if not found
107 * @return the message
109 const char *lookup_msg(const struct message
*mz
, int kz
, const char *nf
)
111 static struct message nt
= {0};
112 const char *rz
= nf
? nf
: "(no message found)";
113 const struct message
*pnt
;
114 for (pnt
= mz
; memcmp(pnt
, &nt
, sizeof(struct message
)); pnt
++)
115 if (pnt
->key
== kz
) {
116 rz
= pnt
->str
? pnt
->str
: rz
;
122 /* For time string format. */
123 size_t quagga_timestamp(int timestamp_precision
, char *buf
, size_t buflen
)
130 struct timeval clock
;
132 gettimeofday(&clock
, NULL
);
134 /* first, we update the cache if the time has changed */
135 if (cache
.last
!= clock
.tv_sec
) {
137 cache
.last
= clock
.tv_sec
;
138 tm
= localtime(&cache
.last
);
139 cache
.len
= strftime(cache
.buf
, sizeof(cache
.buf
),
140 "%Y/%m/%d %H:%M:%S", tm
);
142 /* note: it's not worth caching the subsecond part, because
143 chances are that back-to-back calls are not sufficiently close
145 for the clock not to have ticked forward */
147 if (buflen
> cache
.len
) {
148 memcpy(buf
, cache
.buf
, cache
.len
);
149 if ((timestamp_precision
> 0)
150 && (buflen
> cache
.len
+ 1 + timestamp_precision
)) {
151 /* should we worry about locale issues? */
152 static const int divisor
[] = {0, 100000, 10000, 1000,
155 char *p
= buf
+ cache
.len
+ 1
156 + (prec
= timestamp_precision
);
159 /* this is unlikely to happen, but protect anyway */
164 clock
.tv_usec
/= divisor
[prec
];
166 *p
-- = '0' + (clock
.tv_usec
% 10);
168 } while (--prec
> 0);
170 return cache
.len
+ 1 + timestamp_precision
;
172 buf
[cache
.len
] = '\0';
180 /* Utility routine for current time printing. */
181 static void time_print(FILE *fp
, struct timestamp_control
*ctl
)
183 if (!ctl
->already_rendered
) {
184 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
186 ctl
->already_rendered
= 1;
188 fprintf(fp
, "%s ", ctl
->buf
);
192 static void vzlog_file(struct zlog
*zl
, struct timestamp_control
*tsctl
,
193 const char *proto_str
, int record_priority
, int priority
,
194 FILE *fp
, const char *format
, va_list args
)
198 time_print(fp
, tsctl
);
200 fprintf(fp
, "%s: ", zlog_priority
[priority
]);
202 fprintf(fp
, "%s", proto_str
);
204 vfprintf(fp
, format
, ac
);
210 /* va_list version of zlog. */
211 void vzlog(int priority
, const char *format
, va_list args
)
213 pthread_mutex_lock(&loglock
);
216 int original_errno
= errno
;
217 struct timestamp_control tsctl
;
218 tsctl
.already_rendered
= 0;
219 struct zlog
*zl
= zlog_default
;
221 /* call external hook */
222 hook_call(zebra_ext_log
, priority
, format
, args
);
224 /* When zlog_default is also NULL, use stderr for logging. */
227 time_print(stderr
, &tsctl
);
228 fprintf(stderr
, "%s: ", "unknown");
229 vfprintf(stderr
, format
, args
);
230 fprintf(stderr
, "\n");
233 /* In this case we return at here. */
234 errno
= original_errno
;
235 pthread_mutex_unlock(&loglock
);
238 tsctl
.precision
= zl
->timestamp_precision
;
241 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
]) {
244 vsyslog(priority
| zlog_default
->facility
, format
, ac
);
249 sprintf(proto_str
, "%s[%d]: ", zl
->protoname
, zl
->instance
);
251 sprintf(proto_str
, "%s: ", zl
->protoname
);
254 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
255 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
256 zl
->fp
, format
, args
);
258 /* fixed-config logging to stderr while we're stating up & haven't
259 * daemonized / reached mainloop yet
261 * note the "else" on stdout output -- we don't want to print the same
262 * message to both stderr and stdout. */
263 if (zlog_startup_stderr
&& priority
<= LOG_WARNING
)
264 vzlog_file(zl
, &tsctl
, proto_str
, 1, priority
, stderr
, format
,
266 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
267 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
268 stdout
, format
, args
);
270 /* Terminal monitor. */
271 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
272 vty_log((zl
->record_priority
? zlog_priority
[priority
] : NULL
),
273 proto_str
, format
, &tsctl
, args
);
275 errno
= original_errno
;
276 pthread_mutex_unlock(&loglock
);
279 int vzlog_test(int priority
)
281 pthread_mutex_lock(&loglock
);
285 struct zlog
*zl
= zlog_default
;
287 /* When zlog_default is also NULL, use stderr for logging. */
291 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
294 else if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
297 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
299 /* Terminal monitor. */
300 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
303 pthread_mutex_unlock(&loglock
);
308 static char *str_append(char *dst
, int len
, const char *src
)
310 while ((len
-- > 0) && *src
)
315 static char *num_append(char *s
, int len
, unsigned long x
)
321 return str_append(s
, len
, "0");
322 *(t
= &buf
[sizeof(buf
) - 1]) = '\0';
323 while (x
&& (t
> buf
)) {
324 *--t
= '0' + (x
% 10);
327 return str_append(s
, len
, t
);
330 #if defined(SA_SIGINFO) \
331 || defined(HAVE_PRINTSTACK) \
332 || defined(HAVE_GLIBC_BACKTRACE)
333 static char *hex_append(char *s
, int len
, unsigned long x
)
339 return str_append(s
, len
, "0");
340 *(t
= &buf
[sizeof(buf
) - 1]) = '\0';
341 while (x
&& (t
> buf
)) {
342 unsigned int cc
= (x
% 16);
343 *--t
= ((cc
< 10) ? ('0' + cc
) : ('a' + cc
- 10));
346 return str_append(s
, len
, t
);
350 /* Needs to be enhanced to support Solaris. */
351 static int syslog_connect(void)
358 struct sockaddr_un addr
;
360 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) < 0)
362 addr
.sun_family
= AF_UNIX
;
364 #define SYSLOG_SOCKET_PATH _PATH_LOG
366 #define SYSLOG_SOCKET_PATH "/dev/log"
368 s
= str_append(addr
.sun_path
, sizeof(addr
.sun_path
),
370 #undef SYSLOG_SOCKET_PATH
372 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
380 static void syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
382 static int syslog_fd
= -1;
383 char buf
[sizeof("<1234567890>ripngd[1234567890]: ") + msglen
+ 50];
386 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
389 #define LOC s,buf+sizeof(buf)-s
391 s
= str_append(LOC
, "<");
392 s
= num_append(LOC
, priority
);
393 s
= str_append(LOC
, ">");
394 /* forget about the timestamp, too difficult in a signal handler */
395 s
= str_append(LOC
, zlog_default
->ident
);
396 if (zlog_default
->syslog_options
& LOG_PID
) {
397 s
= str_append(LOC
, "[");
398 s
= num_append(LOC
, getpid());
399 s
= str_append(LOC
, "]");
401 s
= str_append(LOC
, ": ");
402 s
= str_append(LOC
, msg
);
403 write_wrapper(syslog_fd
, buf
, s
- buf
);
407 static int open_crashlog(void)
409 #define CRASHLOG_PREFIX "/var/tmp/quagga."
410 #define CRASHLOG_SUFFIX "crashlog"
411 if (zlog_default
&& zlog_default
->ident
) {
412 /* Avoid strlen since it is not async-signal-safe. */
416 for (p
= zlog_default
->ident
, ilen
= 0; *p
; p
++)
419 char buf
[sizeof(CRASHLOG_PREFIX
) + ilen
420 + sizeof(CRASHLOG_SUFFIX
) + 3];
422 #define LOC s,buf+sizeof(buf)-s
423 s
= str_append(LOC
, CRASHLOG_PREFIX
);
424 s
= str_append(LOC
, zlog_default
->ident
);
425 s
= str_append(LOC
, ".");
426 s
= str_append(LOC
, CRASHLOG_SUFFIX
);
429 return open(buf
, O_WRONLY
| O_CREAT
| O_EXCL
,
433 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX
,
434 O_WRONLY
| O_CREAT
| O_EXCL
, LOGFILE_MASK
);
435 #undef CRASHLOG_SUFFIX
436 #undef CRASHLOG_PREFIX
439 /* Note: the goal here is to use only async-signal-safe functions. */
440 void zlog_signal(int signo
, const char *action
443 siginfo_t
*siginfo
, void *program_counter
448 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")
451 char *msgstart
= buf
;
452 #define LOC s,buf+sizeof(buf)-s
456 s
= str_append(LOC
, zlog_default
->protoname
);
461 s
= str_append(LOC
, "Received signal ");
462 s
= num_append(LOC
, signo
);
463 s
= str_append(LOC
, " at ");
464 s
= num_append(LOC
, now
);
466 s
= str_append(LOC
, " (si_addr 0x");
467 s
= hex_append(LOC
, (unsigned long)(siginfo
->si_addr
));
468 if (program_counter
) {
469 s
= str_append(LOC
, ", PC 0x");
470 s
= hex_append(LOC
, (unsigned long)program_counter
);
472 s
= str_append(LOC
, "); ");
473 #else /* SA_SIGINFO */
474 s
= str_append(LOC
, "; ");
475 #endif /* SA_SIGINFO */
476 s
= str_append(LOC
, action
);
477 if (s
< buf
+ sizeof(buf
))
480 /* N.B. implicit priority is most severe */
483 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
484 /* If no file logging configured, try to write to fallback log file. */
485 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
490 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
492 /* Remove trailing '\n' for monitor and syslog */
494 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
495 vty_log_fixed(buf
, s
- buf
);
496 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
497 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
502 zlog_backtrace_sigsafe(PRI
,
512 tc
= pthread_getspecific(thread_current
);
514 s
= str_append(LOC
, "no thread information available\n");
516 s
= str_append(LOC
, "in thread ");
517 s
= str_append(LOC
, tc
->funcname
);
518 s
= str_append(LOC
, " scheduled from ");
519 s
= str_append(LOC
, tc
->schedfrom
);
520 s
= str_append(LOC
, ":");
521 s
= num_append(LOC
, tc
->schedfrom_line
);
522 s
= str_append(LOC
, "\n");
525 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
526 /* If no file logging configured, try to write to fallback log file. */
532 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
534 /* Remove trailing '\n' for monitor and syslog */
536 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
537 vty_log_fixed(buf
, s
- buf
);
538 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
539 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
548 /* Log a backtrace using only async-signal-safe functions.
549 Needs to be enhanced to support syslog logging. */
550 void zlog_backtrace_sigsafe(int priority
, void *program_counter
)
552 #ifdef HAVE_LIBUNWIND
556 unw_word_t ip
, off
, sp
;
560 unw_init_local(&cursor
, &uc
);
561 while (unw_step(&cursor
) > 0) {
562 char name
[128] = "?";
564 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
565 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
567 if (unw_is_signal_frame(&cursor
))
568 dprintf(2, " ---- signal ----\n");
570 if (!unw_get_proc_name(&cursor
, buf
, sizeof(buf
), &off
)) {
571 snprintf(name
, sizeof(name
), "%s+%#lx",
574 dprintf(2, "%-30s %16lx %16lx", name
, (long)ip
, (long)sp
);
575 if (dladdr((void *)ip
, &dlinfo
)) {
576 dprintf(2, " %s (mapped at %p)",
577 dlinfo
.dli_fname
, dlinfo
.dli_fbase
);
582 #elif defined(HAVE_GLIBC_BACKTRACE) || defined(HAVE_PRINTSTACK)
583 static const char pclabel
[] = "Program counter: ";
587 char *s
, **bt
= NULL
;
588 #define LOC s,buf+sizeof(buf)-s
590 #ifdef HAVE_GLIBC_BACKTRACE
591 size
= backtrace(array
, array_size(array
));
592 if (size
<= 0 || (size_t)size
> array_size(array
))
597 if (program_counter) { \
598 write_wrapper(FD, pclabel, sizeof(pclabel) - 1); \
599 backtrace_symbols_fd(&program_counter, 1, FD); \
601 write_wrapper(FD, buf, s - buf); \
602 backtrace_symbols_fd(array, size, FD); \
604 #elif defined(HAVE_PRINTSTACK)
607 if (program_counter) \
608 write_wrapper((FD), pclabel, sizeof(pclabel) - 1); \
609 write_wrapper((FD), buf, s - buf); \
612 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
615 s
= str_append(LOC
, "Backtrace for ");
616 s
= num_append(LOC
, size
);
617 s
= str_append(LOC
, " stack frames:\n");
619 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
624 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
626 /* Remove trailing '\n' for monitor and syslog */
628 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
629 vty_log_fixed(buf
, s
- buf
);
630 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
631 syslog_sigsafe(priority
| zlog_default
->facility
, buf
,
635 #ifdef HAVE_GLIBC_BACKTRACE
636 bt
= backtrace_symbols(array
, size
);
638 /* Just print the function addresses. */
639 for (i
= 0; i
< size
; i
++) {
642 s
= str_append(LOC
, bt
[i
]);
644 s
= str_append(LOC
, "[bt ");
645 s
= num_append(LOC
, i
);
646 s
= str_append(LOC
, "] 0x");
648 LOC
, (unsigned long)(array
[i
]));
652 <= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
653 vty_log_fixed(buf
, s
- buf
);
655 <= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
668 #endif /* HAVE_STRACK_TRACE */
671 void zlog_backtrace(int priority
)
673 #ifdef HAVE_LIBUNWIND
677 unw_word_t ip
, off
, sp
;
681 unw_init_local(&cursor
, &uc
);
682 zlog(priority
, "Backtrace:");
683 while (unw_step(&cursor
) > 0) {
684 char name
[128] = "?";
686 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
687 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
689 if (unw_is_signal_frame(&cursor
))
690 zlog(priority
, " ---- signal ----");
692 if (!unw_get_proc_name(&cursor
, buf
, sizeof(buf
), &off
))
693 snprintf(name
, sizeof(name
), "%s+%#lx",
696 if (dladdr((void *)ip
, &dlinfo
))
697 zlog(priority
, "%-30s %16lx %16lx %s (mapped at %p)",
698 name
, (long)ip
, (long)sp
,
699 dlinfo
.dli_fname
, dlinfo
.dli_fbase
);
701 zlog(priority
, "%-30s %16lx %16lx",
702 name
, (long)ip
, (long)sp
);
704 #elif defined(HAVE_GLIBC_BACKTRACE)
709 size
= backtrace(array
, array_size(array
));
710 if (size
<= 0 || (size_t)size
> array_size(array
)) {
713 "Cannot get backtrace, returned invalid # of frames %d "
714 "(valid range is between 1 and %lu)",
715 size
, (unsigned long)(array_size(array
)));
718 zlog(priority
, "Backtrace for %d stack frames:", size
);
719 if (!(strings
= backtrace_symbols(array
, size
))) {
720 flog_err_sys(EC_LIB_SYSTEM_CALL
,
721 "Cannot get backtrace symbols (out of memory?)");
722 for (i
= 0; i
< size
; i
++)
723 zlog(priority
, "[bt %d] %p", i
, array
[i
]);
725 for (i
= 0; i
< size
; i
++)
726 zlog(priority
, "[bt %d] %s", i
, strings
[i
]);
729 #else /* !HAVE_GLIBC_BACKTRACE && !HAVE_LIBUNWIND */
730 zlog(priority
, "No backtrace available on this platform.");
734 void zlog(int priority
, const char *format
, ...)
738 va_start(args
, format
);
739 vzlog(priority
, format
, args
);
743 #define ZLOG_FUNC(FUNCNAME, PRIORITY) \
744 void FUNCNAME(const char *format, ...) \
747 va_start(args, format); \
748 vzlog(PRIORITY, format, args); \
752 ZLOG_FUNC(zlog_err
, LOG_ERR
)
754 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
756 ZLOG_FUNC(zlog_info
, LOG_INFO
)
758 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
760 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
764 void zlog_thread_info(int log_level
)
767 tc
= pthread_getspecific(thread_current
);
771 "Current thread function %s, scheduled from "
773 tc
->funcname
, tc
->schedfrom
, tc
->schedfrom_line
);
775 zlog(log_level
, "Current thread not known/applicable");
778 void _zlog_assert_failed(const char *assertion
, const char *file
,
779 unsigned int line
, const char *function
)
781 /* Force fallback file logging? */
782 if (zlog_default
&& !zlog_default
->fp
783 && ((logfile_fd
= open_crashlog()) >= 0)
784 && ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
785 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
786 zlog(LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
787 assertion
, file
, line
, (function
? function
: "?"));
788 zlog_backtrace(LOG_CRIT
);
789 zlog_thread_info(LOG_CRIT
);
790 log_memstats(stderr
, "log");
794 void memory_oom(size_t size
, const char *name
)
796 flog_err_sys(EC_LIB_SYSTEM_CALL
,
797 "out of memory: failed to allocate %zu bytes for %s"
800 zlog_backtrace(LOG_ERR
);
804 /* Open log stream */
805 void openzlog(const char *progname
, const char *protoname
,
806 unsigned short instance
, int syslog_flags
, int syslog_facility
)
811 zl
= XCALLOC(MTYPE_ZLOG
, sizeof(struct zlog
));
813 zl
->ident
= progname
;
814 zl
->protoname
= protoname
;
815 zl
->instance
= instance
;
816 zl
->facility
= syslog_facility
;
817 zl
->syslog_options
= syslog_flags
;
819 /* Set default logging levels. */
820 for (i
= 0; i
< array_size(zl
->maxlvl
); i
++)
821 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
822 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
823 zl
->default_lvl
= LOG_DEBUG
;
825 openlog(progname
, syslog_flags
, zl
->facility
);
827 pthread_mutex_lock(&loglock
);
829 pthread_mutex_unlock(&loglock
);
831 #ifdef HAVE_GLIBC_BACKTRACE
832 /* work around backtrace() using lazily resolved dynamically linked
833 * symbols, which will otherwise cause funny breakage in the SEGV
835 * (particularly, the dynamic linker can call malloc(), which uses locks
836 * in programs linked with -pthread, thus can deadlock.) */
838 backtrace(bt
, array_size(bt
));
839 free(backtrace_symbols(bt
, 0));
840 backtrace_symbols_fd(bt
, 0, 0);
846 pthread_mutex_lock(&loglock
);
847 struct zlog
*zl
= zlog_default
;
854 XFREE(MTYPE_ZLOG
, zl
->filename
);
856 XFREE(MTYPE_ZLOG
, zl
);
858 pthread_mutex_unlock(&loglock
);
861 /* Called from command.c. */
862 void zlog_set_level(zlog_dest_t dest
, int log_level
)
864 pthread_mutex_lock(&loglock
);
865 zlog_default
->maxlvl
[dest
] = log_level
;
866 pthread_mutex_unlock(&loglock
);
869 int zlog_set_file(const char *filename
, int log_level
)
876 /* There is opend file. */
880 oldumask
= umask(0777 & ~LOGFILE_MASK
);
881 fp
= fopen(filename
, "a");
886 pthread_mutex_lock(&loglock
);
890 zl
->filename
= XSTRDUP(MTYPE_ZLOG
, filename
);
891 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
893 logfile_fd
= fileno(fp
);
894 pthread_mutex_unlock(&loglock
);
900 /* Reset opend file. */
901 int zlog_reset_file(void)
903 pthread_mutex_lock(&loglock
);
905 struct zlog
*zl
= zlog_default
;
911 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
913 XFREE(MTYPE_ZLOG
, zl
->filename
);
916 pthread_mutex_unlock(&loglock
);
921 /* Reopen log file. */
922 int zlog_rotate(void)
924 pthread_mutex_lock(&loglock
);
926 struct zlog
*zl
= zlog_default
;
934 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
935 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
941 oldumask
= umask(0777 & ~LOGFILE_MASK
);
942 zl
->fp
= fopen(zl
->filename
, "a");
945 if (zl
->fp
== NULL
) {
947 pthread_mutex_unlock(&loglock
);
951 "Log rotate failed: cannot open file %s for append: %s",
952 zl
->filename
, safe_strerror(save_errno
));
955 pthread_mutex_lock(&loglock
);
957 logfile_fd
= fileno(zl
->fp
);
958 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
962 pthread_mutex_unlock(&loglock
);
967 /* Wrapper around strerror to handle case where it returns NULL. */
968 const char *safe_strerror(int errnum
)
970 const char *s
= strerror(errnum
);
971 return (s
!= NULL
) ? s
: "Unknown error";
974 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
975 static const struct zebra_desc_table command_types
[] = {
976 DESC_ENTRY(ZEBRA_INTERFACE_ADD
),
977 DESC_ENTRY(ZEBRA_INTERFACE_DELETE
),
978 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD
),
979 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE
),
980 DESC_ENTRY(ZEBRA_INTERFACE_UP
),
981 DESC_ENTRY(ZEBRA_INTERFACE_DOWN
),
982 DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER
),
983 DESC_ENTRY(ZEBRA_ROUTE_ADD
),
984 DESC_ENTRY(ZEBRA_ROUTE_DELETE
),
985 DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER
),
986 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD
),
987 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE
),
988 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
989 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
990 DESC_ENTRY(ZEBRA_ROUTER_ID_ADD
),
991 DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE
),
992 DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE
),
993 DESC_ENTRY(ZEBRA_HELLO
),
994 DESC_ENTRY(ZEBRA_CAPABILITIES
),
995 DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER
),
996 DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER
),
997 DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE
),
998 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
),
999 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
),
1000 DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE
),
1001 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_REGISTER
),
1002 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_UNREGISTER
),
1003 DESC_ENTRY(ZEBRA_IMPORT_CHECK_UPDATE
),
1004 DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER
),
1005 DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER
),
1006 DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE
),
1007 DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY
),
1008 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD
),
1009 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL
),
1010 DESC_ENTRY(ZEBRA_VRF_UNREGISTER
),
1011 DESC_ENTRY(ZEBRA_VRF_ADD
),
1012 DESC_ENTRY(ZEBRA_VRF_DELETE
),
1013 DESC_ENTRY(ZEBRA_VRF_LABEL
),
1014 DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE
),
1015 DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER
),
1016 DESC_ENTRY(ZEBRA_BFD_CLIENT_DEREGISTER
),
1017 DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV
),
1018 DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV
),
1019 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
),
1020 DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS
),
1021 DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD
),
1022 DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE
),
1023 DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS
),
1024 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT
),
1025 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
),
1026 DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK
),
1027 DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK
),
1028 DESC_ENTRY(ZEBRA_FEC_REGISTER
),
1029 DESC_ENTRY(ZEBRA_FEC_UNREGISTER
),
1030 DESC_ENTRY(ZEBRA_FEC_UPDATE
),
1031 DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI
),
1032 DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW
),
1033 DESC_ENTRY(ZEBRA_ADVERTISE_SVI_MACIP
),
1034 DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET
),
1035 DESC_ENTRY(ZEBRA_LOCAL_ES_ADD
),
1036 DESC_ENTRY(ZEBRA_LOCAL_ES_DEL
),
1037 DESC_ENTRY(ZEBRA_VNI_ADD
),
1038 DESC_ENTRY(ZEBRA_VNI_DEL
),
1039 DESC_ENTRY(ZEBRA_L3VNI_ADD
),
1040 DESC_ENTRY(ZEBRA_L3VNI_DEL
),
1041 DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD
),
1042 DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL
),
1043 DESC_ENTRY(ZEBRA_MACIP_ADD
),
1044 DESC_ENTRY(ZEBRA_MACIP_DEL
),
1045 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD
),
1046 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL
),
1047 DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD
),
1048 DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL
),
1049 DESC_ENTRY(ZEBRA_DUPLICATE_ADDR_DETECTION
),
1050 DESC_ENTRY(ZEBRA_PW_ADD
),
1051 DESC_ENTRY(ZEBRA_PW_DELETE
),
1052 DESC_ENTRY(ZEBRA_PW_SET
),
1053 DESC_ENTRY(ZEBRA_PW_UNSET
),
1054 DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE
),
1055 DESC_ENTRY(ZEBRA_RULE_ADD
),
1056 DESC_ENTRY(ZEBRA_RULE_DELETE
),
1057 DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER
),
1058 DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT
),
1059 DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK
),
1060 DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK
),
1061 DESC_ENTRY(ZEBRA_IPSET_CREATE
),
1062 DESC_ENTRY(ZEBRA_IPSET_DESTROY
),
1063 DESC_ENTRY(ZEBRA_IPSET_ENTRY_ADD
),
1064 DESC_ENTRY(ZEBRA_IPSET_ENTRY_DELETE
),
1065 DESC_ENTRY(ZEBRA_IPSET_NOTIFY_OWNER
),
1066 DESC_ENTRY(ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
),
1067 DESC_ENTRY(ZEBRA_IPTABLE_ADD
),
1068 DESC_ENTRY(ZEBRA_IPTABLE_DELETE
),
1069 DESC_ENTRY(ZEBRA_IPTABLE_NOTIFY_OWNER
),
1070 DESC_ENTRY(ZEBRA_VXLAN_FLOOD_CONTROL
),
1074 static const struct zebra_desc_table unknown
= {0, "unknown", '?'};
1076 static const struct zebra_desc_table
*zroute_lookup(unsigned int zroute
)
1080 if (zroute
>= array_size(route_types
)) {
1081 flog_err(EC_LIB_DEVELOPMENT
, "unknown zebra route type: %u",
1085 if (zroute
== route_types
[zroute
].type
)
1086 return &route_types
[zroute
];
1087 for (i
= 0; i
< array_size(route_types
); i
++) {
1088 if (zroute
== route_types
[i
].type
) {
1090 "internal error: route type table out of order "
1091 "while searching for %u, please notify developers",
1093 return &route_types
[i
];
1096 flog_err(EC_LIB_DEVELOPMENT
,
1097 "internal error: cannot find route type %u in table!", zroute
);
1101 const char *zebra_route_string(unsigned int zroute
)
1103 return zroute_lookup(zroute
)->string
;
1106 char zebra_route_char(unsigned int zroute
)
1108 return zroute_lookup(zroute
)->chr
;
1111 const char *zserv_command_string(unsigned int command
)
1113 if (command
>= array_size(command_types
)) {
1114 flog_err(EC_LIB_DEVELOPMENT
, "unknown zserv command type: %u",
1116 return unknown
.string
;
1118 return command_types
[command
].string
;
1121 int proto_name2num(const char *s
)
1125 for (i
= 0; i
< array_size(route_types
); ++i
)
1126 if (strcasecmp(s
, route_types
[i
].string
) == 0)
1127 return route_types
[i
].type
;
1131 int proto_redistnum(int afi
, const char *s
)
1136 if (afi
== AFI_IP
) {
1137 if (strmatch(s
, "kernel"))
1138 return ZEBRA_ROUTE_KERNEL
;
1139 else if (strmatch(s
, "connected"))
1140 return ZEBRA_ROUTE_CONNECT
;
1141 else if (strmatch(s
, "static"))
1142 return ZEBRA_ROUTE_STATIC
;
1143 else if (strmatch(s
, "rip"))
1144 return ZEBRA_ROUTE_RIP
;
1145 else if (strmatch(s
, "eigrp"))
1146 return ZEBRA_ROUTE_EIGRP
;
1147 else if (strmatch(s
, "ospf"))
1148 return ZEBRA_ROUTE_OSPF
;
1149 else if (strmatch(s
, "isis"))
1150 return ZEBRA_ROUTE_ISIS
;
1151 else if (strmatch(s
, "bgp"))
1152 return ZEBRA_ROUTE_BGP
;
1153 else if (strmatch(s
, "table"))
1154 return ZEBRA_ROUTE_TABLE
;
1155 else if (strmatch(s
, "vnc"))
1156 return ZEBRA_ROUTE_VNC
;
1157 else if (strmatch(s
, "vnc-direct"))
1158 return ZEBRA_ROUTE_VNC_DIRECT
;
1159 else if (strmatch(s
, "nhrp"))
1160 return ZEBRA_ROUTE_NHRP
;
1161 else if (strmatch(s
, "babel"))
1162 return ZEBRA_ROUTE_BABEL
;
1163 else if (strmatch(s
, "sharp"))
1164 return ZEBRA_ROUTE_SHARP
;
1165 else if (strmatch(s
, "openfabric"))
1166 return ZEBRA_ROUTE_OPENFABRIC
;
1168 if (afi
== AFI_IP6
) {
1169 if (strmatch(s
, "kernel"))
1170 return ZEBRA_ROUTE_KERNEL
;
1171 else if (strmatch(s
, "connected"))
1172 return ZEBRA_ROUTE_CONNECT
;
1173 else if (strmatch(s
, "static"))
1174 return ZEBRA_ROUTE_STATIC
;
1175 else if (strmatch(s
, "ripng"))
1176 return ZEBRA_ROUTE_RIPNG
;
1177 else if (strmatch(s
, "ospf6"))
1178 return ZEBRA_ROUTE_OSPF6
;
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
;
1201 void zlog_hexdump(const void *mem
, unsigned int len
)
1203 unsigned long i
= 0;
1205 unsigned int columns
= 8;
1207 * 19 bytes for 0xADDRESS:
1208 * 24 bytes for data; 2 chars plus a space per data byte
1210 * 8 bytes for ASCII representation
1211 * 1 byte for a newline
1212 * =====================
1213 * 53 bytes per 8 bytes of data
1214 * 1 byte for null term
1216 size_t bs
= ((len
/ 8) + 1) * 53 + 1;
1220 memset(buf
, 0, sizeof(buf
));
1222 for (i
= 0; i
< len
+ ((len
% columns
) ? (columns
- len
% columns
) : 0);
1225 if (i
% columns
== 0)
1226 s
+= snprintf(s
, bs
- (s
- buf
),
1227 "0x%016lx: ", (unsigned long)mem
+ i
);
1229 /* print hex data */
1231 s
+= snprintf(s
, bs
- (s
- buf
), "%02x ",
1232 0xFF & ((const char *)mem
)[i
]);
1234 /* end of block, just aligning for ASCII dump */
1236 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1238 /* print ASCII dump */
1239 if (i
% columns
== (columns
- 1)) {
1240 for (j
= i
- (columns
- 1); j
<= i
; j
++) {
1241 /* end of block not really printing */
1243 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1244 else if (isprint((int)((const char *)mem
)[j
]))
1246 s
, bs
- (s
- buf
), "%c",
1247 0xFF & ((const char *)mem
)[j
]);
1248 else /* other char */
1249 s
+= snprintf(s
, bs
- (s
- buf
), ".");
1251 s
+= snprintf(s
, bs
- (s
- buf
), "\n");
1254 zlog_debug("\n%s", buf
);
1257 const char *zlog_sanitize(char *buf
, size_t bufsz
, const void *in
, size_t inlen
)
1259 const char *inbuf
= in
;
1260 char *pos
= buf
, *end
= buf
+ bufsz
;
1261 const char *iend
= inbuf
+ inlen
;
1263 memset(buf
, 0, bufsz
);
1264 for (; inbuf
< iend
; inbuf
++) {
1265 /* don't write partial escape sequence */
1270 snprintf(pos
, end
- pos
, "\\n");
1271 else if (*inbuf
== '\r')
1272 snprintf(pos
, end
- pos
, "\\r");
1273 else if (*inbuf
== '\t')
1274 snprintf(pos
, end
- pos
, "\\t");
1275 else if (*inbuf
< ' ' || *inbuf
== '"' || *inbuf
>= 127)
1276 snprintf(pos
, end
- pos
, "\\x%02hhx", *inbuf
);