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