From c71d89c95abcf0c2fdba7fda4813ebd258754714 Mon Sep 17 00:00:00 2001 From: T W Lee Date: Wed, 8 Mar 2023 02:35:49 +1300 Subject: [PATCH 1/2] allow named pipe as stdin --- src/console_commands.cpp | 39 +++++++++++++++++++++++++++++---------- src/console_commands.h | 4 +++- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/console_commands.cpp b/src/console_commands.cpp index fe1727e..4a2c3a4 100644 --- a/src/console_commands.cpp +++ b/src/console_commands.cpp @@ -36,15 +36,12 @@ ConsoleCommands::ConsoleCommands(p2pool* pool) , m_loop{} , m_shutdownAsync{} , m_tty{} + , m_stdin_pipe{} + , m_stdin_handle() , m_loopThread{} , 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)); @@ -58,14 +55,36 @@ ConsoleCommands::ConsoleCommands(p2pool* pool) } 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)); + uv_handle_type stdin_type = uv_guess_handle(0); + LOGINFO(3, "uv_guess_handle returned " << (int)stdin_type); + if (stdin_type == UV_TTY) { + LOGINFO(3, "processing stdin as UV_TTY"); + err = uv_tty_init(&m_loop, &m_tty, 0, 1); + if (err) { + LOGERR(1, "uv_tty_init failed, error " << uv_err_name(err)); + throw std::exception(); + } + m_stdin_handle = reinterpret_cast(&m_tty); + } else if (stdin_type == UV_NAMED_PIPE) { + LOGINFO(3, "processing stdin as UV_NAMED_PIPE"); + err = uv_pipe_init(&m_loop, &m_stdin_pipe, 0); + if (err) { + LOGERR(1, "uv_pipe_init failed, error " << uv_err_name(err)); + throw std::exception(); + } + m_stdin_handle = reinterpret_cast(&m_stdin_pipe); + err = uv_pipe_open(&m_stdin_pipe, 0); + if (err) { + LOGERR(1, "uv_pipe_open failed, error " << uv_err_name(err)); + throw std::exception(); + } + } else { + LOGERR(1, "tty or named pipe is not available"); throw std::exception(); } - m_tty.data = this; + m_stdin_handle->data = this; - err = uv_read_start(reinterpret_cast(&m_tty), allocCallback, stdinReadCallback); + err = uv_read_start(reinterpret_cast(m_stdin_handle), allocCallback, stdinReadCallback); if (err) { LOGERR(1, "uv_read_start failed, error " << uv_err_name(err)); throw std::exception(); diff --git a/src/console_commands.h b/src/console_commands.h index 59e9c68..cb289a5 100644 --- a/src/console_commands.h +++ b/src/console_commands.h @@ -35,6 +35,8 @@ private: uv_loop_t m_loop; uv_async_t m_shutdownAsync; uv_tty_t m_tty; + uv_pipe_t m_stdin_pipe; + uv_handle_t* m_stdin_handle; uv_thread_t m_loopThread; char m_readBuf[64]; @@ -48,7 +50,7 @@ private: { ConsoleCommands* pThis = reinterpret_cast(async->data); uv_close(reinterpret_cast(&pThis->m_shutdownAsync), nullptr); - uv_close(reinterpret_cast(&pThis->m_tty), nullptr); + uv_close(reinterpret_cast(pThis->m_stdin_handle), nullptr); } static void allocCallback(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf); From 3df743066320cb67723ef4b8cd55aca809136684 Mon Sep 17 00:00:00 2001 From: T W Lee Date: Thu, 9 Mar 2023 14:36:33 +1300 Subject: [PATCH 2/2] allow named pipe as stdin, fixes after review --- src/console_commands.cpp | 16 +++++++++------- src/console_commands.h | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/console_commands.cpp b/src/console_commands.cpp index 4a2c3a4..df1302c 100644 --- a/src/console_commands.cpp +++ b/src/console_commands.cpp @@ -42,6 +42,13 @@ ConsoleCommands::ConsoleCommands(p2pool* pool) , m_readBuf{} , m_readBufInUse(false) { + uv_handle_type stdin_type = uv_guess_handle(0); + LOGINFO(3, "uv_guess_handle returned " << static_cast(stdin_type)); + if (stdin_type != UV_TTY && stdin_type != UV_NAMED_PIPE) { + LOGERR(1, "tty or named pipe 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)); @@ -55,8 +62,6 @@ ConsoleCommands::ConsoleCommands(p2pool* pool) } m_shutdownAsync.data = this; - uv_handle_type stdin_type = uv_guess_handle(0); - LOGINFO(3, "uv_guess_handle returned " << (int)stdin_type); if (stdin_type == UV_TTY) { LOGINFO(3, "processing stdin as UV_TTY"); err = uv_tty_init(&m_loop, &m_tty, 0, 1); @@ -64,7 +69,7 @@ ConsoleCommands::ConsoleCommands(p2pool* pool) LOGERR(1, "uv_tty_init failed, error " << uv_err_name(err)); throw std::exception(); } - m_stdin_handle = reinterpret_cast(&m_tty); + m_stdin_handle = reinterpret_cast(&m_tty); } else if (stdin_type == UV_NAMED_PIPE) { LOGINFO(3, "processing stdin as UV_NAMED_PIPE"); err = uv_pipe_init(&m_loop, &m_stdin_pipe, 0); @@ -72,15 +77,12 @@ ConsoleCommands::ConsoleCommands(p2pool* pool) LOGERR(1, "uv_pipe_init failed, error " << uv_err_name(err)); throw std::exception(); } - m_stdin_handle = reinterpret_cast(&m_stdin_pipe); + m_stdin_handle = reinterpret_cast(&m_stdin_pipe); err = uv_pipe_open(&m_stdin_pipe, 0); if (err) { LOGERR(1, "uv_pipe_open failed, error " << uv_err_name(err)); throw std::exception(); } - } else { - LOGERR(1, "tty or named pipe is not available"); - throw std::exception(); } m_stdin_handle->data = this; diff --git a/src/console_commands.h b/src/console_commands.h index cb289a5..def59d7 100644 --- a/src/console_commands.h +++ b/src/console_commands.h @@ -36,7 +36,7 @@ private: uv_async_t m_shutdownAsync; uv_tty_t m_tty; uv_pipe_t m_stdin_pipe; - uv_handle_t* m_stdin_handle; + uv_stream_t* m_stdin_handle; uv_thread_t m_loopThread; char m_readBuf[64];