blob: e4ec7c2283eb576c093019d302390d50a9313d82 [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 Lampartere0ca5fd2009-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",
Paul Jakma57345092011-12-25 17:52:09 +010051 "BABEL",
paul718e3742002-12-13 20:15:29 +000052 "OSPF6",
jardin9e867fe2003-12-23 08:56:18 +000053 "ISIS",
paul718e3742002-12-13 20:15:29 +000054 "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};
70
71
72
73/* For time string format. */
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +000074
75size_t
76quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
77{
78 static struct {
79 time_t last;
80 size_t len;
81 char buf[28];
82 } cache;
83 struct timeval clock;
84
85 /* would it be sufficient to use global 'recent_time' here? I fear not... */
86 gettimeofday(&clock, NULL);
87
88 /* first, we update the cache if the time has changed */
89 if (cache.last != clock.tv_sec)
90 {
91 struct tm *tm;
92 cache.last = clock.tv_sec;
93 tm = localtime(&cache.last);
94 cache.len = strftime(cache.buf, sizeof(cache.buf),
95 "%Y/%m/%d %H:%M:%S", tm);
96 }
97 /* note: it's not worth caching the subsecond part, because
98 chances are that back-to-back calls are not sufficiently close together
99 for the clock not to have ticked forward */
100
101 if (buflen > cache.len)
102 {
103 memcpy(buf, cache.buf, cache.len);
104 if ((timestamp_precision > 0) &&
105 (buflen > cache.len+1+timestamp_precision))
106 {
107 /* should we worry about locale issues? */
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000108 static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1};
109 int prec;
110 char *p = buf+cache.len+1+(prec = timestamp_precision);
111 *p-- = '\0';
112 while (prec > 6)
113 /* this is unlikely to happen, but protect anyway */
114 {
115 *p-- = '0';
116 prec--;
117 }
118 clock.tv_usec /= divisor[prec];
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000119 do
120 {
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000121 *p-- = '0'+(clock.tv_usec % 10);
122 clock.tv_usec /= 10;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000123 }
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000124 while (--prec > 0);
125 *p = '.';
126 return cache.len+1+timestamp_precision;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000127 }
128 buf[cache.len] = '\0';
129 return cache.len;
130 }
131 if (buflen > 0)
132 buf[0] = '\0';
133 return 0;
134}
paul718e3742002-12-13 20:15:29 +0000135
136/* Utility routine for current time printing. */
137static void
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000138time_print(FILE *fp, struct timestamp_control *ctl)
paul718e3742002-12-13 20:15:29 +0000139{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000140 if (!ctl->already_rendered)
141 {
142 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
143 ctl->already_rendered = 1;
144 }
145 fprintf(fp, "%s ", ctl->buf);
paul718e3742002-12-13 20:15:29 +0000146}
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000147
paul718e3742002-12-13 20:15:29 +0000148
149/* va_list version of zlog. */
ajsd246bd92004-11-23 17:35:08 +0000150static void
151vzlog (struct zlog *zl, int priority, const char *format, va_list args)
paul718e3742002-12-13 20:15:29 +0000152{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000153 struct timestamp_control tsctl;
154 tsctl.already_rendered = 0;
155
paul718e3742002-12-13 20:15:29 +0000156 /* If zlog is not specified, use default one. */
157 if (zl == NULL)
158 zl = zlog_default;
159
160 /* When zlog_default is also NULL, use stderr for logging. */
161 if (zl == NULL)
162 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000163 tsctl.precision = 0;
164 time_print(stderr, &tsctl);
paul718e3742002-12-13 20:15:29 +0000165 fprintf (stderr, "%s: ", "unknown");
ajsd246bd92004-11-23 17:35:08 +0000166 vfprintf (stderr, format, args);
paul718e3742002-12-13 20:15:29 +0000167 fprintf (stderr, "\n");
168 fflush (stderr);
169
170 /* In this case we return at here. */
171 return;
172 }
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000173 tsctl.precision = zl->timestamp_precision;
paul718e3742002-12-13 20:15:29 +0000174
paul718e3742002-12-13 20:15:29 +0000175 /* Syslog output */
ajs274a4a42004-12-07 15:39:31 +0000176 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
ajsd246bd92004-11-23 17:35:08 +0000177 {
178 va_list ac;
179 va_copy(ac, args);
180 vsyslog (priority|zlog_default->facility, format, ac);
181 va_end(ac);
182 }
paul718e3742002-12-13 20:15:29 +0000183
184 /* File output. */
ajs274a4a42004-12-07 15:39:31 +0000185 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
paul718e3742002-12-13 20:15:29 +0000186 {
ajsd246bd92004-11-23 17:35:08 +0000187 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000188 time_print (zl->fp, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000189 if (zl->record_priority)
190 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000191 fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000192 va_copy(ac, args);
193 vfprintf (zl->fp, format, ac);
194 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000195 fprintf (zl->fp, "\n");
196 fflush (zl->fp);
197 }
198
199 /* stdout output. */
ajs274a4a42004-12-07 15:39:31 +0000200 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
paul718e3742002-12-13 20:15:29 +0000201 {
ajsd246bd92004-11-23 17:35:08 +0000202 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000203 time_print (stdout, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000204 if (zl->record_priority)
205 fprintf (stdout, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000206 fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000207 va_copy(ac, args);
208 vfprintf (stdout, format, ac);
209 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000210 fprintf (stdout, "\n");
211 fflush (stdout);
212 }
213
paul718e3742002-12-13 20:15:29 +0000214 /* Terminal monitor. */
ajs274a4a42004-12-07 15:39:31 +0000215 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
216 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000217 zlog_proto_names[zl->protocol], format, &tsctl, args);
paul718e3742002-12-13 20:15:29 +0000218}
219
ajs59a06a92004-11-23 18:19:14 +0000220static char *
221str_append(char *dst, int len, const char *src)
222{
223 while ((len-- > 0) && *src)
224 *dst++ = *src++;
225 return dst;
226}
227
228static char *
229num_append(char *s, int len, u_long x)
230{
231 char buf[30];
ajs7d149b82004-11-28 23:00:01 +0000232 char *t;
ajs59a06a92004-11-23 18:19:14 +0000233
ajs7d149b82004-11-28 23:00:01 +0000234 if (!x)
235 return str_append(s,len,"0");
236 *(t = &buf[sizeof(buf)-1]) = '\0';
ajs59a06a92004-11-23 18:19:14 +0000237 while (x && (t > buf))
238 {
239 *--t = '0'+(x % 10);
240 x /= 10;
241 }
242 return str_append(s,len,t);
243}
244
Paul Jakmafb66b292006-05-28 08:26:15 +0000245#if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
ajs7d149b82004-11-28 23:00:01 +0000246static char *
247hex_append(char *s, int len, u_long x)
248{
249 char buf[30];
250 char *t;
251
252 if (!x)
253 return str_append(s,len,"0");
254 *(t = &buf[sizeof(buf)-1]) = '\0';
255 while (x && (t > buf))
256 {
257 u_int cc = (x % 16);
258 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
259 x /= 16;
260 }
261 return str_append(s,len,t);
262}
ajs31364272005-01-18 22:18:59 +0000263#endif
ajs7d149b82004-11-28 23:00:01 +0000264
ajs7d149b82004-11-28 23:00:01 +0000265/* Needs to be enhanced to support Solaris. */
266static int
267syslog_connect(void)
268{
269#ifdef SUNOS_5
270 return -1;
271#else
272 int fd;
273 char *s;
274 struct sockaddr_un addr;
275
276 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
277 return -1;
278 addr.sun_family = AF_UNIX;
279#ifdef _PATH_LOG
280#define SYSLOG_SOCKET_PATH _PATH_LOG
281#else
282#define SYSLOG_SOCKET_PATH "/dev/log"
283#endif
284 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
285#undef SYSLOG_SOCKET_PATH
286 *s = '\0';
287 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
288 {
289 close(fd);
290 return -1;
291 }
292 return fd;
293#endif
294}
295
296static void
297syslog_sigsafe(int priority, const char *msg, size_t msglen)
298{
ajs1e221352005-02-03 16:42:40 +0000299 static int syslog_fd = -1;
ajs7d149b82004-11-28 23:00:01 +0000300 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
301 char *s;
302
303 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
304 return;
305
306#define LOC s,buf+sizeof(buf)-s
307 s = buf;
308 s = str_append(LOC,"<");
309 s = num_append(LOC,priority);
310 s = str_append(LOC,">");
311 /* forget about the timestamp, too difficult in a signal handler */
312 s = str_append(LOC,zlog_default->ident);
313 if (zlog_default->syslog_options & LOG_PID)
314 {
315 s = str_append(LOC,"[");
316 s = num_append(LOC,getpid());
317 s = str_append(LOC,"]");
318 }
319 s = str_append(LOC,": ");
320 s = str_append(LOC,msg);
321 write(syslog_fd,buf,s-buf);
322#undef LOC
323}
324
ajs1e221352005-02-03 16:42:40 +0000325static int
326open_crashlog(void)
327{
328#define CRASHLOG_PREFIX "/var/tmp/quagga."
329#define CRASHLOG_SUFFIX "crashlog"
330 if (zlog_default && zlog_default->ident)
331 {
332 /* Avoid strlen since it is not async-signal-safe. */
333 const char *p;
334 size_t ilen;
335
336 for (p = zlog_default->ident, ilen = 0; *p; p++)
337 ilen++;
338 {
339 char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3];
340 char *s = buf;
341#define LOC s,buf+sizeof(buf)-s
342 s = str_append(LOC, CRASHLOG_PREFIX);
343 s = str_append(LOC, zlog_default->ident);
344 s = str_append(LOC, ".");
345 s = str_append(LOC, CRASHLOG_SUFFIX);
346#undef LOC
347 *s = '\0';
348 return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK);
349 }
350 }
351 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL,
352 LOGFILE_MASK);
353#undef CRASHLOG_SUFFIX
354#undef CRASHLOG_PREFIX
355}
356
ajs7d149b82004-11-28 23:00:01 +0000357/* Note: the goal here is to use only async-signal-safe functions. */
ajs59a06a92004-11-23 18:19:14 +0000358void
ajs31364272005-01-18 22:18:59 +0000359zlog_signal(int signo, const char *action
360#ifdef SA_SIGINFO
361 , siginfo_t *siginfo, void *program_counter
362#endif
363 )
ajs59a06a92004-11-23 18:19:14 +0000364{
365 time_t now;
ajs40abf232005-01-12 17:27:27 +0000366 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
ajs59a06a92004-11-23 18:19:14 +0000367 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000368 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000369#define LOC s,buf+sizeof(buf)-s
370
371 time(&now);
372 if (zlog_default)
373 {
374 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
375 *s++ = ':';
376 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000377 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000378 }
379 s = str_append(LOC,"Received signal ");
380 s = num_append(LOC,signo);
381 s = str_append(LOC," at ");
382 s = num_append(LOC,now);
ajs31364272005-01-18 22:18:59 +0000383#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +0000384 s = str_append(LOC," (si_addr 0x");
385 s = hex_append(LOC,(u_long)(siginfo->si_addr));
386 if (program_counter)
387 {
388 s = str_append(LOC,", PC 0x");
389 s = hex_append(LOC,(u_long)program_counter);
390 }
391 s = str_append(LOC,"); ");
ajs31364272005-01-18 22:18:59 +0000392#else /* SA_SIGINFO */
393 s = str_append(LOC,"; ");
394#endif /* SA_SIGINFO */
ajs59a06a92004-11-23 18:19:14 +0000395 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000396 if (s < buf+sizeof(buf))
397 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000398
ajs274a4a42004-12-07 15:39:31 +0000399 /* N.B. implicit priority is most severe */
ajs1e221352005-02-03 16:42:40 +0000400#define PRI LOG_CRIT
ajs274a4a42004-12-07 15:39:31 +0000401
ajs1e221352005-02-03 16:42:40 +0000402#define DUMP(FD) write(FD, buf, s-buf);
403 /* If no file logging configured, try to write to fallback log file. */
ajsc4c7d0c2005-02-03 19:22:05 +0000404 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
405 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000406 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000407 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000408 else
409 {
ajs274a4a42004-12-07 15:39:31 +0000410 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000411 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000412 /* Remove trailing '\n' for monitor and syslog */
413 *--s = '\0';
414 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
415 vty_log_fixed(buf,s-buf);
416 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
417 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000418 }
419#undef DUMP
420
ajs31364272005-01-18 22:18:59 +0000421 zlog_backtrace_sigsafe(PRI,
422#ifdef SA_SIGINFO
423 program_counter
424#else
425 NULL
426#endif
427 );
ajs274a4a42004-12-07 15:39:31 +0000428#undef PRI
ajs063ee522004-11-26 18:11:14 +0000429#undef LOC
430}
ajs59a06a92004-11-23 18:19:14 +0000431
ajs063ee522004-11-26 18:11:14 +0000432/* Log a backtrace using only async-signal-safe functions.
433 Needs to be enhanced to support syslog logging. */
434void
ajs239c26f2005-01-17 15:22:28 +0000435zlog_backtrace_sigsafe(int priority, void *program_counter)
ajs063ee522004-11-26 18:11:14 +0000436{
Paul Jakmafb66b292006-05-28 08:26:15 +0000437#ifdef HAVE_STACK_TRACE
ajs239c26f2005-01-17 15:22:28 +0000438 static const char pclabel[] = "Program counter: ";
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700439 void *array[64];
ajs063ee522004-11-26 18:11:14 +0000440 int size;
441 char buf[100];
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700442 char *s, **bt = NULL;
ajs063ee522004-11-26 18:11:14 +0000443#define LOC s,buf+sizeof(buf)-s
444
Paul Jakmafb66b292006-05-28 08:26:15 +0000445#ifdef HAVE_GLIBC_BACKTRACE
Balaji.G837d16c2012-09-26 14:09:10 +0530446 if (((size = backtrace(array,array_size(array)) <= 0) ||
447 ((size_t)size > array_size(array))))
ajs063ee522004-11-26 18:11:14 +0000448 return;
ajs59a06a92004-11-23 18:19:14 +0000449
ajs1e221352005-02-03 16:42:40 +0000450#define DUMP(FD) { \
ajs239c26f2005-01-17 15:22:28 +0000451 if (program_counter) \
452 { \
ajs1e221352005-02-03 16:42:40 +0000453 write(FD, pclabel, sizeof(pclabel)-1); \
454 backtrace_symbols_fd(&program_counter, 1, FD); \
ajs239c26f2005-01-17 15:22:28 +0000455 } \
ajs1e221352005-02-03 16:42:40 +0000456 write(FD, buf, s-buf); \
457 backtrace_symbols_fd(array, size, FD); \
ajs59a06a92004-11-23 18:19:14 +0000458}
Paul Jakmafb66b292006-05-28 08:26:15 +0000459#elif defined(HAVE_PRINTSTACK)
460#define DUMP(FD) { \
461 if (program_counter) \
462 write((FD), pclabel, sizeof(pclabel)-1); \
463 write((FD), buf, s-buf); \
464 printstack((FD)); \
465}
466#endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
467
468 s = buf;
469 s = str_append(LOC,"Backtrace for ");
470 s = num_append(LOC,size);
471 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000472
ajsc4c7d0c2005-02-03 19:22:05 +0000473 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
474 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000475 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000476 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000477 else
478 {
ajs274a4a42004-12-07 15:39:31 +0000479 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000480 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000481 /* Remove trailing '\n' for monitor and syslog */
482 *--s = '\0';
483 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
484 vty_log_fixed(buf,s-buf);
485 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
486 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
487 {
488 int i;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700489#ifdef HAVE_GLIBC_BACKTRACE
490 bt = backtrace_symbols(array, size);
491#endif
ajs274a4a42004-12-07 15:39:31 +0000492 /* Just print the function addresses. */
493 for (i = 0; i < size; i++)
494 {
495 s = buf;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700496 if (bt)
497 s = str_append(LOC, bt[i]);
498 else {
499 s = str_append(LOC,"[bt ");
500 s = num_append(LOC,i);
501 s = str_append(LOC,"] 0x");
502 s = hex_append(LOC,(u_long)(array[i]));
503 }
ajs274a4a42004-12-07 15:39:31 +0000504 *s = '\0';
505 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
506 vty_log_fixed(buf,s-buf);
507 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000508 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000509 }
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700510 if (bt)
511 free(bt);
ajs274a4a42004-12-07 15:39:31 +0000512 }
ajs59a06a92004-11-23 18:19:14 +0000513 }
514#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000515#undef LOC
Paul Jakmafb66b292006-05-28 08:26:15 +0000516#endif /* HAVE_STRACK_TRACE */
ajs063ee522004-11-26 18:11:14 +0000517}
518
519void
520zlog_backtrace(int priority)
521{
522#ifndef HAVE_GLIBC_BACKTRACE
523 zlog(NULL, priority, "No backtrace available on this platform.");
524#else
525 void *array[20];
526 int size, i;
527 char **strings;
528
Balaji.G837d16c2012-09-26 14:09:10 +0530529 if (((size = backtrace(array,array_size(array))) <= 0) ||
530 ((size_t)size > array_size(array)))
ajs063ee522004-11-26 18:11:14 +0000531 {
532 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000533 "(valid range is between 1 and %lu)",
Balaji.G837d16c2012-09-26 14:09:10 +0530534 size, (unsigned long)(array_size(array)));
ajs063ee522004-11-26 18:11:14 +0000535 return;
536 }
537 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
538 if (!(strings = backtrace_symbols(array, size)))
539 {
540 zlog_err("Cannot get backtrace symbols (out of memory?)");
541 for (i = 0; i < size; i++)
542 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
543 }
544 else
545 {
546 for (i = 0; i < size; i++)
547 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
548 free(strings);
549 }
550#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000551}
552
paul718e3742002-12-13 20:15:29 +0000553void
554zlog (struct zlog *zl, int priority, const char *format, ...)
555{
ajsd246bd92004-11-23 17:35:08 +0000556 va_list args;
paul718e3742002-12-13 20:15:29 +0000557
ajsd246bd92004-11-23 17:35:08 +0000558 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000559 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000560 va_end (args);
paul718e3742002-12-13 20:15:29 +0000561}
562
ajsd246bd92004-11-23 17:35:08 +0000563#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
564void \
565FUNCNAME(const char *format, ...) \
566{ \
567 va_list args; \
568 va_start(args, format); \
569 vzlog (NULL, PRIORITY, format, args); \
570 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000571}
572
ajsd246bd92004-11-23 17:35:08 +0000573ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000574
ajsd246bd92004-11-23 17:35:08 +0000575ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000576
ajsd246bd92004-11-23 17:35:08 +0000577ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000578
ajsd246bd92004-11-23 17:35:08 +0000579ZLOG_FUNC(zlog_notice, LOG_NOTICE)
580
581ZLOG_FUNC(zlog_debug, LOG_DEBUG)
582
583#undef ZLOG_FUNC
584
585#define PLOG_FUNC(FUNCNAME,PRIORITY) \
586void \
587FUNCNAME(struct zlog *zl, const char *format, ...) \
588{ \
589 va_list args; \
590 va_start(args, format); \
591 vzlog (zl, PRIORITY, format, args); \
592 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000593}
594
ajsd246bd92004-11-23 17:35:08 +0000595PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000596
ajsd246bd92004-11-23 17:35:08 +0000597PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000598
ajsd246bd92004-11-23 17:35:08 +0000599PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000600
ajsd246bd92004-11-23 17:35:08 +0000601PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000602
ajsd246bd92004-11-23 17:35:08 +0000603PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000604
ajsd246bd92004-11-23 17:35:08 +0000605#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000606
ajscee3df12004-11-24 17:14:49 +0000607void
608_zlog_assert_failed (const char *assertion, const char *file,
609 unsigned int line, const char *function)
610{
ajsc4c7d0c2005-02-03 19:22:05 +0000611 /* Force fallback file logging? */
612 if (zlog_default && !zlog_default->fp &&
613 ((logfile_fd = open_crashlog()) >= 0) &&
614 ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
615 zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
ajs1e221352005-02-03 16:42:40 +0000616 zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
617 assertion,file,line,(function ? function : "?"));
618 zlog_backtrace(LOG_CRIT);
ajscee3df12004-11-24 17:14:49 +0000619 abort();
620}
621
paul718e3742002-12-13 20:15:29 +0000622
623/* Open log stream */
624struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000625openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000626 int syslog_flags, int syslog_facility)
627{
628 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000629 u_int i;
paul718e3742002-12-13 20:15:29 +0000630
ajs274a4a42004-12-07 15:39:31 +0000631 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000632
633 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000634 zl->protocol = protocol;
635 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000636 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000637
ajs274a4a42004-12-07 15:39:31 +0000638 /* Set default logging levels. */
Balaji.G837d16c2012-09-26 14:09:10 +0530639 for (i = 0; i < array_size(zl->maxlvl); i++)
ajs274a4a42004-12-07 15:39:31 +0000640 zl->maxlvl[i] = ZLOG_DISABLED;
641 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
642 zl->default_lvl = LOG_DEBUG;
643
paul718e3742002-12-13 20:15:29 +0000644 openlog (progname, syslog_flags, zl->facility);
645
646 return zl;
647}
648
649void
650closezlog (struct zlog *zl)
651{
652 closelog();
Chris Caputo228da422009-07-18 05:44:03 +0000653
654 if (zl->fp != NULL)
655 fclose (zl->fp);
paul718e3742002-12-13 20:15:29 +0000656
Tom Goff7e69d992010-11-10 13:01:17 -0800657 if (zl->filename != NULL)
658 free (zl->filename);
659
paul718e3742002-12-13 20:15:29 +0000660 XFREE (MTYPE_ZLOG, zl);
661}
662
663/* Called from command.c. */
664void
ajs274a4a42004-12-07 15:39:31 +0000665zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000666{
667 if (zl == NULL)
668 zl = zlog_default;
669
ajs274a4a42004-12-07 15:39:31 +0000670 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000671}
672
673int
ajs274a4a42004-12-07 15:39:31 +0000674zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000675{
676 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000677 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000678
679 /* There is opend file. */
680 zlog_reset_file (zl);
681
682 /* Set default zl. */
683 if (zl == NULL)
684 zl = zlog_default;
685
686 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000687 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000688 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000689 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000690 if (fp == NULL)
691 return 0;
paul718e3742002-12-13 20:15:29 +0000692
693 /* Set flags. */
694 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000695 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000696 zl->fp = fp;
ajsc4c7d0c2005-02-03 19:22:05 +0000697 logfile_fd = fileno(fp);
paul718e3742002-12-13 20:15:29 +0000698
699 return 1;
700}
701
702/* Reset opend file. */
703int
704zlog_reset_file (struct zlog *zl)
705{
706 if (zl == NULL)
707 zl = zlog_default;
708
paul718e3742002-12-13 20:15:29 +0000709 if (zl->fp)
710 fclose (zl->fp);
711 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000712 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000713 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000714
715 if (zl->filename)
716 free (zl->filename);
717 zl->filename = NULL;
718
719 return 1;
720}
721
722/* Reopen log file. */
723int
724zlog_rotate (struct zlog *zl)
725{
ajs274a4a42004-12-07 15:39:31 +0000726 int level;
paul718e3742002-12-13 20:15:29 +0000727
728 if (zl == NULL)
729 zl = zlog_default;
730
731 if (zl->fp)
732 fclose (zl->fp);
733 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000734 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000735 level = zl->maxlvl[ZLOG_DEST_FILE];
736 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000737
738 if (zl->filename)
739 {
gdtaa593d52003-12-22 20:15:53 +0000740 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000741 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000742
743 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000744 zl->fp = fopen (zl->filename, "a");
745 save_errno = errno;
746 umask(oldumask);
747 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000748 {
ajs274a4a42004-12-07 15:39:31 +0000749 zlog_err("Log rotate failed: cannot open file %s for append: %s",
750 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000751 return -1;
752 }
ajsc4c7d0c2005-02-03 19:22:05 +0000753 logfile_fd = fileno(zl->fp);
ajs274a4a42004-12-07 15:39:31 +0000754 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000755 }
756
757 return 1;
758}
759
paul718e3742002-12-13 20:15:29 +0000760/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000761const char *
Stephen Hemminger1423c802008-08-14 17:59:25 +0100762lookup (const struct message *mes, int key)
paul718e3742002-12-13 20:15:29 +0000763{
Stephen Hemminger1423c802008-08-14 17:59:25 +0100764 const struct message *pnt;
paul718e3742002-12-13 20:15:29 +0000765
766 for (pnt = mes; pnt->key != 0; pnt++)
767 if (pnt->key == key)
768 return pnt->str;
769
770 return "";
771}
772
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000773/* Older/faster version of message lookup function, but requires caller to pass
Paul Jakma11486b52008-02-28 23:26:02 +0000774 * in the array size (instead of relying on a 0 key to terminate the search).
775 *
776 * The return value is the message string if found, or the 'none' pointer
777 * provided otherwise.
778 */
hasso8c328f12004-10-05 21:01:23 +0000779const char *
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400780mes_lookup (const struct message *meslist, int max, int index,
781 const char *none, const char *mesname)
paul718e3742002-12-13 20:15:29 +0000782{
Paul Jakma11486b52008-02-28 23:26:02 +0000783 int pos = index - meslist[0].key;
784
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000785 /* first check for best case: index is in range and matches the key
Paul Jakma11486b52008-02-28 23:26:02 +0000786 * value in that slot.
787 * NB: key numbering might be offset from 0. E.g. protocol constants
788 * often start at 1.
789 */
790 if ((pos >= 0) && (pos < max)
791 && (meslist[pos].key == index))
792 return meslist[pos].str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000793
794 /* fall back to linear search */
795 {
796 int i;
797
798 for (i = 0; i < max; i++, meslist++)
799 {
800 if (meslist->key == index)
801 {
Paul Jakma11486b52008-02-28 23:26:02 +0000802 const char *str = (meslist->str ? meslist->str : none);
803
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400804 zlog_debug ("message index %d [%s] found in %s at position %d (max is %d)",
805 index, str, mesname, i, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000806 return str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000807 }
808 }
809 }
Dmitrij Tejblum51abba52011-09-21 17:41:41 +0400810 zlog_err("message index %d not found in %s (max is %d)", index, mesname, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000811 assert (none);
812 return none;
paul718e3742002-12-13 20:15:29 +0000813}
ajsca359762004-11-19 23:40:16 +0000814
815/* Wrapper around strerror to handle case where it returns NULL. */
816const char *
817safe_strerror(int errnum)
818{
819 const char *s = strerror(errnum);
820 return (s != NULL) ? s : "Unknown error";
821}
ajsf52d13c2005-10-01 17:38:06 +0000822
Paul Jakmad6d672a2006-05-15 16:56:51 +0000823#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
824static const struct zebra_desc_table command_types[] = {
825 DESC_ENTRY (ZEBRA_INTERFACE_ADD),
826 DESC_ENTRY (ZEBRA_INTERFACE_DELETE),
827 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD),
828 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE),
829 DESC_ENTRY (ZEBRA_INTERFACE_UP),
830 DESC_ENTRY (ZEBRA_INTERFACE_DOWN),
831 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD),
832 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE),
833 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD),
834 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE),
835 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD),
836 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE),
837 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
838 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
839 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP),
840 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP),
841 DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP),
842 DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP),
843 DESC_ENTRY (ZEBRA_INTERFACE_RENAME),
844 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
845 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
846 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
Denis Ovsienko4c783762012-01-21 22:50:19 +0400847 DESC_ENTRY (ZEBRA_HELLO),
Paul Jakmad6d672a2006-05-15 16:56:51 +0000848};
849#undef DESC_ENTRY
850
851static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
852
853static const struct zebra_desc_table *
ajsf52d13c2005-10-01 17:38:06 +0000854zroute_lookup(u_int zroute)
855{
ajsf52d13c2005-10-01 17:38:06 +0000856 u_int i;
857
Balaji.G837d16c2012-09-26 14:09:10 +0530858 if (zroute >= array_size(route_types))
ajsf52d13c2005-10-01 17:38:06 +0000859 {
860 zlog_err("unknown zebra route type: %u", zroute);
861 return &unknown;
862 }
Paul Jakmad6d672a2006-05-15 16:56:51 +0000863 if (zroute == route_types[zroute].type)
ajsf52d13c2005-10-01 17:38:06 +0000864 return &route_types[zroute];
Balaji.G837d16c2012-09-26 14:09:10 +0530865 for (i = 0; i < array_size(route_types); i++)
ajsf52d13c2005-10-01 17:38:06 +0000866 {
Paul Jakmad6d672a2006-05-15 16:56:51 +0000867 if (zroute == route_types[i].type)
ajsf52d13c2005-10-01 17:38:06 +0000868 {
869 zlog_warn("internal error: route type table out of order "
870 "while searching for %u, please notify developers", zroute);
871 return &route_types[i];
872 }
873 }
874 zlog_err("internal error: cannot find route type %u in table!", zroute);
875 return &unknown;
876}
877
878const char *
879zebra_route_string(u_int zroute)
880{
881 return zroute_lookup(zroute)->string;
882}
883
884char
885zebra_route_char(u_int zroute)
886{
887 return zroute_lookup(zroute)->chr;
888}
Paul Jakmad6d672a2006-05-15 16:56:51 +0000889
890const char *
891zserv_command_string (unsigned int command)
892{
Balaji.G837d16c2012-09-26 14:09:10 +0530893 if (command >= array_size(command_types))
Paul Jakmad6d672a2006-05-15 16:56:51 +0000894 {
895 zlog_err ("unknown zserv command type: %u", command);
896 return unknown.string;
897 }
898 return command_types[command].string;
899}
Paul Jakma7514fb72007-05-02 16:05:35 +0000900
Paul Jakma7514fb72007-05-02 16:05:35 +0000901int
902proto_name2num(const char *s)
903{
904 unsigned i;
905
Balaji.G837d16c2012-09-26 14:09:10 +0530906 for (i=0; i<array_size(route_types); ++i)
Paul Jakma7514fb72007-05-02 16:05:35 +0000907 if (strcasecmp(s, route_types[i].string) == 0)
908 return route_types[i].type;
909 return -1;
910}
David Lampartere0ca5fd2009-09-16 01:52:42 +0200911
David Lampartere0ca5fd2009-09-16 01:52:42 +0200912int
913proto_redistnum(int afi, const char *s)
914{
915 if (! s)
916 return -1;
917
918 if (afi == AFI_IP)
919 {
920 if (strncmp (s, "k", 1) == 0)
921 return ZEBRA_ROUTE_KERNEL;
922 else if (strncmp (s, "c", 1) == 0)
923 return ZEBRA_ROUTE_CONNECT;
924 else if (strncmp (s, "s", 1) == 0)
925 return ZEBRA_ROUTE_STATIC;
926 else if (strncmp (s, "r", 1) == 0)
927 return ZEBRA_ROUTE_RIP;
928 else if (strncmp (s, "o", 1) == 0)
929 return ZEBRA_ROUTE_OSPF;
930 else if (strncmp (s, "i", 1) == 0)
931 return ZEBRA_ROUTE_ISIS;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400932 else if (strncmp (s, "bg", 2) == 0)
David Lampartere0ca5fd2009-09-16 01:52:42 +0200933 return ZEBRA_ROUTE_BGP;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400934 else if (strncmp (s, "ba", 2) == 0)
935 return ZEBRA_ROUTE_BABEL;
David Lampartere0ca5fd2009-09-16 01:52:42 +0200936 }
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;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400951 else if (strncmp (s, "bg", 2) == 0)
David Lampartere0ca5fd2009-09-16 01:52:42 +0200952 return ZEBRA_ROUTE_BGP;
Denis Ovsienkof8a246d2012-01-11 18:18:56 +0400953 else if (strncmp (s, "ba", 2) == 0)
954 return ZEBRA_ROUTE_BABEL;
David Lampartere0ca5fd2009-09-16 01:52:42 +0200955 }
956 return -1;
957}