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