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