]> git.proxmox.com Git - mirror_frr.git/blob - lib/log.c
b68896cac8ae66f89681d613be50dae6c1944f05
[mirror_frr.git] / lib / log.c
1 /*
2 * $Id: log.c,v 1.17 2004/12/07 15:39:32 ajs Exp $
3 *
4 * Logging of zebra
5 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
6 *
7 * This file is part of GNU Zebra.
8 *
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Zebra; see the file COPYING. If not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 * 02111-1307, USA.
23 */
24
25 #include <zebra.h>
26
27 #include "log.h"
28 #include "memory.h"
29 #include "command.h"
30 #ifndef SUNOS_5
31 #include <sys/un.h>
32 #endif
33
34 struct zlog *zlog_default = NULL;
35
36 const char *zlog_proto_names[] =
37 {
38 "NONE",
39 "DEFAULT",
40 "ZEBRA",
41 "RIP",
42 "BGP",
43 "OSPF",
44 "RIPNG",
45 "OSPF6",
46 "ISIS",
47 "MASC",
48 NULL,
49 };
50
51 const char *zlog_priority[] =
52 {
53 "emergencies",
54 "alerts",
55 "critical",
56 "errors",
57 "warnings",
58 "notifications",
59 "informational",
60 "debugging",
61 NULL,
62 };
63
64
65 \f
66 /* For time string format. */
67 #define TIME_BUF 27
68
69 /* Utility routine for current time printing. */
70 static void
71 time_print (FILE *fp)
72 {
73 int ret;
74 char buf [TIME_BUF];
75 time_t clock;
76 struct tm *tm;
77
78 time (&clock);
79 tm = localtime (&clock);
80
81 ret = strftime (buf, TIME_BUF, "%Y/%m/%d %H:%M:%S", tm);
82 if (ret == 0) {
83 zlog_warn ("strftime error");
84 }
85
86 fprintf (fp, "%s ", buf);
87 }
88 \f
89 /* va_list version of zlog. */
90 static void
91 vzlog (struct zlog *zl, int priority, const char *format, va_list args)
92 {
93 /* If zlog is not specified, use default one. */
94 if (zl == NULL)
95 zl = zlog_default;
96
97 /* When zlog_default is also NULL, use stderr for logging. */
98 if (zl == NULL)
99 {
100 time_print (stderr);
101 fprintf (stderr, "%s: ", "unknown");
102 vfprintf (stderr, format, args);
103 fprintf (stderr, "\n");
104 fflush (stderr);
105
106 /* In this case we return at here. */
107 return;
108 }
109
110 /* Syslog output */
111 if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG])
112 {
113 va_list ac;
114 va_copy(ac, args);
115 vsyslog (priority|zlog_default->facility, format, ac);
116 va_end(ac);
117 }
118
119 /* File output. */
120 if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp)
121 {
122 va_list ac;
123 time_print (zl->fp);
124 if (zl->record_priority)
125 fprintf (zl->fp, "%s: ", zlog_priority[priority]);
126 fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]);
127 va_copy(ac, args);
128 vfprintf (zl->fp, format, ac);
129 va_end(ac);
130 fprintf (zl->fp, "\n");
131 fflush (zl->fp);
132 }
133
134 /* stdout output. */
135 if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT])
136 {
137 va_list ac;
138 time_print (stdout);
139 if (zl->record_priority)
140 fprintf (stdout, "%s: ", zlog_priority[priority]);
141 fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]);
142 va_copy(ac, args);
143 vfprintf (stdout, format, ac);
144 va_end(ac);
145 fprintf (stdout, "\n");
146 fflush (stdout);
147 }
148
149 /* Terminal monitor. */
150 if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR])
151 vty_log ((zl->record_priority ? zlog_priority[priority] : NULL),
152 zlog_proto_names[zl->protocol], format, args);
153 }
154
155 static char *
156 str_append(char *dst, int len, const char *src)
157 {
158 while ((len-- > 0) && *src)
159 *dst++ = *src++;
160 return dst;
161 }
162
163 static char *
164 num_append(char *s, int len, u_long x)
165 {
166 char buf[30];
167 char *t;
168
169 if (!x)
170 return str_append(s,len,"0");
171 *(t = &buf[sizeof(buf)-1]) = '\0';
172 while (x && (t > buf))
173 {
174 *--t = '0'+(x % 10);
175 x /= 10;
176 }
177 return str_append(s,len,t);
178 }
179
180 static char *
181 hex_append(char *s, int len, u_long x)
182 {
183 char buf[30];
184 char *t;
185
186 if (!x)
187 return str_append(s,len,"0");
188 *(t = &buf[sizeof(buf)-1]) = '\0';
189 while (x && (t > buf))
190 {
191 u_int cc = (x % 16);
192 *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10));
193 x /= 16;
194 }
195 return str_append(s,len,t);
196 }
197
198 static int syslog_fd = -1;
199
200 /* Needs to be enhanced to support Solaris. */
201 static int
202 syslog_connect(void)
203 {
204 #ifdef SUNOS_5
205 return -1;
206 #else
207 int fd;
208 char *s;
209 struct sockaddr_un addr;
210
211 if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0)
212 return -1;
213 addr.sun_family = AF_UNIX;
214 #ifdef _PATH_LOG
215 #define SYSLOG_SOCKET_PATH _PATH_LOG
216 #else
217 #define SYSLOG_SOCKET_PATH "/dev/log"
218 #endif
219 s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH);
220 #undef SYSLOG_SOCKET_PATH
221 *s = '\0';
222 if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0)
223 {
224 close(fd);
225 return -1;
226 }
227 return fd;
228 #endif
229 }
230
231 static void
232 syslog_sigsafe(int priority, const char *msg, size_t msglen)
233 {
234 char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50];
235 char *s;
236
237 if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0))
238 return;
239
240 #define LOC s,buf+sizeof(buf)-s
241 s = buf;
242 s = str_append(LOC,"<");
243 s = num_append(LOC,priority);
244 s = str_append(LOC,">");
245 /* forget about the timestamp, too difficult in a signal handler */
246 s = str_append(LOC,zlog_default->ident);
247 if (zlog_default->syslog_options & LOG_PID)
248 {
249 s = str_append(LOC,"[");
250 s = num_append(LOC,getpid());
251 s = str_append(LOC,"]");
252 }
253 s = str_append(LOC,": ");
254 s = str_append(LOC,msg);
255 write(syslog_fd,buf,s-buf);
256 #undef LOC
257 }
258
259 /* Note: the goal here is to use only async-signal-safe functions. */
260 void
261 zlog_signal(int signo, const char *action)
262 {
263 time_t now;
264 char buf[sizeof("DEFAULT: Received signal S at T; aborting...")+60];
265 char *s = buf;
266 char *msgstart = buf;
267 #define LOC s,buf+sizeof(buf)-s
268
269 time(&now);
270 if (zlog_default)
271 {
272 s = str_append(LOC,zlog_proto_names[zlog_default->protocol]);
273 *s++ = ':';
274 *s++ = ' ';
275 msgstart = s;
276 }
277 s = str_append(LOC,"Received signal ");
278 s = num_append(LOC,signo);
279 s = str_append(LOC," at ");
280 s = num_append(LOC,now);
281 s = str_append(LOC,"; ");
282 s = str_append(LOC,action);
283 if (s < buf+sizeof(buf))
284 *s++ = '\n';
285
286 /* N.B. implicit priority is most severe */
287 #define PRI LOG_EMERG
288
289 #define DUMP(FP) write(fileno(FP),buf,s-buf);
290 if (!zlog_default)
291 DUMP(stderr)
292 else
293 {
294 if ((PRI <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && zlog_default->fp)
295 DUMP(zlog_default->fp)
296 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
297 DUMP(stdout)
298 /* Remove trailing '\n' for monitor and syslog */
299 *--s = '\0';
300 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
301 vty_log_fixed(buf,s-buf);
302 if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
303 syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart);
304 }
305 #undef DUMP
306
307 zlog_backtrace_sigsafe(PRI);
308 #undef PRI
309 #undef LOC
310 }
311
312 /* Log a backtrace using only async-signal-safe functions.
313 Needs to be enhanced to support syslog logging. */
314 void
315 zlog_backtrace_sigsafe(int priority)
316 {
317 #ifdef HAVE_GLIBC_BACKTRACE
318 void *array[20];
319 int size;
320 char buf[100];
321 char *s;
322 #define LOC s,buf+sizeof(buf)-s
323
324 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
325 ((size_t)size > sizeof(array)/sizeof(array[0])))
326 return;
327 s = buf;
328 s = str_append(LOC,"Backtrace for ");
329 s = num_append(LOC,size);
330 s = str_append(LOC," stack frames:\n");
331
332 #define DUMP(FP) { \
333 write(fileno(FP),buf,s-buf); \
334 backtrace_symbols_fd(array, size, fileno(FP)); \
335 }
336
337 if (!zlog_default)
338 DUMP(stderr)
339 else
340 {
341 if ((priority <= zlog_default->maxlvl[ZLOG_DEST_FILE]) &&
342 zlog_default->fp)
343 DUMP(zlog_default->fp)
344 if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT])
345 DUMP(stdout)
346 /* Remove trailing '\n' for monitor and syslog */
347 *--s = '\0';
348 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
349 vty_log_fixed(buf,s-buf);
350 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
351 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
352 {
353 int i;
354 /* Just print the function addresses. */
355 for (i = 0; i < size; i++)
356 {
357 s = buf;
358 s = str_append(LOC,"[bt ");
359 s = num_append(LOC,i);
360 s = str_append(LOC,"] 0x");
361 s = hex_append(LOC,(u_long)(array[i]));
362 *s = '\0';
363 if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
364 vty_log_fixed(buf,s-buf);
365 if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
366 syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
367 }
368 }
369 }
370 #undef DUMP
371 #undef LOC
372 #endif /* HAVE_GLIBC_BACKTRACE */
373 }
374
375 void
376 zlog_backtrace(int priority)
377 {
378 #ifndef HAVE_GLIBC_BACKTRACE
379 zlog(NULL, priority, "No backtrace available on this platform.");
380 #else
381 void *array[20];
382 int size, i;
383 char **strings;
384
385 if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
386 ((size_t)size > sizeof(array)/sizeof(array[0])))
387 {
388 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
389 "(valid range is between 1 and %u)",
390 size, sizeof(array)/sizeof(array[0]));
391 return;
392 }
393 zlog(NULL, priority, "Backtrace for %d stack frames:", size);
394 if (!(strings = backtrace_symbols(array, size)))
395 {
396 zlog_err("Cannot get backtrace symbols (out of memory?)");
397 for (i = 0; i < size; i++)
398 zlog(NULL, priority, "[bt %d] %p",i,array[i]);
399 }
400 else
401 {
402 for (i = 0; i < size; i++)
403 zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
404 free(strings);
405 }
406 #endif /* HAVE_GLIBC_BACKTRACE */
407 }
408
409 void
410 zlog (struct zlog *zl, int priority, const char *format, ...)
411 {
412 va_list args;
413
414 va_start(args, format);
415 vzlog (zl, priority, format, args);
416 va_end (args);
417 }
418
419 #define ZLOG_FUNC(FUNCNAME,PRIORITY) \
420 void \
421 FUNCNAME(const char *format, ...) \
422 { \
423 va_list args; \
424 va_start(args, format); \
425 vzlog (NULL, PRIORITY, format, args); \
426 va_end(args); \
427 }
428
429 ZLOG_FUNC(zlog_err, LOG_ERR)
430
431 ZLOG_FUNC(zlog_warn, LOG_WARNING)
432
433 ZLOG_FUNC(zlog_info, LOG_INFO)
434
435 ZLOG_FUNC(zlog_notice, LOG_NOTICE)
436
437 ZLOG_FUNC(zlog_debug, LOG_DEBUG)
438
439 #undef ZLOG_FUNC
440
441 #define PLOG_FUNC(FUNCNAME,PRIORITY) \
442 void \
443 FUNCNAME(struct zlog *zl, const char *format, ...) \
444 { \
445 va_list args; \
446 va_start(args, format); \
447 vzlog (zl, PRIORITY, format, args); \
448 va_end(args); \
449 }
450
451 PLOG_FUNC(plog_err, LOG_ERR)
452
453 PLOG_FUNC(plog_warn, LOG_WARNING)
454
455 PLOG_FUNC(plog_info, LOG_INFO)
456
457 PLOG_FUNC(plog_notice, LOG_NOTICE)
458
459 PLOG_FUNC(plog_debug, LOG_DEBUG)
460
461 #undef PLOG_FUNC
462
463 void
464 _zlog_assert_failed (const char *assertion, const char *file,
465 unsigned int line, const char *function)
466 {
467 zlog_err("Assertion `%s' failed in file %s, line %u, function %s",
468 assertion,file,line,(function ? function : "?"));
469 zlog_backtrace(LOG_EMERG);
470 abort();
471 }
472
473 \f
474 /* Open log stream */
475 struct zlog *
476 openzlog (const char *progname, zlog_proto_t protocol,
477 int syslog_flags, int syslog_facility)
478 {
479 struct zlog *zl;
480 u_int i;
481
482 zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
483
484 zl->ident = progname;
485 zl->protocol = protocol;
486 zl->facility = syslog_facility;
487 zl->syslog_options = syslog_flags;
488
489 /* Set default logging levels. */
490 for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++)
491 zl->maxlvl[i] = ZLOG_DISABLED;
492 zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
493 zl->default_lvl = LOG_DEBUG;
494
495 openlog (progname, syslog_flags, zl->facility);
496
497 return zl;
498 }
499
500 void
501 closezlog (struct zlog *zl)
502 {
503 closelog();
504 fclose (zl->fp);
505
506 XFREE (MTYPE_ZLOG, zl);
507 }
508
509 /* Called from command.c. */
510 void
511 zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
512 {
513 if (zl == NULL)
514 zl = zlog_default;
515
516 zl->maxlvl[dest] = log_level;
517 }
518
519 int
520 zlog_set_file (struct zlog *zl, const char *filename, int log_level)
521 {
522 FILE *fp;
523 mode_t oldumask;
524
525 /* There is opend file. */
526 zlog_reset_file (zl);
527
528 /* Set default zl. */
529 if (zl == NULL)
530 zl = zlog_default;
531
532 /* Open file. */
533 oldumask = umask (0777 & ~LOGFILE_MASK);
534 fp = fopen (filename, "a");
535 umask(oldumask);
536 if (fp == NULL)
537 return 0;
538
539 /* Set flags. */
540 zl->filename = strdup (filename);
541 zl->maxlvl[ZLOG_DEST_FILE] = log_level;
542 zl->fp = fp;
543
544 return 1;
545 }
546
547 /* Reset opend file. */
548 int
549 zlog_reset_file (struct zlog *zl)
550 {
551 if (zl == NULL)
552 zl = zlog_default;
553
554 if (zl->fp)
555 fclose (zl->fp);
556 zl->fp = NULL;
557 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
558
559 if (zl->filename)
560 free (zl->filename);
561 zl->filename = NULL;
562
563 return 1;
564 }
565
566 /* Reopen log file. */
567 int
568 zlog_rotate (struct zlog *zl)
569 {
570 int level;
571
572 if (zl == NULL)
573 zl = zlog_default;
574
575 if (zl->fp)
576 fclose (zl->fp);
577 zl->fp = NULL;
578 level = zl->maxlvl[ZLOG_DEST_FILE];
579 zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
580
581 if (zl->filename)
582 {
583 mode_t oldumask;
584 int save_errno;
585
586 oldumask = umask (0777 & ~LOGFILE_MASK);
587 zl->fp = fopen (zl->filename, "a");
588 save_errno = errno;
589 umask(oldumask);
590 if (zl->fp == NULL)
591 {
592 zlog_err("Log rotate failed: cannot open file %s for append: %s",
593 zl->filename, safe_strerror(save_errno));
594 return -1;
595 }
596 zl->maxlvl[ZLOG_DEST_FILE] = level;
597 }
598
599 return 1;
600 }
601 \f
602 /* Message lookup function. */
603 const char *
604 lookup (struct message *mes, int key)
605 {
606 struct message *pnt;
607
608 for (pnt = mes; pnt->key != 0; pnt++)
609 if (pnt->key == key)
610 return pnt->str;
611
612 return "";
613 }
614
615 /* Very old hacky version of message lookup function. Still partly
616 used in bgpd and ospfd. FIXME Seems that it's not used any more. */
617 const char *
618 mes_lookup (struct message *meslist, int max, int index)
619 {
620 if (index < 0 || index >= max)
621 {
622 zlog_err ("message index out of bound: %d", max);
623 return NULL;
624 }
625 return meslist[index].str;
626 }
627
628 /* Wrapper around strerror to handle case where it returns NULL. */
629 const char *
630 safe_strerror(int errnum)
631 {
632 const char *s = strerror(errnum);
633 return (s != NULL) ? s : "Unknown error";
634 }