From 5e38eb931f9207f577e7e8b752be5a522e5f9b87 Mon Sep 17 00:00:00 2001 From: martingh Date: Thu, 26 Oct 2017 10:54:30 +0200 Subject: [PATCH] Adding timer_event to wakeup as suggested by MHD_get_timeout (#7152) --- src/journal-remote/journal-remote.c | 54 +++++++++++++++++++++++++++++++++---- src/journal-remote/journal-remote.h | 3 ++- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/journal-remote/journal-remote.c b/src/journal-remote/journal-remote.c index 9f51137..69397b8 100644 --- a/src/journal-remote/journal-remote.c +++ b/src/journal-remote/journal-remote.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "sd-daemon.h" @@ -299,6 +300,9 @@ static int dispatch_raw_connection_event(sd_event_source *event, int fd, uint32_t revents, void *userdata); +static int null_timer_event_handler(sd_event_source *s, + uint64_t usec, + void *userdata); static int dispatch_http_event(sd_event_source *event, int fd, uint32_t revents, @@ -727,7 +731,7 @@ static int setup_microhttpd_server(RemoteServer *s, goto error; } - r = sd_event_add_io(s->events, &d->event, + r = sd_event_add_io(s->events, &d->io_event, epoll_fd, EPOLLIN, dispatch_http_event, d); if (r < 0) { @@ -735,7 +739,21 @@ static int setup_microhttpd_server(RemoteServer *s, goto error; } - r = sd_event_source_set_description(d->event, "epoll-fd"); + r = sd_event_source_set_description(d->io_event, "io_event"); + if (r < 0) { + log_error_errno(r, "Failed to set source name: %m"); + goto error; + } + + r = sd_event_add_time(s->events, &d->timer_event, + CLOCK_MONOTONIC, UINT64_MAX, 0, + null_timer_event_handler, d); + if (r < 0) { + log_error_errno(r, "Failed to add timer_event: %m"); + goto error; + } + + r = sd_event_source_set_description(d->io_event, "timer_event"); if (r < 0) { log_error_errno(r, "Failed to set source name: %m"); goto error; @@ -777,21 +795,46 @@ static int setup_microhttpd_socket(RemoteServer *s, return setup_microhttpd_server(s, fd, key, cert, trust); } +static int null_timer_event_handler(sd_event_source *timer_event, + uint64_t usec, + void *userdata) { + return dispatch_http_event(timer_event, 0, 0, userdata); +} + static int dispatch_http_event(sd_event_source *event, int fd, uint32_t revents, void *userdata) { MHDDaemonWrapper *d = userdata; int r; + fd_set rd, w, e; + MHD_UNSIGNED_LONG_LONG timeout = ULONG_LONG_MAX; assert(d); - r = MHD_run(d->daemon); + r = MHD_get_fdset(d->daemon, &rd, &w, &e, 0); if (r == MHD_NO) { - log_error("MHD_run failed!"); + log_error("MHD_get_fdset failed!"); + return -EINVAL; + } + r = MHD_run_from_select(d->daemon, &rd, &w, &e); + if (r == MHD_NO) { + log_error("MHD_run_from_select failed!"); // XXX: unregister daemon return -EINVAL; } + if (MHD_get_timeout(d->daemon, &timeout) == MHD_NO) + timeout = ULONG_LONG_MAX; + + r = sd_event_source_set_time(d->timer_event, timeout); + if (r < 0) { + log_warning_errno(r, "Unable to set event loop timeout: %m, this may result in indefinite blocking!"); + return 1; + } + + r = sd_event_source_set_enabled(d->timer_event, SD_EVENT_ON); + if (r < 0) + log_warning_errno(r, "Unable to enable timer_event: %m, this may result in indefinite blocking!"); return 1; /* work to do */ } @@ -1011,7 +1054,8 @@ static void server_destroy(RemoteServer *s) { while ((d = hashmap_steal_first(s->daemons))) { MHD_stop_daemon(d->daemon); - sd_event_source_unref(d->event); + sd_event_source_unref(d->io_event); + sd_event_source_unref(d->timer_event); free(d); } diff --git a/src/journal-remote/journal-remote.h b/src/journal-remote/journal-remote.h index 30ad7df..154ad62 100644 --- a/src/journal-remote/journal-remote.h +++ b/src/journal-remote/journal-remote.h @@ -32,7 +32,8 @@ struct MHDDaemonWrapper { uint64_t fd; struct MHD_Daemon *daemon; - sd_event_source *event; + sd_event_source *io_event; + sd_event_source *timer_event; }; struct RemoteServer { -- 2.7.4