From 84d141ab55dbc409aed542fe5163f1ed16353beb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Mon, 25 Apr 2016 16:48:17 +0200 Subject: [PATCH] avoid reentering MHD_run MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: I54547f52c44b05573190cd226f71ee9d40181300 Signed-off-by: José Bollo --- src/afb-hsrv.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/afb-hsrv.c b/src/afb-hsrv.c index 73ad6a60..abec0ada 100644 --- a/src/afb-hsrv.c +++ b/src/afb-hsrv.c @@ -54,8 +54,15 @@ struct hsrv_alias { int dirfd; }; +enum afb_hsrv_state { + hsrv_idle = 0, + hsrv_run, + hsrv_rerun +}; + struct afb_hsrv { unsigned refcount; + enum afb_hsrv_state state; struct hsrv_handler *handlers; struct MHD_Daemon *httpd; struct upoll *upoll; @@ -203,6 +210,19 @@ static void end_handler(void *cls, struct MHD_Connection *connection, void **rec afb_hreq_free(hreq); } +static void handle_epoll_readable(struct afb_hsrv *hsrv) +{ + if (hsrv->state != hsrv_idle) + hsrv->state = hsrv_rerun; + else { + do { + hsrv->state = hsrv_run; + MHD_run(hsrv->httpd); + } while (hsrv->state == hsrv_rerun); + hsrv->state = hsrv_idle; + } +}; + static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen) { return MHD_YES; @@ -347,13 +367,13 @@ int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection return 0; } - upoll = upoll_open(info->listen_fd, httpd); + upoll = upoll_open(info->listen_fd, hsrv); if (upoll == NULL) { MHD_stop_daemon(httpd); fprintf(stderr, "Error: connection to upoll of httpd failed"); return 0; } - upoll_on_readable(upoll, (void*)MHD_run); + upoll_on_readable(upoll, (void*)handle_epoll_readable); hsrv->httpd = httpd; hsrv->upoll = upoll; -- 2.16.6