5 * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro
7 * This file is part of GNU Zebra.
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
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.
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
34 static int logfile_fd
= -1; /* Used in signal handler. */
36 struct zlog
*zlog_default
= NULL
;
38 const char *zlog_proto_names
[] =
53 const char *zlog_priority
[] =
68 /* For time string format. */
71 quagga_timestamp(int timestamp_precision
, char *buf
, size_t buflen
)
80 /* would it be sufficient to use global 'recent_time' here? I fear not... */
81 gettimeofday(&clock
, NULL
);
83 /* first, we update the cache if the time has changed */
84 if (cache
.last
!= clock
.tv_sec
)
87 cache
.last
= clock
.tv_sec
;
88 tm
= localtime(&cache
.last
);
89 cache
.len
= strftime(cache
.buf
, sizeof(cache
.buf
),
90 "%Y/%m/%d %H:%M:%S", tm
);
92 /* note: it's not worth caching the subsecond part, because
93 chances are that back-to-back calls are not sufficiently close together
94 for the clock not to have ticked forward */
96 if (buflen
> cache
.len
)
98 memcpy(buf
, cache
.buf
, cache
.len
);
99 if ((timestamp_precision
> 0) &&
100 (buflen
> cache
.len
+1+timestamp_precision
))
102 /* should we worry about locale issues? */
103 static const int divisor
[] = {0, 100000, 10000, 1000, 100, 10, 1};
105 char *p
= buf
+cache
.len
+1+(prec
= timestamp_precision
);
108 /* this is unlikely to happen, but protect anyway */
113 clock
.tv_usec
/= divisor
[prec
];
116 *p
-- = '0'+(clock
.tv_usec
% 10);
121 return cache
.len
+1+timestamp_precision
;
123 buf
[cache
.len
] = '\0';
131 /* Utility routine for current time printing. */
133 time_print(FILE *fp
, struct timestamp_control
*ctl
)
135 if (!ctl
->already_rendered
)
137 ctl
->len
= quagga_timestamp(ctl
->precision
, ctl
->buf
, sizeof(ctl
->buf
));
138 ctl
->already_rendered
= 1;
140 fprintf(fp
, "%s ", ctl
->buf
);
144 /* va_list version of zlog. */
146 vzlog (struct zlog
*zl
, int priority
, const char *format
, va_list args
)
148 struct timestamp_control tsctl
;
149 tsctl
.already_rendered
= 0;
151 /* If zlog is not specified, use default one. */
155 /* When zlog_default is also NULL, use stderr for logging. */
159 time_print(stderr
, &tsctl
);
160 fprintf (stderr
, "%s: ", "unknown");
161 vfprintf (stderr
, format
, args
);
162 fprintf (stderr
, "\n");
165 /* In this case we return at here. */
168 tsctl
.precision
= zl
->timestamp_precision
;
171 if (priority
<= zl
->maxlvl
[ZLOG_DEST_SYSLOG
])
175 vsyslog (priority
|zlog_default
->facility
, format
, ac
);
180 if ((priority
<= zl
->maxlvl
[ZLOG_DEST_FILE
]) && zl
->fp
)
183 time_print (zl
->fp
, &tsctl
);
184 if (zl
->record_priority
)
185 fprintf (zl
->fp
, "%s: ", zlog_priority
[priority
]);
186 fprintf (zl
->fp
, "%s: ", zlog_proto_names
[zl
->protocol
]);
188 vfprintf (zl
->fp
, format
, ac
);
190 fprintf (zl
->fp
, "\n");
195 if (priority
<= zl
->maxlvl
[ZLOG_DEST_STDOUT
])
198 time_print (stdout
, &tsctl
);
199 if (zl
->record_priority
)
200 fprintf (stdout
, "%s: ", zlog_priority
[priority
]);
201 fprintf (stdout
, "%s: ", zlog_proto_names
[zl
->protocol
]);
203 vfprintf (stdout
, format
, ac
);
205 fprintf (stdout
, "\n");
209 /* Terminal monitor. */
210 if (priority
<= zl
->maxlvl
[ZLOG_DEST_MONITOR
])
211 vty_log ((zl
->record_priority
? zlog_priority
[priority
] : NULL
),
212 zlog_proto_names
[zl
->protocol
], format
, &tsctl
, args
);
216 str_append(char *dst
, int len
, const char *src
)
218 while ((len
-- > 0) && *src
)
224 num_append(char *s
, int len
, u_long x
)
230 return str_append(s
,len
,"0");
231 *(t
= &buf
[sizeof(buf
)-1]) = '\0';
232 while (x
&& (t
> buf
))
237 return str_append(s
,len
,t
);
240 #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE)
242 hex_append(char *s
, int len
, u_long x
)
248 return str_append(s
,len
,"0");
249 *(t
= &buf
[sizeof(buf
)-1]) = '\0';
250 while (x
&& (t
> buf
))
253 *--t
= ((cc
< 10) ? ('0'+cc
) : ('a'+cc
-10));
256 return str_append(s
,len
,t
);
260 /* Needs to be enhanced to support Solaris. */
269 struct sockaddr_un addr
;
271 if ((fd
= socket(AF_UNIX
,SOCK_DGRAM
,0)) < 0)
273 addr
.sun_family
= AF_UNIX
;
275 #define SYSLOG_SOCKET_PATH _PATH_LOG
277 #define SYSLOG_SOCKET_PATH "/dev/log"
279 s
= str_append(addr
.sun_path
,sizeof(addr
.sun_path
),SYSLOG_SOCKET_PATH
);
280 #undef SYSLOG_SOCKET_PATH
282 if (connect(fd
,(struct sockaddr
*)&addr
,sizeof(addr
)) < 0)
292 syslog_sigsafe(int priority
, const char *msg
, size_t msglen
)
294 static int syslog_fd
= -1;
295 char buf
[sizeof("<1234567890>ripngd[1234567890]: ")+msglen
+50];
298 if ((syslog_fd
< 0) && ((syslog_fd
= syslog_connect()) < 0))
301 #define LOC s,buf+sizeof(buf)-s
303 s
= str_append(LOC
,"<");
304 s
= num_append(LOC
,priority
);
305 s
= str_append(LOC
,">");
306 /* forget about the timestamp, too difficult in a signal handler */
307 s
= str_append(LOC
,zlog_default
->ident
);
308 if (zlog_default
->syslog_options
& LOG_PID
)
310 s
= str_append(LOC
,"[");
311 s
= num_append(LOC
,getpid());
312 s
= str_append(LOC
,"]");
314 s
= str_append(LOC
,": ");
315 s
= str_append(LOC
,msg
);
316 write(syslog_fd
,buf
,s
-buf
);
323 #define CRASHLOG_PREFIX "/var/tmp/quagga."
324 #define CRASHLOG_SUFFIX "crashlog"
325 if (zlog_default
&& zlog_default
->ident
)
327 /* Avoid strlen since it is not async-signal-safe. */
331 for (p
= zlog_default
->ident
, ilen
= 0; *p
; p
++)
334 char buf
[sizeof(CRASHLOG_PREFIX
)+ilen
+sizeof(CRASHLOG_SUFFIX
)+3];
336 #define LOC s,buf+sizeof(buf)-s
337 s
= str_append(LOC
, CRASHLOG_PREFIX
);
338 s
= str_append(LOC
, zlog_default
->ident
);
339 s
= str_append(LOC
, ".");
340 s
= str_append(LOC
, CRASHLOG_SUFFIX
);
343 return open(buf
, O_WRONLY
|O_CREAT
|O_EXCL
, LOGFILE_MASK
);
346 return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX
, O_WRONLY
|O_CREAT
|O_EXCL
,
348 #undef CRASHLOG_SUFFIX
349 #undef CRASHLOG_PREFIX
352 /* Note: the goal here is to use only async-signal-safe functions. */
354 zlog_signal(int signo
, const char *action
356 , siginfo_t
*siginfo
, void *program_counter
361 char buf
[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100];
363 char *msgstart
= buf
;
364 #define LOC s,buf+sizeof(buf)-s
369 s
= str_append(LOC
,zlog_proto_names
[zlog_default
->protocol
]);
374 s
= str_append(LOC
,"Received signal ");
375 s
= num_append(LOC
,signo
);
376 s
= str_append(LOC
," at ");
377 s
= num_append(LOC
,now
);
379 s
= str_append(LOC
," (si_addr 0x");
380 s
= hex_append(LOC
,(u_long
)(siginfo
->si_addr
));
383 s
= str_append(LOC
,", PC 0x");
384 s
= hex_append(LOC
,(u_long
)program_counter
);
386 s
= str_append(LOC
,"); ");
387 #else /* SA_SIGINFO */
388 s
= str_append(LOC
,"; ");
389 #endif /* SA_SIGINFO */
390 s
= str_append(LOC
,action
);
391 if (s
< buf
+sizeof(buf
))
394 /* N.B. implicit priority is most severe */
397 #define DUMP(FD) write(FD, buf, s-buf);
398 /* If no file logging configured, try to write to fallback log file. */
399 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
405 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
407 /* Remove trailing '\n' for monitor and syslog */
409 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
410 vty_log_fixed(buf
,s
-buf
);
411 if (PRI
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
412 syslog_sigsafe(PRI
|zlog_default
->facility
,msgstart
,s
-msgstart
);
416 zlog_backtrace_sigsafe(PRI
,
427 /* Log a backtrace using only async-signal-safe functions.
428 Needs to be enhanced to support syslog logging. */
430 zlog_backtrace_sigsafe(int priority
, void *program_counter
)
432 #ifdef HAVE_STACK_TRACE
433 static const char pclabel
[] = "Program counter: ";
438 #define LOC s,buf+sizeof(buf)-s
440 #ifdef HAVE_GLIBC_BACKTRACE
441 if (((size
= backtrace(array
,sizeof(array
)/sizeof(array
[0]))) <= 0) ||
442 ((size_t)size
> sizeof(array
)/sizeof(array
[0])))
446 if (program_counter) \
448 write(FD, pclabel, sizeof(pclabel)-1); \
449 backtrace_symbols_fd(&program_counter, 1, FD); \
451 write(FD, buf, s-buf); \
452 backtrace_symbols_fd(array, size, FD); \
454 #elif defined(HAVE_PRINTSTACK)
456 if (program_counter) \
457 write((FD), pclabel, sizeof(pclabel)-1); \
458 write((FD), buf, s-buf); \
461 #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */
464 s
= str_append(LOC
,"Backtrace for ");
465 s
= num_append(LOC
,size
);
466 s
= str_append(LOC
," stack frames:\n");
468 if ((logfile_fd
>= 0) || ((logfile_fd
= open_crashlog()) >= 0))
474 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_STDOUT
])
476 /* Remove trailing '\n' for monitor and syslog */
478 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
479 vty_log_fixed(buf
,s
-buf
);
480 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
481 syslog_sigsafe(priority
|zlog_default
->facility
,buf
,s
-buf
);
484 /* Just print the function addresses. */
485 for (i
= 0; i
< size
; i
++)
488 s
= str_append(LOC
,"[bt ");
489 s
= num_append(LOC
,i
);
490 s
= str_append(LOC
,"] 0x");
491 s
= hex_append(LOC
,(u_long
)(array
[i
]));
493 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_MONITOR
])
494 vty_log_fixed(buf
,s
-buf
);
495 if (priority
<= zlog_default
->maxlvl
[ZLOG_DEST_SYSLOG
])
496 syslog_sigsafe(priority
|zlog_default
->facility
,buf
,s
-buf
);
502 #endif /* HAVE_STRACK_TRACE */
506 zlog_backtrace(int priority
)
508 #ifndef HAVE_GLIBC_BACKTRACE
509 zlog(NULL
, priority
, "No backtrace available on this platform.");
515 if (((size
= backtrace(array
,sizeof(array
)/sizeof(array
[0]))) <= 0) ||
516 ((size_t)size
> sizeof(array
)/sizeof(array
[0])))
518 zlog_err("Cannot get backtrace, returned invalid # of frames %d "
519 "(valid range is between 1 and %lu)",
520 size
, (unsigned long)(sizeof(array
)/sizeof(array
[0])));
523 zlog(NULL
, priority
, "Backtrace for %d stack frames:", size
);
524 if (!(strings
= backtrace_symbols(array
, size
)))
526 zlog_err("Cannot get backtrace symbols (out of memory?)");
527 for (i
= 0; i
< size
; i
++)
528 zlog(NULL
, priority
, "[bt %d] %p",i
,array
[i
]);
532 for (i
= 0; i
< size
; i
++)
533 zlog(NULL
, priority
, "[bt %d] %s",i
,strings
[i
]);
536 #endif /* HAVE_GLIBC_BACKTRACE */
540 zlog (struct zlog
*zl
, int priority
, const char *format
, ...)
544 va_start(args
, format
);
545 vzlog (zl
, priority
, format
, args
);
549 #define ZLOG_FUNC(FUNCNAME,PRIORITY) \
551 FUNCNAME(const char *format, ...) \
554 va_start(args, format); \
555 vzlog (NULL, PRIORITY, format, args); \
559 ZLOG_FUNC(zlog_err
, LOG_ERR
)
561 ZLOG_FUNC(zlog_warn
, LOG_WARNING
)
563 ZLOG_FUNC(zlog_info
, LOG_INFO
)
565 ZLOG_FUNC(zlog_notice
, LOG_NOTICE
)
567 ZLOG_FUNC(zlog_debug
, LOG_DEBUG
)
571 #define PLOG_FUNC(FUNCNAME,PRIORITY) \
573 FUNCNAME(struct zlog *zl, const char *format, ...) \
576 va_start(args, format); \
577 vzlog (zl, PRIORITY, format, args); \
581 PLOG_FUNC(plog_err
, LOG_ERR
)
583 PLOG_FUNC(plog_warn
, LOG_WARNING
)
585 PLOG_FUNC(plog_info
, LOG_INFO
)
587 PLOG_FUNC(plog_notice
, LOG_NOTICE
)
589 PLOG_FUNC(plog_debug
, LOG_DEBUG
)
594 _zlog_assert_failed (const char *assertion
, const char *file
,
595 unsigned int line
, const char *function
)
597 /* Force fallback file logging? */
598 if (zlog_default
&& !zlog_default
->fp
&&
599 ((logfile_fd
= open_crashlog()) >= 0) &&
600 ((zlog_default
->fp
= fdopen(logfile_fd
, "w")) != NULL
))
601 zlog_default
->maxlvl
[ZLOG_DEST_FILE
] = LOG_ERR
;
602 zlog(NULL
, LOG_CRIT
, "Assertion `%s' failed in file %s, line %u, function %s",
603 assertion
,file
,line
,(function
? function
: "?"));
604 zlog_backtrace(LOG_CRIT
);
609 /* Open log stream */
611 openzlog (const char *progname
, zlog_proto_t protocol
,
612 int syslog_flags
, int syslog_facility
)
617 zl
= XCALLOC(MTYPE_ZLOG
, sizeof (struct zlog
));
619 zl
->ident
= progname
;
620 zl
->protocol
= protocol
;
621 zl
->facility
= syslog_facility
;
622 zl
->syslog_options
= syslog_flags
;
624 /* Set default logging levels. */
625 for (i
= 0; i
< sizeof(zl
->maxlvl
)/sizeof(zl
->maxlvl
[0]); i
++)
626 zl
->maxlvl
[i
] = ZLOG_DISABLED
;
627 zl
->maxlvl
[ZLOG_DEST_MONITOR
] = LOG_DEBUG
;
628 zl
->default_lvl
= LOG_DEBUG
;
630 openlog (progname
, syslog_flags
, zl
->facility
);
636 closezlog (struct zlog
*zl
)
641 XFREE (MTYPE_ZLOG
, zl
);
644 /* Called from command.c. */
646 zlog_set_level (struct zlog
*zl
, zlog_dest_t dest
, int log_level
)
651 zl
->maxlvl
[dest
] = log_level
;
655 zlog_set_file (struct zlog
*zl
, const char *filename
, int log_level
)
660 /* There is opend file. */
661 zlog_reset_file (zl
);
663 /* Set default zl. */
668 oldumask
= umask (0777 & ~LOGFILE_MASK
);
669 fp
= fopen (filename
, "a");
675 zl
->filename
= strdup (filename
);
676 zl
->maxlvl
[ZLOG_DEST_FILE
] = log_level
;
678 logfile_fd
= fileno(fp
);
683 /* Reset opend file. */
685 zlog_reset_file (struct zlog
*zl
)
694 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
703 /* Reopen log file. */
705 zlog_rotate (struct zlog
*zl
)
716 level
= zl
->maxlvl
[ZLOG_DEST_FILE
];
717 zl
->maxlvl
[ZLOG_DEST_FILE
] = ZLOG_DISABLED
;
724 oldumask
= umask (0777 & ~LOGFILE_MASK
);
725 zl
->fp
= fopen (zl
->filename
, "a");
730 zlog_err("Log rotate failed: cannot open file %s for append: %s",
731 zl
->filename
, safe_strerror(save_errno
));
734 logfile_fd
= fileno(zl
->fp
);
735 zl
->maxlvl
[ZLOG_DEST_FILE
] = level
;
741 /* Message lookup function. */
743 lookup (struct message
*mes
, int key
)
747 for (pnt
= mes
; pnt
->key
!= 0; pnt
++)
754 /* Older/faster version of message lookup function, but requires caller to pass
755 in the array size (instead of relying on a 0 key to terminate the search). */
757 mes_lookup (struct message
*meslist
, int max
, int index
)
759 /* first check for best case: index is in range and matches the key
760 value in that slot */
761 if ((index
>= 0) && (index
< max
) && (meslist
[index
].key
== index
))
762 return meslist
[index
].str
;
764 /* fall back to linear search */
768 for (i
= 0; i
< max
; i
++, meslist
++)
770 if (meslist
->key
== index
)
772 zlog_debug ("message index %d [%s] found in position %d (max is %d)",
773 index
, meslist
->str
, i
, max
);
778 zlog_err("message index %d not found (max is %d)", index
, max
);
782 /* Wrapper around strerror to handle case where it returns NULL. */
784 safe_strerror(int errnum
)
786 const char *s
= strerror(errnum
);
787 return (s
!= NULL
) ? s
: "Unknown error";
790 struct zebra_desc_table
797 #define DESC_ENTRY(T,S,C) [(T)] = { (T), (S), (C) }
798 static const struct zebra_desc_table route_types
[] = {
799 DESC_ENTRY (ZEBRA_ROUTE_SYSTEM
, "system", 'X' ),
800 DESC_ENTRY (ZEBRA_ROUTE_KERNEL
, "kernel", 'K' ),
801 DESC_ENTRY (ZEBRA_ROUTE_CONNECT
, "connected", 'C' ),
802 DESC_ENTRY (ZEBRA_ROUTE_STATIC
, "static", 'S' ),
803 DESC_ENTRY (ZEBRA_ROUTE_RIP
, "rip", 'R' ),
804 DESC_ENTRY (ZEBRA_ROUTE_RIPNG
, "ripng", 'R' ),
805 DESC_ENTRY (ZEBRA_ROUTE_OSPF
, "ospf", 'O' ),
806 DESC_ENTRY (ZEBRA_ROUTE_OSPF6
, "ospf6", 'O' ),
807 DESC_ENTRY (ZEBRA_ROUTE_ISIS
, "isis", 'I' ),
808 DESC_ENTRY (ZEBRA_ROUTE_BGP
, "bgp", 'B' ),
809 DESC_ENTRY (ZEBRA_ROUTE_HSLS
, "hsls", 'H' ),
813 #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
814 static const struct zebra_desc_table command_types
[] = {
815 DESC_ENTRY (ZEBRA_INTERFACE_ADD
),
816 DESC_ENTRY (ZEBRA_INTERFACE_DELETE
),
817 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD
),
818 DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE
),
819 DESC_ENTRY (ZEBRA_INTERFACE_UP
),
820 DESC_ENTRY (ZEBRA_INTERFACE_DOWN
),
821 DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD
),
822 DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE
),
823 DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD
),
824 DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE
),
825 DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD
),
826 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE
),
827 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD
),
828 DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE
),
829 DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP
),
830 DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP
),
831 DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP
),
832 DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP
),
833 DESC_ENTRY (ZEBRA_INTERFACE_RENAME
),
834 DESC_ENTRY (ZEBRA_ROUTER_ID_ADD
),
835 DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE
),
836 DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE
),
840 static const struct zebra_desc_table unknown
= { 0, "unknown", '?' };
842 static const struct zebra_desc_table
*
843 zroute_lookup(u_int zroute
)
847 if (zroute
>= sizeof(route_types
)/sizeof(route_types
[0]))
849 zlog_err("unknown zebra route type: %u", zroute
);
852 if (zroute
== route_types
[zroute
].type
)
853 return &route_types
[zroute
];
854 for (i
= 0; i
< sizeof(route_types
)/sizeof(route_types
[0]); i
++)
856 if (zroute
== route_types
[i
].type
)
858 zlog_warn("internal error: route type table out of order "
859 "while searching for %u, please notify developers", zroute
);
860 return &route_types
[i
];
863 zlog_err("internal error: cannot find route type %u in table!", zroute
);
868 zebra_route_string(u_int zroute
)
870 return zroute_lookup(zroute
)->string
;
874 zebra_route_char(u_int zroute
)
876 return zroute_lookup(zroute
)->chr
;
880 zserv_command_string (unsigned int command
)
882 if (command
>= sizeof(command_types
)/sizeof(command_types
[0]))
884 zlog_err ("unknown zserv command type: %u", command
);
885 return unknown
.string
;
887 return command_types
[command
].string
;
890 #define RTSIZE (sizeof(route_types)/sizeof(route_types[0]))
893 proto_name2num(const char *s
)
897 for (i
=0; i
<RTSIZE
; ++i
)
898 if (strcasecmp(s
, route_types
[i
].string
) == 0)
899 return route_types
[i
].type
;