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