]> git.proxmox.com Git - mirror_frr.git/blame - lib/log.c
lib: move memory.[ch] out of the way
[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];
d8efb772 182 int original_errno = errno;
1ed72e0b
AS
183 struct timestamp_control tsctl;
184 tsctl.already_rendered = 0;
185
718e3744 186 /* If zlog is not specified, use default one. */
187 if (zl == NULL)
188 zl = zlog_default;
189
190 /* When zlog_default is also NULL, use stderr for logging. */
191 if (zl == NULL)
192 {
1ed72e0b
AS
193 tsctl.precision = 0;
194 time_print(stderr, &tsctl);
718e3744 195 fprintf (stderr, "%s: ", "unknown");
d246bd96 196 vfprintf (stderr, format, args);
718e3744 197 fprintf (stderr, "\n");
198 fflush (stderr);
199
200 /* In this case we return at here. */
d8efb772 201 errno = original_errno;
718e3744 202 return;
203 }
1ed72e0b 204 tsctl.precision = zl->timestamp_precision;
718e3744 205
718e3744 206 /* Syslog output */
274a4a44 207 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
d246bd96 208 {
209 va_list ac;
210 va_copy(ac, args);
211 vsyslog (priority|zlog_default->facility, format, ac);
212 va_end(ac);
213 }
718e3744 214
7c8ff89e
DS
215 if (zl->instance)
216 sprintf (proto_str, "%s[%d]: ", zlog_proto_names[zl->protocol], zl->instance);
217 else
218 sprintf (proto_str, "%s: ", zlog_proto_names[zl->protocol]);
219
718e3744 220 /* File output. */
274a4a44 221 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
718e3744 222 {
d246bd96 223 va_list ac;
1ed72e0b 224 time_print (zl->fp, &tsctl);
b04c699e 225 if (zl->record_priority)
226 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
7c8ff89e 227 fprintf (zl->fp, "%s", proto_str);
d246bd96 228 va_copy(ac, args);
229 vfprintf (zl->fp, format, ac);
230 va_end(ac);
718e3744 231 fprintf (zl->fp, "\n");
232 fflush (zl->fp);
233 }
234
235 /* stdout output. */
274a4a44 236 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
718e3744 237 {
d246bd96 238 va_list ac;
1ed72e0b 239 time_print (stdout, &tsctl);
b04c699e 240 if (zl->record_priority)
241 fprintf (stdout, "%s: ", zlog_priority[priority]);
7c8ff89e 242 fprintf (stdout, "%s", proto_str);
d246bd96 243 va_copy(ac, args);
244 vfprintf (stdout, format, ac);
245 va_end(ac);
718e3744 246 fprintf (stdout, "\n");
247 fflush (stdout);
248 }
249
718e3744 250 /* Terminal monitor. */
274a4a44 251 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
252 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
7c8ff89e 253 proto_str, format, &tsctl, args);
d8efb772
CF
254
255 errno = original_errno;
718e3744 256}
257
59a06a91 258static char *
259str_append(char *dst, int len, const char *src)
260{
261 while ((len-- > 0) && *src)
262 *dst++ = *src++;
263 return dst;
264}
265
266static char *
267num_append(char *s, int len, u_long x)
268{
269 char buf[30];
7d149b8e 270 char *t;
59a06a91 271
7d149b8e 272 if (!x)
273 return str_append(s,len,"0");
274 *(t = &buf[sizeof(buf)-1]) = '\0';
59a06a91 275 while (x && (t > buf))
276 {
277 *--t = '0'+(x % 10);
278 x /= 10;
279 }
280 return str_append(s,len,t);
281}
282
fb66b29c 283#if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
7d149b8e 284static char *
285hex_append(char *s, int len, u_long x)
286{
287 char buf[30];
288 char *t;
289
290 if (!x)
291 return str_append(s,len,"0");
292 *(t = &buf[sizeof(buf)-1]) = '\0';
293 while (x && (t > buf))
294 {
295 u_int cc = (x % 16);
296 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
297 x /= 16;
298 }
299 return str_append(s,len,t);
300}
31364274 301#endif
7d149b8e 302
7d149b8e 303/* Needs to be enhanced to support Solaris. */
304static int
305syslog_connect(void)
306{
307#ifdef SUNOS_5
308 return -1;
309#else
310 int fd;
311 char *s;
312 struct sockaddr_un addr;
313
314 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
315 return -1;
316 addr.sun_family = AF_UNIX;
317#ifdef _PATH_LOG
318#define SYSLOG_SOCKET_PATH _PATH_LOG
319#else
320#define SYSLOG_SOCKET_PATH "/dev/log"
321#endif
322 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
323#undef SYSLOG_SOCKET_PATH
324 *s = '\0';
325 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
326 {
327 close(fd);
328 return -1;
329 }
330 return fd;
331#endif
332}
333
334static void
335syslog_sigsafe(int priority, const char *msg, size_t msglen)
336{
1e221354 337 static int syslog_fd = -1;
7d149b8e 338 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
339 char *s;
340
341 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
342 return;
343
344#define LOC s,buf+sizeof(buf)-s
345 s = buf;
346 s = str_append(LOC,"<");
347 s = num_append(LOC,priority);
348 s = str_append(LOC,">");
349 /* forget about the timestamp, too difficult in a signal handler */
350 s = str_append(LOC,zlog_default->ident);
351 if (zlog_default->syslog_options & LOG_PID)
352 {
353 s = str_append(LOC,"[");
354 s = num_append(LOC,getpid());
355 s = str_append(LOC,"]");
356 }
357 s = str_append(LOC,": ");
358 s = str_append(LOC,msg);
7a49a5b5 359 write_wrapper (syslog_fd,buf,s-buf);
7d149b8e 360#undef LOC
361}
362
1e221354 363static int
364open_crashlog(void)
365{
366#define CRASHLOG_PREFIX "/var/tmp/quagga."
367#define CRASHLOG_SUFFIX "crashlog"
368 if (zlog_default && zlog_default->ident)
369 {
370 /* Avoid strlen since it is not async-signal-safe. */
371 const char *p;
372 size_t ilen;
373
374 for (p = zlog_default->ident, ilen = 0; *p; p++)
375 ilen++;
376 {
377 char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3];
378 char *s = buf;
379#define LOC s,buf+sizeof(buf)-s
380 s = str_append(LOC, CRASHLOG_PREFIX);
381 s = str_append(LOC, zlog_default->ident);
382 s = str_append(LOC, ".");
383 s = str_append(LOC, CRASHLOG_SUFFIX);
384#undef LOC
385 *s = '\0';
386 return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK);
387 }
388 }
389 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL,
390 LOGFILE_MASK);
391#undef CRASHLOG_SUFFIX
392#undef CRASHLOG_PREFIX
393}
394
7d149b8e 395/* Note: the goal here is to use only async-signal-safe functions. */
59a06a91 396void
31364274 397zlog_signal(int signo, const char *action
398#ifdef SA_SIGINFO
399 , siginfo_t *siginfo, void *program_counter
400#endif
401 )
59a06a91 402{
403 time_t now;
40abf239 404 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
59a06a91 405 char *s = buf;
7d149b8e 406 char *msgstart = buf;
59a06a91 407#define LOC s,buf+sizeof(buf)-s
408
409 time(&now);
410 if (zlog_default)
411 {
412 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
413 *s++ = ':';
414 *s++ = ' ';
7d149b8e 415 msgstart = s;
59a06a91 416 }
417 s = str_append(LOC,"Received signal ");
418 s = num_append(LOC,signo);
419 s = str_append(LOC," at ");
420 s = num_append(LOC,now);
31364274 421#ifdef SA_SIGINFO
40abf239 422 s = str_append(LOC," (si_addr 0x");
423 s = hex_append(LOC,(u_long)(siginfo->si_addr));
424 if (program_counter)
425 {
426 s = str_append(LOC,", PC 0x");
427 s = hex_append(LOC,(u_long)program_counter);
428 }
429 s = str_append(LOC,"); ");
31364274 430#else /* SA_SIGINFO */
431 s = str_append(LOC,"; ");
432#endif /* SA_SIGINFO */
59a06a91 433 s = str_append(LOC,action);
7d149b8e 434 if (s < buf+sizeof(buf))
435 *s++ = '\n';
59a06a91 436
274a4a44 437 /* N.B. implicit priority is most severe */
1e221354 438#define PRI LOG_CRIT
274a4a44 439
7a49a5b5 440#define DUMP(FD) write_wrapper(FD, buf, s-buf);
1e221354 441 /* If no file logging configured, try to write to fallback log file. */
c4c7d0c4 442 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
443 DUMP(logfile_fd)
59a06a91 444 if (!zlog_default)
c4c7d0c4 445 DUMP(STDERR_FILENO)
59a06a91 446 else
447 {
274a4a44 448 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
c4c7d0c4 449 DUMP(STDOUT_FILENO)
274a4a44 450 /* Remove trailing '\n' for monitor and syslog */
451 *--s = '\0';
452 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
453 vty_log_fixed(buf,s-buf);
454 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
455 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
59a06a91 456 }
457#undef DUMP
458
31364274 459 zlog_backtrace_sigsafe(PRI,
460#ifdef SA_SIGINFO
461 program_counter
462#else
463 NULL
464#endif
465 );
d1265948
DL
466
467 s = buf;
468 if (!thread_current)
469 s = str_append (LOC, "no thread information available\n");
470 else
471 {
472 s = str_append (LOC, "in thread ");
473 s = str_append (LOC, thread_current->funcname);
474 s = str_append (LOC, " scheduled from ");
475 s = str_append (LOC, thread_current->schedfrom);
476 s = str_append (LOC, ":");
477 s = num_append (LOC, thread_current->schedfrom_line);
478 s = str_append (LOC, "\n");
479 }
480
69f30024 481#define DUMP(FD) write_wrapper(FD, buf, s-buf);
d1265948
DL
482 /* If no file logging configured, try to write to fallback log file. */
483 if (logfile_fd >= 0)
484 DUMP(logfile_fd)
485 if (!zlog_default)
486 DUMP(STDERR_FILENO)
487 else
488 {
489 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
490 DUMP(STDOUT_FILENO)
491 /* Remove trailing '\n' for monitor and syslog */
492 *--s = '\0';
493 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
494 vty_log_fixed(buf,s-buf);
495 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
496 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
497 }
498#undef DUMP
499
274a4a44 500#undef PRI
063ee52a 501#undef LOC
502}
503
504/* Log a backtrace using only async-signal-safe functions.
505 Needs to be enhanced to support syslog logging. */
506void
239c26fd 507zlog_backtrace_sigsafe(int priority, void *program_counter)
063ee52a 508{
fb66b29c 509#ifdef HAVE_STACK_TRACE
239c26fd 510 static const char pclabel[] = "Program counter: ";
94fc1dd4 511 void *array[64];
063ee52a 512 int size;
513 char buf[100];
94fc1dd4 514 char *s, **bt = NULL;
063ee52a 515#define LOC s,buf+sizeof(buf)-s
59a06a91 516
fb66b29c 517#ifdef HAVE_GLIBC_BACKTRACE
4d474fa3
DL
518 size = backtrace(array, array_size(array));
519 if (size <= 0 || (size_t)size > array_size(array))
063ee52a 520 return;
59a06a91 521
1e221354 522#define DUMP(FD) { \
239c26fd 523 if (program_counter) \
524 { \
7a49a5b5 525 write_wrapper(FD, pclabel, sizeof(pclabel)-1); \
1e221354 526 backtrace_symbols_fd(&program_counter, 1, FD); \
239c26fd 527 } \
7a49a5b5 528 write_wrapper(FD, buf, s-buf); \
1e221354 529 backtrace_symbols_fd(array, size, FD); \
59a06a91 530}
fb66b29c
PJ
531#elif defined(HAVE_PRINTSTACK)
532#define DUMP(FD) { \
533 if (program_counter) \
7a49a5b5
DS
534 write_wrapper((FD), pclabel, sizeof(pclabel)-1); \
535 write_wrapper((FD), buf, s-buf); \
fb66b29c
PJ
536 printstack((FD)); \
537}
538#endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
539
540 s = buf;
541 s = str_append(LOC,"Backtrace for ");
542 s = num_append(LOC,size);
543 s = str_append(LOC," stack frames:\n");
59a06a91 544
c4c7d0c4 545 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
546 DUMP(logfile_fd)
59a06a91 547 if (!zlog_default)
c4c7d0c4 548 DUMP(STDERR_FILENO)
59a06a91 549 else
550 {
274a4a44 551 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
c4c7d0c4 552 DUMP(STDOUT_FILENO)
274a4a44 553 /* Remove trailing '\n' for monitor and syslog */
554 *--s = '\0';
555 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
556 vty_log_fixed(buf,s-buf);
557 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
558 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
559 {
560 int i;
94fc1dd4
SH
561#ifdef HAVE_GLIBC_BACKTRACE
562 bt = backtrace_symbols(array, size);
563#endif
274a4a44 564 /* Just print the function addresses. */
565 for (i = 0; i < size; i++)
566 {
567 s = buf;
94fc1dd4
SH
568 if (bt)
569 s = str_append(LOC, bt[i]);
570 else {
571 s = str_append(LOC,"[bt ");
572 s = num_append(LOC,i);
573 s = str_append(LOC,"] 0x");
574 s = hex_append(LOC,(u_long)(array[i]));
575 }
274a4a44 576 *s = '\0';
577 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
578 vty_log_fixed(buf,s-buf);
579 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
7d149b8e 580 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
274a4a44 581 }
94fc1dd4
SH
582 if (bt)
583 free(bt);
274a4a44 584 }
59a06a91 585 }
586#undef DUMP
59a06a91 587#undef LOC
fb66b29c 588#endif /* HAVE_STRACK_TRACE */
063ee52a 589}
590
591void
592zlog_backtrace(int priority)
593{
594#ifndef HAVE_GLIBC_BACKTRACE
595 zlog(NULL, priority, "No backtrace available on this platform.");
596#else
597 void *array[20];
598 int size, i;
599 char **strings;
600
4d474fa3
DL
601 size = backtrace(array, array_size(array));
602 if (size <= 0 || (size_t)size > array_size(array))
063ee52a 603 {
604 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
1ed72e0b 605 "(valid range is between 1 and %lu)",
837d16cc 606 size, (unsigned long)(array_size(array)));
063ee52a 607 return;
608 }
609 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
610 if (!(strings = backtrace_symbols(array, size)))
611 {
612 zlog_err("Cannot get backtrace symbols (out of memory?)");
613 for (i = 0; i < size; i++)
614 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
615 }
616 else
617 {
618 for (i = 0; i < size; i++)
619 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
620 free(strings);
621 }
622#endif /* HAVE_GLIBC_BACKTRACE */
59a06a91 623}
624
718e3744 625void
626zlog (struct zlog *zl, int priority, const char *format, ...)
627{
d246bd96 628 va_list args;
718e3744 629
d246bd96 630 va_start(args, format);
718e3744 631 vzlog (zl, priority, format, args);
d246bd96 632 va_end (args);
718e3744 633}
634
d246bd96 635#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
636void \
637FUNCNAME(const char *format, ...) \
638{ \
639 va_list args; \
640 va_start(args, format); \
641 vzlog (NULL, PRIORITY, format, args); \
642 va_end(args); \
718e3744 643}
644
d246bd96 645ZLOG_FUNC(zlog_err, LOG_ERR)
718e3744 646
d246bd96 647ZLOG_FUNC(zlog_warn, LOG_WARNING)
718e3744 648
d246bd96 649ZLOG_FUNC(zlog_info, LOG_INFO)
718e3744 650
d246bd96 651ZLOG_FUNC(zlog_notice, LOG_NOTICE)
718e3744 652
d246bd96 653ZLOG_FUNC(zlog_debug, LOG_DEBUG)
718e3744 654
d246bd96 655#undef ZLOG_FUNC
718e3744 656
d1265948
DL
657void zlog_thread_info (int log_level)
658{
659 if (thread_current)
660 zlog(NULL, log_level, "Current thread function %s, scheduled from "
661 "file %s, line %u", thread_current->funcname,
662 thread_current->schedfrom, thread_current->schedfrom_line);
663 else
664 zlog(NULL, log_level, "Current thread not known/applicable");
665}
666
cee3df1e 667void
668_zlog_assert_failed (const char *assertion, const char *file,
669 unsigned int line, const char *function)
670{
c4c7d0c4 671 /* Force fallback file logging? */
672 if (zlog_default && !zlog_default->fp &&
673 ((logfile_fd = open_crashlog()) >= 0) &&
674 ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
675 zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
1e221354 676 zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
677 assertion,file,line,(function ? function : "?"));
678 zlog_backtrace(LOG_CRIT);
d1265948 679 zlog_thread_info(LOG_CRIT);
cee3df1e 680 abort();
681}
682
6b0655a2 683
718e3744 684/* Open log stream */
685struct zlog *
7c8ff89e 686openzlog (const char *progname, zlog_proto_t protocol, u_short instance,
718e3744 687 int syslog_flags, int syslog_facility)
688{
689 struct zlog *zl;
274a4a44 690 u_int i;
718e3744 691
274a4a44 692 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
718e3744 693
694 zl->ident = progname;
718e3744 695 zl->protocol = protocol;
7c8ff89e 696 zl->instance = instance;
718e3744 697 zl->facility = syslog_facility;
7d149b8e 698 zl->syslog_options = syslog_flags;
718e3744 699
274a4a44 700 /* Set default logging levels. */
837d16cc 701 for (i = 0; i < array_size(zl->maxlvl); i++)
274a4a44 702 zl->maxlvl[i] = ZLOG_DISABLED;
703 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
704 zl->default_lvl = LOG_DEBUG;
705
718e3744 706 openlog (progname, syslog_flags, zl->facility);
707
708 return zl;
709}
710
711void
712closezlog (struct zlog *zl)
713{
714 closelog();
228da428
CC
715
716 if (zl->fp != NULL)
717 fclose (zl->fp);
718e3744 718
7e69d993 719 if (zl->filename != NULL)
6e919709 720 XFREE(MTYPE_ZLOG, zl->filename);
7e69d993 721
718e3744 722 XFREE (MTYPE_ZLOG, zl);
723}
724
725/* Called from command.c. */
726void
274a4a44 727zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
718e3744 728{
729 if (zl == NULL)
730 zl = zlog_default;
731
274a4a44 732 zl->maxlvl[dest] = log_level;
718e3744 733}
734
735int
274a4a44 736zlog_set_file (struct zlog *zl, const char *filename, int log_level)
718e3744 737{
738 FILE *fp;
aa593d5e 739 mode_t oldumask;
718e3744 740
741 /* There is opend file. */
742 zlog_reset_file (zl);
743
744 /* Set default zl. */
745 if (zl == NULL)
746 zl = zlog_default;
747
748 /* Open file. */
aa593d5e 749 oldumask = umask (0777 & ~LOGFILE_MASK);
718e3744 750 fp = fopen (filename, "a");
aa593d5e 751 umask(oldumask);
274a4a44 752 if (fp == NULL)
753 return 0;
718e3744 754
755 /* Set flags. */
6e919709 756 zl->filename = XSTRDUP(MTYPE_ZLOG, filename);
274a4a44 757 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
718e3744 758 zl->fp = fp;
c4c7d0c4 759 logfile_fd = fileno(fp);
718e3744 760
761 return 1;
762}
763
764/* Reset opend file. */
765int
766zlog_reset_file (struct zlog *zl)
767{
768 if (zl == NULL)
769 zl = zlog_default;
770
718e3744 771 if (zl->fp)
772 fclose (zl->fp);
773 zl->fp = NULL;
c4c7d0c4 774 logfile_fd = -1;
274a4a44 775 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
718e3744 776
777 if (zl->filename)
6e919709 778 XFREE(MTYPE_ZLOG, zl->filename);
718e3744 779 zl->filename = NULL;
780
781 return 1;
782}
783
784/* Reopen log file. */
785int
786zlog_rotate (struct zlog *zl)
787{
274a4a44 788 int level;
718e3744 789
790 if (zl == NULL)
791 zl = zlog_default;
792
793 if (zl->fp)
794 fclose (zl->fp);
795 zl->fp = NULL;
c4c7d0c4 796 logfile_fd = -1;
274a4a44 797 level = zl->maxlvl[ZLOG_DEST_FILE];
798 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
718e3744 799
800 if (zl->filename)
801 {
aa593d5e 802 mode_t oldumask;
274a4a44 803 int save_errno;
aa593d5e 804
805 oldumask = umask (0777 & ~LOGFILE_MASK);
274a4a44 806 zl->fp = fopen (zl->filename, "a");
807 save_errno = errno;
808 umask(oldumask);
809 if (zl->fp == NULL)
aa593d5e 810 {
274a4a44 811 zlog_err("Log rotate failed: cannot open file %s for append: %s",
812 zl->filename, safe_strerror(save_errno));
aa593d5e 813 return -1;
814 }
c4c7d0c4 815 logfile_fd = fileno(zl->fp);
274a4a44 816 zl->maxlvl[ZLOG_DEST_FILE] = level;
718e3744 817 }
818
819 return 1;
820}
6b0655a2 821
718e3744 822/* Message lookup function. */
8c328f11 823const char *
1423c809 824lookup (const struct message *mes, int key)
718e3744 825{
1423c809 826 const struct message *pnt;
718e3744 827
828 for (pnt = mes; pnt->key != 0; pnt++)
829 if (pnt->key == key)
830 return pnt->str;
831
832 return "";
833}
834
afb88a66 835/* Older/faster version of message lookup function, but requires caller to pass
11486b52
PJ
836 * in the array size (instead of relying on a 0 key to terminate the search).
837 *
838 * The return value is the message string if found, or the 'none' pointer
839 * provided otherwise.
840 */
8c328f11 841const char *
51abba50
DT
842mes_lookup (const struct message *meslist, int max, int index,
843 const char *none, const char *mesname)
718e3744 844{
11486b52
PJ
845 int pos = index - meslist[0].key;
846
afb88a66 847 /* first check for best case: index is in range and matches the key
11486b52
PJ
848 * value in that slot.
849 * NB: key numbering might be offset from 0. E.g. protocol constants
850 * often start at 1.
851 */
852 if ((pos >= 0) && (pos < max)
853 && (meslist[pos].key == index))
854 return meslist[pos].str;
afb88a66
AS
855
856 /* fall back to linear search */
857 {
858 int i;
859
860 for (i = 0; i < max; i++, meslist++)
861 {
862 if (meslist->key == index)
863 {
11486b52
PJ
864 const char *str = (meslist->str ? meslist->str : none);
865
51abba50
DT
866 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
867 index, str, mesname, i, max);
11486b52 868 return str;
afb88a66
AS
869 }
870 }
871 }
51abba50 872 zlog_err("message index %d not found in %s (max is %d)", index, mesname, max);
11486b52
PJ
873 assert (none);
874 return none;
718e3744 875}
ca359769 876
877/* Wrapper around strerror to handle case where it returns NULL. */
878const char *
879safe_strerror(int errnum)
880{
881 const char *s = strerror(errnum);
882 return (s != NULL) ? s : "Unknown error";
883}
f52d13cb 884
d6d672aa
PJ
885#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
886static const struct zebra_desc_table command_types[] = {
887 DESC_ENTRY (ZEBRA_INTERFACE_ADD),
888 DESC_ENTRY (ZEBRA_INTERFACE_DELETE),
889 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD),
890 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE),
891 DESC_ENTRY (ZEBRA_INTERFACE_UP),
892 DESC_ENTRY (ZEBRA_INTERFACE_DOWN),
893 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD),
894 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE),
895 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD),
896 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE),
897 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD),
898 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE),
899 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
900 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
d6d672aa
PJ
901 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
902 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
903 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
4c78376f 904 DESC_ENTRY (ZEBRA_HELLO),
fb018d25
DS
905 DESC_ENTRY (ZEBRA_NEXTHOP_REGISTER),
906 DESC_ENTRY (ZEBRA_NEXTHOP_UNREGISTER),
907 DESC_ENTRY (ZEBRA_NEXTHOP_UPDATE),
a7f6b2e2 908 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_ADD),
909 DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE),
910 DESC_ENTRY (ZEBRA_INTERFACE_BFD_DEST_UPDATE),
911 DESC_ENTRY (ZEBRA_IMPORT_ROUTE_REGISTER),
912 DESC_ENTRY (ZEBRA_IMPORT_ROUTE_UNREGISTER),
078430f6 913 DESC_ENTRY (ZEBRA_IMPORT_CHECK_UPDATE),
a7f6b2e2 914 DESC_ENTRY (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD),
915 DESC_ENTRY (ZEBRA_BFD_DEST_REGISTER),
916 DESC_ENTRY (ZEBRA_BFD_DEST_DEREGISTER),
917 DESC_ENTRY (ZEBRA_BFD_DEST_UPDATE),
918 DESC_ENTRY (ZEBRA_BFD_DEST_REPLAY),
919 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_ADD),
920 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_DEL),
921 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_ADD),
c8e264b6 922 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_DEL),
93f7342f
DS
923 DESC_ENTRY (ZEBRA_VRF_UNREGISTER),
924 DESC_ENTRY (ZEBRA_VRF_ADD),
925 DESC_ENTRY (ZEBRA_VRF_DELETE),
055c4dfc 926 DESC_ENTRY (ZEBRA_INTERFACE_VRF_UPDATE),
4a04e5f7 927 DESC_ENTRY (ZEBRA_BFD_CLIENT_REGISTER),
928 DESC_ENTRY (ZEBRA_INTERFACE_ENABLE_RADV),
93f7342f
DS
929 DESC_ENTRY (ZEBRA_INTERFACE_DISABLE_RADV),
930 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB),
d6d672aa
PJ
931};
932#undef DESC_ENTRY
933
934static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
935
936static const struct zebra_desc_table *
f52d13cb 937zroute_lookup(u_int zroute)
938{
f52d13cb 939 u_int i;
940
837d16cc 941 if (zroute >= array_size(route_types))
f52d13cb 942 {
943 zlog_err("unknown zebra route type: %u", zroute);
944 return &unknown;
945 }
d6d672aa 946 if (zroute == route_types[zroute].type)
f52d13cb 947 return &route_types[zroute];
837d16cc 948 for (i = 0; i < array_size(route_types); i++)
f52d13cb 949 {
d6d672aa 950 if (zroute == route_types[i].type)
f52d13cb 951 {
952 zlog_warn("internal error: route type table out of order "
953 "while searching for %u, please notify developers", zroute);
954 return &route_types[i];
955 }
956 }
957 zlog_err("internal error: cannot find route type %u in table!", zroute);
958 return &unknown;
959}
960
961const char *
962zebra_route_string(u_int zroute)
963{
964 return zroute_lookup(zroute)->string;
965}
966
967char
968zebra_route_char(u_int zroute)
969{
970 return zroute_lookup(zroute)->chr;
971}
d6d672aa
PJ
972
973const char *
974zserv_command_string (unsigned int command)
975{
837d16cc 976 if (command >= array_size(command_types))
d6d672aa
PJ
977 {
978 zlog_err ("unknown zserv command type: %u", command);
979 return unknown.string;
980 }
981 return command_types[command].string;
982}
7514fb77 983
7514fb77
PJ
984int
985proto_name2num(const char *s)
986{
987 unsigned i;
988
837d16cc 989 for (i=0; i<array_size(route_types); ++i)
7514fb77
PJ
990 if (strcasecmp(s, route_types[i].string) == 0)
991 return route_types[i].type;
992 return -1;
993}
e0ca5fde 994
e0ca5fde
DL
995int
996proto_redistnum(int afi, const char *s)
997{
998 if (! s)
999 return -1;
1000
1001 if (afi == AFI_IP)
1002 {
1003 if (strncmp (s, "k", 1) == 0)
1004 return ZEBRA_ROUTE_KERNEL;
1005 else if (strncmp (s, "c", 1) == 0)
1006 return ZEBRA_ROUTE_CONNECT;
1007 else if (strncmp (s, "s", 1) == 0)
1008 return ZEBRA_ROUTE_STATIC;
1009 else if (strncmp (s, "r", 1) == 0)
1010 return ZEBRA_ROUTE_RIP;
1011 else if (strncmp (s, "o", 1) == 0)
1012 return ZEBRA_ROUTE_OSPF;
1013 else if (strncmp (s, "i", 1) == 0)
1014 return ZEBRA_ROUTE_ISIS;
f8a246d6 1015 else if (strncmp (s, "bg", 2) == 0)
e0ca5fde 1016 return ZEBRA_ROUTE_BGP;
a48f437a 1017 else if (strncmp (s, "ta", 2) == 0)
7a4bb9c5 1018 return ZEBRA_ROUTE_TABLE;
e0ca5fde
DL
1019 }
1020 if (afi == AFI_IP6)
1021 {
1022 if (strncmp (s, "k", 1) == 0)
1023 return ZEBRA_ROUTE_KERNEL;
1024 else if (strncmp (s, "c", 1) == 0)
1025 return ZEBRA_ROUTE_CONNECT;
1026 else if (strncmp (s, "s", 1) == 0)
1027 return ZEBRA_ROUTE_STATIC;
1028 else if (strncmp (s, "r", 1) == 0)
1029 return ZEBRA_ROUTE_RIPNG;
1030 else if (strncmp (s, "o", 1) == 0)
1031 return ZEBRA_ROUTE_OSPF6;
1032 else if (strncmp (s, "i", 1) == 0)
1033 return ZEBRA_ROUTE_ISIS;
f8a246d6 1034 else if (strncmp (s, "bg", 2) == 0)
e0ca5fde 1035 return ZEBRA_ROUTE_BGP;
a48f437a 1036 else if (strncmp (s, "ta", 2) == 0)
7a4bb9c5 1037 return ZEBRA_ROUTE_TABLE;
e0ca5fde
DL
1038 }
1039 return -1;
1040}
99a6c6cd
DW
1041
1042void
a9b5cbe5 1043zlog_hexdump (const void *mem, unsigned int len) {
99a6c6cd
DW
1044 unsigned long i = 0;
1045 unsigned int j = 0;
1046 unsigned int columns = 8;
1047 char buf[(len * 4) + ((len/4) * 20) + 30];
1048 char *s = buf;
1049
1050 for (i = 0; i < len + ((len % columns) ? (columns - len % columns) : 0); i++)
1051 {
1052 /* print offset */
1053 if (i % columns == 0)
1054 s += sprintf(s, "0x%016lx: ", (unsigned long)mem + i);
1055
1056 /* print hex data */
1057 if (i < len)
a9b5cbe5 1058 s += sprintf(s, "%02x ", 0xFF & ((const char*)mem)[i]);
99a6c6cd
DW
1059
1060 /* end of block, just aligning for ASCII dump */
1061 else
1062 s += sprintf(s, " ");
1063
1064 /* print ASCII dump */
1065 if (i % columns == (columns - 1))
1066 {
1067 for (j = i - (columns - 1); j <= i; j++)
1068 {
1069 if (j >= len) /* end of block, not really printing */
1070 s += sprintf(s, " ");
1071
a9b5cbe5
DS
1072 else if(isprint((int)((const char *)mem)[j])) /* printable char */
1073 s += sprintf(s, "%c", 0xFF & ((const char *)mem)[j]);
99a6c6cd
DW
1074
1075 else /* other char */
1076 s += sprintf(s, ".");
1077 }
1078 s += sprintf(s, "\n");
1079 }
1080 }
1081 zlog_debug("\n%s", buf);
1082}