log.c 24 KB

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