42 #include <sys/types.h>
48 #include <SimoxUtility/algorithm/string/string_tools.h>
56 #include "../parser/DependenciesGenerator.h"
57 #include "../parser/iceparser.h"
61 using namespace Parser;
68 char* name = (
char*)calloc(1024,
sizeof(
char));
71 sprintf(name,
"/proc/%d/cmdline", pid);
72 FILE* f = fopen(name,
"r");
76 size = fread(name,
sizeof(
char), 1024, f);
79 if (
'\n' == name[size - 1])
81 name[size - 1] =
'\0';
86 std::string result(name);
96 const std::string& commandLineParameters,
101 scenario->setStatusWriteBlock(
false);
103 std::vector<std::future<void>> futures;
105 std::vector<ApplicationInstancePtr> apps = *scenario->getApplications();
106 for (
auto it = apps.begin(); it != apps.end(); it++)
109 startApplicationAsync(*it, statusManager, commandLineParameters, printOnly));
112 for (
auto future = futures.begin(); future != futures.end(); ++future)
121 const std::string& commandLineParameters,
126 app->setStatusWriteBlock(
false);
128 if (!app->getEnabled())
133 std::filesystem::path componentFolderPath =
134 std::filesystem::path(ArmarXDataPath::GetCachePath()) /
"ComponentFiles";
136 if (!std::filesystem::exists(componentFolderPath))
138 if (!std::filesystem::create_directories(componentFolderPath))
140 std::cout <<
"Could not create Cache folder for ScenarioManagerPlugin at "
141 << componentFolderPath.string() << std::endl;
146 std::filesystem::path dependenciesFilePath =
147 componentFolderPath /
148 std::filesystem::path(
"./" + app->getScenario()->getPackage()->getName() +
149 ".dependencies.cfg");
151 if (!std::filesystem::exists(dependenciesFilePath))
158 if (!std::filesystem::exists(
159 std::filesystem::path(app->getScenario()->getGlobalConfigPath())) ||
160 !std::filesystem::exists(std::filesystem::path(app->getConfigPath())) ||
161 !std::filesystem::exists(dependenciesFilePath))
163 std::cout <<
"Launching " << app->getName() <<
" in "
164 << app->getScenario()->getPackage()->getName() <<
" without an needed cfg file";
166 if (!std::filesystem::exists(app->getExecutableAbsPath()))
168 std::cout <<
"\033[1;31m"
169 <<
"Warning: Could not launch " << app->getName() <<
" in "
170 << app->getScenario()->getPackage()->getName()
171 <<
" because the executable is missing at " << app->getPathToExecutable()
172 <<
"\033[0m" << std::endl;
175 args += app->getExecutableAbsPath();
176 args += std::string(
" --Ice.Config=")
177 .append(app->getScenario()->getGlobalConfigPath())
179 .append(app->getConfigPath());
180 auto configDomain = app->getConfigDomain();
182 args += std::string(
" --" + configDomain +
".DependenciesConfig=")
183 .append(dependenciesFilePath.string());
185 std::string(
" --" + configDomain +
".LoggingGroup=").append(app->getScenario()->getName());
187 if (!commandLineParameters.empty())
189 Ice::StringSeq additional =
Split(commandLineParameters,
" ");
191 bool inString =
false;
192 std::string inStringChar;
193 std::string toPush =
"";
194 for (
auto command : additional)
196 if (!inString && command.find(
"\"") != std::string::npos)
201 else if (!inString && command.find(
"\'") != std::string::npos)
206 else if (inString && command.find(inStringChar) != std::string::npos)
210 args +=
" " + toPush;
217 toPush += command +
" ";
223 args +=
" " + command;
230 Ice::StringSeq additional =
Split(this->commandLineParameters,
" ");
231 for (
auto arg : additional)
238 using namespace boost::process::initializers;
239 #if defined(BOOST_POSIX_API)
241 signal(SIGCHLD, SIG_IGN);
246 for (
auto arg : args)
250 std::cout <<
" &" << std::endl << std::endl;
258 ARMARX_INFO <<
"Executing command `" << app->getExecutableAbsPath() <<
" " << args;
272 if (application->getPid() < 0)
274 application->setPid(statusManager.
loadPid(application));
277 if (application->getPid() < 0)
283 std::string systemAppName = getSystemAppName(application);
288 if (systemAppName.empty())
290 application->setPid(-1);
291 statusManager.
savePid(application);
296 std::string runName = application->getExecutableName();
297 if (systemAppName.compare(runName) == 0)
299 return getStatus(application, statusManager);
304 ARMARX_INFO_S <<
"The Process name with Pid (" << application->getPid() <<
":"
305 << systemAppName <<
") does not correspond with app name ("
306 << application->getName() <<
"). Resetting Saved Pid.";
307 application->setPid(-1);
308 statusManager.
savePid(application);
316 std::string
status = getSystemAppStatus(application);
319 application->setPid(-1);
320 statusManager.
savePid(application);
335 if (namepluspath.empty())
341 std::string result = namepluspath.substr(namepluspath.rfind(
"/") + 1);
350 std::string processFilePath =
"/proc/";
351 processFilePath << application->getPid() <<
"/status";
352 std::ifstream t(processFilePath);
359 std::string stateLine;
361 while (std::getline(t, line))
369 if (stateLine.empty())
373 std::string result = stateLine.substr(stateLine.find(
':') + 1);
375 simox::alg::trim(result);
377 result = result.at(0);
379 if (result ==
"R" || result ==
"S")
383 else if (result ==
"D" || result ==
"Z")
387 else if (result ==
"T" || result ==
"X")
401 const std::string& commandLineParameters,
404 if (application->getPid() != -1)
406 application->setStatusWriteBlock(
false);
407 return std::future<void>();
414 commandLineParameters,
417 std::future<void> result = task.get_future();
418 std::thread t(std::move(task));