log.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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[20];
  398. int size;
  399. char buf[100];
  400. char *s;
  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. /* Just print the function addresses. */
  444. for (i = 0; i < size; i++)
  445. {
  446. s = buf;
  447. s = str_append(LOC,"[bt ");
  448. s = num_append(LOC,i);
  449. s = str_append(LOC,"] 0x");
  450. s = hex_append(LOC,(u_long)(array[i]));
  451. *s = '\0';
  452. if (priority <= zlog_default->maxlvl[ZLOG_DEST_MONITOR])
  453. vty_log_fixed(buf,s-buf);
  454. if (priority <= zlog_default->maxlvl[ZLOG_DEST_SYSLOG])
  455. syslog_sigsafe(priority|zlog_default->facility,buf,s-buf);
  456. }
  457. }
  458. }
  459. #undef DUMP
  460. #undef LOC
  461. #endif /* HAVE_STRACK_TRACE */
  462. }
  463. void
  464. zlog_backtrace(int priority)
  465. {
  466. #ifndef HAVE_GLIBC_BACKTRACE
  467. zlog(NULL, priority, "No backtrace available on this platform.");
  468. #else
  469. void *array[20];
  470. int size, i;
  471. char **strings;
  472. if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) ||
  473. ((size_t)size > sizeof(array)/sizeof(array[0])))
  474. {
  475. zlog_err("Cannot get backtrace, returned invalid # of frames %d "
  476. "(valid range is between 1 and %lu)",
  477. size, (unsigned long)(sizeof(array)/sizeof(array[0])));
  478. return;
  479. }
  480. zlog(NULL, priority, "Backtrace for %d stack frames:", size);
  481. if (!(strings = backtrace_symbols(array, size)))
  482. {
  483. zlog_err("Cannot get backtrace symbols (out of memory?)");
  484. for (i = 0; i < size; i++)
  485. zlog(NULL, priority, "[bt %d] %p",i,array[i]);
  486. }
  487. else
  488. {
  489. for (i = 0; i < size; i++)
  490. zlog(NULL, priority, "[bt %d] %s",i,strings[i]);
  491. free(strings);
  492. }
  493. #endif /* HAVE_GLIBC_BACKTRACE */
  494. }
  495. void
  496. zlog (struct zlog *zl, int priority, const char *format, ...)
  497. {
  498. va_list args;
  499. va_start(args, format);
  500. vzlog (zl, priority, format, args);
  501. va_end (args);
  502. }
  503. #define ZLOG_FUNC(FUNCNAME,PRIORITY) \
  504. void \
  505. FUNCNAME(const char *format, ...) \
  506. { \
  507. va_list args; \
  508. va_start(args, format); \
  509. vzlog (NULL, PRIORITY, format, args); \
  510. va_end(args); \
  511. }
  512. ZLOG_FUNC(zlog_err, LOG_ERR)
  513. ZLOG_FUNC(zlog_warn, LOG_WARNING)
  514. ZLOG_FUNC(zlog_info, LOG_INFO)
  515. ZLOG_FUNC(zlog_notice, LOG_NOTICE)
  516. ZLOG_FUNC(zlog_debug, LOG_DEBUG)
  517. #undef ZLOG_FUNC
  518. #define PLOG_FUNC(FUNCNAME,PRIORITY) \
  519. void \
  520. FUNCNAME(struct zlog *zl, const char *format, ...) \
  521. { \
  522. va_list args; \
  523. va_start(args, format); \
  524. vzlog (zl, PRIORITY, format, args); \
  525. va_end(args); \
  526. }
  527. PLOG_FUNC(plog_err, LOG_ERR)
  528. PLOG_FUNC(plog_warn, LOG_WARNING)
  529. PLOG_FUNC(plog_info, LOG_INFO)
  530. PLOG_FUNC(plog_notice, LOG_NOTICE)
  531. PLOG_FUNC(plog_debug, LOG_DEBUG)
  532. #undef PLOG_FUNC
  533. void
  534. _zlog_assert_failed (const char *assertion, const char *file,
  535. unsigned int line, const char *function)
  536. {
  537. /* Force fallback file logging? */
  538. if (zlog_default && !zlog_default->fp &&
  539. ((logfile_fd = open_crashlog()) >= 0) &&
  540. ((zlog_default->fp = fdopen(logfile_fd, "w")) != NULL))
  541. zlog_default->maxlvl[ZLOG_DEST_FILE] = LOG_ERR;
  542. zlog(NULL, LOG_CRIT, "Assertion `%s' failed in file %s, line %u, function %s",
  543. assertion,file,line,(function ? function : "?"));
  544. zlog_backtrace(LOG_CRIT);
  545. abort();
  546. }
  547. /* Open log stream */
  548. struct zlog *
  549. openzlog (const char *progname, zlog_proto_t protocol,
  550. int syslog_flags, int syslog_facility)
  551. {
  552. struct zlog *zl;
  553. u_int i;
  554. zl = XCALLOC(MTYPE_ZLOG, sizeof (struct zlog));
  555. zl->ident = progname;
  556. zl->protocol = protocol;
  557. zl->facility = syslog_facility;
  558. zl->syslog_options = syslog_flags;
  559. /* Set default logging levels. */
  560. for (i = 0; i < sizeof(zl->maxlvl)/sizeof(zl->maxlvl[0]); i++)
  561. zl->maxlvl[i] = ZLOG_DISABLED;
  562. zl->maxlvl[ZLOG_DEST_MONITOR] = LOG_DEBUG;
  563. zl->default_lvl = LOG_DEBUG;
  564. openlog (progname, syslog_flags, zl->facility);
  565. return zl;
  566. }
  567. void
  568. closezlog (struct zlog *zl)
  569. {
  570. closelog();
  571. fclose (zl->fp);
  572. XFREE (MTYPE_ZLOG, zl);
  573. }
  574. /* Called from command.c. */
  575. void
  576. zlog_set_level (struct zlog *zl, zlog_dest_t dest, int log_level)
  577. {
  578. if (zl == NULL)
  579. zl = zlog_default;
  580. zl->maxlvl[dest] = log_level;
  581. }
  582. int
  583. zlog_set_file (struct zlog *zl, const char *filename, int log_level)
  584. {
  585. FILE *fp;
  586. mode_t oldumask;
  587. /* There is opend file. */
  588. zlog_reset_file (zl);
  589. /* Set default zl. */
  590. if (zl == NULL)
  591. zl = zlog_default;
  592. /* Open file. */
  593. oldumask = umask (0777 & ~LOGFILE_MASK);
  594. fp = fopen (filename, "a");
  595. umask(oldumask);
  596. if (fp == NULL)
  597. return 0;
  598. /* Set flags. */
  599. zl->filename = strdup (filename);
  600. zl->maxlvl[ZLOG_DEST_FILE] = log_level;
  601. zl->fp = fp;
  602. logfile_fd = fileno(fp);
  603. return 1;
  604. }
  605. /* Reset opend file. */
  606. int
  607. zlog_reset_file (struct zlog *zl)
  608. {
  609. if (zl == NULL)
  610. zl = zlog_default;
  611. if (zl->fp)
  612. fclose (zl->fp);
  613. zl->fp = NULL;
  614. logfile_fd = -1;
  615. zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
  616. if (zl->filename)
  617. free (zl->filename);
  618. zl->filename = NULL;
  619. return 1;
  620. }
  621. /* Reopen log file. */
  622. int
  623. zlog_rotate (struct zlog *zl)
  624. {
  625. int level;
  626. if (zl == NULL)
  627. zl = zlog_default;
  628. if (zl->fp)
  629. fclose (zl->fp);
  630. zl->fp = NULL;
  631. logfile_fd = -1;
  632. level = zl->maxlvl[ZLOG_DEST_FILE];
  633. zl->maxlvl[ZLOG_DEST_FILE] = ZLOG_DISABLED;
  634. if (zl->filename)
  635. {
  636. mode_t oldumask;
  637. int save_errno;
  638. oldumask = umask (0777 & ~LOGFILE_MASK);
  639. zl->fp = fopen (zl->filename, "a");
  640. save_errno = errno;
  641. umask(oldumask);
  642. if (zl->fp == NULL)
  643. {
  644. zlog_err("Log rotate failed: cannot open file %s for append: %s",
  645. zl->filename, safe_strerror(save_errno));
  646. return -1;
  647. }
  648. logfile_fd = fileno(zl->fp);
  649. zl->maxlvl[ZLOG_DEST_FILE] = level;
  650. }
  651. return 1;
  652. }
  653. /* Message lookup function. */
  654. const char *
  655. lookup (const struct message *mes, int key)
  656. {
  657. const struct message *pnt;
  658. for (pnt = mes; pnt->key != 0; pnt++)
  659. if (pnt->key == key)
  660. return pnt->str;
  661. return "";
  662. }
  663. /* Older/faster version of message lookup function, but requires caller to pass
  664. * in the array size (instead of relying on a 0 key to terminate the search).
  665. *
  666. * The return value is the message string if found, or the 'none' pointer
  667. * provided otherwise.
  668. */
  669. const char *
  670. mes_lookup (struct message *meslist, int max, int index, const char *none)
  671. {
  672. int pos = index - meslist[0].key;
  673. /* first check for best case: index is in range and matches the key
  674. * value in that slot.
  675. * NB: key numbering might be offset from 0. E.g. protocol constants
  676. * often start at 1.
  677. */
  678. if ((pos >= 0) && (pos < max)
  679. && (meslist[pos].key == index))
  680. return meslist[pos].str;
  681. /* fall back to linear search */
  682. {
  683. int i;
  684. for (i = 0; i < max; i++, meslist++)
  685. {
  686. if (meslist->key == index)
  687. {
  688. const char *str = (meslist->str ? meslist->str : none);
  689. zlog_debug ("message index %d [%s] found in position %d (max is %d)",
  690. index, str, i, max);
  691. return str;
  692. }
  693. }
  694. }
  695. zlog_err("message index %d not found (max is %d)", index, max);
  696. assert (none);
  697. return none;
  698. }
  699. /* Wrapper around strerror to handle case where it returns NULL. */
  700. const char *
  701. safe_strerror(int errnum)
  702. {
  703. const char *s = strerror(errnum);
  704. return (s != NULL) ? s : "Unknown error";
  705. }
  706. struct zebra_desc_table
  707. {
  708. unsigned int type;
  709. const char *string;
  710. char chr;
  711. };
  712. #define DESC_ENTRY(T,S,C) [(T)] = { (T), (S), (C) }
  713. static const struct zebra_desc_table route_types[] = {
  714. DESC_ENTRY (ZEBRA_ROUTE_SYSTEM, "system", 'X' ),
  715. DESC_ENTRY (ZEBRA_ROUTE_KERNEL, "kernel", 'K' ),
  716. DESC_ENTRY (ZEBRA_ROUTE_CONNECT, "connected", 'C' ),
  717. DESC_ENTRY (ZEBRA_ROUTE_STATIC, "static", 'S' ),
  718. DESC_ENTRY (ZEBRA_ROUTE_RIP, "rip", 'R' ),
  719. DESC_ENTRY (ZEBRA_ROUTE_RIPNG, "ripng", 'R' ),
  720. DESC_ENTRY (ZEBRA_ROUTE_OSPF, "ospf", 'O' ),
  721. DESC_ENTRY (ZEBRA_ROUTE_OSPF6, "ospf6", 'O' ),
  722. DESC_ENTRY (ZEBRA_ROUTE_ISIS, "isis", 'I' ),
  723. DESC_ENTRY (ZEBRA_ROUTE_BGP, "bgp", 'B' ),
  724. DESC_ENTRY (ZEBRA_ROUTE_HSLS, "hsls", 'H' ),
  725. };
  726. #undef DESC_ENTRY
  727. #define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' }
  728. static const struct zebra_desc_table command_types[] = {
  729. DESC_ENTRY (ZEBRA_INTERFACE_ADD),
  730. DESC_ENTRY (ZEBRA_INTERFACE_DELETE),
  731. DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD),
  732. DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE),
  733. DESC_ENTRY (ZEBRA_INTERFACE_UP),
  734. DESC_ENTRY (ZEBRA_INTERFACE_DOWN),
  735. DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD),
  736. DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE),
  737. DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD),
  738. DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE),
  739. DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD),
  740. DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE),
  741. DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD),
  742. DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE),
  743. DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP),
  744. DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP),
  745. DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP),
  746. DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP),
  747. DESC_ENTRY (ZEBRA_INTERFACE_RENAME),
  748. DESC_ENTRY (ZEBRA_ROUTER_ID_ADD),
  749. DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE),
  750. DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE),
  751. };
  752. #undef DESC_ENTRY
  753. static const struct zebra_desc_table unknown = { 0, "unknown", '?' };
  754. static const struct zebra_desc_table *
  755. zroute_lookup(u_int zroute)
  756. {
  757. u_int i;
  758. if (zroute >= sizeof(route_types)/sizeof(route_types[0]))
  759. {
  760. zlog_err("unknown zebra route type: %u", zroute);
  761. return &unknown;
  762. }
  763. if (zroute == route_types[zroute].type)
  764. return &route_types[zroute];
  765. for (i = 0; i < sizeof(route_types)/sizeof(route_types[0]); i++)
  766. {
  767. if (zroute == route_types[i].type)
  768. {
  769. zlog_warn("internal error: route type table out of order "
  770. "while searching for %u, please notify developers", zroute);
  771. return &route_types[i];
  772. }
  773. }
  774. zlog_err("internal error: cannot find route type %u in table!", zroute);
  775. return &unknown;
  776. }
  777. const char *
  778. zebra_route_string(u_int zroute)
  779. {
  780. return zroute_lookup(zroute)->string;
  781. }
  782. char
  783. zebra_route_char(u_int zroute)
  784. {
  785. return zroute_lookup(zroute)->chr;
  786. }
  787. const char *
  788. zserv_command_string (unsigned int command)
  789. {
  790. if (command >= sizeof(command_types)/sizeof(command_types[0]))
  791. {
  792. zlog_err ("unknown zserv command type: %u", command);
  793. return unknown.string;
  794. }
  795. return command_types[command].string;
  796. }
  797. #define RTSIZE (sizeof(route_types)/sizeof(route_types[0]))
  798. int
  799. proto_name2num(const char *s)
  800. {
  801. unsigned i;
  802. for (i=0; i<RTSIZE; ++i)
  803. if (strcasecmp(s, route_types[i].string) == 0)
  804. return route_types[i].type;
  805. return -1;
  806. }
  807. #undef RTSIZE