]> granicus.if.org Git - libevent/commitdiff
Avoid calling exit() during event_base_new*()
authorNick Mathewson <nickm@torproject.org>
Tue, 27 Oct 2009 06:47:25 +0000 (06:47 +0000)
committerNick Mathewson <nickm@torproject.org>
Tue, 27 Oct 2009 06:47:25 +0000 (06:47 +0000)
Previously, each of the three make-an-event-base functions would exit
under different, weird circumstances, but return NULL on others.
  - All three would exit on OOM sometimes.
  - event_base_new() and event_init() would die if all backends were
    disabled.
  - None of them would die if the socketpair() call failed.

Now, only event_init() exits on failure, and it exits on every kind of
failure.  event_base_new() and event_base_new_with_config() never do.

svn:r1472

ChangeLog
event.c
test/regress.c

index cef1fda75e8ee1e4e399258ed4c492e80a97df2d..9f2922a3039b17c11551c701c3224cc7c97dd262 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -35,6 +35,7 @@ Changes in 2.0.3-alpha:
  o Add an API to replace all fatal calls to exit() with a user-provided panic function.
  o Replace all assert() calls with a variant that is aware of the user-provided logging and panic functions.
  o Add a return value to event_assign so that it can fail rather than asserting when the user gives it bad input.  event_set still dies on bad input.
+ o The event_base_new() and event_base_new_with_config() functions now never call exit() on failure.  For backward "compatibility", event_init() still does, but more consistently.
 
 
 Changes in 2.0.2-alpha:
diff --git a/event.c b/event.c
index f0bed0c7ce5b9792bc25663deb17a15558bd0c5b..af202a635c7ae3354167fdce4c549a932d335aac 100644 (file)
--- a/event.c
+++ b/event.c
@@ -189,10 +189,12 @@ gettime(struct event_base *base, struct timeval *tp)
 struct event_base *
 event_init(void)
 {
-       struct event_base *base = event_base_new();
+       struct event_base *base = event_base_new_with_config(NULL);
 
-       if (base != NULL)
-               current_base = base;
+       if (base == NULL)
+               event_errx(1, "%s: Unable to construct event_base", __func__);
+
+       current_base = base;
 
        return (base);
 }
@@ -200,7 +202,13 @@ event_init(void)
 struct event_base *
 event_base_new(void)
 {
-       return (event_base_new_with_config(NULL));
+       struct event_base *base = NULL;
+       struct event_config *cfg = event_config_new();
+       if (cfg) {
+               base = event_base_new_with_config(cfg);
+               event_config_free(cfg);
+       }
+       return base;
 }
 
 static int
@@ -263,8 +271,10 @@ event_base_new_with_config(struct event_config *cfg)
        struct event_base *base;
        int should_check_environment;
 
-       if ((base = mm_calloc(1, sizeof(struct event_base))) == NULL)
-               event_err(1, "%s: calloc", __func__);
+       if ((base = mm_calloc(1, sizeof(struct event_base))) == NULL) {
+               event_warn("%s: calloc", __func__);
+               return NULL;
+       }
 
        detect_monotonic();
        gettime(base, &base->event_tv);
@@ -308,13 +318,10 @@ event_base_new_with_config(struct event_config *cfg)
        }
 
        if (base->evbase == NULL) {
-               if (cfg == NULL)
-                       event_errx(1, "%s: no event mechanism available",
-                           __func__);
-               else {
-                       event_base_free(base);
-                       return NULL;
-               }
+               event_warnx("%s: no event mechanism available",
+                   __func__);
+               event_base_free(base);
+               return NULL;
        }
 
        if (getenv("EVENT_SHOW_METHOD"))
index eef9859bc1b813031005dcf53403fa704bd05513..e19295808b2db19b7da7c4ed04883517be8fadd7 100644 (file)
@@ -1789,7 +1789,7 @@ struct testcase_t main_testcases[] = {
         /* Some converted-over tests */
         { "methods", test_methods, TT_FORK, NULL, NULL },
        { "version", test_version, 0, NULL, NULL },
-       { "base_features", test_base_features, TT_FORK, NULL, NULL },
+       BASIC(base_features, TT_FORK|TT_NO_LOGS),
        { "base_environ", test_base_environ, TT_FORK, NULL, NULL },
 
        BASIC(event_base_new, TT_FORK|TT_NEED_SOCKETPAIR),