Commit 1e4014d7 authored by Matthieu Kermagoret's avatar Matthieu Kermagoret
Browse files

Perl: do not add invalid FD to the list of FD to close.

This fixes #5797.
parent abca02e9
......@@ -190,7 +190,18 @@ pid_t embedded_perl::run(std::string const& cmd, int fds[3]) {
}
else if (!child) { // Child
// Close existing file descriptors.
pipe_handle::close_all_handles();
try {
pipe_handle::close_all_handles();
}
catch (std::exception const& e) {
std::cerr << "could not close all inherited FDs: "
<< e.what() << std::endl;
exit(3);
}
catch (...) {
std::cerr << "could not close all inherited FDs" << std::endl;
exit(3);
}
// Setup process.
close(in_pipe[1]);
......
......@@ -26,6 +26,7 @@
#include "com/centreon/concurrency/mutex.hh"
#include "com/centreon/connector/perl/pipe_handle.hh"
#include "com/centreon/exceptions/basic.hh"
#include "com/centreon/logging/logger.hh"
using namespace com::centreon;
using namespace com::centreon::connector::perl;
......@@ -97,7 +98,10 @@ void pipe_handle::close() throw () {
if (it != gl_fds->end())
gl_fds->erase(it);
}
::close(_fd);
if (::close(_fd) != 0) {
char const* msg(strerror(errno));
log_error(logging::medium) << "could not close pipe FD: " << msg;
}
_fd = -1;
}
return ;
......@@ -112,10 +116,17 @@ void pipe_handle::close_all_handles() {
it(gl_fds->begin()),
end(gl_fds->end());
it != end;
++it)
++it) {
int retval;
do {
::close(*it);
} while (EINTR == errno);
retval = ::close(*it);
} while ((retval != 0) && (EINTR == errno));
if (retval != 0) {
char const* msg(strerror(errno));
gl_fds->erase(gl_fds->begin(), it);
throw (basic_error() << msg);
}
}
gl_fds->clear();
return ;
}
......@@ -165,7 +176,7 @@ unsigned long pipe_handle::read(void* data, unsigned long size) {
void pipe_handle::set_fd(int fd) {
close();
_fd = fd;
{
if (_fd >= 0) {
concurrency::locker lock(gl_fdsm);
gl_fds->insert(fd);
}
......
/*
** Copyright 2012-2013 Merethis
** Copyright 2012-2014 Merethis
**
** This file is part of Centreon Perl Connector.
**
......@@ -24,6 +24,7 @@
#include <string>
#include <sys/wait.h>
#include "com/centreon/connector/perl/embedded_perl.hh"
#include "com/centreon/connector/perl/pipe_handle.hh"
#include "com/centreon/io/file_stream.hh"
#include "com/centreon/logging/engine.hh"
......@@ -42,6 +43,7 @@ using namespace com::centreon::connector::perl;
int main(int argc, char* argv[], char* env[]) {
// Initialization.
logging::engine::load();
pipe_handle::load();
embedded_perl::load(&argc, &argv, &env);
// Return value.
......@@ -85,6 +87,7 @@ int main(int argc, char* argv[], char* env[]) {
// Unload.
embedded_perl::unload();
pipe_handle::unload();
logging::engine::unload();
return (retval);
......
/*
** Copyright 2012-2013 Merethis
** Copyright 2012-2014 Merethis
**
** This file is part of Centreon Perl Connector.
**
......@@ -24,6 +24,7 @@
#include <string>
#include <sys/wait.h>
#include "com/centreon/connector/perl/embedded_perl.hh"
#include "com/centreon/connector/perl/pipe_handle.hh"
#include "com/centreon/io/file_stream.hh"
#include "com/centreon/logging/engine.hh"
......@@ -42,6 +43,7 @@ using namespace com::centreon::connector::perl;
int main(int argc, char* argv[], char* env[]) {
// Initialization.
logging::engine::load();
pipe_handle::load();
embedded_perl::load(&argc, &argv, &env);
// Return value.
......@@ -89,6 +91,7 @@ int main(int argc, char* argv[], char* env[]) {
// Unload.
embedded_perl::unload();
pipe_handle::unload();
logging::engine::unload();
return (retval);
......
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