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
,
71 * glibc has declared that the return value from write *must* not be
73 * gcc see's this problem and issues a warning for the line.
75 * Why is this a big deal you say? Because both of them are right
76 * and if you have -Werror enabled then all calls to write
77 * generate a build error and the build stops.
79 * clang has helpfully allowed this construct:
81 * to tell the compiler yeah I know it has a return value
82 * I don't care about it at this time.
83 * gcc doesn't have this ability.
85 * This code was written such that it didn't care about the
86 * return value from write. At this time do I want
87 * to go through and fix and test this code for correctness.
88 * So just wrapper the bad behavior and move on.
90 static void write_wrapper(int fd
, const void *buf
, size_t count
)
92 if (write(fd
, buf
, count
) <= 0)
99 * Looks up a message in a message list by key.
101 * If the message is not found, returns the provided error message.
103 * Terminates when it hits a struct message that's all zeros.
105 * @param mz the message list
106 * @param kz the message key
107 * @param nf the message to return if not found
108 * @return the message
110 const char *lookup_msg(const struct message
*mz
, int kz
, const char *nf
)
112 static struct message nt
= {0};
113 const char *rz
= nf
? nf
: "(no message found)";
114 const struct message
*pnt
;
115 for (pnt
= mz
; memcmp(pnt
, &nt
, sizeof(struct message
)); pnt
++)
116 if (pnt
->key
== kz
) {
117 rz
= pnt
->str
? pnt
->str
: rz
;
123 /* For time string format. */
124 size_t quagga_timestamp(int timestamp_precision
, char *buf
, size_t buflen
)
131 struct timeval clock
;
133 gettimeofday(&clock
, NULL
);
135 /* first, we update the cache if the time has changed */
136 if (cache
.last
!= clock
.tv_sec
) {
138 cache
.last
= clock
.tv_sec
;
139 tm
= localtime(&cache
.last
);
140 cache
.len
= strftime(cache
.buf
, sizeof(cache
.buf
),
141 "%Y/%m/%d %H:%M:%S", tm
);
143 /* note: it's not worth caching the subsecond part, because
144 chances are that back-to-back calls are not sufficiently close
146 for the clock not to have ticked forward */
148 if (buflen
> cache
.len
) {
149 memcpy(buf
, cache
.buf
, cache
.len
);
150 if ((timestamp_precision
> 0)
151 && (buflen
> cache
.len
+ 1 + timestamp_precision
)) {
152 /* should we worry about locale issues? */
153 static const int divisor
[] = {0, 100000, 10000, 1000,
156 char *p
= buf
+ cache
.len
+ 1
157 + (prec
= timestamp_precision
);
160 /* this is unlikely to happen, but protect anyway */
165 clock
.tv_usec
/= divisor
[prec
];
167 *p
-- = '0' + (clock
.tv_usec
% 10);
169 } while (--prec
> 0);
171 return cache
.len
+ 1 + timestamp_precision
;
173 buf
[cache
.len
] = '\0';
181 /* Utility routine for current time printing. */
182 static void time_print(FILE *fp
, struct timestamp_control
*ctl
)
184 if (!ctl
->already_rendered
) {
185 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
187 ctl
->already_rendered
= 1;
189 fprintf(fp
, "%s ", ctl
->buf
);
193 static void vzlog_file(struct zlog
*zl
, struct timestamp_control
*tsctl
,
194 const char *proto_str
, int record_priority
, int priority
,
195 FILE *fp
, const char *msg
)
197 time_print(fp
, tsctl
);
199 fprintf(fp
, "%s: ", zlog_priority
[priority
]);
201 fprintf(fp
, "%s%s\n", proto_str
, msg
);
205 /* va_list version of zlog. */
206 void vzlog(int priority
, const char *format
, va_list args
)
208 pthread_mutex_lock(&loglock
);
211 int original_errno
= errno
;
212 struct timestamp_control tsctl
;
213 tsctl
.already_rendered
= 0;
214 struct zlog
*zl
= zlog_default
;
217 /* call external hook */
218 hook_call(zebra_ext_log
, priority
, format
, args
);
220 msg
= vasnprintfrr(MTYPE_TMP
, buf
, sizeof(buf
), format
, args
);
222 /* When zlog_default is also NULL, use stderr for logging. */
225 time_print(stderr
, &tsctl
);
226 fprintf(stderr
, "%s: %s\n", "unknown", msg
);
230 tsctl
.precision
= zl
->timestamp_precision
;
233 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
234 syslog(priority
| zlog_default
->facility
, "%s", msg
);
237 sprintf(proto_str
, "%s[%d]: ", zl
->protoname
, zl
->instance
);
239 sprintf(proto_str
, "%s: ", zl
->protoname
);
242 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
243 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
246 /* fixed-config logging to stderr while we're stating up & haven't
247 * daemonized / reached mainloop yet
249 * note the "else" on stdout output -- we don't want to print the same
250 * message to both stderr and stdout. */
251 if (zlog_startup_stderr
&& priority
<= LOG_WARNING
)
252 vzlog_file(zl
, &tsctl
, proto_str
, 1, priority
, stderr
, msg
);
253 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
254 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
257 /* Terminal monitor. */
258 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
259 vty_log((zl
->record_priority
? zlog_priority
[priority
] : NULL
),
260 proto_str
, msg
, &tsctl
);
264 XFREE(MTYPE_TMP
, msg
);
265 errno
= original_errno
;
266 pthread_mutex_unlock(&loglock
);
269 int vzlog_test(int priority
)
271 pthread_mutex_lock(&loglock
);
275 struct zlog
*zl
= zlog_default
;
277 /* When zlog_default is also NULL, use stderr for logging. */
281 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
284 else if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
287 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
289 /* Terminal monitor. */
290 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
293 pthread_mutex_unlock(&loglock
);
298 static char *str_append(char *dst
, int len
, const char *src
)
300 while ((len
-- > 0) && *src
)
305 static char *num_append(char *s
, int len
, unsigned long x
)
311 return str_append(s
, len
, "0");
312 *(t
= &buf
[sizeof(buf
) - 1]) = '\0';
313 while (x
&& (t
> buf
)) {
314 *--t
= '0' + (x
% 10);
317 return str_append(s
, len
, t
);
320 #if defined(SA_SIGINFO) \
321 || defined(HAVE_PRINTSTACK) \
322 || defined(HAVE_GLIBC_BACKTRACE)
323 static char *hex_append(char *s
, int len
, unsigned long x
)
329 return str_append(s
, len
, "0");
330 *(t
= &buf
[sizeof(buf
) - 1]) = '\0';
331 while (x
&& (t
> buf
)) {
332 unsigned int cc
= (x
% 16);
333 *--t
= ((cc
< 10) ? ('0' + cc
) : ('a' + cc
- 10));
336 return str_append(s
, len
, t
);
340 /* Needs to be enhanced to support Solaris. */
341 static int syslog_connect(void)
348 struct sockaddr_un addr
;
350 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) < 0)
352 addr
.sun_family
= AF_UNIX
;
354 #define SYSLOG_SOCKET_PATH _PATH_LOG
356 #define SYSLOG_SOCKET_PATH "/dev/log"
358 s
= str_append(addr
.sun_path
, sizeof(addr
.sun_path
),
360 #undef SYSLOG_SOCKET_PATH
362 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
370 static void syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
372 static int syslog_fd
= -1;
373 char buf
[sizeof("<1234567890>ripngd[1234567890]: ") + msglen
+ 50];
376 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
379 #define LOC s,buf+sizeof(buf)-s
381 s
= str_append(LOC
, "<");
382 s
= num_append(LOC
, priority
);
383 s
= str_append(LOC
, ">");
384 /* forget about the timestamp, too difficult in a signal handler */
385 s
= str_append(LOC
, zlog_default
->ident
);
386 if (zlog_default
->syslog_options
& LOG_PID
) {
387 s
= str_append(LOC
, "[");
388 s
= num_append(LOC
, getpid());
389 s
= str_append(LOC
, "]");
391 s
= str_append(LOC
, ": ");
392 s
= str_append(LOC
, msg
);
393 write_wrapper(syslog_fd
, buf
, s
- buf
);
397 static int open_crashlog(void)
399 #define CRASHLOG_PREFIX "/var/tmp/quagga."
400 #define CRASHLOG_SUFFIX "crashlog"
401 if (zlog_default
&& zlog_default
->ident
) {
402 /* Avoid strlen since it is not async-signal-safe. */
406 for (p
= zlog_default
->ident
, ilen
= 0; *p
; p
++)
409 char buf
[sizeof(CRASHLOG_PREFIX
) + ilen
410 + sizeof(CRASHLOG_SUFFIX
) + 3];
412 #define LOC s,buf+sizeof(buf)-s
413 s
= str_append(LOC
, CRASHLOG_PREFIX
);
414 s
= str_append(LOC
, zlog_default
->ident
);
415 s
= str_append(LOC
, ".");
416 s
= str_append(LOC
, CRASHLOG_SUFFIX
);
419 return open(buf
, O_WRONLY
| O_CREAT
| O_EXCL
,
423 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX
,
424 O_WRONLY
| O_CREAT
| O_EXCL
, LOGFILE_MASK
);
425 #undef CRASHLOG_SUFFIX
426 #undef CRASHLOG_PREFIX
429 /* Note: the goal here is to use only async-signal-safe functions. */
430 void zlog_signal(int signo
, const char *action
433 siginfo_t
*siginfo
, void *program_counter
438 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")
441 char *msgstart
= buf
;
442 #define LOC s,buf+sizeof(buf)-s
446 s
= str_append(LOC
, zlog_default
->protoname
);
451 s
= str_append(LOC
, "Received signal ");
452 s
= num_append(LOC
, signo
);
453 s
= str_append(LOC
, " at ");
454 s
= num_append(LOC
, now
);
456 s
= str_append(LOC
, " (si_addr 0x");
457 s
= hex_append(LOC
, (unsigned long)(siginfo
->si_addr
));
458 if (program_counter
) {
459 s
= str_append(LOC
, ", PC 0x");
460 s
= hex_append(LOC
, (unsigned long)program_counter
);
462 s
= str_append(LOC
, "); ");
463 #else /* SA_SIGINFO */
464 s
= str_append(LOC
, "; ");
465 #endif /* SA_SIGINFO */
466 s
= str_append(LOC
, action
);
467 if (s
< buf
+ sizeof(buf
))
470 /* N.B. implicit priority is most severe */
473 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
474 /* If no file logging configured, try to write to fallback log file. */
475 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
480 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
482 /* Remove trailing '\n' for monitor and syslog */
484 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
485 vty_log_fixed(buf
, s
- buf
);
486 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
487 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
492 zlog_backtrace_sigsafe(PRI
,
502 tc
= pthread_getspecific(thread_current
);
504 s
= str_append(LOC
, "no thread information available\n");
506 s
= str_append(LOC
, "in thread ");
507 s
= str_append(LOC
, tc
->funcname
);
508 s
= str_append(LOC
, " scheduled from ");
509 s
= str_append(LOC
, tc
->schedfrom
);
510 s
= str_append(LOC
, ":");
511 s
= num_append(LOC
, tc
->schedfrom_line
);
512 s
= str_append(LOC
, "\n");
515 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
516 /* If no file logging configured, try to write to fallback log file. */
522 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
524 /* Remove trailing '\n' for monitor and syslog */
526 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
527 vty_log_fixed(buf
, s
- buf
);
528 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
529 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
538 /* Log a backtrace using only async-signal-safe functions.
539 Needs to be enhanced to support syslog logging. */
540 void zlog_backtrace_sigsafe(int priority
, void *program_counter
)
542 #ifdef HAVE_LIBUNWIND
546 unw_word_t ip
, off
, sp
;
550 unw_init_local(&cursor
, &uc
);
551 while (unw_step(&cursor
) > 0) {
552 char name
[128] = "?";
554 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
555 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
557 if (unw_is_signal_frame(&cursor
))
558 dprintf(2, " ---- signal ----\n");
560 if (!unw_get_proc_name(&cursor
, buf
, sizeof(buf
), &off
)) {
561 snprintf(name
, sizeof(name
), "%s+%#lx",
564 dprintf(2, "%-30s %16lx %16lx", name
, (long)ip
, (long)sp
);
565 if (dladdr((void *)ip
, &dlinfo
)) {
566 dprintf(2, " %s (mapped at %p)",
567 dlinfo
.dli_fname
, dlinfo
.dli_fbase
);
572 #elif defined(HAVE_GLIBC_BACKTRACE) || defined(HAVE_PRINTSTACK)
573 static const char pclabel
[] = "Program counter: ";
577 char *s
, **bt
= NULL
;
578 #define LOC s,buf+sizeof(buf)-s
580 #ifdef HAVE_GLIBC_BACKTRACE
581 size
= backtrace(array
, array_size(array
));
582 if (size
<= 0 || (size_t)size
> array_size(array
))
587 if (program_counter) { \
588 write_wrapper(FD, pclabel, sizeof(pclabel) - 1); \
589 backtrace_symbols_fd(&program_counter, 1, FD); \
591 write_wrapper(FD, buf, s - buf); \
592 backtrace_symbols_fd(array, size, FD); \
594 #elif defined(HAVE_PRINTSTACK)
599 if (program_counter) \
600 write_wrapper((FD), pclabel, sizeof(pclabel) - 1); \
601 write_wrapper((FD), buf, s - buf); \
604 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
607 s
= str_append(LOC
, "Backtrace for ");
608 s
= num_append(LOC
, size
);
609 s
= str_append(LOC
, " stack frames:\n");
611 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
616 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
618 /* Remove trailing '\n' for monitor and syslog */
620 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
621 vty_log_fixed(buf
, s
- buf
);
622 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
623 syslog_sigsafe(priority
| zlog_default
->facility
, buf
,
627 #ifdef HAVE_GLIBC_BACKTRACE
628 bt
= backtrace_symbols(array
, size
);
630 /* Just print the function addresses. */
631 for (i
= 0; i
< size
; i
++) {
634 s
= str_append(LOC
, bt
[i
]);
636 s
= str_append(LOC
, "[bt ");
637 s
= num_append(LOC
, i
);
638 s
= str_append(LOC
, "] 0x");
640 LOC
, (unsigned long)(array
[i
]));
644 <= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
645 vty_log_fixed(buf
, s
- buf
);
647 <= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
660 #endif /* HAVE_STRACK_TRACE */
663 void zlog_backtrace(int priority
)
665 #ifdef HAVE_LIBUNWIND
669 unw_word_t ip
, off
, sp
;
673 unw_init_local(&cursor
, &uc
);
674 zlog(priority
, "Backtrace:");
675 while (unw_step(&cursor
) > 0) {
676 char name
[128] = "?";
678 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
679 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
681 if (unw_is_signal_frame(&cursor
))
682 zlog(priority
, " ---- signal ----");
684 if (!unw_get_proc_name(&cursor
, buf
, sizeof(buf
), &off
))
685 snprintf(name
, sizeof(name
), "%s+%#lx",
688 if (dladdr((void *)ip
, &dlinfo
))
689 zlog(priority
, "%-30s %16lx %16lx %s (mapped at %p)",
690 name
, (long)ip
, (long)sp
,
691 dlinfo
.dli_fname
, dlinfo
.dli_fbase
);
693 zlog(priority
, "%-30s %16lx %16lx",
694 name
, (long)ip
, (long)sp
);
696 #elif defined(HAVE_GLIBC_BACKTRACE)
701 size
= backtrace(array
, array_size(array
));
702 if (size
<= 0 || (size_t)size
> array_size(array
)) {
705 "Cannot get backtrace, returned invalid # of frames %d "
706 "(valid range is between 1 and %lu)",
707 size
, (unsigned long)(array_size(array
)));
710 zlog(priority
, "Backtrace for %d stack frames:", size
);
711 if (!(strings
= backtrace_symbols(array
, size
))) {
712 flog_err_sys(EC_LIB_SYSTEM_CALL
,
713 "Cannot get backtrace symbols (out of memory?)");
714 for (i
= 0; i
< size
; i
++)
715 zlog(priority
, "[bt %d] %p", i
, array
[i
]);
717 for (i
= 0; i
< size
; i
++)
718 zlog(priority
, "[bt %d] %s", i
, strings
[i
]);
721 #else /* !HAVE_GLIBC_BACKTRACE && !HAVE_LIBUNWIND */
722 zlog(priority
, "No backtrace available on this platform.");
726 void zlog(int priority
, const char *format
, ...)
730 va_start(args
, format
);
731 vzlog(priority
, format
, args
);
735 #define ZLOG_FUNC(FUNCNAME, PRIORITY) \
736 void FUNCNAME(const char *format, ...) \
739 va_start(args, format); \
740 vzlog(PRIORITY, format, args); \
744 ZLOG_FUNC(zlog_err
, LOG_ERR
)
746 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
748 ZLOG_FUNC(zlog_info
, LOG_INFO
)
750 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
752 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
756 void zlog_thread_info(int log_level
)
759 tc
= pthread_getspecific(thread_current
);
763 "Current thread function %s, scheduled from "
765 tc
->funcname
, tc
->schedfrom
, tc
->schedfrom_line
);
767 zlog(log_level
, "Current thread not known/applicable");
770 void _zlog_assert_failed(const char *assertion
, const char *file
,
771 unsigned int line
, const char *function
)
773 /* Force fallback file logging? */
774 if (zlog_default
&& !zlog_default
->fp
775 && ((logfile_fd
= open_crashlog()) >= 0)
776 && ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
777 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
778 zlog(LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
779 assertion
, file
, line
, (function
? function
: "?"));
780 zlog_backtrace(LOG_CRIT
);
781 zlog_thread_info(LOG_CRIT
);
782 log_memstats(stderr
, "log");
786 void memory_oom(size_t size
, const char *name
)
788 flog_err_sys(EC_LIB_SYSTEM_CALL
,
789 "out of memory: failed to allocate %zu bytes for %s"
792 zlog_backtrace(LOG_ERR
);
796 /* Open log stream */
797 void openzlog(const char *progname
, const char *protoname
,
798 unsigned short instance
, int syslog_flags
, int syslog_facility
)
803 zl
= XCALLOC(MTYPE_ZLOG
, sizeof(struct zlog
));
805 zl
->ident
= progname
;
806 zl
->protoname
= protoname
;
807 zl
->instance
= instance
;
808 zl
->facility
= syslog_facility
;
809 zl
->syslog_options
= syslog_flags
;
811 /* Set default logging levels. */
812 for (i
= 0; i
< array_size(zl
->maxlvl
); i
++)
813 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
814 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
815 zl
->default_lvl
= LOG_DEBUG
;
817 openlog(progname
, syslog_flags
, zl
->facility
);
819 pthread_mutex_lock(&loglock
);
821 pthread_mutex_unlock(&loglock
);
823 #ifdef HAVE_GLIBC_BACKTRACE
824 /* work around backtrace() using lazily resolved dynamically linked
825 * symbols, which will otherwise cause funny breakage in the SEGV
827 * (particularly, the dynamic linker can call malloc(), which uses locks
828 * in programs linked with -pthread, thus can deadlock.) */
830 backtrace(bt
, array_size(bt
));
831 free(backtrace_symbols(bt
, 0));
832 backtrace_symbols_fd(bt
, 0, 0);
838 pthread_mutex_lock(&loglock
);
839 struct zlog
*zl
= zlog_default
;
846 XFREE(MTYPE_ZLOG
, zl
->filename
);
848 XFREE(MTYPE_ZLOG
, zl
);
850 pthread_mutex_unlock(&loglock
);
853 /* Called from command.c. */
854 void zlog_set_level(zlog_dest_t dest
, int log_level
)
856 pthread_mutex_lock(&loglock
);
857 zlog_default
->maxlvl
[dest
] = log_level
;
858 pthread_mutex_unlock(&loglock
);
861 int zlog_set_file(const char *filename
, int log_level
)
868 /* There is opend file. */
872 oldumask
= umask(0777 & ~LOGFILE_MASK
);
873 fp
= fopen(filename
, "a");
878 pthread_mutex_lock(&loglock
);
882 zl
->filename
= XSTRDUP(MTYPE_ZLOG
, filename
);
883 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
885 logfile_fd
= fileno(fp
);
886 pthread_mutex_unlock(&loglock
);
892 /* Reset opend file. */
893 int zlog_reset_file(void)
895 pthread_mutex_lock(&loglock
);
897 struct zlog
*zl
= zlog_default
;
903 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
905 XFREE(MTYPE_ZLOG
, zl
->filename
);
908 pthread_mutex_unlock(&loglock
);
913 /* Reopen log file. */
914 int zlog_rotate(void)
916 pthread_mutex_lock(&loglock
);
918 struct zlog
*zl
= zlog_default
;
926 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
927 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
933 oldumask
= umask(0777 & ~LOGFILE_MASK
);
934 zl
->fp
= fopen(zl
->filename
, "a");
937 if (zl
->fp
== NULL
) {
939 pthread_mutex_unlock(&loglock
);
943 "Log rotate failed: cannot open file %s for append: %s",
944 zl
->filename
, safe_strerror(save_errno
));
947 pthread_mutex_lock(&loglock
);
949 logfile_fd
= fileno(zl
->fp
);
950 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
954 pthread_mutex_unlock(&loglock
);
959 /* Wrapper around strerror to handle case where it returns NULL. */
960 const char *safe_strerror(int errnum
)
962 const char *s
= strerror(errnum
);
963 return (s
!= NULL
) ? s
: "Unknown error";
966 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
967 static const struct zebra_desc_table command_types
[] = {
968 DESC_ENTRY(ZEBRA_INTERFACE_ADD
),
969 DESC_ENTRY(ZEBRA_INTERFACE_DELETE
),
970 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD
),
971 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE
),
972 DESC_ENTRY(ZEBRA_INTERFACE_UP
),
973 DESC_ENTRY(ZEBRA_INTERFACE_DOWN
),
974 DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER
),
975 DESC_ENTRY(ZEBRA_ROUTE_ADD
),
976 DESC_ENTRY(ZEBRA_ROUTE_DELETE
),
977 DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER
),
978 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD
),
979 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE
),
980 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
981 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
982 DESC_ENTRY(ZEBRA_ROUTER_ID_ADD
),
983 DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE
),
984 DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE
),
985 DESC_ENTRY(ZEBRA_HELLO
),
986 DESC_ENTRY(ZEBRA_CAPABILITIES
),
987 DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER
),
988 DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER
),
989 DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE
),
990 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
),
991 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
),
992 DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE
),
993 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_REGISTER
),
994 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_UNREGISTER
),
995 DESC_ENTRY(ZEBRA_IMPORT_CHECK_UPDATE
),
996 DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER
),
997 DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER
),
998 DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE
),
999 DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY
),
1000 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD
),
1001 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL
),
1002 DESC_ENTRY(ZEBRA_VRF_UNREGISTER
),
1003 DESC_ENTRY(ZEBRA_VRF_ADD
),
1004 DESC_ENTRY(ZEBRA_VRF_DELETE
),
1005 DESC_ENTRY(ZEBRA_VRF_LABEL
),
1006 DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE
),
1007 DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER
),
1008 DESC_ENTRY(ZEBRA_BFD_CLIENT_DEREGISTER
),
1009 DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV
),
1010 DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV
),
1011 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
),
1012 DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS
),
1013 DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD
),
1014 DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE
),
1015 DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS
),
1016 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT
),
1017 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT_ASYNC
),
1018 DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK
),
1019 DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK
),
1020 DESC_ENTRY(ZEBRA_FEC_REGISTER
),
1021 DESC_ENTRY(ZEBRA_FEC_UNREGISTER
),
1022 DESC_ENTRY(ZEBRA_FEC_UPDATE
),
1023 DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI
),
1024 DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW
),
1025 DESC_ENTRY(ZEBRA_ADVERTISE_SVI_MACIP
),
1026 DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET
),
1027 DESC_ENTRY(ZEBRA_LOCAL_ES_ADD
),
1028 DESC_ENTRY(ZEBRA_LOCAL_ES_DEL
),
1029 DESC_ENTRY(ZEBRA_VNI_ADD
),
1030 DESC_ENTRY(ZEBRA_VNI_DEL
),
1031 DESC_ENTRY(ZEBRA_L3VNI_ADD
),
1032 DESC_ENTRY(ZEBRA_L3VNI_DEL
),
1033 DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD
),
1034 DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL
),
1035 DESC_ENTRY(ZEBRA_MACIP_ADD
),
1036 DESC_ENTRY(ZEBRA_MACIP_DEL
),
1037 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD
),
1038 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL
),
1039 DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD
),
1040 DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL
),
1041 DESC_ENTRY(ZEBRA_DUPLICATE_ADDR_DETECTION
),
1042 DESC_ENTRY(ZEBRA_PW_ADD
),
1043 DESC_ENTRY(ZEBRA_PW_DELETE
),
1044 DESC_ENTRY(ZEBRA_PW_SET
),
1045 DESC_ENTRY(ZEBRA_PW_UNSET
),
1046 DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE
),
1047 DESC_ENTRY(ZEBRA_RULE_ADD
),
1048 DESC_ENTRY(ZEBRA_RULE_DELETE
),
1049 DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER
),
1050 DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT
),
1051 DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK
),
1052 DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK
),
1053 DESC_ENTRY(ZEBRA_IPSET_CREATE
),
1054 DESC_ENTRY(ZEBRA_IPSET_DESTROY
),
1055 DESC_ENTRY(ZEBRA_IPSET_ENTRY_ADD
),
1056 DESC_ENTRY(ZEBRA_IPSET_ENTRY_DELETE
),
1057 DESC_ENTRY(ZEBRA_IPSET_NOTIFY_OWNER
),
1058 DESC_ENTRY(ZEBRA_IPSET_ENTRY_NOTIFY_OWNER
),
1059 DESC_ENTRY(ZEBRA_IPTABLE_ADD
),
1060 DESC_ENTRY(ZEBRA_IPTABLE_DELETE
),
1061 DESC_ENTRY(ZEBRA_IPTABLE_NOTIFY_OWNER
),
1062 DESC_ENTRY(ZEBRA_VXLAN_FLOOD_CONTROL
),
1063 DESC_ENTRY(ZEBRA_VXLAN_SG_ADD
),
1064 DESC_ENTRY(ZEBRA_VXLAN_SG_DEL
),
1068 static const struct zebra_desc_table unknown
= {0, "unknown", '?'};
1070 static const struct zebra_desc_table
*zroute_lookup(unsigned int zroute
)
1074 if (zroute
>= array_size(route_types
)) {
1075 flog_err(EC_LIB_DEVELOPMENT
, "unknown zebra route type: %u",
1079 if (zroute
== route_types
[zroute
].type
)
1080 return &route_types
[zroute
];
1081 for (i
= 0; i
< array_size(route_types
); i
++) {
1082 if (zroute
== route_types
[i
].type
) {
1084 "internal error: route type table out of order "
1085 "while searching for %u, please notify developers",
1087 return &route_types
[i
];
1090 flog_err(EC_LIB_DEVELOPMENT
,
1091 "internal error: cannot find route type %u in table!", zroute
);
1095 const char *zebra_route_string(unsigned int zroute
)
1097 return zroute_lookup(zroute
)->string
;
1100 char zebra_route_char(unsigned int zroute
)
1102 return zroute_lookup(zroute
)->chr
;
1105 const char *zserv_command_string(unsigned int command
)
1107 if (command
>= array_size(command_types
)) {
1108 flog_err(EC_LIB_DEVELOPMENT
, "unknown zserv command type: %u",
1110 return unknown
.string
;
1112 return command_types
[command
].string
;
1115 int proto_name2num(const char *s
)
1119 for (i
= 0; i
< array_size(route_types
); ++i
)
1120 if (strcasecmp(s
, route_types
[i
].string
) == 0)
1121 return route_types
[i
].type
;
1125 int proto_redistnum(int afi
, const char *s
)
1130 if (afi
== AFI_IP
) {
1131 if (strmatch(s
, "kernel"))
1132 return ZEBRA_ROUTE_KERNEL
;
1133 else if (strmatch(s
, "connected"))
1134 return ZEBRA_ROUTE_CONNECT
;
1135 else if (strmatch(s
, "static"))
1136 return ZEBRA_ROUTE_STATIC
;
1137 else if (strmatch(s
, "rip"))
1138 return ZEBRA_ROUTE_RIP
;
1139 else if (strmatch(s
, "eigrp"))
1140 return ZEBRA_ROUTE_EIGRP
;
1141 else if (strmatch(s
, "ospf"))
1142 return ZEBRA_ROUTE_OSPF
;
1143 else if (strmatch(s
, "isis"))
1144 return ZEBRA_ROUTE_ISIS
;
1145 else if (strmatch(s
, "bgp"))
1146 return ZEBRA_ROUTE_BGP
;
1147 else if (strmatch(s
, "table"))
1148 return ZEBRA_ROUTE_TABLE
;
1149 else if (strmatch(s
, "vnc"))
1150 return ZEBRA_ROUTE_VNC
;
1151 else if (strmatch(s
, "vnc-direct"))
1152 return ZEBRA_ROUTE_VNC_DIRECT
;
1153 else if (strmatch(s
, "nhrp"))
1154 return ZEBRA_ROUTE_NHRP
;
1155 else if (strmatch(s
, "babel"))
1156 return ZEBRA_ROUTE_BABEL
;
1157 else if (strmatch(s
, "sharp"))
1158 return ZEBRA_ROUTE_SHARP
;
1159 else if (strmatch(s
, "openfabric"))
1160 return ZEBRA_ROUTE_OPENFABRIC
;
1162 if (afi
== AFI_IP6
) {
1163 if (strmatch(s
, "kernel"))
1164 return ZEBRA_ROUTE_KERNEL
;
1165 else if (strmatch(s
, "connected"))
1166 return ZEBRA_ROUTE_CONNECT
;
1167 else if (strmatch(s
, "static"))
1168 return ZEBRA_ROUTE_STATIC
;
1169 else if (strmatch(s
, "ripng"))
1170 return ZEBRA_ROUTE_RIPNG
;
1171 else if (strmatch(s
, "ospf6"))
1172 return ZEBRA_ROUTE_OSPF6
;
1173 else if (strmatch(s
, "isis"))
1174 return ZEBRA_ROUTE_ISIS
;
1175 else if (strmatch(s
, "bgp"))
1176 return ZEBRA_ROUTE_BGP
;
1177 else if (strmatch(s
, "table"))
1178 return ZEBRA_ROUTE_TABLE
;
1179 else if (strmatch(s
, "vnc"))
1180 return ZEBRA_ROUTE_VNC
;
1181 else if (strmatch(s
, "vnc-direct"))
1182 return ZEBRA_ROUTE_VNC_DIRECT
;
1183 else if (strmatch(s
, "nhrp"))
1184 return ZEBRA_ROUTE_NHRP
;
1185 else if (strmatch(s
, "babel"))
1186 return ZEBRA_ROUTE_BABEL
;
1187 else if (strmatch(s
, "sharp"))
1188 return ZEBRA_ROUTE_SHARP
;
1189 else if (strmatch(s
, "openfabric"))
1190 return ZEBRA_ROUTE_OPENFABRIC
;
1195 void zlog_hexdump(const void *mem
, unsigned int len
)
1197 unsigned long i
= 0;
1199 unsigned int columns
= 8;
1201 * 19 bytes for 0xADDRESS:
1202 * 24 bytes for data; 2 chars plus a space per data byte
1204 * 8 bytes for ASCII representation
1205 * 1 byte for a newline
1206 * =====================
1207 * 53 bytes per 8 bytes of data
1208 * 1 byte for null term
1210 size_t bs
= ((len
/ 8) + 1) * 53 + 1;
1214 memset(buf
, 0, sizeof(buf
));
1216 for (i
= 0; i
< len
+ ((len
% columns
) ? (columns
- len
% columns
) : 0);
1219 if (i
% columns
== 0)
1220 s
+= snprintf(s
, bs
- (s
- buf
),
1221 "0x%016lx: ", (unsigned long)mem
+ i
);
1223 /* print hex data */
1225 s
+= snprintf(s
, bs
- (s
- buf
), "%02x ",
1226 0xFF & ((const char *)mem
)[i
]);
1228 /* end of block, just aligning for ASCII dump */
1230 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1232 /* print ASCII dump */
1233 if (i
% columns
== (columns
- 1)) {
1234 for (j
= i
- (columns
- 1); j
<= i
; j
++) {
1235 /* end of block not really printing */
1237 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1238 else if (isprint((int)((const char *)mem
)[j
]))
1240 s
, bs
- (s
- buf
), "%c",
1241 0xFF & ((const char *)mem
)[j
]);
1242 else /* other char */
1243 s
+= snprintf(s
, bs
- (s
- buf
), ".");
1245 s
+= snprintf(s
, bs
- (s
- buf
), "\n");
1248 zlog_debug("\n%s", buf
);
1251 const char *zlog_sanitize(char *buf
, size_t bufsz
, const void *in
, size_t inlen
)
1253 const char *inbuf
= in
;
1254 char *pos
= buf
, *end
= buf
+ bufsz
;
1255 const char *iend
= inbuf
+ inlen
;
1257 memset(buf
, 0, bufsz
);
1258 for (; inbuf
< iend
; inbuf
++) {
1259 /* don't write partial escape sequence */
1264 snprintf(pos
, end
- pos
, "\\n");
1265 else if (*inbuf
== '\r')
1266 snprintf(pos
, end
- pos
, "\\r");
1267 else if (*inbuf
== '\t')
1268 snprintf(pos
, end
- pos
, "\\t");
1269 else if (*inbuf
< ' ' || *inbuf
== '"' || *inbuf
>= 127)
1270 snprintf(pos
, end
- pos
, "\\x%02hhx", *inbuf
);