ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1 | /* |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame^] | 2 | * $Id: log.c,v 1.21 2005/01/12 17:27:27 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 | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 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. */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 260 | void |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame^] | 261 | zlog_signal(int signo, const char *action, siginfo_t *siginfo, |
| 262 | void *program_counter) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 263 | { |
| 264 | time_t now; |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame^] | 265 | 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] | 266 | char *s = buf; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 267 | char *msgstart = buf; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 268 | #define LOC s,buf+sizeof(buf)-s |
| 269 | |
| 270 | time(&now); |
| 271 | if (zlog_default) |
| 272 | { |
| 273 | s = str_append(LOC,zlog_proto_names[zlog_default->protocol]); |
| 274 | *s++ = ':'; |
| 275 | *s++ = ' '; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 276 | msgstart = s; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 277 | } |
| 278 | s = str_append(LOC,"Received signal "); |
| 279 | s = num_append(LOC,signo); |
| 280 | s = str_append(LOC," at "); |
| 281 | s = num_append(LOC,now); |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame^] | 282 | s = str_append(LOC," (si_addr 0x"); |
| 283 | s = hex_append(LOC,(u_long)(siginfo->si_addr)); |
| 284 | if (program_counter) |
| 285 | { |
| 286 | s = str_append(LOC,", PC 0x"); |
| 287 | s = hex_append(LOC,(u_long)program_counter); |
| 288 | } |
| 289 | s = str_append(LOC,"); "); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 290 | s = str_append(LOC,action); |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 291 | if (s < buf+sizeof(buf)) |
| 292 | *s++ = '\n'; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 293 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 294 | /* N.B. implicit priority is most severe */ |
ajs | 3378d20 | 2004-12-10 22:43:17 +0000 | [diff] [blame] | 295 | #define PRI LOG_ERR |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 296 | |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 297 | #define DUMP(FP) write(fileno(FP),buf,s-buf); |
| 298 | if (!zlog_default) |
| 299 | DUMP(stderr) |
| 300 | else |
| 301 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 302 | if ((PRI <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && zlog_default->fp) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 303 | DUMP(zlog_default->fp) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 304 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 305 | DUMP(stdout) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 306 | /* Remove trailing '\n' for monitor and syslog */ |
| 307 | *--s = '\0'; |
| 308 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 309 | vty_log_fixed(buf,s-buf); |
| 310 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
| 311 | syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 312 | } |
| 313 | #undef DUMP |
| 314 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 315 | zlog_backtrace_sigsafe(PRI); |
| 316 | #undef PRI |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 317 | #undef LOC |
| 318 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 319 | |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 320 | /* Log a backtrace using only async-signal-safe functions. |
| 321 | Needs to be enhanced to support syslog logging. */ |
| 322 | void |
ajs | 48d6c69 | 2004-11-26 20:52:59 +0000 | [diff] [blame] | 323 | zlog_backtrace_sigsafe(int priority) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 324 | { |
| 325 | #ifdef HAVE_GLIBC_BACKTRACE |
| 326 | void *array[20]; |
| 327 | int size; |
| 328 | char buf[100]; |
| 329 | char *s; |
| 330 | #define LOC s,buf+sizeof(buf)-s |
| 331 | |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 332 | if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) || |
| 333 | ((size_t)size > sizeof(array)/sizeof(array[0]))) |
| 334 | return; |
| 335 | s = buf; |
| 336 | s = str_append(LOC,"Backtrace for "); |
| 337 | s = num_append(LOC,size); |
| 338 | s = str_append(LOC," stack frames:\n"); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 339 | |
| 340 | #define DUMP(FP) { \ |
| 341 | write(fileno(FP),buf,s-buf); \ |
| 342 | backtrace_symbols_fd(array, size, fileno(FP)); \ |
| 343 | } |
| 344 | |
| 345 | if (!zlog_default) |
| 346 | DUMP(stderr) |
| 347 | else |
| 348 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 349 | if ((priority <= zlog_default->maxlvl[ZLOG_DEST_FILE]) && |
| 350 | zlog_default->fp) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 351 | DUMP(zlog_default->fp) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 352 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 353 | DUMP(stdout) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 354 | /* Remove trailing '\n' for monitor and syslog */ |
| 355 | *--s = '\0'; |
| 356 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 357 | vty_log_fixed(buf,s-buf); |
| 358 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
| 359 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); |
| 360 | { |
| 361 | int i; |
| 362 | /* Just print the function addresses. */ |
| 363 | for (i = 0; i < size; i++) |
| 364 | { |
| 365 | s = buf; |
| 366 | s = str_append(LOC,"[bt "); |
| 367 | s = num_append(LOC,i); |
| 368 | s = str_append(LOC,"] 0x"); |
| 369 | s = hex_append(LOC,(u_long)(array[i])); |
| 370 | *s = '\0'; |
| 371 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 372 | vty_log_fixed(buf,s-buf); |
| 373 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 374 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 377 | } |
| 378 | #undef DUMP |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 379 | #undef LOC |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 380 | #endif /* HAVE_GLIBC_BACKTRACE */ |
| 381 | } |
| 382 | |
| 383 | void |
| 384 | zlog_backtrace(int priority) |
| 385 | { |
| 386 | #ifndef HAVE_GLIBC_BACKTRACE |
| 387 | zlog(NULL, priority, "No backtrace available on this platform."); |
| 388 | #else |
| 389 | void *array[20]; |
| 390 | int size, i; |
| 391 | char **strings; |
| 392 | |
| 393 | if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) || |
| 394 | ((size_t)size > sizeof(array)/sizeof(array[0]))) |
| 395 | { |
| 396 | zlog_err("Cannot get backtrace, returned invalid # of frames %d " |
| 397 | "(valid range is between 1 and %u)", |
| 398 | size, sizeof(array)/sizeof(array[0])); |
| 399 | return; |
| 400 | } |
| 401 | zlog(NULL, priority, "Backtrace for %d stack frames:", size); |
| 402 | if (!(strings = backtrace_symbols(array, size))) |
| 403 | { |
| 404 | zlog_err("Cannot get backtrace symbols (out of memory?)"); |
| 405 | for (i = 0; i < size; i++) |
| 406 | zlog(NULL, priority, "[bt %d] %p",i,array[i]); |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | for (i = 0; i < size; i++) |
| 411 | zlog(NULL, priority, "[bt %d] %s",i,strings[i]); |
| 412 | free(strings); |
| 413 | } |
| 414 | #endif /* HAVE_GLIBC_BACKTRACE */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 415 | } |
| 416 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 417 | void |
| 418 | zlog (struct zlog *zl, int priority, const char *format, ...) |
| 419 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 420 | va_list args; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 421 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 422 | va_start(args, format); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 423 | vzlog (zl, priority, format, args); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 424 | va_end (args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 425 | } |
| 426 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 427 | #define ZLOG_FUNC(FUNCNAME,PRIORITY) \ |
| 428 | void \ |
| 429 | FUNCNAME(const char *format, ...) \ |
| 430 | { \ |
| 431 | va_list args; \ |
| 432 | va_start(args, format); \ |
| 433 | vzlog (NULL, PRIORITY, format, args); \ |
| 434 | va_end(args); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 435 | } |
| 436 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 437 | ZLOG_FUNC(zlog_err, LOG_ERR) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 438 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 439 | ZLOG_FUNC(zlog_warn, LOG_WARNING) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 440 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 441 | ZLOG_FUNC(zlog_info, LOG_INFO) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 442 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 443 | ZLOG_FUNC(zlog_notice, LOG_NOTICE) |
| 444 | |
| 445 | ZLOG_FUNC(zlog_debug, LOG_DEBUG) |
| 446 | |
| 447 | #undef ZLOG_FUNC |
| 448 | |
| 449 | #define PLOG_FUNC(FUNCNAME,PRIORITY) \ |
| 450 | void \ |
| 451 | FUNCNAME(struct zlog *zl, const char *format, ...) \ |
| 452 | { \ |
| 453 | va_list args; \ |
| 454 | va_start(args, format); \ |
| 455 | vzlog (zl, PRIORITY, format, args); \ |
| 456 | va_end(args); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 457 | } |
| 458 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 459 | PLOG_FUNC(plog_err, LOG_ERR) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 460 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 461 | PLOG_FUNC(plog_warn, LOG_WARNING) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 462 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 463 | PLOG_FUNC(plog_info, LOG_INFO) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 464 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 465 | PLOG_FUNC(plog_notice, LOG_NOTICE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 466 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 467 | PLOG_FUNC(plog_debug, LOG_DEBUG) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 468 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 469 | #undef PLOG_FUNC |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 470 | |
ajs | cee3df1 | 2004-11-24 17:14:49 +0000 | [diff] [blame] | 471 | void |
| 472 | _zlog_assert_failed (const char *assertion, const char *file, |
| 473 | unsigned int line, const char *function) |
| 474 | { |
| 475 | zlog_err("Assertion `%s' failed in file %s, line %u, function %s", |
| 476 | assertion,file,line,(function ? function : "?")); |
ajs | 3378d20 | 2004-12-10 22:43:17 +0000 | [diff] [blame] | 477 | zlog_backtrace(LOG_ERR); |
ajs | cee3df1 | 2004-11-24 17:14:49 +0000 | [diff] [blame] | 478 | abort(); |
| 479 | } |
| 480 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 481 | |
| 482 | /* Open log stream */ |
| 483 | struct zlog * |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 484 | openzlog (const char *progname, zlog_proto_t protocol, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 485 | int syslog_flags, int syslog_facility) |
| 486 | { |
| 487 | struct zlog *zl; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 488 | u_int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 489 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 490 | zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 491 | |
| 492 | zl->ident = progname; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 493 | zl->protocol = protocol; |
| 494 | zl->facility = syslog_facility; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 495 | zl->syslog_options = syslog_flags; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 496 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 497 | /* Set default logging levels. */ |
| 498 | for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++) |
| 499 | zl->maxlvl[i] = ZLOG_DISABLED; |
| 500 | zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG; |
| 501 | zl->default_lvl = LOG_DEBUG; |
| 502 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 503 | openlog (progname, syslog_flags, zl->facility); |
| 504 | |
| 505 | return zl; |
| 506 | } |
| 507 | |
| 508 | void |
| 509 | closezlog (struct zlog *zl) |
| 510 | { |
| 511 | closelog(); |
| 512 | fclose (zl->fp); |
| 513 | |
| 514 | XFREE (MTYPE_ZLOG, zl); |
| 515 | } |
| 516 | |
| 517 | /* Called from command.c. */ |
| 518 | void |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 519 | zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 520 | { |
| 521 | if (zl == NULL) |
| 522 | zl = zlog_default; |
| 523 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 524 | zl->maxlvl[dest] = log_level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | int |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 528 | zlog_set_file (struct zlog *zl, const char *filename, int log_level) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 529 | { |
| 530 | FILE *fp; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 531 | mode_t oldumask; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 532 | |
| 533 | /* There is opend file. */ |
| 534 | zlog_reset_file (zl); |
| 535 | |
| 536 | /* Set default zl. */ |
| 537 | if (zl == NULL) |
| 538 | zl = zlog_default; |
| 539 | |
| 540 | /* Open file. */ |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 541 | oldumask = umask (0777 & ~LOGFILE_MASK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 542 | fp = fopen (filename, "a"); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 543 | umask(oldumask); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 544 | if (fp == NULL) |
| 545 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 546 | |
| 547 | /* Set flags. */ |
| 548 | zl->filename = strdup (filename); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 549 | zl->maxlvl[ZLOG_DEST_FILE] = log_level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 550 | zl->fp = fp; |
| 551 | |
| 552 | return 1; |
| 553 | } |
| 554 | |
| 555 | /* Reset opend file. */ |
| 556 | int |
| 557 | zlog_reset_file (struct zlog *zl) |
| 558 | { |
| 559 | if (zl == NULL) |
| 560 | zl = zlog_default; |
| 561 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 562 | if (zl->fp) |
| 563 | fclose (zl->fp); |
| 564 | zl->fp = NULL; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 565 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 566 | |
| 567 | if (zl->filename) |
| 568 | free (zl->filename); |
| 569 | zl->filename = NULL; |
| 570 | |
| 571 | return 1; |
| 572 | } |
| 573 | |
| 574 | /* Reopen log file. */ |
| 575 | int |
| 576 | zlog_rotate (struct zlog *zl) |
| 577 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 578 | int level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 579 | |
| 580 | if (zl == NULL) |
| 581 | zl = zlog_default; |
| 582 | |
| 583 | if (zl->fp) |
| 584 | fclose (zl->fp); |
| 585 | zl->fp = NULL; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 586 | level = zl->maxlvl[ZLOG_DEST_FILE]; |
| 587 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 588 | |
| 589 | if (zl->filename) |
| 590 | { |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 591 | mode_t oldumask; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 592 | int save_errno; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 593 | |
| 594 | oldumask = umask (0777 & ~LOGFILE_MASK); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 595 | zl->fp = fopen (zl->filename, "a"); |
| 596 | save_errno = errno; |
| 597 | umask(oldumask); |
| 598 | if (zl->fp == NULL) |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 599 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 600 | zlog_err("Log rotate failed: cannot open file %s for append: %s", |
| 601 | zl->filename, safe_strerror(save_errno)); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 602 | return -1; |
| 603 | } |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 604 | zl->maxlvl[ZLOG_DEST_FILE] = level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | return 1; |
| 608 | } |
| 609 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 610 | /* Message lookup function. */ |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 611 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 612 | lookup (struct message *mes, int key) |
| 613 | { |
| 614 | struct message *pnt; |
| 615 | |
| 616 | for (pnt = mes; pnt->key != 0; pnt++) |
| 617 | if (pnt->key == key) |
| 618 | return pnt->str; |
| 619 | |
| 620 | return ""; |
| 621 | } |
| 622 | |
| 623 | /* Very old hacky version of message lookup function. Still partly |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 624 | 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] | 625 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 626 | mes_lookup (struct message *meslist, int max, int index) |
| 627 | { |
| 628 | if (index < 0 || index >= max) |
| 629 | { |
| 630 | zlog_err ("message index out of bound: %d", max); |
| 631 | return NULL; |
| 632 | } |
| 633 | return meslist[index].str; |
| 634 | } |
ajs | ca35976 | 2004-11-19 23:40:16 +0000 | [diff] [blame] | 635 | |
| 636 | /* Wrapper around strerror to handle case where it returns NULL. */ |
| 637 | const char * |
| 638 | safe_strerror(int errnum) |
| 639 | { |
| 640 | const char *s = strerror(errnum); |
| 641 | return (s != NULL) ? s : "Unknown error"; |
| 642 | } |