ConsoleCommands: check if console is available

This commit is contained in:
SChernykh 2022-07-07 08:32:37 +02:00
parent a7da7f932c
commit 9d692d5194
2 changed files with 22 additions and 6 deletions

View file

@ -40,32 +40,41 @@ ConsoleCommands::ConsoleCommands(p2pool* pool)
, m_readBuf{} , m_readBuf{}
, m_readBufInUse(false) , 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); int err = uv_loop_init(&m_loop);
if (err) { if (err) {
LOGERR(1, "failed to create event loop, error " << uv_err_name(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); err = uv_async_init(&m_loop, &m_shutdownAsync, on_shutdown);
if (err) { if (err) {
LOGERR(1, "uv_async_init failed, error " << uv_err_name(err)); LOGERR(1, "uv_async_init failed, error " << uv_err_name(err));
panic(); throw std::exception();
} }
m_shutdownAsync.data = this; m_shutdownAsync.data = this;
err = uv_tty_init(&m_loop, &m_tty, 0, 1); err = uv_tty_init(&m_loop, &m_tty, 0, 1);
if (err) { if (err) {
LOGERR(1, "uv_tty_init failed, error " << uv_err_name(err)); LOGERR(1, "uv_tty_init failed, error " << uv_err_name(err));
panic(); throw std::exception();
} }
m_tty.data = this; m_tty.data = this;
uv_read_start(reinterpret_cast<uv_stream_t*>(&m_tty), allocCallback, stdinReadCallback); err = uv_read_start(reinterpret_cast<uv_stream_t*>(&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); err = uv_thread_create(&m_loopThread, loop, this);
if (err) { if (err) {
LOGERR(1, "failed to start event loop thread, error " << uv_err_name(err)); LOGERR(1, "failed to start event loop thread, error " << uv_err_name(err));
panic(); throw std::exception();
} }
} }

View file

@ -156,7 +156,14 @@ p2pool::p2pool(int argc, char* argv[])
m_blockTemplate = new BlockTemplate(this); m_blockTemplate = new BlockTemplate(this);
m_mempool = new Mempool(); 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() p2pool::~p2pool()