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