blob: 8a7e5f3a7d4eaed9ddee70cfb02cb9497f18cdbe [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
23#include <zebra.h>
24
25#include "log.h"
26#include "memory.h"
27#include "command.h"
ajs7d149b82004-11-28 23:00:01 +000028#ifndef SUNOS_5
29#include <sys/un.h>
30#endif
Paul Jakma30a22312008-08-15 14:05:22 +010031/* for printstack on solaris */
32#ifdef HAVE_UCONTEXT_H
33#include <ucontext.h>
34#endif
paul718e3742002-12-13 20:15:29 +000035
ajsc4c7d0c2005-02-03 19:22:05 +000036static int logfile_fd = -1; /* Used in signal handler. */
ajs1e221352005-02-03 16:42:40 +000037
paul718e3742002-12-13 20:15:29 +000038struct zlog *zlog_default = NULL;
39
40const char *zlog_proto_names[] =
41{
42 "NONE",
43 "DEFAULT",
44 "ZEBRA",
45 "RIP",
46 "BGP",
47 "OSPF",
48 "RIPNG",
49 "OSPF6",
jardin9e867fe2003-12-23 08:56:18 +000050 "ISIS",
paul718e3742002-12-13 20:15:29 +000051 "MASC",
52 NULL,
53};
54
55const char *zlog_priority[] =
56{
57 "emergencies",
58 "alerts",
59 "critical",
60 "errors",
61 "warnings",
62 "notifications",
63 "informational",
64 "debugging",
65 NULL,
66};
67
68
69
70/* For time string format. */
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +000071
72size_t
73quagga_timestamp(int timestamp_precision, char *buf, size_t buflen)
74{
75 static struct {
76 time_t last;
77 size_t len;
78 char buf[28];
79 } cache;
80 struct timeval clock;
81
82 /* would it be sufficient to use global 'recent_time' here? I fear not... */
83 gettimeofday(&clock, NULL);
84
85 /* first, we update the cache if the time has changed */
86 if (cache.last != clock.tv_sec)
87 {
88 struct tm *tm;
89 cache.last = clock.tv_sec;
90 tm = localtime(&cache.last);
91 cache.len = strftime(cache.buf, sizeof(cache.buf),
92 "%Y/%m/%d %H:%M:%S", tm);
93 }
94 /* note: it's not worth caching the subsecond part, because
95 chances are that back-to-back calls are not sufficiently close together
96 for the clock not to have ticked forward */
97
98 if (buflen > cache.len)
99 {
100 memcpy(buf, cache.buf, cache.len);
101 if ((timestamp_precision > 0) &&
102 (buflen > cache.len+1+timestamp_precision))
103 {
104 /* should we worry about locale issues? */
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000105 static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1};
106 int prec;
107 char *p = buf+cache.len+1+(prec = timestamp_precision);
108 *p-- = '\0';
109 while (prec > 6)
110 /* this is unlikely to happen, but protect anyway */
111 {
112 *p-- = '0';
113 prec--;
114 }
115 clock.tv_usec /= divisor[prec];
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000116 do
117 {
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000118 *p-- = '0'+(clock.tv_usec % 10);
119 clock.tv_usec /= 10;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000120 }
Andrew J. Schorrbcdda302007-04-29 15:48:22 +0000121 while (--prec > 0);
122 *p = '.';
123 return cache.len+1+timestamp_precision;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000124 }
125 buf[cache.len] = '\0';
126 return cache.len;
127 }
128 if (buflen > 0)
129 buf[0] = '\0';
130 return 0;
131}
paul718e3742002-12-13 20:15:29 +0000132
133/* Utility routine for current time printing. */
134static void
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000135time_print(FILE *fp, struct timestamp_control *ctl)
paul718e3742002-12-13 20:15:29 +0000136{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000137 if (!ctl->already_rendered)
138 {
139 ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf));
140 ctl->already_rendered = 1;
141 }
142 fprintf(fp, "%s ", ctl->buf);
paul718e3742002-12-13 20:15:29 +0000143}
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000144
paul718e3742002-12-13 20:15:29 +0000145
146/* va_list version of zlog. */
ajsd246bd92004-11-23 17:35:08 +0000147static void
148vzlog (struct zlog *zl, int priority, const char *format, va_list args)
paul718e3742002-12-13 20:15:29 +0000149{
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000150 struct timestamp_control tsctl;
151 tsctl.already_rendered = 0;
152
paul718e3742002-12-13 20:15:29 +0000153 /* If zlog is not specified, use default one. */
154 if (zl == NULL)
155 zl = zlog_default;
156
157 /* When zlog_default is also NULL, use stderr for logging. */
158 if (zl == NULL)
159 {
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000160 tsctl.precision = 0;
161 time_print(stderr, &tsctl);
paul718e3742002-12-13 20:15:29 +0000162 fprintf (stderr, "%s: ", "unknown");
ajsd246bd92004-11-23 17:35:08 +0000163 vfprintf (stderr, format, args);
paul718e3742002-12-13 20:15:29 +0000164 fprintf (stderr, "\n");
165 fflush (stderr);
166
167 /* In this case we return at here. */
168 return;
169 }
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000170 tsctl.precision = zl->timestamp_precision;
paul718e3742002-12-13 20:15:29 +0000171
paul718e3742002-12-13 20:15:29 +0000172 /* Syslog output */
ajs274a4a42004-12-07 15:39:31 +0000173 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
ajsd246bd92004-11-23 17:35:08 +0000174 {
175 va_list ac;
176 va_copy(ac, args);
177 vsyslog (priority|zlog_default->facility, format, ac);
178 va_end(ac);
179 }
paul718e3742002-12-13 20:15:29 +0000180
181 /* File output. */
ajs274a4a42004-12-07 15:39:31 +0000182 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
paul718e3742002-12-13 20:15:29 +0000183 {
ajsd246bd92004-11-23 17:35:08 +0000184 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000185 time_print (zl->fp, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000186 if (zl->record_priority)
187 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000188 fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000189 va_copy(ac, args);
190 vfprintf (zl->fp, format, ac);
191 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000192 fprintf (zl->fp, "\n");
193 fflush (zl->fp);
194 }
195
196 /* stdout output. */
ajs274a4a42004-12-07 15:39:31 +0000197 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
paul718e3742002-12-13 20:15:29 +0000198 {
ajsd246bd92004-11-23 17:35:08 +0000199 va_list ac;
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000200 time_print (stdout, &tsctl);
hassob04c6992004-10-04 19:10:31 +0000201 if (zl->record_priority)
202 fprintf (stdout, "%s: ", zlog_priority[priority]);
paul718e3742002-12-13 20:15:29 +0000203 fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
ajsd246bd92004-11-23 17:35:08 +0000204 va_copy(ac, args);
205 vfprintf (stdout, format, ac);
206 va_end(ac);
paul718e3742002-12-13 20:15:29 +0000207 fprintf (stdout, "\n");
208 fflush (stdout);
209 }
210
paul718e3742002-12-13 20:15:29 +0000211 /* Terminal monitor. */
ajs274a4a42004-12-07 15:39:31 +0000212 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
213 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000214 zlog_proto_names[zl->protocol], format, &tsctl, args);
paul718e3742002-12-13 20:15:29 +0000215}
216
ajs59a06a92004-11-23 18:19:14 +0000217static char *
218str_append(char *dst, int len, const char *src)
219{
220 while ((len-- > 0) && *src)
221 *dst++ = *src++;
222 return dst;
223}
224
225static char *
226num_append(char *s, int len, u_long x)
227{
228 char buf[30];
ajs7d149b82004-11-28 23:00:01 +0000229 char *t;
ajs59a06a92004-11-23 18:19:14 +0000230
ajs7d149b82004-11-28 23:00:01 +0000231 if (!x)
232 return str_append(s,len,"0");
233 *(t = &buf[sizeof(buf)-1]) = '\0';
ajs59a06a92004-11-23 18:19:14 +0000234 while (x && (t > buf))
235 {
236 *--t = '0'+(x % 10);
237 x /= 10;
238 }
239 return str_append(s,len,t);
240}
241
Paul Jakmafb66b292006-05-28 08:26:15 +0000242#if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
ajs7d149b82004-11-28 23:00:01 +0000243static char *
244hex_append(char *s, int len, u_long x)
245{
246 char buf[30];
247 char *t;
248
249 if (!x)
250 return str_append(s,len,"0");
251 *(t = &buf[sizeof(buf)-1]) = '\0';
252 while (x && (t > buf))
253 {
254 u_int cc = (x % 16);
255 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
256 x /= 16;
257 }
258 return str_append(s,len,t);
259}
ajs31364272005-01-18 22:18:59 +0000260#endif
ajs7d149b82004-11-28 23:00:01 +0000261
ajs7d149b82004-11-28 23:00:01 +0000262/* Needs to be enhanced to support Solaris. */
263static int
264syslog_connect(void)
265{
266#ifdef SUNOS_5
267 return -1;
268#else
269 int fd;
270 char *s;
271 struct sockaddr_un addr;
272
273 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
274 return -1;
275 addr.sun_family = AF_UNIX;
276#ifdef _PATH_LOG
277#define SYSLOG_SOCKET_PATH _PATH_LOG
278#else
279#define SYSLOG_SOCKET_PATH "/dev/log"
280#endif
281 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
282#undef SYSLOG_SOCKET_PATH
283 *s = '\0';
284 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
285 {
286 close(fd);
287 return -1;
288 }
289 return fd;
290#endif
291}
292
293static void
294syslog_sigsafe(int priority, const char *msg, size_t msglen)
295{
ajs1e221352005-02-03 16:42:40 +0000296 static int syslog_fd = -1;
ajs7d149b82004-11-28 23:00:01 +0000297 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
298 char *s;
299
300 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
301 return;
302
303#define LOC s,buf+sizeof(buf)-s
304 s = buf;
305 s = str_append(LOC,"<");
306 s = num_append(LOC,priority);
307 s = str_append(LOC,">");
308 /* forget about the timestamp, too difficult in a signal handler */
309 s = str_append(LOC,zlog_default->ident);
310 if (zlog_default->syslog_options & LOG_PID)
311 {
312 s = str_append(LOC,"[");
313 s = num_append(LOC,getpid());
314 s = str_append(LOC,"]");
315 }
316 s = str_append(LOC,": ");
317 s = str_append(LOC,msg);
318 write(syslog_fd,buf,s-buf);
319#undef LOC
320}
321
ajs1e221352005-02-03 16:42:40 +0000322static int
323open_crashlog(void)
324{
325#define CRASHLOG_PREFIX "/var/tmp/quagga."
326#define CRASHLOG_SUFFIX "crashlog"
327 if (zlog_default && zlog_default->ident)
328 {
329 /* Avoid strlen since it is not async-signal-safe. */
330 const char *p;
331 size_t ilen;
332
333 for (p = zlog_default->ident, ilen = 0; *p; p++)
334 ilen++;
335 {
336 char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3];
337 char *s = buf;
338#define LOC s,buf+sizeof(buf)-s
339 s = str_append(LOC, CRASHLOG_PREFIX);
340 s = str_append(LOC, zlog_default->ident);
341 s = str_append(LOC, ".");
342 s = str_append(LOC, CRASHLOG_SUFFIX);
343#undef LOC
344 *s = '\0';
345 return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK);
346 }
347 }
348 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL,
349 LOGFILE_MASK);
350#undef CRASHLOG_SUFFIX
351#undef CRASHLOG_PREFIX
352}
353
ajs7d149b82004-11-28 23:00:01 +0000354/* Note: the goal here is to use only async-signal-safe functions. */
ajs59a06a92004-11-23 18:19:14 +0000355void
ajs31364272005-01-18 22:18:59 +0000356zlog_signal(int signo, const char *action
357#ifdef SA_SIGINFO
358 , siginfo_t *siginfo, void *program_counter
359#endif
360 )
ajs59a06a92004-11-23 18:19:14 +0000361{
362 time_t now;
ajs40abf232005-01-12 17:27:27 +0000363 char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
ajs59a06a92004-11-23 18:19:14 +0000364 char *s = buf;
ajs7d149b82004-11-28 23:00:01 +0000365 char *msgstart = buf;
ajs59a06a92004-11-23 18:19:14 +0000366#define LOC s,buf+sizeof(buf)-s
367
368 time(&now);
369 if (zlog_default)
370 {
371 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
372 *s++ = ':';
373 *s++ = ' ';
ajs7d149b82004-11-28 23:00:01 +0000374 msgstart = s;
ajs59a06a92004-11-23 18:19:14 +0000375 }
376 s = str_append(LOC,"Received signal ");
377 s = num_append(LOC,signo);
378 s = str_append(LOC," at ");
379 s = num_append(LOC,now);
ajs31364272005-01-18 22:18:59 +0000380#ifdef SA_SIGINFO
ajs40abf232005-01-12 17:27:27 +0000381 s = str_append(LOC," (si_addr 0x");
382 s = hex_append(LOC,(u_long)(siginfo->si_addr));
383 if (program_counter)
384 {
385 s = str_append(LOC,", PC 0x");
386 s = hex_append(LOC,(u_long)program_counter);
387 }
388 s = str_append(LOC,"); ");
ajs31364272005-01-18 22:18:59 +0000389#else /* SA_SIGINFO */
390 s = str_append(LOC,"; ");
391#endif /* SA_SIGINFO */
ajs59a06a92004-11-23 18:19:14 +0000392 s = str_append(LOC,action);
ajs7d149b82004-11-28 23:00:01 +0000393 if (s < buf+sizeof(buf))
394 *s++ = '\n';
ajs59a06a92004-11-23 18:19:14 +0000395
ajs274a4a42004-12-07 15:39:31 +0000396 /* N.B. implicit priority is most severe */
ajs1e221352005-02-03 16:42:40 +0000397#define PRI LOG_CRIT
ajs274a4a42004-12-07 15:39:31 +0000398
ajs1e221352005-02-03 16:42:40 +0000399#define DUMP(FD) write(FD, buf, s-buf);
400 /* If no file logging configured, try to write to fallback log file. */
ajsc4c7d0c2005-02-03 19:22:05 +0000401 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
402 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000403 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000404 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000405 else
406 {
ajs274a4a42004-12-07 15:39:31 +0000407 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000408 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000409 /* Remove trailing '\n' for monitor and syslog */
410 *--s = '\0';
411 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
412 vty_log_fixed(buf,s-buf);
413 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
414 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
ajs59a06a92004-11-23 18:19:14 +0000415 }
416#undef DUMP
417
ajs31364272005-01-18 22:18:59 +0000418 zlog_backtrace_sigsafe(PRI,
419#ifdef SA_SIGINFO
420 program_counter
421#else
422 NULL
423#endif
424 );
ajs274a4a42004-12-07 15:39:31 +0000425#undef PRI
ajs063ee522004-11-26 18:11:14 +0000426#undef LOC
427}
ajs59a06a92004-11-23 18:19:14 +0000428
ajs063ee522004-11-26 18:11:14 +0000429/* Log a backtrace using only async-signal-safe functions.
430 Needs to be enhanced to support syslog logging. */
431void
ajs239c26f2005-01-17 15:22:28 +0000432zlog_backtrace_sigsafe(int priority, void *program_counter)
ajs063ee522004-11-26 18:11:14 +0000433{
Paul Jakmafb66b292006-05-28 08:26:15 +0000434#ifdef HAVE_STACK_TRACE
ajs239c26f2005-01-17 15:22:28 +0000435 static const char pclabel[] = "Program counter: ";
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700436 void *array[64];
ajs063ee522004-11-26 18:11:14 +0000437 int size;
438 char buf[100];
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700439 char *s, **bt = NULL;
ajs063ee522004-11-26 18:11:14 +0000440#define LOC s,buf+sizeof(buf)-s
441
Paul Jakmafb66b292006-05-28 08:26:15 +0000442#ifdef HAVE_GLIBC_BACKTRACE
ajs063ee522004-11-26 18:11:14 +0000443 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
444 ((size_t)size > sizeof(array)/sizeof(array[0])))
445 return;
ajs59a06a92004-11-23 18:19:14 +0000446
ajs1e221352005-02-03 16:42:40 +0000447#define DUMP(FD) { \
ajs239c26f2005-01-17 15:22:28 +0000448 if (program_counter) \
449 { \
ajs1e221352005-02-03 16:42:40 +0000450 write(FD, pclabel, sizeof(pclabel)-1); \
451 backtrace_symbols_fd(&program_counter, 1, FD); \
ajs239c26f2005-01-17 15:22:28 +0000452 } \
ajs1e221352005-02-03 16:42:40 +0000453 write(FD, buf, s-buf); \
454 backtrace_symbols_fd(array, size, FD); \
ajs59a06a92004-11-23 18:19:14 +0000455}
Paul Jakmafb66b292006-05-28 08:26:15 +0000456#elif defined(HAVE_PRINTSTACK)
457#define DUMP(FD) { \
458 if (program_counter) \
459 write((FD), pclabel, sizeof(pclabel)-1); \
460 write((FD), buf, s-buf); \
461 printstack((FD)); \
462}
463#endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
464
465 s = buf;
466 s = str_append(LOC,"Backtrace for ");
467 s = num_append(LOC,size);
468 s = str_append(LOC," stack frames:\n");
ajs59a06a92004-11-23 18:19:14 +0000469
ajsc4c7d0c2005-02-03 19:22:05 +0000470 if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0))
471 DUMP(logfile_fd)
ajs59a06a92004-11-23 18:19:14 +0000472 if (!zlog_default)
ajsc4c7d0c2005-02-03 19:22:05 +0000473 DUMP(STDERR_FILENO)
ajs59a06a92004-11-23 18:19:14 +0000474 else
475 {
ajs274a4a42004-12-07 15:39:31 +0000476 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
ajsc4c7d0c2005-02-03 19:22:05 +0000477 DUMP(STDOUT_FILENO)
ajs274a4a42004-12-07 15:39:31 +0000478 /* Remove trailing '\n' for monitor and syslog */
479 *--s = '\0';
480 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
481 vty_log_fixed(buf,s-buf);
482 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
483 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
484 {
485 int i;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700486#ifdef HAVE_GLIBC_BACKTRACE
487 bt = backtrace_symbols(array, size);
488#endif
ajs274a4a42004-12-07 15:39:31 +0000489 /* Just print the function addresses. */
490 for (i = 0; i < size; i++)
491 {
492 s = buf;
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700493 if (bt)
494 s = str_append(LOC, bt[i]);
495 else {
496 s = str_append(LOC,"[bt ");
497 s = num_append(LOC,i);
498 s = str_append(LOC,"] 0x");
499 s = hex_append(LOC,(u_long)(array[i]));
500 }
ajs274a4a42004-12-07 15:39:31 +0000501 *s = '\0';
502 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
503 vty_log_fixed(buf,s-buf);
504 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
ajs7d149b82004-11-28 23:00:01 +0000505 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
ajs274a4a42004-12-07 15:39:31 +0000506 }
Stephen Hemminger94fc1dd2009-03-09 16:09:50 -0700507 if (bt)
508 free(bt);
ajs274a4a42004-12-07 15:39:31 +0000509 }
ajs59a06a92004-11-23 18:19:14 +0000510 }
511#undef DUMP
ajs59a06a92004-11-23 18:19:14 +0000512#undef LOC
Paul Jakmafb66b292006-05-28 08:26:15 +0000513#endif /* HAVE_STRACK_TRACE */
ajs063ee522004-11-26 18:11:14 +0000514}
515
516void
517zlog_backtrace(int priority)
518{
519#ifndef HAVE_GLIBC_BACKTRACE
520 zlog(NULL, priority, "No backtrace available on this platform.");
521#else
522 void *array[20];
523 int size, i;
524 char **strings;
525
526 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
527 ((size_t)size > sizeof(array)/sizeof(array[0])))
528 {
529 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
Andrew J. Schorr1ed72e02007-04-28 22:14:10 +0000530 "(valid range is between 1 and %lu)",
531 size, (unsigned long)(sizeof(array)/sizeof(array[0])));
ajs063ee522004-11-26 18:11:14 +0000532 return;
533 }
534 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
535 if (!(strings = backtrace_symbols(array, size)))
536 {
537 zlog_err("Cannot get backtrace symbols (out of memory?)");
538 for (i = 0; i < size; i++)
539 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
540 }
541 else
542 {
543 for (i = 0; i < size; i++)
544 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
545 free(strings);
546 }
547#endif /* HAVE_GLIBC_BACKTRACE */
ajs59a06a92004-11-23 18:19:14 +0000548}
549
paul718e3742002-12-13 20:15:29 +0000550void
551zlog (struct zlog *zl, int priority, const char *format, ...)
552{
ajsd246bd92004-11-23 17:35:08 +0000553 va_list args;
paul718e3742002-12-13 20:15:29 +0000554
ajsd246bd92004-11-23 17:35:08 +0000555 va_start(args, format);
paul718e3742002-12-13 20:15:29 +0000556 vzlog (zl, priority, format, args);
ajsd246bd92004-11-23 17:35:08 +0000557 va_end (args);
paul718e3742002-12-13 20:15:29 +0000558}
559
ajsd246bd92004-11-23 17:35:08 +0000560#define ZLOG_FUNC(FUNCNAME,PRIORITY) \
561void \
562FUNCNAME(const char *format, ...) \
563{ \
564 va_list args; \
565 va_start(args, format); \
566 vzlog (NULL, PRIORITY, format, args); \
567 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000568}
569
ajsd246bd92004-11-23 17:35:08 +0000570ZLOG_FUNC(zlog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000571
ajsd246bd92004-11-23 17:35:08 +0000572ZLOG_FUNC(zlog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000573
ajsd246bd92004-11-23 17:35:08 +0000574ZLOG_FUNC(zlog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000575
ajsd246bd92004-11-23 17:35:08 +0000576ZLOG_FUNC(zlog_notice, LOG_NOTICE)
577
578ZLOG_FUNC(zlog_debug, LOG_DEBUG)
579
580#undef ZLOG_FUNC
581
582#define PLOG_FUNC(FUNCNAME,PRIORITY) \
583void \
584FUNCNAME(struct zlog *zl, const char *format, ...) \
585{ \
586 va_list args; \
587 va_start(args, format); \
588 vzlog (zl, PRIORITY, format, args); \
589 va_end(args); \
paul718e3742002-12-13 20:15:29 +0000590}
591
ajsd246bd92004-11-23 17:35:08 +0000592PLOG_FUNC(plog_err, LOG_ERR)
paul718e3742002-12-13 20:15:29 +0000593
ajsd246bd92004-11-23 17:35:08 +0000594PLOG_FUNC(plog_warn, LOG_WARNING)
paul718e3742002-12-13 20:15:29 +0000595
ajsd246bd92004-11-23 17:35:08 +0000596PLOG_FUNC(plog_info, LOG_INFO)
paul718e3742002-12-13 20:15:29 +0000597
ajsd246bd92004-11-23 17:35:08 +0000598PLOG_FUNC(plog_notice, LOG_NOTICE)
paul718e3742002-12-13 20:15:29 +0000599
ajsd246bd92004-11-23 17:35:08 +0000600PLOG_FUNC(plog_debug, LOG_DEBUG)
paul718e3742002-12-13 20:15:29 +0000601
ajsd246bd92004-11-23 17:35:08 +0000602#undef PLOG_FUNC
paul718e3742002-12-13 20:15:29 +0000603
ajscee3df12004-11-24 17:14:49 +0000604void
605_zlog_assert_failed (const char *assertion, const char *file,
606 unsigned int line, const char *function)
607{
ajsc4c7d0c2005-02-03 19:22:05 +0000608 /* Force fallback file logging? */
609 if (zlog_default && !zlog_default->fp &&
610 ((logfile_fd = open_crashlog()) >= 0) &&
611 ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
612 zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
ajs1e221352005-02-03 16:42:40 +0000613 zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
614 assertion,file,line,(function ? function : "?"));
615 zlog_backtrace(LOG_CRIT);
ajscee3df12004-11-24 17:14:49 +0000616 abort();
617}
618
paul718e3742002-12-13 20:15:29 +0000619
620/* Open log stream */
621struct zlog *
ajs274a4a42004-12-07 15:39:31 +0000622openzlog (const char *progname, zlog_proto_t protocol,
paul718e3742002-12-13 20:15:29 +0000623 int syslog_flags, int syslog_facility)
624{
625 struct zlog *zl;
ajs274a4a42004-12-07 15:39:31 +0000626 u_int i;
paul718e3742002-12-13 20:15:29 +0000627
ajs274a4a42004-12-07 15:39:31 +0000628 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
paul718e3742002-12-13 20:15:29 +0000629
630 zl->ident = progname;
paul718e3742002-12-13 20:15:29 +0000631 zl->protocol = protocol;
632 zl->facility = syslog_facility;
ajs7d149b82004-11-28 23:00:01 +0000633 zl->syslog_options = syslog_flags;
paul718e3742002-12-13 20:15:29 +0000634
ajs274a4a42004-12-07 15:39:31 +0000635 /* Set default logging levels. */
636 for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++)
637 zl->maxlvl[i] = ZLOG_DISABLED;
638 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
639 zl->default_lvl = LOG_DEBUG;
640
paul718e3742002-12-13 20:15:29 +0000641 openlog (progname, syslog_flags, zl->facility);
642
643 return zl;
644}
645
646void
647closezlog (struct zlog *zl)
648{
649 closelog();
Chris Caputo228da422009-07-18 05:44:03 +0000650
651 if (zl->fp != NULL)
652 fclose (zl->fp);
paul718e3742002-12-13 20:15:29 +0000653
Tom Goffcd3355b2010-11-10 13:01:17 -0800654 if (zl->filename != NULL)
655 free (zl->filename);
656
paul718e3742002-12-13 20:15:29 +0000657 XFREE (MTYPE_ZLOG, zl);
658}
659
660/* Called from command.c. */
661void
ajs274a4a42004-12-07 15:39:31 +0000662zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
paul718e3742002-12-13 20:15:29 +0000663{
664 if (zl == NULL)
665 zl = zlog_default;
666
ajs274a4a42004-12-07 15:39:31 +0000667 zl->maxlvl[dest] = log_level;
paul718e3742002-12-13 20:15:29 +0000668}
669
670int
ajs274a4a42004-12-07 15:39:31 +0000671zlog_set_file (struct zlog *zl, const char *filename, int log_level)
paul718e3742002-12-13 20:15:29 +0000672{
673 FILE *fp;
gdtaa593d52003-12-22 20:15:53 +0000674 mode_t oldumask;
paul718e3742002-12-13 20:15:29 +0000675
676 /* There is opend file. */
677 zlog_reset_file (zl);
678
679 /* Set default zl. */
680 if (zl == NULL)
681 zl = zlog_default;
682
683 /* Open file. */
gdtaa593d52003-12-22 20:15:53 +0000684 oldumask = umask (0777 & ~LOGFILE_MASK);
paul718e3742002-12-13 20:15:29 +0000685 fp = fopen (filename, "a");
gdtaa593d52003-12-22 20:15:53 +0000686 umask(oldumask);
ajs274a4a42004-12-07 15:39:31 +0000687 if (fp == NULL)
688 return 0;
paul718e3742002-12-13 20:15:29 +0000689
690 /* Set flags. */
691 zl->filename = strdup (filename);
ajs274a4a42004-12-07 15:39:31 +0000692 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
paul718e3742002-12-13 20:15:29 +0000693 zl->fp = fp;
ajsc4c7d0c2005-02-03 19:22:05 +0000694 logfile_fd = fileno(fp);
paul718e3742002-12-13 20:15:29 +0000695
696 return 1;
697}
698
699/* Reset opend file. */
700int
701zlog_reset_file (struct zlog *zl)
702{
703 if (zl == NULL)
704 zl = zlog_default;
705
paul718e3742002-12-13 20:15:29 +0000706 if (zl->fp)
707 fclose (zl->fp);
708 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000709 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000710 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000711
712 if (zl->filename)
713 free (zl->filename);
714 zl->filename = NULL;
715
716 return 1;
717}
718
719/* Reopen log file. */
720int
721zlog_rotate (struct zlog *zl)
722{
ajs274a4a42004-12-07 15:39:31 +0000723 int level;
paul718e3742002-12-13 20:15:29 +0000724
725 if (zl == NULL)
726 zl = zlog_default;
727
728 if (zl->fp)
729 fclose (zl->fp);
730 zl->fp = NULL;
ajsc4c7d0c2005-02-03 19:22:05 +0000731 logfile_fd = -1;
ajs274a4a42004-12-07 15:39:31 +0000732 level = zl->maxlvl[ZLOG_DEST_FILE];
733 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
paul718e3742002-12-13 20:15:29 +0000734
735 if (zl->filename)
736 {
gdtaa593d52003-12-22 20:15:53 +0000737 mode_t oldumask;
ajs274a4a42004-12-07 15:39:31 +0000738 int save_errno;
gdtaa593d52003-12-22 20:15:53 +0000739
740 oldumask = umask (0777 & ~LOGFILE_MASK);
ajs274a4a42004-12-07 15:39:31 +0000741 zl->fp = fopen (zl->filename, "a");
742 save_errno = errno;
743 umask(oldumask);
744 if (zl->fp == NULL)
gdtaa593d52003-12-22 20:15:53 +0000745 {
ajs274a4a42004-12-07 15:39:31 +0000746 zlog_err("Log rotate failed: cannot open file %s for append: %s",
747 zl->filename, safe_strerror(save_errno));
gdtaa593d52003-12-22 20:15:53 +0000748 return -1;
749 }
ajsc4c7d0c2005-02-03 19:22:05 +0000750 logfile_fd = fileno(zl->fp);
ajs274a4a42004-12-07 15:39:31 +0000751 zl->maxlvl[ZLOG_DEST_FILE] = level;
paul718e3742002-12-13 20:15:29 +0000752 }
753
754 return 1;
755}
756
paul718e3742002-12-13 20:15:29 +0000757/* Message lookup function. */
hasso8c328f12004-10-05 21:01:23 +0000758const char *
Stephen Hemminger1423c802008-08-14 17:59:25 +0100759lookup (const struct message *mes, int key)
paul718e3742002-12-13 20:15:29 +0000760{
Stephen Hemminger1423c802008-08-14 17:59:25 +0100761 const struct message *pnt;
paul718e3742002-12-13 20:15:29 +0000762
763 for (pnt = mes; pnt->key != 0; pnt++)
764 if (pnt->key == key)
765 return pnt->str;
766
767 return "";
768}
769
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000770/* Older/faster version of message lookup function, but requires caller to pass
Paul Jakma11486b52008-02-28 23:26:02 +0000771 * in the array size (instead of relying on a 0 key to terminate the search).
772 *
773 * The return value is the message string if found, or the 'none' pointer
774 * provided otherwise.
775 */
hasso8c328f12004-10-05 21:01:23 +0000776const char *
Stephen Hemminger8e4c0932009-05-15 09:47:34 -0700777mes_lookup (const struct message *meslist, int max, int index, const char *none)
paul718e3742002-12-13 20:15:29 +0000778{
Paul Jakma11486b52008-02-28 23:26:02 +0000779 int pos = index - meslist[0].key;
780
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000781 /* first check for best case: index is in range and matches the key
Paul Jakma11486b52008-02-28 23:26:02 +0000782 * value in that slot.
783 * NB: key numbering might be offset from 0. E.g. protocol constants
784 * often start at 1.
785 */
786 if ((pos >= 0) && (pos < max)
787 && (meslist[pos].key == index))
788 return meslist[pos].str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000789
790 /* fall back to linear search */
791 {
792 int i;
793
794 for (i = 0; i < max; i++, meslist++)
795 {
796 if (meslist->key == index)
797 {
Paul Jakma11486b52008-02-28 23:26:02 +0000798 const char *str = (meslist->str ? meslist->str : none);
799
Paul Jakma6d582722007-08-06 15:21:45 +0000800 zlog_debug ("message index %d [%s] found in position %d (max is %d)",
Paul Jakma11486b52008-02-28 23:26:02 +0000801 index, str, i, max);
802 return str;
Andrew J. Schorrafb88a62007-03-20 20:48:27 +0000803 }
804 }
805 }
806 zlog_err("message index %d not found (max is %d)", index, max);
Paul Jakma11486b52008-02-28 23:26:02 +0000807 assert (none);
808 return none;
paul718e3742002-12-13 20:15:29 +0000809}
ajsca359762004-11-19 23:40:16 +0000810
811/* Wrapper around strerror to handle case where it returns NULL. */
812const char *
813safe_strerror(int errnum)
814{
815 const char *s = strerror(errnum);
816 return (s != NULL) ? s : "Unknown error";
817}
ajsf52d13c2005-10-01 17:38:06 +0000818
Paul Jakmad6d672a2006-05-15 16:56:51 +0000819struct zebra_desc_table
820{
821 unsigned int type;
ajsf52d13c2005-10-01 17:38:06 +0000822 const char *string;
823 char chr;
ajsf52d13c2005-10-01 17:38:06 +0000824};
825
Paul Jakmad6d672a2006-05-15 16:56:51 +0000826#define DESC_ENTRY(T,S,C) [(T)] = { (T), (S), (C) }
827static const struct zebra_desc_table route_types[] = {
828 DESC_ENTRY (ZEBRA_ROUTE_SYSTEM, "system", 'X' ),
829 DESC_ENTRY (ZEBRA_ROUTE_KERNEL, "kernel", 'K' ),
830 DESC_ENTRY (ZEBRA_ROUTE_CONNECT, "connected", 'C' ),
831 DESC_ENTRY (ZEBRA_ROUTE_STATIC, "static", 'S' ),
832 DESC_ENTRY (ZEBRA_ROUTE_RIP, "rip", 'R' ),
833 DESC_ENTRY (ZEBRA_ROUTE_RIPNG, "ripng", 'R' ),
834 DESC_ENTRY (ZEBRA_ROUTE_OSPF, "ospf", 'O' ),
835 DESC_ENTRY (ZEBRA_ROUTE_OSPF6, "ospf6", 'O' ),
836 DESC_ENTRY (ZEBRA_ROUTE_ISIS, "isis", 'I' ),
837 DESC_ENTRY (ZEBRA_ROUTE_BGP, "bgp", 'B' ),
838 DESC_ENTRY (ZEBRA_ROUTE_HSLS, "hsls", 'H' ),
839};
840#undef DESC_ENTRY
841
842#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
843static const struct zebra_desc_table command_types[] = {
844 DESC_ENTRY (ZEBRA_INTERFACE_ADD),
845 DESC_ENTRY (ZEBRA_INTERFACE_DELETE),
846 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD),
847 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE),
848 DESC_ENTRY (ZEBRA_INTERFACE_UP),
849 DESC_ENTRY (ZEBRA_INTERFACE_DOWN),
850 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD),
851 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE),
852 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD),
853 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE),
854 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD),
855 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE),
856 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
857 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
858 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP),
859 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP),
860 DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP),
861 DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP),
862 DESC_ENTRY (ZEBRA_INTERFACE_RENAME),
863 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
864 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
865 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
866};
867#undef DESC_ENTRY
868
869static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
870
871static const struct zebra_desc_table *
ajsf52d13c2005-10-01 17:38:06 +0000872zroute_lookup(u_int zroute)
873{
ajsf52d13c2005-10-01 17:38:06 +0000874 u_int i;
875
876 if (zroute >= sizeof(route_types)/sizeof(route_types[0]))
877 {
878 zlog_err("unknown zebra route type: %u", zroute);
879 return &unknown;
880 }
Paul Jakmad6d672a2006-05-15 16:56:51 +0000881 if (zroute == route_types[zroute].type)
ajsf52d13c2005-10-01 17:38:06 +0000882 return &route_types[zroute];
883 for (i = 0; i < sizeof(route_types)/sizeof(route_types[0]); i++)
884 {
Paul Jakmad6d672a2006-05-15 16:56:51 +0000885 if (zroute == route_types[i].type)
ajsf52d13c2005-10-01 17:38:06 +0000886 {
887 zlog_warn("internal error: route type table out of order "
888 "while searching for %u, please notify developers", zroute);
889 return &route_types[i];
890 }
891 }
892 zlog_err("internal error: cannot find route type %u in table!", zroute);
893 return &unknown;
894}
895
896const char *
897zebra_route_string(u_int zroute)
898{
899 return zroute_lookup(zroute)->string;
900}
901
902char
903zebra_route_char(u_int zroute)
904{
905 return zroute_lookup(zroute)->chr;
906}
Paul Jakmad6d672a2006-05-15 16:56:51 +0000907
908const char *
909zserv_command_string (unsigned int command)
910{
911 if (command >= sizeof(command_types)/sizeof(command_types[0]))
912 {
913 zlog_err ("unknown zserv command type: %u", command);
914 return unknown.string;
915 }
916 return command_types[command].string;
917}
Paul Jakma7514fb72007-05-02 16:05:35 +0000918
919#define RTSIZE (sizeof(route_types)/sizeof(route_types[0]))
920
921int
922proto_name2num(const char *s)
923{
924 unsigned i;
925
926 for (i=0; i<RTSIZE; ++i)
927 if (strcasecmp(s, route_types[i].string) == 0)
928 return route_types[i].type;
929 return -1;
930}
931#undef RTSIZE