blob: d4370669c7b94372467a26f3a809d32947ddf969 [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",
Timo Teräsdafa05e2017-01-19 17:27:01 +020056 "NHRP",
paul718e3742002-12-13 20:15:29 +000057 NULL,
58};
59
60const char *zlog_priority[] =
61{
62 "emergencies",
63 "alerts",
64 "critical",
65 "errors",
66 "warnings",
67 "notifications",
68 "informational",
69 "debugging",
70 NULL,
71};
72
73
David Lamparter6b0655a2014-06-04 06:53:35 +020074
paul718e3742002-12-13 20:15:29 +000075/* For time string format. */
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +000076
77size_t
78quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
79{
80 static struct {
81 time_t last;
82 size_t len;
83 char buf[28];
84 } cache;
85 struct timeval clock;
86
87 /* would it be sufficient to use global 'recent_time' here? I fear not... */
88 gettimeofday(&clock, NULL);
89
90 /* first, we update the cache if the time has changed */
91 if (cache.last != clock.tv_sec)
92 {
93 struct tm *tm;
94 cache.last = clock.tv_sec;
95 tm = localtime(&cache.last);
96 cache.len = strftime(cache.buf, sizeof(cache.buf),
97 "%Y/%m/%d %H:%M:%S", tm);
98 }
99 /* note: it's not worth caching the subsecond part, because
100 chances are that back-to-back calls are not sufficiently close together
101 for the clock not to have ticked forward */
102
103 if (buflen > cache.len)
104 {
105 memcpy(buf, cache.buf, cache.len);
106 if ((timestamp_precision > 0) &&
107 (buflen > cache.len+1+timestamp_precision))
108 {
109 /* should we worry about locale issues? */
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000110 static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1};
111 int prec;
112 char *p = buf+cache.len+1+(prec = timestamp_precision);
113 *p-- = '\0';
114 while (prec > 6)
115 /* this is unlikely to happen, but protect anyway */
116 {
117 *p-- = '0';
118 prec--;
119 }
120 clock.tv_usec /= divisor[prec];
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000121 do
122 {
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000123 *p-- = '0'+(clock.tv_usec % 10);
124 clock.tv_usec /= 10;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000125 }
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000126 while (--prec > 0);
127 *p = '.';
128 return cache.len+1+timestamp_precision;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000129 }
130 buf[cache.len] = '\0';
131 return cache.len;
132 }
133 if (buflen > 0)
134 buf[0] = '\0';
135 return 0;
136}
paul718e3742002-12-13 20:15:29 +0000137
138/* Utility routine for current time printing. */
139static void
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000140time_print(FILE *fp, struct timestamp_control *ctl)
paul718e3742002-12-13 20:15:29 +0000141{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000142 if (!ctl->already_rendered)
143 {
144 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
145 ctl->already_rendered = 1;
146 }
147 fprintf(fp, "%s ", ctl->buf);
paul718e3742002-12-13 20:15:29 +0000148}
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000149
David Lamparter6b0655a2014-06-04 06:53:35 +0200150
paul718e3742002-12-13 20:15:29 +0000151/* va_list version of zlog. */
ajsd246bd92004-11-23 17:35:08 +0000152static void
153vzlog (struct zlog *zl, int priority, const char *format, va_list args)
paul718e3742002-12-13 20:15:29 +0000154{
Christian Frankeabfd40d2015-11-10 18:04:42 +0100155 int original_errno = errno;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000156 struct timestamp_control tsctl;
157 tsctl.already_rendered = 0;
158
paul718e3742002-12-13 20:15:29 +0000159 /* If zlog is not specified, use default one. */
160 if (zl == NULL)
161 zl = zlog_default;
162
163 /* When zlog_default is also NULL, use stderr for logging. */
164 if (zl == NULL)
165 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000166 tsctl.precision = 0;
167 time_print(stderr, &tsctl);
paul718e3742002-12-13 20:15:29 +0000168 fprintf (stderr, "%s: ", "unknown");
ajsd246bd92004-11-23 17:35:08 +0000169 vfprintf (stderr, format, args);
paul718e3742002-12-13 20:15:29 +0000170 fprintf (stderr, "\n");
171 fflush (stderr);
172
173 /* In this case we return at here. */
Christian Frankeabfd40d2015-11-10 18:04:42 +0100174 errno = original_errno;
paul718e3742002-12-13 20:15:29 +0000175 return;
176 }
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000177 tsctl.precision = zl->timestamp_precision;
paul718e3742002-12-13 20:15:29 +0000178
paul718e3742002-12-13 20:15:29 +0000179 /* Syslog output */
ajs274a4a42004-12-07 15:39:31 +0000180 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
ajsd246bd92004-11-23 17:35:08 +0000181 {
182 va_list ac;
183 va_copy(ac, args);
184 vsyslog (priority|zlog_default->facility, format, ac);
185 va_end(ac);
186 }
paul718e3742002-12-13 20:15:29 +0000187
188 /* File output. */
ajs274a4a42004-12-07 15:39:31 +0000189 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
paul718e3742002-12-13 20:15:29 +0000190 {
ajsd246bd92004-11-23 17:35:08 +0000191 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000192 time_print (zl->fp, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000193 if (zl->record_priority)
194 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000195 fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000196 va_copy(ac, args);
197 vfprintf (zl->fp, format, ac);
198 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000199 fprintf (zl->fp, "\n");
200 fflush (zl->fp);
201 }
202
203 /* stdout output. */
ajs274a4a42004-12-07 15:39:31 +0000204 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
paul718e3742002-12-13 20:15:29 +0000205 {
ajsd246bd92004-11-23 17:35:08 +0000206 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000207 time_print (stdout, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000208 if (zl->record_priority)
209 fprintf (stdout, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000210 fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000211 va_copy(ac, args);
212 vfprintf (stdout, format, ac);
213 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000214 fprintf (stdout, "\n");
215 fflush (stdout);
216 }
217
paul718e3742002-12-13 20:15:29 +0000218 /* Terminal monitor. */
ajs274a4a42004-12-07 15:39:31 +0000219 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
220 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000221 zlog_proto_names[zl->protocol], format, &tsctl, args);
Christian Frankeabfd40d2015-11-10 18:04:42 +0100222
223 errno = original_errno;
paul718e3742002-12-13 20:15:29 +0000224}
225
ajs59a06a92004-11-23 18:19:14 +0000226static char *
227str_append(char *dst, int len, const char *src)
228{
229 while ((len-- > 0) && *src)
230 *dst++ = *src++;
231 return dst;
232}
233
234static char *
235num_append(char *s, int len, u_long x)
236{
237 char buf[30];
ajs7d149b82004-11-28 23:00:01 +0000238 char *t;
ajs59a06a92004-11-23 18:19:14 +0000239
ajs7d149b82004-11-28 23:00:01 +0000240 if (!x)
241 return str_append(s,len,"0");
242 *(t = &buf[sizeof(buf)-1]) = '\0';
ajs59a06a92004-11-23 18:19:14 +0000243 while (x && (t > buf))
244 {
245 *--t = '0'+(x % 10);
246 x /= 10;
247 }
248 return str_append(s,len,t);
249}
250
Paul Jakmafb66b292006-05-28 08:26:15 +0000251#if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
ajs7d149b82004-11-28 23:00:01 +0000252static char *
253hex_append(char *s, int len, u_long x)
254{
255 char buf[30];
256 char *t;
257
258 if (!x)
259 return str_append(s,len,"0");
260 *(t = &buf[sizeof(buf)-1]) = '\0';
261 while (x && (t > buf))
262 {
263 u_int cc = (x % 16);
264 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
265 x /= 16;
266 }
267 return str_append(s,len,t);
268}
ajs31364272005-01-18 22:18:59 +0000269#endif
ajs7d149b82004-11-28 23:00:01 +0000270
ajs7d149b82004-11-28 23:00:01 +0000271/* Needs to be enhanced to support Solaris. */
272static int
273syslog_connect(void)
274{
275#ifdef SUNOS_5
276 return -1;
277#else
278 int fd;
279 char *s;
280 struct sockaddr_un addr;
281
282 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
283 return -1;
284 addr.sun_family = AF_UNIX;
285#ifdef _PATH_LOG
286#define SYSLOG_SOCKET_PATH _PATH_LOG
287#else
288#define SYSLOG_SOCKET_PATH "/dev/log"
289#endif
290 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
291#undef SYSLOG_SOCKET_PATH
292 *s = '\0';
293 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
294 {
295 close(fd);
296 return -1;
297 }
298 return fd;
299#endif
300}
301
302static void
303syslog_sigsafe(int priority, const char *msg, size_t msglen)
304{
ajs1e221352005-02-03 16:42:40 +0000305 static int syslog_fd = -1;
ajs7d149b82004-11-28 23:00:01 +0000306 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
307 char *s;
308
309 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
310 return;
311
312#define LOC s,buf+sizeof(buf)-s
313 s = buf;
314 s = str_append(LOC,"<");
315 s = num_append(LOC,priority);
316 s = str_append(LOC,">");
317 /* forget about the timestamp, too difficult in a signal handler */
318 s = str_append(LOC,zlog_default->ident);
319 if (zlog_default->syslog_options & LOG_PID)
320 {
321 s = str_append(LOC,"[");
322 s = num_append(LOC,getpid());
323 s = str_append(LOC,"]");
324 }
325 s = str_append(LOC,": ");
326 s = str_append(LOC,msg);
327 write(syslog_fd,buf,s-buf);
328#undef LOC
329}
330
ajs1e221352005-02-03 16:42:40 +0000331static int
332open_crashlog(void)
333{
334#define CRASHLOG_PREFIX "/var/tmp/quagga."
335#define CRASHLOG_SUFFIX "crashlog"
336 if (zlog_default && zlog_default->ident)
337 {
338 /* Avoid strlen since it is not async-signal-safe. */
339 const char *p;
340 size_t ilen;
341
342 for (p = zlog_default->ident, ilen = 0; *p; p++)
343 ilen++;
344 {
345 char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3];
346 char *s = buf;
347#define LOC s,buf+sizeof(buf)-s
348 s = str_append(LOC, CRASHLOG_PREFIX);
349 s = str_append(LOC, zlog_default->ident);
350 s = str_append(LOC, ".");
351 s = str_append(LOC, CRASHLOG_SUFFIX);
352#undef LOC
353 *s = '\0';
354 return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK);
355 }
356 }
357 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL,
358 LOGFILE_MASK);
359#undef CRASHLOG_SUFFIX
360#undef CRASHLOG_PREFIX
361}
362
ajs7d149b82004-11-28 23:00:01 +0000363/* Note: the goal here is to use only async-signal-safe functions. */
ajs59a06a92004-11-23 18:19:14 +0000364void
ajs31364272005-01-18 22:18:59 +0000365zlog_signal(int signo, const char *action
366#ifdef SA_SIGINFO
367 , siginfo_t *siginfo, void *program_counter
368#endif
369 )
ajs59a06a92004-11-23 18:19:14 +0000370{
371 time_t now;
ajs40abf232005-01-12 17:27:27 +0000372 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
ajs59a06a92004-11-23 18:19:14 +0000373 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000374 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000375#define LOC s,buf+sizeof(buf)-s
376
377 time(&now);
378 if (zlog_default)
379 {
380 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
381 *s++ = ':';
382 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000383 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000384 }
385 s = str_append(LOC,"Received signal ");
386 s = num_append(LOC,signo);
387 s = str_append(LOC," at ");
388 s = num_append(LOC,now);
ajs31364272005-01-18 22:18:59 +0000389#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +0000390 s = str_append(LOC," (si_addr 0x");
391 s = hex_append(LOC,(u_long)(siginfo->si_addr));
392 if (program_counter)
393 {
394 s = str_append(LOC,", PC 0x");
395 s = hex_append(LOC,(u_long)program_counter);
396 }
397 s = str_append(LOC,"); ");
ajs31364272005-01-18 22:18:59 +0000398#else /* SA_SIGINFO */
399 s = str_append(LOC,"; ");
400#endif /* SA_SIGINFO */
ajs59a06a92004-11-23 18:19:14 +0000401 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000402 if (s < buf+sizeof(buf))
403 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000404
ajs274a4a42004-12-07 15:39:31 +0000405 /* N.B. implicit priority is most severe */
ajs1e221352005-02-03 16:42:40 +0000406#define PRI LOG_CRIT
ajs274a4a42004-12-07 15:39:31 +0000407
ajs1e221352005-02-03 16:42:40 +0000408#define DUMP(FD) write(FD, buf, s-buf);
409 /* If no file logging configured, try to write to fallback log file. */
ajsc4c7d0c2005-02-03 19:22:05 +0000410 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
411 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000412 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000413 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000414 else
415 {
ajs274a4a42004-12-07 15:39:31 +0000416 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000417 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000418 /* Remove trailing '\n' for monitor and syslog */
419 *--s = '\0';
420 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
421 vty_log_fixed(buf,s-buf);
422 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
423 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000424 }
425#undef DUMP
426
ajs31364272005-01-18 22:18:59 +0000427 zlog_backtrace_sigsafe(PRI,
428#ifdef SA_SIGINFO
429 program_counter
430#else
431 NULL
432#endif
433 );
David Lamparter615f9f12013-11-18 23:52:02 +0100434
435 s = buf;
436 if (!thread_current)
437 s = str_append (LOC, "no thread information available\n");
438 else
439 {
440 s = str_append (LOC, "in thread ");
441 s = str_append (LOC, thread_current->funcname);
442 s = str_append (LOC, " scheduled from ");
443 s = str_append (LOC, thread_current->schedfrom);
444 s = str_append (LOC, ":");
445 s = num_append (LOC, thread_current->schedfrom_line);
446 s = str_append (LOC, "\n");
447 }
448
449#define DUMP(FD) write(FD, buf, s-buf);
450 /* If no file logging configured, try to write to fallback log file. */
451 if (logfile_fd >= 0)
452 DUMP(logfile_fd)
453 if (!zlog_default)
454 DUMP(STDERR_FILENO)
455 else
456 {
457 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
458 DUMP(STDOUT_FILENO)
459 /* Remove trailing '\n' for monitor and syslog */
460 *--s = '\0';
461 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
462 vty_log_fixed(buf,s-buf);
463 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
464 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
465 }
466#undef DUMP
467
ajs274a4a42004-12-07 15:39:31 +0000468#undef PRI
ajs063ee522004-11-26 18:11:14 +0000469#undef LOC
470}
ajs59a06a92004-11-23 18:19:14 +0000471
ajs063ee522004-11-26 18:11:14 +0000472/* Log a backtrace using only async-signal-safe functions.
473 Needs to be enhanced to support syslog logging. */
474void
ajs239c26f2005-01-17 15:22:28 +0000475zlog_backtrace_sigsafe(int priority, void *program_counter)
ajs063ee522004-11-26 18:11:14 +0000476{
Paul Jakmafb66b292006-05-28 08:26:15 +0000477#ifdef HAVE_STACK_TRACE
ajs239c26f2005-01-17 15:22:28 +0000478 static const char pclabel[] = "Program counter: ";
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700479 void *array[64];
ajs063ee522004-11-26 18:11:14 +0000480 int size;
481 char buf[100];
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700482 char *s, **bt = NULL;
ajs063ee522004-11-26 18:11:14 +0000483#define LOC s,buf+sizeof(buf)-s
484
Paul Jakmafb66b292006-05-28 08:26:15 +0000485#ifdef HAVE_GLIBC_BACKTRACE
David Lamparter4d474fa2013-11-19 15:00:06 +0100486 size = backtrace(array, array_size(array));
487 if (size <= 0 || (size_t)size > array_size(array))
ajs063ee522004-11-26 18:11:14 +0000488 return;
ajs59a06a92004-11-23 18:19:14 +0000489
ajs1e221352005-02-03 16:42:40 +0000490#define DUMP(FD) { \
ajs239c26f2005-01-17 15:22:28 +0000491 if (program_counter) \
492 { \
ajs1e221352005-02-03 16:42:40 +0000493 write(FD, pclabel, sizeof(pclabel)-1); \
494 backtrace_symbols_fd(&program_counter, 1, FD); \
ajs239c26f2005-01-17 15:22:28 +0000495 } \
ajs1e221352005-02-03 16:42:40 +0000496 write(FD, buf, s-buf); \
497 backtrace_symbols_fd(array, size, FD); \
ajs59a06a92004-11-23 18:19:14 +0000498}
Paul Jakmafb66b292006-05-28 08:26:15 +0000499#elif defined(HAVE_PRINTSTACK)
500#define DUMP(FD) { \
501 if (program_counter) \
502 write((FD), pclabel, sizeof(pclabel)-1); \
503 write((FD), buf, s-buf); \
504 printstack((FD)); \
505}
506#endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
507
508 s = buf;
509 s = str_append(LOC,"Backtrace for ");
510 s = num_append(LOC,size);
511 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000512
ajsc4c7d0c2005-02-03 19:22:05 +0000513 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
514 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000515 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000516 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000517 else
518 {
ajs274a4a42004-12-07 15:39:31 +0000519 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000520 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000521 /* Remove trailing '\n' for monitor and syslog */
522 *--s = '\0';
523 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
524 vty_log_fixed(buf,s-buf);
525 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
526 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
527 {
528 int i;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700529#ifdef HAVE_GLIBC_BACKTRACE
530 bt = backtrace_symbols(array, size);
531#endif
ajs274a4a42004-12-07 15:39:31 +0000532 /* Just print the function addresses. */
533 for (i = 0; i < size; i++)
534 {
535 s = buf;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700536 if (bt)
537 s = str_append(LOC, bt[i]);
538 else {
539 s = str_append(LOC,"[bt ");
540 s = num_append(LOC,i);
541 s = str_append(LOC,"] 0x");
542 s = hex_append(LOC,(u_long)(array[i]));
543 }
ajs274a4a42004-12-07 15:39:31 +0000544 *s = '\0';
545 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
546 vty_log_fixed(buf,s-buf);
547 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000548 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000549 }
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700550 if (bt)
551 free(bt);
ajs274a4a42004-12-07 15:39:31 +0000552 }
ajs59a06a92004-11-23 18:19:14 +0000553 }
554#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000555#undef LOC
Paul Jakmafb66b292006-05-28 08:26:15 +0000556#endif /* HAVE_STRACK_TRACE */
ajs063ee522004-11-26 18:11:14 +0000557}
558
559void
560zlog_backtrace(int priority)
561{
562#ifndef HAVE_GLIBC_BACKTRACE
563 zlog(NULL, priority, "No backtrace available on this platform.");
564#else
565 void *array[20];
566 int size, i;
567 char **strings;
568
David Lamparter4d474fa2013-11-19 15:00:06 +0100569 size = backtrace(array, array_size(array));
570 if (size <= 0 || (size_t)size > array_size(array))
ajs063ee522004-11-26 18:11:14 +0000571 {
572 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000573 "(valid range is between 1 and %lu)",
Balaji.G837d16c2012-09-26 14:09:10 +0530574 size, (unsigned long)(array_size(array)));
ajs063ee522004-11-26 18:11:14 +0000575 return;
576 }
577 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
578 if (!(strings = backtrace_symbols(array, size)))
579 {
580 zlog_err("Cannot get backtrace symbols (out of memory?)");
581 for (i = 0; i < size; i++)
582 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
583 }
584 else
585 {
586 for (i = 0; i < size; i++)
587 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
588 free(strings);
589 }
590#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000591}
592
paul718e3742002-12-13 20:15:29 +0000593void
594zlog (struct zlog *zl, int priority, const char *format, ...)
595{
ajsd246bd92004-11-23 17:35:08 +0000596 va_list args;
paul718e3742002-12-13 20:15:29 +0000597
ajsd246bd92004-11-23 17:35:08 +0000598 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000599 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000600 va_end (args);
paul718e3742002-12-13 20:15:29 +0000601}
602
ajsd246bd92004-11-23 17:35:08 +0000603#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
604void \
605FUNCNAME(const char *format, ...) \
606{ \
607 va_list args; \
608 va_start(args, format); \
609 vzlog (NULL, PRIORITY, format, args); \
610 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000611}
612
ajsd246bd92004-11-23 17:35:08 +0000613ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000614
ajsd246bd92004-11-23 17:35:08 +0000615ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000616
ajsd246bd92004-11-23 17:35:08 +0000617ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000618
ajsd246bd92004-11-23 17:35:08 +0000619ZLOG_FUNC(zlog_notice, LOG_NOTICE)
620
621ZLOG_FUNC(zlog_debug, LOG_DEBUG)
622
623#undef ZLOG_FUNC
624
625#define PLOG_FUNC(FUNCNAME,PRIORITY) \
626void \
627FUNCNAME(struct zlog *zl, const char *format, ...) \
628{ \
629 va_list args; \
630 va_start(args, format); \
631 vzlog (zl, PRIORITY, format, args); \
632 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000633}
634
ajsd246bd92004-11-23 17:35:08 +0000635PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000636
ajsd246bd92004-11-23 17:35:08 +0000637PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000638
ajsd246bd92004-11-23 17:35:08 +0000639PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000640
ajsd246bd92004-11-23 17:35:08 +0000641PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000642
ajsd246bd92004-11-23 17:35:08 +0000643PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000644
ajsd246bd92004-11-23 17:35:08 +0000645#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000646
David Lamparter615f9f12013-11-18 23:52:02 +0100647void zlog_thread_info (int log_level)
648{
649 if (thread_current)
650 zlog(NULL, log_level, "Current thread function %s, scheduled from "
651 "file %s, line %u", thread_current->funcname,
652 thread_current->schedfrom, thread_current->schedfrom_line);
653 else
654 zlog(NULL, log_level, "Current thread not known/applicable");
655}
656
ajscee3df12004-11-24 17:14:49 +0000657void
658_zlog_assert_failed (const char *assertion, const char *file,
659 unsigned int line, const char *function)
660{
ajsc4c7d0c2005-02-03 19:22:05 +0000661 /* Force fallback file logging? */
662 if (zlog_default && !zlog_default->fp &&
663 ((logfile_fd = open_crashlog()) >= 0) &&
664 ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
665 zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
ajs1e221352005-02-03 16:42:40 +0000666 zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
667 assertion,file,line,(function ? function : "?"));
668 zlog_backtrace(LOG_CRIT);
David Lamparter615f9f12013-11-18 23:52:02 +0100669 zlog_thread_info(LOG_CRIT);
ajscee3df12004-11-24 17:14:49 +0000670 abort();
671}
672
David Lamparter6b0655a2014-06-04 06:53:35 +0200673
paul718e3742002-12-13 20:15:29 +0000674/* Open log stream */
675struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000676openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000677 int syslog_flags, int syslog_facility)
678{
679 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000680 u_int i;
paul718e3742002-12-13 20:15:29 +0000681
ajs274a4a42004-12-07 15:39:31 +0000682 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000683
684 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000685 zl->protocol = protocol;
686 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000687 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000688
ajs274a4a42004-12-07 15:39:31 +0000689 /* Set default logging levels. */
Balaji.G837d16c2012-09-26 14:09:10 +0530690 for (i = 0; i < array_size(zl->maxlvl); i++)
ajs274a4a42004-12-07 15:39:31 +0000691 zl->maxlvl[i] = ZLOG_DISABLED;
692 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
693 zl->default_lvl = LOG_DEBUG;
694
paul718e3742002-12-13 20:15:29 +0000695 openlog (progname, syslog_flags, zl->facility);
696
697 return zl;
698}
699
700void
701closezlog (struct zlog *zl)
702{
703 closelog();
Chris Caputo228da422009-07-18 05:44:03 +0000704
705 if (zl->fp != NULL)
706 fclose (zl->fp);
paul718e3742002-12-13 20:15:29 +0000707
Tom Goff7e69d992010-11-10 13:01:17 -0800708 if (zl->filename != NULL)
709 free (zl->filename);
710
paul718e3742002-12-13 20:15:29 +0000711 XFREE (MTYPE_ZLOG, zl);
712}
713
714/* Called from command.c. */
715void
ajs274a4a42004-12-07 15:39:31 +0000716zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000717{
718 if (zl == NULL)
719 zl = zlog_default;
720
ajs274a4a42004-12-07 15:39:31 +0000721 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000722}
723
724int
ajs274a4a42004-12-07 15:39:31 +0000725zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000726{
727 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000728 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000729
730 /* There is opend file. */
731 zlog_reset_file (zl);
732
733 /* Set default zl. */
734 if (zl == NULL)
735 zl = zlog_default;
736
737 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000738 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000739 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000740 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000741 if (fp == NULL)
742 return 0;
paul718e3742002-12-13 20:15:29 +0000743
744 /* Set flags. */
745 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000746 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000747 zl->fp = fp;
ajsc4c7d0c2005-02-03 19:22:05 +0000748 logfile_fd = fileno(fp);
paul718e3742002-12-13 20:15:29 +0000749
750 return 1;
751}
752
753/* Reset opend file. */
754int
755zlog_reset_file (struct zlog *zl)
756{
757 if (zl == NULL)
758 zl = zlog_default;
759
paul718e3742002-12-13 20:15:29 +0000760 if (zl->fp)
761 fclose (zl->fp);
762 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000763 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000764 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000765
766 if (zl->filename)
767 free (zl->filename);
768 zl->filename = NULL;
769
770 return 1;
771}
772
773/* Reopen log file. */
774int
775zlog_rotate (struct zlog *zl)
776{
ajs274a4a42004-12-07 15:39:31 +0000777 int level;
paul718e3742002-12-13 20:15:29 +0000778
779 if (zl == NULL)
780 zl = zlog_default;
781
782 if (zl->fp)
783 fclose (zl->fp);
784 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000785 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000786 level = zl->maxlvl[ZLOG_DEST_FILE];
787 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000788
789 if (zl->filename)
790 {
gdtaa593d52003-12-22 20:15:53 +0000791 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000792 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000793
794 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000795 zl->fp = fopen (zl->filename, "a");
796 save_errno = errno;
797 umask(oldumask);
798 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000799 {
ajs274a4a42004-12-07 15:39:31 +0000800 zlog_err("Log rotate failed: cannot open file %s for append: %s",
801 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000802 return -1;
803 }
ajsc4c7d0c2005-02-03 19:22:05 +0000804 logfile_fd = fileno(zl->fp);
ajs274a4a42004-12-07 15:39:31 +0000805 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000806 }
807
808 return 1;
809}
David Lamparter6b0655a2014-06-04 06:53:35 +0200810
paul718e3742002-12-13 20:15:29 +0000811/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000812const char *
Stephen Hemminger1423c802008-08-14 17:59:25 +0100813lookup (const struct message *mes, int key)
paul718e3742002-12-13 20:15:29 +0000814{
Stephen Hemminger1423c802008-08-14 17:59:25 +0100815 const struct message *pnt;
paul718e3742002-12-13 20:15:29 +0000816
817 for (pnt = mes; pnt->key != 0; pnt++)
818 if (pnt->key == key)
819 return pnt->str;
820
821 return "";
822}
823
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000824/* Older/faster version of message lookup function, but requires caller to pass
Paul Jakma11486b52008-02-28 23:26:02 +0000825 * in the array size (instead of relying on a 0 key to terminate the search).
826 *
827 * The return value is the message string if found, or the 'none' pointer
828 * provided otherwise.
829 */
hasso8c328f12004-10-05 21:01:23 +0000830const char *
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400831mes_lookup (const struct message *meslist, int max, int index,
832 const char *none, const char *mesname)
paul718e3742002-12-13 20:15:29 +0000833{
Paul Jakma11486b52008-02-28 23:26:02 +0000834 int pos = index - meslist[0].key;
835
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000836 /* first check for best case: index is in range and matches the key
Paul Jakma11486b52008-02-28 23:26:02 +0000837 * value in that slot.
838 * NB: key numbering might be offset from 0. E.g. protocol constants
839 * often start at 1.
840 */
841 if ((pos >= 0) && (pos < max)
842 && (meslist[pos].key == index))
843 return meslist[pos].str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000844
845 /* fall back to linear search */
846 {
847 int i;
848
849 for (i = 0; i < max; i++, meslist++)
850 {
851 if (meslist->key == index)
852 {
Paul Jakma11486b52008-02-28 23:26:02 +0000853 const char *str = (meslist->str ? meslist->str : none);
854
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400855 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
856 index, str, mesname, i, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000857 return str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000858 }
859 }
860 }
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400861 zlog_err("message index %d not found in %s (max is %d)", index, mesname, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000862 assert (none);
863 return none;
paul718e3742002-12-13 20:15:29 +0000864}
ajsca359762004-11-19 23:40:16 +0000865
866/* Wrapper around strerror to handle case where it returns NULL. */
867const char *
868safe_strerror(int errnum)
869{
870 const char *s = strerror(errnum);
871 return (s != NULL) ? s : "Unknown error";
872}
ajsf52d13c2005-10-01 17:38:06 +0000873
Paul Jakmad6d672a2006-05-15 16:56:51 +0000874#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
875static const struct zebra_desc_table command_types[] = {
876 DESC_ENTRY (ZEBRA_INTERFACE_ADD),
877 DESC_ENTRY (ZEBRA_INTERFACE_DELETE),
878 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD),
879 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE),
880 DESC_ENTRY (ZEBRA_INTERFACE_UP),
881 DESC_ENTRY (ZEBRA_INTERFACE_DOWN),
882 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD),
883 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE),
884 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD),
885 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE),
886 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD),
887 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE),
888 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
889 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
890 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP),
891 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP),
892 DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP),
893 DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP),
894 DESC_ENTRY (ZEBRA_INTERFACE_RENAME),
895 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
896 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
897 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
Denis Ovsienko4c783762012-01-21 22:50:19 +0400898 DESC_ENTRY (ZEBRA_HELLO),
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500899 DESC_ENTRY (ZEBRA_NEXTHOP_REGISTER),
900 DESC_ENTRY (ZEBRA_NEXTHOP_UNREGISTER),
901 DESC_ENTRY (ZEBRA_NEXTHOP_UPDATE),
Paul Jakmad6d672a2006-05-15 16:56:51 +0000902};
903#undef DESC_ENTRY
904
905static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
906
907static const struct zebra_desc_table *
ajsf52d13c2005-10-01 17:38:06 +0000908zroute_lookup(u_int zroute)
909{
ajsf52d13c2005-10-01 17:38:06 +0000910 u_int i;
911
Balaji.G837d16c2012-09-26 14:09:10 +0530912 if (zroute >= array_size(route_types))
ajsf52d13c2005-10-01 17:38:06 +0000913 {
914 zlog_err("unknown zebra route type: %u", zroute);
915 return &unknown;
916 }
Paul Jakmad6d672a2006-05-15 16:56:51 +0000917 if (zroute == route_types[zroute].type)
ajsf52d13c2005-10-01 17:38:06 +0000918 return &route_types[zroute];
Balaji.G837d16c2012-09-26 14:09:10 +0530919 for (i = 0; i < array_size(route_types); i++)
ajsf52d13c2005-10-01 17:38:06 +0000920 {
Paul Jakmad6d672a2006-05-15 16:56:51 +0000921 if (zroute == route_types[i].type)
ajsf52d13c2005-10-01 17:38:06 +0000922 {
923 zlog_warn("internal error: route type table out of order "
924 "while searching for %u, please notify developers", zroute);
925 return &route_types[i];
926 }
927 }
928 zlog_err("internal error: cannot find route type %u in table!", zroute);
929 return &unknown;
930}
931
932const char *
933zebra_route_string(u_int zroute)
934{
935 return zroute_lookup(zroute)->string;
936}
937
938char
939zebra_route_char(u_int zroute)
940{
941 return zroute_lookup(zroute)->chr;
942}
Paul Jakmad6d672a2006-05-15 16:56:51 +0000943
944const char *
945zserv_command_string (unsigned int command)
946{
Balaji.G837d16c2012-09-26 14:09:10 +0530947 if (command >= array_size(command_types))
Paul Jakmad6d672a2006-05-15 16:56:51 +0000948 {
949 zlog_err ("unknown zserv command type: %u", command);
950 return unknown.string;
951 }
952 return command_types[command].string;
953}
Paul Jakma7514fb72007-05-02 16:05:35 +0000954
Paul Jakma7514fb72007-05-02 16:05:35 +0000955int
956proto_name2num(const char *s)
957{
958 unsigned i;
959
Balaji.G837d16c2012-09-26 14:09:10 +0530960 for (i=0; i<array_size(route_types); ++i)
Paul Jakma7514fb72007-05-02 16:05:35 +0000961 if (strcasecmp(s, route_types[i].string) == 0)
962 return route_types[i].type;
963 return -1;
964}
David Lampartere0ca5fd2009-09-16 01:52:42 +0200965
David Lampartere0ca5fd2009-09-16 01:52:42 +0200966int
967proto_redistnum(int afi, const char *s)
968{
969 if (! s)
970 return -1;
971
972 if (afi == AFI_IP)
973 {
974 if (strncmp (s, "k", 1) == 0)
975 return ZEBRA_ROUTE_KERNEL;
976 else if (strncmp (s, "c", 1) == 0)
977 return ZEBRA_ROUTE_CONNECT;
978 else if (strncmp (s, "s", 1) == 0)
979 return ZEBRA_ROUTE_STATIC;
980 else if (strncmp (s, "r", 1) == 0)
981 return ZEBRA_ROUTE_RIP;
982 else if (strncmp (s, "o", 1) == 0)
983 return ZEBRA_ROUTE_OSPF;
984 else if (strncmp (s, "i", 1) == 0)
985 return ZEBRA_ROUTE_ISIS;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400986 else if (strncmp (s, "bg", 2) == 0)
David Lampartere0ca5fd2009-09-16 01:52:42 +0200987 return ZEBRA_ROUTE_BGP;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400988 else if (strncmp (s, "ba", 2) == 0)
989 return ZEBRA_ROUTE_BABEL;
Timo Teräsdafa05e2017-01-19 17:27:01 +0200990 else if (strncmp (s, "n", 1) == 0)
991 return ZEBRA_ROUTE_NHRP;
David Lampartere0ca5fd2009-09-16 01:52:42 +0200992 }
993 if (afi == AFI_IP6)
994 {
995 if (strncmp (s, "k", 1) == 0)
996 return ZEBRA_ROUTE_KERNEL;
997 else if (strncmp (s, "c", 1) == 0)
998 return ZEBRA_ROUTE_CONNECT;
999 else if (strncmp (s, "s", 1) == 0)
1000 return ZEBRA_ROUTE_STATIC;
1001 else if (strncmp (s, "r", 1) == 0)
1002 return ZEBRA_ROUTE_RIPNG;
1003 else if (strncmp (s, "o", 1) == 0)
1004 return ZEBRA_ROUTE_OSPF6;
1005 else if (strncmp (s, "i", 1) == 0)
1006 return ZEBRA_ROUTE_ISIS;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +04001007 else if (strncmp (s, "bg", 2) == 0)
David Lampartere0ca5fd2009-09-16 01:52:42 +02001008 return ZEBRA_ROUTE_BGP;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +04001009 else if (strncmp (s, "ba", 2) == 0)
1010 return ZEBRA_ROUTE_BABEL;
Timo Teräsdafa05e2017-01-19 17:27:01 +02001011 else if (strncmp (s, "n", 1) == 0)
1012 return ZEBRA_ROUTE_NHRP;
David Lampartere0ca5fd2009-09-16 01:52:42 +02001013 }
1014 return -1;
1015}
Daniel Waltonc8af6802015-10-21 06:56:44 -07001016
1017void
1018zlog_hexdump (void *mem, unsigned int len) {
1019 unsigned long i = 0;
1020 unsigned int j = 0;
1021 unsigned int columns = 8;
1022 char buf[(len * 4) + ((len/4) * 20) + 30];
1023 char *s = buf;
1024
1025 for (i = 0; i < len + ((len % columns) ? (columns - len % columns) : 0); i++)
1026 {
1027 /* print offset */
1028 if (i % columns == 0)
1029 s += sprintf(s, "0x%016lx: ", (unsigned long)mem + i);
1030
1031 /* print hex data */
1032 if (i < len)
1033 s += sprintf(s, "%02x ", 0xFF & ((char*)mem)[i]);
1034
1035 /* end of block, just aligning for ASCII dump */
1036 else
1037 s += sprintf(s, " ");
1038
1039 /* print ASCII dump */
1040 if (i % columns == (columns - 1))
1041 {
1042 for (j = i - (columns - 1); j <= i; j++)
1043 {
1044 if (j >= len) /* end of block, not really printing */
1045 s += sprintf(s, " ");
1046
Donald Sharp76764dd2015-11-20 09:05:46 -05001047 else if(isprint((int)((char*)mem)[j])) /* printable char */
Daniel Waltonc8af6802015-10-21 06:56:44 -07001048 s += sprintf(s, "%c", 0xFF & ((char*)mem)[j]);
1049
1050 else /* other char */
1051 s += sprintf(s, ".");
1052 }
1053 s += sprintf(s, "\n");
1054 }
1055 }
1056 zlog_debug("\n%s", buf);
1057}