]>
Commit | Line | Data |
---|---|---|
274a4a44 | 1 | /* |
274a4a44 | 2 | * Logging of zebra |
718e3744 | 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 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 20 | */ |
21 | ||
ab0181ee | 22 | #define FRR_DEFINE_DESC_TABLE |
e0ca5fde | 23 | |
718e3744 | 24 | #include <zebra.h> |
25 | ||
8613585e | 26 | #include "zclient.h" |
718e3744 | 27 | #include "log.h" |
deaa50db | 28 | #include "log_int.h" |
718e3744 | 29 | #include "memory.h" |
30 | #include "command.h" | |
7d149b8e | 31 | #ifndef SUNOS_5 |
32 | #include <sys/un.h> | |
33 | #endif | |
30a2231a PJ |
34 | /* for printstack on solaris */ |
35 | #ifdef HAVE_UCONTEXT_H | |
36 | #include <ucontext.h> | |
37 | #endif | |
718e3744 | 38 | |
4a1ab8e4 DL |
39 | DEFINE_MTYPE_STATIC(LIB, ZLOG, "Logging") |
40 | ||
c4c7d0c4 | 41 | static int logfile_fd = -1; /* Used in signal handler. */ |
1e221354 | 42 | |
718e3744 | 43 | struct zlog *zlog_default = NULL; |
44 | ||
718e3744 | 45 | const char *zlog_priority[] = |
46 | { | |
47 | "emergencies", | |
48 | "alerts", | |
49 | "critical", | |
50 | "errors", | |
51 | "warnings", | |
52 | "notifications", | |
53 | "informational", | |
54 | "debugging", | |
55 | NULL, | |
56 | }; | |
718e3744 | 57 | |
7a49a5b5 DS |
58 | /* |
59 | * write_wrapper | |
60 | * | |
61 | * glibc has declared that the return value from write *must* not be | |
62 | * ignored. | |
63 | * gcc see's this problem and issues a warning for the line. | |
64 | * | |
65 | * Why is this a big deal you say? Because both of them are right | |
66 | * and if you have -Werror enabled then all calls to write | |
67 | * generate a build error and the build stops. | |
68 | * | |
69 | * clang has helpfully allowed this construct: | |
70 | * (void)write(...) | |
71 | * to tell the compiler yeah I know it has a return value | |
72 | * I don't care about it at this time. | |
73 | * gcc doesn't have this ability. | |
74 | * | |
75 | * This code was written such that it didn't care about the | |
76 | * return value from write. At this time do I want | |
77 | * to go through and fix and test this code for correctness. | |
78 | * So just wrapper the bad behavior and move on. | |
79 | */ | |
80 | static void write_wrapper (int fd, const void *buf, size_t count) | |
81 | { | |
82 | if (write (fd, buf, count) <= 0) | |
83 | return; | |
84 | ||
85 | return; | |
86 | } | |
6b0655a2 | 87 | |
56b40679 QY |
88 | /** |
89 | * Looks up a message in a message list by key. | |
90 | * | |
91 | * If the message is not found, returns the provided error message. | |
92 | * | |
93 | * Terminates when it hits a struct message that's all zeros. | |
94 | * | |
95 | * @param mz the message list | |
96 | * @param kz the message key | |
97 | * @param nf the message to return if not found | |
98 | * @return the message | |
99 | */ | |
100 | const char * | |
101 | lookup_msg(const struct message *mz, int kz, const char *nf) | |
102 | { | |
103 | static struct message nt = { 0 }; | |
104 | const char *rz = nf ? nf : "(no message found)"; | |
105 | const struct message *pnt; | |
106 | for (pnt = mz; memcmp(pnt, &nt, sizeof(struct message)); pnt++) | |
107 | if (pnt->key == kz) { | |
108 | rz = pnt->str ? pnt->str : rz; | |
109 | break; | |
110 | } | |
111 | return rz; | |
112 | } | |
718e3744 | 113 | |
56b40679 | 114 | /* For time string format. */ |
1ed72e0b AS |
115 | size_t |
116 | quagga_timestamp(int timestamp_precision, char *buf, size_t buflen) | |
718e3744 | 117 | { |
1ed72e0b AS |
118 | static struct { |
119 | time_t last; | |
120 | size_t len; | |
121 | char buf[28]; | |
122 | } cache; | |
123 | struct timeval clock; | |
124 | ||
1ed72e0b AS |
125 | gettimeofday(&clock, NULL); |
126 | ||
127 | /* first, we update the cache if the time has changed */ | |
128 | if (cache.last != clock.tv_sec) | |
129 | { | |
130 | struct tm *tm; | |
131 | cache.last = clock.tv_sec; | |
132 | tm = localtime(&cache.last); | |
133 | cache.len = strftime(cache.buf, sizeof(cache.buf), | |
134 | "%Y/%m/%d %H:%M:%S", tm); | |
135 | } | |
136 | /* note: it's not worth caching the subsecond part, because | |
137 | chances are that back-to-back calls are not sufficiently close together | |
138 | for the clock not to have ticked forward */ | |
718e3744 | 139 | |
1ed72e0b AS |
140 | if (buflen > cache.len) |
141 | { | |
142 | memcpy(buf, cache.buf, cache.len); | |
143 | if ((timestamp_precision > 0) && | |
144 | (buflen > cache.len+1+timestamp_precision)) | |
145 | { | |
146 | /* should we worry about locale issues? */ | |
bcdda30b AS |
147 | static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1}; |
148 | int prec; | |
149 | char *p = buf+cache.len+1+(prec = timestamp_precision); | |
150 | *p-- = '\0'; | |
151 | while (prec > 6) | |
152 | /* this is unlikely to happen, but protect anyway */ | |
153 | { | |
154 | *p-- = '0'; | |
155 | prec--; | |
156 | } | |
157 | clock.tv_usec /= divisor[prec]; | |
1ed72e0b AS |
158 | do |
159 | { | |
bcdda30b AS |
160 | *p-- = '0'+(clock.tv_usec % 10); |
161 | clock.tv_usec /= 10; | |
1ed72e0b | 162 | } |
bcdda30b AS |
163 | while (--prec > 0); |
164 | *p = '.'; | |
165 | return cache.len+1+timestamp_precision; | |
1ed72e0b AS |
166 | } |
167 | buf[cache.len] = '\0'; | |
168 | return cache.len; | |
169 | } | |
170 | if (buflen > 0) | |
171 | buf[0] = '\0'; | |
172 | return 0; | |
173 | } | |
718e3744 | 174 | |
1ed72e0b AS |
175 | /* Utility routine for current time printing. */ |
176 | static void | |
177 | time_print(FILE *fp, struct timestamp_control *ctl) | |
178 | { | |
179 | if (!ctl->already_rendered) | |
180 | { | |
181 | ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf)); | |
182 | ctl->already_rendered = 1; | |
183 | } | |
184 | fprintf(fp, "%s ", ctl->buf); | |
718e3744 | 185 | } |
1ed72e0b | 186 | |
6b0655a2 | 187 | |
718e3744 | 188 | /* va_list version of zlog. */ |
eac6e3f0 | 189 | void |
bb85d700 | 190 | vzlog (int priority, const char *format, va_list args) |
718e3744 | 191 | { |
7c8ff89e | 192 | char proto_str[32]; |
d8efb772 | 193 | int original_errno = errno; |
1ed72e0b AS |
194 | struct timestamp_control tsctl; |
195 | tsctl.already_rendered = 0; | |
bb85d700 | 196 | struct zlog *zl = zlog_default; |
718e3744 | 197 | |
198 | /* When zlog_default is also NULL, use stderr for logging. */ | |
199 | if (zl == NULL) | |
200 | { | |
1ed72e0b AS |
201 | tsctl.precision = 0; |
202 | time_print(stderr, &tsctl); | |
718e3744 | 203 | fprintf (stderr, "%s: ", "unknown"); |
d246bd96 | 204 | vfprintf (stderr, format, args); |
718e3744 | 205 | fprintf (stderr, "\n"); |
206 | fflush (stderr); | |
207 | ||
208 | /* In this case we return at here. */ | |
d8efb772 | 209 | errno = original_errno; |
718e3744 | 210 | return; |
211 | } | |
1ed72e0b | 212 | tsctl.precision = zl->timestamp_precision; |
718e3744 | 213 | |
718e3744 | 214 | /* Syslog output */ |
274a4a44 | 215 | if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG]) |
d246bd96 | 216 | { |
217 | va_list ac; | |
218 | va_copy(ac, args); | |
219 | vsyslog (priority|zlog_default->facility, format, ac); | |
220 | va_end(ac); | |
221 | } | |
718e3744 | 222 | |
7c8ff89e | 223 | if (zl->instance) |
bf1013e6 | 224 | sprintf (proto_str, "%s[%d]: ", zl->protoname, zl->instance); |
7c8ff89e | 225 | else |
bf1013e6 | 226 | sprintf (proto_str, "%s: ", zl->protoname); |
7c8ff89e | 227 | |
718e3744 | 228 | /* File output. */ |
274a4a44 | 229 | if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp) |
718e3744 | 230 | { |
d246bd96 | 231 | va_list ac; |
1ed72e0b | 232 | time_print (zl->fp, &tsctl); |
b04c699e | 233 | if (zl->record_priority) |
234 | fprintf (zl->fp, "%s: ", zlog_priority[priority]); | |
7c8ff89e | 235 | fprintf (zl->fp, "%s", proto_str); |
d246bd96 | 236 | va_copy(ac, args); |
237 | vfprintf (zl->fp, format, ac); | |
238 | va_end(ac); | |
718e3744 | 239 | fprintf (zl->fp, "\n"); |
240 | fflush (zl->fp); | |
241 | } | |
242 | ||
243 | /* stdout output. */ | |
274a4a44 | 244 | if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT]) |
718e3744 | 245 | { |
d246bd96 | 246 | va_list ac; |
1ed72e0b | 247 | time_print (stdout, &tsctl); |
b04c699e | 248 | if (zl->record_priority) |
249 | fprintf (stdout, "%s: ", zlog_priority[priority]); | |
7c8ff89e | 250 | fprintf (stdout, "%s", proto_str); |
d246bd96 | 251 | va_copy(ac, args); |
252 | vfprintf (stdout, format, ac); | |
253 | va_end(ac); | |
718e3744 | 254 | fprintf (stdout, "\n"); |
255 | fflush (stdout); | |
256 | } | |
257 | ||
718e3744 | 258 | /* Terminal monitor. */ |
274a4a44 | 259 | if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR]) |
260 | vty_log ((zl->record_priority ? zlog_priority[priority] : NULL), | |
7c8ff89e | 261 | proto_str, format, &tsctl, args); |
d8efb772 CF |
262 | |
263 | errno = original_errno; | |
718e3744 | 264 | } |
265 | ||
65efcfce | 266 | int |
dd8376fe | 267 | vzlog_test (int priority) |
65efcfce | 268 | { |
dd8376fe | 269 | struct zlog *zl = zlog_default; |
65efcfce LB |
270 | |
271 | /* When zlog_default is also NULL, use stderr for logging. */ | |
272 | if (zl == NULL) | |
273 | { | |
274 | return 1; | |
275 | } | |
276 | ||
277 | /* Syslog output */ | |
278 | if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG]) | |
279 | { | |
280 | return 1; | |
281 | } | |
282 | ||
283 | /* File output. */ | |
284 | if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp) | |
285 | { | |
286 | return 1; | |
287 | } | |
288 | ||
289 | /* stdout output. */ | |
290 | if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT]) | |
291 | { | |
292 | return 1; | |
293 | } | |
294 | ||
295 | /* Terminal monitor. */ | |
296 | if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR]) | |
297 | return 1; | |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
59a06a91 | 302 | static char * |
303 | str_append(char *dst, int len, const char *src) | |
304 | { | |
305 | while ((len-- > 0) && *src) | |
306 | *dst++ = *src++; | |
307 | return dst; | |
308 | } | |
309 | ||
310 | static char * | |
311 | num_append(char *s, int len, u_long x) | |
312 | { | |
313 | char buf[30]; | |
7d149b8e | 314 | char *t; |
59a06a91 | 315 | |
7d149b8e | 316 | if (!x) |
317 | return str_append(s,len,"0"); | |
318 | *(t = &buf[sizeof(buf)-1]) = '\0'; | |
59a06a91 | 319 | while (x && (t > buf)) |
320 | { | |
321 | *--t = '0'+(x % 10); | |
322 | x /= 10; | |
323 | } | |
324 | return str_append(s,len,t); | |
325 | } | |
326 | ||
fb66b29c | 327 | #if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE) |
7d149b8e | 328 | static char * |
329 | hex_append(char *s, int len, u_long x) | |
330 | { | |
331 | char buf[30]; | |
332 | char *t; | |
333 | ||
334 | if (!x) | |
335 | return str_append(s,len,"0"); | |
336 | *(t = &buf[sizeof(buf)-1]) = '\0'; | |
337 | while (x && (t > buf)) | |
338 | { | |
339 | u_int cc = (x % 16); | |
340 | *--t = ((cc < 10) ? ('0'+cc) : ('a'+cc-10)); | |
341 | x /= 16; | |
342 | } | |
343 | return str_append(s,len,t); | |
344 | } | |
31364274 | 345 | #endif |
7d149b8e | 346 | |
7d149b8e | 347 | /* Needs to be enhanced to support Solaris. */ |
348 | static int | |
349 | syslog_connect(void) | |
350 | { | |
351 | #ifdef SUNOS_5 | |
352 | return -1; | |
353 | #else | |
354 | int fd; | |
355 | char *s; | |
356 | struct sockaddr_un addr; | |
357 | ||
358 | if ((fd = socket(AF_UNIX,SOCK_DGRAM,0)) < 0) | |
359 | return -1; | |
360 | addr.sun_family = AF_UNIX; | |
361 | #ifdef _PATH_LOG | |
362 | #define SYSLOG_SOCKET_PATH _PATH_LOG | |
363 | #else | |
364 | #define SYSLOG_SOCKET_PATH "/dev/log" | |
365 | #endif | |
366 | s = str_append(addr.sun_path,sizeof(addr.sun_path),SYSLOG_SOCKET_PATH); | |
367 | #undef SYSLOG_SOCKET_PATH | |
368 | *s = '\0'; | |
369 | if (connect(fd,(struct sockaddr *)&addr,sizeof(addr)) < 0) | |
370 | { | |
371 | close(fd); | |
372 | return -1; | |
373 | } | |
374 | return fd; | |
375 | #endif | |
376 | } | |
377 | ||
378 | static void | |
379 | syslog_sigsafe(int priority, const char *msg, size_t msglen) | |
380 | { | |
1e221354 | 381 | static int syslog_fd = -1; |
7d149b8e | 382 | char buf[sizeof("<1234567890>ripngd[1234567890]: ")+msglen+50]; |
383 | char *s; | |
384 | ||
385 | if ((syslog_fd < 0) && ((syslog_fd = syslog_connect()) < 0)) | |
386 | return; | |
387 | ||
388 | #define LOC s,buf+sizeof(buf)-s | |
389 | s = buf; | |
390 | s = str_append(LOC,"<"); | |
391 | s = num_append(LOC,priority); | |
392 | s = str_append(LOC,">"); | |
393 | /* forget about the timestamp, too difficult in a signal handler */ | |
394 | s = str_append(LOC,zlog_default->ident); | |
395 | if (zlog_default->syslog_options & LOG_PID) | |
396 | { | |
397 | s = str_append(LOC,"["); | |
398 | s = num_append(LOC,getpid()); | |
399 | s = str_append(LOC,"]"); | |
400 | } | |
401 | s = str_append(LOC,": "); | |
402 | s = str_append(LOC,msg); | |
7a49a5b5 | 403 | write_wrapper (syslog_fd,buf,s-buf); |
7d149b8e | 404 | #undef LOC |
405 | } | |
406 | ||
1e221354 | 407 | static int |
408 | open_crashlog(void) | |
409 | { | |
410 | #define CRASHLOG_PREFIX "/var/tmp/quagga." | |
411 | #define CRASHLOG_SUFFIX "crashlog" | |
412 | if (zlog_default && zlog_default->ident) | |
413 | { | |
414 | /* Avoid strlen since it is not async-signal-safe. */ | |
415 | const char *p; | |
416 | size_t ilen; | |
417 | ||
418 | for (p = zlog_default->ident, ilen = 0; *p; p++) | |
419 | ilen++; | |
420 | { | |
421 | char buf[sizeof(CRASHLOG_PREFIX)+ilen+sizeof(CRASHLOG_SUFFIX)+3]; | |
422 | char *s = buf; | |
423 | #define LOC s,buf+sizeof(buf)-s | |
424 | s = str_append(LOC, CRASHLOG_PREFIX); | |
425 | s = str_append(LOC, zlog_default->ident); | |
426 | s = str_append(LOC, "."); | |
427 | s = str_append(LOC, CRASHLOG_SUFFIX); | |
428 | #undef LOC | |
429 | *s = '\0'; | |
430 | return open(buf, O_WRONLY|O_CREAT|O_EXCL, LOGFILE_MASK); | |
431 | } | |
432 | } | |
433 | return open(CRASHLOG_PREFIX CRASHLOG_SUFFIX, O_WRONLY|O_CREAT|O_EXCL, | |
434 | LOGFILE_MASK); | |
435 | #undef CRASHLOG_SUFFIX | |
436 | #undef CRASHLOG_PREFIX | |
437 | } | |
438 | ||
7d149b8e | 439 | /* Note: the goal here is to use only async-signal-safe functions. */ |
59a06a91 | 440 | void |
31364274 | 441 | zlog_signal(int signo, const char *action |
442 | #ifdef SA_SIGINFO | |
443 | , siginfo_t *siginfo, void *program_counter | |
444 | #endif | |
445 | ) | |
59a06a91 | 446 | { |
447 | time_t now; | |
40abf239 | 448 | char buf[sizeof("DEFAULT: Received signal S at T (si_addr 0xP, PC 0xP); aborting...")+100]; |
59a06a91 | 449 | char *s = buf; |
7d149b8e | 450 | char *msgstart = buf; |
59a06a91 | 451 | #define LOC s,buf+sizeof(buf)-s |
452 | ||
453 | time(&now); | |
454 | if (zlog_default) | |
455 | { | |
bf1013e6 | 456 | s = str_append(LOC,zlog_default->protoname); |
59a06a91 | 457 | *s++ = ':'; |
458 | *s++ = ' '; | |
7d149b8e | 459 | msgstart = s; |
59a06a91 | 460 | } |
461 | s = str_append(LOC,"Received signal "); | |
462 | s = num_append(LOC,signo); | |
463 | s = str_append(LOC," at "); | |
464 | s = num_append(LOC,now); | |
31364274 | 465 | #ifdef SA_SIGINFO |
40abf239 | 466 | s = str_append(LOC," (si_addr 0x"); |
467 | s = hex_append(LOC,(u_long)(siginfo->si_addr)); | |
468 | if (program_counter) | |
469 | { | |
470 | s = str_append(LOC,", PC 0x"); | |
471 | s = hex_append(LOC,(u_long)program_counter); | |
472 | } | |
473 | s = str_append(LOC,"); "); | |
31364274 | 474 | #else /* SA_SIGINFO */ |
475 | s = str_append(LOC,"; "); | |
476 | #endif /* SA_SIGINFO */ | |
59a06a91 | 477 | s = str_append(LOC,action); |
7d149b8e | 478 | if (s < buf+sizeof(buf)) |
479 | *s++ = '\n'; | |
59a06a91 | 480 | |
274a4a44 | 481 | /* N.B. implicit priority is most severe */ |
1e221354 | 482 | #define PRI LOG_CRIT |
274a4a44 | 483 | |
7a49a5b5 | 484 | #define DUMP(FD) write_wrapper(FD, buf, s-buf); |
1e221354 | 485 | /* If no file logging configured, try to write to fallback log file. */ |
c4c7d0c4 | 486 | if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0)) |
487 | DUMP(logfile_fd) | |
59a06a91 | 488 | if (!zlog_default) |
c4c7d0c4 | 489 | DUMP(STDERR_FILENO) |
59a06a91 | 490 | else |
491 | { | |
274a4a44 | 492 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) |
c4c7d0c4 | 493 | DUMP(STDOUT_FILENO) |
274a4a44 | 494 | /* Remove trailing '\n' for monitor and syslog */ |
495 | *--s = '\0'; | |
496 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) | |
497 | vty_log_fixed(buf,s-buf); | |
498 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) | |
499 | syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart); | |
59a06a91 | 500 | } |
501 | #undef DUMP | |
502 | ||
31364274 | 503 | zlog_backtrace_sigsafe(PRI, |
504 | #ifdef SA_SIGINFO | |
505 | program_counter | |
506 | #else | |
507 | NULL | |
508 | #endif | |
509 | ); | |
d1265948 DL |
510 | |
511 | s = buf; | |
e0bebc7c QY |
512 | struct thread *tc; |
513 | tc = pthread_getspecific (thread_current); | |
514 | if (!tc) | |
d1265948 DL |
515 | s = str_append (LOC, "no thread information available\n"); |
516 | else | |
517 | { | |
518 | s = str_append (LOC, "in thread "); | |
e0bebc7c | 519 | s = str_append (LOC, tc->funcname); |
d1265948 | 520 | s = str_append (LOC, " scheduled from "); |
e0bebc7c | 521 | s = str_append (LOC, tc->schedfrom); |
d1265948 | 522 | s = str_append (LOC, ":"); |
e0bebc7c | 523 | s = num_append (LOC, tc->schedfrom_line); |
d1265948 DL |
524 | s = str_append (LOC, "\n"); |
525 | } | |
526 | ||
69f30024 | 527 | #define DUMP(FD) write_wrapper(FD, buf, s-buf); |
d1265948 DL |
528 | /* If no file logging configured, try to write to fallback log file. */ |
529 | if (logfile_fd >= 0) | |
530 | DUMP(logfile_fd) | |
531 | if (!zlog_default) | |
532 | DUMP(STDERR_FILENO) | |
533 | else | |
534 | { | |
535 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) | |
536 | DUMP(STDOUT_FILENO) | |
537 | /* Remove trailing '\n' for monitor and syslog */ | |
538 | *--s = '\0'; | |
539 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) | |
540 | vty_log_fixed(buf,s-buf); | |
541 | if (PRI <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) | |
542 | syslog_sigsafe(PRI|zlog_default->facility,msgstart,s-msgstart); | |
543 | } | |
544 | #undef DUMP | |
545 | ||
274a4a44 | 546 | #undef PRI |
063ee52a | 547 | #undef LOC |
548 | } | |
549 | ||
550 | /* Log a backtrace using only async-signal-safe functions. | |
551 | Needs to be enhanced to support syslog logging. */ | |
552 | void | |
239c26fd | 553 | zlog_backtrace_sigsafe(int priority, void *program_counter) |
063ee52a | 554 | { |
fb66b29c | 555 | #ifdef HAVE_STACK_TRACE |
239c26fd | 556 | static const char pclabel[] = "Program counter: "; |
94fc1dd4 | 557 | void *array[64]; |
063ee52a | 558 | int size; |
559 | char buf[100]; | |
94fc1dd4 | 560 | char *s, **bt = NULL; |
063ee52a | 561 | #define LOC s,buf+sizeof(buf)-s |
59a06a91 | 562 | |
fb66b29c | 563 | #ifdef HAVE_GLIBC_BACKTRACE |
4d474fa3 DL |
564 | size = backtrace(array, array_size(array)); |
565 | if (size <= 0 || (size_t)size > array_size(array)) | |
063ee52a | 566 | return; |
59a06a91 | 567 | |
1e221354 | 568 | #define DUMP(FD) { \ |
239c26fd | 569 | if (program_counter) \ |
570 | { \ | |
7a49a5b5 | 571 | write_wrapper(FD, pclabel, sizeof(pclabel)-1); \ |
1e221354 | 572 | backtrace_symbols_fd(&program_counter, 1, FD); \ |
239c26fd | 573 | } \ |
7a49a5b5 | 574 | write_wrapper(FD, buf, s-buf); \ |
1e221354 | 575 | backtrace_symbols_fd(array, size, FD); \ |
59a06a91 | 576 | } |
fb66b29c PJ |
577 | #elif defined(HAVE_PRINTSTACK) |
578 | #define DUMP(FD) { \ | |
579 | if (program_counter) \ | |
7a49a5b5 DS |
580 | write_wrapper((FD), pclabel, sizeof(pclabel)-1); \ |
581 | write_wrapper((FD), buf, s-buf); \ | |
fb66b29c PJ |
582 | printstack((FD)); \ |
583 | } | |
584 | #endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */ | |
585 | ||
586 | s = buf; | |
587 | s = str_append(LOC,"Backtrace for "); | |
588 | s = num_append(LOC,size); | |
589 | s = str_append(LOC," stack frames:\n"); | |
59a06a91 | 590 | |
c4c7d0c4 | 591 | if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0)) |
592 | DUMP(logfile_fd) | |
59a06a91 | 593 | if (!zlog_default) |
c4c7d0c4 | 594 | DUMP(STDERR_FILENO) |
59a06a91 | 595 | else |
596 | { | |
274a4a44 | 597 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_STDOUT]) |
c4c7d0c4 | 598 | DUMP(STDOUT_FILENO) |
274a4a44 | 599 | /* Remove trailing '\n' for monitor and syslog */ |
600 | *--s = '\0'; | |
601 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) | |
602 | vty_log_fixed(buf,s-buf); | |
603 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) | |
604 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); | |
605 | { | |
606 | int i; | |
94fc1dd4 SH |
607 | #ifdef HAVE_GLIBC_BACKTRACE |
608 | bt = backtrace_symbols(array, size); | |
609 | #endif | |
274a4a44 | 610 | /* Just print the function addresses. */ |
611 | for (i = 0; i < size; i++) | |
612 | { | |
613 | s = buf; | |
94fc1dd4 SH |
614 | if (bt) |
615 | s = str_append(LOC, bt[i]); | |
616 | else { | |
617 | s = str_append(LOC,"[bt "); | |
618 | s = num_append(LOC,i); | |
619 | s = str_append(LOC,"] 0x"); | |
620 | s = hex_append(LOC,(u_long)(array[i])); | |
621 | } | |
274a4a44 | 622 | *s = '\0'; |
623 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR]) | |
624 | vty_log_fixed(buf,s-buf); | |
625 | if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG]) | |
7d149b8e | 626 | syslog_sigsafe(priority|zlog_default->facility,buf,s-buf); |
274a4a44 | 627 | } |
94fc1dd4 SH |
628 | if (bt) |
629 | free(bt); | |
274a4a44 | 630 | } |
59a06a91 | 631 | } |
632 | #undef DUMP | |
59a06a91 | 633 | #undef LOC |
fb66b29c | 634 | #endif /* HAVE_STRACK_TRACE */ |
063ee52a | 635 | } |
636 | ||
637 | void | |
638 | zlog_backtrace(int priority) | |
639 | { | |
640 | #ifndef HAVE_GLIBC_BACKTRACE | |
bb85d700 | 641 | zlog(priority, "No backtrace available on this platform."); |
063ee52a | 642 | #else |
643 | void *array[20]; | |
644 | int size, i; | |
645 | char **strings; | |
646 | ||
4d474fa3 DL |
647 | size = backtrace(array, array_size(array)); |
648 | if (size <= 0 || (size_t)size > array_size(array)) | |
063ee52a | 649 | { |
650 | zlog_err("Cannot get backtrace, returned invalid # of frames %d " | |
1ed72e0b | 651 | "(valid range is between 1 and %lu)", |
837d16cc | 652 | size, (unsigned long)(array_size(array))); |
063ee52a | 653 | return; |
654 | } | |
bb85d700 | 655 | zlog(priority, "Backtrace for %d stack frames:", size); |
063ee52a | 656 | if (!(strings = backtrace_symbols(array, size))) |
657 | { | |
658 | zlog_err("Cannot get backtrace symbols (out of memory?)"); | |
659 | for (i = 0; i < size; i++) | |
bb85d700 | 660 | zlog(priority, "[bt %d] %p",i,array[i]); |
063ee52a | 661 | } |
662 | else | |
663 | { | |
664 | for (i = 0; i < size; i++) | |
bb85d700 | 665 | zlog(priority, "[bt %d] %s",i,strings[i]); |
063ee52a | 666 | free(strings); |
667 | } | |
668 | #endif /* HAVE_GLIBC_BACKTRACE */ | |
59a06a91 | 669 | } |
670 | ||
718e3744 | 671 | void |
bb85d700 | 672 | zlog (int priority, const char *format, ...) |
718e3744 | 673 | { |
d246bd96 | 674 | va_list args; |
718e3744 | 675 | |
d246bd96 | 676 | va_start(args, format); |
bb85d700 | 677 | vzlog (priority, format, args); |
d246bd96 | 678 | va_end (args); |
718e3744 | 679 | } |
680 | ||
d246bd96 | 681 | #define ZLOG_FUNC(FUNCNAME,PRIORITY) \ |
682 | void \ | |
683 | FUNCNAME(const char *format, ...) \ | |
684 | { \ | |
685 | va_list args; \ | |
686 | va_start(args, format); \ | |
bb85d700 | 687 | vzlog (PRIORITY, format, args); \ |
d246bd96 | 688 | va_end(args); \ |
718e3744 | 689 | } |
690 | ||
d246bd96 | 691 | ZLOG_FUNC(zlog_err, LOG_ERR) |
718e3744 | 692 | |
d246bd96 | 693 | ZLOG_FUNC(zlog_warn, LOG_WARNING) |
718e3744 | 694 | |
d246bd96 | 695 | ZLOG_FUNC(zlog_info, LOG_INFO) |
718e3744 | 696 | |
d246bd96 | 697 | ZLOG_FUNC(zlog_notice, LOG_NOTICE) |
718e3744 | 698 | |
d246bd96 | 699 | ZLOG_FUNC(zlog_debug, LOG_DEBUG) |
718e3744 | 700 | |
d246bd96 | 701 | #undef ZLOG_FUNC |
718e3744 | 702 | |
d1265948 DL |
703 | void zlog_thread_info (int log_level) |
704 | { | |
e0bebc7c QY |
705 | struct thread *tc; |
706 | tc = pthread_getspecific (thread_current); | |
707 | ||
708 | if (tc) | |
bb85d700 | 709 | zlog(log_level, "Current thread function %s, scheduled from " |
e0bebc7c QY |
710 | "file %s, line %u", tc->funcname, |
711 | tc->schedfrom, tc->schedfrom_line); | |
d1265948 | 712 | else |
bb85d700 | 713 | zlog(log_level, "Current thread not known/applicable"); |
d1265948 DL |
714 | } |
715 | ||
cee3df1e | 716 | void |
717 | _zlog_assert_failed (const char *assertion, const char *file, | |
718 | unsigned int line, const char *function) | |
719 | { | |
c4c7d0c4 | 720 | /* Force fallback file logging? */ |
721 | if (zlog_default && !zlog_default->fp && | |
722 | ((logfile_fd = open_crashlog()) >= 0) && | |
723 | ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL)) | |
724 | zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR; | |
bb85d700 | 725 | zlog(LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s", |
1e221354 | 726 | assertion,file,line,(function ? function : "?")); |
727 | zlog_backtrace(LOG_CRIT); | |
d1265948 | 728 | zlog_thread_info(LOG_CRIT); |
65efcfce | 729 | log_memstats_stderr ("log"); |
cee3df1e | 730 | abort(); |
731 | } | |
732 | ||
3b4cd783 DL |
733 | void |
734 | memory_oom (size_t size, const char *name) | |
735 | { | |
736 | zlog_err("out of memory: failed to allocate %zu bytes for %s" | |
737 | "object", size, name); | |
738 | zlog_backtrace(LOG_ERR); | |
739 | abort(); | |
740 | } | |
6b0655a2 | 741 | |
718e3744 | 742 | /* Open log stream */ |
bf1013e6 DL |
743 | void |
744 | openzlog (const char *progname, const char *protoname, u_short instance, | |
718e3744 | 745 | int syslog_flags, int syslog_facility) |
746 | { | |
747 | struct zlog *zl; | |
274a4a44 | 748 | u_int i; |
718e3744 | 749 | |
274a4a44 | 750 | zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog)); |
718e3744 | 751 | |
752 | zl->ident = progname; | |
bf1013e6 | 753 | zl->protoname = protoname; |
7c8ff89e | 754 | zl->instance = instance; |
718e3744 | 755 | zl->facility = syslog_facility; |
7d149b8e | 756 | zl->syslog_options = syslog_flags; |
718e3744 | 757 | |
274a4a44 | 758 | /* Set default logging levels. */ |
837d16cc | 759 | for (i = 0; i < array_size(zl->maxlvl); i++) |
274a4a44 | 760 | zl->maxlvl[i] = ZLOG_DISABLED; |
761 | zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG; | |
762 | zl->default_lvl = LOG_DEBUG; | |
763 | ||
718e3744 | 764 | openlog (progname, syslog_flags, zl->facility); |
bf1013e6 | 765 | zlog_default = zl; |
3f11a103 DL |
766 | |
767 | #ifdef HAVE_GLIBC_BACKTRACE | |
768 | /* work around backtrace() using lazily resolved dynamically linked | |
769 | * symbols, which will otherwise cause funny breakage in the SEGV handler. | |
770 | * (particularly, the dynamic linker can call malloc(), which uses locks | |
771 | * in programs linked with -pthread, thus can deadlock.) */ | |
772 | void *bt[4]; | |
773 | backtrace (bt, array_size(bt)); | |
774 | free (backtrace_symbols (bt, 0)); | |
775 | backtrace_symbols_fd (bt, 0, 0); | |
776 | #endif | |
718e3744 | 777 | } |
778 | ||
779 | void | |
bf1013e6 | 780 | closezlog (void) |
718e3744 | 781 | { |
bf1013e6 DL |
782 | struct zlog *zl = zlog_default; |
783 | ||
718e3744 | 784 | closelog(); |
228da428 CC |
785 | |
786 | if (zl->fp != NULL) | |
787 | fclose (zl->fp); | |
718e3744 | 788 | |
7e69d993 | 789 | if (zl->filename != NULL) |
6e919709 | 790 | XFREE(MTYPE_ZLOG, zl->filename); |
7e69d993 | 791 | |
718e3744 | 792 | XFREE (MTYPE_ZLOG, zl); |
bf1013e6 | 793 | zlog_default = NULL; |
718e3744 | 794 | } |
795 | ||
796 | /* Called from command.c. */ | |
797 | void | |
dd8376fe | 798 | zlog_set_level (zlog_dest_t dest, int log_level) |
718e3744 | 799 | { |
dd8376fe | 800 | zlog_default->maxlvl[dest] = log_level; |
718e3744 | 801 | } |
802 | ||
803 | int | |
dd8376fe | 804 | zlog_set_file (const char *filename, int log_level) |
718e3744 | 805 | { |
dd8376fe | 806 | struct zlog *zl = zlog_default; |
718e3744 | 807 | FILE *fp; |
aa593d5e | 808 | mode_t oldumask; |
718e3744 | 809 | |
810 | /* There is opend file. */ | |
dd8376fe | 811 | zlog_reset_file (); |
718e3744 | 812 | |
813 | /* Open file. */ | |
aa593d5e | 814 | oldumask = umask (0777 & ~LOGFILE_MASK); |
718e3744 | 815 | fp = fopen (filename, "a"); |
aa593d5e | 816 | umask(oldumask); |
274a4a44 | 817 | if (fp == NULL) |
818 | return 0; | |
718e3744 | 819 | |
820 | /* Set flags. */ | |
6e919709 | 821 | zl->filename = XSTRDUP(MTYPE_ZLOG, filename); |
274a4a44 | 822 | zl->maxlvl[ZLOG_DEST_FILE] = log_level; |
718e3744 | 823 | zl->fp = fp; |
c4c7d0c4 | 824 | logfile_fd = fileno(fp); |
718e3744 | 825 | |
826 | return 1; | |
827 | } | |
828 | ||
829 | /* Reset opend file. */ | |
830 | int | |
dd8376fe | 831 | zlog_reset_file (void) |
718e3744 | 832 | { |
dd8376fe | 833 | struct zlog *zl = zlog_default; |
718e3744 | 834 | |
718e3744 | 835 | if (zl->fp) |
836 | fclose (zl->fp); | |
837 | zl->fp = NULL; | |
c4c7d0c4 | 838 | logfile_fd = -1; |
274a4a44 | 839 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; |
718e3744 | 840 | |
841 | if (zl->filename) | |
6e919709 | 842 | XFREE(MTYPE_ZLOG, zl->filename); |
718e3744 | 843 | zl->filename = NULL; |
844 | ||
845 | return 1; | |
846 | } | |
847 | ||
848 | /* Reopen log file. */ | |
849 | int | |
dd8376fe | 850 | zlog_rotate (void) |
718e3744 | 851 | { |
dd8376fe | 852 | struct zlog *zl = zlog_default; |
274a4a44 | 853 | int level; |
718e3744 | 854 | |
718e3744 | 855 | if (zl->fp) |
856 | fclose (zl->fp); | |
857 | zl->fp = NULL; | |
c4c7d0c4 | 858 | logfile_fd = -1; |
274a4a44 | 859 | level = zl->maxlvl[ZLOG_DEST_FILE]; |
860 | zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED; | |
718e3744 | 861 | |
862 | if (zl->filename) | |
863 | { | |
aa593d5e | 864 | mode_t oldumask; |
274a4a44 | 865 | int save_errno; |
aa593d5e | 866 | |
867 | oldumask = umask (0777 & ~LOGFILE_MASK); | |
274a4a44 | 868 | zl->fp = fopen (zl->filename, "a"); |
869 | save_errno = errno; | |
870 | umask(oldumask); | |
871 | if (zl->fp == NULL) | |
aa593d5e | 872 | { |
274a4a44 | 873 | zlog_err("Log rotate failed: cannot open file %s for append: %s", |
874 | zl->filename, safe_strerror(save_errno)); | |
aa593d5e | 875 | return -1; |
876 | } | |
c4c7d0c4 | 877 | logfile_fd = fileno(zl->fp); |
274a4a44 | 878 | zl->maxlvl[ZLOG_DEST_FILE] = level; |
718e3744 | 879 | } |
880 | ||
881 | return 1; | |
882 | } | |
6b0655a2 | 883 | |
ca359769 | 884 | /* Wrapper around strerror to handle case where it returns NULL. */ |
885 | const char * | |
886 | safe_strerror(int errnum) | |
887 | { | |
888 | const char *s = strerror(errnum); | |
889 | return (s != NULL) ? s : "Unknown error"; | |
890 | } | |
f52d13cb | 891 | |
d6d672aa PJ |
892 | #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' } |
893 | static const struct zebra_desc_table command_types[] = { | |
894 | DESC_ENTRY (ZEBRA_INTERFACE_ADD), | |
895 | DESC_ENTRY (ZEBRA_INTERFACE_DELETE), | |
896 | DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD), | |
897 | DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE), | |
898 | DESC_ENTRY (ZEBRA_INTERFACE_UP), | |
899 | DESC_ENTRY (ZEBRA_INTERFACE_DOWN), | |
900 | DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD), | |
901 | DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE), | |
902 | DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD), | |
903 | DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE), | |
904 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD), | |
905 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE), | |
906 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD), | |
907 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE), | |
d6d672aa PJ |
908 | DESC_ENTRY (ZEBRA_ROUTER_ID_ADD), |
909 | DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE), | |
910 | DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE), | |
4c78376f | 911 | DESC_ENTRY (ZEBRA_HELLO), |
fb018d25 DS |
912 | DESC_ENTRY (ZEBRA_NEXTHOP_REGISTER), |
913 | DESC_ENTRY (ZEBRA_NEXTHOP_UNREGISTER), | |
914 | DESC_ENTRY (ZEBRA_NEXTHOP_UPDATE), | |
a7f6b2e2 | 915 | DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_ADD), |
916 | DESC_ENTRY (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE), | |
917 | DESC_ENTRY (ZEBRA_INTERFACE_BFD_DEST_UPDATE), | |
918 | DESC_ENTRY (ZEBRA_IMPORT_ROUTE_REGISTER), | |
919 | DESC_ENTRY (ZEBRA_IMPORT_ROUTE_UNREGISTER), | |
078430f6 | 920 | DESC_ENTRY (ZEBRA_IMPORT_CHECK_UPDATE), |
a7f6b2e2 | 921 | DESC_ENTRY (ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD), |
922 | DESC_ENTRY (ZEBRA_BFD_DEST_REGISTER), | |
923 | DESC_ENTRY (ZEBRA_BFD_DEST_DEREGISTER), | |
924 | DESC_ENTRY (ZEBRA_BFD_DEST_UPDATE), | |
925 | DESC_ENTRY (ZEBRA_BFD_DEST_REPLAY), | |
926 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_ADD), | |
927 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV4_DEL), | |
928 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_ADD), | |
c8e264b6 | 929 | DESC_ENTRY (ZEBRA_REDISTRIBUTE_IPV6_DEL), |
93f7342f DS |
930 | DESC_ENTRY (ZEBRA_VRF_UNREGISTER), |
931 | DESC_ENTRY (ZEBRA_VRF_ADD), | |
932 | DESC_ENTRY (ZEBRA_VRF_DELETE), | |
055c4dfc | 933 | DESC_ENTRY (ZEBRA_INTERFACE_VRF_UPDATE), |
4a04e5f7 | 934 | DESC_ENTRY (ZEBRA_BFD_CLIENT_REGISTER), |
935 | DESC_ENTRY (ZEBRA_INTERFACE_ENABLE_RADV), | |
93f7342f DS |
936 | DESC_ENTRY (ZEBRA_INTERFACE_DISABLE_RADV), |
937 | DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB), | |
40297824 | 938 | DESC_ENTRY (ZEBRA_INTERFACE_LINK_PARAMS), |
ce549947 RW |
939 | DESC_ENTRY (ZEBRA_MPLS_LABELS_ADD), |
940 | DESC_ENTRY (ZEBRA_MPLS_LABELS_DELETE), | |
65efcfce LB |
941 | DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_ADD), |
942 | DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_DELETE), | |
943 | DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_ADD), | |
944 | DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_DELETE), | |
e3be0432 | 945 | DESC_ENTRY (ZEBRA_IPMR_ROUTE_STATS), |
fea12efb | 946 | DESC_ENTRY (ZEBRA_LABEL_MANAGER_CONNECT), |
947 | DESC_ENTRY (ZEBRA_GET_LABEL_CHUNK), | |
948 | DESC_ENTRY (ZEBRA_RELEASE_LABEL_CHUNK), | |
13d60d35 | 949 | DESC_ENTRY (ZEBRA_ADVERTISE_ALL_VNI), |
950 | DESC_ENTRY (ZEBRA_VNI_ADD), | |
951 | DESC_ENTRY (ZEBRA_VNI_DEL), | |
952 | DESC_ENTRY (ZEBRA_REMOTE_VTEP_ADD), | |
953 | DESC_ENTRY (ZEBRA_REMOTE_VTEP_DEL), | |
2232a77c | 954 | DESC_ENTRY (ZEBRA_MACIP_ADD), |
955 | DESC_ENTRY (ZEBRA_MACIP_DEL), | |
956 | DESC_ENTRY (ZEBRA_REMOTE_MACIP_ADD), | |
957 | DESC_ENTRY (ZEBRA_REMOTE_MACIP_DEL), | |
d6d672aa PJ |
958 | }; |
959 | #undef DESC_ENTRY | |
960 | ||
961 | static const struct zebra_desc_table unknown = { 0, "unknown", '?' }; | |
962 | ||
963 | static const struct zebra_desc_table * | |
f52d13cb | 964 | zroute_lookup(u_int zroute) |
965 | { | |
f52d13cb | 966 | u_int i; |
967 | ||
837d16cc | 968 | if (zroute >= array_size(route_types)) |
f52d13cb | 969 | { |
970 | zlog_err("unknown zebra route type: %u", zroute); | |
971 | return &unknown; | |
972 | } | |
d6d672aa | 973 | if (zroute == route_types[zroute].type) |
f52d13cb | 974 | return &route_types[zroute]; |
837d16cc | 975 | for (i = 0; i < array_size(route_types); i++) |
f52d13cb | 976 | { |
d6d672aa | 977 | if (zroute == route_types[i].type) |
f52d13cb | 978 | { |
979 | zlog_warn("internal error: route type table out of order " | |
980 | "while searching for %u, please notify developers", zroute); | |
981 | return &route_types[i]; | |
982 | } | |
983 | } | |
984 | zlog_err("internal error: cannot find route type %u in table!", zroute); | |
985 | return &unknown; | |
986 | } | |
987 | ||
988 | const char * | |
989 | zebra_route_string(u_int zroute) | |
990 | { | |
991 | return zroute_lookup(zroute)->string; | |
992 | } | |
993 | ||
994 | char | |
995 | zebra_route_char(u_int zroute) | |
996 | { | |
997 | return zroute_lookup(zroute)->chr; | |
998 | } | |
d6d672aa PJ |
999 | |
1000 | const char * | |
1001 | zserv_command_string (unsigned int command) | |
1002 | { | |
837d16cc | 1003 | if (command >= array_size(command_types)) |
d6d672aa PJ |
1004 | { |
1005 | zlog_err ("unknown zserv command type: %u", command); | |
1006 | return unknown.string; | |
1007 | } | |
1008 | return command_types[command].string; | |
1009 | } | |
7514fb77 | 1010 | |
7514fb77 PJ |
1011 | int |
1012 | proto_name2num(const char *s) | |
1013 | { | |
1014 | unsigned i; | |
1015 | ||
837d16cc | 1016 | for (i=0; i<array_size(route_types); ++i) |
7514fb77 PJ |
1017 | if (strcasecmp(s, route_types[i].string) == 0) |
1018 | return route_types[i].type; | |
1019 | return -1; | |
1020 | } | |
e0ca5fde | 1021 | |
e0ca5fde DL |
1022 | int |
1023 | proto_redistnum(int afi, const char *s) | |
1024 | { | |
1025 | if (! s) | |
1026 | return -1; | |
1027 | ||
1028 | if (afi == AFI_IP) | |
1029 | { | |
6d681bd8 | 1030 | if (strmatch (s, "kernel")) |
e0ca5fde | 1031 | return ZEBRA_ROUTE_KERNEL; |
6d681bd8 | 1032 | else if (strmatch (s, "connected")) |
e0ca5fde | 1033 | return ZEBRA_ROUTE_CONNECT; |
6d681bd8 | 1034 | else if (strmatch (s, "static")) |
e0ca5fde | 1035 | return ZEBRA_ROUTE_STATIC; |
6d681bd8 | 1036 | else if (strmatch (s, "rip")) |
e0ca5fde | 1037 | return ZEBRA_ROUTE_RIP; |
7f57883e DS |
1038 | else if (strmatch (s, "eigrp")) |
1039 | return ZEBRA_ROUTE_EIGRP; | |
6d681bd8 | 1040 | else if (strmatch (s, "ospf")) |
e0ca5fde | 1041 | return ZEBRA_ROUTE_OSPF; |
6d681bd8 | 1042 | else if (strmatch (s, "isis")) |
e0ca5fde | 1043 | return ZEBRA_ROUTE_ISIS; |
6d681bd8 | 1044 | else if (strmatch (s, "bgp")) |
e0ca5fde | 1045 | return ZEBRA_ROUTE_BGP; |
6d681bd8 | 1046 | else if (strmatch (s, "table")) |
7a4bb9c5 | 1047 | return ZEBRA_ROUTE_TABLE; |
6d681bd8 | 1048 | else if (strmatch (s, "vnc")) |
65efcfce | 1049 | return ZEBRA_ROUTE_VNC; |
b84aadae | 1050 | else if (strmatch (s, "vnc-direct")) |
65efcfce | 1051 | return ZEBRA_ROUTE_VNC_DIRECT; |
f257ff2c | 1052 | else if (strmatch (s, "nhrp")) |
2fb975da | 1053 | return ZEBRA_ROUTE_NHRP; |
2579b2c1 DS |
1054 | else if (strmatch (s, "babel")) |
1055 | return ZEBRA_ROUTE_BABEL; | |
e0ca5fde DL |
1056 | } |
1057 | if (afi == AFI_IP6) | |
1058 | { | |
6d681bd8 | 1059 | if (strmatch (s, "kernel")) |
e0ca5fde | 1060 | return ZEBRA_ROUTE_KERNEL; |
6d681bd8 | 1061 | else if (strmatch (s, "connected")) |
e0ca5fde | 1062 | return ZEBRA_ROUTE_CONNECT; |
6d681bd8 | 1063 | else if (strmatch (s, "static")) |
e0ca5fde | 1064 | return ZEBRA_ROUTE_STATIC; |
6d681bd8 | 1065 | else if (strmatch (s, "ripng")) |
e0ca5fde | 1066 | return ZEBRA_ROUTE_RIPNG; |
6d681bd8 | 1067 | else if (strmatch (s, "ospf6")) |
e0ca5fde | 1068 | return ZEBRA_ROUTE_OSPF6; |
6d681bd8 | 1069 | else if (strmatch (s, "isis")) |
e0ca5fde | 1070 | return ZEBRA_ROUTE_ISIS; |
6d681bd8 | 1071 | else if (strmatch (s, "bgp")) |
e0ca5fde | 1072 | return ZEBRA_ROUTE_BGP; |
6d681bd8 | 1073 | else if (strmatch (s, "table")) |
7a4bb9c5 | 1074 | return ZEBRA_ROUTE_TABLE; |
6d681bd8 | 1075 | else if (strmatch (s, "vnc")) |
65efcfce | 1076 | return ZEBRA_ROUTE_VNC; |
b84aadae | 1077 | else if (strmatch (s, "vnc-direct")) |
65efcfce | 1078 | return ZEBRA_ROUTE_VNC_DIRECT; |
f257ff2c | 1079 | else if (strmatch (s, "nhrp")) |
2fb975da | 1080 | return ZEBRA_ROUTE_NHRP; |
2579b2c1 DS |
1081 | else if (strmatch (s, "babel")) |
1082 | return ZEBRA_ROUTE_BABEL; | |
e0ca5fde DL |
1083 | } |
1084 | return -1; | |
1085 | } | |
99a6c6cd DW |
1086 | |
1087 | void | |
a9b5cbe5 | 1088 | zlog_hexdump (const void *mem, unsigned int len) { |
99a6c6cd DW |
1089 | unsigned long i = 0; |
1090 | unsigned int j = 0; | |
1091 | unsigned int columns = 8; | |
1092 | char buf[(len * 4) + ((len/4) * 20) + 30]; | |
1093 | char *s = buf; | |
1094 | ||
1095 | for (i = 0; i < len + ((len % columns) ? (columns - len % columns) : 0); i++) | |
1096 | { | |
1097 | /* print offset */ | |
1098 | if (i % columns == 0) | |
1099 | s += sprintf(s, "0x%016lx: ", (unsigned long)mem + i); | |
1100 | ||
1101 | /* print hex data */ | |
1102 | if (i < len) | |
a9b5cbe5 | 1103 | s += sprintf(s, "%02x ", 0xFF & ((const char*)mem)[i]); |
99a6c6cd DW |
1104 | |
1105 | /* end of block, just aligning for ASCII dump */ | |
1106 | else | |
1107 | s += sprintf(s, " "); | |
1108 | ||
1109 | /* print ASCII dump */ | |
1110 | if (i % columns == (columns - 1)) | |
1111 | { | |
1112 | for (j = i - (columns - 1); j <= i; j++) | |
1113 | { | |
1114 | if (j >= len) /* end of block, not really printing */ | |
1115 | s += sprintf(s, " "); | |
1116 | ||
a9b5cbe5 DS |
1117 | else if(isprint((int)((const char *)mem)[j])) /* printable char */ |
1118 | s += sprintf(s, "%c", 0xFF & ((const char *)mem)[j]); | |
99a6c6cd DW |
1119 | |
1120 | else /* other char */ | |
1121 | s += sprintf(s, "."); | |
1122 | } | |
1123 | s += sprintf(s, "\n"); | |
1124 | } | |
1125 | } | |
1126 | zlog_debug("\n%s", buf); | |
1127 | } | |
1f806fc2 DL |
1128 | |
1129 | const char * | |
1130 | zlog_sanitize (char *buf, size_t bufsz, const void *in, size_t inlen) | |
1131 | { | |
1132 | const char *inbuf = in; | |
1133 | char *pos = buf, *end = buf + bufsz; | |
1134 | const char *iend = inbuf + inlen; | |
1135 | ||
1136 | memset (buf, 0, bufsz); | |
1137 | for (; inbuf < iend; inbuf++) | |
1138 | { | |
1139 | /* don't write partial escape sequence */ | |
1140 | if (end - pos < 5) | |
1141 | break; | |
1142 | ||
1143 | if (*inbuf == '\n') | |
1144 | snprintf (pos, end - pos, "\\n"); | |
1145 | else if (*inbuf == '\r') | |
1146 | snprintf (pos, end - pos, "\\r"); | |
1147 | else if (*inbuf == '\t') | |
1148 | snprintf (pos, end - pos, "\\t"); | |
1149 | else if (*inbuf < ' ' || *inbuf == '"' || *inbuf >= 127) | |
1150 | snprintf (pos, end - pos, "\\x%02hhx", *inbuf); | |
1151 | else | |
1152 | *pos = *inbuf; | |
1153 | ||
1154 | pos += strlen (pos); | |
1155 | } | |
1156 | return buf; | |
1157 | } |