compat_libevent.c 17 KB

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