#1183 Disable stdin handler if not available.

This commit is contained in:
XMRig 2019-09-21 03:22:19 +07:00
parent 7a1ff6bfed
commit e57798360f
5 changed files with 100 additions and 14 deletions

View file

@ -31,8 +31,11 @@
xmrig::Console::Console(IConsoleListener *listener)
: m_listener(listener)
{
m_tty = new uv_tty_t;
if (!isSupported()) {
return;
}
m_tty = new uv_tty_t;
m_tty->data = this;
uv_tty_init(uv_default_loop(), m_tty, 0, 1);
@ -53,6 +56,10 @@ xmrig::Console::~Console()
void xmrig::Console::stop()
{
if (!m_tty) {
return;
}
uv_tty_reset_mode();
Handle::close(m_tty);
@ -60,6 +67,13 @@ void xmrig::Console::stop()
}
bool xmrig::Console::isSupported() const
{
const uv_handle_type type = uv_guess_handle(0);
return type == UV_TTY || type == UV_NAMED_PIPE;
}
void xmrig::Console::onAllocBuffer(uv_handle_t *handle, size_t, uv_buf_t *buf)
{
auto console = static_cast<Console*>(handle->data);

View file

@ -26,9 +26,11 @@
#define XMRIG_CONSOLE_H
#include <uv.h>
#include "base/tools/Object.h"
#include <uv.h>
namespace xmrig {
@ -39,18 +41,22 @@ class IConsoleListener;
class Console
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(Console)
Console(IConsoleListener *listener);
~Console();
void stop();
private:
bool isSupported() const;
static void onAllocBuffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf);
static void onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf);
char m_buf[1];
char m_buf[1] = { 0 };
IConsoleListener *m_listener;
uv_tty_t *m_tty;
uv_tty_t *m_tty = nullptr;
};

View file

@ -24,7 +24,7 @@
*/
#include <stdio.h>
#include <cstdio>
#include "base/tools/Handle.h"
@ -32,9 +32,13 @@
#include "base/io/log/Log.h"
xmrig::ConsoleLog::ConsoleLog() :
m_stream(nullptr)
xmrig::ConsoleLog::ConsoleLog()
{
if (!isSupported()) {
Log::colors = false;
return;
}
m_tty = new uv_tty_t;
if (uv_tty_init(uv_default_loop(), m_tty, 1, 0) < 0) {
@ -66,7 +70,7 @@ xmrig::ConsoleLog::~ConsoleLog()
void xmrig::ConsoleLog::print(int, const char *line, size_t, size_t size, bool colors)
{
if (Log::colors != colors) {
if (!m_tty || Log::colors != colors) {
return;
}
@ -86,12 +90,18 @@ void xmrig::ConsoleLog::print(int, const char *line, size_t, size_t size, bool c
}
bool xmrig::ConsoleLog::isSupported() const
{
const uv_handle_type type = uv_guess_handle(1);
return type == UV_TTY || type == UV_NAMED_PIPE;
}
bool xmrig::ConsoleLog::isWritable() const
{
if (!m_stream || uv_is_writable(m_stream) != 1) {
return false;
}
const uv_handle_type type = uv_guess_handle(1);
return type == UV_TTY || type == UV_NAMED_PIPE;
return isSupported();
}

View file

@ -27,11 +27,12 @@
#define XMRIG_CONSOLELOG_H
typedef struct uv_stream_s uv_stream_t;
typedef struct uv_tty_s uv_tty_t;
using uv_stream_t = struct uv_stream_s;
using uv_tty_t = struct uv_tty_s;
#include "base/kernel/interfaces/ILogBackend.h"
#include "base/tools/Object.h"
namespace xmrig {
@ -40,6 +41,8 @@ namespace xmrig {
class ConsoleLog : public ILogBackend
{
public:
XMRIG_DISABLE_COPY_MOVE(ConsoleLog)
ConsoleLog();
~ConsoleLog() override;
@ -47,10 +50,11 @@ protected:
void print(int level, const char *line, size_t offset, size_t size, bool colors) override;
private:
bool isSupported() const;
bool isWritable() const;
uv_stream_t *m_stream;
uv_tty_t *m_tty;
uv_stream_t *m_stream = nullptr;
uv_tty_t *m_tty = nullptr;
};

52
src/base/tools/Object.h Normal file
View file

@ -0,0 +1,52 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_OBJECT_H
#define XMRIG_OBJECT_H
#include <chrono>
namespace xmrig {
#define XMRIG_DISABLE_COPY_MOVE(X) \
X(const X &other) = delete; \
X(X &&other) = delete; \
X &operator=(const X &other) = delete; \
X &operator=(X &&other) = delete;
#define XMRIG_DISABLE_COPY_MOVE_DEFAULT(X) \
X() = delete; \
X(const X &other) = delete; \
X(X &&other) = delete; \
X &operator=(const X &other) = delete; \
X &operator=(X &&other) = delete;
} /* namespace xmrig */
#endif /* XMRIG_OBJECT_H */