From 9d692d5194a9740654286e62e71f65a73ace69a3 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Thu, 7 Jul 2022 08:32:37 +0200 Subject: [PATCH] ConsoleCommands: check if console is available --- src/console_commands.cpp | 19 ++++++++++++++----- src/p2pool.cpp | 9 ++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/console_commands.cpp b/src/console_commands.cpp index d92bedc..67a28c4 100644 --- a/src/console_commands.cpp +++ b/src/console_commands.cpp @@ -40,32 +40,41 @@ ConsoleCommands::ConsoleCommands(p2pool* pool) , m_readBuf{} , m_readBufInUse(false) { + if (uv_guess_handle(0) != UV_TTY) { + LOGERR(1, "tty is not available"); + throw std::exception(); + } + int err = uv_loop_init(&m_loop); if (err) { LOGERR(1, "failed to create event loop, error " << uv_err_name(err)); - panic(); + throw std::exception(); } err = uv_async_init(&m_loop, &m_shutdownAsync, on_shutdown); if (err) { LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); - panic(); + throw std::exception(); } m_shutdownAsync.data = this; err = uv_tty_init(&m_loop, &m_tty, 0, 1); if (err) { LOGERR(1, "uv_tty_init failed, error " << uv_err_name(err)); - panic(); + throw std::exception(); } m_tty.data = this; - uv_read_start(reinterpret_cast(&m_tty), allocCallback, stdinReadCallback); + err = uv_read_start(reinterpret_cast(&m_tty), allocCallback, stdinReadCallback); + if (err) { + LOGERR(1, "uv_read_start failed, error " << uv_err_name(err)); + throw std::exception(); + } err = uv_thread_create(&m_loopThread, loop, this); if (err) { LOGERR(1, "failed to start event loop thread, error " << uv_err_name(err)); - panic(); + throw std::exception(); } } diff --git a/src/p2pool.cpp b/src/p2pool.cpp index dd2f231..78a564a 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -156,7 +156,14 @@ p2pool::p2pool(int argc, char* argv[]) m_blockTemplate = new BlockTemplate(this); m_mempool = new Mempool(); - m_consoleCommands = new ConsoleCommands(this); + + try { + m_consoleCommands = new ConsoleCommands(this); + } + catch (...) { + LOGERR(1, "Couldn't start console commands handler"); + m_consoleCommands = nullptr; + } } p2pool::~p2pool()