Console: check stdin handle early at startup

This commit is contained in:
SChernykh 2023-04-19 20:47:18 +02:00
parent 6e258bb210
commit 7a83fd2f30
2 changed files with 11 additions and 8 deletions

View file

@ -34,6 +34,8 @@ static constexpr int DEFAULT_BACKLOG = 1;
namespace p2pool { namespace p2pool {
extern const uv_handle_type stdin_type;
ConsoleCommands::ConsoleCommands(p2pool* pool) ConsoleCommands::ConsoleCommands(p2pool* pool)
: TCPServer(DEFAULT_BACKLOG, ConsoleClient::allocate) : TCPServer(DEFAULT_BACKLOG, ConsoleClient::allocate)
, m_pool(pool) , m_pool(pool)
@ -43,6 +45,12 @@ ConsoleCommands::ConsoleCommands(p2pool* pool)
, m_readBuf{} , m_readBuf{}
, m_readBufInUse(false) , m_readBufInUse(false)
{ {
LOGINFO(3, "uv_guess_handle returned " << static_cast<int>(stdin_type));
if (stdin_type != UV_TTY && stdin_type != UV_NAMED_PIPE) {
LOGERR(1, "tty or named pipe is not available");
throw std::exception();
}
std::random_device rd; std::random_device rd;
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
@ -62,13 +70,6 @@ ConsoleCommands::ConsoleCommands(p2pool* pool)
PANIC_STOP(); PANIC_STOP();
} }
const uv_handle_type stdin_type = uv_guess_handle(0);
LOGINFO(3, "uv_guess_handle returned " << static_cast<int>(stdin_type));
if (stdin_type != UV_TTY && stdin_type != UV_NAMED_PIPE) {
LOGERR(1, "tty or named pipe is not available");
throw std::exception();
}
if (stdin_type == UV_TTY) { if (stdin_type == UV_TTY) {
LOGINFO(3, "processing stdin as UV_TTY"); LOGINFO(3, "processing stdin as UV_TTY");
err = uv_tty_init(&m_loop, &m_tty, 0, 1); err = uv_tty_init(&m_loop, &m_tty, 0, 1);
@ -102,7 +103,7 @@ ConsoleCommands::ConsoleCommands(p2pool* pool)
if (m_pool->api() && m_pool->params().m_localStats) { if (m_pool->api() && m_pool->params().m_localStats) {
m_pool->api()->set(p2pool_api::Category::LOCAL, "console", m_pool->api()->set(p2pool_api::Category::LOCAL, "console",
[stdin_type, this](log::Stream& s) [this](log::Stream& s)
{ {
s << "{\"mode\":" << ((stdin_type == UV_TTY) ? "\"tty\"" : "\"pipe\"") s << "{\"mode\":" << ((stdin_type == UV_TTY) ? "\"tty\"" : "\"pipe\"")
<< ",\"tcp_port\":" << m_listenPort << ",\"tcp_port\":" << m_listenPort

View file

@ -32,6 +32,8 @@ static constexpr char log_file_name[] = "p2pool.log";
namespace p2pool { namespace p2pool {
extern const uv_handle_type stdin_type = uv_guess_handle(0);
namespace log { namespace log {
int GLOBAL_LOG_LEVEL = 3; int GLOBAL_LOG_LEVEL = 3;