log.c 22 KB

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