ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1 | /* |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 2 | * $Id: log.c,v 1.23 2005/01/18 22:18:59 ajs Exp $ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 3 | * |
| 4 | * Logging of zebra |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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" |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 30 | #ifndef SUNOS_5 |
| 31 | #include <sys/un.h> |
| 32 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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", |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 46 | "ISIS", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
| 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 | |
| 89 | /* va_list version of zlog. */ |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 90 | static void |
| 91 | vzlog (struct zlog *zl, int priority, const char *format, va_list args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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"); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 102 | vfprintf (stderr, format, args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 103 | fprintf (stderr, "\n"); |
| 104 | fflush (stderr); |
| 105 | |
| 106 | /* In this case we return at here. */ |
| 107 | return; |
| 108 | } |
| 109 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 110 | /* Syslog output */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 111 | if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG]) |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 112 | { |
| 113 | va_list ac; |
| 114 | va_copy(ac, args); |
| 115 | vsyslog (priority|zlog_default->facility, format, ac); |
| 116 | va_end(ac); |
| 117 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 118 | |
| 119 | /* File output. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 120 | if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 121 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 122 | va_list ac; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 123 | time_print (zl->fp); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 124 | if (zl->record_priority) |
| 125 | fprintf (zl->fp, "%s: ", zlog_priority[priority]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 126 | fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 127 | va_copy(ac, args); |
| 128 | vfprintf (zl->fp, format, ac); |
| 129 | va_end(ac); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 130 | fprintf (zl->fp, "\n"); |
| 131 | fflush (zl->fp); |
| 132 | } |
| 133 | |
| 134 | /* stdout output. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 135 | if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT]) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 137 | va_list ac; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 138 | time_print (stdout); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 139 | if (zl->record_priority) |
| 140 | fprintf (stdout, "%s: ", zlog_priority[priority]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 141 | fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 142 | va_copy(ac, args); |
| 143 | vfprintf (stdout, format, ac); |
| 144 | va_end(ac); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 145 | fprintf (stdout, "\n"); |
| 146 | fflush (stdout); |
| 147 | } |
| 148 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | /* Terminal monitor. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 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); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 153 | } |
| 154 | |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 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]; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 167 | char *t; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 168 | |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 169 | if (!x) |
| 170 | return str_append(s,len,"0"); |
| 171 | *(t = &buf[sizeof(buf)-1]) = '\0'; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 172 | while (x && (t > buf)) |
| 173 | { |
| 174 | *--t = '0'+(x % 10); |
| 175 | x /= 10; |
| 176 | } |
| 177 | return str_append(s,len,t); |
| 178 | } |
| 179 | |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 180 | #if defined(SA_SIGINFO) || defined(HAVE_GLIBC_BACKTRACE) |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 181 | static char * |
| 182 | hex_append(char *s, int len, u_long x) |
| 183 | { |
| 184 | char buf[30]; |
| 185 | char *t; |
| 186 | |
| 187 | if (!x) |
| 188 | return str_append(s,len,"0"); |
| 189 | *(t = &buf[sizeof(buf)-1]) = '\0'; |
| 190 | while (x && (t > buf)) |
| 191 | { |
| 192 | u_int cc = (x % 16); |
| 193 | *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10)); |
| 194 | x /= 16; |
| 195 | } |
| 196 | return str_append(s,len,t); |
| 197 | } |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 198 | #endif |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 199 | |
| 200 | static int syslog_fd = -1; |
| 201 | |
| 202 | /* Needs to be enhanced to support Solaris. */ |
| 203 | static int |
| 204 | syslog_connect(void) |
| 205 | { |
| 206 | #ifdef SUNOS_5 |
| 207 | return -1; |
| 208 | #else |
| 209 | int fd; |
| 210 | char *s; |
| 211 | struct sockaddr_un addr; |
| 212 | |
| 213 | if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0) |
| 214 | return -1; |
| 215 | addr.sun_family = AF_UNIX; |
| 216 | #ifdef _PATH_LOG |
| 217 | #define SYSLOG_SOCKET_PATH _PATH_LOG |
| 218 | #else |
| 219 | #define SYSLOG_SOCKET_PATH "/dev/log" |
| 220 | #endif |
| 221 | s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH); |
| 222 | #undef SYSLOG_SOCKET_PATH |
| 223 | *s = '\0'; |
| 224 | if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0) |
| 225 | { |
| 226 | close(fd); |
| 227 | return -1; |
| 228 | } |
| 229 | return fd; |
| 230 | #endif |
| 231 | } |
| 232 | |
| 233 | static void |
| 234 | syslog_sigsafe(int priority, const char *msg, size_t msglen) |
| 235 | { |
| 236 | char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50]; |
| 237 | char *s; |
| 238 | |
| 239 | if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0)) |
| 240 | return; |
| 241 | |
| 242 | #define LOC s,buf+sizeof(buf)-s |
| 243 | s = buf; |
| 244 | s = str_append(LOC,"<"); |
| 245 | s = num_append(LOC,priority); |
| 246 | s = str_append(LOC,">"); |
| 247 | /* forget about the timestamp, too difficult in a signal handler */ |
| 248 | s = str_append(LOC,zlog_default->ident); |
| 249 | if (zlog_default->syslog_options & LOG_PID) |
| 250 | { |
| 251 | s = str_append(LOC,"["); |
| 252 | s = num_append(LOC,getpid()); |
| 253 | s = str_append(LOC,"]"); |
| 254 | } |
| 255 | s = str_append(LOC,": "); |
| 256 | s = str_append(LOC,msg); |
| 257 | write(syslog_fd,buf,s-buf); |
| 258 | #undef LOC |
| 259 | } |
| 260 | |
| 261 | /* Note: the goal here is to use only async-signal-safe functions. */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 262 | void |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 263 | zlog_signal(int signo, const char *action |
| 264 | #ifdef SA_SIGINFO |
| 265 | , siginfo_t *siginfo, void *program_counter |
| 266 | #endif |
| 267 | ) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 268 | { |
| 269 | time_t now; |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 270 | char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100]; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 271 | char *s = buf; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 272 | char *msgstart = buf; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 273 | #define LOC s,buf+sizeof(buf)-s |
| 274 | |
| 275 | time(&now); |
| 276 | if (zlog_default) |
| 277 | { |
| 278 | s = str_append(LOC,zlog_proto_names[zlog_default->protocol]); |
| 279 | *s++ = ':'; |
| 280 | *s++ = ' '; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 281 | msgstart = s; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 282 | } |
| 283 | s = str_append(LOC,"Received signal "); |
| 284 | s = num_append(LOC,signo); |
| 285 | s = str_append(LOC," at "); |
| 286 | s = num_append(LOC,now); |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 287 | #ifdef SA_SIGINFO |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 288 | s = str_append(LOC," (si_addr 0x"); |
| 289 | s = hex_append(LOC,(u_long)(siginfo->si_addr)); |
| 290 | if (program_counter) |
| 291 | { |
| 292 | s = str_append(LOC,", PC 0x"); |
| 293 | s = hex_append(LOC,(u_long)program_counter); |
| 294 | } |
| 295 | s = str_append(LOC,"); "); |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 296 | #else /* SA_SIGINFO */ |
| 297 | s = str_append(LOC,"; "); |
| 298 | #endif /* SA_SIGINFO */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 299 | s = str_append(LOC,action); |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 300 | if (s < buf+sizeof(buf)) |
| 301 | *s++ = '\n'; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 302 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 303 | /* N.B. implicit priority is most severe */ |
ajs | 3378d20 | 2004-12-10 22:43:17 +0000 | [diff] [blame] | 304 | #define PRI LOG_ERR |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 305 | |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 306 | #define DUMP(FP) write(fileno(FP),buf,s-buf); |
| 307 | if (!zlog_default) |
| 308 | DUMP(stderr) |
| 309 | else |
| 310 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 311 | if ((PRI <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && zlog_default->fp) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 312 | DUMP(zlog_default->fp) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 313 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 314 | DUMP(stdout) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 315 | /* Remove trailing '\n' for monitor and syslog */ |
| 316 | *--s = '\0'; |
| 317 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 318 | vty_log_fixed(buf,s-buf); |
| 319 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
| 320 | syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 321 | } |
| 322 | #undef DUMP |
| 323 | |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 324 | zlog_backtrace_sigsafe(PRI, |
| 325 | #ifdef SA_SIGINFO |
| 326 | program_counter |
| 327 | #else |
| 328 | NULL |
| 329 | #endif |
| 330 | ); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 331 | #undef PRI |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 332 | #undef LOC |
| 333 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 334 | |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 335 | /* Log a backtrace using only async-signal-safe functions. |
| 336 | Needs to be enhanced to support syslog logging. */ |
| 337 | void |
ajs | 239c26f | 2005-01-17 15:22:28 +0000 | [diff] [blame] | 338 | zlog_backtrace_sigsafe(int priority, void *program_counter) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 339 | { |
| 340 | #ifdef HAVE_GLIBC_BACKTRACE |
ajs | 239c26f | 2005-01-17 15:22:28 +0000 | [diff] [blame] | 341 | static const char pclabel[] = "Program counter: "; |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 342 | void *array[20]; |
| 343 | int size; |
| 344 | char buf[100]; |
| 345 | char *s; |
| 346 | #define LOC s,buf+sizeof(buf)-s |
| 347 | |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 348 | if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) || |
| 349 | ((size_t)size > sizeof(array)/sizeof(array[0]))) |
| 350 | return; |
| 351 | s = buf; |
| 352 | s = str_append(LOC,"Backtrace for "); |
| 353 | s = num_append(LOC,size); |
| 354 | s = str_append(LOC," stack frames:\n"); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 355 | |
| 356 | #define DUMP(FP) { \ |
ajs | 239c26f | 2005-01-17 15:22:28 +0000 | [diff] [blame] | 357 | if (program_counter) \ |
| 358 | { \ |
| 359 | write(fileno(FP),pclabel,sizeof(pclabel)-1); \ |
| 360 | backtrace_symbols_fd(&program_counter, 1, fileno(FP)); \ |
| 361 | } \ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 362 | write(fileno(FP),buf,s-buf); \ |
| 363 | backtrace_symbols_fd(array, size, fileno(FP)); \ |
| 364 | } |
| 365 | |
| 366 | if (!zlog_default) |
| 367 | DUMP(stderr) |
| 368 | else |
| 369 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 370 | if ((priority <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && |
| 371 | zlog_default->fp) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 372 | DUMP(zlog_default->fp) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 373 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 374 | DUMP(stdout) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 375 | /* Remove trailing '\n' for monitor and syslog */ |
| 376 | *--s = '\0'; |
| 377 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 378 | vty_log_fixed(buf,s-buf); |
| 379 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
| 380 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); |
| 381 | { |
| 382 | int i; |
| 383 | /* Just print the function addresses. */ |
| 384 | for (i = 0; i < size; i++) |
| 385 | { |
| 386 | s = buf; |
| 387 | s = str_append(LOC,"[bt "); |
| 388 | s = num_append(LOC,i); |
| 389 | s = str_append(LOC,"] 0x"); |
| 390 | s = hex_append(LOC,(u_long)(array[i])); |
| 391 | *s = '\0'; |
| 392 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 393 | vty_log_fixed(buf,s-buf); |
| 394 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 395 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 398 | } |
| 399 | #undef DUMP |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 400 | #undef LOC |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 401 | #endif /* HAVE_GLIBC_BACKTRACE */ |
| 402 | } |
| 403 | |
| 404 | void |
| 405 | zlog_backtrace(int priority) |
| 406 | { |
| 407 | #ifndef HAVE_GLIBC_BACKTRACE |
| 408 | zlog(NULL, priority, "No backtrace available on this platform."); |
| 409 | #else |
| 410 | void *array[20]; |
| 411 | int size, i; |
| 412 | char **strings; |
| 413 | |
| 414 | if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) || |
| 415 | ((size_t)size > sizeof(array)/sizeof(array[0]))) |
| 416 | { |
| 417 | zlog_err("Cannot get backtrace, returned invalid # of frames %d " |
| 418 | "(valid range is between 1 and %u)", |
| 419 | size, sizeof(array)/sizeof(array[0])); |
| 420 | return; |
| 421 | } |
| 422 | zlog(NULL, priority, "Backtrace for %d stack frames:", size); |
| 423 | if (!(strings = backtrace_symbols(array, size))) |
| 424 | { |
| 425 | zlog_err("Cannot get backtrace symbols (out of memory?)"); |
| 426 | for (i = 0; i < size; i++) |
| 427 | zlog(NULL, priority, "[bt %d] %p",i,array[i]); |
| 428 | } |
| 429 | else |
| 430 | { |
| 431 | for (i = 0; i < size; i++) |
| 432 | zlog(NULL, priority, "[bt %d] %s",i,strings[i]); |
| 433 | free(strings); |
| 434 | } |
| 435 | #endif /* HAVE_GLIBC_BACKTRACE */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 436 | } |
| 437 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 438 | void |
| 439 | zlog (struct zlog *zl, int priority, const char *format, ...) |
| 440 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 441 | va_list args; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 442 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 443 | va_start(args, format); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 444 | vzlog (zl, priority, format, args); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 445 | va_end (args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 446 | } |
| 447 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 448 | #define ZLOG_FUNC(FUNCNAME,PRIORITY) \ |
| 449 | void \ |
| 450 | FUNCNAME(const char *format, ...) \ |
| 451 | { \ |
| 452 | va_list args; \ |
| 453 | va_start(args, format); \ |
| 454 | vzlog (NULL, PRIORITY, format, args); \ |
| 455 | va_end(args); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 456 | } |
| 457 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 458 | ZLOG_FUNC(zlog_err, LOG_ERR) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 459 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 460 | ZLOG_FUNC(zlog_warn, LOG_WARNING) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 461 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 462 | ZLOG_FUNC(zlog_info, LOG_INFO) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 463 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 464 | ZLOG_FUNC(zlog_notice, LOG_NOTICE) |
| 465 | |
| 466 | ZLOG_FUNC(zlog_debug, LOG_DEBUG) |
| 467 | |
| 468 | #undef ZLOG_FUNC |
| 469 | |
| 470 | #define PLOG_FUNC(FUNCNAME,PRIORITY) \ |
| 471 | void \ |
| 472 | FUNCNAME(struct zlog *zl, const char *format, ...) \ |
| 473 | { \ |
| 474 | va_list args; \ |
| 475 | va_start(args, format); \ |
| 476 | vzlog (zl, PRIORITY, format, args); \ |
| 477 | va_end(args); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 478 | } |
| 479 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 480 | PLOG_FUNC(plog_err, LOG_ERR) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 481 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 482 | PLOG_FUNC(plog_warn, LOG_WARNING) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 483 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 484 | PLOG_FUNC(plog_info, LOG_INFO) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 485 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 486 | PLOG_FUNC(plog_notice, LOG_NOTICE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 487 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 488 | PLOG_FUNC(plog_debug, LOG_DEBUG) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 489 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 490 | #undef PLOG_FUNC |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 491 | |
ajs | cee3df1 | 2004-11-24 17:14:49 +0000 | [diff] [blame] | 492 | void |
| 493 | _zlog_assert_failed (const char *assertion, const char *file, |
| 494 | unsigned int line, const char *function) |
| 495 | { |
| 496 | zlog_err("Assertion `%s' failed in file %s, line %u, function %s", |
| 497 | assertion,file,line,(function ? function : "?")); |
ajs | 3378d20 | 2004-12-10 22:43:17 +0000 | [diff] [blame] | 498 | zlog_backtrace(LOG_ERR); |
ajs | cee3df1 | 2004-11-24 17:14:49 +0000 | [diff] [blame] | 499 | abort(); |
| 500 | } |
| 501 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 502 | |
| 503 | /* Open log stream */ |
| 504 | struct zlog * |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 505 | openzlog (const char *progname, zlog_proto_t protocol, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 506 | int syslog_flags, int syslog_facility) |
| 507 | { |
| 508 | struct zlog *zl; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 509 | u_int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 510 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 511 | zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 512 | |
| 513 | zl->ident = progname; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 514 | zl->protocol = protocol; |
| 515 | zl->facility = syslog_facility; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 516 | zl->syslog_options = syslog_flags; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 517 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 518 | /* Set default logging levels. */ |
| 519 | for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++) |
| 520 | zl->maxlvl[i] = ZLOG_DISABLED; |
| 521 | zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG; |
| 522 | zl->default_lvl = LOG_DEBUG; |
| 523 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 524 | openlog (progname, syslog_flags, zl->facility); |
| 525 | |
| 526 | return zl; |
| 527 | } |
| 528 | |
| 529 | void |
| 530 | closezlog (struct zlog *zl) |
| 531 | { |
| 532 | closelog(); |
| 533 | fclose (zl->fp); |
| 534 | |
| 535 | XFREE (MTYPE_ZLOG, zl); |
| 536 | } |
| 537 | |
| 538 | /* Called from command.c. */ |
| 539 | void |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 540 | zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 541 | { |
| 542 | if (zl == NULL) |
| 543 | zl = zlog_default; |
| 544 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 545 | zl->maxlvl[dest] = log_level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | int |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 549 | zlog_set_file (struct zlog *zl, const char *filename, int log_level) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 550 | { |
| 551 | FILE *fp; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 552 | mode_t oldumask; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 553 | |
| 554 | /* There is opend file. */ |
| 555 | zlog_reset_file (zl); |
| 556 | |
| 557 | /* Set default zl. */ |
| 558 | if (zl == NULL) |
| 559 | zl = zlog_default; |
| 560 | |
| 561 | /* Open file. */ |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 562 | oldumask = umask (0777 & ~LOGFILE_MASK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 563 | fp = fopen (filename, "a"); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 564 | umask(oldumask); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 565 | if (fp == NULL) |
| 566 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 567 | |
| 568 | /* Set flags. */ |
| 569 | zl->filename = strdup (filename); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 570 | zl->maxlvl[ZLOG_DEST_FILE] = log_level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 571 | zl->fp = fp; |
| 572 | |
| 573 | return 1; |
| 574 | } |
| 575 | |
| 576 | /* Reset opend file. */ |
| 577 | int |
| 578 | zlog_reset_file (struct zlog *zl) |
| 579 | { |
| 580 | if (zl == NULL) |
| 581 | zl = zlog_default; |
| 582 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 583 | if (zl->fp) |
| 584 | fclose (zl->fp); |
| 585 | zl->fp = NULL; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 586 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 587 | |
| 588 | if (zl->filename) |
| 589 | free (zl->filename); |
| 590 | zl->filename = NULL; |
| 591 | |
| 592 | return 1; |
| 593 | } |
| 594 | |
| 595 | /* Reopen log file. */ |
| 596 | int |
| 597 | zlog_rotate (struct zlog *zl) |
| 598 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 599 | int level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 600 | |
| 601 | if (zl == NULL) |
| 602 | zl = zlog_default; |
| 603 | |
| 604 | if (zl->fp) |
| 605 | fclose (zl->fp); |
| 606 | zl->fp = NULL; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 607 | level = zl->maxlvl[ZLOG_DEST_FILE]; |
| 608 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 609 | |
| 610 | if (zl->filename) |
| 611 | { |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 612 | mode_t oldumask; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 613 | int save_errno; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 614 | |
| 615 | oldumask = umask (0777 & ~LOGFILE_MASK); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 616 | zl->fp = fopen (zl->filename, "a"); |
| 617 | save_errno = errno; |
| 618 | umask(oldumask); |
| 619 | if (zl->fp == NULL) |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 620 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 621 | zlog_err("Log rotate failed: cannot open file %s for append: %s", |
| 622 | zl->filename, safe_strerror(save_errno)); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 623 | return -1; |
| 624 | } |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 625 | zl->maxlvl[ZLOG_DEST_FILE] = level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | return 1; |
| 629 | } |
| 630 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 631 | /* Message lookup function. */ |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 632 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 633 | lookup (struct message *mes, int key) |
| 634 | { |
| 635 | struct message *pnt; |
| 636 | |
| 637 | for (pnt = mes; pnt->key != 0; pnt++) |
| 638 | if (pnt->key == key) |
| 639 | return pnt->str; |
| 640 | |
| 641 | return ""; |
| 642 | } |
| 643 | |
| 644 | /* Very old hacky version of message lookup function. Still partly |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 645 | used in bgpd and ospfd. FIXME Seems that it's not used any more. */ |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 646 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 647 | mes_lookup (struct message *meslist, int max, int index) |
| 648 | { |
| 649 | if (index < 0 || index >= max) |
| 650 | { |
| 651 | zlog_err ("message index out of bound: %d", max); |
| 652 | return NULL; |
| 653 | } |
| 654 | return meslist[index].str; |
| 655 | } |
ajs | ca35976 | 2004-11-19 23:40:16 +0000 | [diff] [blame] | 656 | |
| 657 | /* Wrapper around strerror to handle case where it returns NULL. */ |
| 658 | const char * |
| 659 | safe_strerror(int errnum) |
| 660 | { |
| 661 | const char *s = strerror(errnum); |
| 662 | return (s != NULL) ? s : "Unknown error"; |
| 663 | } |