From 60ee63c0dbdf02813016ad337cc58df1b4f2f1d2 Mon Sep 17 00:00:00 2001 From: patrick96 Date: Thu, 11 Mar 2021 00:09:06 +0100 Subject: [PATCH] eventloop: Throw exception for libuv errors --- include/components/eventloop.hpp | 17 +++++++-- src/components/controller.cpp | 63 ++++++++++++++++++-------------- src/components/eventloop.cpp | 20 +++++----- 3 files changed, 58 insertions(+), 42 deletions(-) diff --git a/include/components/eventloop.hpp b/include/components/eventloop.hpp index dcdd9517..3a9e49cb 100644 --- a/include/components/eventloop.hpp +++ b/include/components/eventloop.hpp @@ -2,10 +2,22 @@ #include +#include + #include "common.hpp" POLYBAR_NS +/** + * Runs any libuv function with an integer error code return value and throws an + * exception on error. + */ +#define UV(fun, ...) \ + int res = fun(__VA_ARGS__); \ + if (res < 0) { \ + throw std::runtime_error("libuv error for '" #fun "': "s + uv_strerror(res)); \ + } + template struct cb_helper { std::function func; @@ -19,8 +31,7 @@ struct cb_helper { struct SignalHandle { SignalHandle(uv_loop_t* loop, std::function fun) { handle = std::make_unique(); - // TODO handle return value - uv_signal_init(loop, handle.get()); + UV(uv_signal_init, loop, handle.get()); cb = cb_helper{fun}; handle->data = &cb; @@ -48,7 +59,7 @@ class eventloop { void signal_handler(int signum, std::function fun) { auto handle = std::make_unique(get(), fun); - uv_signal_start(handle->handle.get(), &handle->cb.callback, signum); + UV(uv_signal_start, handle->handle.get(), &handle->cb.callback, signum); m_sig_handles.push_back(std::move(handle)); } diff --git a/src/components/controller.cpp b/src/components/controller.cpp index 8cbf9073..ea89fdda 100644 --- a/src/components/controller.cpp +++ b/src/components/controller.cpp @@ -204,6 +204,7 @@ static void conn_cb_wrapper(uv_poll_t* handle, int status, int events) { } void controller::signal_handler(int signum) { + m_log.notice("Received signal SIG%s", sigabbrev_np(signum)); g_terminate = 1; g_reload = (signum == SIGUSR1); eloop->stop(); @@ -248,40 +249,46 @@ static void ipc_read_cb_wrapper(uv_stream_t* stream, ssize_t nread, const uv_buf void controller::read_events() { m_log.info("Entering event loop (thread-id=%lu)", this_thread::get_id()); - eloop = std::make_unique(); - auto loop = eloop->get(); - auto conn_handle = std::make_unique(); - uv_poll_init(loop, conn_handle.get(), m_connection.get_file_descriptor()); - conn_handle->data = this; - - uv_poll_start(conn_handle.get(), UV_READABLE, conn_cb_wrapper); - - for (auto s : {SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGALRM}) { - eloop->signal_handler(s, [this](int signum) { signal_handler(signum); }); - } - auto conf_handle = std::unique_ptr(nullptr); - - if (m_confwatch) { - conf_handle = std::make_unique(); - uv_fs_event_init(loop, conf_handle.get()); - conf_handle->data = this; - - uv_fs_event_start(conf_handle.get(), confwatch_cb_wrapper, m_confwatch->path().c_str(), 0); - } - auto ipc_handle = std::unique_ptr(nullptr); - if (m_ipc) { - ipc_handle = std::make_unique(); - uv_pipe_init(loop, ipc_handle.get(), false); - ipc_handle->data = this; - uv_pipe_open(ipc_handle.get(), m_ipc->get_file_descriptor()); - uv_read_start((uv_stream_t*)ipc_handle.get(), ipc_alloc_cb, ipc_read_cb_wrapper); + try { + eloop = std::make_unique(); + auto loop = eloop->get(); + + uv_poll_init(loop, conn_handle.get(), m_connection.get_file_descriptor()); + conn_handle->data = this; + + uv_poll_start(conn_handle.get(), UV_READABLE, conn_cb_wrapper); + + for (auto s : {SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGALRM}) { + eloop->signal_handler(s, [this](int signum) { signal_handler(signum); }); + } + + if (m_confwatch) { + conf_handle = std::make_unique(); + uv_fs_event_init(loop, conf_handle.get()); + conf_handle->data = this; + + uv_fs_event_start(conf_handle.get(), confwatch_cb_wrapper, m_confwatch->path().c_str(), 0); + } + + if (m_ipc) { + ipc_handle = std::make_unique(); + uv_pipe_init(loop, ipc_handle.get(), false); + ipc_handle->data = this; + uv_pipe_open(ipc_handle.get(), m_ipc->get_file_descriptor()); + uv_read_start((uv_stream_t*)ipc_handle.get(), ipc_alloc_cb, ipc_read_cb_wrapper); + } + + eloop->run(); + } catch (const exception& err) { + m_log.err("Fatal Error in eventloop: %s", err.what()); + g_terminate = 1; + eloop->stop(); } - eloop->run(); eloop.reset(); } diff --git a/src/components/eventloop.cpp b/src/components/eventloop.cpp index d3d4a1bc..da7dad97 100644 --- a/src/components/eventloop.cpp +++ b/src/components/eventloop.cpp @@ -4,9 +4,7 @@ POLYBAR_NS eventloop::eventloop() { m_loop = std::make_unique(); - uv_loop_init(m_loop.get()); - // TODO handle return value - + UV(uv_loop_init, m_loop.get()); m_loop->data = this; } @@ -23,28 +21,28 @@ static void close_walk_cb(uv_handle_t* handle, void*) { */ static void close_loop(uv_loop_t* loop) { uv_walk(loop, close_walk_cb, nullptr); - uv_run(loop, UV_RUN_DEFAULT); - // TODO handle return value + UV(uv_run, loop, UV_RUN_DEFAULT); } eventloop::~eventloop() { if (m_loop) { - close_loop(m_loop.get()); - uv_loop_close(m_loop.get()); - // TODO handle return value + try { + close_loop(m_loop.get()); + UV(uv_loop_close, m_loop.get()); + } catch (const std::exception& e) { + // TODO log error + } m_loop.reset(); } } void eventloop::run() { - uv_run(m_loop.get(), UV_RUN_DEFAULT); - // TODO handle return value + UV(uv_run, m_loop.get(), UV_RUN_DEFAULT); } void eventloop::stop() { uv_stop(m_loop.get()); - // TODO handle return value } POLYBAR_NS_END