Commit a37aa8df authored by Jean Christophe Roques's avatar Jean Christophe Roques
Browse files

MON-13336-unknown-perl_connector-status correct decode child's exit status

parent f015320d
......@@ -72,16 +72,21 @@ void policy::start_second_timer() {
*
*/
void policy::wait_pid() {
pid_t child;
int exit_code;
while ((child = waitpid(-1, &exit_code, WNOHANG)) > 0) {
pid_to_check_map::iterator ended = _checks.find(child);
siginfo_t child_info;
child_info.si_pid = 0;
while (!waitid(P_ALL, 0, &child_info, WNOHANG | WEXITED)) {
if (!child_info.si_pid) { // no exited child
break;
}
pid_to_check_map::iterator ended = _checks.find(child_info.si_pid);
if (ended == _checks.end()) {
log::core()->error("pid {} inconnu", child);
log::core()->error("pid {} inconnu", child_info.si_pid);
child_info.si_pid = 0;
continue;
}
ended->second->set_exit_code(exit_code);
ended->second->set_exit_code(child_info.si_status);
_checks.erase(ended);
child_info.si_pid = 0;
}
}
......
......@@ -45,6 +45,24 @@ static constexpr const char result[] =
"Centreon is wonderful\n"
"\x00\x00\x00\x00";
static constexpr const char result_warning[] =
"3\x00"
"4242\x00"
"1\x00"
"1\x00"
" \x00"
"Centreon is wonderful\n"
"\x00\x00\x00\x00";
static constexpr const char result_critical[] =
"3\x00"
"4242\x00"
"1\x00"
"2\x00"
" \x00"
"Centreon is wonderful\n"
"\x00\x00\x00\x00";
static constexpr std::size_t count = 300;
static constexpr const char cmd3[] = "2\x00";
......@@ -289,8 +307,76 @@ TEST_F(TestConnector, ExecuteSingleScript) {
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, ExecuteSingleWarningScript) {
// Write Perl script.
std::string script_path(com::centreon::io::file_stream::temp_path());
_write_file(script_path.c_str(),
"#!/usr/bin/perl\n"
"\n"
"print \"Centreon is wonderful\\n\";\n"
"exit 1;\n");
log::core()->info("write perl code to {}", script_path);
// Process.
p.exec(perl_connector);
// Write command.
std::ostringstream oss;
oss.write(cmd1, sizeof(cmd1) - 1);
oss << script_path;
oss.write(cmd2, sizeof(cmd2) - 1);
write_cmd(oss.str());
// Read reply.
std::string output{std::move(read_reply())};
int retval{wait_for_termination()};
// Remove temporary files.
remove(script_path.c_str());
ASSERT_EQ(retval, 0);
std::string expected(result_warning,
result_warning + sizeof(result_warning) - 1);
ASSERT_EQ(output, expected);
}
TEST_F(TestConnector, ExecuteSingleCriticalScript) {
// Write Perl script.
std::string script_path(com::centreon::io::file_stream::temp_path());
_write_file(script_path.c_str(),
"#!/usr/bin/perl\n"
"\n"
"print \"Centreon is wonderful\\n\";\n"
"exit 2;\n");
log::core()->info("write perl code to {}", script_path);
// Process.
p.exec(perl_connector);
// Write command.
std::ostringstream oss;
oss.write(cmd1, sizeof(cmd1) - 1);
oss << script_path;
oss.write(cmd2, sizeof(cmd2) - 1);
write_cmd(oss.str());
// Read reply.
std::string output{std::move(read_reply())};
int retval{wait_for_termination()};
// Remove temporary files.
remove(script_path.c_str());
ASSERT_EQ(retval, 0);
std::string expected(result_critical,
result_critical + sizeof(result_critical) - 1);
ASSERT_EQ(output, expected);
}
TEST_F(TestConnector, ExecuteSingleScriptLogFile) {
......
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