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