38# if !defined(WIN32_LEAN_AND_MEAN)
39# define WIN32_LEAN_AND_MEAN
49inline std::string lastError2String()
51 int error=GetLastError();
53 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
nullptr,
error, 0, buff, 1024,
nullptr);
55 return std::string(buff);
68std::string yarp::run::Run::mPortName;
70int yarp::run::Run::mProcCNT=0;
71bool yarp::run::Run::mStresstest=
false;
72bool yarp::run::Run::mLogged=
false;
73std::string yarp::run::Run::mLoggerPort(
"/yarplogger");
83 sprintf(msg,
"SIGNAL %d", sig);
96 for (
const auto& s_iter : ss)
99 if (sss.size() == 2 && sss[0] ==
"YARP_LOG_PROCESS_LABEL")
108int yarp::run::Run::main(
int argc,
char *argv[])
114 if (config.
check(
"server"))
116 mLogged=config.
check(
"log");
122 if (botPortLogger.
size()>1)
128 mPortName=std::string(config.
find(
"server").
asString());
138 fprintf(stderr,
"ERROR: no yarp network found.\n");
145 if (config.
check(
"readwrite"))
148 std::string fPortName;
149 std::string lPortName;
151 if (config.
check(
"forward"))
161#elif defined(__APPLE__)
164 struct sigaction new_action;
166 sigfillset(&new_action.sa_mask);
167 new_action.sa_flags=0;
169 sigaction(SIGTERM, &new_action,
nullptr);
170 sigaction(SIGHUP, &new_action,
nullptr);
173 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
175 if (getppid()==1)
return 0;
177 yarp::run::impl::prctl(PR_SET_PDEATHSIG, SIGTERM);
179 struct sigaction new_action;
181 yarp::os::impl::sigfillset(&new_action.sa_mask);
182 new_action.sa_flags=0;
184 yarp::os::impl::sigaction(SIGTERM, &new_action,
nullptr);
185 yarp::os::impl::signal(SIGHUP, SIG_IGN);
187 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
203 if (config.
check(
"write"))
212 struct sigaction new_action;
214 yarp::os::impl::sigfillset(&new_action.sa_mask);
215 new_action.sa_flags=0;
216 yarp::os::impl::sigaction(SIGTERM, &new_action,
nullptr);
218 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
219 yarp::os::impl::signal(SIGHUP, SIG_IGN);
222 if (config.
check(
"log"))
244 if (config.
check(
"read"))
255 yarp::os::impl::signal(SIGHUP, SIG_IGN);
267 if (config.
check(
"stresstest"))
269 fprintf(stderr,
"Yarprun stress test started.\n");
272 int max_interval_ms=config.
find(
"stresstest").
asInt32();
277 config.
unput(
"stresstest");
281 bool isCommand=
false;
283 if (config.
check(
"cmd"))
290 unsigned int t=0, u=0;
294 char cmd_and_name[512];
298 std::random_device rd;
299 std::mt19937 mt(rd());
300 std::uniform_int_distribution<int> dist0maxint(0, max_interval_ms -1);
308 sprintf(tag,
"%s_%u", tag_zero.c_str(), t++);
309 stresser.
put(
"as", tag);
313 sprintf(cmd_and_name,
"%s --name /%s", cmd.c_str(), tag);
314 stresser.
put(
"cmd", cmd_and_name);
319 std::uniform_int_distribution<int> dist07(0, 7);
320 if (isCommand && ++term_cycle>=4)
324 int r = t - (dist07(mt));
326 for (
int i=u; i<r; ++i)
328 sprintf(tag,
"%s_%u", tag_zero.c_str(), i);
349 if (config.
check(
"help"))
357 if (config.
check(
"stdio")
358 || config.
check(
"cmd")
359 || config.
check(
"kill")
360 || config.
check(
"sigterm")
361 || config.
check(
"sigtermall")
362 || config.
check(
"exit")
363 || config.
check(
"isrunning")
364 || config.
check(
"ps")
365 || config.
check(
"env")
366 || config.
check(
"sysinfo")
367 || config.
check(
"which"))
383 for (
int r=0; r<RETRY; ++r)
388 if (!port.
open(
"..."))
391 static std::random_device rd;
392 static std::mt19937 gen(rd());
393 static std::uniform_int_distribution<> dist(0, 99999);
394 int number = dist(gen);
395 std::ostringstream oss;
396 oss << std::setw(5) << std::setfill(
'0') << number;
397 std::string tempy =
"/yarprunclient/temp" + oss.str();
398 if (!port.
open(tempy))
402 fprintf(stderr,
"retrying...");
406 std::string tempportname = port.
getName();
413 fprintf(stderr,
"retrying...");
417 RUNLOG(
"<<<port.write(msg, response)")
418 if (!port.write(msg, response))
422 fprintf(stderr,
"retrying...");
425 RUNLOG(
">>>port.write(msg, response)")
429 fprintf(stderr, "RESPONSE:\n=========\n");
430 for (
size_t s=0; s<response.size(); ++s)
432 fprintf(stderr,
"%s\n", response.get(s).toString().c_str());
440 response.
addString(
"Cannot connect to remote server, aborting...\n");
441 for (
size_t s=0; s<response.
size(); ++s)
443 fprintf(stderr,
"%s\n", response.
get(s).
toString().c_str());
460 if (yarp::run::Run::mBraveZombieHunter)
482 std::string strStdioUUID=msg.
find(
"stdiouuid").
asString();
486 SECURITY_ATTRIBUTES pipe_sec_attr;
487 pipe_sec_attr.nLength=
sizeof(SECURITY_ATTRIBUTES);
488 pipe_sec_attr.bInheritHandle=TRUE;
489 pipe_sec_attr.lpSecurityDescriptor =
nullptr;
490 HANDLE read_from_pipe_stdin_to_cmd, write_to_pipe_stdin_to_cmd;
491 CreatePipe(&read_from_pipe_stdin_to_cmd, &write_to_pipe_stdin_to_cmd, &pipe_sec_attr, 0);
492 HANDLE read_from_pipe_cmd_to_stdout, write_to_pipe_cmd_to_stdout;
493 CreatePipe(&read_from_pipe_cmd_to_stdout, &write_to_pipe_cmd_to_stdout, &pipe_sec_attr, 0);
496 PROCESS_INFORMATION stdout_process_info;
497 ZeroMemory(&stdout_process_info,
sizeof(PROCESS_INFORMATION));
498 STARTUPINFO stdout_startup_info;
499 ZeroMemory(&stdout_startup_info,
sizeof(STARTUPINFO));
501 stdout_startup_info.cb=
sizeof(STARTUPINFO);
502 stdout_startup_info.hStdError=GetStdHandle(STD_ERROR_HANDLE);
503 stdout_startup_info.hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
504 stdout_startup_info.hStdInput=read_from_pipe_cmd_to_stdout;
505 stdout_startup_info.dwFlags|=STARTF_USESTDHANDLES;
507 BOOL bSuccess=CreateProcess(
nullptr,
508 (
char*)(std::string(
"yarprun --write ")+strStdioUUID).c_str(),
512 CREATE_NEW_PROCESS_GROUP,
515 &stdout_startup_info,
516 &stdout_process_info);
520 std::string strError=std::string(
"ABORTED: server=")+mPortName
521 +std::string(
" alias=")+strAlias
522 +std::string(
" cmd=stdout\n")
523 +std::string(
"Can't execute stdout because ")+lastError2String()
528 fprintf(stderr,
"%s", strError.c_str());
531 CloseHandle(write_to_pipe_stdin_to_cmd);
532 CloseHandle(read_from_pipe_stdin_to_cmd);
533 CloseHandle(write_to_pipe_cmd_to_stdout);
534 CloseHandle(read_from_pipe_cmd_to_stdout);
541 PROCESS_INFORMATION stdin_process_info;
542 ZeroMemory(&stdin_process_info,
sizeof(PROCESS_INFORMATION));
543 STARTUPINFO stdin_startup_info;
544 ZeroMemory(&stdin_startup_info,
sizeof(STARTUPINFO));
546 stdin_startup_info.cb=
sizeof(STARTUPINFO);
547 stdin_startup_info.hStdError=write_to_pipe_stdin_to_cmd;
548 stdin_startup_info.hStdOutput=write_to_pipe_stdin_to_cmd;
549 stdin_startup_info.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
550 stdin_startup_info.dwFlags|=STARTF_USESTDHANDLES;
552 bSuccess=CreateProcess(
nullptr,
553 (
char*)(std::string(
"yarprun --read ")+strStdioUUID).c_str(),
557 CREATE_NEW_PROCESS_GROUP,
561 &stdin_process_info);
565 std::string strError=std::string(
"ABORTED: server=")+mPortName
566 +std::string(
" alias=")+strAlias
567 +std::string(
" cmd=stdin\n")
568 +std::string(
"Can't execute stdin because ")+lastError2String()
573 fprintf(stderr,
"%s", strError.c_str());
575 TerminateProcess(stdout_process_info.hProcess,
YARPRUN_ERROR);
577 CloseHandle(stdout_process_info.hProcess);
579 CloseHandle(write_to_pipe_stdin_to_cmd);
580 CloseHandle(read_from_pipe_stdin_to_cmd);
581 CloseHandle(write_to_pipe_cmd_to_stdout);
582 CloseHandle(read_from_pipe_cmd_to_stdout);
589 PROCESS_INFORMATION cmd_process_info;
590 ZeroMemory(&cmd_process_info,
sizeof(PROCESS_INFORMATION));
591 STARTUPINFO cmd_startup_info;
592 ZeroMemory(&cmd_startup_info,
sizeof(STARTUPINFO));
594 cmd_startup_info.cb=
sizeof(STARTUPINFO);
595 cmd_startup_info.hStdError=write_to_pipe_cmd_to_stdout;
596 cmd_startup_info.hStdOutput=write_to_pipe_cmd_to_stdout;
597 cmd_startup_info.hStdInput=read_from_pipe_stdin_to_cmd;
598 cmd_startup_info.dwFlags|=STARTF_USESTDHANDLES;
603 for (
int s=0; s<botCmd.
size(); ++s)
611 TCHAR chNewEnv[32767];
614 LPTCH chOldEnv = GetEnvironmentStrings();
617 LPTSTR lpOld = (LPTSTR) chOldEnv;
618 LPTSTR lpNew = (LPTSTR) chNewEnv;
621 lstrcpy(lpNew, lpOld);
622 lpOld += lstrlen(lpOld) + 1;
623 lpNew += lstrlen(lpNew) + 1;
628 lstrcpy(lpNew, (LPTCH)
"YARP_IS_YARPRUN=1");
629 lpNew += lstrlen(lpNew) + 1;
633 lstrcpy(lpNew, (LPTCH)
"YARPRUN_IS_FORWARDING_LOG=1");
634 lpNew += lstrlen(lpNew) + 1;
637 std::string cstrEnvName;
638 if (msg.
check(
"env"))
641 for (
const auto& s : ss) {
642 lstrcpy(lpNew, (LPTCH) s.c_str());
643 lpNew += lstrlen(lpNew) + 1;
650 bool bWorkdir=msg.
check(
"workdir");
651 std::string strWorkdir=bWorkdir?msg.
find(
"workdir").
asString()+
"\\":
"";
653 bSuccess=CreateProcess(
nullptr,
654 (
char*)(strWorkdir+strCmd).c_str(),
658 CREATE_NEW_PROCESS_GROUP,
660 bWorkdir ? strWorkdir.c_str() : nullptr,
664 if (!bSuccess && bWorkdir)
666 bSuccess=CreateProcess(
nullptr,
667 (
char*)(strCmd.c_str()),
671 CREATE_NEW_PROCESS_GROUP,
679 FreeEnvironmentStrings(chOldEnv);
686 std::string line1=std::string(
"ABORTED: server=")+mPortName
687 +std::string(
" alias=")+strAlias
688 +std::string(
" cmd=")+strCmd
689 +std::string(
"pid=")+
int2String(cmd_process_info.dwProcessId)
692 WriteFile(write_to_pipe_cmd_to_stdout, line1.c_str(), line1.length(), &nBytes, 0);
694 std::string line2=std::string(
"Can't execute command because ")+lastError2String()+std::string(
"\n");
695 WriteFile(write_to_pipe_cmd_to_stdout, line1.c_str(), line2.length(), &nBytes, 0);
696 FlushFileBuffers(write_to_pipe_cmd_to_stdout);
698 std::string out=line1+line2;
700 fprintf(stderr,
"%s", out.c_str());
703 CloseHandle(write_to_pipe_stdin_to_cmd);
704 CloseHandle(read_from_pipe_stdin_to_cmd);
705 CloseHandle(write_to_pipe_cmd_to_stdout);
706 CloseHandle(read_from_pipe_cmd_to_stdout);
708 TerminateProcess(stdout_process_info.hProcess,
YARPRUN_ERROR);
710 CloseHandle(stdout_process_info.hProcess);
712 TerminateProcess(stdin_process_info.hProcess,
YARPRUN_ERROR);
714 CloseHandle(stdin_process_info.hProcess);
719 FlushFileBuffers(write_to_pipe_cmd_to_stdout);
725 cmd_process_info.dwProcessId,
728 stdin_process_info.dwProcessId,
729 stdout_process_info.dwProcessId,
730 read_from_pipe_stdin_to_cmd,
731 write_to_pipe_stdin_to_cmd,
732 read_from_pipe_cmd_to_stdout,
733 write_to_pipe_cmd_to_stdout,
734 cmd_process_info.hProcess,
738 if (msg.
check(
"env"))
742 mProcessVector.Add(pInf);
744 result.
addInt32(cmd_process_info.dwProcessId);
745 std::string out=std::string(
"STARTED: server=")+mPortName
746 +std::string(
" alias=")+strAlias
747 +std::string(
" cmd=")+strCmd
748 +std::string(
" pid=")+
int2String(cmd_process_info.dwProcessId)
753 fprintf(stderr,
"%s", out.c_str());
755 return cmd_process_info.dwProcessId;
763 std::string portName=
"/log";
764 portName+=mPortName+
"/";
766 command = command.substr(0, command.find(
' '));
767 command = command.substr(command.find_last_of(
"\\/") + 1);
769 if (proc_label !=
"") { portName +=
"[" + proc_label +
"]"; }
772 SECURITY_ATTRIBUTES pipe_sec_attr;
773 pipe_sec_attr.nLength=
sizeof(SECURITY_ATTRIBUTES);
774 pipe_sec_attr.bInheritHandle=TRUE;
775 pipe_sec_attr.lpSecurityDescriptor =
nullptr;
776 HANDLE read_from_pipe_cmd_to_stdout, write_to_pipe_cmd_to_stdout;
777 CreatePipe(&read_from_pipe_cmd_to_stdout, &write_to_pipe_cmd_to_stdout, &pipe_sec_attr, 0);
780 PROCESS_INFORMATION stdout_process_info;
781 ZeroMemory(&stdout_process_info,
sizeof(PROCESS_INFORMATION));
782 STARTUPINFO stdout_startup_info;
783 ZeroMemory(&stdout_startup_info,
sizeof(STARTUPINFO));
785 stdout_startup_info.cb=
sizeof(STARTUPINFO);
786 stdout_startup_info.hStdError=GetStdHandle(STD_ERROR_HANDLE);
787 stdout_startup_info.hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
788 stdout_startup_info.hStdInput=read_from_pipe_cmd_to_stdout;
789 stdout_startup_info.dwFlags|=STARTF_USESTDHANDLES;
791 BOOL bSuccess=CreateProcess(
nullptr,
792 (
char*)(std::string(
"yarprun --log ")+loggerName+std::string(
" --write ")+portName).c_str(),
796 CREATE_NEW_PROCESS_GROUP,
799 &stdout_startup_info,
800 &stdout_process_info);
804 std::string strError=std::string(
"ABORTED: server=")+mPortName
805 +std::string(
" alias=")+strAlias
806 +std::string(
" cmd=stdout\n")
807 +std::string(
"Can't execute stdout because ")+lastError2String()
812 fprintf(stderr,
"%s", strError.c_str());
815 CloseHandle(write_to_pipe_cmd_to_stdout);
816 CloseHandle(read_from_pipe_cmd_to_stdout);
823 PROCESS_INFORMATION cmd_process_info;
824 ZeroMemory(&cmd_process_info,
sizeof(PROCESS_INFORMATION));
825 STARTUPINFO cmd_startup_info;
826 ZeroMemory(&cmd_startup_info,
sizeof(STARTUPINFO));
828 cmd_startup_info.cb=
sizeof(STARTUPINFO);
829 cmd_startup_info.hStdError=write_to_pipe_cmd_to_stdout;
830 cmd_startup_info.hStdOutput=write_to_pipe_cmd_to_stdout;
831 cmd_startup_info.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
832 cmd_startup_info.dwFlags|=STARTF_USESTDHANDLES;
837 for (
int s=0; s<botCmd.
size(); ++s)
845 TCHAR chNewEnv[32767];
848 LPTCH chOldEnv = GetEnvironmentStrings();
851 LPTSTR lpOld = (LPTSTR) chOldEnv;
852 LPTSTR lpNew = (LPTSTR) chNewEnv;
855 lstrcpy(lpNew, lpOld);
856 lpOld += lstrlen(lpOld) + 1;
857 lpNew += lstrlen(lpNew) + 1;
862 lstrcpy(lpNew, (LPTCH)
"YARP_IS_YARPRUN=1");
863 lpNew += lstrlen(lpNew) + 1;
867 lstrcpy(lpNew, (LPTCH)
"YARPRUN_IS_FORWARDING_LOG=1");
868 lpNew += lstrlen(lpNew) + 1;
871 std::string cstrEnvName;
872 if (msg.
check(
"env"))
875 for (
const auto& s : ss) {
876 lstrcpy(lpNew, (LPTCH)s.c_str());
877 lpNew += lstrlen(lpNew) + 1;
884 bool bWorkdir=msg.
check(
"workdir");
885 std::string strWorkdir=bWorkdir?msg.
find(
"workdir").
asString()+
"\\":
"";
887 bSuccess=CreateProcess(
nullptr,
888 (
char*)(strWorkdir+strCmd).c_str(),
892 CREATE_NEW_PROCESS_GROUP,
894 bWorkdir?strWorkdir.c_str():nullptr,
898 if (!bSuccess && bWorkdir)
900 bSuccess=CreateProcess(
nullptr,
901 (
char*)(strCmd.c_str()),
905 CREATE_NEW_PROCESS_GROUP,
913 FreeEnvironmentStrings(chOldEnv);
920 std::string line1=std::string(
"ABORTED: server=")+mPortName
921 +std::string(
" alias=")+strAlias
922 +std::string(
" cmd=")+strCmd
923 +std::string(
"pid=")+
int2String(cmd_process_info.dwProcessId)
926 WriteFile(write_to_pipe_cmd_to_stdout, line1.c_str(), line1.length(), &nBytes, 0);
928 std::string line2=std::string(
"Can't execute command because ")+lastError2String()+std::string(
"\n");
929 WriteFile(write_to_pipe_cmd_to_stdout, line1.c_str(), line2.length(), &nBytes, 0);
930 FlushFileBuffers(write_to_pipe_cmd_to_stdout);
932 std::string out=line1+line2;
934 fprintf(stderr,
"%s", out.c_str());
937 CloseHandle(write_to_pipe_cmd_to_stdout);
938 CloseHandle(read_from_pipe_cmd_to_stdout);
940 TerminateProcess(stdout_process_info.hProcess,
YARPRUN_ERROR);
942 CloseHandle(stdout_process_info.hProcess);
947 FlushFileBuffers(write_to_pipe_cmd_to_stdout);
953 cmd_process_info.dwProcessId,
954 stdout_process_info.dwProcessId,
955 read_from_pipe_cmd_to_stdout,
956 write_to_pipe_cmd_to_stdout,
957 cmd_process_info.hProcess,
964 if (msg.
check(
"env"))
968 mProcessVector.Add(pInf);
970 result.
addInt32(cmd_process_info.dwProcessId);
971 std::string out=std::string(
"STARTED: server=")+mPortName
972 +std::string(
" alias=")+strAlias
973 +std::string(
" cmd=")+strCmd
974 +std::string(
" pid=")+
int2String(cmd_process_info.dwProcessId)
979 fprintf(stderr,
"%s", out.c_str());
981 return cmd_process_info.dwProcessId;
990 PROCESS_INFORMATION cmd_process_info;
991 ZeroMemory(&cmd_process_info,
sizeof(PROCESS_INFORMATION));
992 STARTUPINFO cmd_startup_info;
993 ZeroMemory(&cmd_startup_info,
sizeof(STARTUPINFO));
995 cmd_startup_info.cb=
sizeof(STARTUPINFO);
1000 for (
int s=0; s<botCmd.
size(); ++s)
1002 strCmd+=botCmd.
get(s).
toString()+std::string(
" ");
1008 TCHAR chNewEnv[32767];
1011 LPTCH chOldEnv = GetEnvironmentStrings();
1014 LPTSTR lpOld = (LPTSTR) chOldEnv;
1015 LPTSTR lpNew = (LPTSTR) chNewEnv;
1018 lstrcpy(lpNew, lpOld);
1019 lpOld += lstrlen(lpOld) + 1;
1020 lpNew += lstrlen(lpNew) + 1;
1025 lstrcpy(lpNew, (LPTCH)
"YARP_IS_YARPRUN=1");
1026 lpNew += lstrlen(lpNew) + 1;
1030 lstrcpy(lpNew, (LPTCH)
"YARPRUN_IS_FORWARDING_LOG=0");
1031 lpNew += lstrlen(lpNew) + 1;
1034 std::string cstrEnvName;
1035 if (msg.
check(
"env"))
1038 for (
const auto& s : ss) {
1039 lstrcpy(lpNew, (LPTCH)s.c_str());
1040 lpNew += lstrlen(lpNew) + 1;
1047 bool bWorkdir=msg.
check(
"workdir");
1048 std::string strWorkdir=bWorkdir?msg.
find(
"workdir").
asString()+
"\\":
"";
1050 BOOL bSuccess=CreateProcess(
nullptr,
1051 (
char*)(strWorkdir+strCmd).c_str(),
1055 CREATE_NEW_PROCESS_GROUP,
1057 bWorkdir ? strWorkdir.c_str() : nullptr,
1061 if (!bSuccess && bWorkdir)
1063 bSuccess=CreateProcess(
nullptr,
1064 (
char*)(strCmd.c_str()),
1068 CREATE_NEW_PROCESS_GROUP,
1076 FreeEnvironmentStrings(chOldEnv);
1082 std::string out=std::string(
"ABORTED: server=")+mPortName
1083 +std::string(
" alias=")+strAlias
1084 +std::string(
" cmd=")+strCmd
1085 +std::string(
" pid=")+
int2String(cmd_process_info.dwProcessId)
1086 +std::string(
"\nCan't execute command because ")+lastError2String()
1090 fprintf(stderr,
"%s", out.c_str());
1099 cmd_process_info.dwProcessId,
1100 cmd_process_info.hProcess,
1103 if (msg.
check(
"env"))
1106 mProcessVector.Add(pInf);
1108 result.
addInt32(cmd_process_info.dwProcessId);
1109 std::string out=std::string(
"STARTED: server=")+mPortName
1110 +std::string(
" alias=")+strAlias
1111 +std::string(
" cmd=")+strCmd
1112 +std::string(
" pid=")+
int2String(cmd_process_info.dwProcessId)
1115 fprintf(stderr,
"%s", out.c_str());
1117 return cmd_process_info.dwProcessId;
1123 PROCESS_INFORMATION stdio_process_info;
1124 ZeroMemory(&stdio_process_info,
sizeof(PROCESS_INFORMATION));
1126 STARTUPINFO stdio_startup_info;
1127 ZeroMemory(&stdio_startup_info,
sizeof(STARTUPINFO));
1128 stdio_startup_info.cb=
sizeof(STARTUPINFO);
1129 stdio_startup_info.wShowWindow=SW_SHOWNOACTIVATE;
1130 stdio_startup_info.dwFlags=STARTF_USESHOWWINDOW;
1134 std::string strCmd=std::string(
"yarprun --readwrite ")+strUUID;
1137 BOOL bSuccess=CreateProcess(
nullptr,
1138 (
char*)strCmd.c_str(),
1145 &stdio_startup_info,
1146 &stdio_process_info);
1154 stdio_process_info.dwProcessId,
1155 stdio_process_info.hProcess,
1158 out=std::string(
"STARTED: server=")+mPortName
1159 +std::string(
" alias=")+strAlias
1160 +std::string(
" cmd=stdio pid=")+
int2String(stdio_process_info.dwProcessId)
1167 out=std::string(
"ABORTED: server=")+mPortName
1168 +std::string(
" alias=")+strAlias
1169 +std::string(
" cmd=stdio\n")
1170 +std::string(
"Can't open stdio window because ")+lastError2String()
1175 result.
addInt32(stdio_process_info.dwProcessId);
1177 fprintf(stderr,
"%s", out.c_str());
1180 return stdio_process_info.dwProcessId;
1191 char *pTmp = strchr(pLine,
' ');
1196 while ((*pTmp) && (*pTmp ==
' ')) {
1199 if (*pTmp ==
'\0') {
1211 char *pNext = io_pLine;
1215 size_t len = strlen(io_pLine);
1218 for(i = 0; i < len; i++) {
1219 if ((!quoted) && (
'"' == io_pLine[i])) {
1222 }
else if ((quoted) && (
'"' == io_pLine[i])) {
1225 }
else if ((quoted) && (
' ' == io_pLine[i])) {
1231 memset(o_pArgv, 0x00,
sizeof(
char*) *
C_MAXARGS);
1233 o_pArgv[0] = io_pLine;
1235 while ((
nullptr != pNext) && (*o_pArgc <
C_MAXARGS)) {
1237 pNext = o_pArgv[*o_pArgc];
1239 if (
nullptr != o_pArgv[*o_pArgc]) {
1244 for(j = 0; j < *o_pArgc; j++) {
1245 len = strlen(o_pArgv[j]);
1246 for(i = 0; i < len; i++) {
1247 if (
'\1' == o_pArgv[j][i]) {
1248 o_pArgv[j][i] =
' ';
1254void yarp::run::Run::CleanZombie(
int pid)
1256 bool bFound=mProcessVector && mProcessVector->CleanZombie(pid);
1260 mStdioVector->CleanZombie(pid);
1272 std::string strStdioUUID=msg.
find(
"stdiouuid").
asString();
1274 int pipe_stdin_to_cmd[2];
1275 int ret_stdin_to_cmd=yarp::run::impl::pipe(pipe_stdin_to_cmd);
1277 int pipe_cmd_to_stdout[2];
1278 int ret_cmd_to_stdout=yarp::run::impl::pipe(pipe_cmd_to_stdout);
1280 int pipe_child_to_parent[2];
1281 int ret_child_to_parent=yarp::run::impl::pipe(pipe_child_to_parent);
1283 if (ret_child_to_parent!=0 || ret_cmd_to_stdout!=0 || ret_stdin_to_cmd!=0)
1287 std::string out=std::string(
"ABORTED: server=")+mPortName
1288 +std::string(
" alias=")+strAlias
1289 +std::string(
" cmd=stdout\n")
1290 +std::string(
"Can't create pipes ")+strerror(
error)
1295 fprintf(stderr,
"%s", out.c_str());
1300 int pid_stdout=yarp::run::impl::fork();
1302 if (IS_INVALID(pid_stdout))
1313 std::string out=std::string(
"ABORTED: server=")+mPortName
1314 +std::string(
" alias=")+strAlias
1315 +std::string(
" cmd=stdout\n")
1316 +std::string(
"Can't fork stdout process because ")+strerror(
error)
1321 fprintf(stderr,
"%s", out.c_str());
1326 if (IS_NEW_PROCESS(pid_stdout))
1342 int ret = yarp::run::impl::execlp(
"yarprun",
"yarprun",
"--write", strStdioUUID.c_str(),
static_cast<char*
>(
nullptr));
1350 std::string out=std::string(
"ABORTED: server=")+mPortName
1351 +std::string(
" alias=")+strAlias
1352 +std::string(
" cmd=stdout\n")
1353 +std::string(
"Can't execute stdout because ")+strerror(
error)
1356 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1357 fprintf(out_to_parent,
"%s", out.c_str());
1358 fflush(out_to_parent);
1359 fclose(out_to_parent);
1361 fprintf(stderr,
"%s", out.c_str());
1369 if (IS_PARENT_OF(pid_stdout))
1373 fprintf(stderr,
"STARTED: server=%s alias=%s cmd=stdout pid=%d\n", mPortName.c_str(), strAlias.c_str(), pid_stdout);
1375 int pid_stdin=yarp::run::impl::fork();
1377 if (IS_INVALID(pid_stdin))
1387 std::string out=std::string(
"ABORTED: server=")+mPortName
1388 +std::string(
" alias=")+strAlias
1389 +std::string(
" cmd=stdin\n")
1390 +std::string(
"Can't fork stdin process because ")+strerror(
error)
1395 fprintf(stderr,
"%s", out.c_str());
1397 SIGNAL(pid_stdout, SIGTERM);
1398 fprintf(stderr,
"TERMINATING stdout (%d)\n", pid_stdout);
1403 if (IS_NEW_PROCESS(pid_stdin))
1406 REDIRECT_TO(STDOUT_FILENO, pipe_stdin_to_cmd[
WRITE_TO_PIPE]);
1407 REDIRECT_TO(STDERR_FILENO, pipe_stdin_to_cmd[
WRITE_TO_PIPE]);
1420 int ret = yarp::run::impl::execlp(
"yarprun",
"yarprun",
"--read", strStdioUUID.c_str(),
static_cast<char*
>(
nullptr));
1428 std::string out=std::string(
"ABORTED: server=")+mPortName
1429 +std::string(
" alias=")+strAlias
1430 +std::string(
" cmd=stdin\n")
1431 +std::string(
"Can't execute stdin because ")+strerror(
error)
1435 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1436 fprintf(out_to_parent,
"%s", out.c_str());
1437 fflush(out_to_parent);
1438 fclose(out_to_parent);
1439 fprintf(stderr,
"%s", out.c_str());
1447 if (IS_PARENT_OF(pid_stdin))
1452 fprintf(stderr,
"STARTED: server=%s alias=%s cmd=stdin pid=%d\n", mPortName.c_str(), strAlias.c_str(), pid_stdin);
1454 int pid_cmd=yarp::run::impl::fork();
1456 if (IS_INVALID(pid_cmd))
1464 std::string out=std::string(
"ABORTED: server=")+mPortName
1465 +std::string(
" alias=")+strAlias
1466 +std::string(
" cmd=")+strCmd
1467 +std::string(
"\nCan't fork command process because ")+strerror(
error)
1472 fprintf(stderr,
"%s", out.c_str());
1474 FILE* to_yarp_stdout=fdopen(pipe_cmd_to_stdout[
WRITE_TO_PIPE],
"w");
1475 fprintf(to_yarp_stdout,
"%s", out.c_str());
1476 fflush(to_yarp_stdout);
1477 fclose(to_yarp_stdout);
1479 SIGNAL(pid_stdout, SIGTERM);
1480 fprintf(stderr,
"TERMINATING stdout (%d)\n", pid_stdout);
1481 SIGNAL(pid_stdin, SIGTERM);
1482 fprintf(stderr,
"TERMINATING stdin (%d)\n", pid_stdin);
1489 if (IS_NEW_PROCESS(pid_cmd))
1493 char *cmd_str=
new char[strCmd.length()+1];
1494 strcpy(cmd_str, strCmd.c_str());
1502 char **arg_str =
new char*[
C_MAXARGS + 1];
1504 arg_str[nargs]=
nullptr;
1506 setvbuf(stdout,
nullptr, _IONBF, 0);
1509 REDIRECT_TO(STDOUT_FILENO, pipe_cmd_to_stdout[
WRITE_TO_PIPE]);
1510 REDIRECT_TO(STDERR_FILENO, pipe_cmd_to_stdout[
WRITE_TO_PIPE]);
1520 if (msg.
check(
"env"))
1523 for (
const auto& s : ss) {
1524 char* szenv =
new char[s.size()+1];
1525 strcpy(szenv, s.c_str());
1526 yarp::run::impl::putenv(szenv);
1531 if (msg.
check(
"workdir"))
1533 int ret = yarp::os::impl::chdir(msg.
find(
"workdir").
asString().c_str());
1539 std::string out=std::string(
"ABORTED: server=")+mPortName
1540 +std::string(
" alias=")+strAlias
1541 +std::string(
" cmd=")+strCmd
1542 +std::string(
"\nCan't execute command, cannot set working directory ")+strerror(
error)
1545 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1546 fprintf(out_to_parent,
"%s", out.c_str());
1547 fflush(out_to_parent);
1548 fclose(out_to_parent);
1549 fprintf(stderr,
"%s", out.c_str());
1557 char currWorkDirBuff[1024];
1558 char *currWorkDir=yarp::os::impl::getcwd(currWorkDirBuff, 1024);
1562 char **cwd_arg_str=
new char*[nargs+1];
1563 for (
int i = 1; i < nargs; ++i) {
1564 cwd_arg_str[i] = arg_str[i];
1566 cwd_arg_str[nargs]=
nullptr;
1567 cwd_arg_str[0]=
new char[strlen(currWorkDir)+strlen(arg_str[0])+16];
1569 strcpy(cwd_arg_str[0], currWorkDir);
1570 strcat(cwd_arg_str[0],
"/");
1571 strcat(cwd_arg_str[0], arg_str[0]);
1578 ret = yarp::run::impl::execvp(cwd_arg_str[0], cwd_arg_str);
1580 delete [] cwd_arg_str[0];
1581 delete [] cwd_arg_str;
1591 ret = yarp::run::impl::execvp(arg_str[0], arg_str);
1603 std::string out=std::string(
"ABORTED: server=")+mPortName
1604 +std::string(
" alias=")+strAlias
1605 +std::string(
" cmd=")+strCmd
1606 +std::string(
"\nCan't execute command because ")+strerror(
error)
1609 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1610 fprintf(out_to_parent,
"%s", out.c_str());
1611 fflush(out_to_parent);
1612 fclose(out_to_parent);
1613 fprintf(stderr,
"%s", out.c_str());
1625 if (IS_PARENT_OF(pid_cmd))
1650 if (msg.
check(
"env"))
1655 mProcessVector->Add(pInf);
1659 FILE* in_from_child=fdopen(pipe_child_to_parent[
READ_FROM_PIPE],
"r");
1660 int flags=fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_GETFL, 0);
1661 fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_SETFL, flags|O_NONBLOCK);
1671 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
1675 out+=std::string(buff);
1678 fclose(in_from_child);
1687 out=std::string(
"STARTED: server=")+mPortName
1688 +std::string(
" alias=")+strAlias
1689 +std::string(
" cmd=")+strCmd
1698 fprintf(stderr,
"%s", out.c_str());
1708 result.
addString(
"I should never reach this point!!!\n");
1720 std::string portName=
"/log";
1721 portName+=mPortName+
"/";
1723 std::string command = strCmd;
1724 command = command.substr(0, command.find(
' '));
1725 command = command.substr(command.find_last_of(
"\\/") + 1);
1728 if (proc_label !=
"") { portName +=
"[" + proc_label +
"]"; }
1731 int pipe_cmd_to_stdout[2];
1732 int ret_cmd_to_stdout=yarp::run::impl::pipe(pipe_cmd_to_stdout);
1734 int pipe_child_to_parent[2];
1735 int ret_child_to_parent=yarp::run::impl::pipe(pipe_child_to_parent);
1737 if (ret_child_to_parent!=0 || ret_cmd_to_stdout!=0)
1741 std::string out=std::string(
"ABORTED: server=")+mPortName
1742 +std::string(
" alias=")+strAlias
1743 +std::string(
" cmd=stdout\n")
1744 +std::string(
"Can't create pipes ")+strerror(
error)
1749 fprintf(stderr,
"%s", out.c_str());
1754 int pid_stdout=yarp::run::impl::fork();
1756 if (IS_INVALID(pid_stdout))
1765 std::string out=std::string(
"ABORTED: server=")+mPortName
1766 +std::string(
" alias=")+strAlias
1767 +std::string(
" cmd=stdout\n")
1768 +std::string(
"Can't fork stdout process because ")+strerror(
error)
1773 fprintf(stderr,
"%s", out.c_str());
1778 if (IS_NEW_PROCESS(pid_stdout))
1792 int ret = yarp::run::impl::execlp(
"yarprun",
"yarprun",
"--write", portName.c_str(),
"--log", loggerName.c_str(),
static_cast<char*
>(
nullptr));
1800 std::string out=std::string(
"ABORTED: server=")+mPortName
1801 +std::string(
" alias=")+strAlias
1802 +std::string(
" cmd=stdout\n")
1803 +std::string(
"Can't execute stdout because ")+strerror(
error)
1806 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1807 fprintf(out_to_parent,
"%s", out.c_str());
1808 fflush(out_to_parent);
1809 fclose(out_to_parent);
1811 fprintf(stderr,
"%s", out.c_str());
1819 if (IS_PARENT_OF(pid_stdout))
1823 fprintf(stderr,
"STARTED: server=%s alias=%s cmd=stdout pid=%d\n", mPortName.c_str(), strAlias.c_str(), pid_stdout);
1829 int pid_cmd=yarp::run::impl::fork();
1831 if (IS_INVALID(pid_cmd))
1838 std::string out=std::string(
"ABORTED: server=")+mPortName
1839 +std::string(
" alias=")+strAlias
1840 +std::string(
" cmd=")+strCmd
1841 +std::string(
"\nCan't fork command process because ")+strerror(
error)
1846 fprintf(stderr,
"%s", out.c_str());
1848 FILE* to_yarp_stdout=fdopen(pipe_cmd_to_stdout[
WRITE_TO_PIPE],
"w");
1849 fprintf(to_yarp_stdout,
"%s", out.c_str());
1850 fflush(to_yarp_stdout);
1851 fclose(to_yarp_stdout);
1853 SIGNAL(pid_stdout, SIGTERM);
1854 fprintf(stderr,
"TERMINATING stdout (%d)\n", pid_stdout);
1860 if (IS_NEW_PROCESS(pid_cmd))
1864 char *cmd_str=
new char[strCmd.length()+1];
1865 strcpy(cmd_str, strCmd.c_str());
1873 char **arg_str =
new char*[
C_MAXARGS + 1];
1875 arg_str[nargs]=
nullptr;
1877 setvbuf(stdout,
nullptr, _IONBF, 0);
1879 REDIRECT_TO(STDOUT_FILENO, pipe_cmd_to_stdout[
WRITE_TO_PIPE]);
1880 REDIRECT_TO(STDERR_FILENO, pipe_cmd_to_stdout[
WRITE_TO_PIPE]);
1890 if (msg.
check(
"env"))
1893 for (
const auto& s : ss) {
1894 char* szenv =
new char[s.size()+1];
1895 strcpy(szenv, s.c_str());
1896 yarp::run::impl::putenv(szenv);
1901 if (msg.
check(
"workdir"))
1903 int ret = yarp::os::impl::chdir(msg.
find(
"workdir").
asString().c_str());
1909 std::string out=std::string(
"ABORTED: server=")+mPortName
1910 +std::string(
" alias=")+strAlias
1911 +std::string(
" cmd=")+strCmd
1912 +std::string(
"\nCan't execute command, cannot set working directory ")+strerror(
error)
1915 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1916 fprintf(out_to_parent,
"%s", out.c_str());
1917 fflush(out_to_parent);
1918 fclose(out_to_parent);
1919 fprintf(stderr,
"%s", out.c_str());
1927 char currWorkDirBuff[1024];
1928 char *currWorkDir=
getcwd(currWorkDirBuff, 1024);
1932 char **cwd_arg_str=
new char*[nargs+1];
1933 for (
int i = 1; i < nargs; ++i) {
1934 cwd_arg_str[i] = arg_str[i];
1936 cwd_arg_str[nargs]=
nullptr;
1937 cwd_arg_str[0]=
new char[strlen(currWorkDir)+strlen(arg_str[0])+16];
1939 strcpy(cwd_arg_str[0], currWorkDir);
1940 strcat(cwd_arg_str[0],
"/");
1941 strcat(cwd_arg_str[0], arg_str[0]);
1948 ret = yarp::run::impl::execvp(cwd_arg_str[0], cwd_arg_str);
1950 delete [] cwd_arg_str[0];
1951 delete [] cwd_arg_str;
1961 ret = yarp::run::impl::execvp(arg_str[0], arg_str);
1972 std::string out=std::string(
"ABORTED: server=")+mPortName
1973 +std::string(
" alias=")+strAlias
1974 +std::string(
" cmd=")+strCmd
1975 +std::string(
"\nCan't execute command because ")+strerror(
error)
1978 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1979 fprintf(out_to_parent,
"%s", out.c_str());
1980 fflush(out_to_parent);
1981 fclose(out_to_parent);
1982 fprintf(stderr,
"%s", out.c_str());
1994 if (IS_PARENT_OF(pid_cmd))
2013 if (msg.
check(
"env"))
2018 mProcessVector->Add(pInf);
2022 FILE* in_from_child=fdopen(pipe_child_to_parent[
READ_FROM_PIPE],
"r");
2023 int flags=fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_GETFL, 0);
2024 fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_SETFL, flags|O_NONBLOCK);
2034 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
2038 out+=std::string(buff);
2041 fclose(in_from_child);
2050 out=std::string(
"STARTED: server=")+mPortName
2051 +std::string(
" alias=")+strAlias
2052 +std::string(
" cmd=")+strCmd
2060 fprintf(stderr,
"%s", out.c_str());
2070 result.
addString(
"I should never reach this point!!!\n");
2082 if (msg.
check(
"forward"))
2084 strCmd=std::string(
"/bin/bash -l -c \"yarprun --readwrite ")+strUUID
2089 strCmd=std::string(
"/bin/bash -l -c \"yarprun --readwrite ")+strUUID+
"\"";
2092 int pipe_child_to_parent[2];
2094 if (yarp::run::impl::pipe(pipe_child_to_parent))
2098 std::string out=std::string(
"ABORTED: server=")+mPortName
2099 +std::string(
" alias=")+strAlias
2100 +std::string(
" cmd=stdio\nCan't create pipe ")+strerror(
error)
2106 fprintf(stderr,
"%s", out.c_str());
2113 for (
auto & i : command) {
2117 cmdcpy(command[c++],
"xterm");
2118 cmdcpy(command[c++], msg.
check(
"hold")?
"-hold":
"+hold");
2120 if (msg.
check(
"geometry"))
2122 cmdcpy(command[c++],
"-geometry");
2123 cmdcpy(command[c++], msg.
find(
"geometry").
asString().c_str());
2126 cmdcpy(command[c++],
"-title");
2127 cmdcpy(command[c++], strAlias.c_str());
2129 cmdcpy(command[c++],
"-e");
2130 cmdcpy(command[c++], strCmd.c_str());
2132 int pid_cmd=yarp::run::impl::fork();
2134 if (IS_INVALID(pid_cmd))
2138 std::string out=std::string(
"ABORTED: server=")+mPortName
2139 +std::string(
" alias=")+strAlias
2140 +std::string(
" cmd=stdio\nCan't fork stdout process because ")+strerror(
error)
2146 fprintf(stderr,
"%s", out.c_str());
2156 if (IS_NEW_PROCESS(pid_cmd))
2162 REDIRECT_TO(STDERR_FILENO, pipe_child_to_parent[
WRITE_TO_PIPE]);
2171 int ret = yarp::run::impl::execvp(
"xterm", command);
2179 std::string out=std::string(
"ABORTED: server=")+mPortName
2180 +std::string(
" alias=")+strAlias
2181 +std::string(
" cmd=xterm\nCan't execute command because ")+strerror(
error)
2184 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
2186 fprintf(out_to_parent,
"%s", out.c_str());
2187 fflush(out_to_parent);
2188 fclose(out_to_parent);
2190 fprintf(stderr,
"%s", out.c_str());
2198 if (IS_PARENT_OF(pid_cmd))
2202 mStdioVector->Add(
new YarpRunProcInfo(strAlias, mPortName, pid_cmd,
nullptr, msg.
check(
"hold")));
2210 FILE* in_from_child=fdopen(pipe_child_to_parent[
READ_FROM_PIPE],
"r");
2211 int flags=fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_GETFL, 0);
2212 fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_SETFL, flags|O_NONBLOCK);
2221 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
2225 out+=std::string(buff);
2228 fclose(in_from_child);
2234 if (out.substr(0, 14)==
"xterm Xt error" || out.substr(0, 7)==
"ABORTED")
2240 out=std::string(
"STARTED: server=")+mPortName
2241 +std::string(
" alias=")+strAlias
2242 +std::string(
" cmd=xterm pid=")+
int2String(pid_cmd)
2247 fprintf(stderr,
"%s", out.c_str());
2268 int pipe_child_to_parent[2];
2269 int ret_pipe_child_to_parent=yarp::run::impl::pipe(pipe_child_to_parent);
2271 if (ret_pipe_child_to_parent!=0)
2275 std::string out=std::string(
"ABORTED: server=")+mPortName
2276 +std::string(
" alias=")+strAlias
2277 +std::string(
" cmd=stdio\nCan't create pipe ")+strerror(
error)
2283 fprintf(stderr,
"%s", out.c_str());
2288 int pid_cmd=yarp::run::impl::fork();
2290 if (IS_INVALID(pid_cmd))
2294 std::string out=std::string(
"ABORTED: server=")+mPortName
2295 +std::string(
" alias=")+strAlias
2296 +std::string(
" cmd=")+strCmd
2297 +std::string(
"\nCan't fork command process because ")+strerror(
error)
2302 fprintf(stderr,
"%s", out.c_str());
2307 if (IS_NEW_PROCESS(pid_cmd))
2309 int saved_stderr = yarp::run::impl::dup(STDERR_FILENO);
2310 int null_file=open(
"/dev/null", O_WRONLY);
2313 REDIRECT_TO(STDOUT_FILENO, null_file);
2314 REDIRECT_TO(STDERR_FILENO, null_file);
2317 char *cmd_str=
new char[strCmd.length()+1];
2318 strcpy(cmd_str, strCmd.c_str());
2326 char **arg_str =
new char*[
C_MAXARGS + 1];
2328 arg_str[nargs]=
nullptr;
2338 if (msg.
check(
"env"))
2341 for (
const auto& s : ss) {
2342 char* szenv =
new char[s.size()+1];
2343 strcpy(szenv, s.c_str());
2344 yarp::run::impl::putenv(szenv);
2348 if (msg.
check(
"workdir"))
2350 int ret = yarp::os::impl::chdir(msg.
find(
"workdir").
asString().c_str());
2356 std::string out=std::string(
"ABORTED: server=")+mPortName
2357 +std::string(
" alias=")+strAlias
2358 +std::string(
" cmd=")+strCmd
2359 +std::string(
"\nCan't execute command, cannot set working directory ")+strerror(
error)
2362 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
2363 fprintf(out_to_parent,
"%s", out.c_str());
2364 fflush(out_to_parent);
2365 fclose(out_to_parent);
2367 REDIRECT_TO(STDERR_FILENO, saved_stderr);
2368 fprintf(stderr,
"%s", out.c_str());
2374 char currWorkDirBuff[1024];
2375 char *currWorkDir=
getcwd(currWorkDirBuff, 1024);
2379 char **cwd_arg_str=
new char*[nargs+1];
2380 for (
int i = 1; i < nargs; ++i) {
2381 cwd_arg_str[i] = arg_str[i];
2383 cwd_arg_str[nargs]=
nullptr;
2384 cwd_arg_str[0]=
new char[strlen(currWorkDir)+strlen(arg_str[0])+16];
2387 strcpy(cwd_arg_str[0], currWorkDir);
2388 strcat(cwd_arg_str[0],
"/");
2389 strcat(cwd_arg_str[0], arg_str[0]);
2396 ret = yarp::run::impl::execvp(cwd_arg_str[0], cwd_arg_str);
2398 delete [] cwd_arg_str[0];
2399 delete [] cwd_arg_str;
2408 ret = yarp::run::impl::execvp(arg_str[0], arg_str);
2415 std::string out=std::string(
"ABORTED: server=")+mPortName
2416 +std::string(
" alias=")+strAlias
2417 +std::string(
" cmd=")+strCmd
2418 +std::string(
"\nCan't execute command because ")+strerror(
error)
2421 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
2422 fprintf(out_to_parent,
"%s", out.c_str());
2423 fflush(out_to_parent);
2424 fclose(out_to_parent);
2426 if (saved_stderr >= 0)
2428 REDIRECT_TO(STDERR_FILENO, saved_stderr);
2430 fprintf(stderr,
"%s", out.c_str());
2439 if (IS_PARENT_OF(pid_cmd))
2441 auto* pInf =
new YarpRunProcInfo(strAlias, mPortName, pid_cmd,
nullptr,
false);
2443 if (msg.
check(
"env")) {
2446 mProcessVector->Add(pInf);
2448 sprintf(pidstr,
"%d", pid_cmd);
2452 FILE* in_from_child=fdopen(pipe_child_to_parent[
READ_FROM_PIPE],
"r");
2453 int flags=fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_GETFL, 0);
2454 fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_SETFL, flags|O_NONBLOCK);
2464 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
2468 out+=std::string(buff);
2471 fclose(in_from_child);
2480 out=std::string(
"STARTED: server=")+mPortName
2481 +std::string(
" alias=")+strAlias
2482 +std::string(
" cmd=")+strCmd
2487 fprintf(stderr,
"%s", out.c_str());
int SIGNAL(int pid, int signum)
std::string int2String(int x)
static void sigchld_handler(int sig)
void sigstdio_handler(int sig)
static std::string getProcLabel(const yarp::os::Bottle &msg)
void parseArguments(char *io_pLine, int *o_pArgc, char **o_pArgv)
Breaks up a line into multiple arguments.
void splitLine(char *pLine, char **pArgs)
Split a line into separate words.
static RunTerminator * pTerminator
void setEnv(const std::string &env)
void setCmd(const std::string &cmd)
A simple collection of objects that can be described and transmitted in a portable way.
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.
Bottle tail() const
Get all but the first element of a bottle.
void clear()
Empties the bottle of any objects it contains.
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
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 getLocalMode()
Get current value of flag "localMode", see setLocalMode function.
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.
static bool checkNetwork()
Check if the YARP Network is up and running.
static bool isConnected(const std::string &src, const std::string &dest, bool quiet)
Check if a connection with tcp carrier exists between two ports.
A class for storing options and configuration information.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
void unput(const std::string &key)
Remove the association from the given key to a value, if present.
void fromCommand(int argc, char *argv[], bool skipFirst=true, bool wipe=true)
Interprets a list of command arguments as a list of properties.
A port that is specialized as an RPC client.
A port that is specialized as an RPC server.
static void delaySystem(double seconds)
bool start()
Start the new thread running.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
std::string toString() const override
Return a standard text representation of the content of the object.
virtual std::string asString() const
Get string value.
static int client(yarp::os::Property &config)
Send a property object to a run server, bundling up all the settings usually specified on the command...
bool set_string(const std::string &key, const std::string &value)
Set a string to an environment variable.
ContainerT split(const typename ContainerT::value_type &s, std::basic_regex< typename ContainerT::value_type::value_type > regex)
Utility to split a string by a separator, into a vector of strings.
void useSystemClock()
Configure YARP to use system time (this is the default).
char * getcwd(char *buf, size_t size)
Portable wrapper for the getcwd() function.
int getpid()
Portable wrapper for the getppid() function.