45 void sigint_handler(
int sig)
48 yarp::run::Run::mStresstest=
false;
50 if (yarp::run::Run::pServerPort)
53 yarp::run::Run::pServerPort =
nullptr;
65 const char *at = txt.c_str();
70 result.
addString(std::string(at, len-slash_tweak));
76 slash_tweak = (ch==
slash && len>0)?1:0;
80 result.
addString(std::string(at, len-slash_tweak));
88 fp = fopen(fname,
"r");
100int yarp::run::Run::server()
106 if (!port.
open(mPortName.c_str()))
108 yError() <<
"Yarprun failed to open port: " << mPortName.c_str();
119 yInfo() <<
"Yarprun successfully started on port: " << mPortName.c_str();
123 yarp::os::impl::signal(SIGINT, sigint_handler);
124 yarp::os::impl::signal(SIGTERM, sigint_handler);
133 RUNLOG(
"<<<port.read(msg, true)")
134 if (!port.read(msg, true)) break;
135 RUNLOG(">>>port.read(msg, true)")
137 if (!pServerPort) break;
145 if (msg.check("stdio"))
148 std::string strStdioPort=msg.find(
"stdio").asString();
150 if (strOnPort==mPortName)
156 msg.addList()=botUUID;
158 if (mLogged || msg.check(
"log"))
161 std::string portName=
"/log";
162 portName+=mPortName+
"/";
163 std::string command = msg.findGroup(
"cmd").get(1).asString();
164 command = command.substr(0, command.find(
' '));
165 command = command.substr(command.find_last_of(
"\\/") + 1);
171 if (msg.check(
"log"))
175 if (botLogger.
size()>1)
188 msg.addList()=botFwd;
192 if (executeCmdAndStdio(msg, cmdResult)>0)
194 if (strStdioPort==mPortName)
197 userStdio(msg, stdioResult);
198 cmdResult.
append(stdioResult);
202 cmdResult.
append(sendMsg(msg, strStdioPort));
206 port.
reply(cmdResult);
211 userStdio(msg, stdioResult);
212 port.
reply(stdioResult);
219 if (msg.check(
"cmd"))
223 if (msg.check(
"log"))
227 if (botLogger.
size()>1)
229 std::string loggerName=botLogger.
get(1).
asString();
230 executeCmdStdout(msg, cmdResult, loggerName);
234 executeCmdStdout(msg, cmdResult, mLoggerPort);
239 executeCmdStdout(msg, cmdResult, mLoggerPort);
243 executeCmd(msg, cmdResult);
245 port.
reply(cmdResult);
249 if (msg.check(
"kill"))
251 std::string alias(msg.findGroup(
"kill").get(1).asString());
252 int sig=msg.findGroup(
"kill").get(2).asInt32();
254 result.
addString(mProcessVector.Signal(alias, sig)?
"kill OK":
"kill FAILED");
259 if (msg.check(
"sigterm"))
261 std::string alias(msg.find(
"sigterm").asString());
263 result.
addString(mProcessVector.Signal(alias, SIGTERM)?
"sigterm OK":
"sigterm FAILED");
268 if (msg.check(
"sigtermall"))
270 mProcessVector.Killall(SIGTERM);
280 result.
append(mProcessVector.PS());
285 if (msg.check(
"isrunning"))
287 std::string alias(msg.find(
"isrunning").asString());
289 result.
addString(mProcessVector.IsRunning(alias)?
"running":
"not running");
294 if (msg.check(
"killstdio"))
296 std::string alias(msg.find(
"killstdio").asString());
297 mStdioVector.Signal(alias, SIGTERM);
306 if (msg.check(
"sysinfo"))
314 if (msg.check(
"which"))
316 std::string fileName=msg.find(
"which").asString();
320 for (
int i=0; i<possiblePaths.
size(); ++i)
322 std::string guessString=possiblePaths.
get(i).
asString() +
323 std::string{
slash} + fileName;
324 const char* guess=guessString.c_str();
327 fileName=
"\"" + std::string(guess) +
"\"";
333 port.
reply(fileNameWriter);
337 if (msg.check(
"exit"))
339 yInfo() <<
"Yarprun server closing...";
349 Run::mStdioVector.Killall(SIGTERM);
351 Run::mProcessVector.Killall(SIGTERM);
353 yInfo() <<
"Yarprun server closed";
361void yarp::run::Run::cleanBeforeExec()
373 mProcessVector =
nullptr;
379 mStdioVector =
nullptr;
382 if (mBraveZombieHunter)
385 mBraveZombieHunter =
nullptr;
393void yarp::run::Run::writeToPipe(
int fd, std::string str)
395 int len = str.length() + 1;
397 ret =
write(fd, &len,
sizeof(len));
398 if (ret !=
sizeof(len)) {
399 fprintf(stderr,
"Warning: could not write string length to pipe.\n");
401 ret =
write(fd, str.c_str(), len);
403 fprintf(stderr,
"Warning: could not write string to pipe.\n");
407int yarp::run::Run::readFromPipe(
int fd,
char* &data,
int& buffsize)
410 char* buff=(
char*)&len;
412 for (
int c=4, r=0; c>0; c-=r)
430 data=
new char[buffsize=1024+(len/1024)*1024];
435 for (
int c=len, r=0; c>0; c-=r)
452 if (yarp::run::Run::mBraveZombieHunter)
454 yarp::run::Run::mBraveZombieHunter->sigchldHandler();
458int yarp::run::Run::server()
460 int pipe_server2manager[2];
461 int pipe_manager2server[2];
463 if (yarp::run::impl::pipe(pipe_server2manager))
465 fprintf(stderr,
"Can't open pipe because %s\n", strerror(errno));
471 if (yarp::run::impl::pipe(pipe_manager2server))
473 fprintf(stderr,
"Can't open pipe because %s\n", strerror(errno));
479 int pid_process_manager=yarp::run::impl::fork();
481 if (IS_INVALID(pid_process_manager))
490 fprintf(stderr,
"Can't fork process manager because %s\n", strerror(
error));
496 if (IS_PARENT_OF(pid_process_manager))
498 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
505 if (!port.
open(mPortName))
507 yError() <<
"Yarprun failed to open port: " << mPortName.c_str();
509 if (mPortName[0] !=
'/') {
510 yError(
"Invalid port name '%s', it should start with '/'\n", mPortName.c_str());
522 yInfo() <<
"Yarprun successfully started on port: " << mPortName.c_str();
526 yarp::os::impl::signal(SIGINT, sigint_handler);
527 yarp::os::impl::signal(SIGTERM, sigint_handler);
530 char *rsp_str=
new char[rsp_size];
536 RUNLOG(
"<<<port.read(msg, true)")
537 if (!port.read(msg, true)) {
540 RUNLOG(
">>>port.read(msg, true)")
546 if (msg.
check(
"sysinfo"))
554 if (msg.
check(
"which"))
560 for (
size_t i=0; i<possiblePaths.
size(); ++i)
563 const char* guess=guessString.c_str();
572 port.
reply(fileNameWriter);
576 if (msg.
check(
"exit"))
578 pServerPort =
nullptr;
591 int nread=readFromPipe(pipe_manager2server[
READ_FROM_PIPE], rsp_str, rsp_size);
596 fprintf(stderr,
"ERROR: broken pipe between server and manager\n");
604 port.
reply(response);
618 if (IS_NEW_PROCESS(pid_process_manager))
620 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
631 mBraveZombieHunter->
start();
638 char *msg_str=
new char[msg_size];
646 if (readFromPipe(pipe_server2manager[
READ_FROM_PIPE], msg_str, msg_size) <= 0) {
654 msg.fromString(msg_str);
657 if (msg.check("stdio"))
662 if (strOnPort==mPortName)
670 if (mLogged || msg.
check(
"log"))
673 std::string portName=
"/log";
674 portName+=mPortName+
"/";
676 command = command.substr(0, command.find(
' '));
677 command = command.substr(command.find_last_of(
"\\/") + 1);
683 if (msg.
check(
"log"))
687 if (botLogger.
size()>1)
708 if (executeCmdAndStdio(msg, cmdResult)>0)
710 if (strStdioPort==mPortName)
713 userStdio(msg, stdioResult);
714 cmdResult.
append(stdioResult);
718 cmdResult.
append(sendMsg(msg, strStdioPort));
729 userStdio(msg, stdioResult);
739 if (msg.check("cmd"))
743 if (msg.
check(
"log"))
747 if (botLogger.
size()>1)
749 std::string loggerName=botLogger.
get(1).
asString();
750 executeCmdStdout(msg, cmdResult, loggerName);
754 executeCmdStdout(msg, cmdResult, mLoggerPort);
759 executeCmdStdout(msg, cmdResult, mLoggerPort);
763 executeCmd(msg, cmdResult);
772 if (msg.check("kill"))
777 result.
addString(mProcessVector->Signal(alias, sig)?
"kill OK":
"kill FAILED");
784 if (msg.check("sigterm"))
788 result.
addString(mProcessVector->Signal(alias, SIGTERM)?
"sigterm OK":
"sigterm FAILED");
795 if (msg.check("sigtermall"))
797 mProcessVector->Killall(SIGTERM);
810 result.
append(mProcessVector->PS());
817 if (msg.check("isrunning"))
821 result.
addString(mProcessVector->IsRunning(alias)?
"running":
"not running");
828 if (msg.check("killstdio"))
831 mStdioVector->Signal(alias, SIGTERM);
841 mStdioVector->Killall(SIGTERM);
843 mProcessVector->Killall(SIGTERM);
845 if (mBraveZombieHunter)
847 mBraveZombieHunter->stop();
848 delete mBraveZombieHunter;
849 mBraveZombieHunter =
nullptr;
852 delete mProcessVector;
std::string toString(const T &value)
convert an arbitrary type to string.
std::string int2String(int x)
static yarp::os::Bottle parsePaths(const std::string &txt)
constexpr fs::value_type slash
static bool fileExists(const char *fname)
static void sigchld_handler(int sig)
A simple collection of objects that can be described and transmitted in a portable way.
void fromString(const std::string &text)
Initializes bottle from a string.
void append(const Bottle &alt)
Append the content of the given bottle to the current list.
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
size_type size() const
Gets the number of elements in the bottle.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
A mini-server for performing network communication in the background.
static bool connect(const std::string &src, const std::string &dest, const std::string &carrier="", bool quiet=true)
Request that an output port connect to an input port.
A port that is specialized as an RPC server.
A class to get the system (platform) status such as available memory, storage, CPU load and etc.
void updateSystemInfo()
updates the system information by calling the static methods to retrieve the latest data.
bool stop()
Stop the thread.
bool start()
Start the new thread running.
A single value (typically within a Bottle).
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual std::string asString() const
Get string value.
static NameClient & getNameClient()
Get an instance of the name client.
static bool sysinfo(const std::string &node, yarp::os::SystemInfo &info)
Get a report of system information of a yarprun server.
static constexpr char path_separator
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
static constexpr value_type preferred_separator
int getpid()
Portable wrapper for the getppid() function.
bool read(ImageOf< PixelRgb > &dest, const std::string &src, image_fileformat format=FORMAT_ANY)
bool write(const ImageOf< PixelRgb > &src, const std::string &dest, image_fileformat format=FORMAT_PPM)