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