27 #include "../parser/iceparser.h"
28 #include "../parser/DependenciesGenerator.h"
35 #include <SimoxUtility/algorithm/string/string_tools.h>
41 #include <sys/types.h>
57 using namespace Parser;
63 char* name = (
char*)calloc(1024,
sizeof(
char));
66 sprintf(name,
"/proc/%d/cmdline", pid);
67 FILE* f = fopen(name,
"r");
71 size = fread(name,
sizeof(
char), 1024, f);
74 if (
'\n' == name[size - 1])
76 name[size - 1] =
'\0';
81 std::string result(name);
92 scenario->setStatusWriteBlock(
false);
94 std::vector<std::future<void>> futures;
96 std::vector<ApplicationInstancePtr> apps = *scenario->getApplications();
97 for (
auto it = apps.begin(); it != apps.end(); it++)
99 futures.push_back(startApplicationAsync(*it, statusManager, commandLineParameters, printOnly));
102 for (
auto future = futures.begin(); future != futures.end(); ++future)
113 app->setStatusWriteBlock(
false);
115 if (!app->getEnabled())
120 std::filesystem::path componentFolderPath = std::filesystem::path(ArmarXDataPath::GetCachePath()) /
"ComponentFiles";
122 if (!std::filesystem::exists(componentFolderPath))
124 if (!std::filesystem::create_directories(componentFolderPath))
126 std::cout <<
"Could not create Cache folder for ScenarioManagerPlugin at " << componentFolderPath.string() << std::endl;
131 std::filesystem::path dependenciesFilePath = componentFolderPath / std::filesystem::path(
"./" + app->getScenario()->getPackage()->getName() +
".dependencies.cfg");
133 if (!std::filesystem::exists(dependenciesFilePath))
139 if (!std::filesystem::exists(std::filesystem::path(app->getScenario()->getGlobalConfigPath()))
140 || !std::filesystem::exists(std::filesystem::path(app->getConfigPath()))
141 || !std::filesystem::exists(dependenciesFilePath))
143 std::cout <<
"Launching " << app->getName() <<
" in " << app->getScenario()->getPackage()->getName() <<
" without an needed cfg file";
145 if (!std::filesystem::exists(app->getExecutableAbsPath()))
147 std::cout <<
"\033[1;31m" <<
"Warning: Could not launch " << app->getName() <<
" in " << app->getScenario()->getPackage()->getName() <<
" because the executable is missing at " << app->getPathToExecutable() <<
"\033[0m" << std::endl;
150 args += app->getExecutableAbsPath();
151 args += std::string(
" --Ice.Config=").append(app->getScenario()->getGlobalConfigPath())
152 .append(
",").append(app->getConfigPath());
153 auto configDomain = app->getConfigDomain();
155 args += std::string(
" --" + configDomain +
".DependenciesConfig=").append(dependenciesFilePath.string());
156 args += std::string(
" --" + configDomain +
".LoggingGroup=").append(app->getScenario()->getName());
158 if (!commandLineParameters.empty())
160 Ice::StringSeq additional =
Split(commandLineParameters,
" ");
162 bool inString =
false;
163 std::string inStringChar;
164 std::string toPush =
"";
165 for (
auto command : additional)
167 if (!inString && command.find(
"\"") != std::string::npos)
172 else if (!inString && command.find(
"\'") != std::string::npos)
177 else if (inString && command.find(inStringChar) != std::string::npos)
181 args +=
" " + toPush;
188 toPush += command +
" ";
194 args +=
" " + command;
201 Ice::StringSeq additional =
Split(this->commandLineParameters,
" ");
202 for (
auto arg : additional)
209 using namespace boost::process::initializers;
210 #if defined(BOOST_POSIX_API)
212 signal(SIGCHLD, SIG_IGN);
217 for (
auto arg : args)
221 std::cout <<
" &" << std::endl << std::endl;
229 ARMARX_INFO <<
"Executing command `" << app->getExecutableAbsPath() <<
" "<< args;
243 if (application->getPid() < 0)
245 application->setPid(statusManager.
loadPid(application));
248 if (application->getPid() < 0)
254 std::string systemAppName = getSystemAppName(application);
259 if (systemAppName.empty())
261 application->setPid(-1);
262 statusManager.
savePid(application);
267 std::string runName = application->getExecutableName();
268 if (systemAppName.compare(runName) == 0)
270 return getStatus(application, statusManager);
275 ARMARX_INFO_S <<
"The Process name with Pid (" << application->getPid() <<
":" << systemAppName <<
") does not correspond with app name (" << application->getName() <<
"). Resetting Saved Pid.";
276 application->setPid(-1);
277 statusManager.
savePid(application);
285 std::string
status = getSystemAppStatus(application);
288 application->setPid(-1);
289 statusManager.
savePid(application);
303 if (namepluspath.empty())
309 std::string result = namepluspath.substr(namepluspath.rfind(
"/") + 1);
317 std::string processFilePath =
"/proc/";
318 processFilePath << application->getPid() <<
"/status";
319 std::ifstream t(processFilePath);
326 std::string stateLine;
328 while (std::getline(t, line))
336 if (stateLine.empty())
340 std::string result = stateLine.substr(stateLine.find(
':') + 1);
342 simox::alg::trim(result);
344 result = result.at(0);
346 if (result ==
"R" || result ==
"S")
350 else if (result ==
"D" || result ==
"Z")
354 else if (result ==
"T" || result ==
"X")
365 std::future<void> LinuxStarter::startApplicationAsync(
ApplicationInstancePtr application,
StatusManager statusManager,
const std::string& commandLineParameters,
bool printOnly)
367 if (application->getPid() != -1)
369 application->setStatusWriteBlock(
false);
370 return std::future<void>();
375 std::future<void> result = task.get_future();
376 std::thread t(std::move(task));