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