39# if !defined(WIN32_LEAN_AND_MEAN)
40# define WIN32_LEAN_AND_MEAN
50inline std::string lastError2String()
52 int error=GetLastError();
54 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
nullptr,
error, 0, buff, 1024,
nullptr);
56 return std::string(buff);
69std::string yarp::run::Run::mPortName;
71int yarp::run::Run::mProcCNT=0;
72bool yarp::run::Run::mStresstest=
false;
73bool yarp::run::Run::mLogged=
false;
74std::string yarp::run::Run::mLoggerPort(
"/yarplogger");
84 sprintf(msg,
"SIGNAL %d", sig);
97 for (
const auto& s_iter : ss)
100 if (sss.size() == 2 && sss[0] ==
"YARP_LOG_PROCESS_LABEL")
109int yarp::run::Run::main(
int argc,
char *argv[])
115 if (config.
check(
"server"))
117 mLogged=config.
check(
"log");
123 if (botPortLogger.
size()>1)
129 mPortName=std::string(config.
find(
"server").
asString());
139 fprintf(stderr,
"ERROR: no yarp network found.\n");
146 if (config.
check(
"readwrite"))
149 std::string fPortName;
150 std::string lPortName;
152 if (config.
check(
"forward"))
162#elif defined(__APPLE__)
165 struct sigaction new_action;
167 sigfillset(&new_action.sa_mask);
168 new_action.sa_flags=0;
170 sigaction(SIGTERM, &new_action,
nullptr);
171 sigaction(SIGHUP, &new_action,
nullptr);
174 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
176 if (getppid()==1)
return 0;
178 yarp::run::impl::prctl(PR_SET_PDEATHSIG, SIGTERM);
180 struct sigaction new_action;
182 yarp::os::impl::sigfillset(&new_action.sa_mask);
183 new_action.sa_flags=0;
185 yarp::os::impl::sigaction(SIGTERM, &new_action,
nullptr);
186 yarp::os::impl::signal(SIGHUP, SIG_IGN);
188 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
204 if (config.
check(
"write"))
213 struct sigaction new_action;
215 yarp::os::impl::sigfillset(&new_action.sa_mask);
216 new_action.sa_flags=0;
217 yarp::os::impl::sigaction(SIGTERM, &new_action,
nullptr);
219 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
220 yarp::os::impl::signal(SIGHUP, SIG_IGN);
223 if (config.
check(
"log"))
245 if (config.
check(
"read"))
256 yarp::os::impl::signal(SIGHUP, SIG_IGN);
268 if (config.
check(
"stresstest"))
270 fprintf(stderr,
"Yarprun stress test started.\n");
273 int max_interval_ms=config.
find(
"stresstest").
asInt32();
278 config.
unput(
"stresstest");
282 bool isCommand=
false;
284 if (config.
check(
"cmd"))
291 unsigned int t=0, u=0;
295 char cmd_and_name[512];
299 std::random_device rd;
300 std::mt19937 mt(rd());
301 std::uniform_int_distribution<int> dist0maxint(0, max_interval_ms -1);
309 sprintf(tag,
"%s_%u", tag_zero.c_str(), t++);
310 stresser.
put(
"as", tag);
314 sprintf(cmd_and_name,
"%s --name /%s", cmd.c_str(), tag);
315 stresser.
put(
"cmd", cmd_and_name);
320 std::uniform_int_distribution<int> dist07(0, 7);
321 if (isCommand && ++term_cycle>=4)
325 int r = t - (dist07(mt));
327 for (
int i=u; i<r; ++i)
329 sprintf(tag,
"%s_%u", tag_zero.c_str(), i);
350 if (config.
check(
"help"))
358 if (config.
check(
"stdio")
359 || config.
check(
"cmd")
360 || config.
check(
"kill")
361 || config.
check(
"sigterm")
362 || config.
check(
"sigtermall")
363 || config.
check(
"exit")
364 || config.
check(
"isrunning")
365 || config.
check(
"ps")
366 || config.
check(
"env")
367 || config.
check(
"sysinfo")
368 || config.
check(
"which"))
384 for (
int r=0; r<RETRY; ++r)
389 if (!port.
open(
"..."))
392 static std::random_device rd;
393 static std::mt19937 gen(rd());
394 static std::uniform_int_distribution<> dist(0, 99999);
395 int number = dist(gen);
396 std::ostringstream oss;
397 oss << std::setw(5) << std::setfill(
'0') << number;
398 std::string tempy =
"/yarprunclient/temp" + oss.str();
399 if (!port.
open(tempy))
403 fprintf(stderr,
"retrying...");
407 std::string tempportname = port.
getName();
414 fprintf(stderr,
"retrying...");
418 RUNLOG(
"<<<port.write(msg, response)")
419 if (!port.write(msg, response))
423 fprintf(stderr,
"retrying...");
426 RUNLOG(
">>>port.write(msg, response)")
430 fprintf(stderr, "RESPONSE:\n=========\n");
431 for (
size_t s=0; s<response.size(); ++s)
433 fprintf(stderr,
"%s\n", response.get(s).toString().c_str());
441 response.
addString(
"Cannot connect to remote server, aborting...\n");
442 for (
size_t s=0; s<response.
size(); ++s)
444 fprintf(stderr,
"%s\n", response.
get(s).
toString().c_str());
461 if (yarp::run::Run::mBraveZombieHunter)
483 std::string strStdioUUID=msg.
find(
"stdiouuid").
asString();
487 SECURITY_ATTRIBUTES pipe_sec_attr;
488 pipe_sec_attr.nLength=
sizeof(SECURITY_ATTRIBUTES);
489 pipe_sec_attr.bInheritHandle=TRUE;
490 pipe_sec_attr.lpSecurityDescriptor =
nullptr;
491 HANDLE read_from_pipe_stdin_to_cmd, write_to_pipe_stdin_to_cmd;
492 CreatePipe(&read_from_pipe_stdin_to_cmd, &write_to_pipe_stdin_to_cmd, &pipe_sec_attr, 0);
493 HANDLE read_from_pipe_cmd_to_stdout, write_to_pipe_cmd_to_stdout;
494 CreatePipe(&read_from_pipe_cmd_to_stdout, &write_to_pipe_cmd_to_stdout, &pipe_sec_attr, 0);
497 PROCESS_INFORMATION stdout_process_info;
498 ZeroMemory(&stdout_process_info,
sizeof(PROCESS_INFORMATION));
499 STARTUPINFO stdout_startup_info;
500 ZeroMemory(&stdout_startup_info,
sizeof(STARTUPINFO));
502 stdout_startup_info.cb=
sizeof(STARTUPINFO);
503 stdout_startup_info.hStdError=GetStdHandle(STD_ERROR_HANDLE);
504 stdout_startup_info.hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
505 stdout_startup_info.hStdInput=read_from_pipe_cmd_to_stdout;
506 stdout_startup_info.dwFlags|=STARTF_USESTDHANDLES;
508 BOOL bSuccess=CreateProcess(
nullptr,
509 (
char*)(std::string(
"yarprun --write ")+strStdioUUID).c_str(),
513 CREATE_NEW_PROCESS_GROUP,
516 &stdout_startup_info,
517 &stdout_process_info);
521 std::string strError=std::string(
"ABORTED: server=")+mPortName
522 +std::string(
" alias=")+strAlias
523 +std::string(
" cmd=stdout\n")
524 +std::string(
"Can't execute stdout because ")+lastError2String()
529 fprintf(stderr,
"%s", strError.c_str());
532 CloseHandle(write_to_pipe_stdin_to_cmd);
533 CloseHandle(read_from_pipe_stdin_to_cmd);
534 CloseHandle(write_to_pipe_cmd_to_stdout);
535 CloseHandle(read_from_pipe_cmd_to_stdout);
542 PROCESS_INFORMATION stdin_process_info;
543 ZeroMemory(&stdin_process_info,
sizeof(PROCESS_INFORMATION));
544 STARTUPINFO stdin_startup_info;
545 ZeroMemory(&stdin_startup_info,
sizeof(STARTUPINFO));
547 stdin_startup_info.cb=
sizeof(STARTUPINFO);
548 stdin_startup_info.hStdError=write_to_pipe_stdin_to_cmd;
549 stdin_startup_info.hStdOutput=write_to_pipe_stdin_to_cmd;
550 stdin_startup_info.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
551 stdin_startup_info.dwFlags|=STARTF_USESTDHANDLES;
553 bSuccess=CreateProcess(
nullptr,
554 (
char*)(std::string(
"yarprun --read ")+strStdioUUID).c_str(),
558 CREATE_NEW_PROCESS_GROUP,
562 &stdin_process_info);
566 std::string strError=std::string(
"ABORTED: server=")+mPortName
567 +std::string(
" alias=")+strAlias
568 +std::string(
" cmd=stdin\n")
569 +std::string(
"Can't execute stdin because ")+lastError2String()
574 fprintf(stderr,
"%s", strError.c_str());
576 TerminateProcess(stdout_process_info.hProcess,
YARPRUN_ERROR);
578 CloseHandle(stdout_process_info.hProcess);
580 CloseHandle(write_to_pipe_stdin_to_cmd);
581 CloseHandle(read_from_pipe_stdin_to_cmd);
582 CloseHandle(write_to_pipe_cmd_to_stdout);
583 CloseHandle(read_from_pipe_cmd_to_stdout);
590 PROCESS_INFORMATION cmd_process_info;
591 ZeroMemory(&cmd_process_info,
sizeof(PROCESS_INFORMATION));
592 STARTUPINFO cmd_startup_info;
593 ZeroMemory(&cmd_startup_info,
sizeof(STARTUPINFO));
595 cmd_startup_info.cb=
sizeof(STARTUPINFO);
596 cmd_startup_info.hStdError=write_to_pipe_cmd_to_stdout;
597 cmd_startup_info.hStdOutput=write_to_pipe_cmd_to_stdout;
598 cmd_startup_info.hStdInput=read_from_pipe_stdin_to_cmd;
599 cmd_startup_info.dwFlags|=STARTF_USESTDHANDLES;
604 for (
int s=0; s<botCmd.
size(); ++s)
612 TCHAR chNewEnv[32767];
615 LPTCH chOldEnv = GetEnvironmentStrings();
618 LPTSTR lpOld = (LPTSTR) chOldEnv;
619 LPTSTR lpNew = (LPTSTR) chNewEnv;
622 lstrcpy(lpNew, lpOld);
623 lpOld += lstrlen(lpOld) + 1;
624 lpNew += lstrlen(lpNew) + 1;
629 lstrcpy(lpNew, (LPTCH)
"YARP_IS_YARPRUN=1");
630 lpNew += lstrlen(lpNew) + 1;
634 lstrcpy(lpNew, (LPTCH)
"YARPRUN_IS_FORWARDING_LOG=1");
635 lpNew += lstrlen(lpNew) + 1;
638 std::string cstrEnvName;
639 if (msg.
check(
"env"))
642 for (
const auto& s : ss) {
643 lstrcpy(lpNew, (LPTCH) s.c_str());
644 lpNew += lstrlen(lpNew) + 1;
651 bool bWorkdir=msg.
check(
"workdir");
652 std::string strWorkdir=bWorkdir?msg.
find(
"workdir").
asString()+
"\\":
"";
654 bSuccess=CreateProcess(
nullptr,
655 (
char*)(strWorkdir+strCmd).c_str(),
659 CREATE_NEW_PROCESS_GROUP,
661 bWorkdir ? strWorkdir.c_str() : nullptr,
665 if (!bSuccess && bWorkdir)
667 bSuccess=CreateProcess(
nullptr,
668 (
char*)(strCmd.c_str()),
672 CREATE_NEW_PROCESS_GROUP,
680 FreeEnvironmentStrings(chOldEnv);
687 std::string line1=std::string(
"ABORTED: server=")+mPortName
688 +std::string(
" alias=")+strAlias
689 +std::string(
" cmd=")+strCmd
690 +std::string(
"pid=")+
int2String(cmd_process_info.dwProcessId)
693 WriteFile(write_to_pipe_cmd_to_stdout, line1.c_str(), line1.length(), &nBytes, 0);
695 std::string line2=std::string(
"Can't execute command because ")+lastError2String()+std::string(
"\n");
696 WriteFile(write_to_pipe_cmd_to_stdout, line1.c_str(), line2.length(), &nBytes, 0);
697 FlushFileBuffers(write_to_pipe_cmd_to_stdout);
699 std::string out=line1+line2;
701 fprintf(stderr,
"%s", out.c_str());
704 CloseHandle(write_to_pipe_stdin_to_cmd);
705 CloseHandle(read_from_pipe_stdin_to_cmd);
706 CloseHandle(write_to_pipe_cmd_to_stdout);
707 CloseHandle(read_from_pipe_cmd_to_stdout);
709 TerminateProcess(stdout_process_info.hProcess,
YARPRUN_ERROR);
711 CloseHandle(stdout_process_info.hProcess);
713 TerminateProcess(stdin_process_info.hProcess,
YARPRUN_ERROR);
715 CloseHandle(stdin_process_info.hProcess);
720 FlushFileBuffers(write_to_pipe_cmd_to_stdout);
726 cmd_process_info.dwProcessId,
729 stdin_process_info.dwProcessId,
730 stdout_process_info.dwProcessId,
731 read_from_pipe_stdin_to_cmd,
732 write_to_pipe_stdin_to_cmd,
733 read_from_pipe_cmd_to_stdout,
734 write_to_pipe_cmd_to_stdout,
735 cmd_process_info.hProcess,
739 if (msg.
check(
"env"))
743 mProcessVector.Add(pInf);
745 result.
addInt32(cmd_process_info.dwProcessId);
746 std::string out=std::string(
"STARTED: server=")+mPortName
747 +std::string(
" alias=")+strAlias
748 +std::string(
" cmd=")+strCmd
749 +std::string(
" pid=")+
int2String(cmd_process_info.dwProcessId)
754 fprintf(stderr,
"%s", out.c_str());
756 return cmd_process_info.dwProcessId;
764 std::string portName=
"/log";
765 portName+=mPortName+
"/";
767 command = command.substr(0, command.find(
' '));
768 command = command.substr(command.find_last_of(
"\\/") + 1);
770 if (proc_label !=
"") { portName +=
"[" + proc_label +
"]"; }
773 SECURITY_ATTRIBUTES pipe_sec_attr;
774 pipe_sec_attr.nLength=
sizeof(SECURITY_ATTRIBUTES);
775 pipe_sec_attr.bInheritHandle=TRUE;
776 pipe_sec_attr.lpSecurityDescriptor =
nullptr;
777 HANDLE read_from_pipe_cmd_to_stdout, write_to_pipe_cmd_to_stdout;
778 CreatePipe(&read_from_pipe_cmd_to_stdout, &write_to_pipe_cmd_to_stdout, &pipe_sec_attr, 0);
781 PROCESS_INFORMATION stdout_process_info;
782 ZeroMemory(&stdout_process_info,
sizeof(PROCESS_INFORMATION));
783 STARTUPINFO stdout_startup_info;
784 ZeroMemory(&stdout_startup_info,
sizeof(STARTUPINFO));
786 stdout_startup_info.cb=
sizeof(STARTUPINFO);
787 stdout_startup_info.hStdError=GetStdHandle(STD_ERROR_HANDLE);
788 stdout_startup_info.hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
789 stdout_startup_info.hStdInput=read_from_pipe_cmd_to_stdout;
790 stdout_startup_info.dwFlags|=STARTF_USESTDHANDLES;
792 BOOL bSuccess=CreateProcess(
nullptr,
793 (
char*)(std::string(
"yarprun --log ")+loggerName+std::string(
" --write ")+portName).c_str(),
797 CREATE_NEW_PROCESS_GROUP,
800 &stdout_startup_info,
801 &stdout_process_info);
805 std::string strError=std::string(
"ABORTED: server=")+mPortName
806 +std::string(
" alias=")+strAlias
807 +std::string(
" cmd=stdout\n")
808 +std::string(
"Can't execute stdout because ")+lastError2String()
813 fprintf(stderr,
"%s", strError.c_str());
816 CloseHandle(write_to_pipe_cmd_to_stdout);
817 CloseHandle(read_from_pipe_cmd_to_stdout);
824 PROCESS_INFORMATION cmd_process_info;
825 ZeroMemory(&cmd_process_info,
sizeof(PROCESS_INFORMATION));
826 STARTUPINFO cmd_startup_info;
827 ZeroMemory(&cmd_startup_info,
sizeof(STARTUPINFO));
829 cmd_startup_info.cb=
sizeof(STARTUPINFO);
830 cmd_startup_info.hStdError=write_to_pipe_cmd_to_stdout;
831 cmd_startup_info.hStdOutput=write_to_pipe_cmd_to_stdout;
832 cmd_startup_info.hStdInput=GetStdHandle(STD_INPUT_HANDLE);
833 cmd_startup_info.dwFlags|=STARTF_USESTDHANDLES;
838 for (
int s=0; s<botCmd.
size(); ++s)
846 TCHAR chNewEnv[32767];
849 LPTCH chOldEnv = GetEnvironmentStrings();
852 LPTSTR lpOld = (LPTSTR) chOldEnv;
853 LPTSTR lpNew = (LPTSTR) chNewEnv;
856 lstrcpy(lpNew, lpOld);
857 lpOld += lstrlen(lpOld) + 1;
858 lpNew += lstrlen(lpNew) + 1;
863 lstrcpy(lpNew, (LPTCH)
"YARP_IS_YARPRUN=1");
864 lpNew += lstrlen(lpNew) + 1;
868 lstrcpy(lpNew, (LPTCH)
"YARPRUN_IS_FORWARDING_LOG=1");
869 lpNew += lstrlen(lpNew) + 1;
872 std::string cstrEnvName;
873 if (msg.
check(
"env"))
876 for (
const auto& s : ss) {
877 lstrcpy(lpNew, (LPTCH)s.c_str());
878 lpNew += lstrlen(lpNew) + 1;
885 bool bWorkdir=msg.
check(
"workdir");
886 std::string strWorkdir=bWorkdir?msg.
find(
"workdir").
asString()+
"\\":
"";
888 bSuccess=CreateProcess(
nullptr,
889 (
char*)(strWorkdir+strCmd).c_str(),
893 CREATE_NEW_PROCESS_GROUP,
895 bWorkdir?strWorkdir.c_str():nullptr,
899 if (!bSuccess && bWorkdir)
901 bSuccess=CreateProcess(
nullptr,
902 (
char*)(strCmd.c_str()),
906 CREATE_NEW_PROCESS_GROUP,
914 FreeEnvironmentStrings(chOldEnv);
921 std::string line1=std::string(
"ABORTED: server=")+mPortName
922 +std::string(
" alias=")+strAlias
923 +std::string(
" cmd=")+strCmd
924 +std::string(
"pid=")+
int2String(cmd_process_info.dwProcessId)
927 WriteFile(write_to_pipe_cmd_to_stdout, line1.c_str(), line1.length(), &nBytes, 0);
929 std::string line2=std::string(
"Can't execute command because ")+lastError2String()+std::string(
"\n");
930 WriteFile(write_to_pipe_cmd_to_stdout, line1.c_str(), line2.length(), &nBytes, 0);
931 FlushFileBuffers(write_to_pipe_cmd_to_stdout);
933 std::string out=line1+line2;
935 fprintf(stderr,
"%s", out.c_str());
938 CloseHandle(write_to_pipe_cmd_to_stdout);
939 CloseHandle(read_from_pipe_cmd_to_stdout);
941 TerminateProcess(stdout_process_info.hProcess,
YARPRUN_ERROR);
943 CloseHandle(stdout_process_info.hProcess);
948 FlushFileBuffers(write_to_pipe_cmd_to_stdout);
954 cmd_process_info.dwProcessId,
955 stdout_process_info.dwProcessId,
956 read_from_pipe_cmd_to_stdout,
957 write_to_pipe_cmd_to_stdout,
958 cmd_process_info.hProcess,
965 if (msg.
check(
"env"))
969 mProcessVector.Add(pInf);
971 result.
addInt32(cmd_process_info.dwProcessId);
972 std::string out=std::string(
"STARTED: server=")+mPortName
973 +std::string(
" alias=")+strAlias
974 +std::string(
" cmd=")+strCmd
975 +std::string(
" pid=")+
int2String(cmd_process_info.dwProcessId)
980 fprintf(stderr,
"%s", out.c_str());
982 return cmd_process_info.dwProcessId;
991 PROCESS_INFORMATION cmd_process_info;
992 ZeroMemory(&cmd_process_info,
sizeof(PROCESS_INFORMATION));
993 STARTUPINFO cmd_startup_info;
994 ZeroMemory(&cmd_startup_info,
sizeof(STARTUPINFO));
996 cmd_startup_info.cb=
sizeof(STARTUPINFO);
1001 for (
int s=0; s<botCmd.
size(); ++s)
1003 strCmd+=botCmd.
get(s).
toString()+std::string(
" ");
1009 TCHAR chNewEnv[32767];
1012 LPTCH chOldEnv = GetEnvironmentStrings();
1015 LPTSTR lpOld = (LPTSTR) chOldEnv;
1016 LPTSTR lpNew = (LPTSTR) chNewEnv;
1019 lstrcpy(lpNew, lpOld);
1020 lpOld += lstrlen(lpOld) + 1;
1021 lpNew += lstrlen(lpNew) + 1;
1026 lstrcpy(lpNew, (LPTCH)
"YARP_IS_YARPRUN=1");
1027 lpNew += lstrlen(lpNew) + 1;
1031 lstrcpy(lpNew, (LPTCH)
"YARPRUN_IS_FORWARDING_LOG=0");
1032 lpNew += lstrlen(lpNew) + 1;
1035 std::string cstrEnvName;
1036 if (msg.
check(
"env"))
1039 for (
const auto& s : ss) {
1040 lstrcpy(lpNew, (LPTCH)s.c_str());
1041 lpNew += lstrlen(lpNew) + 1;
1048 bool bWorkdir=msg.
check(
"workdir");
1049 std::string strWorkdir=bWorkdir?msg.
find(
"workdir").
asString()+
"\\":
"";
1051 BOOL bSuccess=CreateProcess(
nullptr,
1052 (
char*)(strWorkdir+strCmd).c_str(),
1056 CREATE_NEW_PROCESS_GROUP,
1058 bWorkdir ? strWorkdir.c_str() : nullptr,
1062 if (!bSuccess && bWorkdir)
1064 bSuccess=CreateProcess(
nullptr,
1065 (
char*)(strCmd.c_str()),
1069 CREATE_NEW_PROCESS_GROUP,
1077 FreeEnvironmentStrings(chOldEnv);
1083 std::string out=std::string(
"ABORTED: server=")+mPortName
1084 +std::string(
" alias=")+strAlias
1085 +std::string(
" cmd=")+strCmd
1086 +std::string(
" pid=")+
int2String(cmd_process_info.dwProcessId)
1087 +std::string(
"\nCan't execute command because ")+lastError2String()
1091 fprintf(stderr,
"%s", out.c_str());
1100 cmd_process_info.dwProcessId,
1101 cmd_process_info.hProcess,
1104 if (msg.
check(
"env"))
1107 mProcessVector.Add(pInf);
1109 result.
addInt32(cmd_process_info.dwProcessId);
1110 std::string out=std::string(
"STARTED: server=")+mPortName
1111 +std::string(
" alias=")+strAlias
1112 +std::string(
" cmd=")+strCmd
1113 +std::string(
" pid=")+
int2String(cmd_process_info.dwProcessId)
1116 fprintf(stderr,
"%s", out.c_str());
1118 return cmd_process_info.dwProcessId;
1124 PROCESS_INFORMATION stdio_process_info;
1125 ZeroMemory(&stdio_process_info,
sizeof(PROCESS_INFORMATION));
1127 STARTUPINFO stdio_startup_info;
1128 ZeroMemory(&stdio_startup_info,
sizeof(STARTUPINFO));
1129 stdio_startup_info.cb=
sizeof(STARTUPINFO);
1130 stdio_startup_info.wShowWindow=SW_SHOWNOACTIVATE;
1131 stdio_startup_info.dwFlags=STARTF_USESHOWWINDOW;
1135 std::string strCmd=std::string(
"yarprun --readwrite ")+strUUID;
1138 BOOL bSuccess=CreateProcess(
nullptr,
1139 (
char*)strCmd.c_str(),
1146 &stdio_startup_info,
1147 &stdio_process_info);
1155 stdio_process_info.dwProcessId,
1156 stdio_process_info.hProcess,
1159 out=std::string(
"STARTED: server=")+mPortName
1160 +std::string(
" alias=")+strAlias
1161 +std::string(
" cmd=stdio pid=")+
int2String(stdio_process_info.dwProcessId)
1168 out=std::string(
"ABORTED: server=")+mPortName
1169 +std::string(
" alias=")+strAlias
1170 +std::string(
" cmd=stdio\n")
1171 +std::string(
"Can't open stdio window because ")+lastError2String()
1176 result.
addInt32(stdio_process_info.dwProcessId);
1178 fprintf(stderr,
"%s", out.c_str());
1181 return stdio_process_info.dwProcessId;
1192 char *pTmp = strchr(pLine,
' ');
1197 while ((*pTmp) && (*pTmp ==
' ')) {
1200 if (*pTmp ==
'\0') {
1212 char *pNext = io_pLine;
1216 size_t len = strlen(io_pLine);
1219 for(i = 0; i < len; i++) {
1220 if ((!quoted) && (
'"' == io_pLine[i])) {
1223 }
else if ((quoted) && (
'"' == io_pLine[i])) {
1226 }
else if ((quoted) && (
' ' == io_pLine[i])) {
1232 memset(o_pArgv, 0x00,
sizeof(
char*) *
C_MAXARGS);
1234 o_pArgv[0] = io_pLine;
1236 while ((
nullptr != pNext) && (*o_pArgc <
C_MAXARGS)) {
1238 pNext = o_pArgv[*o_pArgc];
1240 if (
nullptr != o_pArgv[*o_pArgc]) {
1245 for(j = 0; j < *o_pArgc; j++) {
1246 len = strlen(o_pArgv[j]);
1247 for(i = 0; i < len; i++) {
1248 if (
'\1' == o_pArgv[j][i]) {
1249 o_pArgv[j][i] =
' ';
1255void yarp::run::Run::CleanZombie(
int pid)
1257 bool bFound=mProcessVector && mProcessVector->CleanZombie(pid);
1261 mStdioVector->CleanZombie(pid);
1273 std::string strStdioUUID=msg.
find(
"stdiouuid").
asString();
1275 int pipe_stdin_to_cmd[2];
1276 int ret_stdin_to_cmd=yarp::run::impl::pipe(pipe_stdin_to_cmd);
1278 int pipe_cmd_to_stdout[2];
1279 int ret_cmd_to_stdout=yarp::run::impl::pipe(pipe_cmd_to_stdout);
1281 int pipe_child_to_parent[2];
1282 int ret_child_to_parent=yarp::run::impl::pipe(pipe_child_to_parent);
1284 if (ret_child_to_parent!=0 || ret_cmd_to_stdout!=0 || ret_stdin_to_cmd!=0)
1288 std::string out=std::string(
"ABORTED: server=")+mPortName
1289 +std::string(
" alias=")+strAlias
1290 +std::string(
" cmd=stdout\n")
1291 +std::string(
"Can't create pipes ")+strerror(
error)
1296 fprintf(stderr,
"%s", out.c_str());
1301 int pid_stdout=yarp::run::impl::fork();
1303 if (IS_INVALID(pid_stdout))
1314 std::string out=std::string(
"ABORTED: server=")+mPortName
1315 +std::string(
" alias=")+strAlias
1316 +std::string(
" cmd=stdout\n")
1317 +std::string(
"Can't fork stdout process because ")+strerror(
error)
1322 fprintf(stderr,
"%s", out.c_str());
1327 if (IS_NEW_PROCESS(pid_stdout))
1343 int ret = yarp::run::impl::execlp(
"yarprun",
"yarprun",
"--write", strStdioUUID.c_str(),
static_cast<char*
>(
nullptr));
1351 std::string out=std::string(
"ABORTED: server=")+mPortName
1352 +std::string(
" alias=")+strAlias
1353 +std::string(
" cmd=stdout\n")
1354 +std::string(
"Can't execute stdout because ")+strerror(
error)
1357 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1358 fprintf(out_to_parent,
"%s", out.c_str());
1359 fflush(out_to_parent);
1360 fclose(out_to_parent);
1362 fprintf(stderr,
"%s", out.c_str());
1370 if (IS_PARENT_OF(pid_stdout))
1374 fprintf(stderr,
"STARTED: server=%s alias=%s cmd=stdout pid=%d\n", mPortName.c_str(), strAlias.c_str(), pid_stdout);
1376 int pid_stdin=yarp::run::impl::fork();
1378 if (IS_INVALID(pid_stdin))
1388 std::string out=std::string(
"ABORTED: server=")+mPortName
1389 +std::string(
" alias=")+strAlias
1390 +std::string(
" cmd=stdin\n")
1391 +std::string(
"Can't fork stdin process because ")+strerror(
error)
1396 fprintf(stderr,
"%s", out.c_str());
1398 SIGNAL(pid_stdout, SIGTERM);
1399 fprintf(stderr,
"TERMINATING stdout (%d)\n", pid_stdout);
1404 if (IS_NEW_PROCESS(pid_stdin))
1407 REDIRECT_TO(STDOUT_FILENO, pipe_stdin_to_cmd[
WRITE_TO_PIPE]);
1408 REDIRECT_TO(STDERR_FILENO, pipe_stdin_to_cmd[
WRITE_TO_PIPE]);
1421 int ret = yarp::run::impl::execlp(
"yarprun",
"yarprun",
"--read", strStdioUUID.c_str(),
static_cast<char*
>(
nullptr));
1429 std::string out=std::string(
"ABORTED: server=")+mPortName
1430 +std::string(
" alias=")+strAlias
1431 +std::string(
" cmd=stdin\n")
1432 +std::string(
"Can't execute stdin because ")+strerror(
error)
1436 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1437 fprintf(out_to_parent,
"%s", out.c_str());
1438 fflush(out_to_parent);
1439 fclose(out_to_parent);
1440 fprintf(stderr,
"%s", out.c_str());
1448 if (IS_PARENT_OF(pid_stdin))
1453 fprintf(stderr,
"STARTED: server=%s alias=%s cmd=stdin pid=%d\n", mPortName.c_str(), strAlias.c_str(), pid_stdin);
1455 int pid_cmd=yarp::run::impl::fork();
1457 if (IS_INVALID(pid_cmd))
1465 std::string out=std::string(
"ABORTED: server=")+mPortName
1466 +std::string(
" alias=")+strAlias
1467 +std::string(
" cmd=")+strCmd
1468 +std::string(
"\nCan't fork command process because ")+strerror(
error)
1473 fprintf(stderr,
"%s", out.c_str());
1475 FILE* to_yarp_stdout=fdopen(pipe_cmd_to_stdout[
WRITE_TO_PIPE],
"w");
1476 fprintf(to_yarp_stdout,
"%s", out.c_str());
1477 fflush(to_yarp_stdout);
1478 fclose(to_yarp_stdout);
1480 SIGNAL(pid_stdout, SIGTERM);
1481 fprintf(stderr,
"TERMINATING stdout (%d)\n", pid_stdout);
1482 SIGNAL(pid_stdin, SIGTERM);
1483 fprintf(stderr,
"TERMINATING stdin (%d)\n", pid_stdin);
1490 if (IS_NEW_PROCESS(pid_cmd))
1494 char *cmd_str=
new char[strCmd.length()+1];
1495 strcpy(cmd_str, strCmd.c_str());
1503 char **arg_str =
new char*[
C_MAXARGS + 1];
1505 arg_str[nargs]=
nullptr;
1507 setvbuf(stdout,
nullptr, _IONBF, 0);
1510 REDIRECT_TO(STDOUT_FILENO, pipe_cmd_to_stdout[
WRITE_TO_PIPE]);
1511 REDIRECT_TO(STDERR_FILENO, pipe_cmd_to_stdout[
WRITE_TO_PIPE]);
1521 if (msg.
check(
"env"))
1524 for (
const auto& s : ss) {
1525 char* szenv =
new char[s.size()+1];
1526 strcpy(szenv, s.c_str());
1527 yarp::run::impl::putenv(szenv);
1532 if (msg.
check(
"workdir"))
1534 int ret = yarp::os::impl::chdir(msg.
find(
"workdir").
asString().c_str());
1540 std::string out=std::string(
"ABORTED: server=")+mPortName
1541 +std::string(
" alias=")+strAlias
1542 +std::string(
" cmd=")+strCmd
1543 +std::string(
"\nCan't execute command, cannot set working directory ")+strerror(
error)
1546 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1547 fprintf(out_to_parent,
"%s", out.c_str());
1548 fflush(out_to_parent);
1549 fclose(out_to_parent);
1550 fprintf(stderr,
"%s", out.c_str());
1558 char currWorkDirBuff[1024];
1559 char *currWorkDir=yarp::os::impl::getcwd(currWorkDirBuff, 1024);
1563 char **cwd_arg_str=
new char*[nargs+1];
1564 for (
int i = 1; i < nargs; ++i) {
1565 cwd_arg_str[i] = arg_str[i];
1567 cwd_arg_str[nargs]=
nullptr;
1568 cwd_arg_str[0]=
new char[strlen(currWorkDir)+strlen(arg_str[0])+16];
1570 strcpy(cwd_arg_str[0], currWorkDir);
1571 strcat(cwd_arg_str[0],
"/");
1572 strcat(cwd_arg_str[0], arg_str[0]);
1579 ret = yarp::run::impl::execvp(cwd_arg_str[0], cwd_arg_str);
1581 delete [] cwd_arg_str[0];
1582 delete [] cwd_arg_str;
1592 ret = yarp::run::impl::execvp(arg_str[0], arg_str);
1604 std::string out=std::string(
"ABORTED: server=")+mPortName
1605 +std::string(
" alias=")+strAlias
1606 +std::string(
" cmd=")+strCmd
1607 +std::string(
"\nCan't execute command because ")+strerror(
error)
1610 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1611 fprintf(out_to_parent,
"%s", out.c_str());
1612 fflush(out_to_parent);
1613 fclose(out_to_parent);
1614 fprintf(stderr,
"%s", out.c_str());
1626 if (IS_PARENT_OF(pid_cmd))
1651 if (msg.
check(
"env"))
1656 mProcessVector->Add(pInf);
1660 FILE* in_from_child=fdopen(pipe_child_to_parent[
READ_FROM_PIPE],
"r");
1661 int flags=fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_GETFL, 0);
1662 fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_SETFL, flags|O_NONBLOCK);
1672 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
1676 out+=std::string(buff);
1679 fclose(in_from_child);
1688 out=std::string(
"STARTED: server=")+mPortName
1689 +std::string(
" alias=")+strAlias
1690 +std::string(
" cmd=")+strCmd
1699 fprintf(stderr,
"%s", out.c_str());
1709 result.
addString(
"I should never reach this point!!!\n");
1721 std::string portName=
"/log";
1722 portName+=mPortName+
"/";
1724 std::string command = strCmd;
1725 command = command.substr(0, command.find(
' '));
1726 command = command.substr(command.find_last_of(
"\\/") + 1);
1729 if (proc_label !=
"") { portName +=
"[" + proc_label +
"]"; }
1732 int pipe_cmd_to_stdout[2];
1733 int ret_cmd_to_stdout=yarp::run::impl::pipe(pipe_cmd_to_stdout);
1735 int pipe_child_to_parent[2];
1736 int ret_child_to_parent=yarp::run::impl::pipe(pipe_child_to_parent);
1738 if (ret_child_to_parent!=0 || ret_cmd_to_stdout!=0)
1742 std::string out=std::string(
"ABORTED: server=")+mPortName
1743 +std::string(
" alias=")+strAlias
1744 +std::string(
" cmd=stdout\n")
1745 +std::string(
"Can't create pipes ")+strerror(
error)
1750 fprintf(stderr,
"%s", out.c_str());
1755 int pid_stdout=yarp::run::impl::fork();
1757 if (IS_INVALID(pid_stdout))
1766 std::string out=std::string(
"ABORTED: server=")+mPortName
1767 +std::string(
" alias=")+strAlias
1768 +std::string(
" cmd=stdout\n")
1769 +std::string(
"Can't fork stdout process because ")+strerror(
error)
1774 fprintf(stderr,
"%s", out.c_str());
1779 if (IS_NEW_PROCESS(pid_stdout))
1793 int ret = yarp::run::impl::execlp(
"yarprun",
"yarprun",
"--write", portName.c_str(),
"--log", loggerName.c_str(),
static_cast<char*
>(
nullptr));
1801 std::string out=std::string(
"ABORTED: server=")+mPortName
1802 +std::string(
" alias=")+strAlias
1803 +std::string(
" cmd=stdout\n")
1804 +std::string(
"Can't execute stdout because ")+strerror(
error)
1807 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1808 fprintf(out_to_parent,
"%s", out.c_str());
1809 fflush(out_to_parent);
1810 fclose(out_to_parent);
1812 fprintf(stderr,
"%s", out.c_str());
1820 if (IS_PARENT_OF(pid_stdout))
1824 fprintf(stderr,
"STARTED: server=%s alias=%s cmd=stdout pid=%d\n", mPortName.c_str(), strAlias.c_str(), pid_stdout);
1830 int pid_cmd=yarp::run::impl::fork();
1832 if (IS_INVALID(pid_cmd))
1839 std::string out=std::string(
"ABORTED: server=")+mPortName
1840 +std::string(
" alias=")+strAlias
1841 +std::string(
" cmd=")+strCmd
1842 +std::string(
"\nCan't fork command process because ")+strerror(
error)
1847 fprintf(stderr,
"%s", out.c_str());
1849 FILE* to_yarp_stdout=fdopen(pipe_cmd_to_stdout[
WRITE_TO_PIPE],
"w");
1850 fprintf(to_yarp_stdout,
"%s", out.c_str());
1851 fflush(to_yarp_stdout);
1852 fclose(to_yarp_stdout);
1854 SIGNAL(pid_stdout, SIGTERM);
1855 fprintf(stderr,
"TERMINATING stdout (%d)\n", pid_stdout);
1861 if (IS_NEW_PROCESS(pid_cmd))
1865 char *cmd_str=
new char[strCmd.length()+1];
1866 strcpy(cmd_str, strCmd.c_str());
1874 char **arg_str =
new char*[
C_MAXARGS + 1];
1876 arg_str[nargs]=
nullptr;
1878 setvbuf(stdout,
nullptr, _IONBF, 0);
1880 REDIRECT_TO(STDOUT_FILENO, pipe_cmd_to_stdout[
WRITE_TO_PIPE]);
1881 REDIRECT_TO(STDERR_FILENO, pipe_cmd_to_stdout[
WRITE_TO_PIPE]);
1891 if (msg.
check(
"env"))
1894 for (
const auto& s : ss) {
1895 char* szenv =
new char[s.size()+1];
1896 strcpy(szenv, s.c_str());
1897 yarp::run::impl::putenv(szenv);
1902 if (msg.
check(
"workdir"))
1904 int ret = yarp::os::impl::chdir(msg.
find(
"workdir").
asString().c_str());
1910 std::string out=std::string(
"ABORTED: server=")+mPortName
1911 +std::string(
" alias=")+strAlias
1912 +std::string(
" cmd=")+strCmd
1913 +std::string(
"\nCan't execute command, cannot set working directory ")+strerror(
error)
1916 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1917 fprintf(out_to_parent,
"%s", out.c_str());
1918 fflush(out_to_parent);
1919 fclose(out_to_parent);
1920 fprintf(stderr,
"%s", out.c_str());
1928 char currWorkDirBuff[1024];
1929 char *currWorkDir=
getcwd(currWorkDirBuff, 1024);
1933 char **cwd_arg_str=
new char*[nargs+1];
1934 for (
int i = 1; i < nargs; ++i) {
1935 cwd_arg_str[i] = arg_str[i];
1937 cwd_arg_str[nargs]=
nullptr;
1938 cwd_arg_str[0]=
new char[strlen(currWorkDir)+strlen(arg_str[0])+16];
1940 strcpy(cwd_arg_str[0], currWorkDir);
1941 strcat(cwd_arg_str[0],
"/");
1942 strcat(cwd_arg_str[0], arg_str[0]);
1949 ret = yarp::run::impl::execvp(cwd_arg_str[0], cwd_arg_str);
1951 delete [] cwd_arg_str[0];
1952 delete [] cwd_arg_str;
1962 ret = yarp::run::impl::execvp(arg_str[0], arg_str);
1973 std::string out=std::string(
"ABORTED: server=")+mPortName
1974 +std::string(
" alias=")+strAlias
1975 +std::string(
" cmd=")+strCmd
1976 +std::string(
"\nCan't execute command because ")+strerror(
error)
1979 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
1980 fprintf(out_to_parent,
"%s", out.c_str());
1981 fflush(out_to_parent);
1982 fclose(out_to_parent);
1983 fprintf(stderr,
"%s", out.c_str());
1995 if (IS_PARENT_OF(pid_cmd))
2014 if (msg.
check(
"env"))
2019 mProcessVector->Add(pInf);
2023 FILE* in_from_child=fdopen(pipe_child_to_parent[
READ_FROM_PIPE],
"r");
2024 int flags=fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_GETFL, 0);
2025 fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_SETFL, flags|O_NONBLOCK);
2035 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
2039 out+=std::string(buff);
2042 fclose(in_from_child);
2051 out=std::string(
"STARTED: server=")+mPortName
2052 +std::string(
" alias=")+strAlias
2053 +std::string(
" cmd=")+strCmd
2061 fprintf(stderr,
"%s", out.c_str());
2071 result.
addString(
"I should never reach this point!!!\n");
2083 if (msg.
check(
"forward"))
2085 strCmd=std::string(
"/bin/bash -l -c \"yarprun --readwrite ")+strUUID
2090 strCmd=std::string(
"/bin/bash -l -c \"yarprun --readwrite ")+strUUID+
"\"";
2093 int pipe_child_to_parent[2];
2095 if (yarp::run::impl::pipe(pipe_child_to_parent))
2099 std::string out=std::string(
"ABORTED: server=")+mPortName
2100 +std::string(
" alias=")+strAlias
2101 +std::string(
" cmd=stdio\nCan't create pipe ")+strerror(
error)
2107 fprintf(stderr,
"%s", out.c_str());
2114 for (
auto & i : command) {
2118 cmdcpy(command[c++],
"xterm");
2119 cmdcpy(command[c++], msg.
check(
"hold")?
"-hold":
"+hold");
2121 if (msg.
check(
"geometry"))
2123 cmdcpy(command[c++],
"-geometry");
2124 cmdcpy(command[c++], msg.
find(
"geometry").
asString().c_str());
2127 cmdcpy(command[c++],
"-title");
2128 cmdcpy(command[c++], strAlias.c_str());
2130 cmdcpy(command[c++],
"-e");
2131 cmdcpy(command[c++], strCmd.c_str());
2133 int pid_cmd=yarp::run::impl::fork();
2135 if (IS_INVALID(pid_cmd))
2139 std::string out=std::string(
"ABORTED: server=")+mPortName
2140 +std::string(
" alias=")+strAlias
2141 +std::string(
" cmd=stdio\nCan't fork stdout process because ")+strerror(
error)
2147 fprintf(stderr,
"%s", out.c_str());
2157 if (IS_NEW_PROCESS(pid_cmd))
2163 REDIRECT_TO(STDERR_FILENO, pipe_child_to_parent[
WRITE_TO_PIPE]);
2172 int ret = yarp::run::impl::execvp(
"xterm", command);
2180 std::string out=std::string(
"ABORTED: server=")+mPortName
2181 +std::string(
" alias=")+strAlias
2182 +std::string(
" cmd=xterm\nCan't execute command because ")+strerror(
error)
2185 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
2187 fprintf(out_to_parent,
"%s", out.c_str());
2188 fflush(out_to_parent);
2189 fclose(out_to_parent);
2191 fprintf(stderr,
"%s", out.c_str());
2199 if (IS_PARENT_OF(pid_cmd))
2203 mStdioVector->Add(
new YarpRunProcInfo(strAlias, mPortName, pid_cmd,
nullptr, msg.
check(
"hold")));
2211 FILE* in_from_child=fdopen(pipe_child_to_parent[
READ_FROM_PIPE],
"r");
2212 int flags=fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_GETFL, 0);
2213 fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_SETFL, flags|O_NONBLOCK);
2222 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
2226 out+=std::string(buff);
2229 fclose(in_from_child);
2235 if (out.substr(0, 14)==
"xterm Xt error" || out.substr(0, 7)==
"ABORTED")
2241 out=std::string(
"STARTED: server=")+mPortName
2242 +std::string(
" alias=")+strAlias
2243 +std::string(
" cmd=xterm pid=")+
int2String(pid_cmd)
2248 fprintf(stderr,
"%s", out.c_str());
2269 int pipe_child_to_parent[2];
2270 int ret_pipe_child_to_parent=yarp::run::impl::pipe(pipe_child_to_parent);
2272 if (ret_pipe_child_to_parent!=0)
2276 std::string out=std::string(
"ABORTED: server=")+mPortName
2277 +std::string(
" alias=")+strAlias
2278 +std::string(
" cmd=stdio\nCan't create pipe ")+strerror(
error)
2284 fprintf(stderr,
"%s", out.c_str());
2289 int pid_cmd=yarp::run::impl::fork();
2291 if (IS_INVALID(pid_cmd))
2295 std::string out=std::string(
"ABORTED: server=")+mPortName
2296 +std::string(
" alias=")+strAlias
2297 +std::string(
" cmd=")+strCmd
2298 +std::string(
"\nCan't fork command process because ")+strerror(
error)
2303 fprintf(stderr,
"%s", out.c_str());
2308 if (IS_NEW_PROCESS(pid_cmd))
2310 int saved_stderr = yarp::run::impl::dup(STDERR_FILENO);
2311 int null_file=open(
"/dev/null", O_WRONLY);
2314 REDIRECT_TO(STDOUT_FILENO, null_file);
2315 REDIRECT_TO(STDERR_FILENO, null_file);
2318 char *cmd_str=
new char[strCmd.length()+1];
2319 strcpy(cmd_str, strCmd.c_str());
2327 char **arg_str =
new char*[
C_MAXARGS + 1];
2329 arg_str[nargs]=
nullptr;
2339 if (msg.
check(
"env"))
2342 for (
const auto& s : ss) {
2343 char* szenv =
new char[s.size()+1];
2344 strcpy(szenv, s.c_str());
2345 yarp::run::impl::putenv(szenv);
2349 if (msg.
check(
"workdir"))
2351 int ret = yarp::os::impl::chdir(msg.
find(
"workdir").
asString().c_str());
2357 std::string out=std::string(
"ABORTED: server=")+mPortName
2358 +std::string(
" alias=")+strAlias
2359 +std::string(
" cmd=")+strCmd
2360 +std::string(
"\nCan't execute command, cannot set working directory ")+strerror(
error)
2363 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
2364 fprintf(out_to_parent,
"%s", out.c_str());
2365 fflush(out_to_parent);
2366 fclose(out_to_parent);
2368 REDIRECT_TO(STDERR_FILENO, saved_stderr);
2369 fprintf(stderr,
"%s", out.c_str());
2375 char currWorkDirBuff[1024];
2376 char *currWorkDir=
getcwd(currWorkDirBuff, 1024);
2380 char **cwd_arg_str=
new char*[nargs+1];
2381 for (
int i = 1; i < nargs; ++i) {
2382 cwd_arg_str[i] = arg_str[i];
2384 cwd_arg_str[nargs]=
nullptr;
2385 cwd_arg_str[0]=
new char[strlen(currWorkDir)+strlen(arg_str[0])+16];
2388 strcpy(cwd_arg_str[0], currWorkDir);
2389 strcat(cwd_arg_str[0],
"/");
2390 strcat(cwd_arg_str[0], arg_str[0]);
2397 ret = yarp::run::impl::execvp(cwd_arg_str[0], cwd_arg_str);
2399 delete [] cwd_arg_str[0];
2400 delete [] cwd_arg_str;
2409 ret = yarp::run::impl::execvp(arg_str[0], arg_str);
2416 std::string out=std::string(
"ABORTED: server=")+mPortName
2417 +std::string(
" alias=")+strAlias
2418 +std::string(
" cmd=")+strCmd
2419 +std::string(
"\nCan't execute command because ")+strerror(
error)
2422 FILE* out_to_parent=fdopen(pipe_child_to_parent[
WRITE_TO_PIPE],
"w");
2423 fprintf(out_to_parent,
"%s", out.c_str());
2424 fflush(out_to_parent);
2425 fclose(out_to_parent);
2427 if (saved_stderr >= 0)
2429 REDIRECT_TO(STDERR_FILENO, saved_stderr);
2431 fprintf(stderr,
"%s", out.c_str());
2440 if (IS_PARENT_OF(pid_cmd))
2442 auto* pInf =
new YarpRunProcInfo(strAlias, mPortName, pid_cmd,
nullptr,
false);
2444 if (msg.
check(
"env")) {
2447 mProcessVector->Add(pInf);
2449 sprintf(pidstr,
"%d", pid_cmd);
2453 FILE* in_from_child=fdopen(pipe_child_to_parent[
READ_FROM_PIPE],
"r");
2454 int flags=fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_GETFL, 0);
2455 fcntl(pipe_child_to_parent[
READ_FROM_PIPE], F_SETFL, flags|O_NONBLOCK);
2465 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
2469 out+=std::string(buff);
2472 fclose(in_from_child);
2481 out=std::string(
"STARTED: server=")+mPortName
2482 +std::string(
" alias=")+strAlias
2483 +std::string(
" cmd=")+strCmd
2488 fprintf(stderr,
"%s", out.c_str());
int SIGNAL(int pid, int signum)
std::string int2String(int x)
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 sigchld_handler(int)
An error handler that reaps the zombies.
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.