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