From 2deb3ce061adffccbacff6b87401134c4047b8b3 Mon Sep 17 00:00:00 2001 From: Niels Provos Date: Thu, 29 May 2008 01:39:43 +0000 Subject: [PATCH] simplify handling of environment variables for disabling backends; make event_get_supported_methods obey environment variables; this fixes make verify; problem reported by Scott Lamb. svn:r838 --- ChangeLog | 3 ++- devpoll.c | 4 ---- epoll.c | 4 ---- event.c | 39 +++++++++++++++++++++++++++++++-------- evport.c | 5 ----- kqueue.c | 4 ---- poll.c | 4 ---- select.c | 4 ---- test/regress.c | 20 ++++++++++---------- 9 files changed, 43 insertions(+), 44 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5f268c2c..d02f498f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -102,7 +102,8 @@ Changes in current version: o Fix connection keep-alive behavior for HTTP/1.0 o Fix use of freed memory in event_reinit; pointed out by Peter Postma o constify struct timeval * where possible - + o make event_get_supported_methods obey environment variables + Changes in 1.4.0: o allow \r or \n individually to separate HTTP headers instead of the standard "\r\n"; from Charles Kerr. o demote most http warnings to debug messages diff --git a/devpoll.c b/devpoll.c index 92e54dee..ccb47c29 100644 --- a/devpoll.c +++ b/devpoll.c @@ -131,10 +131,6 @@ devpoll_init(struct event_base *base) struct rlimit rl; struct devpollop *devpollop; - /* Disable devpoll when this environment variable is set */ - if (getenv("EVENT_NODEVPOLL")) - return (NULL); - if (!(devpollop = mm_calloc(1, sizeof(struct devpollop)))) return (NULL); diff --git a/epoll.c b/epoll.c index a111b22a..423d9898 100644 --- a/epoll.c +++ b/epoll.c @@ -112,10 +112,6 @@ epoll_init(struct event_base *base) struct rlimit rl; struct epollop *epollop; - /* Disable epollueue when this environment variable is set */ - if (getenv("EVENT_NOEPOLL")) - return (NULL); - if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && rl.rlim_cur != RLIM_INFINITY) { /* diff --git a/event.c b/event.c index 47fef92c..da2b6f3d 100644 --- a/event.c +++ b/event.c @@ -51,6 +51,7 @@ #ifdef HAVE_UNISTD_H #include #endif +#include #include #include #include @@ -208,6 +209,19 @@ event_config_is_avoided_method(struct event_config *cfg, const char *method) return (0); } +static int +event_is_method_disabled(const char *name) +{ + char environment[64]; + int i; + + evutil_snprintf(environment, sizeof(environment), "EVENT_NO%s", name); + for (i = 8; environment[i] != '\0'; ++i) + environment[i] = toupper(environment[i]); + return (getenv(environment) != NULL); + +} + struct event_base * event_base_new_with_config(struct event_config *cfg) { @@ -238,6 +252,10 @@ event_base_new_with_config(struct event_config *cfg) continue; } + /* also obey the environment variables */ + if (event_is_method_disabled(eventops[i]->name)) + continue; + base->evsel = eventops[i]; base->evbase = base->evsel->init(base); @@ -247,8 +265,7 @@ event_base_new_with_config(struct event_config *cfg) event_errx(1, "%s: no event mechanism available", __func__); if (getenv("EVENT_SHOW_METHOD")) - event_msgx("libevent using: %s\n", - base->evsel->name); + event_msgx("libevent using: %s", base->evsel->name); /* allocate a single active event queue */ event_base_priority_init(base, 1); @@ -356,12 +373,12 @@ event_get_supported_methods(void) const char **tmp; int i = 0, k; - if (methods != NULL) - return (methods); - /* count all methods */ - for (method = &eventops[0]; *method != NULL; ++method) + for (method = &eventops[0]; *method != NULL; ++method) { + if (event_is_method_disabled((*method)->name)) + continue; ++i; + } /* allocate one more than we need for the NULL pointer */ tmp = mm_malloc((i + 1) * sizeof(char *)); @@ -369,10 +386,16 @@ event_get_supported_methods(void) return (NULL); /* populate the array with the supported methods */ - for (k = 0; k < i; ++k) - tmp[k] = eventops[k]->name; + for (k = 0, i = 0; eventops[k] != NULL; ++k) { + if (event_is_method_disabled(eventops[k]->name)) + continue; + tmp[i++] = eventops[k]->name; + } tmp[i] = NULL; + if (methods != NULL) + mm_free(methods); + methods = tmp; return (methods); diff --git a/evport.c b/evport.c index d2fe3a8c..fc396baa 100644 --- a/evport.c +++ b/evport.c @@ -141,11 +141,6 @@ evport_init(struct event_base *base) { struct evport_data *evpd; int i; - /* - * Disable event ports when this environment variable is set - */ - if (getenv("EVENT_NOEVPORT")) - return (NULL); if (!(evpd = mm_calloc(1, sizeof(struct evport_data)))) return (NULL); diff --git a/kqueue.c b/kqueue.c index 58670651..b213be02 100644 --- a/kqueue.c +++ b/kqueue.c @@ -99,10 +99,6 @@ kq_init(struct event_base *base) int kq; struct kqop *kqueueop; - /* Disable kqueue when this environment variable is set */ - if (getenv("EVENT_NOKQUEUE")) - return (NULL); - if (!(kqueueop = mm_calloc(1, sizeof(struct kqop)))) return (NULL); diff --git a/poll.c b/poll.c index 0cc37f02..7b2270bc 100644 --- a/poll.c +++ b/poll.c @@ -87,10 +87,6 @@ poll_init(struct event_base *base) { struct pollop *pollop; - /* Disable poll when this environment variable is set */ - if (getenv("EVENT_NOPOLL")) - return (NULL); - if (!(pollop = mm_calloc(1, sizeof(struct pollop)))) return (NULL); diff --git a/select.c b/select.c index e0848ee3..0df99e58 100644 --- a/select.c +++ b/select.c @@ -94,10 +94,6 @@ select_init(struct event_base *base) { struct selectop *sop; - /* Disable select when this environment variable is set */ - if (getenv("EVENT_NOSELECT")) - return (NULL); - if (!(sop = mm_calloc(1, sizeof(struct selectop)))) return (NULL); diff --git a/test/regress.c b/test/regress.c index 6850aeca..ca83e879 100644 --- a/test/regress.c +++ b/test/regress.c @@ -495,7 +495,7 @@ test_fork(void) if ((pid = fork()) == 0) { /* in the child */ if (event_reinit(current_base) == -1) { - fprintf(stderr, "FAILED (reinit)\n"); + fprintf(stdout, "FAILED (reinit)\n"); exit(1); } @@ -515,12 +515,12 @@ test_fork(void) write(pair[0], TEST1, strlen(TEST1)+1); if (waitpid(pid, &status, 0) == -1) { - fprintf(stderr, "FAILED (fork)\n"); + fprintf(stdout, "FAILED (fork)\n"); exit(1); } if (WEXITSTATUS(status) != 76) { - fprintf(stderr, "FAILED (exit): %d\n", WEXITSTATUS(status)); + fprintf(stdout, "FAILED (exit): %d\n", WEXITSTATUS(status)); exit(1); } @@ -1971,7 +1971,7 @@ rpc_test(void) gettimeofday(&tv_end, NULL); evutil_timersub(&tv_end, &tv_start, &tv_end); - fprintf(stderr, "(%.1f us/add) ", + fprintf(stdout, "(%.1f us/add) ", (float)tv_end.tv_sec/(float)i * 1000000.0 + tv_end.tv_usec / (float)i); @@ -2109,16 +2109,16 @@ test_methods(void) const char *backend; int n_methods = 0; - fprintf(stderr, "Testing supported methods: "); + fprintf(stdout, "Testing supported methods: "); if (methods == NULL) { - fprintf(stderr, "FAILED\n"); + fprintf(stdout, "FAILED\n"); exit(1); } backend = methods[0]; while (*methods != NULL) { - fprintf(stderr, "%s ", *methods); + fprintf(stdout, "%s ", *methods); ++methods; ++n_methods; } @@ -2135,19 +2135,19 @@ test_methods(void) base = event_base_new_with_config(cfg); if (base == NULL) { - fprintf(stderr, "FAILED\n"); + fprintf(stdout, "FAILED\n"); exit(1); } if (strcmp(backend, event_base_get_method(base)) == 0) { - fprintf(stderr, "FAILED\n"); + fprintf(stdout, "FAILED\n"); exit(1); } event_base_free(base); event_config_free(cfg); done: - fprintf(stderr, "OK\n"); + fprintf(stdout, "OK\n"); }