2012-07-13 21:07:39 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-07-13 21:00:54 +02:00
|
|
|
#include "i2-base.h"
|
2013-02-10 18:57:23 +01:00
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
2013-02-10 17:44:03 +01:00
|
|
|
#include <execvpe.h>
|
2013-02-10 18:57:23 +01:00
|
|
|
#endif /* _MSC_VER */
|
2012-07-13 21:00:54 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-07-14 12:44:37 +02:00
|
|
|
bool Process::m_ThreadCreated = false;
|
2012-07-13 21:00:54 +02:00
|
|
|
boost::mutex Process::m_Mutex;
|
|
|
|
deque<Process::Ptr> Process::m_Tasks;
|
2013-02-10 14:07:17 +01:00
|
|
|
#ifndef _MSC_VER
|
2013-02-10 01:35:40 +01:00
|
|
|
int Process::m_TaskFd;
|
2013-02-10 12:45:39 +01:00
|
|
|
extern char **environ;
|
2013-02-10 14:07:17 +01:00
|
|
|
#endif /* _MSC_VER */
|
2013-02-10 12:45:39 +01:00
|
|
|
|
|
|
|
Process::Process(const vector<String>& arguments, const Dictionary::Ptr& extraEnvironment)
|
|
|
|
: AsyncTask<Process, ProcessResult>(), m_FP(NULL)
|
2012-07-13 21:00:54 +02:00
|
|
|
{
|
2012-09-03 10:28:14 +02:00
|
|
|
assert(Application::IsMainThread());
|
|
|
|
|
2012-07-14 12:44:37 +02:00
|
|
|
if (!m_ThreadCreated) {
|
2013-02-10 14:07:17 +01:00
|
|
|
int childTaskFd;
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
childTaskFd = 0;
|
|
|
|
#else /* _MSC_VER */
|
2013-02-10 01:35:40 +01:00
|
|
|
int fds[2];
|
|
|
|
|
|
|
|
if (pipe(fds) < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(PosixException("pipe() failed.", errno));
|
|
|
|
|
2013-02-10 14:07:17 +01:00
|
|
|
childTaskFd = fds[0];
|
2013-02-10 01:35:40 +01:00
|
|
|
m_TaskFd = fds[1];
|
2013-02-10 14:07:17 +01:00
|
|
|
#endif /* _MSC_VER */
|
2013-02-10 01:35:40 +01:00
|
|
|
|
2013-02-10 14:07:17 +01:00
|
|
|
thread t(&Process::WorkerThreadProc, childTaskFd);
|
2012-07-14 12:44:37 +02:00
|
|
|
t.detach();
|
2012-07-13 21:00:54 +02:00
|
|
|
|
2012-07-14 12:44:37 +02:00
|
|
|
m_ThreadCreated = true;
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
2013-02-10 12:45:39 +01:00
|
|
|
|
|
|
|
// build argv
|
|
|
|
m_Arguments = new char *[arguments.size() + 1];
|
|
|
|
|
|
|
|
for (int i = 0; i < arguments.size(); i++)
|
|
|
|
m_Arguments[i] = strdup(arguments[i].CStr());
|
|
|
|
|
|
|
|
m_Arguments[arguments.size()] = NULL;
|
|
|
|
|
|
|
|
// build envp
|
|
|
|
int envc = 0;
|
|
|
|
|
|
|
|
/* count existing environment variables */
|
|
|
|
while (environ[envc] != NULL)
|
|
|
|
envc++;
|
|
|
|
|
|
|
|
m_Environment = new char *[envc + (extraEnvironment ? extraEnvironment->GetLength() : 0) + 1];
|
|
|
|
|
|
|
|
for (int i = 0; i < envc; i++)
|
|
|
|
m_Environment[i] = strdup(environ[i]);
|
|
|
|
|
|
|
|
if (extraEnvironment) {
|
|
|
|
String key;
|
|
|
|
Value value;
|
|
|
|
int index = envc;
|
|
|
|
BOOST_FOREACH(tie(key, value), extraEnvironment) {
|
|
|
|
String kv = key + "=" + Convert::ToString(value);
|
|
|
|
m_Environment[index] = strdup(kv.CStr());
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Environment[envc + (extraEnvironment ? extraEnvironment->GetLength() : 0)] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<String> Process::ParseCommand(const String& command)
|
|
|
|
{
|
|
|
|
// TODO: implement
|
|
|
|
vector<String> args;
|
2013-02-10 14:07:17 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
args.push_back(command);
|
|
|
|
#else /* MSC_VER */
|
2013-02-10 12:45:39 +01:00
|
|
|
args.push_back("sh");
|
|
|
|
args.push_back("-c");
|
|
|
|
args.push_back(command);
|
2013-02-10 14:07:17 +01:00
|
|
|
#endif
|
2013-02-10 12:45:39 +01:00
|
|
|
return args;
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Process::Run(void)
|
|
|
|
{
|
2013-02-10 01:35:40 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
m_Tasks.push_back(GetSelf());
|
|
|
|
}
|
|
|
|
|
2013-02-10 14:07:17 +01:00
|
|
|
#ifndef _MSC_VER
|
2013-02-10 01:35:40 +01:00
|
|
|
/**
|
|
|
|
* This little gem which is commonly known as the "self-pipe trick"
|
|
|
|
* takes care of waking up the select() call in the worker thread.
|
|
|
|
*/
|
|
|
|
if (write(m_TaskFd, "T", 1) < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(PosixException("write() failed.", errno));
|
2013-02-10 14:07:17 +01:00
|
|
|
#endif /* _MSC_VER */
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
|
|
|
|
2013-02-10 01:35:40 +01:00
|
|
|
void Process::WorkerThreadProc(int taskFd)
|
2012-07-13 21:00:54 +02:00
|
|
|
{
|
|
|
|
map<int, Process::Ptr> tasks;
|
|
|
|
|
|
|
|
for (;;) {
|
2013-02-10 01:35:40 +01:00
|
|
|
map<int, Process::Ptr>::iterator it, prev;
|
2012-07-13 21:00:54 +02:00
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
2013-02-10 01:35:40 +01:00
|
|
|
fd_set readfds;
|
|
|
|
int nfds = 0;
|
2013-02-09 18:39:43 +01:00
|
|
|
|
2013-02-10 01:35:40 +01:00
|
|
|
FD_ZERO(&readfds);
|
2013-02-10 21:53:08 +01:00
|
|
|
|
|
|
|
if (tasks.size() < MaxTasksPerThread)
|
|
|
|
FD_SET(taskFd, &readfds);
|
2012-07-13 21:00:54 +02:00
|
|
|
|
2013-02-10 01:35:40 +01:00
|
|
|
if (taskFd > nfds)
|
|
|
|
nfds = taskFd;
|
2012-07-13 21:00:54 +02:00
|
|
|
|
2013-02-10 01:35:40 +01:00
|
|
|
int fd;
|
|
|
|
BOOST_FOREACH(tie(fd, tuples::ignore), tasks) {
|
|
|
|
if (fd > nfds)
|
|
|
|
nfds = fd;
|
|
|
|
|
|
|
|
FD_SET(fd, &readfds);
|
|
|
|
}
|
2012-07-13 21:00:54 +02:00
|
|
|
|
2013-02-10 21:53:08 +01:00
|
|
|
int rc = select(nfds + 1, &readfds, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (rc < 0 && errno != EINTR)
|
|
|
|
BOOST_THROW_EXCEPTION(PosixException("select() failed.", errno));
|
|
|
|
|
|
|
|
if (rc == 0)
|
|
|
|
continue;
|
|
|
|
|
2012-07-13 21:00:54 +02:00
|
|
|
#else /* _MSC_VER */
|
2013-02-10 01:35:40 +01:00
|
|
|
Utility::Sleep(1);
|
2012-07-13 21:00:54 +02:00
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
2013-02-10 14:07:17 +01:00
|
|
|
#ifndef _MSC_VER
|
2013-02-10 01:35:40 +01:00
|
|
|
if (FD_ISSET(taskFd, &readfds)) {
|
2013-02-10 21:53:08 +01:00
|
|
|
#endif /* _MSC_VER */
|
|
|
|
/* Figure out how many tasks we'd ideally want. */
|
|
|
|
int tasknum = MaxTasksPerThread - tasks.size();
|
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
/* Read one byte for every task we take from the pending tasks list. */
|
|
|
|
char buffer[MaxTasksPerThread];
|
|
|
|
tasknum = read(taskFd, &buffer, tasknum);
|
|
|
|
|
|
|
|
if (tasknum < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(PosixException("read() failed.", errno));
|
|
|
|
|
|
|
|
assert(tasknum >= 1);
|
2013-02-10 14:07:17 +01:00
|
|
|
#endif /* _MSC_VER */
|
2012-07-13 21:00:54 +02:00
|
|
|
|
2013-02-10 21:53:08 +01:00
|
|
|
for (int i = 0; i < tasknum; i++) {
|
2013-02-10 01:35:40 +01:00
|
|
|
Process::Ptr task;
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
if (m_Tasks.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
task = m_Tasks.front();
|
|
|
|
m_Tasks.pop_front();
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
|
|
|
|
2013-02-10 01:35:40 +01:00
|
|
|
try {
|
|
|
|
task->InitTask();
|
2012-07-13 21:00:54 +02:00
|
|
|
|
2013-02-10 12:45:39 +01:00
|
|
|
int fd = fileno(task->m_FP);
|
2013-02-10 01:35:40 +01:00
|
|
|
if (fd >= 0)
|
|
|
|
tasks[fd] = task;
|
|
|
|
} catch (...) {
|
|
|
|
Event::Post(boost::bind(&Process::FinishException, task, boost::current_exception()));
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
2013-02-10 14:07:17 +01:00
|
|
|
#ifndef _MSC_VER
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
2013-02-10 14:07:17 +01:00
|
|
|
#endif /* _MSC_VER */
|
2012-07-13 21:00:54 +02:00
|
|
|
|
2013-02-10 01:35:40 +01:00
|
|
|
for (it = tasks.begin(); it != tasks.end(); ) {
|
|
|
|
int fd = it->first;
|
|
|
|
Process::Ptr task = it->second;
|
2012-07-14 12:44:37 +02:00
|
|
|
|
2013-02-10 01:35:40 +01:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
if (!FD_ISSET(fd, &readfds)) {
|
|
|
|
it++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif /* _MSC_VER */
|
2012-07-14 12:44:37 +02:00
|
|
|
|
2013-02-10 01:35:40 +01:00
|
|
|
if (!task->RunTask()) {
|
|
|
|
prev = it;
|
|
|
|
it++;
|
|
|
|
tasks.erase(prev);
|
2012-07-15 17:29:59 +02:00
|
|
|
|
2013-02-10 01:35:40 +01:00
|
|
|
Event::Post(boost::bind(&Process::FinishResult, task, task->m_Result));
|
|
|
|
} else {
|
|
|
|
it++;
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-10 12:45:39 +01:00
|
|
|
void Process::InitTask(void)
|
2012-07-13 21:00:54 +02:00
|
|
|
{
|
2013-02-10 12:45:39 +01:00
|
|
|
m_Result.ExecutionStart = Utility::GetTime();
|
2013-02-09 23:02:33 +01:00
|
|
|
|
|
|
|
assert(m_FP == NULL);
|
2012-07-14 12:44:37 +02:00
|
|
|
|
2012-07-13 21:00:54 +02:00
|
|
|
#ifdef _MSC_VER
|
2013-02-10 14:07:17 +01:00
|
|
|
String cmdLine;
|
|
|
|
|
|
|
|
// This is almost certainly wrong, but will have to do for now. (#3684)
|
|
|
|
for (int i = 0; m_Arguments[i] != NULL ; i++) {
|
|
|
|
cmdLine += "\"";
|
|
|
|
cmdLine += m_Arguments[i];
|
|
|
|
cmdLine += "\" ";
|
|
|
|
}
|
|
|
|
|
|
|
|
// free arguments
|
|
|
|
for (int i = 0; m_Arguments[i] != NULL; i++)
|
|
|
|
free(m_Arguments[i]);
|
|
|
|
|
|
|
|
delete [] m_Arguments;
|
|
|
|
|
|
|
|
// free environment
|
|
|
|
for (int i = 0; m_Environment[i] != NULL; i++)
|
|
|
|
free(m_Environment[i]);
|
|
|
|
|
|
|
|
delete [] m_Environment;
|
|
|
|
|
|
|
|
m_FP = _popen(cmdLine.CStr(), "r");
|
2012-07-13 21:00:54 +02:00
|
|
|
#else /* _MSC_VER */
|
2013-02-09 23:02:33 +01:00
|
|
|
int fds[2];
|
|
|
|
|
|
|
|
if (pipe(fds) < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(PosixException("pipe() failed.", errno));
|
|
|
|
|
|
|
|
#ifdef HAVE_VFORK
|
|
|
|
m_Pid = vfork();
|
|
|
|
#else /* HAVE_VFORK */
|
|
|
|
m_Pid = fork();
|
|
|
|
#endif /* HAVE_VFORK */
|
|
|
|
|
|
|
|
if (m_Pid < 0)
|
|
|
|
BOOST_THROW_EXCEPTION(PosixException("fork() failed.", errno));
|
|
|
|
|
|
|
|
if (m_Pid == 0) {
|
2013-02-10 12:45:39 +01:00
|
|
|
// child process
|
|
|
|
|
2013-02-09 23:02:33 +01:00
|
|
|
if (dup2(fds[1], STDOUT_FILENO) < 0 || dup2(fds[1], STDERR_FILENO) < 0) {
|
|
|
|
perror("dup2() failed.");
|
|
|
|
_exit(128);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) close(fds[1]);
|
|
|
|
|
2013-02-10 12:45:39 +01:00
|
|
|
if (execvpe(m_Arguments[0], m_Arguments, m_Environment) < 0) {
|
2013-02-09 23:02:33 +01:00
|
|
|
perror("execvpe() failed.");
|
|
|
|
_exit(128);
|
|
|
|
}
|
|
|
|
|
|
|
|
_exit(128);
|
|
|
|
} else {
|
2013-02-10 12:45:39 +01:00
|
|
|
// parent process
|
2013-02-09 23:02:33 +01:00
|
|
|
|
2013-02-10 12:45:39 +01:00
|
|
|
// free arguments
|
|
|
|
for (int i = 0; m_Arguments[i] != NULL; i++)
|
|
|
|
free(m_Arguments[i]);
|
2013-02-09 23:02:33 +01:00
|
|
|
|
2013-02-10 12:45:39 +01:00
|
|
|
delete [] m_Arguments;
|
|
|
|
|
|
|
|
// free environment
|
|
|
|
for (int i = 0; m_Environment[i] != NULL; i++)
|
|
|
|
free(m_Environment[i]);
|
2013-02-09 23:02:33 +01:00
|
|
|
|
2013-02-10 12:45:39 +01:00
|
|
|
delete [] m_Environment;
|
2012-07-13 21:00:54 +02:00
|
|
|
|
2013-02-09 23:02:33 +01:00
|
|
|
(void) close(fds[1]);
|
|
|
|
|
|
|
|
m_FP = fdopen(fds[0], "r");
|
2012-07-13 21:00:54 +02:00
|
|
|
|
2012-07-15 10:58:03 +02:00
|
|
|
if (m_FP == NULL)
|
2013-02-09 23:02:33 +01:00
|
|
|
BOOST_THROW_EXCEPTION(PosixException("fdopen() failed.", errno));
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
2013-02-09 23:02:33 +01:00
|
|
|
#endif /* _MSC_VER */
|
|
|
|
}
|
|
|
|
|
2012-07-13 21:00:54 +02:00
|
|
|
bool Process::RunTask(void)
|
|
|
|
{
|
|
|
|
char buffer[512];
|
|
|
|
size_t read = fread(buffer, 1, sizeof(buffer), m_FP);
|
|
|
|
|
|
|
|
if (read > 0)
|
|
|
|
m_OutputStream.write(buffer, read);
|
|
|
|
|
|
|
|
if (!feof(m_FP))
|
|
|
|
return true;
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String output = m_OutputStream.str();
|
2012-07-13 21:00:54 +02:00
|
|
|
|
|
|
|
int status, exitcode;
|
|
|
|
|
2013-02-10 12:45:39 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
status = _pclose(m_FP);
|
|
|
|
#else /* _MSC_VER */
|
|
|
|
fclose(m_FP);
|
|
|
|
|
|
|
|
if (waitpid(m_Pid, &status, 0) != m_Pid)
|
|
|
|
BOOST_THROW_EXCEPTION(PosixException("waitpid() failed.", errno));
|
|
|
|
#endif /* _MSC_VER */
|
2012-07-14 12:44:37 +02:00
|
|
|
|
2012-07-13 21:00:54 +02:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
exitcode = WEXITSTATUS(status);
|
|
|
|
#else /* _MSC_VER */
|
|
|
|
exitcode = status;
|
|
|
|
|
|
|
|
/* cmd.exe returns error code 1 (warning) when the plugin
|
|
|
|
* could not be executed - change the exit status to "unknown"
|
|
|
|
* when we have no plugin output. */
|
2012-08-02 09:38:08 +02:00
|
|
|
if (output.IsEmpty())
|
2012-07-13 21:00:54 +02:00
|
|
|
exitcode = 128;
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
stringstream outputbuf;
|
|
|
|
outputbuf << "Process was terminated by signal " << WTERMSIG(status);
|
|
|
|
output = outputbuf.str();
|
|
|
|
exitcode = 128;
|
2012-08-04 09:58:31 +02:00
|
|
|
} else {
|
|
|
|
exitcode = 128;
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
2013-02-09 23:02:33 +01:00
|
|
|
m_Result.ExecutionEnd = Utility::GetTime();
|
2012-07-15 10:58:03 +02:00
|
|
|
m_Result.ExitStatus = exitcode;
|
|
|
|
m_Result.Output = output;
|
2012-07-13 21:00:54 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|