46 void sigint_handler(
int sig)
49 yarp::run::Run::mStresstest=
false;
51 if (yarp::run::Run::pServerPort)
54 yarp::run::Run::pServerPort =
nullptr;
66 const char *at = txt.c_str();
71 result.
addString(std::string(at, len-slash_tweak));
77 slash_tweak = (ch==
slash && len>0)?1:0;
81 result.
addString(std::string(at, len-slash_tweak));
89 fp = fopen(fname,
"r");
101int yarp::run::Run::server()
107 if (!port.
open(mPortName.c_str()))
109 yError() <<
"Yarprun failed to open port: " << mPortName.c_str();
120 yInfo() <<
"Yarprun successfully started on port: " << mPortName.c_str();
124 yarp::os::impl::signal(SIGINT, sigint_handler);
125 yarp::os::impl::signal(SIGTERM, sigint_handler);
134 RUNLOG(
"<<<port.read(msg, true)")
135 if (!port.read(msg, true)) break;
136 RUNLOG(">>>port.read(msg, true)")
138 if (!pServerPort) break;
146 if (msg.check("stdio"))
149 std::string strStdioPort=msg.find(
"stdio").asString();
151 if (strOnPort==mPortName)
157 msg.addList()=botUUID;
159 if (mLogged || msg.check(
"log"))
162 std::string portName=
"/log";
163 portName+=mPortName+
"/";
164 std::string command = msg.findGroup(
"cmd").get(1).asString();
165 command = command.substr(0, command.find(
' '));
166 command = command.substr(command.find_last_of(
"\\/") + 1);
172 if (msg.check(
"log"))
176 if (botLogger.
size()>1)
189 msg.addList()=botFwd;
193 if (executeCmdAndStdio(msg, cmdResult)>0)
195 if (strStdioPort==mPortName)
198 userStdio(msg, stdioResult);
199 cmdResult.
append(stdioResult);
203 cmdResult.
append(sendMsg(msg, strStdioPort));
207 port.
reply(cmdResult);
212 userStdio(msg, stdioResult);
213 port.
reply(stdioResult);
220 if (msg.check(
"cmd"))
224 if (msg.check(
"log"))
228 if (botLogger.
size()>1)
230 std::string loggerName=botLogger.
get(1).
asString();
231 executeCmdStdout(msg, cmdResult, loggerName);
235 executeCmdStdout(msg, cmdResult, mLoggerPort);
240 executeCmdStdout(msg, cmdResult, mLoggerPort);
244 executeCmd(msg, cmdResult);
246 port.
reply(cmdResult);
250 if (msg.check(
"kill"))
252 std::string alias(msg.findGroup(
"kill").get(1).asString());
253 int sig=msg.findGroup(
"kill").get(2).asInt32();
255 result.
addString(mProcessVector.Signal(alias, sig)?
"kill OK":
"kill FAILED");
260 if (msg.check(
"sigterm"))
262 std::string alias(msg.find(
"sigterm").asString());
264 result.
addString(mProcessVector.Signal(alias, SIGTERM)?
"sigterm OK":
"sigterm FAILED");
269 if (msg.check(
"sigtermall"))
271 mProcessVector.Killall(SIGTERM);
281 result.
append(mProcessVector.PS());
286 if (msg.check(
"isrunning"))
288 std::string alias(msg.find(
"isrunning").asString());
290 result.
addString(mProcessVector.IsRunning(alias)?
"running":
"not running");
295 if (msg.check(
"killstdio"))
297 std::string alias(msg.find(
"killstdio").asString());
298 mStdioVector.Signal(alias, SIGTERM);
307 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"))
553 if (msg.
check(
"which"))
559 for (
size_t i=0; i<possiblePaths.
size(); ++i)
562 const char* guess=guessString.c_str();
571 port.
reply(fileNameWriter);
575 if (msg.
check(
"exit"))
577 pServerPort =
nullptr;
590 int nread=readFromPipe(pipe_manager2server[
READ_FROM_PIPE], rsp_str, rsp_size);
595 fprintf(stderr,
"ERROR: broken pipe between server and manager\n");
603 port.
reply(response);
617 if (IS_NEW_PROCESS(pid_process_manager))
619 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
630 mBraveZombieHunter->
start();
637 char *msg_str=
new char[msg_size];
645 if (readFromPipe(pipe_server2manager[
READ_FROM_PIPE], msg_str, msg_size) <= 0) {
653 msg.fromString(msg_str);
656 if (msg.check("stdio"))
661 if (strOnPort==mPortName)
669 if (mLogged || msg.
check(
"log"))
672 std::string portName=
"/log";
673 portName+=mPortName+
"/";
675 command = command.substr(0, command.find(
' '));
676 command = command.substr(command.find_last_of(
"\\/") + 1);
682 if (msg.
check(
"log"))
686 if (botLogger.
size()>1)
707 if (executeCmdAndStdio(msg, cmdResult)>0)
709 if (strStdioPort==mPortName)
712 userStdio(msg, stdioResult);
713 cmdResult.
append(stdioResult);
717 cmdResult.
append(sendMsg(msg, strStdioPort));
728 userStdio(msg, stdioResult);
738 if (msg.check("cmd"))
742 if (msg.
check(
"log"))
746 if (botLogger.
size()>1)
748 std::string loggerName=botLogger.
get(1).
asString();
749 executeCmdStdout(msg, cmdResult, loggerName);
753 executeCmdStdout(msg, cmdResult, mLoggerPort);
758 executeCmdStdout(msg, cmdResult, mLoggerPort);
762 executeCmd(msg, cmdResult);
771 if (msg.check("kill"))
776 result.
addString(mProcessVector->Signal(alias, sig)?
"kill OK":
"kill FAILED");
783 if (msg.check("sigterm"))
787 result.
addString(mProcessVector->Signal(alias, SIGTERM)?
"sigterm OK":
"sigterm FAILED");
794 if (msg.check("sigtermall"))
796 mProcessVector->Killall(SIGTERM);
809 result.
append(mProcessVector->PS());
816 if (msg.check("isrunning"))
820 result.
addString(mProcessVector->IsRunning(alias)?
"running":
"not running");
827 if (msg.check("killstdio"))
830 mStdioVector->Signal(alias, SIGTERM);
840 mStdioVector->Killall(SIGTERM);
842 mProcessVector->Killall(SIGTERM);
844 if (mBraveZombieHunter)
846 mBraveZombieHunter->stop();
847 delete mBraveZombieHunter;
848 mBraveZombieHunter =
nullptr;
851 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)
void sigchld_handler(int)
An error handler that reaps the zombies.
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 helper class to pass the SystemInfo object around the YARP network.
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::SystemInfoSerializer &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)