]> git.proxmox.com Git - mirror_frr.git/blame - lib/log.c
lib: Fix vrf_id_t data type
[mirror_frr.git] / lib / log.c
CommitLineData
274a4a44 1/*
274a4a44 2 * Logging of zebra
718e3744 3 * 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
e0ca5fde
DL
23#define QUAGGA_DEFINE_DESC_TABLE
24
718e3744 25#include <zebra.h>
26
27#include "log.h"
28#include "memory.h"
29#include "command.h"
7d149b8e 30#ifndef SUNOS_5
31#include <sys/un.h>
32#endif
30a2231a
PJ
33/* for printstack on solaris */
34#ifdef HAVE_UCONTEXT_H
35#include <ucontext.h>
36#endif
718e3744 37
c4c7d0c4 38static int logfile_fd = -1; /* Used in signal handler. */
1e221354 39
718e3744 40struct 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",
51 "OSPF6",
9e867fe6 52 "ISIS",
12e41d03 53 "PIM",
718e3744 54 "MASC",
55 NULL,
56};
57
58const char *zlog_priority[] =
59{
60 "emergencies",
61 "alerts",
62 "critical",
63 "errors",
64 "warnings",
65 "notifications",
66 "informational",
67 "debugging",
68 NULL,
69};
718e3744 70
7a49a5b5
DS
71/*
72 * write_wrapper
73 *
74 * glibc has declared that the return value from write *must* not be
75 * ignored.
76 * gcc see's this problem and issues a warning for the line.
77 *
78 * Why is this a big deal you say? Because both of them are right
79 * and if you have -Werror enabled then all calls to write
80 * generate a build error and the build stops.
81 *
82 * clang has helpfully allowed this construct:
83 * (void)write(...)
84 * to tell the compiler yeah I know it has a return value
85 * I don't care about it at this time.
86 * gcc doesn't have this ability.
87 *
88 * This code was written such that it didn't care about the
89 * return value from write. At this time do I want
90 * to go through and fix and test this code for correctness.
91 * So just wrapper the bad behavior and move on.
92 */
93static void write_wrapper (int fd, const void *buf, size_t count)
94{
95 if (write (fd, buf, count) <= 0)
96 return;
97
98 return;
99}
6b0655a2 100
718e3744 101/* For time string format. */
718e3744 102
1ed72e0b
AS
103size_t
104quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
718e3744 105{
1ed72e0b
AS
106 static struct {
107 time_t last;
108 size_t len;
109 char buf[28];
110 } cache;
111 struct timeval clock;
112
113 /* would it be sufficient to use global 'recent_time' here? I fear not... */
114 gettimeofday(&clock, NULL);
115
116 /* first, we update the cache if the time has changed */
117 if (cache.last != clock.tv_sec)
118 {
119 struct tm *tm;
120 cache.last = clock.tv_sec;
121 tm = localtime(&cache.last);
122 cache.len = strftime(cache.buf, sizeof(cache.buf),
123 "%Y/%m/%d %H:%M:%S", tm);
124 }
125 /* note: it's not worth caching the subsecond part, because
126 chances are that back-to-back calls are not sufficiently close together
127 for the clock not to have ticked forward */
718e3744 128
1ed72e0b
AS
129 if (buflen > cache.len)
130 {
131 memcpy(buf, cache.buf, cache.len);
132 if ((timestamp_precision > 0) &&
133 (buflen > cache.len+1+timestamp_precision))
134 {
135 /* should we worry about locale issues? */
bcdda30b
AS
136 static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1};
137 int prec;
138 char *p = buf+cache.len+1+(prec = timestamp_precision);
139 *p-- = '\0';
140 while (prec > 6)
141 /* this is unlikely to happen, but protect anyway */
142 {
143 *p-- = '0';
144 prec--;
145 }
146 clock.tv_usec /= divisor[prec];
1ed72e0b
AS
147 do
148 {
bcdda30b
AS
149 *p-- = '0'+(clock.tv_usec % 10);
150 clock.tv_usec /= 10;
1ed72e0b 151 }
bcdda30b
AS
152 while (--prec > 0);
153 *p = '.';
154 return cache.len+1+timestamp_precision;
1ed72e0b
AS
155 }
156 buf[cache.len] = '\0';
157 return cache.len;
158 }
159 if (buflen > 0)
160 buf[0] = '\0';
161 return 0;
162}
718e3744 163
1ed72e0b
AS
164/* Utility routine for current time printing. */
165static void
166time_print(FILE *fp, struct timestamp_control *ctl)
167{
168 if (!ctl->already_rendered)
169 {
170 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
171 ctl->already_rendered = 1;
172 }
173 fprintf(fp, "%s ", ctl->buf);
718e3744 174}
1ed72e0b 175
6b0655a2 176
718e3744 177/* va_list version of zlog. */
d246bd96 178static void
179vzlog (struct zlog *zl, int priority, const char *format, va_list args)
718e3744 180{
7c8ff89e 181 char proto_str[32];
1ed72e0b
AS
182 struct timestamp_control tsctl;
183 tsctl.already_rendered = 0;
184
718e3744 185 /* If zlog is not specified, use default one. */
186 if (zl == NULL)
187 zl = zlog_default;
188
189 /* When zlog_default is also NULL, use stderr for logging. */
190 if (zl == NULL)
191 {
1ed72e0b
AS
192 tsctl.precision = 0;
193 time_print(stderr, &tsctl);
718e3744 194 fprintf (stderr, "%s: ", "unknown");
d246bd96 195 vfprintf (stderr, format, args);
718e3744 196 fprintf (stderr, "\n");
197 fflush (stderr);
198
199 /* In this case we return at here. */
200 return;
201 }
1ed72e0b 202 tsctl.precision = zl->timestamp_precision;
718e3744 203
718e3744 204 /* Syslog output */
274a4a44 205 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
d246bd96 206 {
207 va_list ac;
208 va_copy(ac, args);
209 vsyslog (priority|zlog_default->facility, format, ac);
210 va_end(ac);
211 }
718e3744 212
7c8ff89e
DS
213 if (zl->instance)
214 sprintf (proto_str, "%s[%d]: ", zlog_proto_names[zl->protocol], zl->instance);
215 else
216 sprintf (proto_str, "%s: ", zlog_proto_names[zl->protocol]);
217
718e3744 218 /* File output. */
274a4a44 219 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
718e3744 220 {
d246bd96 221 va_list ac;
1ed72e0b 222 time_print (zl->fp, &tsctl);
b04c699e 223 if (zl->record_priority)
224 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
7c8ff89e 225 fprintf (zl->fp, "%s", proto_str);
d246bd96 226 va_copy(ac, args);
227 vfprintf (zl->fp, format, ac);
228 va_end(ac);
718e3744 229 fprintf (zl->fp, "\n");
230 fflush (zl->fp);
231 }
232
233 /* stdout output. */
274a4a44 234 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
718e3744 235 {
d246bd96 236 va_list ac;
1ed72e0b 237 time_print (stdout, &tsctl);
b04c699e 238 if (zl->record_priority)
239 fprintf (stdout, "%s: ", zlog_priority[priority]);
7c8ff89e 240 fprintf (stdout, "%s", proto_str);
d246bd96 241 va_copy(ac, args);
242 vfprintf (stdout, format, ac);
243 va_end(ac);
718e3744 244 fprintf (stdout, "\n");
245 fflush (stdout);
246 }
247
718e3744 248 /* Terminal monitor. */
274a4a44 249 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
250 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
7c8ff89e 251 proto_str, format, &tsctl, args);
718e3744 252}
253
59a06a91 254static char *
255str_append(char *dst, int len, const char *src)
256{
257 while ((len-- > 0) && *src)
258 *dst++ = *src++;
259 return dst;
260}
261
262static char *
263num_append(char *s, int len, u_long x)
264{
265 char buf[30];
7d149b8e 266 char *t;
59a06a91 267
7d149b8e 268 if (!x)
269 return str_append(s,len,"0");
270 *(t = &buf[sizeof(buf)-1]) = '\0';
59a06a91 271 while (x && (t > buf))
272 {
273 *--t = '0'+(x % 10);
274 x /= 10;
275 }
276 return str_append(s,len,t);
277}
278
fb66b29c 279#if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
7d149b8e 280static char *
281hex_append(char *s, int len, u_long x)
282{
283 char buf[30];
284 char *t;
285
286 if (!x)
287 return str_append(s,len,"0");
288 *(t = &buf[sizeof(buf)-1]) = '\0';
289 while (x && (t > buf))
290 {
291 u_int cc = (x % 16);
292 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
293 x /= 16;
294 }
295 return str_append(s,len,t);
296}
31364274 297#endif
7d149b8e 298
7d149b8e 299/* Needs to be enhanced to support Solaris. */
300static int
301syslog_connect(void)
302{
303#ifdef SUNOS_5
304 return -1;
305#else
306 int fd;
307 char *s;
308 struct sockaddr_un addr;
309
310 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
311 return -1;
312 addr.sun_family = AF_UNIX;
313#ifdef _PATH_LOG
314#define SYSLOG_SOCKET_PATH _PATH_LOG
315#else
316#define SYSLOG_SOCKET_PATH "/dev/log"
317#endif
318 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
319#undef SYSLOG_SOCKET_PATH
320 *s = '\0';
321 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
322 {
323 close(fd);
324 return -1;
325 }
326 return fd;
327#endif
328}
329
330static void
331syslog_sigsafe(int priority, const char *msg, size_t msglen)
332{
1e221354 333 static int syslog_fd = -1;
7d149b8e 334 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
335 char *s;
336
337 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
338 return;
339
340#define LOC s,buf+sizeof(buf)-s
341 s = buf;
342 s = str_append(LOC,"<");
343 s = num_append(LOC,priority);
344 s = str_append(LOC,">");
345 /* forget about the timestamp, too difficult in a signal handler */
346 s = str_append(LOC,zlog_default->ident);
347 if (zlog_default->syslog_options & LOG_PID)
348 {
349 s = str_append(LOC,"[");
350 s = num_append(LOC,getpid());
351 s = str_append(LOC,"]");
352 }
353 s = str_append(LOC,": ");
354 s = str_append(LOC,msg);
7a49a5b5 355 write_wrapper (syslog_fd,buf,s-buf);
7d149b8e 356#undef LOC
357}
358
1e221354 359static int
360open_crashlog(void)
361{
362#define CRASHLOG_PREFIX "/var/tmp/quagga."
363#define CRASHLOG_SUFFIX "crashlog"
364 if (zlog_default && zlog_default->ident)
365 {
366 /* Avoid strlen since it is not async-signal-safe. */
367 const char *p;
368 size_t ilen;
369
370 for (p = zlog_default->ident, ilen = 0; *p; p++)
371 ilen++;
372 {
373 char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3];
374 char *s = buf;
375#define LOC s,buf+sizeof(buf)-s
376 s = str_append(LOC, CRASHLOG_PREFIX);
377 s = str_append(LOC, zlog_default->ident);
378 s = str_append(LOC, ".");
379 s = str_append(LOC, CRASHLOG_SUFFIX);
380#undef LOC
381 *s = '\0';
382 return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK);
383 }
384 }
385 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL,
386 LOGFILE_MASK);
387#undef CRASHLOG_SUFFIX
388#undef CRASHLOG_PREFIX
389}
390
7d149b8e 391/* Note: the goal here is to use only async-signal-safe functions. */
59a06a91 392void
31364274 393zlog_signal(int signo, const char *action
394#ifdef SA_SIGINFO
395 , siginfo_t *siginfo, void *program_counter
396#endif
397 )
59a06a91 398{
399 time_t now;
40abf239 400 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
59a06a91 401 char *s = buf;
7d149b8e 402 char *msgstart = buf;
59a06a91 403#define LOC s,buf+sizeof(buf)-s
404
405 time(&now);
406 if (zlog_default)
407 {
408 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
409 *s++ = ':';
410 *s++ = ' ';
7d149b8e 411 msgstart = s;
59a06a91 412 }
413 s = str_append(LOC,"Received signal ");
414 s = num_append(LOC,signo);
415 s = str_append(LOC," at ");
416 s = num_append(LOC,now);
31364274 417#ifdef SA_SIGINFO
40abf239 418 s = str_append(LOC," (si_addr 0x");
419 s = hex_append(LOC,(u_long)(siginfo->si_addr));
420 if (program_counter)
421 {
422 s = str_append(LOC,", PC 0x");
423 s = hex_append(LOC,(u_long)program_counter);
424 }
425 s = str_append(LOC,"); ");
31364274 426#else /* SA_SIGINFO */
427 s = str_append(LOC,"; ");
428#endif /* SA_SIGINFO */
59a06a91 429 s = str_append(LOC,action);
7d149b8e 430 if (s < buf+sizeof(buf))
431 *s++ = '\n';
59a06a91 432
274a4a44 433 /* N.B. implicit priority is most severe */
1e221354 434#define PRI LOG_CRIT
274a4a44 435
7a49a5b5 436#define DUMP(FD) write_wrapper(FD, buf, s-buf);
1e221354 437 /* If no file logging configured, try to write to fallback log file. */
c4c7d0c4 438 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
439 DUMP(logfile_fd)
59a06a91 440 if (!zlog_default)
c4c7d0c4 441 DUMP(STDERR_FILENO)
59a06a91 442 else
443 {
274a4a44 444 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
c4c7d0c4 445 DUMP(STDOUT_FILENO)
274a4a44 446 /* Remove trailing '\n' for monitor and syslog */
447 *--s = '\0';
448 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
449 vty_log_fixed(buf,s-buf);
450 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
451 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
59a06a91 452 }
453#undef DUMP
454
31364274 455 zlog_backtrace_sigsafe(PRI,
456#ifdef SA_SIGINFO
457 program_counter
458#else
459 NULL
460#endif
461 );
274a4a44 462#undef PRI
063ee52a 463#undef LOC
464}
465
466/* Log a backtrace using only async-signal-safe functions.
467 Needs to be enhanced to support syslog logging. */
468void
239c26fd 469zlog_backtrace_sigsafe(int priority, void *program_counter)
063ee52a 470{
fb66b29c 471#ifdef HAVE_STACK_TRACE
239c26fd 472 static const char pclabel[] = "Program counter: ";
94fc1dd4 473 void *array[64];
063ee52a 474 int size;
475 char buf[100];
94fc1dd4 476 char *s, **bt = NULL;
063ee52a 477#define LOC s,buf+sizeof(buf)-s
59a06a91 478
fb66b29c 479#ifdef HAVE_GLIBC_BACKTRACE
4d474fa3
DL
480 size = backtrace(array, array_size(array));
481 if (size <= 0 || (size_t)size > array_size(array))
063ee52a 482 return;
59a06a91 483
1e221354 484#define DUMP(FD) { \
239c26fd 485 if (program_counter) \
486 { \
7a49a5b5 487 write_wrapper(FD, pclabel, sizeof(pclabel)-1); \
1e221354 488 backtrace_symbols_fd(&program_counter, 1, FD); \
239c26fd 489 } \
7a49a5b5 490 write_wrapper(FD, buf, s-buf); \
1e221354 491 backtrace_symbols_fd(array, size, FD); \
59a06a91 492}
fb66b29c
PJ
493#elif defined(HAVE_PRINTSTACK)
494#define DUMP(FD) { \
495 if (program_counter) \
7a49a5b5
DS
496 write_wrapper((FD), pclabel, sizeof(pclabel)-1); \
497 write_wrapper((FD), buf, s-buf); \
fb66b29c
PJ
498 printstack((FD)); \
499}
500#endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
501
502 s = buf;
503 s = str_append(LOC,"Backtrace for ");
504 s = num_append(LOC,size);
505 s = str_append(LOC," stack frames:\n");
59a06a91 506
c4c7d0c4 507 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
508 DUMP(logfile_fd)
59a06a91 509 if (!zlog_default)
c4c7d0c4 510 DUMP(STDERR_FILENO)
59a06a91 511 else
512 {
274a4a44 513 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
c4c7d0c4 514 DUMP(STDOUT_FILENO)
274a4a44 515 /* Remove trailing '\n' for monitor and syslog */
516 *--s = '\0';
517 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
518 vty_log_fixed(buf,s-buf);
519 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
520 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
521 {
522 int i;
94fc1dd4
SH
523#ifdef HAVE_GLIBC_BACKTRACE
524 bt = backtrace_symbols(array, size);
525#endif
274a4a44 526 /* Just print the function addresses. */
527 for (i = 0; i < size; i++)
528 {
529 s = buf;
94fc1dd4
SH
530 if (bt)
531 s = str_append(LOC, bt[i]);
532 else {
533 s = str_append(LOC,"[bt ");
534 s = num_append(LOC,i);
535 s = str_append(LOC,"] 0x");
536 s = hex_append(LOC,(u_long)(array[i]));
537 }
274a4a44 538 *s = '\0';
539 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
540 vty_log_fixed(buf,s-buf);
541 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
7d149b8e 542 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
274a4a44 543 }
94fc1dd4
SH
544 if (bt)
545 free(bt);
274a4a44 546 }
59a06a91 547 }
548#undef DUMP
59a06a91 549#undef LOC
fb66b29c 550#endif /* HAVE_STRACK_TRACE */
063ee52a 551}
552
553void
554zlog_backtrace(int priority)
555{
556#ifndef HAVE_GLIBC_BACKTRACE
557 zlog(NULL, priority, "No backtrace available on this platform.");
558#else
559 void *array[20];
560 int size, i;
561 char **strings;
562
4d474fa3
DL
563 size = backtrace(array, array_size(array));
564 if (size <= 0 || (size_t)size > array_size(array))
063ee52a 565 {
566 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
1ed72e0b 567 "(valid range is between 1 and %lu)",
837d16cc 568 size, (unsigned long)(array_size(array)));
063ee52a 569 return;
570 }
571 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
572 if (!(strings = backtrace_symbols(array, size)))
573 {
574 zlog_err("Cannot get backtrace symbols (out of memory?)");
575 for (i = 0; i < size; i++)
576 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
577 }
578 else
579 {
580 for (i = 0; i < size; i++)
581 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
582 free(strings);
583 }
584#endif /* HAVE_GLIBC_BACKTRACE */
59a06a91 585}
586
718e3744 587void
588zlog (struct zlog *zl, int priority, const char *format, ...)
589{
d246bd96 590 va_list args;
718e3744 591
d246bd96 592 va_start(args, format);
718e3744 593 vzlog (zl, priority, format, args);
d246bd96 594 va_end (args);
718e3744 595}
596
d246bd96 597#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
598void \
599FUNCNAME(const char *format, ...) \
600{ \
601 va_list args; \
602 va_start(args, format); \
603 vzlog (NULL, PRIORITY, format, args); \
604 va_end(args); \
718e3744 605}
606
d246bd96 607ZLOG_FUNC(zlog_err, LOG_ERR)
718e3744 608
d246bd96 609ZLOG_FUNC(zlog_warn, LOG_WARNING)
718e3744 610
d246bd96 611ZLOG_FUNC(zlog_info, LOG_INFO)
718e3744 612
d246bd96 613ZLOG_FUNC(zlog_notice, LOG_NOTICE)
718e3744 614
d246bd96 615ZLOG_FUNC(zlog_debug, LOG_DEBUG)
718e3744 616
d246bd96 617#undef ZLOG_FUNC
718e3744 618
cee3df1e 619void
620_zlog_assert_failed (const char *assertion, const char *file,
621 unsigned int line, const char *function)
622{
c4c7d0c4 623 /* Force fallback file logging? */
624 if (zlog_default && !zlog_default->fp &&
625 ((logfile_fd = open_crashlog()) >= 0) &&
626 ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
627 zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
1e221354 628 zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
629 assertion,file,line,(function ? function : "?"));
630 zlog_backtrace(LOG_CRIT);
cee3df1e 631 abort();
632}
633
6b0655a2 634
718e3744 635/* Open log stream */
636struct zlog *
7c8ff89e 637openzlog (const char *progname, zlog_proto_t protocol, u_short instance,
718e3744 638 int syslog_flags, int syslog_facility)
639{
640 struct zlog *zl;
274a4a44 641 u_int i;
718e3744 642
274a4a44 643 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
718e3744 644
645 zl->ident = progname;
718e3744 646 zl->protocol = protocol;
7c8ff89e 647 zl->instance = instance;
718e3744 648 zl->facility = syslog_facility;
7d149b8e 649 zl->syslog_options = syslog_flags;
718e3744 650
274a4a44 651 /* Set default logging levels. */
837d16cc 652 for (i = 0; i < array_size(zl->maxlvl); i++)
274a4a44 653 zl->maxlvl[i] = ZLOG_DISABLED;
654 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
655 zl->default_lvl = LOG_DEBUG;
656
718e3744 657 openlog (progname, syslog_flags, zl->facility);
658
659 return zl;
660}
661
662void
663closezlog (struct zlog *zl)
664{
665 closelog();
228da428
CC
666
667 if (zl->fp != NULL)
668 fclose (zl->fp);
718e3744 669
7e69d993 670 if (zl->filename != NULL)
6e919709 671 XFREE(MTYPE_ZLOG, zl->filename);
7e69d993 672
718e3744 673 XFREE (MTYPE_ZLOG, zl);
674}
675
676/* Called from command.c. */
677void
274a4a44 678zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
718e3744 679{
680 if (zl == NULL)
681 zl = zlog_default;
682
274a4a44 683 zl->maxlvl[dest] = log_level;
718e3744 684}
685
686int
274a4a44 687zlog_set_file (struct zlog *zl, const char *filename, int log_level)
718e3744 688{
689 FILE *fp;
aa593d5e 690 mode_t oldumask;
718e3744 691
692 /* There is opend file. */
693 zlog_reset_file (zl);
694
695 /* Set default zl. */
696 if (zl == NULL)
697 zl = zlog_default;
698
699 /* Open file. */
aa593d5e 700 oldumask = umask (0777 & ~LOGFILE_MASK);
718e3744 701 fp = fopen (filename, "a");
aa593d5e 702 umask(oldumask);
274a4a44 703 if (fp == NULL)
704 return 0;
718e3744 705
706 /* Set flags. */
6e919709 707 zl->filename = XSTRDUP(MTYPE_ZLOG, filename);
274a4a44 708 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
718e3744 709 zl->fp = fp;
c4c7d0c4 710 logfile_fd = fileno(fp);
718e3744 711
712 return 1;
713}
714
715/* Reset opend file. */
716int
717zlog_reset_file (struct zlog *zl)
718{
719 if (zl == NULL)
720 zl = zlog_default;
721
718e3744 722 if (zl->fp)
723 fclose (zl->fp);
724 zl->fp = NULL;
c4c7d0c4 725 logfile_fd = -1;
274a4a44 726 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
718e3744 727
728 if (zl->filename)
6e919709 729 XFREE(MTYPE_ZLOG, zl->filename);
718e3744 730 zl->filename = NULL;
731
732 return 1;
733}
734
735/* Reopen log file. */
736int
737zlog_rotate (struct zlog *zl)
738{
274a4a44 739 int level;
718e3744 740
741 if (zl == NULL)
742 zl = zlog_default;
743
744 if (zl->fp)
745 fclose (zl->fp);
746 zl->fp = NULL;
c4c7d0c4 747 logfile_fd = -1;
274a4a44 748 level = zl->maxlvl[ZLOG_DEST_FILE];
749 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
718e3744 750
751 if (zl->filename)
752 {
aa593d5e 753 mode_t oldumask;
274a4a44 754 int save_errno;
aa593d5e 755
756 oldumask = umask (0777 & ~LOGFILE_MASK);
274a4a44 757 zl->fp = fopen (zl->filename, "a");
758 save_errno = errno;
759 umask(oldumask);
760 if (zl->fp == NULL)
aa593d5e 761 {
274a4a44 762 zlog_err("Log rotate failed: cannot open file %s for append: %s",
763 zl->filename, safe_strerror(save_errno));
aa593d5e 764 return -1;
765 }
c4c7d0c4 766 logfile_fd = fileno(zl->fp);
274a4a44 767 zl->maxlvl[ZLOG_DEST_FILE] = level;
718e3744 768 }
769
770 return 1;
771}
6b0655a2 772
718e3744 773/* Message lookup function. */
8c328f11 774const char *
1423c809 775lookup (const struct message *mes, int key)
718e3744 776{
1423c809 777 const struct message *pnt;
718e3744 778
779 for (pnt = mes; pnt->key != 0; pnt++)
780 if (pnt->key == key)
781 return pnt->str;
782
783 return "";
784}
785
afb88a66 786/* Older/faster version of message lookup function, but requires caller to pass
11486b52
PJ
787 * in the array size (instead of relying on a 0 key to terminate the search).
788 *
789 * The return value is the message string if found, or the 'none' pointer
790 * provided otherwise.
791 */
8c328f11 792const char *
51abba50
DT
793mes_lookup (const struct message *meslist, int max, int index,
794 const char *none, const char *mesname)
718e3744 795{
11486b52
PJ
796 int pos = index - meslist[0].key;
797
afb88a66 798 /* first check for best case: index is in range and matches the key
11486b52
PJ
799 * value in that slot.
800 * NB: key numbering might be offset from 0. E.g. protocol constants
801 * often start at 1.
802 */
803 if ((pos >= 0) && (pos < max)
804 && (meslist[pos].key == index))
805 return meslist[pos].str;
afb88a66
AS
806
807 /* fall back to linear search */
808 {
809 int i;
810
811 for (i = 0; i < max; i++, meslist++)
812 {
813 if (meslist->key == index)
814 {
11486b52
PJ
815 const char *str = (meslist->str ? meslist->str : none);
816
51abba50
DT
817 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
818 index, str, mesname, i, max);
11486b52 819 return str;
afb88a66
AS
820 }
821 }
822 }
51abba50 823 zlog_err("message index %d not found in %s (max is %d)", index, mesname, max);
11486b52
PJ
824 assert (none);
825 return none;
718e3744 826}
ca359769 827
828/* Wrapper around strerror to handle case where it returns NULL. */
829const char *
830safe_strerror(int errnum)
831{
832 const char *s = strerror(errnum);
833 return (s != NULL) ? s : "Unknown error";
834}
f52d13cb 835
d6d672aa
PJ
836#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
837static const struct zebra_desc_table command_types[] = {
838 DESC_ENTRY (ZEBRA_INTERFACE_ADD),
839 DESC_ENTRY (ZEBRA_INTERFACE_DELETE),
840 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD),
841 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE),
842 DESC_ENTRY (ZEBRA_INTERFACE_UP),
843 DESC_ENTRY (ZEBRA_INTERFACE_DOWN),
844 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD),
845 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE),
846 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD),
847 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE),
848 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD),
849 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE),
850 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
851 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
852 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP),
853 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP),
854 DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP),
855 DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP),
856 DESC_ENTRY (ZEBRA_INTERFACE_RENAME),
857 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
858 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
859 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
4c78376f 860 DESC_ENTRY (ZEBRA_HELLO),
fb018d25
DS
861 DESC_ENTRY (ZEBRA_NEXTHOP_REGISTER),
862 DESC_ENTRY (ZEBRA_NEXTHOP_UNREGISTER),
863 DESC_ENTRY (ZEBRA_NEXTHOP_UPDATE),
a7f6b2e2 864 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_ADD),
865 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE),
866 DESC_ENTRY (ZEBRA_INTERFACE_BFD_DEST_UPDATE),
867 DESC_ENTRY (ZEBRA_IMPORT_ROUTE_REGISTER),
868 DESC_ENTRY (ZEBRA_IMPORT_ROUTE_UNREGISTER),
078430f6 869 DESC_ENTRY (ZEBRA_IMPORT_CHECK_UPDATE),
a7f6b2e2 870 DESC_ENTRY (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD),
871 DESC_ENTRY (ZEBRA_BFD_DEST_REGISTER),
872 DESC_ENTRY (ZEBRA_BFD_DEST_DEREGISTER),
873 DESC_ENTRY (ZEBRA_BFD_DEST_UPDATE),
874 DESC_ENTRY (ZEBRA_BFD_DEST_REPLAY),
875 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_ADD),
876 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_DEL),
877 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_ADD),
c8e264b6 878 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_DEL),
055c4dfc 879 DESC_ENTRY (ZEBRA_INTERFACE_VRF_UPDATE),
4a04e5f7 880 DESC_ENTRY (ZEBRA_BFD_CLIENT_REGISTER),
881 DESC_ENTRY (ZEBRA_INTERFACE_ENABLE_RADV),
882 DESC_ENTRY (ZEBRA_INTERFACE_DISABLE_RADV)
d6d672aa
PJ
883};
884#undef DESC_ENTRY
885
886static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
887
888static const struct zebra_desc_table *
f52d13cb 889zroute_lookup(u_int zroute)
890{
f52d13cb 891 u_int i;
892
837d16cc 893 if (zroute >= array_size(route_types))
f52d13cb 894 {
895 zlog_err("unknown zebra route type: %u", zroute);
896 return &unknown;
897 }
d6d672aa 898 if (zroute == route_types[zroute].type)
f52d13cb 899 return &route_types[zroute];
837d16cc 900 for (i = 0; i < array_size(route_types); i++)
f52d13cb 901 {
d6d672aa 902 if (zroute == route_types[i].type)
f52d13cb 903 {
904 zlog_warn("internal error: route type table out of order "
905 "while searching for %u, please notify developers", zroute);
906 return &route_types[i];
907 }
908 }
909 zlog_err("internal error: cannot find route type %u in table!", zroute);
910 return &unknown;
911}
912
913const char *
914zebra_route_string(u_int zroute)
915{
916 return zroute_lookup(zroute)->string;
917}
918
919char
920zebra_route_char(u_int zroute)
921{
922 return zroute_lookup(zroute)->chr;
923}
d6d672aa
PJ
924
925const char *
926zserv_command_string (unsigned int command)
927{
837d16cc 928 if (command >= array_size(command_types))
d6d672aa
PJ
929 {
930 zlog_err ("unknown zserv command type: %u", command);
931 return unknown.string;
932 }
933 return command_types[command].string;
934}
7514fb77 935
7514fb77
PJ
936int
937proto_name2num(const char *s)
938{
939 unsigned i;
940
837d16cc 941 for (i=0; i<array_size(route_types); ++i)
7514fb77
PJ
942 if (strcasecmp(s, route_types[i].string) == 0)
943 return route_types[i].type;
944 return -1;
945}
e0ca5fde 946
e0ca5fde
DL
947int
948proto_redistnum(int afi, const char *s)
949{
950 if (! s)
951 return -1;
952
953 if (afi == AFI_IP)
954 {
955 if (strncmp (s, "k", 1) == 0)
956 return ZEBRA_ROUTE_KERNEL;
957 else if (strncmp (s, "c", 1) == 0)
958 return ZEBRA_ROUTE_CONNECT;
959 else if (strncmp (s, "s", 1) == 0)
960 return ZEBRA_ROUTE_STATIC;
961 else if (strncmp (s, "r", 1) == 0)
962 return ZEBRA_ROUTE_RIP;
963 else if (strncmp (s, "o", 1) == 0)
964 return ZEBRA_ROUTE_OSPF;
965 else if (strncmp (s, "i", 1) == 0)
966 return ZEBRA_ROUTE_ISIS;
f8a246d6 967 else if (strncmp (s, "bg", 2) == 0)
e0ca5fde 968 return ZEBRA_ROUTE_BGP;
a48f437a 969 else if (strncmp (s, "ta", 2) == 0)
7a4bb9c5 970 return ZEBRA_ROUTE_TABLE;
e0ca5fde
DL
971 }
972 if (afi == AFI_IP6)
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_RIPNG;
982 else if (strncmp (s, "o", 1) == 0)
983 return ZEBRA_ROUTE_OSPF6;
984 else if (strncmp (s, "i", 1) == 0)
985 return ZEBRA_ROUTE_ISIS;
f8a246d6 986 else if (strncmp (s, "bg", 2) == 0)
e0ca5fde 987 return ZEBRA_ROUTE_BGP;
a48f437a 988 else if (strncmp (s, "ta", 2) == 0)
7a4bb9c5 989 return ZEBRA_ROUTE_TABLE;
e0ca5fde
DL
990 }
991 return -1;
992}
99a6c6cd
DW
993
994void
a9b5cbe5 995zlog_hexdump (const void *mem, unsigned int len) {
99a6c6cd
DW
996 unsigned long i = 0;
997 unsigned int j = 0;
998 unsigned int columns = 8;
999 char buf[(len * 4) + ((len/4) * 20) + 30];
1000 char *s = buf;
1001
1002 for (i = 0; i < len + ((len % columns) ? (columns - len % columns) : 0); i++)
1003 {
1004 /* print offset */
1005 if (i % columns == 0)
1006 s += sprintf(s, "0x%016lx: ", (unsigned long)mem + i);
1007
1008 /* print hex data */
1009 if (i < len)
a9b5cbe5 1010 s += sprintf(s, "%02x ", 0xFF & ((const char*)mem)[i]);
99a6c6cd
DW
1011
1012 /* end of block, just aligning for ASCII dump */
1013 else
1014 s += sprintf(s, " ");
1015
1016 /* print ASCII dump */
1017 if (i % columns == (columns - 1))
1018 {
1019 for (j = i - (columns - 1); j <= i; j++)
1020 {
1021 if (j >= len) /* end of block, not really printing */
1022 s += sprintf(s, " ");
1023
a9b5cbe5
DS
1024 else if(isprint((int)((const char *)mem)[j])) /* printable char */
1025 s += sprintf(s, "%c", 0xFF & ((const char *)mem)[j]);
99a6c6cd
DW
1026
1027 else /* other char */
1028 s += sprintf(s, ".");
1029 }
1030 s += sprintf(s, "\n");
1031 }
1032 }
1033 zlog_debug("\n%s", buf);
1034}