blob: 10a771e1ea4cb3221e9b33eaf84846cf2b7e3a3b [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
ajs274a4a42004-12-07 15:39:31 +00002 * Logging of zebra
paul718e3742002-12-13 20:15:29 +00003 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
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
10 * later version.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
David Lampartere0ca5fd2009-09-16 01:52:42 +020023#define QUAGGA_DEFINE_DESC_TABLE
24
paul718e3742002-12-13 20:15:29 +000025#include <zebra.h>
26
27#include "log.h"
28#include "memory.h"
29#include "command.h"
ajs7d149b82004-11-28 23:00:01 +000030#ifndef SUNOS_5
31#include <sys/un.h>
32#endif
Paul Jakma30a22312008-08-15 14:05:22 +010033/* for printstack on solaris */
34#ifdef HAVE_UCONTEXT_H
35#include <ucontext.h>
36#endif
paul718e3742002-12-13 20:15:29 +000037
ajsc4c7d0c2005-02-03 19:22:05 +000038static int logfile_fd = -1; /* Used in signal handler. */
ajs1e221352005-02-03 16:42:40 +000039
paul718e3742002-12-13 20:15:29 +000040struct zlog *zlog_default = NULL;
41
42const char *zlog_proto_names[] =
43{
44 "NONE",
45 "DEFAULT",
46 "ZEBRA",
47 "RIP",
48 "BGP",
49 "OSPF",
50 "RIPNG",
Paul Jakma57345092011-12-25 17:52:09 +010051 "BABEL",
paul718e3742002-12-13 20:15:29 +000052 "OSPF6",
jardin9e867fe2003-12-23 08:56:18 +000053 "ISIS",
Everton Marques871dbcf2009-08-11 15:43:05 -030054 "PIM",
paul718e3742002-12-13 20:15:29 +000055 "MASC",
56 NULL,
57};
58
59const char *zlog_priority[] =
60{
61 "emergencies",
62 "alerts",
63 "critical",
64 "errors",
65 "warnings",
66 "notifications",
67 "informational",
68 "debugging",
69 NULL,
70};
71
72
David Lamparter6b0655a2014-06-04 06:53:35 +020073
paul718e3742002-12-13 20:15:29 +000074/* For time string format. */
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +000075
76size_t
77quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
78{
79 static struct {
80 time_t last;
81 size_t len;
82 char buf[28];
83 } cache;
84 struct timeval clock;
85
86 /* would it be sufficient to use global 'recent_time' here? I fear not... */
87 gettimeofday(&clock, NULL);
88
89 /* first, we update the cache if the time has changed */
90 if (cache.last != clock.tv_sec)
91 {
92 struct tm *tm;
93 cache.last = clock.tv_sec;
94 tm = localtime(&cache.last);
95 cache.len = strftime(cache.buf, sizeof(cache.buf),
96 "%Y/%m/%d %H:%M:%S", tm);
97 }
98 /* note: it's not worth caching the subsecond part, because
99 chances are that back-to-back calls are not sufficiently close together
100 for the clock not to have ticked forward */
101
102 if (buflen > cache.len)
103 {
104 memcpy(buf, cache.buf, cache.len);
105 if ((timestamp_precision > 0) &&
106 (buflen > cache.len+1+timestamp_precision))
107 {
108 /* should we worry about locale issues? */
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000109 static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1};
110 int prec;
111 char *p = buf+cache.len+1+(prec = timestamp_precision);
112 *p-- = '\0';
113 while (prec > 6)
114 /* this is unlikely to happen, but protect anyway */
115 {
116 *p-- = '0';
117 prec--;
118 }
119 clock.tv_usec /= divisor[prec];
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000120 do
121 {
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000122 *p-- = '0'+(clock.tv_usec % 10);
123 clock.tv_usec /= 10;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000124 }
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000125 while (--prec > 0);
126 *p = '.';
127 return cache.len+1+timestamp_precision;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000128 }
129 buf[cache.len] = '\0';
130 return cache.len;
131 }
132 if (buflen > 0)
133 buf[0] = '\0';
134 return 0;
135}
paul718e3742002-12-13 20:15:29 +0000136
137/* Utility routine for current time printing. */
138static void
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000139time_print(FILE *fp, struct timestamp_control *ctl)
paul718e3742002-12-13 20:15:29 +0000140{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000141 if (!ctl->already_rendered)
142 {
143 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
144 ctl->already_rendered = 1;
145 }
146 fprintf(fp, "%s ", ctl->buf);
paul718e3742002-12-13 20:15:29 +0000147}
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000148
David Lamparter6b0655a2014-06-04 06:53:35 +0200149
paul718e3742002-12-13 20:15:29 +0000150/* va_list version of zlog. */
ajsd246bd92004-11-23 17:35:08 +0000151static void
152vzlog (struct zlog *zl, int priority, const char *format, va_list args)
paul718e3742002-12-13 20:15:29 +0000153{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000154 struct timestamp_control tsctl;
155 tsctl.already_rendered = 0;
156
paul718e3742002-12-13 20:15:29 +0000157 /* If zlog is not specified, use default one. */
158 if (zl == NULL)
159 zl = zlog_default;
160
161 /* When zlog_default is also NULL, use stderr for logging. */
162 if (zl == NULL)
163 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000164 tsctl.precision = 0;
165 time_print(stderr, &tsctl);
paul718e3742002-12-13 20:15:29 +0000166 fprintf (stderr, "%s: ", "unknown");
ajsd246bd92004-11-23 17:35:08 +0000167 vfprintf (stderr, format, args);
paul718e3742002-12-13 20:15:29 +0000168 fprintf (stderr, "\n");
169 fflush (stderr);
170
171 /* In this case we return at here. */
172 return;
173 }
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000174 tsctl.precision = zl->timestamp_precision;
paul718e3742002-12-13 20:15:29 +0000175
paul718e3742002-12-13 20:15:29 +0000176 /* Syslog output */
ajs274a4a42004-12-07 15:39:31 +0000177 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
ajsd246bd92004-11-23 17:35:08 +0000178 {
179 va_list ac;
180 va_copy(ac, args);
181 vsyslog (priority|zlog_default->facility, format, ac);
182 va_end(ac);
183 }
paul718e3742002-12-13 20:15:29 +0000184
185 /* File output. */
ajs274a4a42004-12-07 15:39:31 +0000186 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
paul718e3742002-12-13 20:15:29 +0000187 {
ajsd246bd92004-11-23 17:35:08 +0000188 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000189 time_print (zl->fp, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000190 if (zl->record_priority)
191 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000192 fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000193 va_copy(ac, args);
194 vfprintf (zl->fp, format, ac);
195 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000196 fprintf (zl->fp, "\n");
197 fflush (zl->fp);
198 }
199
200 /* stdout output. */
ajs274a4a42004-12-07 15:39:31 +0000201 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
paul718e3742002-12-13 20:15:29 +0000202 {
ajsd246bd92004-11-23 17:35:08 +0000203 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000204 time_print (stdout, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000205 if (zl->record_priority)
206 fprintf (stdout, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000207 fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000208 va_copy(ac, args);
209 vfprintf (stdout, format, ac);
210 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000211 fprintf (stdout, "\n");
212 fflush (stdout);
213 }
214
paul718e3742002-12-13 20:15:29 +0000215 /* Terminal monitor. */
ajs274a4a42004-12-07 15:39:31 +0000216 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
217 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000218 zlog_proto_names[zl->protocol], format, &tsctl, args);
paul718e3742002-12-13 20:15:29 +0000219}
220
ajs59a06a92004-11-23 18:19:14 +0000221static char *
222str_append(char *dst, int len, const char *src)
223{
224 while ((len-- > 0) && *src)
225 *dst++ = *src++;
226 return dst;
227}
228
229static char *
230num_append(char *s, int len, u_long x)
231{
232 char buf[30];
ajs7d149b82004-11-28 23:00:01 +0000233 char *t;
ajs59a06a92004-11-23 18:19:14 +0000234
ajs7d149b82004-11-28 23:00:01 +0000235 if (!x)
236 return str_append(s,len,"0");
237 *(t = &buf[sizeof(buf)-1]) = '\0';
ajs59a06a92004-11-23 18:19:14 +0000238 while (x && (t > buf))
239 {
240 *--t = '0'+(x % 10);
241 x /= 10;
242 }
243 return str_append(s,len,t);
244}
245
Paul Jakmafb66b292006-05-28 08:26:15 +0000246#if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
ajs7d149b82004-11-28 23:00:01 +0000247static char *
248hex_append(char *s, int len, u_long x)
249{
250 char buf[30];
251 char *t;
252
253 if (!x)
254 return str_append(s,len,"0");
255 *(t = &buf[sizeof(buf)-1]) = '\0';
256 while (x && (t > buf))
257 {
258 u_int cc = (x % 16);
259 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
260 x /= 16;
261 }
262 return str_append(s,len,t);
263}
ajs31364272005-01-18 22:18:59 +0000264#endif
ajs7d149b82004-11-28 23:00:01 +0000265
ajs7d149b82004-11-28 23:00:01 +0000266/* Needs to be enhanced to support Solaris. */
267static int
268syslog_connect(void)
269{
270#ifdef SUNOS_5
271 return -1;
272#else
273 int fd;
274 char *s;
275 struct sockaddr_un addr;
276
277 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
278 return -1;
279 addr.sun_family = AF_UNIX;
280#ifdef _PATH_LOG
281#define SYSLOG_SOCKET_PATH _PATH_LOG
282#else
283#define SYSLOG_SOCKET_PATH "/dev/log"
284#endif
285 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
286#undef SYSLOG_SOCKET_PATH
287 *s = '\0';
288 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
289 {
290 close(fd);
291 return -1;
292 }
293 return fd;
294#endif
295}
296
297static void
298syslog_sigsafe(int priority, const char *msg, size_t msglen)
299{
ajs1e221352005-02-03 16:42:40 +0000300 static int syslog_fd = -1;
ajs7d149b82004-11-28 23:00:01 +0000301 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
302 char *s;
303
304 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
305 return;
306
307#define LOC s,buf+sizeof(buf)-s
308 s = buf;
309 s = str_append(LOC,"<");
310 s = num_append(LOC,priority);
311 s = str_append(LOC,">");
312 /* forget about the timestamp, too difficult in a signal handler */
313 s = str_append(LOC,zlog_default->ident);
314 if (zlog_default->syslog_options & LOG_PID)
315 {
316 s = str_append(LOC,"[");
317 s = num_append(LOC,getpid());
318 s = str_append(LOC,"]");
319 }
320 s = str_append(LOC,": ");
321 s = str_append(LOC,msg);
322 write(syslog_fd,buf,s-buf);
323#undef LOC
324}
325
ajs1e221352005-02-03 16:42:40 +0000326static int
327open_crashlog(void)
328{
329#define CRASHLOG_PREFIX "/var/tmp/quagga."
330#define CRASHLOG_SUFFIX "crashlog"
331 if (zlog_default && zlog_default->ident)
332 {
333 /* Avoid strlen since it is not async-signal-safe. */
334 const char *p;
335 size_t ilen;
336
337 for (p = zlog_default->ident, ilen = 0; *p; p++)
338 ilen++;
339 {
340 char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3];
341 char *s = buf;
342#define LOC s,buf+sizeof(buf)-s
343 s = str_append(LOC, CRASHLOG_PREFIX);
344 s = str_append(LOC, zlog_default->ident);
345 s = str_append(LOC, ".");
346 s = str_append(LOC, CRASHLOG_SUFFIX);
347#undef LOC
348 *s = '\0';
349 return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK);
350 }
351 }
352 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL,
353 LOGFILE_MASK);
354#undef CRASHLOG_SUFFIX
355#undef CRASHLOG_PREFIX
356}
357
ajs7d149b82004-11-28 23:00:01 +0000358/* Note: the goal here is to use only async-signal-safe functions. */
ajs59a06a92004-11-23 18:19:14 +0000359void
ajs31364272005-01-18 22:18:59 +0000360zlog_signal(int signo, const char *action
361#ifdef SA_SIGINFO
362 , siginfo_t *siginfo, void *program_counter
363#endif
364 )
ajs59a06a92004-11-23 18:19:14 +0000365{
366 time_t now;
ajs40abf232005-01-12 17:27:27 +0000367 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
ajs59a06a92004-11-23 18:19:14 +0000368 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000369 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000370#define LOC s,buf+sizeof(buf)-s
371
372 time(&now);
373 if (zlog_default)
374 {
375 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
376 *s++ = ':';
377 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000378 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000379 }
380 s = str_append(LOC,"Received signal ");
381 s = num_append(LOC,signo);
382 s = str_append(LOC," at ");
383 s = num_append(LOC,now);
ajs31364272005-01-18 22:18:59 +0000384#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +0000385 s = str_append(LOC," (si_addr 0x");
386 s = hex_append(LOC,(u_long)(siginfo->si_addr));
387 if (program_counter)
388 {
389 s = str_append(LOC,", PC 0x");
390 s = hex_append(LOC,(u_long)program_counter);
391 }
392 s = str_append(LOC,"); ");
ajs31364272005-01-18 22:18:59 +0000393#else /* SA_SIGINFO */
394 s = str_append(LOC,"; ");
395#endif /* SA_SIGINFO */
ajs59a06a92004-11-23 18:19:14 +0000396 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000397 if (s < buf+sizeof(buf))
398 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000399
ajs274a4a42004-12-07 15:39:31 +0000400 /* N.B. implicit priority is most severe */
ajs1e221352005-02-03 16:42:40 +0000401#define PRI LOG_CRIT
ajs274a4a42004-12-07 15:39:31 +0000402
ajs1e221352005-02-03 16:42:40 +0000403#define DUMP(FD) write(FD, buf, s-buf);
404 /* If no file logging configured, try to write to fallback log file. */
ajsc4c7d0c2005-02-03 19:22:05 +0000405 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
406 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000407 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000408 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000409 else
410 {
ajs274a4a42004-12-07 15:39:31 +0000411 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000412 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000413 /* Remove trailing '\n' for monitor and syslog */
414 *--s = '\0';
415 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
416 vty_log_fixed(buf,s-buf);
417 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
418 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000419 }
420#undef DUMP
421
ajs31364272005-01-18 22:18:59 +0000422 zlog_backtrace_sigsafe(PRI,
423#ifdef SA_SIGINFO
424 program_counter
425#else
426 NULL
427#endif
428 );
David Lamparter615f9f12013-11-18 23:52:02 +0100429
430 s = buf;
431 if (!thread_current)
432 s = str_append (LOC, "no thread information available\n");
433 else
434 {
435 s = str_append (LOC, "in thread ");
436 s = str_append (LOC, thread_current->funcname);
437 s = str_append (LOC, " scheduled from ");
438 s = str_append (LOC, thread_current->schedfrom);
439 s = str_append (LOC, ":");
440 s = num_append (LOC, thread_current->schedfrom_line);
441 s = str_append (LOC, "\n");
442 }
443
444#define DUMP(FD) write(FD, buf, s-buf);
445 /* If no file logging configured, try to write to fallback log file. */
446 if (logfile_fd >= 0)
447 DUMP(logfile_fd)
448 if (!zlog_default)
449 DUMP(STDERR_FILENO)
450 else
451 {
452 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
453 DUMP(STDOUT_FILENO)
454 /* Remove trailing '\n' for monitor and syslog */
455 *--s = '\0';
456 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
457 vty_log_fixed(buf,s-buf);
458 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
459 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
460 }
461#undef DUMP
462
ajs274a4a42004-12-07 15:39:31 +0000463#undef PRI
ajs063ee522004-11-26 18:11:14 +0000464#undef LOC
465}
ajs59a06a92004-11-23 18:19:14 +0000466
ajs063ee522004-11-26 18:11:14 +0000467/* Log a backtrace using only async-signal-safe functions.
468 Needs to be enhanced to support syslog logging. */
469void
ajs239c26f2005-01-17 15:22:28 +0000470zlog_backtrace_sigsafe(int priority, void *program_counter)
ajs063ee522004-11-26 18:11:14 +0000471{
Paul Jakmafb66b292006-05-28 08:26:15 +0000472#ifdef HAVE_STACK_TRACE
ajs239c26f2005-01-17 15:22:28 +0000473 static const char pclabel[] = "Program counter: ";
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700474 void *array[64];
ajs063ee522004-11-26 18:11:14 +0000475 int size;
476 char buf[100];
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700477 char *s, **bt = NULL;
ajs063ee522004-11-26 18:11:14 +0000478#define LOC s,buf+sizeof(buf)-s
479
Paul Jakmafb66b292006-05-28 08:26:15 +0000480#ifdef HAVE_GLIBC_BACKTRACE
David Lamparter4d474fa2013-11-19 15:00:06 +0100481 size = backtrace(array, array_size(array));
482 if (size <= 0 || (size_t)size > array_size(array))
ajs063ee522004-11-26 18:11:14 +0000483 return;
ajs59a06a92004-11-23 18:19:14 +0000484
ajs1e221352005-02-03 16:42:40 +0000485#define DUMP(FD) { \
ajs239c26f2005-01-17 15:22:28 +0000486 if (program_counter) \
487 { \
ajs1e221352005-02-03 16:42:40 +0000488 write(FD, pclabel, sizeof(pclabel)-1); \
489 backtrace_symbols_fd(&program_counter, 1, FD); \
ajs239c26f2005-01-17 15:22:28 +0000490 } \
ajs1e221352005-02-03 16:42:40 +0000491 write(FD, buf, s-buf); \
492 backtrace_symbols_fd(array, size, FD); \
ajs59a06a92004-11-23 18:19:14 +0000493}
Paul Jakmafb66b292006-05-28 08:26:15 +0000494#elif defined(HAVE_PRINTSTACK)
495#define DUMP(FD) { \
496 if (program_counter) \
497 write((FD), pclabel, sizeof(pclabel)-1); \
498 write((FD), buf, s-buf); \
499 printstack((FD)); \
500}
501#endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
502
503 s = buf;
504 s = str_append(LOC,"Backtrace for ");
505 s = num_append(LOC,size);
506 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000507
ajsc4c7d0c2005-02-03 19:22:05 +0000508 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
509 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000510 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000511 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000512 else
513 {
ajs274a4a42004-12-07 15:39:31 +0000514 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000515 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000516 /* Remove trailing '\n' for monitor and syslog */
517 *--s = '\0';
518 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
519 vty_log_fixed(buf,s-buf);
520 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
521 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
522 {
523 int i;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700524#ifdef HAVE_GLIBC_BACKTRACE
525 bt = backtrace_symbols(array, size);
526#endif
ajs274a4a42004-12-07 15:39:31 +0000527 /* Just print the function addresses. */
528 for (i = 0; i < size; i++)
529 {
530 s = buf;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700531 if (bt)
532 s = str_append(LOC, bt[i]);
533 else {
534 s = str_append(LOC,"[bt ");
535 s = num_append(LOC,i);
536 s = str_append(LOC,"] 0x");
537 s = hex_append(LOC,(u_long)(array[i]));
538 }
ajs274a4a42004-12-07 15:39:31 +0000539 *s = '\0';
540 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
541 vty_log_fixed(buf,s-buf);
542 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000543 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000544 }
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700545 if (bt)
546 free(bt);
ajs274a4a42004-12-07 15:39:31 +0000547 }
ajs59a06a92004-11-23 18:19:14 +0000548 }
549#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000550#undef LOC
Paul Jakmafb66b292006-05-28 08:26:15 +0000551#endif /* HAVE_STRACK_TRACE */
ajs063ee522004-11-26 18:11:14 +0000552}
553
554void
555zlog_backtrace(int priority)
556{
557#ifndef HAVE_GLIBC_BACKTRACE
558 zlog(NULL, priority, "No backtrace available on this platform.");
559#else
560 void *array[20];
561 int size, i;
562 char **strings;
563
David Lamparter4d474fa2013-11-19 15:00:06 +0100564 size = backtrace(array, array_size(array));
565 if (size <= 0 || (size_t)size > array_size(array))
ajs063ee522004-11-26 18:11:14 +0000566 {
567 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000568 "(valid range is between 1 and %lu)",
Balaji.G837d16c2012-09-26 14:09:10 +0530569 size, (unsigned long)(array_size(array)));
ajs063ee522004-11-26 18:11:14 +0000570 return;
571 }
572 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
573 if (!(strings = backtrace_symbols(array, size)))
574 {
575 zlog_err("Cannot get backtrace symbols (out of memory?)");
576 for (i = 0; i < size; i++)
577 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
578 }
579 else
580 {
581 for (i = 0; i < size; i++)
582 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
583 free(strings);
584 }
585#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000586}
587
paul718e3742002-12-13 20:15:29 +0000588void
589zlog (struct zlog *zl, int priority, const char *format, ...)
590{
ajsd246bd92004-11-23 17:35:08 +0000591 va_list args;
paul718e3742002-12-13 20:15:29 +0000592
ajsd246bd92004-11-23 17:35:08 +0000593 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000594 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000595 va_end (args);
paul718e3742002-12-13 20:15:29 +0000596}
597
ajsd246bd92004-11-23 17:35:08 +0000598#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
599void \
600FUNCNAME(const char *format, ...) \
601{ \
602 va_list args; \
603 va_start(args, format); \
604 vzlog (NULL, PRIORITY, format, args); \
605 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000606}
607
ajsd246bd92004-11-23 17:35:08 +0000608ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000609
ajsd246bd92004-11-23 17:35:08 +0000610ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000611
ajsd246bd92004-11-23 17:35:08 +0000612ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000613
ajsd246bd92004-11-23 17:35:08 +0000614ZLOG_FUNC(zlog_notice, LOG_NOTICE)
615
616ZLOG_FUNC(zlog_debug, LOG_DEBUG)
617
618#undef ZLOG_FUNC
619
620#define PLOG_FUNC(FUNCNAME,PRIORITY) \
621void \
622FUNCNAME(struct zlog *zl, const char *format, ...) \
623{ \
624 va_list args; \
625 va_start(args, format); \
626 vzlog (zl, PRIORITY, format, args); \
627 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000628}
629
ajsd246bd92004-11-23 17:35:08 +0000630PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000631
ajsd246bd92004-11-23 17:35:08 +0000632PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000633
ajsd246bd92004-11-23 17:35:08 +0000634PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000635
ajsd246bd92004-11-23 17:35:08 +0000636PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000637
ajsd246bd92004-11-23 17:35:08 +0000638PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000639
ajsd246bd92004-11-23 17:35:08 +0000640#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000641
David Lamparter615f9f12013-11-18 23:52:02 +0100642void zlog_thread_info (int log_level)
643{
644 if (thread_current)
645 zlog(NULL, log_level, "Current thread function %s, scheduled from "
646 "file %s, line %u", thread_current->funcname,
647 thread_current->schedfrom, thread_current->schedfrom_line);
648 else
649 zlog(NULL, log_level, "Current thread not known/applicable");
650}
651
ajscee3df12004-11-24 17:14:49 +0000652void
653_zlog_assert_failed (const char *assertion, const char *file,
654 unsigned int line, const char *function)
655{
ajsc4c7d0c2005-02-03 19:22:05 +0000656 /* Force fallback file logging? */
657 if (zlog_default && !zlog_default->fp &&
658 ((logfile_fd = open_crashlog()) >= 0) &&
659 ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
660 zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
ajs1e221352005-02-03 16:42:40 +0000661 zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
662 assertion,file,line,(function ? function : "?"));
663 zlog_backtrace(LOG_CRIT);
David Lamparter615f9f12013-11-18 23:52:02 +0100664 zlog_thread_info(LOG_CRIT);
ajscee3df12004-11-24 17:14:49 +0000665 abort();
666}
667
David Lamparter6b0655a2014-06-04 06:53:35 +0200668
paul718e3742002-12-13 20:15:29 +0000669/* Open log stream */
670struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000671openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000672 int syslog_flags, int syslog_facility)
673{
674 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000675 u_int i;
paul718e3742002-12-13 20:15:29 +0000676
ajs274a4a42004-12-07 15:39:31 +0000677 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000678
679 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000680 zl->protocol = protocol;
681 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000682 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000683
ajs274a4a42004-12-07 15:39:31 +0000684 /* Set default logging levels. */
Balaji.G837d16c2012-09-26 14:09:10 +0530685 for (i = 0; i < array_size(zl->maxlvl); i++)
ajs274a4a42004-12-07 15:39:31 +0000686 zl->maxlvl[i] = ZLOG_DISABLED;
687 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
688 zl->default_lvl = LOG_DEBUG;
689
paul718e3742002-12-13 20:15:29 +0000690 openlog (progname, syslog_flags, zl->facility);
691
692 return zl;
693}
694
695void
696closezlog (struct zlog *zl)
697{
698 closelog();
Chris Caputo228da422009-07-18 05:44:03 +0000699
700 if (zl->fp != NULL)
701 fclose (zl->fp);
paul718e3742002-12-13 20:15:29 +0000702
Tom Goff7e69d992010-11-10 13:01:17 -0800703 if (zl->filename != NULL)
704 free (zl->filename);
705
paul718e3742002-12-13 20:15:29 +0000706 XFREE (MTYPE_ZLOG, zl);
707}
708
709/* Called from command.c. */
710void
ajs274a4a42004-12-07 15:39:31 +0000711zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000712{
713 if (zl == NULL)
714 zl = zlog_default;
715
ajs274a4a42004-12-07 15:39:31 +0000716 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000717}
718
719int
ajs274a4a42004-12-07 15:39:31 +0000720zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000721{
722 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000723 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000724
725 /* There is opend file. */
726 zlog_reset_file (zl);
727
728 /* Set default zl. */
729 if (zl == NULL)
730 zl = zlog_default;
731
732 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000733 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000734 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000735 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000736 if (fp == NULL)
737 return 0;
paul718e3742002-12-13 20:15:29 +0000738
739 /* Set flags. */
740 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000741 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000742 zl->fp = fp;
ajsc4c7d0c2005-02-03 19:22:05 +0000743 logfile_fd = fileno(fp);
paul718e3742002-12-13 20:15:29 +0000744
745 return 1;
746}
747
748/* Reset opend file. */
749int
750zlog_reset_file (struct zlog *zl)
751{
752 if (zl == NULL)
753 zl = zlog_default;
754
paul718e3742002-12-13 20:15:29 +0000755 if (zl->fp)
756 fclose (zl->fp);
757 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000758 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000759 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000760
761 if (zl->filename)
762 free (zl->filename);
763 zl->filename = NULL;
764
765 return 1;
766}
767
768/* Reopen log file. */
769int
770zlog_rotate (struct zlog *zl)
771{
ajs274a4a42004-12-07 15:39:31 +0000772 int level;
paul718e3742002-12-13 20:15:29 +0000773
774 if (zl == NULL)
775 zl = zlog_default;
776
777 if (zl->fp)
778 fclose (zl->fp);
779 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000780 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000781 level = zl->maxlvl[ZLOG_DEST_FILE];
782 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000783
784 if (zl->filename)
785 {
gdtaa593d52003-12-22 20:15:53 +0000786 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000787 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000788
789 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000790 zl->fp = fopen (zl->filename, "a");
791 save_errno = errno;
792 umask(oldumask);
793 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000794 {
ajs274a4a42004-12-07 15:39:31 +0000795 zlog_err("Log rotate failed: cannot open file %s for append: %s",
796 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000797 return -1;
798 }
ajsc4c7d0c2005-02-03 19:22:05 +0000799 logfile_fd = fileno(zl->fp);
ajs274a4a42004-12-07 15:39:31 +0000800 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000801 }
802
803 return 1;
804}
David Lamparter6b0655a2014-06-04 06:53:35 +0200805
paul718e3742002-12-13 20:15:29 +0000806/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000807const char *
Stephen Hemminger1423c802008-08-14 17:59:25 +0100808lookup (const struct message *mes, int key)
paul718e3742002-12-13 20:15:29 +0000809{
Stephen Hemminger1423c802008-08-14 17:59:25 +0100810 const struct message *pnt;
paul718e3742002-12-13 20:15:29 +0000811
812 for (pnt = mes; pnt->key != 0; pnt++)
813 if (pnt->key == key)
814 return pnt->str;
815
816 return "";
817}
818
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000819/* Older/faster version of message lookup function, but requires caller to pass
Paul Jakma11486b52008-02-28 23:26:02 +0000820 * in the array size (instead of relying on a 0 key to terminate the search).
821 *
822 * The return value is the message string if found, or the 'none' pointer
823 * provided otherwise.
824 */
hasso8c328f12004-10-05 21:01:23 +0000825const char *
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400826mes_lookup (const struct message *meslist, int max, int index,
827 const char *none, const char *mesname)
paul718e3742002-12-13 20:15:29 +0000828{
Paul Jakma11486b52008-02-28 23:26:02 +0000829 int pos = index - meslist[0].key;
830
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000831 /* first check for best case: index is in range and matches the key
Paul Jakma11486b52008-02-28 23:26:02 +0000832 * value in that slot.
833 * NB: key numbering might be offset from 0. E.g. protocol constants
834 * often start at 1.
835 */
836 if ((pos >= 0) && (pos < max)
837 && (meslist[pos].key == index))
838 return meslist[pos].str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000839
840 /* fall back to linear search */
841 {
842 int i;
843
844 for (i = 0; i < max; i++, meslist++)
845 {
846 if (meslist->key == index)
847 {
Paul Jakma11486b52008-02-28 23:26:02 +0000848 const char *str = (meslist->str ? meslist->str : none);
849
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400850 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
851 index, str, mesname, i, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000852 return str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000853 }
854 }
855 }
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400856 zlog_err("message index %d not found in %s (max is %d)", index, mesname, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000857 assert (none);
858 return none;
paul718e3742002-12-13 20:15:29 +0000859}
ajsca359762004-11-19 23:40:16 +0000860
861/* Wrapper around strerror to handle case where it returns NULL. */
862const char *
863safe_strerror(int errnum)
864{
865 const char *s = strerror(errnum);
866 return (s != NULL) ? s : "Unknown error";
867}
ajsf52d13c2005-10-01 17:38:06 +0000868
Paul Jakmad6d672a2006-05-15 16:56:51 +0000869#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
870static const struct zebra_desc_table command_types[] = {
871 DESC_ENTRY (ZEBRA_INTERFACE_ADD),
872 DESC_ENTRY (ZEBRA_INTERFACE_DELETE),
873 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD),
874 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE),
875 DESC_ENTRY (ZEBRA_INTERFACE_UP),
876 DESC_ENTRY (ZEBRA_INTERFACE_DOWN),
877 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD),
878 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE),
879 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD),
880 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE),
881 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD),
882 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE),
883 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
884 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
885 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP),
886 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP),
887 DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP),
888 DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP),
889 DESC_ENTRY (ZEBRA_INTERFACE_RENAME),
890 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
891 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
892 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
Denis Ovsienko4c783762012-01-21 22:50:19 +0400893 DESC_ENTRY (ZEBRA_HELLO),
Paul Jakmad6d672a2006-05-15 16:56:51 +0000894};
895#undef DESC_ENTRY
896
897static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
898
899static const struct zebra_desc_table *
ajsf52d13c2005-10-01 17:38:06 +0000900zroute_lookup(u_int zroute)
901{
ajsf52d13c2005-10-01 17:38:06 +0000902 u_int i;
903
Balaji.G837d16c2012-09-26 14:09:10 +0530904 if (zroute >= array_size(route_types))
ajsf52d13c2005-10-01 17:38:06 +0000905 {
906 zlog_err("unknown zebra route type: %u", zroute);
907 return &unknown;
908 }
Paul Jakmad6d672a2006-05-15 16:56:51 +0000909 if (zroute == route_types[zroute].type)
ajsf52d13c2005-10-01 17:38:06 +0000910 return &route_types[zroute];
Balaji.G837d16c2012-09-26 14:09:10 +0530911 for (i = 0; i < array_size(route_types); i++)
ajsf52d13c2005-10-01 17:38:06 +0000912 {
Paul Jakmad6d672a2006-05-15 16:56:51 +0000913 if (zroute == route_types[i].type)
ajsf52d13c2005-10-01 17:38:06 +0000914 {
915 zlog_warn("internal error: route type table out of order "
916 "while searching for %u, please notify developers", zroute);
917 return &route_types[i];
918 }
919 }
920 zlog_err("internal error: cannot find route type %u in table!", zroute);
921 return &unknown;
922}
923
924const char *
925zebra_route_string(u_int zroute)
926{
927 return zroute_lookup(zroute)->string;
928}
929
930char
931zebra_route_char(u_int zroute)
932{
933 return zroute_lookup(zroute)->chr;
934}
Paul Jakmad6d672a2006-05-15 16:56:51 +0000935
936const char *
937zserv_command_string (unsigned int command)
938{
Balaji.G837d16c2012-09-26 14:09:10 +0530939 if (command >= array_size(command_types))
Paul Jakmad6d672a2006-05-15 16:56:51 +0000940 {
941 zlog_err ("unknown zserv command type: %u", command);
942 return unknown.string;
943 }
944 return command_types[command].string;
945}
Paul Jakma7514fb72007-05-02 16:05:35 +0000946
Paul Jakma7514fb72007-05-02 16:05:35 +0000947int
948proto_name2num(const char *s)
949{
950 unsigned i;
951
Balaji.G837d16c2012-09-26 14:09:10 +0530952 for (i=0; i<array_size(route_types); ++i)
Paul Jakma7514fb72007-05-02 16:05:35 +0000953 if (strcasecmp(s, route_types[i].string) == 0)
954 return route_types[i].type;
955 return -1;
956}
David Lampartere0ca5fd2009-09-16 01:52:42 +0200957
David Lampartere0ca5fd2009-09-16 01:52:42 +0200958int
959proto_redistnum(int afi, const char *s)
960{
961 if (! s)
962 return -1;
963
964 if (afi == AFI_IP)
965 {
966 if (strncmp (s, "k", 1) == 0)
967 return ZEBRA_ROUTE_KERNEL;
968 else if (strncmp (s, "c", 1) == 0)
969 return ZEBRA_ROUTE_CONNECT;
970 else if (strncmp (s, "s", 1) == 0)
971 return ZEBRA_ROUTE_STATIC;
972 else if (strncmp (s, "r", 1) == 0)
973 return ZEBRA_ROUTE_RIP;
974 else if (strncmp (s, "o", 1) == 0)
975 return ZEBRA_ROUTE_OSPF;
976 else if (strncmp (s, "i", 1) == 0)
977 return ZEBRA_ROUTE_ISIS;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400978 else if (strncmp (s, "bg", 2) == 0)
David Lampartere0ca5fd2009-09-16 01:52:42 +0200979 return ZEBRA_ROUTE_BGP;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400980 else if (strncmp (s, "ba", 2) == 0)
981 return ZEBRA_ROUTE_BABEL;
David Lampartere0ca5fd2009-09-16 01:52:42 +0200982 }
983 if (afi == AFI_IP6)
984 {
985 if (strncmp (s, "k", 1) == 0)
986 return ZEBRA_ROUTE_KERNEL;
987 else if (strncmp (s, "c", 1) == 0)
988 return ZEBRA_ROUTE_CONNECT;
989 else if (strncmp (s, "s", 1) == 0)
990 return ZEBRA_ROUTE_STATIC;
991 else if (strncmp (s, "r", 1) == 0)
992 return ZEBRA_ROUTE_RIPNG;
993 else if (strncmp (s, "o", 1) == 0)
994 return ZEBRA_ROUTE_OSPF6;
995 else if (strncmp (s, "i", 1) == 0)
996 return ZEBRA_ROUTE_ISIS;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400997 else if (strncmp (s, "bg", 2) == 0)
David Lampartere0ca5fd2009-09-16 01:52:42 +0200998 return ZEBRA_ROUTE_BGP;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400999 else if (strncmp (s, "ba", 2) == 0)
1000 return ZEBRA_ROUTE_BABEL;
David Lampartere0ca5fd2009-09-16 01:52:42 +02001001 }
1002 return -1;
1003}
Daniel Waltonc8af6802015-10-21 06:56:44 -07001004
1005void
1006zlog_hexdump (void *mem, unsigned int len) {
1007 unsigned long i = 0;
1008 unsigned int j = 0;
1009 unsigned int columns = 8;
1010 char buf[(len * 4) + ((len/4) * 20) + 30];
1011 char *s = buf;
1012
1013 for (i = 0; i < len + ((len % columns) ? (columns - len % columns) : 0); i++)
1014 {
1015 /* print offset */
1016 if (i % columns == 0)
1017 s += sprintf(s, "0x%016lx: ", (unsigned long)mem + i);
1018
1019 /* print hex data */
1020 if (i < len)
1021 s += sprintf(s, "%02x ", 0xFF & ((char*)mem)[i]);
1022
1023 /* end of block, just aligning for ASCII dump */
1024 else
1025 s += sprintf(s, " ");
1026
1027 /* print ASCII dump */
1028 if (i % columns == (columns - 1))
1029 {
1030 for (j = i - (columns - 1); j <= i; j++)
1031 {
1032 if (j >= len) /* end of block, not really printing */
1033 s += sprintf(s, " ");
1034
1035 else if(isprint(((char*)mem)[j])) /* printable char */
1036 s += sprintf(s, "%c", 0xFF & ((char*)mem)[j]);
1037
1038 else /* other char */
1039 s += sprintf(s, ".");
1040 }
1041 s += sprintf(s, "\n");
1042 }
1043 }
1044 zlog_debug("\n%s", buf);
1045}