blob: 7edc5c4c553072d1b79be438c0ca9691fc1cccb6 [file] [log] [blame]
ajs274a4a42004-12-07 15:39:31 +00001/*
ajs274a4a42004-12-07 15:39:31 +00002 * Logging of zebra
paul718e3742002-12-13 20:15:29 +00003 * 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
David Lamparterdaca2cf2009-09-16 01:52:42 +020023#define QUAGGA_DEFINE_DESC_TABLE
24
paul718e3742002-12-13 20:15:29 +000025#include <zebra.h>
26
27#include "log.h"
28#include "memory.h"
29#include "command.h"
ajs7d149b82004-11-28 23:00:01 +000030#ifndef SUNOS_5
31#include <sys/un.h>
32#endif
Paul Jakma30a22312008-08-15 14:05:22 +010033/* for printstack on solaris */
34#ifdef HAVE_UCONTEXT_H
35#include <ucontext.h>
36#endif
paul718e3742002-12-13 20:15:29 +000037
ajsc4c7d0c2005-02-03 19:22:05 +000038static int logfile_fd = -1; /* Used in signal handler. */
ajs1e221352005-02-03 16:42:40 +000039
paul718e3742002-12-13 20:15:29 +000040struct 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",
jardin9e867fe2003-12-23 08:56:18 +000052 "ISIS",
paul718e3742002-12-13 20:15:29 +000053 "MASC",
54 NULL,
55};
56
57const char *zlog_priority[] =
58{
59 "emergencies",
60 "alerts",
61 "critical",
62 "errors",
63 "warnings",
64 "notifications",
65 "informational",
66 "debugging",
67 NULL,
68};
69
70
71
72/* For time string format. */
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +000073
74size_t
75quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
76{
77 static struct {
78 time_t last;
79 size_t len;
80 char buf[28];
81 } cache;
82 struct timeval clock;
83
84 /* would it be sufficient to use global 'recent_time' here? I fear not... */
85 gettimeofday(&clock, NULL);
86
87 /* first, we update the cache if the time has changed */
88 if (cache.last != clock.tv_sec)
89 {
90 struct tm *tm;
91 cache.last = clock.tv_sec;
92 tm = localtime(&cache.last);
93 cache.len = strftime(cache.buf, sizeof(cache.buf),
94 "%Y/%m/%d %H:%M:%S", tm);
95 }
96 /* note: it's not worth caching the subsecond part, because
97 chances are that back-to-back calls are not sufficiently close together
98 for the clock not to have ticked forward */
99
100 if (buflen > cache.len)
101 {
102 memcpy(buf, cache.buf, cache.len);
103 if ((timestamp_precision > 0) &&
104 (buflen > cache.len+1+timestamp_precision))
105 {
106 /* should we worry about locale issues? */
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000107 static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1};
108 int prec;
109 char *p = buf+cache.len+1+(prec = timestamp_precision);
110 *p-- = '\0';
111 while (prec > 6)
112 /* this is unlikely to happen, but protect anyway */
113 {
114 *p-- = '0';
115 prec--;
116 }
117 clock.tv_usec /= divisor[prec];
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000118 do
119 {
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000120 *p-- = '0'+(clock.tv_usec % 10);
121 clock.tv_usec /= 10;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000122 }
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000123 while (--prec > 0);
124 *p = '.';
125 return cache.len+1+timestamp_precision;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000126 }
127 buf[cache.len] = '\0';
128 return cache.len;
129 }
130 if (buflen > 0)
131 buf[0] = '\0';
132 return 0;
133}
paul718e3742002-12-13 20:15:29 +0000134
135/* Utility routine for current time printing. */
136static void
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000137time_print(FILE *fp, struct timestamp_control *ctl)
paul718e3742002-12-13 20:15:29 +0000138{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000139 if (!ctl->already_rendered)
140 {
141 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
142 ctl->already_rendered = 1;
143 }
144 fprintf(fp, "%s ", ctl->buf);
paul718e3742002-12-13 20:15:29 +0000145}
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000146
paul718e3742002-12-13 20:15:29 +0000147
148/* va_list version of zlog. */
ajsd246bd92004-11-23 17:35:08 +0000149static void
150vzlog (struct zlog *zl, int priority, const char *format, va_list args)
paul718e3742002-12-13 20:15:29 +0000151{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000152 struct timestamp_control tsctl;
153 tsctl.already_rendered = 0;
154
paul718e3742002-12-13 20:15:29 +0000155 /* If zlog is not specified, use default one. */
156 if (zl == NULL)
157 zl = zlog_default;
158
159 /* When zlog_default is also NULL, use stderr for logging. */
160 if (zl == NULL)
161 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000162 tsctl.precision = 0;
163 time_print(stderr, &tsctl);
paul718e3742002-12-13 20:15:29 +0000164 fprintf (stderr, "%s: ", "unknown");
ajsd246bd92004-11-23 17:35:08 +0000165 vfprintf (stderr, format, args);
paul718e3742002-12-13 20:15:29 +0000166 fprintf (stderr, "\n");
167 fflush (stderr);
168
169 /* In this case we return at here. */
170 return;
171 }
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000172 tsctl.precision = zl->timestamp_precision;
paul718e3742002-12-13 20:15:29 +0000173
paul718e3742002-12-13 20:15:29 +0000174 /* Syslog output */
ajs274a4a42004-12-07 15:39:31 +0000175 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
ajsd246bd92004-11-23 17:35:08 +0000176 {
177 va_list ac;
178 va_copy(ac, args);
179 vsyslog (priority|zlog_default->facility, format, ac);
180 va_end(ac);
181 }
paul718e3742002-12-13 20:15:29 +0000182
183 /* File output. */
ajs274a4a42004-12-07 15:39:31 +0000184 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
paul718e3742002-12-13 20:15:29 +0000185 {
ajsd246bd92004-11-23 17:35:08 +0000186 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000187 time_print (zl->fp, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000188 if (zl->record_priority)
189 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000190 fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000191 va_copy(ac, args);
192 vfprintf (zl->fp, format, ac);
193 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000194 fprintf (zl->fp, "\n");
195 fflush (zl->fp);
196 }
197
198 /* stdout output. */
ajs274a4a42004-12-07 15:39:31 +0000199 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
paul718e3742002-12-13 20:15:29 +0000200 {
ajsd246bd92004-11-23 17:35:08 +0000201 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000202 time_print (stdout, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000203 if (zl->record_priority)
204 fprintf (stdout, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000205 fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000206 va_copy(ac, args);
207 vfprintf (stdout, format, ac);
208 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000209 fprintf (stdout, "\n");
210 fflush (stdout);
211 }
212
paul718e3742002-12-13 20:15:29 +0000213 /* Terminal monitor. */
ajs274a4a42004-12-07 15:39:31 +0000214 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
215 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000216 zlog_proto_names[zl->protocol], format, &tsctl, args);
paul718e3742002-12-13 20:15:29 +0000217}
218
ajs59a06a92004-11-23 18:19:14 +0000219static char *
220str_append(char *dst, int len, const char *src)
221{
222 while ((len-- > 0) && *src)
223 *dst++ = *src++;
224 return dst;
225}
226
227static char *
228num_append(char *s, int len, u_long x)
229{
230 char buf[30];
ajs7d149b82004-11-28 23:00:01 +0000231 char *t;
ajs59a06a92004-11-23 18:19:14 +0000232
ajs7d149b82004-11-28 23:00:01 +0000233 if (!x)
234 return str_append(s,len,"0");
235 *(t = &buf[sizeof(buf)-1]) = '\0';
ajs59a06a92004-11-23 18:19:14 +0000236 while (x && (t > buf))
237 {
238 *--t = '0'+(x % 10);
239 x /= 10;
240 }
241 return str_append(s,len,t);
242}
243
Paul Jakmafb66b292006-05-28 08:26:15 +0000244#if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
ajs7d149b82004-11-28 23:00:01 +0000245static char *
246hex_append(char *s, int len, u_long x)
247{
248 char buf[30];
249 char *t;
250
251 if (!x)
252 return str_append(s,len,"0");
253 *(t = &buf[sizeof(buf)-1]) = '\0';
254 while (x && (t > buf))
255 {
256 u_int cc = (x % 16);
257 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
258 x /= 16;
259 }
260 return str_append(s,len,t);
261}
ajs31364272005-01-18 22:18:59 +0000262#endif
ajs7d149b82004-11-28 23:00:01 +0000263
ajs7d149b82004-11-28 23:00:01 +0000264/* Needs to be enhanced to support Solaris. */
265static int
266syslog_connect(void)
267{
268#ifdef SUNOS_5
269 return -1;
270#else
271 int fd;
272 char *s;
273 struct sockaddr_un addr;
274
275 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
276 return -1;
277 addr.sun_family = AF_UNIX;
278#ifdef _PATH_LOG
279#define SYSLOG_SOCKET_PATH _PATH_LOG
280#else
281#define SYSLOG_SOCKET_PATH "/dev/log"
282#endif
283 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
284#undef SYSLOG_SOCKET_PATH
285 *s = '\0';
286 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
287 {
288 close(fd);
289 return -1;
290 }
291 return fd;
292#endif
293}
294
295static void
296syslog_sigsafe(int priority, const char *msg, size_t msglen)
297{
ajs1e221352005-02-03 16:42:40 +0000298 static int syslog_fd = -1;
ajs7d149b82004-11-28 23:00:01 +0000299 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
300 char *s;
301
302 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
303 return;
304
305#define LOC s,buf+sizeof(buf)-s
306 s = buf;
307 s = str_append(LOC,"<");
308 s = num_append(LOC,priority);
309 s = str_append(LOC,">");
310 /* forget about the timestamp, too difficult in a signal handler */
311 s = str_append(LOC,zlog_default->ident);
312 if (zlog_default->syslog_options & LOG_PID)
313 {
314 s = str_append(LOC,"[");
315 s = num_append(LOC,getpid());
316 s = str_append(LOC,"]");
317 }
318 s = str_append(LOC,": ");
319 s = str_append(LOC,msg);
320 write(syslog_fd,buf,s-buf);
321#undef LOC
322}
323
ajs1e221352005-02-03 16:42:40 +0000324static int
325open_crashlog(void)
326{
327#define CRASHLOG_PREFIX "/var/tmp/quagga."
328#define CRASHLOG_SUFFIX "crashlog"
329 if (zlog_default && zlog_default->ident)
330 {
331 /* Avoid strlen since it is not async-signal-safe. */
332 const char *p;
333 size_t ilen;
334
335 for (p = zlog_default->ident, ilen = 0; *p; p++)
336 ilen++;
337 {
338 char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3];
339 char *s = buf;
340#define LOC s,buf+sizeof(buf)-s
341 s = str_append(LOC, CRASHLOG_PREFIX);
342 s = str_append(LOC, zlog_default->ident);
343 s = str_append(LOC, ".");
344 s = str_append(LOC, CRASHLOG_SUFFIX);
345#undef LOC
346 *s = '\0';
347 return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK);
348 }
349 }
350 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL,
351 LOGFILE_MASK);
352#undef CRASHLOG_SUFFIX
353#undef CRASHLOG_PREFIX
354}
355
ajs7d149b82004-11-28 23:00:01 +0000356/* Note: the goal here is to use only async-signal-safe functions. */
ajs59a06a92004-11-23 18:19:14 +0000357void
ajs31364272005-01-18 22:18:59 +0000358zlog_signal(int signo, const char *action
359#ifdef SA_SIGINFO
360 , siginfo_t *siginfo, void *program_counter
361#endif
362 )
ajs59a06a92004-11-23 18:19:14 +0000363{
364 time_t now;
ajs40abf232005-01-12 17:27:27 +0000365 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
ajs59a06a92004-11-23 18:19:14 +0000366 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000367 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000368#define LOC s,buf+sizeof(buf)-s
369
370 time(&now);
371 if (zlog_default)
372 {
373 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
374 *s++ = ':';
375 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000376 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000377 }
378 s = str_append(LOC,"Received signal ");
379 s = num_append(LOC,signo);
380 s = str_append(LOC," at ");
381 s = num_append(LOC,now);
ajs31364272005-01-18 22:18:59 +0000382#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +0000383 s = str_append(LOC," (si_addr 0x");
384 s = hex_append(LOC,(u_long)(siginfo->si_addr));
385 if (program_counter)
386 {
387 s = str_append(LOC,", PC 0x");
388 s = hex_append(LOC,(u_long)program_counter);
389 }
390 s = str_append(LOC,"); ");
ajs31364272005-01-18 22:18:59 +0000391#else /* SA_SIGINFO */
392 s = str_append(LOC,"; ");
393#endif /* SA_SIGINFO */
ajs59a06a92004-11-23 18:19:14 +0000394 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000395 if (s < buf+sizeof(buf))
396 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000397
ajs274a4a42004-12-07 15:39:31 +0000398 /* N.B. implicit priority is most severe */
ajs1e221352005-02-03 16:42:40 +0000399#define PRI LOG_CRIT
ajs274a4a42004-12-07 15:39:31 +0000400
ajs1e221352005-02-03 16:42:40 +0000401#define DUMP(FD) write(FD, buf, s-buf);
402 /* If no file logging configured, try to write to fallback log file. */
ajsc4c7d0c2005-02-03 19:22:05 +0000403 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
404 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000405 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000406 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000407 else
408 {
ajs274a4a42004-12-07 15:39:31 +0000409 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000410 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000411 /* Remove trailing '\n' for monitor and syslog */
412 *--s = '\0';
413 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
414 vty_log_fixed(buf,s-buf);
415 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
416 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000417 }
418#undef DUMP
419
ajs31364272005-01-18 22:18:59 +0000420 zlog_backtrace_sigsafe(PRI,
421#ifdef SA_SIGINFO
422 program_counter
423#else
424 NULL
425#endif
426 );
ajs274a4a42004-12-07 15:39:31 +0000427#undef PRI
ajs063ee522004-11-26 18:11:14 +0000428#undef LOC
429}
ajs59a06a92004-11-23 18:19:14 +0000430
ajs063ee522004-11-26 18:11:14 +0000431/* Log a backtrace using only async-signal-safe functions.
432 Needs to be enhanced to support syslog logging. */
433void
ajs239c26f2005-01-17 15:22:28 +0000434zlog_backtrace_sigsafe(int priority, void *program_counter)
ajs063ee522004-11-26 18:11:14 +0000435{
Paul Jakmafb66b292006-05-28 08:26:15 +0000436#ifdef HAVE_STACK_TRACE
ajs239c26f2005-01-17 15:22:28 +0000437 static const char pclabel[] = "Program counter: ";
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700438 void *array[64];
ajs063ee522004-11-26 18:11:14 +0000439 int size;
440 char buf[100];
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700441 char *s, **bt = NULL;
ajs063ee522004-11-26 18:11:14 +0000442#define LOC s,buf+sizeof(buf)-s
443
Paul Jakmafb66b292006-05-28 08:26:15 +0000444#ifdef HAVE_GLIBC_BACKTRACE
ajs063ee522004-11-26 18:11:14 +0000445 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
446 ((size_t)size > sizeof(array)/sizeof(array[0])))
447 return;
ajs59a06a92004-11-23 18:19:14 +0000448
ajs1e221352005-02-03 16:42:40 +0000449#define DUMP(FD) { \
ajs239c26f2005-01-17 15:22:28 +0000450 if (program_counter) \
451 { \
ajs1e221352005-02-03 16:42:40 +0000452 write(FD, pclabel, sizeof(pclabel)-1); \
453 backtrace_symbols_fd(&program_counter, 1, FD); \
ajs239c26f2005-01-17 15:22:28 +0000454 } \
ajs1e221352005-02-03 16:42:40 +0000455 write(FD, buf, s-buf); \
456 backtrace_symbols_fd(array, size, FD); \
ajs59a06a92004-11-23 18:19:14 +0000457}
Paul Jakmafb66b292006-05-28 08:26:15 +0000458#elif defined(HAVE_PRINTSTACK)
459#define DUMP(FD) { \
460 if (program_counter) \
461 write((FD), pclabel, sizeof(pclabel)-1); \
462 write((FD), buf, s-buf); \
463 printstack((FD)); \
464}
465#endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
466
467 s = buf;
468 s = str_append(LOC,"Backtrace for ");
469 s = num_append(LOC,size);
470 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000471
ajsc4c7d0c2005-02-03 19:22:05 +0000472 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
473 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000474 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000475 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000476 else
477 {
ajs274a4a42004-12-07 15:39:31 +0000478 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000479 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000480 /* Remove trailing '\n' for monitor and syslog */
481 *--s = '\0';
482 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
483 vty_log_fixed(buf,s-buf);
484 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
485 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
486 {
487 int i;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700488#ifdef HAVE_GLIBC_BACKTRACE
489 bt = backtrace_symbols(array, size);
490#endif
ajs274a4a42004-12-07 15:39:31 +0000491 /* Just print the function addresses. */
492 for (i = 0; i < size; i++)
493 {
494 s = buf;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700495 if (bt)
496 s = str_append(LOC, bt[i]);
497 else {
498 s = str_append(LOC,"[bt ");
499 s = num_append(LOC,i);
500 s = str_append(LOC,"] 0x");
501 s = hex_append(LOC,(u_long)(array[i]));
502 }
ajs274a4a42004-12-07 15:39:31 +0000503 *s = '\0';
504 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
505 vty_log_fixed(buf,s-buf);
506 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000507 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000508 }
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700509 if (bt)
510 free(bt);
ajs274a4a42004-12-07 15:39:31 +0000511 }
ajs59a06a92004-11-23 18:19:14 +0000512 }
513#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000514#undef LOC
Paul Jakmafb66b292006-05-28 08:26:15 +0000515#endif /* HAVE_STRACK_TRACE */
ajs063ee522004-11-26 18:11:14 +0000516}
517
518void
519zlog_backtrace(int priority)
520{
521#ifndef HAVE_GLIBC_BACKTRACE
522 zlog(NULL, priority, "No backtrace available on this platform.");
523#else
524 void *array[20];
525 int size, i;
526 char **strings;
527
528 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
529 ((size_t)size > sizeof(array)/sizeof(array[0])))
530 {
531 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000532 "(valid range is between 1 and %lu)",
533 size, (unsigned long)(sizeof(array)/sizeof(array[0])));
ajs063ee522004-11-26 18:11:14 +0000534 return;
535 }
536 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
537 if (!(strings = backtrace_symbols(array, size)))
538 {
539 zlog_err("Cannot get backtrace symbols (out of memory?)");
540 for (i = 0; i < size; i++)
541 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
542 }
543 else
544 {
545 for (i = 0; i < size; i++)
546 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
547 free(strings);
548 }
549#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000550}
551
paul718e3742002-12-13 20:15:29 +0000552void
553zlog (struct zlog *zl, int priority, const char *format, ...)
554{
ajsd246bd92004-11-23 17:35:08 +0000555 va_list args;
paul718e3742002-12-13 20:15:29 +0000556
ajsd246bd92004-11-23 17:35:08 +0000557 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000558 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000559 va_end (args);
paul718e3742002-12-13 20:15:29 +0000560}
561
ajsd246bd92004-11-23 17:35:08 +0000562#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
563void \
564FUNCNAME(const char *format, ...) \
565{ \
566 va_list args; \
567 va_start(args, format); \
568 vzlog (NULL, PRIORITY, format, args); \
569 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000570}
571
ajsd246bd92004-11-23 17:35:08 +0000572ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000573
ajsd246bd92004-11-23 17:35:08 +0000574ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000575
ajsd246bd92004-11-23 17:35:08 +0000576ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000577
ajsd246bd92004-11-23 17:35:08 +0000578ZLOG_FUNC(zlog_notice, LOG_NOTICE)
579
580ZLOG_FUNC(zlog_debug, LOG_DEBUG)
581
582#undef ZLOG_FUNC
583
584#define PLOG_FUNC(FUNCNAME,PRIORITY) \
585void \
586FUNCNAME(struct zlog *zl, const char *format, ...) \
587{ \
588 va_list args; \
589 va_start(args, format); \
590 vzlog (zl, PRIORITY, format, args); \
591 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000592}
593
ajsd246bd92004-11-23 17:35:08 +0000594PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000595
ajsd246bd92004-11-23 17:35:08 +0000596PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000597
ajsd246bd92004-11-23 17:35:08 +0000598PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000599
ajsd246bd92004-11-23 17:35:08 +0000600PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000601
ajsd246bd92004-11-23 17:35:08 +0000602PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000603
ajsd246bd92004-11-23 17:35:08 +0000604#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000605
ajscee3df12004-11-24 17:14:49 +0000606void
607_zlog_assert_failed (const char *assertion, const char *file,
608 unsigned int line, const char *function)
609{
ajsc4c7d0c2005-02-03 19:22:05 +0000610 /* Force fallback file logging? */
611 if (zlog_default && !zlog_default->fp &&
612 ((logfile_fd = open_crashlog()) >= 0) &&
613 ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
614 zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
ajs1e221352005-02-03 16:42:40 +0000615 zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
616 assertion,file,line,(function ? function : "?"));
617 zlog_backtrace(LOG_CRIT);
ajscee3df12004-11-24 17:14:49 +0000618 abort();
619}
620
paul718e3742002-12-13 20:15:29 +0000621
622/* Open log stream */
623struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000624openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000625 int syslog_flags, int syslog_facility)
626{
627 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000628 u_int i;
paul718e3742002-12-13 20:15:29 +0000629
ajs274a4a42004-12-07 15:39:31 +0000630 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000631
632 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000633 zl->protocol = protocol;
634 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000635 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000636
ajs274a4a42004-12-07 15:39:31 +0000637 /* Set default logging levels. */
638 for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++)
639 zl->maxlvl[i] = ZLOG_DISABLED;
640 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
641 zl->default_lvl = LOG_DEBUG;
642
paul718e3742002-12-13 20:15:29 +0000643 openlog (progname, syslog_flags, zl->facility);
644
645 return zl;
646}
647
648void
649closezlog (struct zlog *zl)
650{
651 closelog();
Chris Caputo228da422009-07-18 05:44:03 +0000652
653 if (zl->fp != NULL)
654 fclose (zl->fp);
paul718e3742002-12-13 20:15:29 +0000655
Tom Goffcd3355b2010-11-10 13:01:17 -0800656 if (zl->filename != NULL)
657 free (zl->filename);
658
paul718e3742002-12-13 20:15:29 +0000659 XFREE (MTYPE_ZLOG, zl);
660}
661
662/* Called from command.c. */
663void
ajs274a4a42004-12-07 15:39:31 +0000664zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000665{
666 if (zl == NULL)
667 zl = zlog_default;
668
ajs274a4a42004-12-07 15:39:31 +0000669 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000670}
671
672int
ajs274a4a42004-12-07 15:39:31 +0000673zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000674{
675 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000676 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000677
678 /* There is opend file. */
679 zlog_reset_file (zl);
680
681 /* Set default zl. */
682 if (zl == NULL)
683 zl = zlog_default;
684
685 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000686 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000687 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000688 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000689 if (fp == NULL)
690 return 0;
paul718e3742002-12-13 20:15:29 +0000691
692 /* Set flags. */
693 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000694 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000695 zl->fp = fp;
ajsc4c7d0c2005-02-03 19:22:05 +0000696 logfile_fd = fileno(fp);
paul718e3742002-12-13 20:15:29 +0000697
698 return 1;
699}
700
701/* Reset opend file. */
702int
703zlog_reset_file (struct zlog *zl)
704{
705 if (zl == NULL)
706 zl = zlog_default;
707
paul718e3742002-12-13 20:15:29 +0000708 if (zl->fp)
709 fclose (zl->fp);
710 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000711 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000712 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000713
714 if (zl->filename)
715 free (zl->filename);
716 zl->filename = NULL;
717
718 return 1;
719}
720
721/* Reopen log file. */
722int
723zlog_rotate (struct zlog *zl)
724{
ajs274a4a42004-12-07 15:39:31 +0000725 int level;
paul718e3742002-12-13 20:15:29 +0000726
727 if (zl == NULL)
728 zl = zlog_default;
729
730 if (zl->fp)
731 fclose (zl->fp);
732 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000733 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000734 level = zl->maxlvl[ZLOG_DEST_FILE];
735 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000736
737 if (zl->filename)
738 {
gdtaa593d52003-12-22 20:15:53 +0000739 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000740 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000741
742 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000743 zl->fp = fopen (zl->filename, "a");
744 save_errno = errno;
745 umask(oldumask);
746 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000747 {
ajs274a4a42004-12-07 15:39:31 +0000748 zlog_err("Log rotate failed: cannot open file %s for append: %s",
749 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000750 return -1;
751 }
ajsc4c7d0c2005-02-03 19:22:05 +0000752 logfile_fd = fileno(zl->fp);
ajs274a4a42004-12-07 15:39:31 +0000753 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000754 }
755
756 return 1;
757}
758
paul718e3742002-12-13 20:15:29 +0000759/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000760const char *
Stephen Hemminger1423c802008-08-14 17:59:25 +0100761lookup (const struct message *mes, int key)
paul718e3742002-12-13 20:15:29 +0000762{
Stephen Hemminger1423c802008-08-14 17:59:25 +0100763 const struct message *pnt;
paul718e3742002-12-13 20:15:29 +0000764
765 for (pnt = mes; pnt->key != 0; pnt++)
766 if (pnt->key == key)
767 return pnt->str;
768
769 return "";
770}
771
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000772/* Older/faster version of message lookup function, but requires caller to pass
Paul Jakma11486b52008-02-28 23:26:02 +0000773 * in the array size (instead of relying on a 0 key to terminate the search).
774 *
775 * The return value is the message string if found, or the 'none' pointer
776 * provided otherwise.
777 */
hasso8c328f12004-10-05 21:01:23 +0000778const char *
Dmitrij Tejblum614da2b2011-09-21 17:41:41 +0400779mes_lookup (const struct message *meslist, int max, int index,
780 const char *none, const char *mesname)
paul718e3742002-12-13 20:15:29 +0000781{
Paul Jakma11486b52008-02-28 23:26:02 +0000782 int pos = index - meslist[0].key;
783
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000784 /* first check for best case: index is in range and matches the key
Paul Jakma11486b52008-02-28 23:26:02 +0000785 * value in that slot.
786 * NB: key numbering might be offset from 0. E.g. protocol constants
787 * often start at 1.
788 */
789 if ((pos >= 0) && (pos < max)
790 && (meslist[pos].key == index))
791 return meslist[pos].str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000792
793 /* fall back to linear search */
794 {
795 int i;
796
797 for (i = 0; i < max; i++, meslist++)
798 {
799 if (meslist->key == index)
800 {
Paul Jakma11486b52008-02-28 23:26:02 +0000801 const char *str = (meslist->str ? meslist->str : none);
802
Dmitrij Tejblum614da2b2011-09-21 17:41:41 +0400803 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
804 index, str, mesname, i, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000805 return str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000806 }
807 }
808 }
Dmitrij Tejblum614da2b2011-09-21 17:41:41 +0400809 zlog_err("message index %d not found in %s (max is %d)", index, mesname, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000810 assert (none);
811 return none;
paul718e3742002-12-13 20:15:29 +0000812}
ajsca359762004-11-19 23:40:16 +0000813
814/* Wrapper around strerror to handle case where it returns NULL. */
815const char *
816safe_strerror(int errnum)
817{
818 const char *s = strerror(errnum);
819 return (s != NULL) ? s : "Unknown error";
820}
ajsf52d13c2005-10-01 17:38:06 +0000821
Paul Jakmad6d672a2006-05-15 16:56:51 +0000822#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
823static const struct zebra_desc_table command_types[] = {
824 DESC_ENTRY (ZEBRA_INTERFACE_ADD),
825 DESC_ENTRY (ZEBRA_INTERFACE_DELETE),
826 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD),
827 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE),
828 DESC_ENTRY (ZEBRA_INTERFACE_UP),
829 DESC_ENTRY (ZEBRA_INTERFACE_DOWN),
830 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD),
831 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE),
832 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD),
833 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE),
834 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD),
835 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE),
836 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
837 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
838 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP),
839 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP),
840 DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP),
841 DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP),
842 DESC_ENTRY (ZEBRA_INTERFACE_RENAME),
843 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
844 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
845 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
846};
847#undef DESC_ENTRY
848
849static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
850
851static const struct zebra_desc_table *
ajsf52d13c2005-10-01 17:38:06 +0000852zroute_lookup(u_int zroute)
853{
ajsf52d13c2005-10-01 17:38:06 +0000854 u_int i;
855
856 if (zroute >= sizeof(route_types)/sizeof(route_types[0]))
857 {
858 zlog_err("unknown zebra route type: %u", zroute);
859 return &unknown;
860 }
Paul Jakmad6d672a2006-05-15 16:56:51 +0000861 if (zroute == route_types[zroute].type)
ajsf52d13c2005-10-01 17:38:06 +0000862 return &route_types[zroute];
863 for (i = 0; i < sizeof(route_types)/sizeof(route_types[0]); i++)
864 {
Paul Jakmad6d672a2006-05-15 16:56:51 +0000865 if (zroute == route_types[i].type)
ajsf52d13c2005-10-01 17:38:06 +0000866 {
867 zlog_warn("internal error: route type table out of order "
868 "while searching for %u, please notify developers", zroute);
869 return &route_types[i];
870 }
871 }
872 zlog_err("internal error: cannot find route type %u in table!", zroute);
873 return &unknown;
874}
875
876const char *
877zebra_route_string(u_int zroute)
878{
879 return zroute_lookup(zroute)->string;
880}
881
882char
883zebra_route_char(u_int zroute)
884{
885 return zroute_lookup(zroute)->chr;
886}
Paul Jakmad6d672a2006-05-15 16:56:51 +0000887
888const char *
889zserv_command_string (unsigned int command)
890{
891 if (command >= sizeof(command_types)/sizeof(command_types[0]))
892 {
893 zlog_err ("unknown zserv command type: %u", command);
894 return unknown.string;
895 }
896 return command_types[command].string;
897}
Paul Jakma7514fb72007-05-02 16:05:35 +0000898
899#define RTSIZE (sizeof(route_types)/sizeof(route_types[0]))
900
901int
902proto_name2num(const char *s)
903{
904 unsigned i;
905
906 for (i=0; i<RTSIZE; ++i)
907 if (strcasecmp(s, route_types[i].string) == 0)
908 return route_types[i].type;
909 return -1;
910}
David Lamparterdaca2cf2009-09-16 01:52:42 +0200911
Paul Jakma7514fb72007-05-02 16:05:35 +0000912#undef RTSIZE
David Lamparterdaca2cf2009-09-16 01:52:42 +0200913
914int
915proto_redistnum(int afi, const char *s)
916{
917 if (! s)
918 return -1;
919
920 if (afi == AFI_IP)
921 {
922 if (strncmp (s, "k", 1) == 0)
923 return ZEBRA_ROUTE_KERNEL;
924 else if (strncmp (s, "c", 1) == 0)
925 return ZEBRA_ROUTE_CONNECT;
926 else if (strncmp (s, "s", 1) == 0)
927 return ZEBRA_ROUTE_STATIC;
928 else if (strncmp (s, "r", 1) == 0)
929 return ZEBRA_ROUTE_RIP;
930 else if (strncmp (s, "o", 1) == 0)
931 return ZEBRA_ROUTE_OSPF;
932 else if (strncmp (s, "i", 1) == 0)
933 return ZEBRA_ROUTE_ISIS;
934 else if (strncmp (s, "b", 1) == 0)
935 return ZEBRA_ROUTE_BGP;
936 }
937 if (afi == AFI_IP6)
938 {
939 if (strncmp (s, "k", 1) == 0)
940 return ZEBRA_ROUTE_KERNEL;
941 else if (strncmp (s, "c", 1) == 0)
942 return ZEBRA_ROUTE_CONNECT;
943 else if (strncmp (s, "s", 1) == 0)
944 return ZEBRA_ROUTE_STATIC;
945 else if (strncmp (s, "r", 1) == 0)
946 return ZEBRA_ROUTE_RIPNG;
947 else if (strncmp (s, "o", 1) == 0)
948 return ZEBRA_ROUTE_OSPF6;
949 else if (strncmp (s, "i", 1) == 0)
950 return ZEBRA_ROUTE_ISIS;
951 else if (strncmp (s, "b", 1) == 0)
952 return ZEBRA_ROUTE_BGP;
953 }
954 return -1;
955}