compat_libevent.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /* Copyright (c) 2009-2011, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file compat_libevent.c
  5. * \brief Wrappers to handle porting between different versions of libevent.
  6. *
  7. * In an ideal world, we'd just use Libevent 2.0 from now on. But as of June
  8. * 2009, Libevent 2.0 is still in alpha, and we will have old versions of
  9. * Libevent for the forseeable future.
  10. **/
  11. #include "orconfig.h"
  12. #include "compat.h"
  13. #include "compat_libevent.h"
  14. #include "util.h"
  15. #include "torlog.h"
  16. #ifdef HAVE_EVENT2_EVENT_H
  17. #include <event2/event.h>
  18. #else
  19. #include <event.h>
  20. #endif
  21. /** A number representing a version of Libevent.
  22. This is a 4-byte number, with the first three bytes representing the
  23. major, minor, and patchlevel respectively of the library. The fourth
  24. byte is unused.
  25. This is equivalent to the format of LIBEVENT_VERSION_NUMBER on Libevent
  26. 2.0.1 or later. For versions of Libevent before 1.4.0, which followed the
  27. format of "1.0, 1.0a, 1.0b", we define 1.0 to be equivalent to 1.0.0, 1.0a
  28. to be equivalent to 1.0.1, and so on.
  29. */
  30. typedef uint32_t le_version_t;
  31. /** @{ */
  32. /** Macros: returns the number of a libevent version as a le_version_t */
  33. #define V(major, minor, patch) \
  34. (((major) << 24) | ((minor) << 16) | ((patch) << 8))
  35. #define V_OLD(major, minor, patch) \
  36. V((major), (minor), (patch)-'a'+1)
  37. /** @} */
  38. /** Represetns a version of libevent so old we can't figure out what version
  39. * it is. */
  40. #define LE_OLD V(0,0,0)
  41. /** Represents a version of libevent so weird we can't figure out what version
  42. * it it. */
  43. #define LE_OTHER V(0,0,99)
  44. static le_version_t tor_get_libevent_version(const char **v_out);
  45. #ifdef HAVE_EVENT_SET_LOG_CALLBACK
  46. /** A string which, if it appears in a libevent log, should be ignored. */
  47. static const char *suppress_msg = NULL;
  48. /** Callback function passed to event_set_log() so we can intercept
  49. * log messages from libevent. */
  50. static void
  51. libevent_logging_callback(int severity, const char *msg)
  52. {
  53. char buf[1024];
  54. size_t n;
  55. if (suppress_msg && strstr(msg, suppress_msg))
  56. return;
  57. n = strlcpy(buf, msg, sizeof(buf));
  58. if (n && n < sizeof(buf) && buf[n-1] == '\n') {
  59. buf[n-1] = '\0';
  60. }
  61. switch (severity) {
  62. case _EVENT_LOG_DEBUG:
  63. log(LOG_DEBUG, LD_NOCB|LD_NET, "Message from libevent: %s", buf);
  64. break;
  65. case _EVENT_LOG_MSG:
  66. log(LOG_INFO, LD_NOCB|LD_NET, "Message from libevent: %s", buf);
  67. break;
  68. case _EVENT_LOG_WARN:
  69. log(LOG_WARN, LD_NOCB|LD_GENERAL, "Warning from libevent: %s", buf);
  70. break;
  71. case _EVENT_LOG_ERR:
  72. log(LOG_ERR, LD_NOCB|LD_GENERAL, "Error from libevent: %s", buf);
  73. break;
  74. default:
  75. log(LOG_WARN, LD_NOCB|LD_GENERAL, "Message [%d] from libevent: %s",
  76. severity, buf);
  77. break;
  78. }
  79. }
  80. /** Set hook to intercept log messages from libevent. */
  81. void
  82. configure_libevent_logging(void)
  83. {
  84. event_set_log_callback(libevent_logging_callback);
  85. }
  86. /** Ignore any libevent log message that contains <b>msg</b>. */
  87. void
  88. suppress_libevent_log_msg(const char *msg)
  89. {
  90. suppress_msg = msg;
  91. }
  92. #else
  93. void
  94. configure_libevent_logging(void)
  95. {
  96. }
  97. void
  98. suppress_libevent_log_msg(const char *msg)
  99. {
  100. (void)msg;
  101. }
  102. #endif
  103. #ifndef HAVE_EVENT2_EVENT_H
  104. /** Work-alike replacement for event_new() on pre-Libevent-2.0 systems. */
  105. struct event *
  106. tor_event_new(struct event_base *base, int sock, short what,
  107. void (*cb)(int, short, void *), void *arg)
  108. {
  109. struct event *e = tor_malloc_zero(sizeof(struct event));
  110. event_set(e, sock, what, cb, arg);
  111. if (! base)
  112. base = tor_libevent_get_base();
  113. event_base_set(base, e);
  114. return e;
  115. }
  116. /** Work-alike replacement for evtimer_new() on pre-Libevent-2.0 systems. */
  117. struct event *
  118. tor_evtimer_new(struct event_base *base,
  119. void (*cb)(int, short, void *), void *arg)
  120. {
  121. return tor_event_new(base, -1, 0, cb, arg);
  122. }
  123. /** Work-alike replacement for evsignal_new() on pre-Libevent-2.0 systems. */
  124. struct event *
  125. tor_evsignal_new(struct event_base * base, int sig,
  126. void (*cb)(int, short, void *), void *arg)
  127. {
  128. return tor_event_new(base, sig, EV_SIGNAL|EV_PERSIST, cb, arg);
  129. }
  130. /** Work-alike replacement for event_free() on pre-Libevent-2.0 systems. */
  131. void
  132. tor_event_free(struct event *ev)
  133. {
  134. event_del(ev);
  135. tor_free(ev);
  136. }
  137. #endif
  138. /** Global event base for use by the main thread. */
  139. struct event_base *the_event_base = NULL;
  140. /* This is what passes for version detection on OSX. We set
  141. * MACOSX_KQUEUE_IS_BROKEN to true iff we're on a version of OSX before
  142. * 10.4.0 (aka 1040). */
  143. #ifdef __APPLE__
  144. #ifdef __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
  145. #define MACOSX_KQUEUE_IS_BROKEN \
  146. (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1040)
  147. #else
  148. #define MACOSX_KQUEUE_IS_BROKEN 0
  149. #endif
  150. #endif
  151. /** Initialize the Libevent library and set up the event base. */
  152. void
  153. tor_libevent_initialize(void)
  154. {
  155. tor_assert(the_event_base == NULL);
  156. #ifdef __APPLE__
  157. if (MACOSX_KQUEUE_IS_BROKEN ||
  158. tor_get_libevent_version(NULL) < V_OLD(1,1,'b')) {
  159. setenv("EVENT_NOKQUEUE","1",1);
  160. }
  161. #endif
  162. #ifdef HAVE_EVENT2_EVENT_H
  163. the_event_base = event_base_new();
  164. #else
  165. the_event_base = event_init();
  166. #endif
  167. #if defined(HAVE_EVENT_GET_VERSION) && defined(HAVE_EVENT_GET_METHOD)
  168. /* Making this a NOTICE for now so we can link bugs to a libevent versions
  169. * or methods better. */
  170. log(LOG_NOTICE, LD_GENERAL,
  171. "Initialized libevent version %s using method %s. Good.",
  172. event_get_version(), tor_libevent_get_method());
  173. #else
  174. log(LOG_NOTICE, LD_GENERAL,
  175. "Initialized old libevent (version 1.0b or earlier).");
  176. log(LOG_WARN, LD_GENERAL,
  177. "You have a *VERY* old version of libevent. It is likely to be buggy; "
  178. "please build Tor with a more recent version.");
  179. #endif
  180. }
  181. /** Return the current Libevent event base that we're set up to use. */
  182. struct event_base *
  183. tor_libevent_get_base(void)
  184. {
  185. return the_event_base;
  186. }
  187. #ifndef HAVE_EVENT_BASE_LOOPEXIT
  188. /** Replacement for event_base_loopexit on some very old versions of Libevent
  189. * that we are not yet brave enough to deprecate. */
  190. int
  191. tor_event_base_loopexit(struct event_base *base, struct timeval *tv)
  192. {
  193. tor_assert(base == the_event_base);
  194. return event_loopexit(tv);
  195. }
  196. #endif
  197. /** Return the name of the Libevent backend we're using. */
  198. const char *
  199. tor_libevent_get_method(void)
  200. {
  201. #ifdef HAVE_EVENT2_EVENT_H
  202. return event_base_get_method(the_event_base);
  203. #elif defined(HAVE_EVENT_GET_METHOD)
  204. return event_get_method();
  205. #else
  206. return "<unknown>";
  207. #endif
  208. }
  209. /** Return the le_version_t for the current version of libevent. If the
  210. * version is very new, return LE_OTHER. If the version is so old that it
  211. * doesn't support event_get_version(), return LE_OLD. DOCDOC */
  212. static le_version_t
  213. tor_decode_libevent_version(const char *v)
  214. {
  215. unsigned major, minor, patchlevel;
  216. char c, e, extra;
  217. int fields;
  218. /* Try the new preferred "1.4.11-stable" format.
  219. * Also accept "1.4.14b-stable". */
  220. fields = sscanf(v, "%u.%u.%u%c%c", &major, &minor, &patchlevel, &c, &e);
  221. if (fields == 3 ||
  222. ((fields == 4 || fields == 5 ) && (c == '-' || c == '_')) ||
  223. (fields == 5 && TOR_ISALPHA(c) && (e == '-' || e == '_'))) {
  224. return V(major,minor,patchlevel);
  225. }
  226. /* Try the old "1.3e" format. */
  227. fields = sscanf(v, "%u.%u%c%c", &major, &minor, &c, &extra);
  228. if (fields == 3 && TOR_ISALPHA(c)) {
  229. return V_OLD(major, minor, c);
  230. } else if (fields == 2) {
  231. return V(major, minor, 0);
  232. }
  233. return LE_OTHER;
  234. }
  235. /** Return an integer representing the binary interface of a Libevent library.
  236. * Two different versions with different numbers are sure not to be binary
  237. * compatible. Two different versions with the same numbers have a decent
  238. * chance of binary compatibility.*/
  239. static int
  240. le_versions_compatibility(le_version_t v)
  241. {
  242. if (v == LE_OTHER)
  243. return 0;
  244. if (v < V_OLD(1,0,'c'))
  245. return 1;
  246. else if (v < V(1,4,0))
  247. return 2;
  248. else if (v < V(1,4,99))
  249. return 3;
  250. else if (v < V(2,0,1))
  251. return 4;
  252. else /* Everything 2.0 and later should be compatible. */
  253. return 5;
  254. }
  255. /** Return the version number of the currently running version of Libevent.
  256. See le_version_t for info on the format.
  257. */
  258. static le_version_t
  259. tor_get_libevent_version(const char **v_out)
  260. {
  261. const char *v;
  262. le_version_t r;
  263. #if defined(HAVE_EVENT_GET_VERSION_NUMBER)
  264. v = event_get_version();
  265. r = event_get_version_number();
  266. #elif defined (HAVE_EVENT_GET_VERSION)
  267. v = event_get_version();
  268. r = tor_decode_libevent_version(v);
  269. #else
  270. v = "pre-1.0c";
  271. r = LE_OLD;
  272. #endif
  273. if (v_out)
  274. *v_out = v;
  275. return r;
  276. }
  277. /** Return a string representation of the version of the currently running
  278. * version of Libevent. */
  279. const char *
  280. tor_libevent_get_version_str(void)
  281. {
  282. #ifdef HAVE_EVENT_GET_VERSION
  283. return event_get_version();
  284. #else
  285. return "pre-1.0c";
  286. #endif
  287. }
  288. /**
  289. * Compare the current Libevent method and version to a list of versions
  290. * which are known not to work. Warn the user as appropriate.
  291. */
  292. void
  293. tor_check_libevent_version(const char *m, int server,
  294. const char **badness_out)
  295. {
  296. int buggy = 0, iffy = 0, slow = 0, thread_unsafe = 0;
  297. le_version_t version;
  298. const char *v = NULL;
  299. const char *badness = NULL;
  300. const char *sad_os = "";
  301. version = tor_get_libevent_version(&v);
  302. /* XXX Would it be worthwhile disabling the methods that we know
  303. * are buggy, rather than just warning about them and then proceeding
  304. * to use them? If so, we should probably not wrap this whole thing
  305. * in HAVE_EVENT_GET_VERSION and HAVE_EVENT_GET_METHOD. -RD */
  306. /* XXXX The problem is that it's not trivial to get libevent to change it's
  307. * method once it's initialized, and it's not trivial to tell what method it
  308. * will use without initializing it. I guess we could preemptively disable
  309. * buggy libevent modes based on the version _before_ initializing it,
  310. * though, but then there's no good way (afaict) to warn "I would have used
  311. * kqueue, but instead I'm using select." -NM */
  312. /* XXXX022 revist the above; it is fixable now. */
  313. if (!strcmp(m, "kqueue")) {
  314. if (version < V_OLD(1,1,'b'))
  315. buggy = 1;
  316. } else if (!strcmp(m, "epoll")) {
  317. if (version < V(1,1,0))
  318. iffy = 1;
  319. } else if (!strcmp(m, "poll")) {
  320. if (version < V_OLD(1,0,'e'))
  321. buggy = 1;
  322. if (version < V(1,1,0))
  323. slow = 1;
  324. } else if (!strcmp(m, "select")) {
  325. if (version < V(1,1,0))
  326. slow = 1;
  327. } else if (!strcmp(m, "win32")) {
  328. if (version < V_OLD(1,1,'b'))
  329. buggy = 1;
  330. }
  331. /* Libevent versions before 1.3b do very badly on operating systems with
  332. * user-space threading implementations. */
  333. #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
  334. if (server && version < V_OLD(1,3,'b')) {
  335. thread_unsafe = 1;
  336. sad_os = "BSD variants";
  337. }
  338. #elif defined(__APPLE__) || defined(__darwin__)
  339. if (server && version < V_OLD(1,3,'b')) {
  340. thread_unsafe = 1;
  341. sad_os = "Mac OS X";
  342. }
  343. #endif
  344. if (thread_unsafe) {
  345. log(LOG_WARN, LD_GENERAL,
  346. "Libevent version %s often crashes when running a Tor server with %s. "
  347. "Please use the latest version of libevent (1.3b or later)",v,sad_os);
  348. badness = "BROKEN";
  349. } else if (buggy) {
  350. log(LOG_WARN, LD_GENERAL,
  351. "There are serious bugs in using %s with libevent %s. "
  352. "Please use the latest version of libevent.", m, v);
  353. badness = "BROKEN";
  354. } else if (iffy) {
  355. log(LOG_WARN, LD_GENERAL,
  356. "There are minor bugs in using %s with libevent %s. "
  357. "You may want to use the latest version of libevent.", m, v);
  358. badness = "BUGGY";
  359. } else if (slow && server) {
  360. log(LOG_WARN, LD_GENERAL,
  361. "libevent %s can be very slow with %s. "
  362. "When running a server, please use the latest version of libevent.",
  363. v,m);
  364. badness = "SLOW";
  365. }
  366. *badness_out = badness;
  367. }
  368. #if defined(LIBEVENT_VERSION)
  369. #define HEADER_VERSION LIBEVENT_VERSION
  370. #elif defined(_EVENT_VERSION)
  371. #define HEADER_VERSION _EVENT_VERSION
  372. #endif
  373. /** See whether the headers we were built against differ from the library we
  374. * linked against so much that we're likely to crash. If so, warn the
  375. * user. */
  376. void
  377. tor_check_libevent_header_compatibility(void)
  378. {
  379. (void) le_versions_compatibility;
  380. (void) tor_decode_libevent_version;
  381. /* In libevent versions before 2.0, it's hard to keep binary compatibility
  382. * between upgrades, and unpleasant to detect when the version we compiled
  383. * against is unlike the version we have linked against. Here's how. */
  384. #if defined(HEADER_VERSION) && defined(HAVE_EVENT_GET_VERSION)
  385. /* We have a header-file version and a function-call version. Easy. */
  386. if (strcmp(HEADER_VERSION, event_get_version())) {
  387. le_version_t v1, v2;
  388. int compat1 = -1, compat2 = -1;
  389. int verybad;
  390. v1 = tor_decode_libevent_version(HEADER_VERSION);
  391. v2 = tor_decode_libevent_version(event_get_version());
  392. compat1 = le_versions_compatibility(v1);
  393. compat2 = le_versions_compatibility(v2);
  394. verybad = compat1 != compat2;
  395. log(verybad ? LOG_WARN : LOG_NOTICE,
  396. LD_GENERAL, "We were compiled with headers from version %s "
  397. "of Libevent, but we're using a Libevent library that says it's "
  398. "version %s.", HEADER_VERSION, event_get_version());
  399. if (verybad)
  400. log_warn(LD_GENERAL, "This will almost certainly make Tor crash.");
  401. else
  402. log_info(LD_GENERAL, "I think these versions are binary-compatible.");
  403. }
  404. #elif defined(HAVE_EVENT_GET_VERSION)
  405. /* event_get_version but no _EVENT_VERSION. We might be in 1.4.0-beta or
  406. earlier, where that's normal. To see whether we were compiled with an
  407. earlier version, let's see whether the struct event defines MIN_HEAP_IDX.
  408. */
  409. #ifdef HAVE_STRUCT_EVENT_MIN_HEAP_IDX
  410. /* The header files are 1.4.0-beta or later. If the version is not
  411. * 1.4.0-beta, we are incompatible. */
  412. {
  413. if (strcmp(event_get_version(), "1.4.0-beta")) {
  414. log_warn(LD_GENERAL, "It's a little hard to tell, but you seem to have "
  415. "Libevent 1.4.0-beta header files, whereas you have linked "
  416. "against Libevent %s. This will probably make Tor crash.",
  417. event_get_version());
  418. }
  419. }
  420. #else
  421. /* Our headers are 1.3e or earlier. If the library version is not 1.4.x or
  422. later, we're probably fine. */
  423. {
  424. const char *v = event_get_version();
  425. if ((v[0] == '1' && v[2] == '.' && v[3] > '3') || v[0] > '1') {
  426. log_warn(LD_GENERAL, "It's a little hard to tell, but you seem to have "
  427. "Libevent header file from 1.3e or earlier, whereas you have "
  428. "linked against Libevent %s. This will probably make Tor "
  429. "crash.", event_get_version());
  430. }
  431. }
  432. #endif
  433. #elif defined(HEADER_VERSION)
  434. #warn "_EVENT_VERSION is defined but not get_event_version(): Libevent is odd."
  435. #else
  436. /* Your libevent is ancient. */
  437. #endif
  438. }
  439. /*
  440. If possible, we're going to try to use Libevent's periodic timer support,
  441. since it does a pretty good job of making sure that periodic events get
  442. called exactly M seconds apart, rather than starting each one exactly M
  443. seconds after the time that the last one was run.
  444. */
  445. #ifdef HAVE_EVENT2_EVENT_H
  446. #define HAVE_PERIODIC
  447. #define PERIODIC_FLAGS EV_PERSIST
  448. #else
  449. #define PERIODIC_FLAGS 0
  450. #endif
  451. /** Represents a timer that's run every N microseconds by Libevent. */
  452. struct periodic_timer_t {
  453. /** Underlying event used to implement this periodic event. */
  454. struct event *ev;
  455. /** The callback we'll be invoking whenever the event triggers */
  456. void (*cb)(struct periodic_timer_t *, void *);
  457. /** User-supplied data for the callback */
  458. void *data;
  459. #ifndef HAVE_PERIODIC
  460. /** If Libevent doesn't know how to invoke events every N microseconds,
  461. * we'll need to remember the timeout interval here. */
  462. struct timeval tv;
  463. #endif
  464. };
  465. /** Libevent callback to implement a periodic event. */
  466. static void
  467. periodic_timer_cb(evutil_socket_t fd, short what, void *arg)
  468. {
  469. periodic_timer_t *timer = arg;
  470. (void) what;
  471. (void) fd;
  472. #ifndef HAVE_PERIODIC
  473. /** reschedule the event as needed. */
  474. event_add(timer->ev, &timer->tv);
  475. #endif
  476. timer->cb(timer, timer->data);
  477. }
  478. /** Create and schedule a new timer that will run every <b>tv</b> in
  479. * the event loop of <b>base</b>. When the timer fires, it will
  480. * run the timer in <b>cb</b> with the user-supplied data in <b>data</b>. */
  481. periodic_timer_t *
  482. periodic_timer_new(struct event_base *base,
  483. const struct timeval *tv,
  484. void (*cb)(periodic_timer_t *timer, void *data),
  485. void *data)
  486. {
  487. periodic_timer_t *timer;
  488. tor_assert(base);
  489. tor_assert(tv);
  490. tor_assert(cb);
  491. timer = tor_malloc_zero(sizeof(periodic_timer_t));
  492. if (!(timer->ev = tor_event_new(base, -1, PERIODIC_FLAGS,
  493. periodic_timer_cb, timer))) {
  494. tor_free(timer);
  495. return NULL;
  496. }
  497. timer->cb = cb;
  498. timer->data = data;
  499. #ifndef HAVE_PERIODIC
  500. memcpy(&timer->tv, tv, sizeof(struct timeval));
  501. #endif
  502. event_add(timer->ev, (struct timeval *)tv); /*drop const for old libevent*/
  503. return timer;
  504. }
  505. /** Stop and free a periodic timer */
  506. void
  507. periodic_timer_free(periodic_timer_t *timer)
  508. {
  509. if (!timer)
  510. return;
  511. tor_event_free(timer->ev);
  512. tor_free(timer);
  513. }