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