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