2013-02-13 11:39:24 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-01-09 00:32:11 +01:00
|
|
|
* Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) *
|
2013-02-13 11:39:24 +01:00
|
|
|
* *
|
|
|
|
* 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 2 *
|
|
|
|
* 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, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/process.h"
|
|
|
|
#include "base/exception.h"
|
|
|
|
#include "base/convert.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/logger_fwd.h"
|
2013-03-25 18:36:15 +01:00
|
|
|
#include "base/utility.h"
|
2014-01-31 08:44:00 +01:00
|
|
|
#include "base/scriptvariable.h"
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/bind.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/make_shared.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
2013-03-18 11:02:18 +01:00
|
|
|
#include <boost/thread/thread.hpp>
|
2013-12-16 16:22:22 +01:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2013-02-24 08:52:09 +01:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
2013-02-13 11:39:24 +01:00
|
|
|
#include <execvpe.h>
|
2013-06-13 12:05:24 +02:00
|
|
|
#include <poll.h>
|
2013-02-13 11:39:24 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-14 15:14:07 +01:00
|
|
|
#ifndef __APPLE__
|
2013-02-13 11:39:24 +01:00
|
|
|
extern char **environ;
|
2013-03-14 15:14:07 +01:00
|
|
|
#else /* __APPLE__ */
|
|
|
|
#include <crt_externs.h>
|
|
|
|
#define environ (*_NSGetEnviron())
|
|
|
|
#endif /* __APPLE__ */
|
2013-02-13 11:39:24 +01:00
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
ProcessResult Process::Run(void)
|
2013-02-13 11:39:24 +01:00
|
|
|
{
|
2013-03-23 00:26:56 +01:00
|
|
|
ProcessResult result;
|
2013-02-13 11:39:24 +01:00
|
|
|
|
2013-03-23 00:26:56 +01:00
|
|
|
result.ExecutionStart = Utility::GetTime();
|
2013-02-13 11:39:24 +01:00
|
|
|
|
|
|
|
int fds[2];
|
|
|
|
|
2013-11-03 13:45:26 +01:00
|
|
|
#ifdef HAVE_PIPE2
|
2013-03-23 18:36:28 +01:00
|
|
|
if (pipe2(fds, O_CLOEXEC) < 0) {
|
2013-03-11 13:45:08 +01:00
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("pipe2")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
2013-02-13 11:39:24 +01:00
|
|
|
#else /* HAVE_PIPE2 */
|
2013-03-11 13:45:08 +01:00
|
|
|
if (pipe(fds) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("pipe")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
2013-02-13 11:39:24 +01:00
|
|
|
|
2013-02-13 13:03:21 +01:00
|
|
|
Utility::SetCloExec(fds[0]);
|
|
|
|
Utility::SetCloExec(fds[1]);
|
2013-02-13 11:39:24 +01:00
|
|
|
#endif /* HAVE_PIPE2 */
|
|
|
|
|
|
|
|
// build argv
|
2013-12-16 16:22:22 +01:00
|
|
|
Log(LogDebug, "base", "Running command '" + boost::algorithm::join(m_Arguments, " ") + "'.");
|
|
|
|
|
2013-02-13 11:39:24 +01:00
|
|
|
char **argv = new char *[m_Arguments.size() + 1];
|
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
for (unsigned int i = 0; i < m_Arguments.size(); i++)
|
2013-02-13 11:39:24 +01:00
|
|
|
argv[i] = strdup(m_Arguments[i].CStr());
|
|
|
|
|
|
|
|
argv[m_Arguments.size()] = NULL;
|
|
|
|
|
|
|
|
m_Arguments.clear();
|
|
|
|
|
|
|
|
// build envp
|
|
|
|
int envc = 0;
|
|
|
|
|
|
|
|
/* count existing environment variables */
|
|
|
|
while (environ[envc] != NULL)
|
|
|
|
envc++;
|
|
|
|
|
|
|
|
char **envp = new char *[envc + (m_ExtraEnvironment ? m_ExtraEnvironment->GetLength() : 0) + 1];
|
|
|
|
|
|
|
|
for (int i = 0; i < envc; i++)
|
|
|
|
envp[i] = strdup(environ[i]);
|
|
|
|
|
|
|
|
if (m_ExtraEnvironment) {
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(m_ExtraEnvironment);
|
|
|
|
|
2013-02-13 11:39:24 +01:00
|
|
|
int index = envc;
|
2013-11-30 17:42:50 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, m_ExtraEnvironment) {
|
|
|
|
String skv = kv.first + "=" + Convert::ToString(kv.second);
|
|
|
|
envp[index] = strdup(skv.CStr());
|
2013-02-13 11:39:24 +01:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
envp[envc + (m_ExtraEnvironment ? m_ExtraEnvironment->GetLength() : 0)] = NULL;
|
|
|
|
|
|
|
|
m_ExtraEnvironment.reset();
|
|
|
|
|
2013-11-03 13:45:26 +01:00
|
|
|
#ifdef HAVE_VFORK
|
2014-01-31 08:44:00 +01:00
|
|
|
Value use_vfork = ScriptVariable::Get("IcingaUseVfork");
|
|
|
|
|
|
|
|
if (use_vfork.IsEmpty() || static_cast<bool>(use_vfork))
|
|
|
|
m_Pid = vfork();
|
|
|
|
else
|
|
|
|
m_Pid = fork();
|
2013-11-03 13:45:26 +01:00
|
|
|
#else /* HAVE_VFORK */
|
2013-02-13 11:39:24 +01:00
|
|
|
m_Pid = fork();
|
2013-11-03 13:45:26 +01:00
|
|
|
#endif /* HAVE_VFORK */
|
2013-02-13 11:39:24 +01:00
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
if (m_Pid < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("fork")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
2013-02-13 11:39:24 +01:00
|
|
|
|
|
|
|
if (m_Pid == 0) {
|
|
|
|
// child process
|
|
|
|
|
|
|
|
if (dup2(fds[1], STDOUT_FILENO) < 0 || dup2(fds[1], STDERR_FILENO) < 0) {
|
|
|
|
perror("dup2() failed.");
|
|
|
|
_exit(128);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) close(fds[0]);
|
|
|
|
(void) close(fds[1]);
|
|
|
|
|
2013-11-28 15:15:28 +01:00
|
|
|
(void) nice(5);
|
|
|
|
|
2013-10-26 09:41:45 +02:00
|
|
|
if (icinga2_execvpe(argv[0], argv, envp) < 0) {
|
2013-02-13 11:39:24 +01:00
|
|
|
perror("execvpe() failed.");
|
|
|
|
_exit(128);
|
|
|
|
}
|
|
|
|
|
|
|
|
_exit(128);
|
|
|
|
}
|
|
|
|
|
|
|
|
// parent process
|
|
|
|
|
|
|
|
// free arguments
|
|
|
|
for (int i = 0; argv[i] != NULL; i++)
|
|
|
|
free(argv[i]);
|
|
|
|
|
|
|
|
delete [] argv;
|
|
|
|
|
|
|
|
// free environment
|
|
|
|
for (int i = 0; envp[i] != NULL; i++)
|
|
|
|
free(envp[i]);
|
|
|
|
|
|
|
|
delete [] envp;
|
|
|
|
|
2013-03-23 00:26:56 +01:00
|
|
|
int fd = fds[0];
|
2013-02-13 11:39:24 +01:00
|
|
|
(void) close(fds[1]);
|
|
|
|
|
2013-12-16 16:22:22 +01:00
|
|
|
char buffer[512];
|
2013-02-13 11:39:24 +01:00
|
|
|
|
2013-03-23 00:26:56 +01:00
|
|
|
std::ostringstream outputStream;
|
|
|
|
|
2013-06-13 12:05:24 +02:00
|
|
|
pollfd pfd;
|
|
|
|
pfd.fd = fd;
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
pfd.revents = 0;
|
|
|
|
|
2013-03-27 19:02:51 +01:00
|
|
|
for (;;) {
|
2013-06-13 12:05:24 +02:00
|
|
|
int rc1, timeout;
|
|
|
|
|
|
|
|
timeout = 0;
|
|
|
|
|
|
|
|
if (m_Timeout != 0) {
|
|
|
|
timeout = m_Timeout - (Utility::GetTime() - result.ExecutionStart);
|
|
|
|
|
|
|
|
if (timeout < 0) {
|
|
|
|
outputStream << "<Timeout exceeded.>";
|
|
|
|
kill(m_Pid, SIGKILL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc1 = poll(&pfd, 1, timeout * 1000);
|
2013-02-13 11:39:24 +01:00
|
|
|
|
2013-06-13 12:05:24 +02:00
|
|
|
if (rc1 > 0) {
|
|
|
|
int rc2 = read(fd, buffer, sizeof(buffer));
|
2013-03-27 19:02:51 +01:00
|
|
|
|
2013-06-13 12:05:24 +02:00
|
|
|
if (rc2 <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
outputStream.write(buffer, rc2);
|
|
|
|
}
|
2013-03-27 19:02:51 +01:00
|
|
|
}
|
2013-02-13 11:39:24 +01:00
|
|
|
|
2013-03-23 00:26:56 +01:00
|
|
|
String output = outputStream.str();
|
2013-02-13 11:39:24 +01:00
|
|
|
|
|
|
|
int status, exitcode;
|
|
|
|
|
2013-03-23 00:26:56 +01:00
|
|
|
(void) close(fd);
|
2013-02-13 11:39:24 +01:00
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
if (waitpid(m_Pid, &status, 0) != m_Pid) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("waitpid")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
2013-02-13 11:39:24 +01:00
|
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
exitcode = WEXITSTATUS(status);
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream outputbuf;
|
2013-06-13 12:05:24 +02:00
|
|
|
outputbuf << "<Terminated by signal " << WTERMSIG(status) << ".>";
|
|
|
|
output = output + outputbuf.str();
|
2013-02-13 11:39:24 +01:00
|
|
|
exitcode = 128;
|
|
|
|
} else {
|
|
|
|
exitcode = 128;
|
|
|
|
}
|
|
|
|
|
2013-03-23 00:26:56 +01:00
|
|
|
result.ExecutionEnd = Utility::GetTime();
|
|
|
|
result.ExitStatus = exitcode;
|
|
|
|
result.Output = output;
|
2013-02-13 11:39:24 +01:00
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
return result;
|
2013-02-24 01:10:34 +01:00
|
|
|
}
|
|
|
|
|
2013-02-13 11:39:24 +01:00
|
|
|
#endif /* _WIN32 */
|