]> git.proxmox.com Git - mirror_frr.git/blob - lib/log.c
Merge pull request #37 from LabNConsulting/working/2.0/patch-set-170102a
[mirror_frr.git] / lib / log.c
1 /*
2 * Logging of zebra
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
23 #define FRR_DEFINE_DESC_TABLE
24
25 #include <zebra.h>
26
27 #include "log.h"
28 #include "memory.h"
29 #include "command.h"
30 #ifndef SUNOS_5
31 #include <sys/un.h>
32 #endif
33 /* for printstack on solaris */
34 #ifdef HAVE_UCONTEXT_H
35 #include <ucontext.h>
36 #endif
37
38 DEFINE_MTYPE_STATIC(LIB, ZLOG, "Logging")
39
40 static int logfile_fd = -1; /* Used in signal handler. */
41
42 struct zlog *zlog_default = NULL;
43
44 /*
45 * This must be kept in the same order as the
46 * zlog_proto_t enum
47 */
48 const char *zlog_proto_names[] =
49 {
50 "NONE",
51 "DEFAULT",
52 "ZEBRA",
53 "RIP",
54 "BGP",
55 "OSPF",
56 "RIPNG",
57 "OSPF6",
58 "LDP",
59 "ISIS",
60 "PIM",
61 "RFP",
62 "WATCHQUAGGA",
63 NULL,
64 };
65
66 const char *zlog_priority[] =
67 {
68 "emergencies",
69 "alerts",
70 "critical",
71 "errors",
72 "warnings",
73 "notifications",
74 "informational",
75 "debugging",
76 NULL,
77 };
78
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 */
101 static 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 }
108
109 /* For time string format. */
110
111 size_t
112 quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
113 {
114 static struct {
115 time_t last;
116 size_t len;
117 char buf[28];
118 } cache;
119 struct timeval clock;
120
121 /* would it be sufficient to use global 'recent_time' here? I fear not... */
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 */
136
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? */
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];
155 do
156 {
157 *p-- = '0'+(clock.tv_usec % 10);
158 clock.tv_usec /= 10;
159 }
160 while (--prec > 0);
161 *p = '.';
162 return cache.len+1+timestamp_precision;
163 }
164 buf[cache.len] = '\0';
165 return cache.len;
166 }
167 if (buflen > 0)
168 buf[0] = '\0';
169 return 0;
170 }
171
172 /* Utility routine for current time printing. */
173 static void
174 time_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);
182 }
183
184
185 /* va_list version of zlog. */
186 void
187 vzlog (struct zlog *zl, int priority, const char *format, va_list args)
188 {
189 char proto_str[32];
190 int original_errno = errno;
191 struct timestamp_control tsctl;
192 tsctl.already_rendered = 0;
193
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 {
201 tsctl.precision = 0;
202 time_print(stderr, &tsctl);
203 fprintf (stderr, "%s: ", "unknown");
204 vfprintf (stderr, format, args);
205 fprintf (stderr, "\n");
206 fflush (stderr);
207
208 /* In this case we return at here. */
209 errno = original_errno;
210 return;
211 }
212 tsctl.precision = zl->timestamp_precision;
213
214 /* Syslog output */
215 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
216 {
217 va_list ac;
218 va_copy(ac, args);
219 vsyslog (priority|zlog_default->facility, format, ac);
220 va_end(ac);
221 }
222
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
228 /* File output. */
229 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
230 {
231 va_list ac;
232 time_print (zl->fp, &tsctl);
233 if (zl->record_priority)
234 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
235 fprintf (zl->fp, "%s", proto_str);
236 va_copy(ac, args);
237 vfprintf (zl->fp, format, ac);
238 va_end(ac);
239 fprintf (zl->fp, "\n");
240 fflush (zl->fp);
241 }
242
243 /* stdout output. */
244 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
245 {
246 va_list ac;
247 time_print (stdout, &tsctl);
248 if (zl->record_priority)
249 fprintf (stdout, "%s: ", zlog_priority[priority]);
250 fprintf (stdout, "%s", proto_str);
251 va_copy(ac, args);
252 vfprintf (stdout, format, ac);
253 va_end(ac);
254 fprintf (stdout, "\n");
255 fflush (stdout);
256 }
257
258 /* Terminal monitor. */
259 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
260 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
261 proto_str, format, &tsctl, args);
262
263 errno = original_errno;
264 }
265
266 int
267 vzlog_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
304 static char *
305 str_append(char *dst, int len, const char *src)
306 {
307 while ((len-- > 0) && *src)
308 *dst++ = *src++;
309 return dst;
310 }
311
312 static char *
313 num_append(char *s, int len, u_long x)
314 {
315 char buf[30];
316 char *t;
317
318 if (!x)
319 return str_append(s,len,"0");
320 *(t = &buf[sizeof(buf)-1]) = '\0';
321 while (x && (t > buf))
322 {
323 *--t = '0'+(x % 10);
324 x /= 10;
325 }
326 return str_append(s,len,t);
327 }
328
329 #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
330 static char *
331 hex_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 }
347 #endif
348
349 /* Needs to be enhanced to support Solaris. */
350 static int
351 syslog_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
380 static void
381 syslog_sigsafe(int priority, const char *msg, size_t msglen)
382 {
383 static int syslog_fd = -1;
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);
405 write_wrapper (syslog_fd,buf,s-buf);
406 #undef LOC
407 }
408
409 static int
410 open_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
441 /* Note: the goal here is to use only async-signal-safe functions. */
442 void
443 zlog_signal(int signo, const char *action
444 #ifdef SA_SIGINFO
445 , siginfo_t *siginfo, void *program_counter
446 #endif
447 )
448 {
449 time_t now;
450 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
451 char *s = buf;
452 char *msgstart = buf;
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++ = ' ';
461 msgstart = s;
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);
467 #ifdef SA_SIGINFO
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,"); ");
476 #else /* SA_SIGINFO */
477 s = str_append(LOC,"; ");
478 #endif /* SA_SIGINFO */
479 s = str_append(LOC,action);
480 if (s < buf+sizeof(buf))
481 *s++ = '\n';
482
483 /* N.B. implicit priority is most severe */
484 #define PRI LOG_CRIT
485
486 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
487 /* If no file logging configured, try to write to fallback log file. */
488 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
489 DUMP(logfile_fd)
490 if (!zlog_default)
491 DUMP(STDERR_FILENO)
492 else
493 {
494 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
495 DUMP(STDOUT_FILENO)
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);
502 }
503 #undef DUMP
504
505 zlog_backtrace_sigsafe(PRI,
506 #ifdef SA_SIGINFO
507 program_counter
508 #else
509 NULL
510 #endif
511 );
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
527 #define DUMP(FD) write_wrapper(FD, buf, s-buf);
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
546 #undef PRI
547 #undef LOC
548 }
549
550 /* Log a backtrace using only async-signal-safe functions.
551 Needs to be enhanced to support syslog logging. */
552 void
553 zlog_backtrace_sigsafe(int priority, void *program_counter)
554 {
555 #ifdef HAVE_STACK_TRACE
556 static const char pclabel[] = "Program counter: ";
557 void *array[64];
558 int size;
559 char buf[100];
560 char *s, **bt = NULL;
561 #define LOC s,buf+sizeof(buf)-s
562
563 #ifdef HAVE_GLIBC_BACKTRACE
564 size = backtrace(array, array_size(array));
565 if (size <= 0 || (size_t)size > array_size(array))
566 return;
567
568 #define DUMP(FD) { \
569 if (program_counter) \
570 { \
571 write_wrapper(FD, pclabel, sizeof(pclabel)-1); \
572 backtrace_symbols_fd(&program_counter, 1, FD); \
573 } \
574 write_wrapper(FD, buf, s-buf); \
575 backtrace_symbols_fd(array, size, FD); \
576 }
577 #elif defined(HAVE_PRINTSTACK)
578 #define DUMP(FD) { \
579 if (program_counter) \
580 write_wrapper((FD), pclabel, sizeof(pclabel)-1); \
581 write_wrapper((FD), buf, s-buf); \
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");
590
591 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
592 DUMP(logfile_fd)
593 if (!zlog_default)
594 DUMP(STDERR_FILENO)
595 else
596 {
597 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
598 DUMP(STDOUT_FILENO)
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;
607 #ifdef HAVE_GLIBC_BACKTRACE
608 bt = backtrace_symbols(array, size);
609 #endif
610 /* Just print the function addresses. */
611 for (i = 0; i < size; i++)
612 {
613 s = buf;
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 }
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])
626 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
627 }
628 if (bt)
629 free(bt);
630 }
631 }
632 #undef DUMP
633 #undef LOC
634 #endif /* HAVE_STRACK_TRACE */
635 }
636
637 void
638 zlog_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
647 size = backtrace(array, array_size(array));
648 if (size <= 0 || (size_t)size > array_size(array))
649 {
650 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
651 "(valid range is between 1 and %lu)",
652 size, (unsigned long)(array_size(array)));
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 */
669 }
670
671 void
672 zlog (struct zlog *zl, int priority, const char *format, ...)
673 {
674 va_list args;
675
676 va_start(args, format);
677 vzlog (zl, priority, format, args);
678 va_end (args);
679 }
680
681 #define ZLOG_FUNC(FUNCNAME,PRIORITY) \
682 void \
683 FUNCNAME(const char *format, ...) \
684 { \
685 va_list args; \
686 va_start(args, format); \
687 vzlog (NULL, PRIORITY, format, args); \
688 va_end(args); \
689 }
690
691 ZLOG_FUNC(zlog_err, LOG_ERR)
692
693 ZLOG_FUNC(zlog_warn, LOG_WARNING)
694
695 ZLOG_FUNC(zlog_info, LOG_INFO)
696
697 ZLOG_FUNC(zlog_notice, LOG_NOTICE)
698
699 ZLOG_FUNC(zlog_debug, LOG_DEBUG)
700
701 #undef ZLOG_FUNC
702
703 void 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
713 void
714 _zlog_assert_failed (const char *assertion, const char *file,
715 unsigned int line, const char *function)
716 {
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;
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);
725 zlog_thread_info(LOG_CRIT);
726 log_memstats_stderr ("log");
727 abort();
728 }
729
730 void
731 memory_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 }
738
739 /* Open log stream */
740 struct zlog *
741 openzlog (const char *progname, zlog_proto_t protocol, u_short instance,
742 int syslog_flags, int syslog_facility)
743 {
744 struct zlog *zl;
745 u_int i;
746
747 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
748
749 zl->ident = progname;
750 zl->protocol = protocol;
751 zl->instance = instance;
752 zl->facility = syslog_facility;
753 zl->syslog_options = syslog_flags;
754
755 /* Set default logging levels. */
756 for (i = 0; i < array_size(zl->maxlvl); i++)
757 zl->maxlvl[i] = ZLOG_DISABLED;
758 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
759 zl->default_lvl = LOG_DEBUG;
760
761 openlog (progname, syslog_flags, zl->facility);
762
763 return zl;
764 }
765
766 void
767 closezlog (struct zlog *zl)
768 {
769 closelog();
770
771 if (zl->fp != NULL)
772 fclose (zl->fp);
773
774 if (zl->filename != NULL)
775 XFREE(MTYPE_ZLOG, zl->filename);
776
777 XFREE (MTYPE_ZLOG, zl);
778 }
779
780 /* Called from command.c. */
781 void
782 zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
783 {
784 if (zl == NULL)
785 zl = zlog_default;
786
787 zl->maxlvl[dest] = log_level;
788 }
789
790 int
791 zlog_set_file (struct zlog *zl, const char *filename, int log_level)
792 {
793 FILE *fp;
794 mode_t oldumask;
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. */
804 oldumask = umask (0777 & ~LOGFILE_MASK);
805 fp = fopen (filename, "a");
806 umask(oldumask);
807 if (fp == NULL)
808 return 0;
809
810 /* Set flags. */
811 zl->filename = XSTRDUP(MTYPE_ZLOG, filename);
812 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
813 zl->fp = fp;
814 logfile_fd = fileno(fp);
815
816 return 1;
817 }
818
819 /* Reset opend file. */
820 int
821 zlog_reset_file (struct zlog *zl)
822 {
823 if (zl == NULL)
824 zl = zlog_default;
825
826 if (zl->fp)
827 fclose (zl->fp);
828 zl->fp = NULL;
829 logfile_fd = -1;
830 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
831
832 if (zl->filename)
833 XFREE(MTYPE_ZLOG, zl->filename);
834 zl->filename = NULL;
835
836 return 1;
837 }
838
839 /* Reopen log file. */
840 int
841 zlog_rotate (struct zlog *zl)
842 {
843 int level;
844
845 if (zl == NULL)
846 zl = zlog_default;
847
848 if (zl->fp)
849 fclose (zl->fp);
850 zl->fp = NULL;
851 logfile_fd = -1;
852 level = zl->maxlvl[ZLOG_DEST_FILE];
853 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
854
855 if (zl->filename)
856 {
857 mode_t oldumask;
858 int save_errno;
859
860 oldumask = umask (0777 & ~LOGFILE_MASK);
861 zl->fp = fopen (zl->filename, "a");
862 save_errno = errno;
863 umask(oldumask);
864 if (zl->fp == NULL)
865 {
866 zlog_err("Log rotate failed: cannot open file %s for append: %s",
867 zl->filename, safe_strerror(save_errno));
868 return -1;
869 }
870 logfile_fd = fileno(zl->fp);
871 zl->maxlvl[ZLOG_DEST_FILE] = level;
872 }
873
874 return 1;
875 }
876
877 /* Message lookup function. */
878 const char *
879 lookup (const struct message *mes, int key)
880 {
881 const struct message *pnt;
882
883 for (pnt = mes; pnt->key != 0; pnt++)
884 if (pnt->key == key)
885 return pnt->str;
886
887 return "";
888 }
889
890 /* Older/faster version of message lookup function, but requires caller to pass
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 */
896 const char *
897 mes_lookup (const struct message *meslist, int max, int index,
898 const char *none, const char *mesname)
899 {
900 int pos = index - meslist[0].key;
901
902 /* first check for best case: index is in range and matches the key
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;
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 {
919 const char *str = (meslist->str ? meslist->str : none);
920
921 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
922 index, str, mesname, i, max);
923 return str;
924 }
925 }
926 }
927 zlog_err("message index %d not found in %s (max is %d)", index, mesname, max);
928 assert (none);
929 return none;
930 }
931
932 /* Wrapper around strerror to handle case where it returns NULL. */
933 const char *
934 safe_strerror(int errnum)
935 {
936 const char *s = strerror(errnum);
937 return (s != NULL) ? s : "Unknown error";
938 }
939
940 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
941 static 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),
956 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
957 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
958 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
959 DESC_ENTRY (ZEBRA_HELLO),
960 DESC_ENTRY (ZEBRA_NEXTHOP_REGISTER),
961 DESC_ENTRY (ZEBRA_NEXTHOP_UNREGISTER),
962 DESC_ENTRY (ZEBRA_NEXTHOP_UPDATE),
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),
968 DESC_ENTRY (ZEBRA_IMPORT_CHECK_UPDATE),
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),
977 DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_DEL),
978 DESC_ENTRY (ZEBRA_VRF_UNREGISTER),
979 DESC_ENTRY (ZEBRA_VRF_ADD),
980 DESC_ENTRY (ZEBRA_VRF_DELETE),
981 DESC_ENTRY (ZEBRA_INTERFACE_VRF_UPDATE),
982 DESC_ENTRY (ZEBRA_BFD_CLIENT_REGISTER),
983 DESC_ENTRY (ZEBRA_INTERFACE_ENABLE_RADV),
984 DESC_ENTRY (ZEBRA_INTERFACE_DISABLE_RADV),
985 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB),
986 DESC_ENTRY (ZEBRA_MPLS_LABELS_ADD),
987 DESC_ENTRY (ZEBRA_MPLS_LABELS_DELETE),
988 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_ADD),
989 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_DELETE),
990 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_ADD),
991 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_DELETE),
992 };
993 #undef DESC_ENTRY
994
995 static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
996
997 static const struct zebra_desc_table *
998 zroute_lookup(u_int zroute)
999 {
1000 u_int i;
1001
1002 if (zroute >= array_size(route_types))
1003 {
1004 zlog_err("unknown zebra route type: %u", zroute);
1005 return &unknown;
1006 }
1007 if (zroute == route_types[zroute].type)
1008 return &route_types[zroute];
1009 for (i = 0; i < array_size(route_types); i++)
1010 {
1011 if (zroute == route_types[i].type)
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
1022 const char *
1023 zebra_route_string(u_int zroute)
1024 {
1025 return zroute_lookup(zroute)->string;
1026 }
1027
1028 char
1029 zebra_route_char(u_int zroute)
1030 {
1031 return zroute_lookup(zroute)->chr;
1032 }
1033
1034 const char *
1035 zserv_command_string (unsigned int command)
1036 {
1037 if (command >= array_size(command_types))
1038 {
1039 zlog_err ("unknown zserv command type: %u", command);
1040 return unknown.string;
1041 }
1042 return command_types[command].string;
1043 }
1044
1045 int
1046 proto_name2num(const char *s)
1047 {
1048 unsigned i;
1049
1050 for (i=0; i<array_size(route_types); ++i)
1051 if (strcasecmp(s, route_types[i].string) == 0)
1052 return route_types[i].type;
1053 return -1;
1054 }
1055
1056 int
1057 proto_redistnum(int afi, const char *s)
1058 {
1059 if (! s)
1060 return -1;
1061
1062 if (afi == AFI_IP)
1063 {
1064 if (strncmp (s, "k", 1) == 0)
1065 return ZEBRA_ROUTE_KERNEL;
1066 else if (strncmp (s, "c", 1) == 0)
1067 return ZEBRA_ROUTE_CONNECT;
1068 else if (strncmp (s, "s", 1) == 0)
1069 return ZEBRA_ROUTE_STATIC;
1070 else if (strncmp (s, "r", 1) == 0)
1071 return ZEBRA_ROUTE_RIP;
1072 else if (strncmp (s, "o", 1) == 0)
1073 return ZEBRA_ROUTE_OSPF;
1074 else if (strncmp (s, "i", 1) == 0)
1075 return ZEBRA_ROUTE_ISIS;
1076 else if (strncmp (s, "bg", 2) == 0)
1077 return ZEBRA_ROUTE_BGP;
1078 else if (strncmp (s, "ta", 2) == 0)
1079 return ZEBRA_ROUTE_TABLE;
1080 else if (strcmp (s, "vnc-direct") == 0)
1081 return ZEBRA_ROUTE_VNC_DIRECT;
1082 else if (strcmp (s, "vnc") == 0)
1083 return ZEBRA_ROUTE_VNC;
1084 }
1085 if (afi == AFI_IP6)
1086 {
1087 if (strncmp (s, "k", 1) == 0)
1088 return ZEBRA_ROUTE_KERNEL;
1089 else if (strncmp (s, "c", 1) == 0)
1090 return ZEBRA_ROUTE_CONNECT;
1091 else if (strncmp (s, "s", 1) == 0)
1092 return ZEBRA_ROUTE_STATIC;
1093 else if (strncmp (s, "r", 1) == 0)
1094 return ZEBRA_ROUTE_RIPNG;
1095 else if (strncmp (s, "o", 1) == 0)
1096 return ZEBRA_ROUTE_OSPF6;
1097 else if (strncmp (s, "i", 1) == 0)
1098 return ZEBRA_ROUTE_ISIS;
1099 else if (strncmp (s, "bg", 2) == 0)
1100 return ZEBRA_ROUTE_BGP;
1101 else if (strncmp (s, "ta", 2) == 0)
1102 return ZEBRA_ROUTE_TABLE;
1103 else if (strcmp (s, "vnc-direct") == 0)
1104 return ZEBRA_ROUTE_VNC_DIRECT;
1105 else if (strcmp (s, "vnc") == 0)
1106 return ZEBRA_ROUTE_VNC;
1107 }
1108 return -1;
1109 }
1110
1111 void
1112 zlog_hexdump (const void *mem, unsigned int len) {
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)
1127 s += sprintf(s, "%02x ", 0xFF & ((const char*)mem)[i]);
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
1141 else if(isprint((int)((const char *)mem)[j])) /* printable char */
1142 s += sprintf(s, "%c", 0xFF & ((const char *)mem)[j]);
1143
1144 else /* other char */
1145 s += sprintf(s, ".");
1146 }
1147 s += sprintf(s, "\n");
1148 }
1149 }
1150 zlog_debug("\n%s", buf);
1151 }