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
34 /* for printstack on solaris */
35 #ifdef HAVE_UCONTEXT_H
39 DEFINE_MTYPE_STATIC(LIB
, ZLOG
, "Logging")
41 static int logfile_fd
= -1; /* Used in signal handler. */
43 struct zlog
*zlog_default
= NULL
;
44 bool zlog_startup_stderr
= true;
46 /* lock protecting zlog_default for mt-safe zlog */
47 pthread_mutex_t loglock
= PTHREAD_MUTEX_INITIALIZER
;
49 const char *zlog_priority
[] = {
50 "emergencies", "alerts", "critical", "errors", "warnings",
51 "notifications", "informational", "debugging", NULL
,
57 * glibc has declared that the return value from write *must* not be
59 * gcc see's this problem and issues a warning for the line.
61 * Why is this a big deal you say? Because both of them are right
62 * and if you have -Werror enabled then all calls to write
63 * generate a build error and the build stops.
65 * clang has helpfully allowed this construct:
67 * to tell the compiler yeah I know it has a return value
68 * I don't care about it at this time.
69 * gcc doesn't have this ability.
71 * This code was written such that it didn't care about the
72 * return value from write. At this time do I want
73 * to go through and fix and test this code for correctness.
74 * So just wrapper the bad behavior and move on.
76 static void write_wrapper(int fd
, const void *buf
, size_t count
)
78 if (write(fd
, buf
, count
) <= 0)
85 * Looks up a message in a message list by key.
87 * If the message is not found, returns the provided error message.
89 * Terminates when it hits a struct message that's all zeros.
91 * @param mz the message list
92 * @param kz the message key
93 * @param nf the message to return if not found
96 const char *lookup_msg(const struct message
*mz
, int kz
, const char *nf
)
98 static struct message nt
= {0};
99 const char *rz
= nf
? nf
: "(no message found)";
100 const struct message
*pnt
;
101 for (pnt
= mz
; memcmp(pnt
, &nt
, sizeof(struct message
)); pnt
++)
102 if (pnt
->key
== kz
) {
103 rz
= pnt
->str
? pnt
->str
: rz
;
109 /* For time string format. */
110 size_t quagga_timestamp(int timestamp_precision
, char *buf
, size_t buflen
)
117 struct timeval clock
;
119 gettimeofday(&clock
, NULL
);
121 /* first, we update the cache if the time has changed */
122 if (cache
.last
!= clock
.tv_sec
) {
124 cache
.last
= clock
.tv_sec
;
125 tm
= localtime(&cache
.last
);
126 cache
.len
= strftime(cache
.buf
, sizeof(cache
.buf
),
127 "%Y/%m/%d %H:%M:%S", tm
);
129 /* note: it's not worth caching the subsecond part, because
130 chances are that back-to-back calls are not sufficiently close
132 for the clock not to have ticked forward */
134 if (buflen
> cache
.len
) {
135 memcpy(buf
, cache
.buf
, cache
.len
);
136 if ((timestamp_precision
> 0)
137 && (buflen
> cache
.len
+ 1 + timestamp_precision
)) {
138 /* should we worry about locale issues? */
139 static const int divisor
[] = {0, 100000, 10000, 1000,
142 char *p
= buf
+ cache
.len
+ 1
143 + (prec
= timestamp_precision
);
146 /* this is unlikely to happen, but protect anyway */
151 clock
.tv_usec
/= divisor
[prec
];
153 *p
-- = '0' + (clock
.tv_usec
% 10);
155 } while (--prec
> 0);
157 return cache
.len
+ 1 + timestamp_precision
;
159 buf
[cache
.len
] = '\0';
167 /* Utility routine for current time printing. */
168 static void time_print(FILE *fp
, struct timestamp_control
*ctl
)
170 if (!ctl
->already_rendered
) {
171 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
,
173 ctl
->already_rendered
= 1;
175 fprintf(fp
, "%s ", ctl
->buf
);
179 static void vzlog_file(struct zlog
*zl
, struct timestamp_control
*tsctl
,
180 const char *proto_str
, int record_priority
, int priority
,
181 FILE *fp
, const char *format
, va_list args
)
185 time_print(fp
, tsctl
);
187 fprintf(fp
, "%s: ", zlog_priority
[priority
]);
189 fprintf(fp
, "%s", proto_str
);
191 vfprintf(fp
, format
, ac
);
197 /* va_list version of zlog. */
198 void vzlog(int priority
, const char *format
, va_list args
)
200 pthread_mutex_lock(&loglock
);
203 int original_errno
= errno
;
204 struct timestamp_control tsctl
;
205 tsctl
.already_rendered
= 0;
206 struct zlog
*zl
= zlog_default
;
208 /* When zlog_default is also NULL, use stderr for logging. */
211 time_print(stderr
, &tsctl
);
212 fprintf(stderr
, "%s: ", "unknown");
213 vfprintf(stderr
, format
, args
);
214 fprintf(stderr
, "\n");
217 /* In this case we return at here. */
218 errno
= original_errno
;
219 pthread_mutex_unlock(&loglock
);
222 tsctl
.precision
= zl
->timestamp_precision
;
225 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
]) {
228 vsyslog(priority
| zlog_default
->facility
, format
, ac
);
233 sprintf(proto_str
, "%s[%d]: ", zl
->protoname
, zl
->instance
);
235 sprintf(proto_str
, "%s: ", zl
->protoname
);
238 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
239 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
240 zl
->fp
, format
, args
);
242 /* fixed-config logging to stderr while we're stating up & haven't
243 * daemonized / reached mainloop yet
245 * note the "else" on stdout output -- we don't want to print the same
246 * message to both stderr and stdout. */
247 if (zlog_startup_stderr
&& priority
<= LOG_WARNING
)
248 vzlog_file(zl
, &tsctl
, proto_str
, 1, priority
, stderr
, format
,
250 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
251 vzlog_file(zl
, &tsctl
, proto_str
, zl
->record_priority
, priority
,
252 stdout
, format
, args
);
254 /* Terminal monitor. */
255 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
256 vty_log((zl
->record_priority
? zlog_priority
[priority
] : NULL
),
257 proto_str
, format
, &tsctl
, args
);
259 errno
= original_errno
;
260 pthread_mutex_unlock(&loglock
);
263 int vzlog_test(int priority
)
265 pthread_mutex_lock(&loglock
);
269 struct zlog
*zl
= zlog_default
;
271 /* When zlog_default is also NULL, use stderr for logging. */
275 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
278 else if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
281 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
283 /* Terminal monitor. */
284 else if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
287 pthread_mutex_unlock(&loglock
);
292 static char *str_append(char *dst
, int len
, const char *src
)
294 while ((len
-- > 0) && *src
)
299 static char *num_append(char *s
, int len
, unsigned long x
)
305 return str_append(s
, len
, "0");
306 *(t
= &buf
[sizeof(buf
) - 1]) = '\0';
307 while (x
&& (t
> buf
)) {
308 *--t
= '0' + (x
% 10);
311 return str_append(s
, len
, t
);
314 #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
315 static char *hex_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 unsigned int cc
= (x
% 16);
325 *--t
= ((cc
< 10) ? ('0' + cc
) : ('a' + cc
- 10));
328 return str_append(s
, len
, t
);
332 /* Needs to be enhanced to support Solaris. */
333 static int syslog_connect(void)
340 struct sockaddr_un addr
;
342 if ((fd
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) < 0)
344 addr
.sun_family
= AF_UNIX
;
346 #define SYSLOG_SOCKET_PATH _PATH_LOG
348 #define SYSLOG_SOCKET_PATH "/dev/log"
350 s
= str_append(addr
.sun_path
, sizeof(addr
.sun_path
),
352 #undef SYSLOG_SOCKET_PATH
354 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
362 static void syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
364 static int syslog_fd
= -1;
365 char buf
[sizeof("<1234567890>ripngd[1234567890]: ") + msglen
+ 50];
368 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
371 #define LOC s,buf+sizeof(buf)-s
373 s
= str_append(LOC
, "<");
374 s
= num_append(LOC
, priority
);
375 s
= str_append(LOC
, ">");
376 /* forget about the timestamp, too difficult in a signal handler */
377 s
= str_append(LOC
, zlog_default
->ident
);
378 if (zlog_default
->syslog_options
& LOG_PID
) {
379 s
= str_append(LOC
, "[");
380 s
= num_append(LOC
, getpid());
381 s
= str_append(LOC
, "]");
383 s
= str_append(LOC
, ": ");
384 s
= str_append(LOC
, msg
);
385 write_wrapper(syslog_fd
, buf
, s
- buf
);
389 static int open_crashlog(void)
391 #define CRASHLOG_PREFIX "/var/tmp/quagga."
392 #define CRASHLOG_SUFFIX "crashlog"
393 if (zlog_default
&& zlog_default
->ident
) {
394 /* Avoid strlen since it is not async-signal-safe. */
398 for (p
= zlog_default
->ident
, ilen
= 0; *p
; p
++)
401 char buf
[sizeof(CRASHLOG_PREFIX
) + ilen
402 + sizeof(CRASHLOG_SUFFIX
) + 3];
404 #define LOC s,buf+sizeof(buf)-s
405 s
= str_append(LOC
, CRASHLOG_PREFIX
);
406 s
= str_append(LOC
, zlog_default
->ident
);
407 s
= str_append(LOC
, ".");
408 s
= str_append(LOC
, CRASHLOG_SUFFIX
);
411 return open(buf
, O_WRONLY
| O_CREAT
| O_EXCL
,
415 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX
,
416 O_WRONLY
| O_CREAT
| O_EXCL
, LOGFILE_MASK
);
417 #undef CRASHLOG_SUFFIX
418 #undef CRASHLOG_PREFIX
421 /* Note: the goal here is to use only async-signal-safe functions. */
422 void zlog_signal(int signo
, const char *action
425 siginfo_t
*siginfo
, void *program_counter
430 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")
433 char *msgstart
= buf
;
434 #define LOC s,buf+sizeof(buf)-s
438 s
= str_append(LOC
, zlog_default
->protoname
);
443 s
= str_append(LOC
, "Received signal ");
444 s
= num_append(LOC
, signo
);
445 s
= str_append(LOC
, " at ");
446 s
= num_append(LOC
, now
);
448 s
= str_append(LOC
, " (si_addr 0x");
449 s
= hex_append(LOC
, (unsigned long)(siginfo
->si_addr
));
450 if (program_counter
) {
451 s
= str_append(LOC
, ", PC 0x");
452 s
= hex_append(LOC
, (unsigned long)program_counter
);
454 s
= str_append(LOC
, "); ");
455 #else /* SA_SIGINFO */
456 s
= str_append(LOC
, "; ");
457 #endif /* SA_SIGINFO */
458 s
= str_append(LOC
, action
);
459 if (s
< buf
+ sizeof(buf
))
462 /* N.B. implicit priority is most severe */
465 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
466 /* If no file logging configured, try to write to fallback log file. */
467 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
472 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
474 /* Remove trailing '\n' for monitor and syslog */
476 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
477 vty_log_fixed(buf
, s
- buf
);
478 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
479 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
484 zlog_backtrace_sigsafe(PRI
,
494 tc
= pthread_getspecific(thread_current
);
496 s
= str_append(LOC
, "no thread information available\n");
498 s
= str_append(LOC
, "in thread ");
499 s
= str_append(LOC
, tc
->funcname
);
500 s
= str_append(LOC
, " scheduled from ");
501 s
= str_append(LOC
, tc
->schedfrom
);
502 s
= str_append(LOC
, ":");
503 s
= num_append(LOC
, tc
->schedfrom_line
);
504 s
= str_append(LOC
, "\n");
507 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
508 /* If no file logging configured, try to write to fallback log file. */
514 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
516 /* Remove trailing '\n' for monitor and syslog */
518 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
519 vty_log_fixed(buf
, s
- buf
);
520 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
521 syslog_sigsafe(PRI
| zlog_default
->facility
, msgstart
,
530 /* Log a backtrace using only async-signal-safe functions.
531 Needs to be enhanced to support syslog logging. */
532 void zlog_backtrace_sigsafe(int priority
, void *program_counter
)
534 #ifdef HAVE_STACK_TRACE
535 static const char pclabel
[] = "Program counter: ";
539 char *s
, **bt
= NULL
;
540 #define LOC s,buf+sizeof(buf)-s
542 #ifdef HAVE_GLIBC_BACKTRACE
543 size
= backtrace(array
, array_size(array
));
544 if (size
<= 0 || (size_t)size
> array_size(array
))
549 if (program_counter) { \
550 write_wrapper(FD, pclabel, sizeof(pclabel) - 1); \
551 backtrace_symbols_fd(&program_counter, 1, FD); \
553 write_wrapper(FD, buf, s - buf); \
554 backtrace_symbols_fd(array, size, FD); \
556 #elif defined(HAVE_PRINTSTACK)
559 if (program_counter) \
560 write_wrapper((FD), pclabel, sizeof(pclabel) - 1); \
561 write_wrapper((FD), buf, s - buf); \
564 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
567 s
= str_append(LOC
, "Backtrace for ");
568 s
= num_append(LOC
, size
);
569 s
= str_append(LOC
, " stack frames:\n");
571 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
576 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
578 /* Remove trailing '\n' for monitor and syslog */
580 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
581 vty_log_fixed(buf
, s
- buf
);
582 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
583 syslog_sigsafe(priority
| zlog_default
->facility
, buf
,
587 #ifdef HAVE_GLIBC_BACKTRACE
588 bt
= backtrace_symbols(array
, size
);
590 /* Just print the function addresses. */
591 for (i
= 0; i
< size
; i
++) {
594 s
= str_append(LOC
, bt
[i
]);
596 s
= str_append(LOC
, "[bt ");
597 s
= num_append(LOC
, i
);
598 s
= str_append(LOC
, "] 0x");
600 LOC
, (unsigned long)(array
[i
]));
604 <= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
605 vty_log_fixed(buf
, s
- buf
);
607 <= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
620 #endif /* HAVE_STRACK_TRACE */
623 void zlog_backtrace(int priority
)
625 #ifndef HAVE_GLIBC_BACKTRACE
626 zlog(priority
, "No backtrace available on this platform.");
632 size
= backtrace(array
, array_size(array
));
633 if (size
<= 0 || (size_t)size
> array_size(array
)) {
635 "Cannot get backtrace, returned invalid # of frames %d "
636 "(valid range is between 1 and %lu)",
637 size
, (unsigned long)(array_size(array
)));
640 zlog(priority
, "Backtrace for %d stack frames:", size
);
641 if (!(strings
= backtrace_symbols(array
, size
))) {
642 zlog_err("Cannot get backtrace symbols (out of memory?)");
643 for (i
= 0; i
< size
; i
++)
644 zlog(priority
, "[bt %d] %p", i
, array
[i
]);
646 for (i
= 0; i
< size
; i
++)
647 zlog(priority
, "[bt %d] %s", i
, strings
[i
]);
650 #endif /* HAVE_GLIBC_BACKTRACE */
653 void zlog(int priority
, const char *format
, ...)
657 va_start(args
, format
);
658 vzlog(priority
, format
, args
);
662 #define ZLOG_FUNC(FUNCNAME, PRIORITY) \
663 void FUNCNAME(const char *format, ...) \
666 va_start(args, format); \
667 vzlog(PRIORITY, format, args); \
671 ZLOG_FUNC(zlog_err
, LOG_ERR
)
673 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
675 ZLOG_FUNC(zlog_info
, LOG_INFO
)
677 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
679 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
683 void zlog_thread_info(int log_level
)
686 tc
= pthread_getspecific(thread_current
);
690 "Current thread function %s, scheduled from "
692 tc
->funcname
, tc
->schedfrom
, tc
->schedfrom_line
);
694 zlog(log_level
, "Current thread not known/applicable");
697 void _zlog_assert_failed(const char *assertion
, const char *file
,
698 unsigned int line
, const char *function
)
700 /* Force fallback file logging? */
701 if (zlog_default
&& !zlog_default
->fp
702 && ((logfile_fd
= open_crashlog()) >= 0)
703 && ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
704 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
705 zlog(LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
706 assertion
, file
, line
, (function
? function
: "?"));
707 zlog_backtrace(LOG_CRIT
);
708 zlog_thread_info(LOG_CRIT
);
709 log_memstats(stderr
, "log");
713 void memory_oom(size_t size
, const char *name
)
716 "out of memory: failed to allocate %zu bytes for %s"
719 zlog_backtrace(LOG_ERR
);
723 /* Open log stream */
724 void openzlog(const char *progname
, const char *protoname
,
725 unsigned short instance
, int syslog_flags
, int syslog_facility
)
730 zl
= XCALLOC(MTYPE_ZLOG
, sizeof(struct zlog
));
732 zl
->ident
= progname
;
733 zl
->protoname
= protoname
;
734 zl
->instance
= instance
;
735 zl
->facility
= syslog_facility
;
736 zl
->syslog_options
= syslog_flags
;
738 /* Set default logging levels. */
739 for (i
= 0; i
< array_size(zl
->maxlvl
); i
++)
740 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
741 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
742 zl
->default_lvl
= LOG_DEBUG
;
744 openlog(progname
, syslog_flags
, zl
->facility
);
746 pthread_mutex_lock(&loglock
);
748 pthread_mutex_unlock(&loglock
);
750 #ifdef HAVE_GLIBC_BACKTRACE
751 /* work around backtrace() using lazily resolved dynamically linked
752 * symbols, which will otherwise cause funny breakage in the SEGV
754 * (particularly, the dynamic linker can call malloc(), which uses locks
755 * in programs linked with -pthread, thus can deadlock.) */
757 backtrace(bt
, array_size(bt
));
758 free(backtrace_symbols(bt
, 0));
759 backtrace_symbols_fd(bt
, 0, 0);
765 pthread_mutex_lock(&loglock
);
766 struct zlog
*zl
= zlog_default
;
773 if (zl
->filename
!= NULL
)
774 XFREE(MTYPE_ZLOG
, zl
->filename
);
776 XFREE(MTYPE_ZLOG
, zl
);
778 pthread_mutex_unlock(&loglock
);
781 /* Called from command.c. */
782 void zlog_set_level(zlog_dest_t dest
, int log_level
)
784 pthread_mutex_lock(&loglock
);
785 zlog_default
->maxlvl
[dest
] = log_level
;
786 pthread_mutex_unlock(&loglock
);
789 int zlog_set_file(const char *filename
, int log_level
)
796 /* There is opend file. */
800 oldumask
= umask(0777 & ~LOGFILE_MASK
);
801 fp
= fopen(filename
, "a");
806 pthread_mutex_lock(&loglock
);
810 zl
->filename
= XSTRDUP(MTYPE_ZLOG
, filename
);
811 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
813 logfile_fd
= fileno(fp
);
814 pthread_mutex_unlock(&loglock
);
820 /* Reset opend file. */
821 int zlog_reset_file(void)
823 pthread_mutex_lock(&loglock
);
825 struct zlog
*zl
= zlog_default
;
831 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
834 XFREE(MTYPE_ZLOG
, zl
->filename
);
837 pthread_mutex_unlock(&loglock
);
842 /* Reopen log file. */
843 int zlog_rotate(void)
845 pthread_mutex_lock(&loglock
);
847 struct zlog
*zl
= zlog_default
;
855 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
856 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
862 oldumask
= umask(0777 & ~LOGFILE_MASK
);
863 zl
->fp
= fopen(zl
->filename
, "a");
866 if (zl
->fp
== NULL
) {
868 "Log rotate failed: cannot open file %s for append: %s",
869 zl
->filename
, safe_strerror(save_errno
));
872 logfile_fd
= fileno(zl
->fp
);
873 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
877 pthread_mutex_unlock(&loglock
);
882 /* Wrapper around strerror to handle case where it returns NULL. */
883 const char *safe_strerror(int errnum
)
885 const char *s
= strerror(errnum
);
886 return (s
!= NULL
) ? s
: "Unknown error";
889 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
890 static const struct zebra_desc_table command_types
[] = {
891 DESC_ENTRY(ZEBRA_INTERFACE_ADD
),
892 DESC_ENTRY(ZEBRA_INTERFACE_DELETE
),
893 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_ADD
),
894 DESC_ENTRY(ZEBRA_INTERFACE_ADDRESS_DELETE
),
895 DESC_ENTRY(ZEBRA_INTERFACE_UP
),
896 DESC_ENTRY(ZEBRA_INTERFACE_DOWN
),
897 DESC_ENTRY(ZEBRA_INTERFACE_SET_MASTER
),
898 DESC_ENTRY(ZEBRA_ROUTE_ADD
),
899 DESC_ENTRY(ZEBRA_ROUTE_DELETE
),
900 DESC_ENTRY(ZEBRA_ROUTE_NOTIFY_OWNER
),
901 DESC_ENTRY(ZEBRA_IPV4_ROUTE_ADD
),
902 DESC_ENTRY(ZEBRA_IPV4_ROUTE_DELETE
),
903 DESC_ENTRY(ZEBRA_IPV6_ROUTE_ADD
),
904 DESC_ENTRY(ZEBRA_IPV6_ROUTE_DELETE
),
905 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ADD
),
906 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DELETE
),
907 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
908 DESC_ENTRY(ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
909 DESC_ENTRY(ZEBRA_ROUTER_ID_ADD
),
910 DESC_ENTRY(ZEBRA_ROUTER_ID_DELETE
),
911 DESC_ENTRY(ZEBRA_ROUTER_ID_UPDATE
),
912 DESC_ENTRY(ZEBRA_HELLO
),
913 DESC_ENTRY(ZEBRA_NEXTHOP_REGISTER
),
914 DESC_ENTRY(ZEBRA_NEXTHOP_UNREGISTER
),
915 DESC_ENTRY(ZEBRA_NEXTHOP_UPDATE
),
916 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_ADD
),
917 DESC_ENTRY(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE
),
918 DESC_ENTRY(ZEBRA_INTERFACE_BFD_DEST_UPDATE
),
919 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_REGISTER
),
920 DESC_ENTRY(ZEBRA_IMPORT_ROUTE_UNREGISTER
),
921 DESC_ENTRY(ZEBRA_IMPORT_CHECK_UPDATE
),
922 DESC_ENTRY(ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD
),
923 DESC_ENTRY(ZEBRA_BFD_DEST_REGISTER
),
924 DESC_ENTRY(ZEBRA_BFD_DEST_DEREGISTER
),
925 DESC_ENTRY(ZEBRA_BFD_DEST_UPDATE
),
926 DESC_ENTRY(ZEBRA_BFD_DEST_REPLAY
),
927 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_ADD
),
928 DESC_ENTRY(ZEBRA_REDISTRIBUTE_ROUTE_DEL
),
929 DESC_ENTRY(ZEBRA_VRF_UNREGISTER
),
930 DESC_ENTRY(ZEBRA_VRF_ADD
),
931 DESC_ENTRY(ZEBRA_VRF_DELETE
),
932 DESC_ENTRY(ZEBRA_VRF_LABEL
),
933 DESC_ENTRY(ZEBRA_INTERFACE_VRF_UPDATE
),
934 DESC_ENTRY(ZEBRA_BFD_CLIENT_REGISTER
),
935 DESC_ENTRY(ZEBRA_INTERFACE_ENABLE_RADV
),
936 DESC_ENTRY(ZEBRA_INTERFACE_DISABLE_RADV
),
937 DESC_ENTRY(ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB
),
938 DESC_ENTRY(ZEBRA_INTERFACE_LINK_PARAMS
),
939 DESC_ENTRY(ZEBRA_MPLS_LABELS_ADD
),
940 DESC_ENTRY(ZEBRA_MPLS_LABELS_DELETE
),
941 DESC_ENTRY(ZEBRA_IPMR_ROUTE_STATS
),
942 DESC_ENTRY(ZEBRA_LABEL_MANAGER_CONNECT
),
943 DESC_ENTRY(ZEBRA_GET_LABEL_CHUNK
),
944 DESC_ENTRY(ZEBRA_RELEASE_LABEL_CHUNK
),
945 DESC_ENTRY(ZEBRA_ADVERTISE_ALL_VNI
),
946 DESC_ENTRY(ZEBRA_ADVERTISE_DEFAULT_GW
),
947 DESC_ENTRY(ZEBRA_ADVERTISE_SUBNET
),
948 DESC_ENTRY(ZEBRA_VNI_ADD
),
949 DESC_ENTRY(ZEBRA_VNI_DEL
),
950 DESC_ENTRY(ZEBRA_L3VNI_ADD
),
951 DESC_ENTRY(ZEBRA_L3VNI_DEL
),
952 DESC_ENTRY(ZEBRA_REMOTE_VTEP_ADD
),
953 DESC_ENTRY(ZEBRA_REMOTE_VTEP_DEL
),
954 DESC_ENTRY(ZEBRA_MACIP_ADD
),
955 DESC_ENTRY(ZEBRA_MACIP_DEL
),
956 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_ADD
),
957 DESC_ENTRY(ZEBRA_IP_PREFIX_ROUTE_DEL
),
958 DESC_ENTRY(ZEBRA_REMOTE_MACIP_ADD
),
959 DESC_ENTRY(ZEBRA_REMOTE_MACIP_DEL
),
960 DESC_ENTRY(ZEBRA_PW_ADD
),
961 DESC_ENTRY(ZEBRA_PW_DELETE
),
962 DESC_ENTRY(ZEBRA_PW_SET
),
963 DESC_ENTRY(ZEBRA_PW_UNSET
),
964 DESC_ENTRY(ZEBRA_PW_STATUS_UPDATE
),
965 DESC_ENTRY(ZEBRA_RULE_ADD
),
966 DESC_ENTRY(ZEBRA_RULE_DELETE
),
967 DESC_ENTRY(ZEBRA_RULE_NOTIFY_OWNER
),
968 DESC_ENTRY(ZEBRA_TABLE_MANAGER_CONNECT
),
969 DESC_ENTRY(ZEBRA_GET_TABLE_CHUNK
),
970 DESC_ENTRY(ZEBRA_RELEASE_TABLE_CHUNK
),
974 static const struct zebra_desc_table unknown
= {0, "unknown", '?'};
976 static const struct zebra_desc_table
*zroute_lookup(unsigned int zroute
)
980 if (zroute
>= array_size(route_types
)) {
981 zlog_err("unknown zebra route type: %u", zroute
);
984 if (zroute
== route_types
[zroute
].type
)
985 return &route_types
[zroute
];
986 for (i
= 0; i
< array_size(route_types
); i
++) {
987 if (zroute
== route_types
[i
].type
) {
989 "internal error: route type table out of order "
990 "while searching for %u, please notify developers",
992 return &route_types
[i
];
995 zlog_err("internal error: cannot find route type %u in table!", zroute
);
999 const char *zebra_route_string(unsigned int zroute
)
1001 return zroute_lookup(zroute
)->string
;
1004 char zebra_route_char(unsigned int zroute
)
1006 return zroute_lookup(zroute
)->chr
;
1009 const char *zserv_command_string(unsigned int command
)
1011 if (command
>= array_size(command_types
)) {
1012 zlog_err("unknown zserv command type: %u", command
);
1013 return unknown
.string
;
1015 return command_types
[command
].string
;
1018 int proto_name2num(const char *s
)
1022 for (i
= 0; i
< array_size(route_types
); ++i
)
1023 if (strcasecmp(s
, route_types
[i
].string
) == 0)
1024 return route_types
[i
].type
;
1028 int proto_redistnum(int afi
, const char *s
)
1033 if (afi
== AFI_IP
) {
1034 if (strmatch(s
, "kernel"))
1035 return ZEBRA_ROUTE_KERNEL
;
1036 else if (strmatch(s
, "connected"))
1037 return ZEBRA_ROUTE_CONNECT
;
1038 else if (strmatch(s
, "static"))
1039 return ZEBRA_ROUTE_STATIC
;
1040 else if (strmatch(s
, "rip"))
1041 return ZEBRA_ROUTE_RIP
;
1042 else if (strmatch(s
, "eigrp"))
1043 return ZEBRA_ROUTE_EIGRP
;
1044 else if (strmatch(s
, "ospf"))
1045 return ZEBRA_ROUTE_OSPF
;
1046 else if (strmatch(s
, "isis"))
1047 return ZEBRA_ROUTE_ISIS
;
1048 else if (strmatch(s
, "bgp"))
1049 return ZEBRA_ROUTE_BGP
;
1050 else if (strmatch(s
, "table"))
1051 return ZEBRA_ROUTE_TABLE
;
1052 else if (strmatch(s
, "vnc"))
1053 return ZEBRA_ROUTE_VNC
;
1054 else if (strmatch(s
, "vnc-direct"))
1055 return ZEBRA_ROUTE_VNC_DIRECT
;
1056 else if (strmatch(s
, "nhrp"))
1057 return ZEBRA_ROUTE_NHRP
;
1058 else if (strmatch(s
, "babel"))
1059 return ZEBRA_ROUTE_BABEL
;
1060 else if (strmatch(s
, "sharp"))
1061 return ZEBRA_ROUTE_SHARP
;
1063 if (afi
== AFI_IP6
) {
1064 if (strmatch(s
, "kernel"))
1065 return ZEBRA_ROUTE_KERNEL
;
1066 else if (strmatch(s
, "connected"))
1067 return ZEBRA_ROUTE_CONNECT
;
1068 else if (strmatch(s
, "static"))
1069 return ZEBRA_ROUTE_STATIC
;
1070 else if (strmatch(s
, "ripng"))
1071 return ZEBRA_ROUTE_RIPNG
;
1072 else if (strmatch(s
, "ospf6"))
1073 return ZEBRA_ROUTE_OSPF6
;
1074 else if (strmatch(s
, "isis"))
1075 return ZEBRA_ROUTE_ISIS
;
1076 else if (strmatch(s
, "bgp"))
1077 return ZEBRA_ROUTE_BGP
;
1078 else if (strmatch(s
, "table"))
1079 return ZEBRA_ROUTE_TABLE
;
1080 else if (strmatch(s
, "vnc"))
1081 return ZEBRA_ROUTE_VNC
;
1082 else if (strmatch(s
, "vnc-direct"))
1083 return ZEBRA_ROUTE_VNC_DIRECT
;
1084 else if (strmatch(s
, "nhrp"))
1085 return ZEBRA_ROUTE_NHRP
;
1086 else if (strmatch(s
, "babel"))
1087 return ZEBRA_ROUTE_BABEL
;
1088 else if (strmatch(s
, "sharp"))
1089 return ZEBRA_ROUTE_SHARP
;
1094 void zlog_hexdump(const void *mem
, unsigned int len
)
1096 unsigned long i
= 0;
1098 unsigned int columns
= 8;
1100 * 19 bytes for 0xADDRESS:
1101 * 24 bytes for data; 2 chars plus a space per data byte
1103 * 8 bytes for ASCII representation
1104 * 1 byte for a newline
1105 * =====================
1106 * 53 bytes per 8 bytes of data
1107 * 1 byte for null term
1109 size_t bs
= ((len
/ 8) + 1) * 53 + 1;
1113 memset(buf
, 0, sizeof(buf
));
1115 for (i
= 0; i
< len
+ ((len
% columns
) ? (columns
- len
% columns
) : 0);
1118 if (i
% columns
== 0)
1119 s
+= snprintf(s
, bs
- (s
- buf
),
1120 "0x%016lx: ", (unsigned long)mem
+ i
);
1122 /* print hex data */
1124 s
+= snprintf(s
, bs
- (s
- buf
), "%02x ",
1125 0xFF & ((const char *)mem
)[i
]);
1127 /* end of block, just aligning for ASCII dump */
1129 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1131 /* print ASCII dump */
1132 if (i
% columns
== (columns
- 1)) {
1133 for (j
= i
- (columns
- 1); j
<= i
; j
++) {
1134 /* end of block not really printing */
1136 s
+= snprintf(s
, bs
- (s
- buf
), " ");
1137 else if (isprint((int)((const char *)mem
)[j
]))
1139 s
, bs
- (s
- buf
), "%c",
1140 0xFF & ((const char *)mem
)[j
]);
1141 else /* other char */
1142 s
+= snprintf(s
, bs
- (s
- buf
), ".");
1144 s
+= snprintf(s
, bs
- (s
- buf
), "\n");
1147 zlog_debug("\n%s", buf
);
1150 const char *zlog_sanitize(char *buf
, size_t bufsz
, const void *in
, size_t inlen
)
1152 const char *inbuf
= in
;
1153 char *pos
= buf
, *end
= buf
+ bufsz
;
1154 const char *iend
= inbuf
+ inlen
;
1156 memset(buf
, 0, bufsz
);
1157 for (; inbuf
< iend
; inbuf
++) {
1158 /* don't write partial escape sequence */
1163 snprintf(pos
, end
- pos
, "\\n");
1164 else if (*inbuf
== '\r')
1165 snprintf(pos
, end
- pos
, "\\r");
1166 else if (*inbuf
== '\t')
1167 snprintf(pos
, end
- pos
, "\\t");
1168 else if (*inbuf
< ' ' || *inbuf
== '"' || *inbuf
>= 127)
1169 snprintf(pos
, end
- pos
, "\\x%02hhx", *inbuf
);