log.c 22 KB

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