Unverified Commit f015320d authored by jean-christophe81's avatar jean-christophe81 Committed by GitHub
Browse files

MON-12394-backport 21.04 (#54)

parent 4a7a3b3b
# Changelog
## 21.04.4
### centreon-connector
#### Fixes
*perl connector*
add io_context fork notification
## 21.04.1
### Bugfixes
......
......@@ -6,7 +6,7 @@ add_definitions("-D_GLIBCXX_USE_CXX11_ABI=1")
set(CONNECTOR_MAJOR 21)
set(CONNECTOR_MINOR 04)
set(CONNECTOR_PATCH 3)
set(CONNECTOR_PATCH 4)
set(CONNECTOR_VERSION "${CONNECTOR_MAJOR}.${CONNECTOR_MINOR}.${CONNECTOR_PATCH}")
add_definitions(-DCENTREON_CONNECTOR_VERSION=\"${CONNECTOR_VERSION}\")
......
......@@ -26,6 +26,7 @@ CCC_BEGIN();
class log {
private:
std::shared_ptr<spdlog::logger> _core_log;
bool _log_to_file;
log();
~log();
......@@ -37,6 +38,8 @@ class log {
void switch_to_stdout();
void switch_to_file(std::string const& filename);
bool is_log_to_file() const { return _log_to_file; }
static std::shared_ptr<spdlog::logger> core();
};
......
......@@ -24,7 +24,7 @@
using namespace com::centreon::connector;
log::log() {
log::log() : _log_to_file(false) {
auto filesink = std::make_shared<spdlog::sinks::null_sink_mt>();
_core_log = std::make_shared<spdlog::logger>("core", filesink);
_core_log->info("log started");
......@@ -50,6 +50,7 @@ void log::add_pid_to_log() {
}
void log::switch_to_stdout() {
_log_to_file = false;
auto filesink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
spdlog::level::level_enum lvl = _core_log->level();
_core_log = std::make_shared<spdlog::logger>("core", filesink);
......@@ -59,6 +60,7 @@ void log::switch_to_stdout() {
}
void log::switch_to_file(std::string const& filename) {
_log_to_file = true;
auto filesink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(filename);
spdlog::level::level_enum lvl = _core_log->level();
_core_log = std::make_shared<spdlog::logger>("core", filesink);
......
......@@ -62,10 +62,6 @@ class check : public std::enable_shared_from_this<check> {
static void close_all_father_fd();
static unsigned get_nb_check() { return _active_check.size(); }
static void add_signal_set(const shared_signal_set& s) {
_father_signal_set.push_back(s);
}
static void close_all_father_signal_set();
private:
void _start_read_out();
......
......@@ -40,13 +40,6 @@ absl::flat_hash_set<int> check::_all_child_fd;
*/
absl::flat_hash_set<check*> check::_active_check;
/**
* @brief after a fork, child must close all father signal_set. If you don't do
* that father signal_set will handle a child signal
*
*/
std::vector<shared_signal_set> check::_father_signal_set;
/**
* @brief Construct a new check::check object
*
......@@ -95,17 +88,6 @@ void check::close_all_father_fd() {
}
}
/**
* @brief static method that close all father signal set
*
*/
void check::close_all_father_signal_set() {
for (shared_signal_set& s : _father_signal_set) {
s->clear();
}
_father_signal_set.clear();
}
/**
* Execute a Perl script.
*
......@@ -116,10 +98,6 @@ pid_t check::execute() {
// Run process.
int fds[3];
_child = embedded_perl::instance().run(_cmd, fds, _io_context);
if (!_child) { // son
log::core()->debug("{} son started", *this);
return 0;
}
::close(fds[0]);
_all_child_fd.insert(fds[1]);
_all_child_fd.insert(fds[2]);
......@@ -168,9 +146,6 @@ void check::_start_read_out() {
return;
}
log::core()->debug("{} stdout read {} bytes", *this, bytes_transferred);
if (!bytes_transferred) { // close
return;
}
_stdout.append(_out_buff.data(), bytes_transferred);
_start_read_out();
});
......@@ -196,9 +171,6 @@ void check::_start_read_err() {
return;
}
log::core()->debug("{} stderr read {} bytes", *this, bytes_transferred);
if (!bytes_transferred) { // close
return;
}
_stderr.append(_err_buff.data(), bytes_transferred);
_start_read_err();
});
......
......@@ -162,10 +162,12 @@ pid_t embedded_perl::run(std::string const& cmd,
throw basic_error() << msg;
}
io_context->notify_fork(asio::io_context::fork_prepare);
log::core()->flush();
// Execute Perl file.
pid_t child(fork());
if (child > 0) { // Parent
io_context->notify_fork(asio::io_context::fork_parent);
close(in_pipe[0]);
close(err_pipe[1]);
close(out_pipe[1]);
......@@ -173,6 +175,7 @@ pid_t embedded_perl::run(std::string const& cmd,
fds[1] = out_pipe[0];
fds[2] = err_pipe[0];
} else if (!child) { // Child
io_context->notify_fork(asio::io_context::fork_child);
unsigned father_process_name_length = strlen(_argv[0]);
std::string new_process_name("c_");
new_process_name += basename(file.c_str());
......@@ -182,9 +185,14 @@ pid_t embedded_perl::run(std::string const& cmd,
memset(_argv[0], 0, father_process_name_length);
strcpy(_argv[0], new_process_name.c_str());
if (log::instance().is_log_to_file()) {
log::core()->debug("son started pid={}", getpid());
}
// default signal handler
sigset(SIGCHLD, SIG_DFL);
sigset(SIGTERM, SIG_DFL);
// close all father fds
checks::check::close_all_father_fd();
checks::check::close_all_father_signal_set();
io_context->stop();
// Setup process.
close(in_pipe[1]);
......@@ -224,8 +232,11 @@ pid_t embedded_perl::run(std::string const& cmd,
XPUSHs(sv_2mortal(newSVpv(args.c_str(), 0)));
PUTBACK;
call_pv("Embed::Persistent::run_file", G_DISCARD);
std::cerr << "error while executing Perl script '" << file
<< "': " << SvPV_nolen(ERRSV) << std::endl;
if (log::instance().is_log_to_file()) {
log::core()->debug("son end pid={} error:{}", getpid(),
SvPV_nolen(ERRSV));
}
exit(EXIT_SUCCESS);
} else if (child < 0) { // Error
char const* msg(strerror(errno));
close(in_pipe[0]);
......
......@@ -96,8 +96,6 @@ int main(int argc, char** argv, char** env) {
io_context->stop();
});
checks::check::add_signal_set(signal_handler);
// Load Embedded Perl.
embedded_perl::load(argc, argv, env,
(opts.get_argument("code").get_is_set()
......
......@@ -48,7 +48,6 @@ void policy::start(const std::string& test_cmd_file) {
wait_pid();
}
});
checks::check::add_signal_set(signal);
start_second_timer();
}
......
......@@ -187,7 +187,7 @@ TEST_F(TestConnector, ExecuteModuleLoading) {
_write_file(script_path.c_str(),
"#!/usr/bin/perl\n"
"\n"
"use Error::Simple;\n"
"use Sys::Hostname;\n"
"use IO::Socket;\n"
"\n"
"print \"Centreon is wonderful\\n\";\n"
......@@ -212,8 +212,9 @@ TEST_F(TestConnector, ExecuteModuleLoading) {
remove(script_path.c_str());
ASSERT_EQ(retval, 0);
ASSERT_EQ(output.size(), (sizeof(result) - 1));
ASSERT_FALSE(memcmp(output.c_str(), result, sizeof(result) - 1));
std::string expected(result, result + sizeof(result) - 1);
ASSERT_EQ(output, expected);
}
TEST_F(TestConnector, ExecuteMultipleScripts) {
......
......@@ -151,7 +151,7 @@ void check::_open() {
return me->_session->new_channel(me->_channel);
},
[me = shared_from_this(), this](int retval) {
if (!retval) {
if (retval == 0) {
log::core()->info("check {} channel was successfully opened",
_cmd_id);
_step = e_step::chan_exec;
......@@ -233,7 +233,7 @@ void check::_exec() {
return libssh2_channel_exec(me->_channel, me->_cmds.front().c_str());
},
[me = shared_from_this(), this](int retval) {
if (!retval) {
if (retval == 0) {
log::core()->info("check {} was successfully executed", _cmd_id);
_step = e_step::chan_read_stdout;
_process();
......
/*
** Copyright 2011-2014 Centreon
** Copyright 2011-2014, 2022 Centreon
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
......@@ -31,7 +31,7 @@ parser::parser(
parser::pointer parser::create(shared_io_context io_context,
const std::shared_ptr<policy_interface>& policy,
const std::string& test_cmd_file) {
pointer ret(new parser(io_context, policy));
pointer ret{new parser(io_context, policy)};
if (!test_cmd_file.empty()) {
ret->read_file(test_cmd_file);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment