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