2012-07-13 21:07:39 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2012-07-13 21:07:39 +02: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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/process.hpp"
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/array.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/scriptvariable.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2014-04-21 14:39:35 +02:00
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2014-05-11 17:14:35 +02:00
|
|
|
#include <boost/thread/once.hpp>
|
2014-04-21 14:39:35 +02:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
# include <execvpe.h>
|
|
|
|
# include <poll.h>
|
|
|
|
|
|
|
|
# ifndef __APPLE__
|
|
|
|
extern char **environ;
|
|
|
|
# else /* __APPLE__ */
|
|
|
|
# include <crt_externs.h>
|
|
|
|
# define environ (*_NSGetEnviron())
|
|
|
|
# endif /* __APPLE__ */
|
|
|
|
#endif /* _WIN32 */
|
2013-02-10 18:57:23 +01:00
|
|
|
|
2012-07-13 21:00:54 +02:00
|
|
|
using namespace icinga;
|
|
|
|
|
2014-04-22 19:47:19 +02:00
|
|
|
#define IOTHREADS 2
|
2014-04-21 14:39:35 +02:00
|
|
|
|
|
|
|
static boost::mutex l_ProcessMutex[IOTHREADS];
|
|
|
|
static std::map<Process::ProcessHandle, Process::Ptr> l_Processes[IOTHREADS];
|
|
|
|
#ifdef _WIN32
|
|
|
|
static HANDLE l_Events[IOTHREADS];
|
|
|
|
#else /* _WIN32 */
|
|
|
|
static int l_EventFDs[IOTHREADS][2];
|
|
|
|
static std::map<Process::ConsoleHandle, Process::ProcessHandle> l_FDs[IOTHREADS];
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
static boost::once_flag l_OnceFlag = BOOST_ONCE_INIT;
|
|
|
|
|
|
|
|
INITIALIZE_ONCE(&Process::StaticInitialize);
|
|
|
|
|
|
|
|
Process::Process(const Process::Arguments& arguments, const Dictionary::Ptr& extraEnvironment)
|
2013-06-13 12:05:24 +02:00
|
|
|
: m_Arguments(arguments), m_ExtraEnvironment(extraEnvironment), m_Timeout(600)
|
2013-12-16 16:22:22 +01:00
|
|
|
{ }
|
2013-02-10 12:45:39 +01:00
|
|
|
|
2014-04-21 14:39:35 +02:00
|
|
|
void Process::StaticInitialize(void)
|
|
|
|
{
|
|
|
|
for (int tid = 0; tid < IOTHREADS; tid++) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
l_Events[tid] = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
# ifdef HAVE_PIPE2
|
|
|
|
if (pipe2(l_EventFDs[tid], O_CLOEXEC) < 0) {
|
2014-09-03 08:10:55 +02:00
|
|
|
if (errno == ENOSYS) {
|
2014-04-21 14:39:35 +02:00
|
|
|
# endif /* HAVE_PIPE2 */
|
2014-09-03 08:10:55 +02:00
|
|
|
if (pipe(l_EventFDs[tid]) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("pipe")
|
|
|
|
<< boost::errinfo_errno(errno));
|
|
|
|
}
|
2014-04-21 14:39:35 +02:00
|
|
|
|
2014-09-03 08:10:55 +02:00
|
|
|
Utility::SetCloExec(l_EventFDs[tid][0]);
|
|
|
|
Utility::SetCloExec(l_EventFDs[tid][1]);
|
|
|
|
# ifdef HAVE_PIPE2
|
|
|
|
} else {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("pipe2")
|
|
|
|
<< boost::errinfo_errno(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif /* HAVE_PIPE2 */
|
2014-04-21 14:39:35 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Process::ThreadInitialize(void)
|
2013-02-10 12:45:39 +01:00
|
|
|
{
|
2014-04-21 14:39:35 +02:00
|
|
|
/* Note to self: Make sure this runs _after_ we've daemonized. */
|
|
|
|
for (int tid = 0; tid < IOTHREADS; tid++) {
|
|
|
|
boost::thread t(boost::bind(&Process::IOThreadProc, tid));
|
|
|
|
t.detach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Process::Arguments Process::PrepareCommand(const Value& command)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
String args;
|
|
|
|
#else /* _WIN32 */
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<String> args;
|
2014-04-21 14:39:35 +02:00
|
|
|
#endif /* _WIN32 */
|
2013-02-13 20:08:09 +01:00
|
|
|
|
2013-03-14 12:40:02 +01:00
|
|
|
if (command.IsObjectType<Array>()) {
|
|
|
|
Array::Ptr arguments = command;
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-03-14 12:40:02 +01:00
|
|
|
ObjectLock olock(arguments);
|
|
|
|
BOOST_FOREACH(const Value& argument, arguments) {
|
2014-04-21 14:39:35 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (args != "")
|
|
|
|
args += " ";
|
|
|
|
|
|
|
|
args += Utility::EscapeShellArg(argument);
|
|
|
|
#else /* _WIN32 */
|
2013-03-14 12:40:02 +01:00
|
|
|
args.push_back(argument);
|
2014-04-21 14:39:35 +02:00
|
|
|
#endif /* _WIN32 */
|
2013-02-13 20:08:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2013-02-13 11:39:24 +01:00
|
|
|
#ifdef _WIN32
|
2014-04-21 14:39:35 +02:00
|
|
|
return command;
|
2013-02-13 11:39:24 +01:00
|
|
|
#else /* _WIN32 */
|
2013-02-10 12:45:39 +01:00
|
|
|
args.push_back("sh");
|
|
|
|
args.push_back("-c");
|
|
|
|
args.push_back(command);
|
|
|
|
return args;
|
2014-04-21 14:39:35 +02:00
|
|
|
#endif
|
2012-07-13 21:00:54 +02:00
|
|
|
}
|
2013-06-13 12:05:24 +02:00
|
|
|
|
|
|
|
void Process::SetTimeout(double timeout)
|
|
|
|
{
|
|
|
|
m_Timeout = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Process::GetTimeout(void) const
|
|
|
|
{
|
|
|
|
return m_Timeout;
|
|
|
|
}
|
2014-04-21 14:39:35 +02:00
|
|
|
|
|
|
|
void Process::IOThreadProc(int tid)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE *handles = NULL;
|
|
|
|
#else /* _WIN32 */
|
|
|
|
pollfd *pfds = NULL;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
int count = 0;
|
2014-05-19 13:09:26 +02:00
|
|
|
double now;
|
2014-04-21 14:39:35 +02:00
|
|
|
|
|
|
|
Utility::SetThreadName("ProcessIO");
|
|
|
|
|
|
|
|
for (;;) {
|
2014-05-19 13:09:26 +02:00
|
|
|
double timeout = -1;
|
2014-04-21 14:39:35 +02:00
|
|
|
|
|
|
|
now = Utility::GetTime();
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_ProcessMutex[tid]);
|
|
|
|
|
|
|
|
count = 1 + l_Processes[tid].size();
|
|
|
|
#ifdef _WIN32
|
|
|
|
handles = reinterpret_cast<HANDLE *>(realloc(handles, sizeof(HANDLE) * count));
|
|
|
|
|
|
|
|
handles[0] = l_Events[tid];
|
|
|
|
|
|
|
|
#else /* _WIN32 */
|
|
|
|
pfds = reinterpret_cast<pollfd *>(realloc(pfds, sizeof(pollfd) * count));
|
|
|
|
|
|
|
|
pfds[0].fd = l_EventFDs[tid][0];
|
|
|
|
pfds[0].events = POLLIN;
|
|
|
|
pfds[0].revents = 0;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
std::pair<ProcessHandle, Process::Ptr> kv;
|
|
|
|
BOOST_FOREACH(kv, l_Processes[tid]) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
handles[i] = kv.first;
|
|
|
|
#else /* _WIN32 */
|
|
|
|
pfds[i].fd = kv.second->m_FD;
|
|
|
|
pfds[i].events = POLLIN;
|
|
|
|
pfds[i].revents = 0;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
if (kv.second->m_Timeout != 0) {
|
|
|
|
double delta = kv.second->m_Timeout - (now - kv.second->m_Result.ExecutionStart);
|
|
|
|
|
|
|
|
if (timeout == -1 || delta < timeout)
|
|
|
|
timeout = delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 13:09:26 +02:00
|
|
|
if (timeout < 0.01)
|
|
|
|
timeout = 0.5;
|
|
|
|
|
|
|
|
timeout *= 1000;
|
2014-04-21 14:39:35 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD rc = WaitForMultipleObjects(count, handles, FALSE, timeout == -1 ? INFINITE : static_cast<DWORD>(timeout));
|
|
|
|
#else /* _WIN32 */
|
|
|
|
int rc = poll(pfds, count, timeout);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
continue;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2014-05-19 13:09:26 +02:00
|
|
|
now = Utility::GetTime();
|
|
|
|
|
2014-04-21 14:39:35 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_ProcessMutex[tid]);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (rc == WAIT_OBJECT_0)
|
|
|
|
ResetEvent(l_Events[tid]);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
if (pfds[0].revents & (POLLIN | POLLHUP | POLLERR)) {
|
|
|
|
char buffer[512];
|
2014-06-26 14:12:20 +02:00
|
|
|
if (read(l_EventFDs[tid][0], buffer, sizeof(buffer)) < 0)
|
|
|
|
Log(LogCritical, "base", "Read from event FD failed.");
|
2014-04-21 14:39:35 +02:00
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
for (int i = 1; i < count; i++) {
|
2014-05-22 21:32:08 +02:00
|
|
|
std::map<ProcessHandle, Process::Ptr>::iterator it;
|
|
|
|
#ifdef _WIN32
|
|
|
|
it = l_Processes[tid].find(handles[i]);
|
|
|
|
#else /* _WIN32 */
|
2014-05-19 13:09:26 +02:00
|
|
|
std::map<ConsoleHandle, ProcessHandle>::iterator it2;
|
|
|
|
it2 = l_FDs[tid].find(pfds[i].fd);
|
2014-04-21 14:39:35 +02:00
|
|
|
|
2014-05-19 13:09:26 +02:00
|
|
|
if (it2 == l_FDs[tid].end())
|
|
|
|
continue; /* This should never happen. */
|
2014-04-21 14:39:35 +02:00
|
|
|
|
2014-05-19 13:09:26 +02:00
|
|
|
it = l_Processes[tid].find(it2->second);
|
2014-04-21 14:39:35 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2014-05-19 13:09:26 +02:00
|
|
|
if (it == l_Processes[tid].end())
|
|
|
|
continue; /* This should never happen. */
|
2014-04-21 14:39:35 +02:00
|
|
|
|
2014-05-19 13:09:26 +02:00
|
|
|
bool is_timeout = false;
|
|
|
|
|
|
|
|
if (it->second->m_Timeout != 0) {
|
|
|
|
double timeout = it->second->m_Result.ExecutionStart + it->second->m_Timeout;
|
|
|
|
|
|
|
|
if (timeout < now)
|
|
|
|
is_timeout = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (rc == WAIT_OBJECT_0 + i || is_timeout) {
|
|
|
|
#else /* _WIN32 */
|
|
|
|
if (pfds[i].revents & (POLLIN | POLLHUP | POLLERR) || is_timeout) {
|
|
|
|
#endif /* _WIN32 */
|
2014-04-21 14:39:35 +02:00
|
|
|
if (!it->second->DoEvents()) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
CloseHandle(it->first);
|
|
|
|
CloseHandle(it->second->m_FD);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
l_FDs[tid].erase(it->second->m_FD);
|
|
|
|
(void)close(it->second->m_FD);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
l_Processes[tid].erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 11:25:04 +02:00
|
|
|
String Process::PrettyPrintArguments(const Process::Arguments& arguments)
|
2014-08-19 12:58:55 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2014-08-21 11:25:04 +02:00
|
|
|
return "'" + arguments + "'";
|
2014-08-19 12:58:55 +02:00
|
|
|
#else /* _WIN32 */
|
2014-08-21 11:25:04 +02:00
|
|
|
return "'" + boost::algorithm::join(arguments, "' '") + "'";
|
2014-08-19 12:58:55 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
2014-04-21 14:39:35 +02:00
|
|
|
void Process::Run(const boost::function<void(const ProcessResult&)>& callback)
|
|
|
|
{
|
|
|
|
boost::call_once(l_OnceFlag, &Process::ThreadInitialize);
|
|
|
|
|
|
|
|
m_Result.ExecutionStart = Utility::GetTime();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2014-05-03 19:55:46 +02:00
|
|
|
SECURITY_ATTRIBUTES sa = {};
|
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
|
2014-04-21 14:39:35 +02:00
|
|
|
HANDLE outReadPipe, outWritePipe;
|
2014-05-03 19:55:46 +02:00
|
|
|
if (!CreatePipe(&outReadPipe, &outWritePipe, &sa, 0))
|
2014-04-21 14:39:35 +02:00
|
|
|
BOOST_THROW_EXCEPTION(win32_error()
|
|
|
|
<< boost::errinfo_api_function("CreatePipe")
|
|
|
|
<< errinfo_win32_error(GetLastError()));
|
|
|
|
|
|
|
|
if (!SetHandleInformation(outReadPipe, HANDLE_FLAG_INHERIT, 0))
|
|
|
|
BOOST_THROW_EXCEPTION(win32_error()
|
|
|
|
<< boost::errinfo_api_function("SetHandleInformation")
|
|
|
|
<< errinfo_win32_error(GetLastError()));
|
|
|
|
|
|
|
|
HANDLE outWritePipeDup;
|
|
|
|
if (!DuplicateHandle(GetCurrentProcess(), outWritePipe, GetCurrentProcess(),
|
|
|
|
&outWritePipeDup, 0, TRUE, DUPLICATE_SAME_ACCESS))
|
|
|
|
BOOST_THROW_EXCEPTION(win32_error()
|
|
|
|
<< boost::errinfo_api_function("DuplicateHandle")
|
|
|
|
<< errinfo_win32_error(GetLastError()));
|
|
|
|
|
2014-11-17 09:45:35 +01:00
|
|
|
/* LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList;
|
2014-05-03 19:55:46 +02:00
|
|
|
SIZE_T cbSize;
|
|
|
|
|
|
|
|
if (!InitializeProcThreadAttributeList(NULL, 1, 0, &cbSize) && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
BOOST_THROW_EXCEPTION(win32_error()
|
|
|
|
<< boost::errinfo_api_function("InitializeProcThreadAttributeList")
|
|
|
|
<< errinfo_win32_error(GetLastError()));
|
|
|
|
|
|
|
|
lpAttributeList = reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(new char[cbSize]);
|
|
|
|
|
|
|
|
if (!InitializeProcThreadAttributeList(lpAttributeList, 1, 0, &cbSize))
|
|
|
|
BOOST_THROW_EXCEPTION(win32_error()
|
|
|
|
<< boost::errinfo_api_function("InitializeProcThreadAttributeList")
|
|
|
|
<< errinfo_win32_error(GetLastError()));
|
|
|
|
|
|
|
|
HANDLE rgHandles[3];
|
|
|
|
rgHandles[0] = outWritePipe;
|
|
|
|
rgHandles[1] = outWritePipeDup;
|
|
|
|
rgHandles[2] = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
|
|
|
|
if (!UpdateProcThreadAttribute(lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
|
|
|
|
rgHandles, sizeof(rgHandles), NULL, NULL))
|
|
|
|
BOOST_THROW_EXCEPTION(win32_error()
|
|
|
|
<< boost::errinfo_api_function("UpdateProcThreadAttribute")
|
|
|
|
<< errinfo_win32_error(GetLastError()));
|
2014-11-17 09:45:35 +01:00
|
|
|
*/
|
2014-05-03 19:55:46 +02:00
|
|
|
|
|
|
|
STARTUPINFOEX si = {};
|
|
|
|
si.StartupInfo.cb = sizeof(si);
|
|
|
|
si.StartupInfo.hStdError = outWritePipe;
|
|
|
|
si.StartupInfo.hStdOutput = outWritePipeDup;
|
|
|
|
si.StartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;
|
2014-11-17 09:45:35 +01:00
|
|
|
// si.lpAttributeList = lpAttributeList;
|
2014-04-21 14:39:35 +02:00
|
|
|
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
|
|
|
char *args = new char[m_Arguments.GetLength() + 1];
|
|
|
|
strncpy(args, m_Arguments.CStr(), m_Arguments.GetLength() + 1);
|
|
|
|
args[m_Arguments.GetLength()] = '\0';
|
|
|
|
|
2014-04-21 20:15:03 +02:00
|
|
|
LPCH pEnvironment = GetEnvironmentStrings();
|
|
|
|
size_t ioffset = 0, offset = 0;
|
|
|
|
|
|
|
|
char *envp = NULL;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
size_t len = strlen(pEnvironment + ioffset);
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
char *eqp = strchr(pEnvironment + ioffset, '=');
|
|
|
|
if (eqp && m_ExtraEnvironment && m_ExtraEnvironment->Contains(String(pEnvironment + ioffset, eqp))) {
|
|
|
|
ioffset += len + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
envp = static_cast<char *>(realloc(envp, offset + len + 1));
|
|
|
|
|
|
|
|
if (envp == NULL)
|
|
|
|
BOOST_THROW_EXCEPTION(std::bad_alloc());
|
|
|
|
|
|
|
|
strcpy(envp + offset, pEnvironment + ioffset);
|
|
|
|
offset += len + 1;
|
|
|
|
ioffset += len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeEnvironmentStrings(pEnvironment);
|
|
|
|
|
|
|
|
if (m_ExtraEnvironment) {
|
|
|
|
ObjectLock olock(m_ExtraEnvironment);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, m_ExtraEnvironment) {
|
|
|
|
String skv = kv.first + "=" + Convert::ToString(kv.second);
|
|
|
|
|
|
|
|
envp = static_cast<char *>(realloc(envp, offset + skv.GetLength() + 1));
|
|
|
|
|
|
|
|
if (envp == NULL)
|
|
|
|
BOOST_THROW_EXCEPTION(std::bad_alloc());
|
|
|
|
|
|
|
|
strcpy(envp + offset, skv.CStr());
|
|
|
|
offset += skv.GetLength() + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
envp = static_cast<char *>(realloc(envp, offset + 1));
|
|
|
|
|
|
|
|
if (envp == NULL)
|
|
|
|
BOOST_THROW_EXCEPTION(std::bad_alloc());
|
|
|
|
|
|
|
|
envp[offset] = '\0';
|
|
|
|
|
2014-05-03 19:55:46 +02:00
|
|
|
if (!CreateProcess(NULL, args, NULL, NULL, TRUE,
|
2014-11-17 09:45:35 +01:00
|
|
|
0 /*EXTENDED_STARTUPINFO_PRESENT*/, envp, NULL, &si.StartupInfo, &pi)) {
|
2014-11-07 09:53:23 +01:00
|
|
|
DWORD error = GetLastError();
|
2014-04-21 14:39:35 +02:00
|
|
|
CloseHandle(outWritePipe);
|
|
|
|
CloseHandle(outWritePipeDup);
|
2014-04-21 20:15:03 +02:00
|
|
|
free(envp);
|
2014-11-17 09:45:35 +01:00
|
|
|
/* DeleteProcThreadAttributeList(lpAttributeList);
|
|
|
|
delete [] reinterpret_cast<char *>(lpAttributeList); */
|
2014-11-07 09:53:23 +01:00
|
|
|
|
|
|
|
m_Result.PID = 0;
|
|
|
|
m_Result.ExecutionEnd = Utility::GetTime();
|
|
|
|
m_Result.ExitStatus = 127;
|
|
|
|
m_Result.Output = "Command " + String(args) + " failed to execute: " + Utility::FormatErrorNumber(error);
|
|
|
|
|
|
|
|
delete [] args;
|
|
|
|
|
|
|
|
if (callback)
|
|
|
|
Utility::QueueAsyncCallback(boost::bind(callback, m_Result));
|
|
|
|
|
|
|
|
return;
|
2014-04-21 14:39:35 +02:00
|
|
|
}
|
|
|
|
|
2014-11-07 09:53:23 +01:00
|
|
|
delete [] args;
|
2014-04-21 20:15:03 +02:00
|
|
|
free(envp);
|
2014-11-17 09:45:35 +01:00
|
|
|
/* DeleteProcThreadAttributeList(lpAttributeList);
|
|
|
|
delete [] reinterpret_cast<char *>(lpAttributeList); */
|
2014-04-21 14:39:35 +02:00
|
|
|
|
|
|
|
CloseHandle(outWritePipe);
|
|
|
|
CloseHandle(outWritePipeDup);
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
|
|
|
|
m_Process = pi.hProcess;
|
|
|
|
m_FD = outReadPipe;
|
2014-05-18 18:04:34 +02:00
|
|
|
m_PID = pi.dwProcessId;
|
2014-04-21 14:39:35 +02:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Process")
|
|
|
|
<< "Running command " << PrettyPrintArguments(m_Arguments) << ": PID " << m_PID;
|
2014-04-21 14:39:35 +02:00
|
|
|
|
|
|
|
#else /* _WIN32 */
|
|
|
|
int fds[2];
|
|
|
|
|
|
|
|
#ifdef HAVE_PIPE2
|
|
|
|
if (pipe2(fds, O_CLOEXEC) < 0) {
|
2014-09-03 08:10:55 +02:00
|
|
|
if (errno == ENOSYS) {
|
|
|
|
#endif /* HAVE_PIPE2 */
|
|
|
|
if (pipe(fds) < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("pipe")
|
|
|
|
<< boost::errinfo_errno(errno));
|
|
|
|
}
|
2014-04-21 14:39:35 +02:00
|
|
|
|
2014-09-03 08:10:55 +02:00
|
|
|
Utility::SetCloExec(fds[0]);
|
|
|
|
Utility::SetCloExec(fds[1]);
|
|
|
|
#ifdef HAVE_PIPE2
|
|
|
|
} else {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("pipe2")
|
|
|
|
<< boost::errinfo_errno(errno));
|
|
|
|
}
|
|
|
|
}
|
2014-04-21 14:39:35 +02:00
|
|
|
#endif /* HAVE_PIPE2 */
|
|
|
|
|
|
|
|
// build argv
|
|
|
|
char **argv = new char *[m_Arguments.size() + 1];
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < m_Arguments.size(); i++)
|
|
|
|
argv[i] = strdup(m_Arguments[i].CStr());
|
|
|
|
|
|
|
|
argv[m_Arguments.size()] = NULL;
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
ObjectLock olock(m_ExtraEnvironment);
|
|
|
|
|
|
|
|
int index = envc;
|
|
|
|
BOOST_FOREACH(const Dictionary::Pair& kv, m_ExtraEnvironment) {
|
|
|
|
String skv = kv.first + "=" + Convert::ToString(kv.second);
|
|
|
|
envp[index] = strdup(skv.CStr());
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
envp[envc + (m_ExtraEnvironment ? m_ExtraEnvironment->GetLength() : 0)] = NULL;
|
|
|
|
|
|
|
|
m_ExtraEnvironment.reset();
|
|
|
|
|
|
|
|
#ifdef HAVE_VFORK
|
|
|
|
Value use_vfork = ScriptVariable::Get("UseVfork");
|
|
|
|
|
|
|
|
if (use_vfork.IsEmpty() || static_cast<bool>(use_vfork))
|
|
|
|
m_Process = vfork();
|
|
|
|
else
|
|
|
|
m_Process = fork();
|
|
|
|
#else /* HAVE_VFORK */
|
|
|
|
m_Process = fork();
|
|
|
|
#endif /* HAVE_VFORK */
|
|
|
|
|
|
|
|
if (m_Process < 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("fork")
|
|
|
|
<< boost::errinfo_errno(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Process == 0) {
|
|
|
|
// child process
|
|
|
|
|
|
|
|
if (dup2(fds[1], STDOUT_FILENO) < 0 || dup2(fds[1], STDERR_FILENO) < 0) {
|
2014-08-21 11:25:04 +02:00
|
|
|
perror("dup2() failed");
|
2014-04-21 14:39:35 +02:00
|
|
|
_exit(128);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)close(fds[0]);
|
|
|
|
(void)close(fds[1]);
|
|
|
|
|
2014-09-06 02:20:25 +02:00
|
|
|
#ifdef HAVE_NICE
|
2014-06-26 14:12:20 +02:00
|
|
|
if (nice(5) < 0)
|
|
|
|
Log(LogWarning, "base", "Failed to renice child process.");
|
2014-09-06 02:20:25 +02:00
|
|
|
#endif /* HAVE_NICE */
|
2014-04-21 14:39:35 +02:00
|
|
|
|
|
|
|
if (icinga2_execvpe(argv[0], argv, envp) < 0) {
|
2014-05-12 10:58:17 +02:00
|
|
|
char errmsg[512];
|
|
|
|
strcpy(errmsg, "execvpe(");
|
|
|
|
strncat(errmsg, argv[0], sizeof(errmsg) - 1);
|
2014-08-21 11:25:04 +02:00
|
|
|
strncat(errmsg, ") failed", sizeof(errmsg) - 1);
|
2014-05-12 10:58:17 +02:00
|
|
|
errmsg[sizeof(errmsg) - 1] = '\0';
|
|
|
|
perror(errmsg);
|
2014-04-21 14:39:35 +02:00
|
|
|
_exit(128);
|
|
|
|
}
|
|
|
|
|
|
|
|
_exit(128);
|
|
|
|
}
|
|
|
|
|
|
|
|
// parent process
|
|
|
|
|
2014-05-18 18:04:34 +02:00
|
|
|
m_PID = m_Process;
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Process")
|
|
|
|
<< "Running command " << PrettyPrintArguments(m_Arguments) <<": PID " << m_PID;
|
2014-04-21 14:39:35 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
(void)close(fds[1]);
|
|
|
|
|
|
|
|
Utility::SetNonBlocking(fds[0]);
|
|
|
|
|
|
|
|
m_FD = fds[0];
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
m_Callback = callback;
|
|
|
|
|
|
|
|
int tid = GetTID();
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(l_ProcessMutex[tid]);
|
2014-11-08 21:17:16 +01:00
|
|
|
l_Processes[tid][m_Process] = this;
|
2014-04-21 14:39:35 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
l_FDs[tid][m_FD] = m_Process;
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetEvent(l_Events[tid]);
|
|
|
|
#else /* _WIN32 */
|
2014-06-26 14:12:20 +02:00
|
|
|
if (write(l_EventFDs[tid][1], "T", 1) < 0 && errno != EINTR && errno != EAGAIN)
|
|
|
|
Log(LogCritical, "base", "Write to event FD failed.");
|
2014-04-21 14:39:35 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Process::DoEvents(void)
|
|
|
|
{
|
2014-05-19 13:09:26 +02:00
|
|
|
bool is_timeout = false;
|
|
|
|
|
2014-04-21 14:39:35 +02:00
|
|
|
if (m_Timeout != 0) {
|
2014-05-19 13:09:26 +02:00
|
|
|
double timeout = m_Result.ExecutionStart + m_Timeout;
|
2014-04-21 14:39:35 +02:00
|
|
|
|
2014-05-19 13:09:26 +02:00
|
|
|
if (timeout < Utility::GetTime()) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogWarning, "Process")
|
|
|
|
<< "Killing process " << m_PID << " (" << PrettyPrintArguments(m_Arguments)
|
|
|
|
<< ") after timeout of " << m_Timeout << " seconds";
|
2014-05-23 19:00:55 +02:00
|
|
|
|
2014-04-21 14:39:35 +02:00
|
|
|
m_OutputStream << "<Timeout exceeded.>";
|
|
|
|
#ifdef _WIN32
|
|
|
|
TerminateProcess(m_Process, 1);
|
|
|
|
#else /* _WIN32 */
|
|
|
|
kill(m_Process, SIGKILL);
|
|
|
|
#endif /* _WIN32 */
|
2014-05-19 13:09:26 +02:00
|
|
|
|
|
|
|
is_timeout = true;
|
2014-04-21 14:39:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 13:09:26 +02:00
|
|
|
if (!is_timeout) {
|
|
|
|
char buffer[512];
|
|
|
|
for (;;) {
|
2014-04-21 14:39:35 +02:00
|
|
|
#ifdef _WIN32
|
2014-05-19 13:09:26 +02:00
|
|
|
DWORD rc;
|
|
|
|
if (!ReadFile(m_FD, buffer, sizeof(buffer), &rc, NULL) || rc == 0)
|
|
|
|
break;
|
2014-04-21 14:39:35 +02:00
|
|
|
#else /* _WIN32 */
|
2014-05-19 13:09:26 +02:00
|
|
|
int rc = read(m_FD, buffer, sizeof(buffer));
|
2014-04-21 14:39:35 +02:00
|
|
|
|
2014-05-19 13:09:26 +02:00
|
|
|
if (rc < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
|
|
|
|
return true;
|
2014-04-21 14:39:35 +02:00
|
|
|
|
2014-05-19 13:09:26 +02:00
|
|
|
if (rc > 0) {
|
2014-04-21 14:39:35 +02:00
|
|
|
#endif /* _WIN32 */
|
2014-05-19 13:09:26 +02:00
|
|
|
m_OutputStream.write(buffer, rc);
|
2014-04-21 14:39:35 +02:00
|
|
|
#ifdef _WIN32
|
2014-05-19 13:09:26 +02:00
|
|
|
return true;
|
2014-04-21 14:39:35 +02:00
|
|
|
#else /* _WIN32 */
|
2014-05-19 13:09:26 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-21 14:39:35 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2014-05-19 13:09:26 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-04-21 14:39:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String output = m_OutputStream.str();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
WaitForSingleObject(m_Process, INFINITE);
|
|
|
|
|
|
|
|
DWORD exitcode;
|
|
|
|
GetExitCodeProcess(m_Process, &exitcode);
|
2014-05-23 19:00:55 +02:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Process")
|
|
|
|
<< "PID " << m_PID << " (" << PrettyPrintArguments(m_Arguments) << ") terminated with exit code " << exitcode;
|
2014-04-21 14:39:35 +02:00
|
|
|
#else /* _WIN32 */
|
|
|
|
int status, exitcode;
|
|
|
|
if (waitpid(m_Process, &status, 0) != m_Process) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("waitpid")
|
|
|
|
<< boost::errinfo_errno(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
exitcode = WEXITSTATUS(status);
|
2014-05-23 19:00:55 +02:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "Process")
|
|
|
|
<< "PID " << m_PID << " (" << PrettyPrintArguments(m_Arguments) << ") terminated with exit code " << exitcode;
|
2014-05-23 19:00:55 +02:00
|
|
|
} else if (WIFSIGNALED(status)) {
|
2014-11-13 08:53:12 +01:00
|
|
|
int signum = WTERMSIG(status);
|
|
|
|
char *zsigname = strsignal(signum);
|
|
|
|
|
|
|
|
String signame = Convert::ToString(signum);
|
|
|
|
|
|
|
|
if (zsigname) {
|
|
|
|
signame += " (";
|
|
|
|
signame += zsigname;
|
|
|
|
signame += ")";
|
|
|
|
}
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogWarning, "Process")
|
2014-11-13 08:53:12 +01:00
|
|
|
<< "PID " << m_PID << " was terminated by signal " << signame;
|
2014-05-23 19:00:55 +02:00
|
|
|
|
2014-04-21 14:39:35 +02:00
|
|
|
std::ostringstream outputbuf;
|
2014-11-13 08:53:12 +01:00
|
|
|
outputbuf << "<Terminated by signal " << signame << ".>";
|
2014-04-21 14:39:35 +02:00
|
|
|
output = output + outputbuf.str();
|
|
|
|
exitcode = 128;
|
2014-05-23 19:00:55 +02:00
|
|
|
} else {
|
2014-04-21 14:39:35 +02:00
|
|
|
exitcode = 128;
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2014-08-21 11:25:04 +02:00
|
|
|
m_Result.PID = m_PID;
|
2014-04-21 14:39:35 +02:00
|
|
|
m_Result.ExecutionEnd = Utility::GetTime();
|
|
|
|
m_Result.ExitStatus = exitcode;
|
|
|
|
m_Result.Output = output;
|
|
|
|
|
|
|
|
if (m_Callback)
|
|
|
|
Utility::QueueAsyncCallback(boost::bind(m_Callback, m_Result));
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-18 18:04:34 +02:00
|
|
|
pid_t Process::GetPID(void) const
|
2014-05-17 23:07:10 +02:00
|
|
|
{
|
2014-05-18 18:04:34 +02:00
|
|
|
return m_PID;
|
2014-05-17 23:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-21 14:39:35 +02:00
|
|
|
int Process::GetTID(void) const
|
|
|
|
{
|
|
|
|
return (reinterpret_cast<uintptr_t>(this) / sizeof(void *)) % IOTHREADS;
|
|
|
|
}
|
|
|
|
|