ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 1 | /* |
Paul Jakma | fb66b29 | 2006-05-28 08:26:15 +0000 | [diff] [blame] | 2 | * $Id$ |
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 Jakma | 30a2231 | 2008-08-15 14:05:22 +0100 | [diff] [blame] | 33 | /* for printstack on solaris */ |
| 34 | #ifdef HAVE_UCONTEXT_H |
| 35 | #include <ucontext.h> |
| 36 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 38 | static int logfile_fd = -1; /* Used in signal handler. */ |
ajs | 1e22135 | 2005-02-03 16:42:40 +0000 | [diff] [blame] | 39 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | struct zlog *zlog_default = NULL; |
| 41 | |
| 42 | const char *zlog_proto_names[] = |
| 43 | { |
| 44 | "NONE", |
| 45 | "DEFAULT", |
| 46 | "ZEBRA", |
| 47 | "RIP", |
| 48 | "BGP", |
| 49 | "OSPF", |
| 50 | "RIPNG", |
| 51 | "OSPF6", |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 52 | "ISIS", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 53 | "MASC", |
| 54 | NULL, |
| 55 | }; |
| 56 | |
| 57 | const char *zlog_priority[] = |
| 58 | { |
| 59 | "emergencies", |
| 60 | "alerts", |
| 61 | "critical", |
| 62 | "errors", |
| 63 | "warnings", |
| 64 | "notifications", |
| 65 | "informational", |
| 66 | "debugging", |
| 67 | NULL, |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | |
| 72 | /* For time string format. */ |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 73 | |
| 74 | size_t |
| 75 | quagga_timestamp(int timestamp_precision, char *buf, size_t buflen) |
| 76 | { |
| 77 | static struct { |
| 78 | time_t last; |
| 79 | size_t len; |
| 80 | char buf[28]; |
| 81 | } cache; |
| 82 | struct timeval clock; |
| 83 | |
| 84 | /* would it be sufficient to use global 'recent_time' here? I fear not... */ |
| 85 | gettimeofday(&clock, NULL); |
| 86 | |
| 87 | /* first, we update the cache if the time has changed */ |
| 88 | if (cache.last != clock.tv_sec) |
| 89 | { |
| 90 | struct tm *tm; |
| 91 | cache.last = clock.tv_sec; |
| 92 | tm = localtime(&cache.last); |
| 93 | cache.len = strftime(cache.buf, sizeof(cache.buf), |
| 94 | "%Y/%m/%d %H:%M:%S", tm); |
| 95 | } |
| 96 | /* note: it's not worth caching the subsecond part, because |
| 97 | chances are that back-to-back calls are not sufficiently close together |
| 98 | for the clock not to have ticked forward */ |
| 99 | |
| 100 | if (buflen > cache.len) |
| 101 | { |
| 102 | memcpy(buf, cache.buf, cache.len); |
| 103 | if ((timestamp_precision > 0) && |
| 104 | (buflen > cache.len+1+timestamp_precision)) |
| 105 | { |
| 106 | /* should we worry about locale issues? */ |
Andrew J. Schorr | bcdda30 | 2007-04-29 15:48:22 +0000 | [diff] [blame] | 107 | static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1}; |
| 108 | int prec; |
| 109 | char *p = buf+cache.len+1+(prec = timestamp_precision); |
| 110 | *p-- = '\0'; |
| 111 | while (prec > 6) |
| 112 | /* this is unlikely to happen, but protect anyway */ |
| 113 | { |
| 114 | *p-- = '0'; |
| 115 | prec--; |
| 116 | } |
| 117 | clock.tv_usec /= divisor[prec]; |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 118 | do |
| 119 | { |
Andrew J. Schorr | bcdda30 | 2007-04-29 15:48:22 +0000 | [diff] [blame] | 120 | *p-- = '0'+(clock.tv_usec % 10); |
| 121 | clock.tv_usec /= 10; |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 122 | } |
Andrew J. Schorr | bcdda30 | 2007-04-29 15:48:22 +0000 | [diff] [blame] | 123 | while (--prec > 0); |
| 124 | *p = '.'; |
| 125 | return cache.len+1+timestamp_precision; |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 126 | } |
| 127 | buf[cache.len] = '\0'; |
| 128 | return cache.len; |
| 129 | } |
| 130 | if (buflen > 0) |
| 131 | buf[0] = '\0'; |
| 132 | return 0; |
| 133 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 134 | |
| 135 | /* Utility routine for current time printing. */ |
| 136 | static void |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 137 | time_print(FILE *fp, struct timestamp_control *ctl) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 138 | { |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 139 | if (!ctl->already_rendered) |
| 140 | { |
| 141 | ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf)); |
| 142 | ctl->already_rendered = 1; |
| 143 | } |
| 144 | fprintf(fp, "%s ", ctl->buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 145 | } |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 146 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 147 | |
| 148 | /* va_list version of zlog. */ |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 149 | static void |
| 150 | vzlog (struct zlog *zl, int priority, const char *format, va_list args) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 151 | { |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 152 | struct timestamp_control tsctl; |
| 153 | tsctl.already_rendered = 0; |
| 154 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 155 | /* If zlog is not specified, use default one. */ |
| 156 | if (zl == NULL) |
| 157 | zl = zlog_default; |
| 158 | |
| 159 | /* When zlog_default is also NULL, use stderr for logging. */ |
| 160 | if (zl == NULL) |
| 161 | { |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 162 | tsctl.precision = 0; |
| 163 | time_print(stderr, &tsctl); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 164 | fprintf (stderr, "%s: ", "unknown"); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 165 | vfprintf (stderr, format, args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 166 | fprintf (stderr, "\n"); |
| 167 | fflush (stderr); |
| 168 | |
| 169 | /* In this case we return at here. */ |
| 170 | return; |
| 171 | } |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 172 | tsctl.precision = zl->timestamp_precision; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 174 | /* Syslog output */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 175 | if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG]) |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 176 | { |
| 177 | va_list ac; |
| 178 | va_copy(ac, args); |
| 179 | vsyslog (priority|zlog_default->facility, format, ac); |
| 180 | va_end(ac); |
| 181 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 182 | |
| 183 | /* File output. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 184 | if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 185 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 186 | va_list ac; |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 187 | time_print (zl->fp, &tsctl); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 188 | if (zl->record_priority) |
| 189 | fprintf (zl->fp, "%s: ", zlog_priority[priority]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 190 | fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 191 | va_copy(ac, args); |
| 192 | vfprintf (zl->fp, format, ac); |
| 193 | va_end(ac); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 194 | fprintf (zl->fp, "\n"); |
| 195 | fflush (zl->fp); |
| 196 | } |
| 197 | |
| 198 | /* stdout output. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 199 | if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT]) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 200 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 201 | va_list ac; |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 202 | time_print (stdout, &tsctl); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 203 | if (zl->record_priority) |
| 204 | fprintf (stdout, "%s: ", zlog_priority[priority]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 206 | va_copy(ac, args); |
| 207 | vfprintf (stdout, format, ac); |
| 208 | va_end(ac); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 209 | fprintf (stdout, "\n"); |
| 210 | fflush (stdout); |
| 211 | } |
| 212 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | /* Terminal monitor. */ |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 214 | if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR]) |
| 215 | vty_log ((zl->record_priority ? zlog_priority[priority] : NULL), |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 216 | zlog_proto_names[zl->protocol], format, &tsctl, args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | } |
| 218 | |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 219 | static char * |
| 220 | str_append(char *dst, int len, const char *src) |
| 221 | { |
| 222 | while ((len-- > 0) && *src) |
| 223 | *dst++ = *src++; |
| 224 | return dst; |
| 225 | } |
| 226 | |
| 227 | static char * |
| 228 | num_append(char *s, int len, u_long x) |
| 229 | { |
| 230 | char buf[30]; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 231 | char *t; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 232 | |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 233 | if (!x) |
| 234 | return str_append(s,len,"0"); |
| 235 | *(t = &buf[sizeof(buf)-1]) = '\0'; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 236 | while (x && (t > buf)) |
| 237 | { |
| 238 | *--t = '0'+(x % 10); |
| 239 | x /= 10; |
| 240 | } |
| 241 | return str_append(s,len,t); |
| 242 | } |
| 243 | |
Paul Jakma | fb66b29 | 2006-05-28 08:26:15 +0000 | [diff] [blame] | 244 | #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE) |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 245 | static char * |
| 246 | hex_append(char *s, int len, u_long x) |
| 247 | { |
| 248 | char buf[30]; |
| 249 | char *t; |
| 250 | |
| 251 | if (!x) |
| 252 | return str_append(s,len,"0"); |
| 253 | *(t = &buf[sizeof(buf)-1]) = '\0'; |
| 254 | while (x && (t > buf)) |
| 255 | { |
| 256 | u_int cc = (x % 16); |
| 257 | *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10)); |
| 258 | x /= 16; |
| 259 | } |
| 260 | return str_append(s,len,t); |
| 261 | } |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 262 | #endif |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 263 | |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 264 | /* Needs to be enhanced to support Solaris. */ |
| 265 | static int |
| 266 | syslog_connect(void) |
| 267 | { |
| 268 | #ifdef SUNOS_5 |
| 269 | return -1; |
| 270 | #else |
| 271 | int fd; |
| 272 | char *s; |
| 273 | struct sockaddr_un addr; |
| 274 | |
| 275 | if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0) |
| 276 | return -1; |
| 277 | addr.sun_family = AF_UNIX; |
| 278 | #ifdef _PATH_LOG |
| 279 | #define SYSLOG_SOCKET_PATH _PATH_LOG |
| 280 | #else |
| 281 | #define SYSLOG_SOCKET_PATH "/dev/log" |
| 282 | #endif |
| 283 | s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH); |
| 284 | #undef SYSLOG_SOCKET_PATH |
| 285 | *s = '\0'; |
| 286 | if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0) |
| 287 | { |
| 288 | close(fd); |
| 289 | return -1; |
| 290 | } |
| 291 | return fd; |
| 292 | #endif |
| 293 | } |
| 294 | |
| 295 | static void |
| 296 | syslog_sigsafe(int priority, const char *msg, size_t msglen) |
| 297 | { |
ajs | 1e22135 | 2005-02-03 16:42:40 +0000 | [diff] [blame] | 298 | static int syslog_fd = -1; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 299 | char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50]; |
| 300 | char *s; |
| 301 | |
| 302 | if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0)) |
| 303 | return; |
| 304 | |
| 305 | #define LOC s,buf+sizeof(buf)-s |
| 306 | s = buf; |
| 307 | s = str_append(LOC,"<"); |
| 308 | s = num_append(LOC,priority); |
| 309 | s = str_append(LOC,">"); |
| 310 | /* forget about the timestamp, too difficult in a signal handler */ |
| 311 | s = str_append(LOC,zlog_default->ident); |
| 312 | if (zlog_default->syslog_options & LOG_PID) |
| 313 | { |
| 314 | s = str_append(LOC,"["); |
| 315 | s = num_append(LOC,getpid()); |
| 316 | s = str_append(LOC,"]"); |
| 317 | } |
| 318 | s = str_append(LOC,": "); |
| 319 | s = str_append(LOC,msg); |
| 320 | write(syslog_fd,buf,s-buf); |
| 321 | #undef LOC |
| 322 | } |
| 323 | |
ajs | 1e22135 | 2005-02-03 16:42:40 +0000 | [diff] [blame] | 324 | static int |
| 325 | open_crashlog(void) |
| 326 | { |
| 327 | #define CRASHLOG_PREFIX "/var/tmp/quagga." |
| 328 | #define CRASHLOG_SUFFIX "crashlog" |
| 329 | if (zlog_default && zlog_default->ident) |
| 330 | { |
| 331 | /* Avoid strlen since it is not async-signal-safe. */ |
| 332 | const char *p; |
| 333 | size_t ilen; |
| 334 | |
| 335 | for (p = zlog_default->ident, ilen = 0; *p; p++) |
| 336 | ilen++; |
| 337 | { |
| 338 | char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3]; |
| 339 | char *s = buf; |
| 340 | #define LOC s,buf+sizeof(buf)-s |
| 341 | s = str_append(LOC, CRASHLOG_PREFIX); |
| 342 | s = str_append(LOC, zlog_default->ident); |
| 343 | s = str_append(LOC, "."); |
| 344 | s = str_append(LOC, CRASHLOG_SUFFIX); |
| 345 | #undef LOC |
| 346 | *s = '\0'; |
| 347 | return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK); |
| 348 | } |
| 349 | } |
| 350 | return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL, |
| 351 | LOGFILE_MASK); |
| 352 | #undef CRASHLOG_SUFFIX |
| 353 | #undef CRASHLOG_PREFIX |
| 354 | } |
| 355 | |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 356 | /* Note: the goal here is to use only async-signal-safe functions. */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 357 | void |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 358 | zlog_signal(int signo, const char *action |
| 359 | #ifdef SA_SIGINFO |
| 360 | , siginfo_t *siginfo, void *program_counter |
| 361 | #endif |
| 362 | ) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 363 | { |
| 364 | time_t now; |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 365 | 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] | 366 | char *s = buf; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 367 | char *msgstart = buf; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 368 | #define LOC s,buf+sizeof(buf)-s |
| 369 | |
| 370 | time(&now); |
| 371 | if (zlog_default) |
| 372 | { |
| 373 | s = str_append(LOC,zlog_proto_names[zlog_default->protocol]); |
| 374 | *s++ = ':'; |
| 375 | *s++ = ' '; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 376 | msgstart = s; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 377 | } |
| 378 | s = str_append(LOC,"Received signal "); |
| 379 | s = num_append(LOC,signo); |
| 380 | s = str_append(LOC," at "); |
| 381 | s = num_append(LOC,now); |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 382 | #ifdef SA_SIGINFO |
ajs | 40abf23 | 2005-01-12 17:27:27 +0000 | [diff] [blame] | 383 | s = str_append(LOC," (si_addr 0x"); |
| 384 | s = hex_append(LOC,(u_long)(siginfo->si_addr)); |
| 385 | if (program_counter) |
| 386 | { |
| 387 | s = str_append(LOC,", PC 0x"); |
| 388 | s = hex_append(LOC,(u_long)program_counter); |
| 389 | } |
| 390 | s = str_append(LOC,"); "); |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 391 | #else /* SA_SIGINFO */ |
| 392 | s = str_append(LOC,"; "); |
| 393 | #endif /* SA_SIGINFO */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 394 | s = str_append(LOC,action); |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 395 | if (s < buf+sizeof(buf)) |
| 396 | *s++ = '\n'; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 397 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 398 | /* N.B. implicit priority is most severe */ |
ajs | 1e22135 | 2005-02-03 16:42:40 +0000 | [diff] [blame] | 399 | #define PRI LOG_CRIT |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 400 | |
ajs | 1e22135 | 2005-02-03 16:42:40 +0000 | [diff] [blame] | 401 | #define DUMP(FD) write(FD, buf, s-buf); |
| 402 | /* If no file logging configured, try to write to fallback log file. */ |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 403 | if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0)) |
| 404 | DUMP(logfile_fd) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 405 | if (!zlog_default) |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 406 | DUMP(STDERR_FILENO) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 407 | else |
| 408 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 409 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 410 | DUMP(STDOUT_FILENO) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 411 | /* Remove trailing '\n' for monitor and syslog */ |
| 412 | *--s = '\0'; |
| 413 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 414 | vty_log_fixed(buf,s-buf); |
| 415 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
| 416 | syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 417 | } |
| 418 | #undef DUMP |
| 419 | |
ajs | 3136427 | 2005-01-18 22:18:59 +0000 | [diff] [blame] | 420 | zlog_backtrace_sigsafe(PRI, |
| 421 | #ifdef SA_SIGINFO |
| 422 | program_counter |
| 423 | #else |
| 424 | NULL |
| 425 | #endif |
| 426 | ); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 427 | #undef PRI |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 428 | #undef LOC |
| 429 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 430 | |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 431 | /* Log a backtrace using only async-signal-safe functions. |
| 432 | Needs to be enhanced to support syslog logging. */ |
| 433 | void |
ajs | 239c26f | 2005-01-17 15:22:28 +0000 | [diff] [blame] | 434 | zlog_backtrace_sigsafe(int priority, void *program_counter) |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 435 | { |
Paul Jakma | fb66b29 | 2006-05-28 08:26:15 +0000 | [diff] [blame] | 436 | #ifdef HAVE_STACK_TRACE |
ajs | 239c26f | 2005-01-17 15:22:28 +0000 | [diff] [blame] | 437 | static const char pclabel[] = "Program counter: "; |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 438 | void *array[20]; |
| 439 | int size; |
| 440 | char buf[100]; |
| 441 | char *s; |
| 442 | #define LOC s,buf+sizeof(buf)-s |
| 443 | |
Paul Jakma | fb66b29 | 2006-05-28 08:26:15 +0000 | [diff] [blame] | 444 | #ifdef HAVE_GLIBC_BACKTRACE |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 445 | if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) || |
| 446 | ((size_t)size > sizeof(array)/sizeof(array[0]))) |
| 447 | return; |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 448 | |
ajs | 1e22135 | 2005-02-03 16:42:40 +0000 | [diff] [blame] | 449 | #define DUMP(FD) { \ |
ajs | 239c26f | 2005-01-17 15:22:28 +0000 | [diff] [blame] | 450 | if (program_counter) \ |
| 451 | { \ |
ajs | 1e22135 | 2005-02-03 16:42:40 +0000 | [diff] [blame] | 452 | write(FD, pclabel, sizeof(pclabel)-1); \ |
| 453 | backtrace_symbols_fd(&program_counter, 1, FD); \ |
ajs | 239c26f | 2005-01-17 15:22:28 +0000 | [diff] [blame] | 454 | } \ |
ajs | 1e22135 | 2005-02-03 16:42:40 +0000 | [diff] [blame] | 455 | write(FD, buf, s-buf); \ |
| 456 | backtrace_symbols_fd(array, size, FD); \ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 457 | } |
Paul Jakma | fb66b29 | 2006-05-28 08:26:15 +0000 | [diff] [blame] | 458 | #elif defined(HAVE_PRINTSTACK) |
| 459 | #define DUMP(FD) { \ |
| 460 | if (program_counter) \ |
| 461 | write((FD), pclabel, sizeof(pclabel)-1); \ |
| 462 | write((FD), buf, s-buf); \ |
| 463 | printstack((FD)); \ |
| 464 | } |
| 465 | #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */ |
| 466 | |
| 467 | s = buf; |
| 468 | s = str_append(LOC,"Backtrace for "); |
| 469 | s = num_append(LOC,size); |
| 470 | s = str_append(LOC," stack frames:\n"); |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 471 | |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 472 | if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0)) |
| 473 | DUMP(logfile_fd) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 474 | if (!zlog_default) |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 475 | DUMP(STDERR_FILENO) |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 476 | else |
| 477 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 478 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 479 | DUMP(STDOUT_FILENO) |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 480 | /* Remove trailing '\n' for monitor and syslog */ |
| 481 | *--s = '\0'; |
| 482 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 483 | vty_log_fixed(buf,s-buf); |
| 484 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
| 485 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); |
| 486 | { |
| 487 | int i; |
| 488 | /* Just print the function addresses. */ |
| 489 | for (i = 0; i < size; i++) |
| 490 | { |
| 491 | s = buf; |
| 492 | s = str_append(LOC,"[bt "); |
| 493 | s = num_append(LOC,i); |
| 494 | s = str_append(LOC,"] 0x"); |
| 495 | s = hex_append(LOC,(u_long)(array[i])); |
| 496 | *s = '\0'; |
| 497 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) |
| 498 | vty_log_fixed(buf,s-buf); |
| 499 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 500 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 503 | } |
| 504 | #undef DUMP |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 505 | #undef LOC |
Paul Jakma | fb66b29 | 2006-05-28 08:26:15 +0000 | [diff] [blame] | 506 | #endif /* HAVE_STRACK_TRACE */ |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void |
| 510 | zlog_backtrace(int priority) |
| 511 | { |
| 512 | #ifndef HAVE_GLIBC_BACKTRACE |
| 513 | zlog(NULL, priority, "No backtrace available on this platform."); |
| 514 | #else |
| 515 | void *array[20]; |
| 516 | int size, i; |
| 517 | char **strings; |
| 518 | |
| 519 | if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) || |
| 520 | ((size_t)size > sizeof(array)/sizeof(array[0]))) |
| 521 | { |
| 522 | zlog_err("Cannot get backtrace, returned invalid # of frames %d " |
Andrew J. Schorr | 1ed72e0 | 2007-04-28 22:14:10 +0000 | [diff] [blame] | 523 | "(valid range is between 1 and %lu)", |
| 524 | size, (unsigned long)(sizeof(array)/sizeof(array[0]))); |
ajs | 063ee52 | 2004-11-26 18:11:14 +0000 | [diff] [blame] | 525 | return; |
| 526 | } |
| 527 | zlog(NULL, priority, "Backtrace for %d stack frames:", size); |
| 528 | if (!(strings = backtrace_symbols(array, size))) |
| 529 | { |
| 530 | zlog_err("Cannot get backtrace symbols (out of memory?)"); |
| 531 | for (i = 0; i < size; i++) |
| 532 | zlog(NULL, priority, "[bt %d] %p",i,array[i]); |
| 533 | } |
| 534 | else |
| 535 | { |
| 536 | for (i = 0; i < size; i++) |
| 537 | zlog(NULL, priority, "[bt %d] %s",i,strings[i]); |
| 538 | free(strings); |
| 539 | } |
| 540 | #endif /* HAVE_GLIBC_BACKTRACE */ |
ajs | 59a06a9 | 2004-11-23 18:19:14 +0000 | [diff] [blame] | 541 | } |
| 542 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 543 | void |
| 544 | zlog (struct zlog *zl, int priority, const char *format, ...) |
| 545 | { |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 546 | va_list args; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 547 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 548 | va_start(args, format); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 549 | vzlog (zl, priority, format, args); |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 550 | va_end (args); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 551 | } |
| 552 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 553 | #define ZLOG_FUNC(FUNCNAME,PRIORITY) \ |
| 554 | void \ |
| 555 | FUNCNAME(const char *format, ...) \ |
| 556 | { \ |
| 557 | va_list args; \ |
| 558 | va_start(args, format); \ |
| 559 | vzlog (NULL, PRIORITY, format, args); \ |
| 560 | va_end(args); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 561 | } |
| 562 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 563 | ZLOG_FUNC(zlog_err, LOG_ERR) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 564 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 565 | ZLOG_FUNC(zlog_warn, LOG_WARNING) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 566 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 567 | ZLOG_FUNC(zlog_info, LOG_INFO) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 568 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 569 | ZLOG_FUNC(zlog_notice, LOG_NOTICE) |
| 570 | |
| 571 | ZLOG_FUNC(zlog_debug, LOG_DEBUG) |
| 572 | |
| 573 | #undef ZLOG_FUNC |
| 574 | |
| 575 | #define PLOG_FUNC(FUNCNAME,PRIORITY) \ |
| 576 | void \ |
| 577 | FUNCNAME(struct zlog *zl, const char *format, ...) \ |
| 578 | { \ |
| 579 | va_list args; \ |
| 580 | va_start(args, format); \ |
| 581 | vzlog (zl, PRIORITY, format, args); \ |
| 582 | va_end(args); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 583 | } |
| 584 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 585 | PLOG_FUNC(plog_err, LOG_ERR) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 586 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 587 | PLOG_FUNC(plog_warn, LOG_WARNING) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 588 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 589 | PLOG_FUNC(plog_info, LOG_INFO) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 590 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 591 | PLOG_FUNC(plog_notice, LOG_NOTICE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 592 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 593 | PLOG_FUNC(plog_debug, LOG_DEBUG) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 594 | |
ajs | d246bd9 | 2004-11-23 17:35:08 +0000 | [diff] [blame] | 595 | #undef PLOG_FUNC |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 596 | |
ajs | cee3df1 | 2004-11-24 17:14:49 +0000 | [diff] [blame] | 597 | void |
| 598 | _zlog_assert_failed (const char *assertion, const char *file, |
| 599 | unsigned int line, const char *function) |
| 600 | { |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 601 | /* Force fallback file logging? */ |
| 602 | if (zlog_default && !zlog_default->fp && |
| 603 | ((logfile_fd = open_crashlog()) >= 0) && |
| 604 | ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL)) |
| 605 | zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR; |
ajs | 1e22135 | 2005-02-03 16:42:40 +0000 | [diff] [blame] | 606 | zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s", |
| 607 | assertion,file,line,(function ? function : "?")); |
| 608 | zlog_backtrace(LOG_CRIT); |
ajs | cee3df1 | 2004-11-24 17:14:49 +0000 | [diff] [blame] | 609 | abort(); |
| 610 | } |
| 611 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 612 | |
| 613 | /* Open log stream */ |
| 614 | struct zlog * |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 615 | openzlog (const char *progname, zlog_proto_t protocol, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 616 | int syslog_flags, int syslog_facility) |
| 617 | { |
| 618 | struct zlog *zl; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 619 | u_int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 620 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 621 | zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 622 | |
| 623 | zl->ident = progname; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 624 | zl->protocol = protocol; |
| 625 | zl->facility = syslog_facility; |
ajs | 7d149b8 | 2004-11-28 23:00:01 +0000 | [diff] [blame] | 626 | zl->syslog_options = syslog_flags; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 627 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 628 | /* Set default logging levels. */ |
| 629 | for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++) |
| 630 | zl->maxlvl[i] = ZLOG_DISABLED; |
| 631 | zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG; |
| 632 | zl->default_lvl = LOG_DEBUG; |
| 633 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 634 | openlog (progname, syslog_flags, zl->facility); |
| 635 | |
| 636 | return zl; |
| 637 | } |
| 638 | |
| 639 | void |
| 640 | closezlog (struct zlog *zl) |
| 641 | { |
| 642 | closelog(); |
| 643 | fclose (zl->fp); |
| 644 | |
| 645 | XFREE (MTYPE_ZLOG, zl); |
| 646 | } |
| 647 | |
| 648 | /* Called from command.c. */ |
| 649 | void |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 650 | zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 651 | { |
| 652 | if (zl == NULL) |
| 653 | zl = zlog_default; |
| 654 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 655 | zl->maxlvl[dest] = log_level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | int |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 659 | zlog_set_file (struct zlog *zl, const char *filename, int log_level) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 660 | { |
| 661 | FILE *fp; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 662 | mode_t oldumask; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 663 | |
| 664 | /* There is opend file. */ |
| 665 | zlog_reset_file (zl); |
| 666 | |
| 667 | /* Set default zl. */ |
| 668 | if (zl == NULL) |
| 669 | zl = zlog_default; |
| 670 | |
| 671 | /* Open file. */ |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 672 | oldumask = umask (0777 & ~LOGFILE_MASK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 673 | fp = fopen (filename, "a"); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 674 | umask(oldumask); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 675 | if (fp == NULL) |
| 676 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 677 | |
| 678 | /* Set flags. */ |
| 679 | zl->filename = strdup (filename); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 680 | zl->maxlvl[ZLOG_DEST_FILE] = log_level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 681 | zl->fp = fp; |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 682 | logfile_fd = fileno(fp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 683 | |
| 684 | return 1; |
| 685 | } |
| 686 | |
| 687 | /* Reset opend file. */ |
| 688 | int |
| 689 | zlog_reset_file (struct zlog *zl) |
| 690 | { |
| 691 | if (zl == NULL) |
| 692 | zl = zlog_default; |
| 693 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 694 | if (zl->fp) |
| 695 | fclose (zl->fp); |
| 696 | zl->fp = NULL; |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 697 | logfile_fd = -1; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 698 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 699 | |
| 700 | if (zl->filename) |
| 701 | free (zl->filename); |
| 702 | zl->filename = NULL; |
| 703 | |
| 704 | return 1; |
| 705 | } |
| 706 | |
| 707 | /* Reopen log file. */ |
| 708 | int |
| 709 | zlog_rotate (struct zlog *zl) |
| 710 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 711 | int level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 712 | |
| 713 | if (zl == NULL) |
| 714 | zl = zlog_default; |
| 715 | |
| 716 | if (zl->fp) |
| 717 | fclose (zl->fp); |
| 718 | zl->fp = NULL; |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 719 | logfile_fd = -1; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 720 | level = zl->maxlvl[ZLOG_DEST_FILE]; |
| 721 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 722 | |
| 723 | if (zl->filename) |
| 724 | { |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 725 | mode_t oldumask; |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 726 | int save_errno; |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 727 | |
| 728 | oldumask = umask (0777 & ~LOGFILE_MASK); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 729 | zl->fp = fopen (zl->filename, "a"); |
| 730 | save_errno = errno; |
| 731 | umask(oldumask); |
| 732 | if (zl->fp == NULL) |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 733 | { |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 734 | zlog_err("Log rotate failed: cannot open file %s for append: %s", |
| 735 | zl->filename, safe_strerror(save_errno)); |
gdt | aa593d5 | 2003-12-22 20:15:53 +0000 | [diff] [blame] | 736 | return -1; |
| 737 | } |
ajs | c4c7d0c | 2005-02-03 19:22:05 +0000 | [diff] [blame] | 738 | logfile_fd = fileno(zl->fp); |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 739 | zl->maxlvl[ZLOG_DEST_FILE] = level; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | return 1; |
| 743 | } |
| 744 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 745 | /* Message lookup function. */ |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 746 | const char * |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 747 | lookup (const struct message *mes, int key) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 748 | { |
Stephen Hemminger | 1423c80 | 2008-08-14 17:59:25 +0100 | [diff] [blame] | 749 | const struct message *pnt; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 750 | |
| 751 | for (pnt = mes; pnt->key != 0; pnt++) |
| 752 | if (pnt->key == key) |
| 753 | return pnt->str; |
| 754 | |
| 755 | return ""; |
| 756 | } |
| 757 | |
Andrew J. Schorr | afb88a6 | 2007-03-20 20:48:27 +0000 | [diff] [blame] | 758 | /* Older/faster version of message lookup function, but requires caller to pass |
Paul Jakma | 11486b5 | 2008-02-28 23:26:02 +0000 | [diff] [blame] | 759 | * in the array size (instead of relying on a 0 key to terminate the search). |
| 760 | * |
| 761 | * The return value is the message string if found, or the 'none' pointer |
| 762 | * provided otherwise. |
| 763 | */ |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 764 | const char * |
Paul Jakma | 11486b5 | 2008-02-28 23:26:02 +0000 | [diff] [blame] | 765 | mes_lookup (struct message *meslist, int max, int index, const char *none) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 766 | { |
Paul Jakma | 11486b5 | 2008-02-28 23:26:02 +0000 | [diff] [blame] | 767 | int pos = index - meslist[0].key; |
| 768 | |
Andrew J. Schorr | afb88a6 | 2007-03-20 20:48:27 +0000 | [diff] [blame] | 769 | /* first check for best case: index is in range and matches the key |
Paul Jakma | 11486b5 | 2008-02-28 23:26:02 +0000 | [diff] [blame] | 770 | * value in that slot. |
| 771 | * NB: key numbering might be offset from 0. E.g. protocol constants |
| 772 | * often start at 1. |
| 773 | */ |
| 774 | if ((pos >= 0) && (pos < max) |
| 775 | && (meslist[pos].key == index)) |
| 776 | return meslist[pos].str; |
Andrew J. Schorr | afb88a6 | 2007-03-20 20:48:27 +0000 | [diff] [blame] | 777 | |
| 778 | /* fall back to linear search */ |
| 779 | { |
| 780 | int i; |
| 781 | |
| 782 | for (i = 0; i < max; i++, meslist++) |
| 783 | { |
| 784 | if (meslist->key == index) |
| 785 | { |
Paul Jakma | 11486b5 | 2008-02-28 23:26:02 +0000 | [diff] [blame] | 786 | const char *str = (meslist->str ? meslist->str : none); |
| 787 | |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 788 | zlog_debug ("message index %d [%s] found in position %d (max is %d)", |
Paul Jakma | 11486b5 | 2008-02-28 23:26:02 +0000 | [diff] [blame] | 789 | index, str, i, max); |
| 790 | return str; |
Andrew J. Schorr | afb88a6 | 2007-03-20 20:48:27 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | } |
| 794 | zlog_err("message index %d not found (max is %d)", index, max); |
Paul Jakma | 11486b5 | 2008-02-28 23:26:02 +0000 | [diff] [blame] | 795 | assert (none); |
| 796 | return none; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 797 | } |
ajs | ca35976 | 2004-11-19 23:40:16 +0000 | [diff] [blame] | 798 | |
| 799 | /* Wrapper around strerror to handle case where it returns NULL. */ |
| 800 | const char * |
| 801 | safe_strerror(int errnum) |
| 802 | { |
| 803 | const char *s = strerror(errnum); |
| 804 | return (s != NULL) ? s : "Unknown error"; |
| 805 | } |
ajs | f52d13c | 2005-10-01 17:38:06 +0000 | [diff] [blame] | 806 | |
Paul Jakma | d6d672a | 2006-05-15 16:56:51 +0000 | [diff] [blame] | 807 | struct zebra_desc_table |
| 808 | { |
| 809 | unsigned int type; |
ajs | f52d13c | 2005-10-01 17:38:06 +0000 | [diff] [blame] | 810 | const char *string; |
| 811 | char chr; |
ajs | f52d13c | 2005-10-01 17:38:06 +0000 | [diff] [blame] | 812 | }; |
| 813 | |
Paul Jakma | d6d672a | 2006-05-15 16:56:51 +0000 | [diff] [blame] | 814 | #define DESC_ENTRY(T,S,C) [(T)] = { (T), (S), (C) } |
| 815 | static const struct zebra_desc_table route_types[] = { |
| 816 | DESC_ENTRY (ZEBRA_ROUTE_SYSTEM, "system", 'X' ), |
| 817 | DESC_ENTRY (ZEBRA_ROUTE_KERNEL, "kernel", 'K' ), |
| 818 | DESC_ENTRY (ZEBRA_ROUTE_CONNECT, "connected", 'C' ), |
| 819 | DESC_ENTRY (ZEBRA_ROUTE_STATIC, "static", 'S' ), |
| 820 | DESC_ENTRY (ZEBRA_ROUTE_RIP, "rip", 'R' ), |
| 821 | DESC_ENTRY (ZEBRA_ROUTE_RIPNG, "ripng", 'R' ), |
| 822 | DESC_ENTRY (ZEBRA_ROUTE_OSPF, "ospf", 'O' ), |
| 823 | DESC_ENTRY (ZEBRA_ROUTE_OSPF6, "ospf6", 'O' ), |
| 824 | DESC_ENTRY (ZEBRA_ROUTE_ISIS, "isis", 'I' ), |
| 825 | DESC_ENTRY (ZEBRA_ROUTE_BGP, "bgp", 'B' ), |
| 826 | DESC_ENTRY (ZEBRA_ROUTE_HSLS, "hsls", 'H' ), |
| 827 | }; |
| 828 | #undef DESC_ENTRY |
| 829 | |
| 830 | #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' } |
| 831 | static const struct zebra_desc_table command_types[] = { |
| 832 | DESC_ENTRY (ZEBRA_INTERFACE_ADD), |
| 833 | DESC_ENTRY (ZEBRA_INTERFACE_DELETE), |
| 834 | DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD), |
| 835 | DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE), |
| 836 | DESC_ENTRY (ZEBRA_INTERFACE_UP), |
| 837 | DESC_ENTRY (ZEBRA_INTERFACE_DOWN), |
| 838 | DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD), |
| 839 | DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE), |
| 840 | DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD), |
| 841 | DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE), |
| 842 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD), |
| 843 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE), |
| 844 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD), |
| 845 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE), |
| 846 | DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP), |
| 847 | DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP), |
| 848 | DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP), |
| 849 | DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP), |
| 850 | DESC_ENTRY (ZEBRA_INTERFACE_RENAME), |
| 851 | DESC_ENTRY (ZEBRA_ROUTER_ID_ADD), |
| 852 | DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE), |
| 853 | DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE), |
| 854 | }; |
| 855 | #undef DESC_ENTRY |
| 856 | |
| 857 | static const struct zebra_desc_table unknown = { 0, "unknown", '?' }; |
| 858 | |
| 859 | static const struct zebra_desc_table * |
ajs | f52d13c | 2005-10-01 17:38:06 +0000 | [diff] [blame] | 860 | zroute_lookup(u_int zroute) |
| 861 | { |
ajs | f52d13c | 2005-10-01 17:38:06 +0000 | [diff] [blame] | 862 | u_int i; |
| 863 | |
| 864 | if (zroute >= sizeof(route_types)/sizeof(route_types[0])) |
| 865 | { |
| 866 | zlog_err("unknown zebra route type: %u", zroute); |
| 867 | return &unknown; |
| 868 | } |
Paul Jakma | d6d672a | 2006-05-15 16:56:51 +0000 | [diff] [blame] | 869 | if (zroute == route_types[zroute].type) |
ajs | f52d13c | 2005-10-01 17:38:06 +0000 | [diff] [blame] | 870 | return &route_types[zroute]; |
| 871 | for (i = 0; i < sizeof(route_types)/sizeof(route_types[0]); i++) |
| 872 | { |
Paul Jakma | d6d672a | 2006-05-15 16:56:51 +0000 | [diff] [blame] | 873 | if (zroute == route_types[i].type) |
ajs | f52d13c | 2005-10-01 17:38:06 +0000 | [diff] [blame] | 874 | { |
| 875 | zlog_warn("internal error: route type table out of order " |
| 876 | "while searching for %u, please notify developers", zroute); |
| 877 | return &route_types[i]; |
| 878 | } |
| 879 | } |
| 880 | zlog_err("internal error: cannot find route type %u in table!", zroute); |
| 881 | return &unknown; |
| 882 | } |
| 883 | |
| 884 | const char * |
| 885 | zebra_route_string(u_int zroute) |
| 886 | { |
| 887 | return zroute_lookup(zroute)->string; |
| 888 | } |
| 889 | |
| 890 | char |
| 891 | zebra_route_char(u_int zroute) |
| 892 | { |
| 893 | return zroute_lookup(zroute)->chr; |
| 894 | } |
Paul Jakma | d6d672a | 2006-05-15 16:56:51 +0000 | [diff] [blame] | 895 | |
| 896 | const char * |
| 897 | zserv_command_string (unsigned int command) |
| 898 | { |
| 899 | if (command >= sizeof(command_types)/sizeof(command_types[0])) |
| 900 | { |
| 901 | zlog_err ("unknown zserv command type: %u", command); |
| 902 | return unknown.string; |
| 903 | } |
| 904 | return command_types[command].string; |
| 905 | } |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 906 | |
| 907 | #define RTSIZE (sizeof(route_types)/sizeof(route_types[0])) |
| 908 | |
| 909 | int |
| 910 | proto_name2num(const char *s) |
| 911 | { |
| 912 | unsigned i; |
| 913 | |
| 914 | for (i=0; i<RTSIZE; ++i) |
| 915 | if (strcasecmp(s, route_types[i].string) == 0) |
| 916 | return route_types[i].type; |
| 917 | return -1; |
| 918 | } |
| 919 | #undef RTSIZE |