YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Run.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <yarp/run/Run.h>
14
17
18#include <yarp/os/Network.h>
19#include <yarp/os/Os.h>
20#include <yarp/os/LogStream.h>
21#include <yarp/os/RpcClient.h>
22#include <yarp/os/RpcServer.h>
23#include <yarp/os/Semaphore.h>
24#include <yarp/os/SystemInfo.h>
25#include <yarp/os/Time.h>
26
30
31#include <cstdio>
32#include <string>
33#include <cstring>
34#include <random>
35#include <iomanip>
36
37#if defined(_WIN32)
38# if !defined(WIN32_LEAN_AND_MEAN)
39# define WIN32_LEAN_AND_MEAN
40# endif
41# include <windows.h>
42#else
43# define C_MAXARGS 128 // the max number of command parameters. rational?
44#endif
45
46#if defined(_WIN32)
47YarpRunInfoVector yarp::run::Run::mProcessVector;
48YarpRunInfoVector yarp::run::Run::mStdioVector;
49inline std::string lastError2String()
50{
51 int error=GetLastError();
52 char buff[1024];
53 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error, 0, buff, 1024, nullptr);
54
55 return std::string(buff);
56}
57#else
58//#define SIGSTDIO SIGHUP
59YarpRunInfoVector* yarp::run::Run::mProcessVector = nullptr;
60YarpRunInfoVector* yarp::run::Run::mStdioVector = nullptr;
61ZombieHunterThread* yarp::run::Run::mBraveZombieHunter = nullptr;
62#endif
63
65// OS INDEPENDENT FUNCTIONS
67
68std::string yarp::run::Run::mPortName;
69yarp::os::RpcServer* yarp::run::Run::pServerPort=nullptr;
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");
74
76
77static RunTerminator *pTerminator = nullptr;
78
79void sigstdio_handler(int sig)
80{
81 char msg[16];
83 sprintf(msg, "SIGNAL %d", sig);
84 RUNLOG(msg);
85
86 if (pTerminator) {
88 }
89}
90
92
93static std::string getProcLabel(const yarp::os::Bottle& msg)
94{
95 auto ss = yarp::conf::string::split(msg.find("env").asString(), ';');
96 for (const auto& s_iter : ss)
97 {
98 auto sss = yarp::conf::string::split(s_iter, '=');
99 if (sss.size() == 2 && sss[0] == "YARP_LOG_PROCESS_LABEL")
100 {
101 return sss[1];
102 }
103 }
104 return "";
105}
106
108int yarp::run::Run::main(int argc, char *argv[])
109{
110 yarp::os::Property config;
111 config.fromCommand(argc, argv, false);
112
113 // SERVER
114 if (config.check("server"))
115 {
116 mLogged=config.check("log");
117
118 if (mLogged)
119 {
120 yarp::os::Bottle botPortLogger=config.findGroup("log");
121
122 if (botPortLogger.size()>1)
123 {
124 mLoggerPort=botPortLogger.get(1).asString();
125 }
126 }
127
128 mPortName=std::string(config.find("server").asString());
129 return server();
130 }
131
132 mPortName="";
133
135 {
137 {
138 fprintf(stderr, "ERROR: no yarp network found.\n");
139
140 return YARPRUN_ERROR;
141 }
142 }
143
144 // READWRITE
145 if (config.check("readwrite"))
146 {
147 std::string uuid=config.findGroup("readwrite").get(1).asString();
148 std::string fPortName;
149 std::string lPortName;
150
151 if (config.check("forward"))
152 {
153 fPortName=config.findGroup("forward").get(1).asString();
154 lPortName=config.findGroup("forward").get(2).asString();
155 }
156
157#if defined(_WIN32)
158 yarp::os::impl::signal(SIGINT, sigstdio_handler);
159 yarp::os::impl::signal(SIGTERM, sigstdio_handler);
160 yarp::os::impl::signal(SIGBREAK, sigstdio_handler);
161#elif defined(__APPLE__)
162 //prctl(PR_SET_PDEATHSIG, SIGTERM);
163
164 struct sigaction new_action;
165 new_action.sa_handler=sigstdio_handler;
166 sigfillset(&new_action.sa_mask);
167 new_action.sa_flags=0;
168
169 sigaction(SIGTERM, &new_action, nullptr);
170 sigaction(SIGHUP, &new_action, nullptr);
171 //yarp::os::impl::signal(SIGHUP, SIG_IGN);
172 //yarp::os::impl::signal(SIGINT, SIG_IGN);
173 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
174
175 if (getppid()==1) return 0;
176#else
177 yarp::run::impl::prctl(PR_SET_PDEATHSIG, SIGTERM);
178
179 struct sigaction new_action;
180 new_action.sa_handler=sigstdio_handler;
181 yarp::os::impl::sigfillset(&new_action.sa_mask);
182 new_action.sa_flags=0;
183
184 yarp::os::impl::sigaction(SIGTERM, &new_action, nullptr);
185 yarp::os::impl::signal(SIGHUP, SIG_IGN);
186 //yarp::os::impl::signal(SIGINT, SIG_IGN);
187 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
188
189 if (yarp::os::getpid() == 1) {
190 return 0;
191 }
192#endif
193
194 RunReadWrite rw(uuid, fPortName, lPortName);
195 RunTerminator rt(&rw);
196 pTerminator=&rt;
197 rt.start();
198
199 return rw.loop();
200 }
201
202 // WRITE
203 if (config.check("write"))
204 {
205 std::string portName=config.findGroup("write").get(1).asString();
206
207#if defined(_WIN32)
208 yarp::os::impl::signal(SIGINT, sigstdio_handler);
209 yarp::os::impl::signal(SIGTERM, sigstdio_handler);
210 yarp::os::impl::signal(SIGBREAK, sigstdio_handler);
211#else
212 struct sigaction new_action;
213 new_action.sa_handler=sigstdio_handler;
214 yarp::os::impl::sigfillset(&new_action.sa_mask);
215 new_action.sa_flags=0;
216 yarp::os::impl::sigaction(SIGTERM, &new_action, nullptr);
217 //yarp::os::impl::signal(SIGINT, SIG_IGN);
218 yarp::os::impl::signal(SIGPIPE, SIG_IGN);
219 yarp::os::impl::signal(SIGHUP, SIG_IGN);
220#endif
221
222 if (config.check("log"))
223 {
224 std::string loggerName=config.find("log").asString();
225 RunWrite w(portName, loggerName);
226 RunTerminator rt(&w);
227 pTerminator=&rt;
228 rt.start();
229 return w.loop();
230 }
231 else
232 {
233 RunWrite w(portName);
234 RunTerminator rt(&w);
235 pTerminator=&rt;
236 rt.start();
237 return w.loop();
238 }
239
240 return 0;
241 }
242
243 // READ
244 if (config.check("read"))
245 {
246 std::string uuid=config.findGroup("read").get(1).asString();
247
248 #if defined(_WIN32)
249 yarp::os::impl::signal(SIGINT, sigstdio_handler);
250 yarp::os::impl::signal(SIGTERM, sigstdio_handler);
251 yarp::os::impl::signal(SIGBREAK, sigstdio_handler);
252 #else
253 //yarp::os::impl::signal(SIGINT, SIG_IGN);
254 yarp::os::impl::signal(SIGTERM, sigstdio_handler);
255 yarp::os::impl::signal(SIGHUP, SIG_IGN);
256 #endif
257
258 RunRead r(uuid);
259 RunTerminator rt(&r);
260 pTerminator=&rt;
261 rt.start();
262
263 return r.loop();
264 }
265
266 // STRESSTEST
267 if (config.check("stresstest"))
268 {
269 fprintf(stderr, "Yarprun stress test started.\n");
270 fflush(stderr);
271
272 int max_interval_ms=config.find("stresstest").asInt32();
273 std::string tag_zero=config.find("as").asString();
274 yarp::os::Bottle srv=config.findGroup("on");
275
276 config.unput("as");
277 config.unput("stresstest");
278
279 std::string cmd;
280
281 bool isCommand=false;
282
283 if (config.check("cmd"))
284 {
285 isCommand=true;
286 cmd=config.find("cmd").asString();
287 config.unput("cmd");
288 }
289
290 unsigned int t=0, u=0;
291 int term_cycle=0;
292
293 char tag[256];
294 char cmd_and_name[512];
295
296 mStresstest=true;
297
298 std::random_device rd;
299 std::mt19937 mt(rd());
300 std::uniform_int_distribution<int> dist0maxint(0, max_interval_ms -1);
301
302 while (mStresstest)
303 {
304 yarp::os::SystemClock::delaySystem(0.001*(dist0maxint(mt)));
305
306 yarp::os::Property stresser=config;
307
308 sprintf(tag, "%s_%u", tag_zero.c_str(), t++);
309 stresser.put("as", tag);
310
311 if (isCommand)
312 {
313 sprintf(cmd_and_name, "%s --name /%s", cmd.c_str(), tag);
314 stresser.put("cmd", cmd_and_name);
315 }
316
317 client(stresser);
318
319 std::uniform_int_distribution<int> dist07(0, 7);
320 if (isCommand && ++term_cycle>=4)
321 {
322 term_cycle=0;
323
324 int r = t - (dist07(mt));
325
326 for (int i=u; i<r; ++i)
327 {
328 sprintf(tag, "%s_%u", tag_zero.c_str(), i);
329
331 as.addString("sigterm");
332 as.addString(tag);
333
334 yarp::os::Bottle term;
335 term.addList()=srv;
336 term.addList()=as;
337
338 sendMsg(term, srv.get(1).asString());
339
340 ++u;
341 }
342 }
343 }
344
345 return 0;
346 }
347
348 // HELP
349 if (config.check("help"))
350 {
351 Help();
352
353 return 0;
354 }
355
356 // CLIENT (config is from keyboard)
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"))
368 {
369 int ret=client(config);
370
371 return ret;
372 }
373
374 Help();
375
376 return 0;
377}
378
379yarp::os::Bottle yarp::run::Run::sendMsg(yarp::os::Bottle& msg, std::string target, int RETRY, double DELAY)
380{
381 yarp::os::Bottle response;
382
383 for (int r=0; r<RETRY; ++r)
384 {
386
387#if 0
388 if (!port.open("..."))
389#else
390
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))
399#endif
400 {
402 fprintf(stderr, "retrying...");
403 continue;
404 }
405
406 std::string tempportname = port.getName();
407 bool bb = yarp::os::Network::isConnected(tempportname, target);
408
409 if (!yarp::os::Network::connect(tempportname, target, "fast_tcp", false))
410 {
411 port.close();
413 fprintf(stderr, "retrying...");
414 continue;
415 }
416
417 RUNLOG("<<<port.write(msg, response)")
418 if (!port.write(msg, response))
419 {
420 port.close();
422 fprintf(stderr, "retrying...");
423 continue;
424 }
425 RUNLOG(">>>port.write(msg, response)")
426
427 port.close();
428
429 fprintf(stderr, "RESPONSE:\n=========\n");
430 for (size_t s=0; s<response.size(); ++s)
431 {
432 fprintf(stderr, "%s\n", response.get(s).toString().c_str());
433 }
434
435 return response;
436 }
437
438 response.addString("RESPONSE:\n");
439 response.addString("=========\n");
440 response.addString("Cannot connect to remote server, aborting...\n");
441 for (size_t s=0; s<response.size(); ++s)
442 {
443 fprintf(stderr, "%s\n", response.get(s).toString().c_str());
444 }
445 return response;
446}
447
448
450// WINDOWS SERVER
451#if defined(_WIN32)
452
454#else // LINUX SERVER
456
457static void sigchld_handler(int sig)
458{
459 YARP_UNUSED(sig);
460 if (yarp::run::Run::mBraveZombieHunter)
461 {
462 yarp::run::Run::mBraveZombieHunter->sigchldHandler();
463 }
464}
465
466#endif
467
468
470// OS DEPENDENT FUNCTIONS
472
473// WINDOWS
474
475#if defined(_WIN32)
476
477// CMD SERVER
478int yarp::run::Run::executeCmdAndStdio(const yarp::os::Bottle& msg, yarp::os::Bottle& result)
479{
480 std::string strAlias=msg.find("as").asString();
481 std::string strStdio=msg.find("stdio").asString();
482 std::string strStdioUUID=msg.find("stdiouuid").asString();
483 //std::string strCmdUUID=mPortName+"/"+int2String(GetCurrentProcessId())+"/"+strAlias+"-"+int2String(mProcCNT++);
484
485 // PIPES
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);
494
495 // RUN STDOUT
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));
500
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;
506
507 BOOL bSuccess=CreateProcess(nullptr, // command name
508 (char*)(std::string("yarprun --write ")+strStdioUUID).c_str(), // command line
509 nullptr, // process security attributes
510 nullptr, // primary thread security attributes
511 TRUE, // handles are inherited
512 CREATE_NEW_PROCESS_GROUP, // creation flags
513 nullptr, // use parent's environment
514 nullptr, // use parent's current directory
515 &stdout_startup_info, // STARTUPINFO pointer
516 &stdout_process_info); // receives PROCESS_INFORMATION
517
518 if (!bSuccess)
519 {
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()
524 +std::string("\n");
525
526 result.addInt32(YARPRUN_ERROR);
527 result.addString(strError.c_str());
528 fprintf(stderr, "%s", strError.c_str());
529 fflush(stderr);
530
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);
535
536 return YARPRUN_ERROR;
537 }
538
539 // RUN STDIN
540
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));
545
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;
551
552 bSuccess=CreateProcess(nullptr, // command name
553 (char*)(std::string("yarprun --read ")+strStdioUUID).c_str(), // command line
554 nullptr, // process security attributes
555 nullptr, // primary thread security attributes
556 TRUE, // handles are inherited
557 CREATE_NEW_PROCESS_GROUP, // creation flags
558 nullptr, // use parent's environment
559 nullptr, // use parent's current directory
560 &stdin_startup_info, // STARTUPINFO pointer
561 &stdin_process_info); // receives PROCESS_INFORMATION
562
563 if (!bSuccess)
564 {
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()
569 +std::string("\n");
570
571 result.addInt32(YARPRUN_ERROR);
572 result.addString(strError.c_str());
573 fprintf(stderr, "%s", strError.c_str());
574
575 TerminateProcess(stdout_process_info.hProcess, YARPRUN_ERROR);
576
577 CloseHandle(stdout_process_info.hProcess);
578
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);
583
584 return YARPRUN_ERROR;
585 }
586
587 // RUN COMMAND
588
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));
593
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;
599
600 yarp::os::Bottle botCmd=msg.findGroup("cmd").tail();
601
602 std::string strCmd;
603 for (int s=0; s<botCmd.size(); ++s)
604 {
605 strCmd+=botCmd.get(s).toString()+std::string(" ");
606 }
607
608 /*
609 * setting environment variable for child process
610 */
611 TCHAR chNewEnv[32767];
612
613 // Get a pointer to the env block.
614 LPTCH chOldEnv = GetEnvironmentStrings();
615
616 // copying parent env variables
617 LPTSTR lpOld = (LPTSTR) chOldEnv;
618 LPTSTR lpNew = (LPTSTR) chNewEnv;
619 while (*lpOld)
620 {
621 lstrcpy(lpNew, lpOld);
622 lpOld += lstrlen(lpOld) + 1;
623 lpNew += lstrlen(lpNew) + 1;
624 }
625
626 // Set the YARP_IS_YARPRUN environment variable to 1, so that the child
627 // process will now that is running inside yarprun.
628 lstrcpy(lpNew, (LPTCH) "YARP_IS_YARPRUN=1");
629 lpNew += lstrlen(lpNew) + 1;
630
631 // Set the YARPRUN_IS_FORWARDING_LOG environment variable to 1, so that
632 // the child process will now that yarprun is not logging the output.
633 lstrcpy(lpNew, (LPTCH) "YARPRUN_IS_FORWARDING_LOG=1");
634 lpNew += lstrlen(lpNew) + 1;
635
636 // adding new env variables
637 std::string cstrEnvName;
638 if (msg.check("env"))
639 {
640 auto ss = yarp::conf::string::split(msg.find("env").asString(), ';');
641 for (const auto& s : ss) {
642 lstrcpy(lpNew, (LPTCH) s.c_str());
643 lpNew += lstrlen(lpNew) + 1;
644 }
645 }
646
647 // closing env block
648 *lpNew = (TCHAR)0;
649
650 bool bWorkdir=msg.check("workdir");
651 std::string strWorkdir=bWorkdir?msg.find("workdir").asString()+"\\":"";
652
653 bSuccess=CreateProcess(nullptr, // command name
654 (char*)(strWorkdir+strCmd).c_str(), // command line
655 nullptr, // process security attributes
656 nullptr, // primary thread security attributes
657 TRUE, // handles are inherited
658 CREATE_NEW_PROCESS_GROUP, // creation flags
659 (LPVOID) chNewEnv, // use new environment list
660 bWorkdir ? strWorkdir.c_str() : nullptr, // working directory
661 &cmd_startup_info, // STARTUPINFO pointer
662 &cmd_process_info); // receives PROCESS_INFORMATION
663
664 if (!bSuccess && bWorkdir)
665 {
666 bSuccess=CreateProcess(nullptr, // command name
667 (char*)(strCmd.c_str()), // command line
668 nullptr, // process security attributes
669 nullptr, // primary thread security attributes
670 TRUE, // handles are inherited
671 CREATE_NEW_PROCESS_GROUP, // creation flags
672 (LPVOID) chNewEnv, // use new environment list
673 strWorkdir.c_str(), // working directory
674 &cmd_startup_info, // STARTUPINFO pointer
675 &cmd_process_info); // receives PROCESS_INFORMATION
676 }
677
678 // deleting old environment variable
679 FreeEnvironmentStrings(chOldEnv);
680
681 if (!bSuccess)
682 {
683 result.addInt32(YARPRUN_ERROR);
684
685 DWORD nBytes;
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)
690 +std::string("\n");
691
692 WriteFile(write_to_pipe_cmd_to_stdout, line1.c_str(), line1.length(), &nBytes, 0);
693
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);
697
698 std::string out=line1+line2;
699 result.addString(out.c_str());
700 fprintf(stderr, "%s", out.c_str());
701 fflush(stderr);
702
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);
707
708 TerminateProcess(stdout_process_info.hProcess, YARPRUN_ERROR);
709
710 CloseHandle(stdout_process_info.hProcess);
711
712 TerminateProcess(stdin_process_info.hProcess, YARPRUN_ERROR);
713
714 CloseHandle(stdin_process_info.hProcess);
715
716 return YARPRUN_ERROR;
717 }
718
719 FlushFileBuffers(write_to_pipe_cmd_to_stdout);
720
721 // EVERYTHING IS ALL RIGHT
723 mPortName,
724 strStdio,
725 cmd_process_info.dwProcessId,
726 strStdioUUID,
727 &mStdioVector,
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,
735 false);
736
737 pInf->setCmd(strCmd);
738 if (msg.check("env"))
739 {
740 pInf->setEnv(msg.find("env").asString());
741 }
742 mProcessVector.Add(pInf);
743
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)
749 +std::string("\n");
750
751 result.addString(out.c_str());
752 result.addString(strStdioUUID.c_str());
753 fprintf(stderr, "%s", out.c_str());
754
755 return cmd_process_info.dwProcessId;
756}
757
758int yarp::run::Run::executeCmdStdout(const yarp::os::Bottle& msg, yarp::os::Bottle& result, std::string& loggerName)
759{
760 std::string proc_label = getProcLabel(msg);
761
762 std::string strAlias=msg.find("as").asString();
763 std::string portName="/log";
764 portName+=mPortName+"/";
765 std::string command = msg.findGroup("cmd").get(1).asString();
766 command = command.substr(0, command.find(' '));
767 command = command.substr(command.find_last_of("\\/") + 1);
768 portName+=command;
769 if (proc_label != "") { portName += "[" + proc_label + "]"; }
770
771 // PIPES
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);
778
779 // RUN STDOUT
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));
784
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;
790
791 BOOL bSuccess=CreateProcess(nullptr, // command name
792 (char*)(std::string("yarprun --log ")+loggerName+std::string(" --write ")+portName).c_str(), // command line
793 nullptr, // process security attributes
794 nullptr, // primary thread security attributes
795 TRUE, // handles are inherited
796 CREATE_NEW_PROCESS_GROUP, // creation flags
797 nullptr, // use parent's environment
798 nullptr, // use parent's current directory
799 &stdout_startup_info, // STARTUPINFO pointer
800 &stdout_process_info); // receives PROCESS_INFORMATION
801
802 if (!bSuccess)
803 {
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()
808 +std::string("\n");
809
810 result.addInt32(YARPRUN_ERROR);
811 result.addString(strError.c_str());
812 fprintf(stderr, "%s", strError.c_str());
813 fflush(stderr);
814
815 CloseHandle(write_to_pipe_cmd_to_stdout);
816 CloseHandle(read_from_pipe_cmd_to_stdout);
817
818 return YARPRUN_ERROR;
819 }
820
821 // RUN COMMAND
822
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));
827
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;
833
834 yarp::os::Bottle botCmd=msg.findGroup("cmd").tail();
835
836 std::string strCmd;
837 for (int s=0; s<botCmd.size(); ++s)
838 {
839 strCmd+=botCmd.get(s).toString()+std::string(" ");
840 }
841
842 /*
843 * setting environment variable for child process
844 */
845 TCHAR chNewEnv[32767];
846
847 // Get a pointer to the env block.
848 LPTCH chOldEnv = GetEnvironmentStrings();
849
850 // copying parent env variables
851 LPTSTR lpOld = (LPTSTR) chOldEnv;
852 LPTSTR lpNew = (LPTSTR) chNewEnv;
853 while (*lpOld)
854 {
855 lstrcpy(lpNew, lpOld);
856 lpOld += lstrlen(lpOld) + 1;
857 lpNew += lstrlen(lpNew) + 1;
858 }
859
860 // Set the YARP_IS_YARPRUN environment variable to 1, so that the child
861 // process will now that is running inside yarprun.
862 lstrcpy(lpNew, (LPTCH) "YARP_IS_YARPRUN=1");
863 lpNew += lstrlen(lpNew) + 1;
864
865 // Set the YARPRUN_IS_FORWARDING_LOG environment variable to 1, so that
866 // the child process will now that yarprun is not logging the output.
867 lstrcpy(lpNew, (LPTCH) "YARPRUN_IS_FORWARDING_LOG=1");
868 lpNew += lstrlen(lpNew) + 1;
869
870 // adding new env variables
871 std::string cstrEnvName;
872 if (msg.check("env"))
873 {
874 auto ss = yarp::conf::string::split(msg.find("env").asString(), ';');
875 for (const auto& s : ss) {
876 lstrcpy(lpNew, (LPTCH)s.c_str());
877 lpNew += lstrlen(lpNew) + 1;
878 }
879 }
880
881 // closing env block
882 *lpNew = (TCHAR)0;
883
884 bool bWorkdir=msg.check("workdir");
885 std::string strWorkdir=bWorkdir?msg.find("workdir").asString()+"\\":"";
886
887 bSuccess=CreateProcess(nullptr, // command name
888 (char*)(strWorkdir+strCmd).c_str(), // command line
889 nullptr, // process security attributes
890 nullptr, // primary thread security attributes
891 TRUE, // handles are inherited
892 CREATE_NEW_PROCESS_GROUP, // creation flags
893 (LPVOID) chNewEnv, // use new environment list
894 bWorkdir?strWorkdir.c_str():nullptr, // working directory
895 &cmd_startup_info, // STARTUPINFO pointer
896 &cmd_process_info); // receives PROCESS_INFORMATION
897
898 if (!bSuccess && bWorkdir)
899 {
900 bSuccess=CreateProcess(nullptr, // command name
901 (char*)(strCmd.c_str()), // command line
902 nullptr, // process security attributes
903 nullptr, // primary thread security attributes
904 TRUE, // handles are inherited
905 CREATE_NEW_PROCESS_GROUP, // creation flags
906 (LPVOID) chNewEnv, // use new environment list
907 strWorkdir.c_str(), // working directory
908 &cmd_startup_info, // STARTUPINFO pointer
909 &cmd_process_info); // receives PROCESS_INFORMATION
910 }
911
912 // deleting old environment variable
913 FreeEnvironmentStrings(chOldEnv);
914
915 if (!bSuccess)
916 {
917 result.addInt32(YARPRUN_ERROR);
918
919 DWORD nBytes;
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)
924 +std::string("\n");
925
926 WriteFile(write_to_pipe_cmd_to_stdout, line1.c_str(), line1.length(), &nBytes, 0);
927
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);
931
932 std::string out=line1+line2;
933 result.addString(out.c_str());
934 fprintf(stderr, "%s", out.c_str());
935 fflush(stderr);
936
937 CloseHandle(write_to_pipe_cmd_to_stdout);
938 CloseHandle(read_from_pipe_cmd_to_stdout);
939
940 TerminateProcess(stdout_process_info.hProcess, YARPRUN_ERROR);
941
942 CloseHandle(stdout_process_info.hProcess);
943
944 return YARPRUN_ERROR;
945 }
946
947 FlushFileBuffers(write_to_pipe_cmd_to_stdout);
948
949 // EVERYTHING IS ALL RIGHT
951 mPortName,
952 portName,
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,
958 false);
959
960
961
962
963 pInf->setCmd(strCmd);
964 if (msg.check("env"))
965 {
966 pInf->setEnv(msg.find("env").asString());
967 }
968 mProcessVector.Add(pInf);
969
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)
975 +std::string("\n");
976
977 result.addString(out.c_str());
978 result.addString(portName.c_str());
979 fprintf(stderr, "%s", out.c_str());
980
981 return cmd_process_info.dwProcessId;
982}
983
984
985int yarp::run::Run::executeCmd(const yarp::os::Bottle& msg, yarp::os::Bottle& result)
986{
987 std::string strAlias=msg.find("as").asString().c_str();
988
989 // RUN COMMAND
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));
994
995 cmd_startup_info.cb=sizeof(STARTUPINFO);
996
997 yarp::os::Bottle botCmd=msg.findGroup("cmd").tail();
998
999 std::string strCmd;
1000 for (int s=0; s<botCmd.size(); ++s)
1001 {
1002 strCmd+=botCmd.get(s).toString()+std::string(" ");
1003 }
1004
1005 /*
1006 * setting environment variable for child process
1007 */
1008 TCHAR chNewEnv[32767];
1009
1010 // Get a pointer to the env block.
1011 LPTCH chOldEnv = GetEnvironmentStrings();
1012
1013 // copying parent env variables
1014 LPTSTR lpOld = (LPTSTR) chOldEnv;
1015 LPTSTR lpNew = (LPTSTR) chNewEnv;
1016 while (*lpOld)
1017 {
1018 lstrcpy(lpNew, lpOld);
1019 lpOld += lstrlen(lpOld) + 1;
1020 lpNew += lstrlen(lpNew) + 1;
1021 }
1022
1023 // Set the YARP_IS_YARPRUN environment variable to 1, so that the child
1024 // process will know that is running inside yarprun.
1025 lstrcpy(lpNew, (LPTCH) "YARP_IS_YARPRUN=1");
1026 lpNew += lstrlen(lpNew) + 1;
1027
1028 // Set the YARPRUN_IS_FORWARDING_LOG environment variable to 0, so that
1029 // the child process will know that yarprun is not logging the output.
1030 lstrcpy(lpNew, (LPTCH) "YARPRUN_IS_FORWARDING_LOG=0");
1031 lpNew += lstrlen(lpNew) + 1;
1032
1033 // adding new env variables
1034 std::string cstrEnvName;
1035 if (msg.check("env"))
1036 {
1037 auto ss = yarp::conf::string::split(msg.find("env").asString(), ';');
1038 for (const auto& s : ss) {
1039 lstrcpy(lpNew, (LPTCH)s.c_str());
1040 lpNew += lstrlen(lpNew) + 1;
1041 }
1042 }
1043
1044 // closing env block
1045 *lpNew = (TCHAR)0;
1046
1047 bool bWorkdir=msg.check("workdir");
1048 std::string strWorkdir=bWorkdir?msg.find("workdir").asString()+"\\":"";
1049
1050 BOOL bSuccess=CreateProcess(nullptr, // command name
1051 (char*)(strWorkdir+strCmd).c_str(), // command line
1052 nullptr, // process security attributes
1053 nullptr, // primary thread security attributes
1054 TRUE, // handles are inherited
1055 CREATE_NEW_PROCESS_GROUP, // creation flags
1056 (LPVOID) chNewEnv, // use new environment
1057 bWorkdir ? strWorkdir.c_str() : nullptr, // working directory
1058 &cmd_startup_info, // STARTUPINFO pointer
1059 &cmd_process_info); // receives PROCESS_INFORMATION
1060
1061 if (!bSuccess && bWorkdir)
1062 {
1063 bSuccess=CreateProcess(nullptr, // command name
1064 (char*)(strCmd.c_str()), // command line
1065 nullptr, // process security attributes
1066 nullptr, // primary thread security attributes
1067 TRUE, // handles are inherited
1068 CREATE_NEW_PROCESS_GROUP, // creation flags
1069 (LPVOID) chNewEnv, // use new environment
1070 strWorkdir.c_str(), // working directory
1071 &cmd_startup_info, // STARTUPINFO pointer
1072 &cmd_process_info); // receives PROCESS_INFORMATION
1073 }
1074
1075 // deleting old environment variable
1076 FreeEnvironmentStrings(chOldEnv);
1077
1078 if (!bSuccess)
1079 {
1080 result.addInt32(YARPRUN_ERROR);
1081
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()
1087 +std::string("\n");
1088
1089 result.addString(out.c_str());
1090 fprintf(stderr, "%s", out.c_str());
1091 fflush(stderr);
1092
1093 return YARPRUN_ERROR;
1094 }
1095
1096 // EVERYTHING IS ALL RIGHT
1097 YarpRunProcInfo* pInf = new YarpRunProcInfo(strAlias,
1098 mPortName,
1099 cmd_process_info.dwProcessId,
1100 cmd_process_info.hProcess,
1101 false);
1102 pInf->setCmd(strCmd);
1103 if (msg.check("env"))
1104 pInf->setEnv(msg.find("env").asString());
1105
1106 mProcessVector.Add(pInf);
1107
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)
1113 +std::string("\n");
1114
1115 fprintf(stderr, "%s", out.c_str());
1116
1117 return cmd_process_info.dwProcessId;
1118}
1119
1120// STDIO SERVER
1121int yarp::run::Run::userStdio(const yarp::os::Bottle& msg, yarp::os::Bottle& result)
1122{
1123 PROCESS_INFORMATION stdio_process_info;
1124 ZeroMemory(&stdio_process_info, sizeof(PROCESS_INFORMATION));
1125
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;
1131
1132 std::string strAlias=msg.find("as").asString();
1133 std::string strUUID=msg.find("stdiouuid").asString();
1134 std::string strCmd=std::string("yarprun --readwrite ")+strUUID;
1135 if (msg.check("forward")) strCmd+=std::string(" --forward ")+msg.findGroup("forward").get(1).asString()+std::string(" ")+msg.findGroup("forward").get(2).asString();
1136
1137 BOOL bSuccess=CreateProcess(nullptr, // command name
1138 (char*)strCmd.c_str(), // command line
1139 nullptr, // process security attributes
1140 nullptr, // primary thread security attributes
1141 TRUE, // handles are inherited
1142 CREATE_NEW_CONSOLE, // creation flags
1143 nullptr, // use parent's environment
1144 nullptr, // use parent's current directory
1145 &stdio_startup_info, // STARTUPINFO pointer
1146 &stdio_process_info); // receives PROCESS_INFORMATION
1147
1148 std::string out;
1149
1150 if (bSuccess)
1151 {
1152 mStdioVector.Add(new YarpRunProcInfo(strAlias,
1153 mPortName,
1154 stdio_process_info.dwProcessId,
1155 stdio_process_info.hProcess,
1156 false));
1157
1158 out=std::string("STARTED: server=")+mPortName
1159 +std::string(" alias=")+strAlias
1160 +std::string(" cmd=stdio pid=")+int2String(stdio_process_info.dwProcessId)
1161 +std::string("\n");
1162 }
1163 else
1164 {
1165 stdio_process_info.dwProcessId=YARPRUN_ERROR;
1166
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()
1171 +std::string("\n");
1172 }
1173
1174 result.clear();
1175 result.addInt32(stdio_process_info.dwProcessId);
1176 result.addString(out.c_str());
1177 fprintf(stderr, "%s", out.c_str());
1178 fflush(stderr);
1179
1180 return stdio_process_info.dwProcessId;
1181}
1182
1184#else // LINUX
1186
1189void splitLine(char *pLine, char **pArgs)
1190{
1191 char *pTmp = strchr(pLine, ' ');
1192
1193 if (pTmp) {
1194 *pTmp = '\0';
1195 pTmp++;
1196 while ((*pTmp) && (*pTmp == ' ')) {
1197 pTmp++;
1198 }
1199 if (*pTmp == '\0') {
1200 pTmp = nullptr;
1201 }
1202 }
1203 *pArgs = pTmp;
1204}
1205
1209void parseArguments(char *io_pLine, int *o_pArgc, char **o_pArgv)
1210{
1211 char *pNext = io_pLine;
1212 size_t i;
1213 int j;
1214 int quoted = 0;
1215 size_t len = strlen(io_pLine);
1216
1217 // Protect spaces inside quotes, but lose the quotes
1218 for(i = 0; i < len; i++) {
1219 if ((!quoted) && ('"' == io_pLine[i])) {
1220 quoted = 1;
1221 io_pLine[i] = ' ';
1222 } else if ((quoted) && ('"' == io_pLine[i])) {
1223 quoted = 0;
1224 io_pLine[i] = ' ';
1225 } else if ((quoted) && (' ' == io_pLine[i])) {
1226 io_pLine[i] = '\1';
1227 }
1228 }
1229
1230 // init
1231 memset(o_pArgv, 0x00, sizeof(char*) * C_MAXARGS);
1232 *o_pArgc = 1;
1233 o_pArgv[0] = io_pLine;
1234
1235 while ((nullptr != pNext) && (*o_pArgc < C_MAXARGS)) {
1236 splitLine(pNext, &(o_pArgv[*o_pArgc]));
1237 pNext = o_pArgv[*o_pArgc];
1238
1239 if (nullptr != o_pArgv[*o_pArgc]) {
1240 *o_pArgc += 1;
1241 }
1242 }
1243
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] = ' ';
1249 }
1250 }
1251 }
1252}
1253
1254void yarp::run::Run::CleanZombie(int pid)
1255{
1256 bool bFound=mProcessVector && mProcessVector->CleanZombie(pid);
1257
1258 if (!bFound) {
1259 if (mStdioVector) {
1260 mStdioVector->CleanZombie(pid);
1261 }
1262 }
1263}
1264
1266
1267int yarp::run::Run::executeCmdAndStdio(const yarp::os::Bottle& msg, yarp::os::Bottle& result)
1268{
1269 std::string strAlias=msg.find("as").asString();
1270 std::string strCmd=msg.find("cmd").asString();
1271 std::string strStdio=msg.find("stdio").asString();
1272 std::string strStdioUUID=msg.find("stdiouuid").asString();
1273
1274 int pipe_stdin_to_cmd[2];
1275 int ret_stdin_to_cmd=yarp::run::impl::pipe(pipe_stdin_to_cmd);
1276
1277 int pipe_cmd_to_stdout[2];
1278 int ret_cmd_to_stdout=yarp::run::impl::pipe(pipe_cmd_to_stdout);
1279
1280 int pipe_child_to_parent[2];
1281 int ret_child_to_parent=yarp::run::impl::pipe(pipe_child_to_parent);
1282
1283 if (ret_child_to_parent!=0 || ret_cmd_to_stdout!=0 || ret_stdin_to_cmd!=0)
1284 {
1285 int error=errno;
1286
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)
1291 +std::string("\n");
1292
1293 result.addInt32(YARPRUN_ERROR);
1294 result.addString(out.c_str());
1295 fprintf(stderr, "%s", out.c_str());
1296
1297 return YARPRUN_ERROR;
1298 }
1299
1300 int pid_stdout=yarp::run::impl::fork();
1301
1302 if (IS_INVALID(pid_stdout))
1303 {
1304 int error=errno;
1305
1306 CLOSE(pipe_stdin_to_cmd[WRITE_TO_PIPE]);
1307 CLOSE(pipe_stdin_to_cmd[READ_FROM_PIPE]);
1308 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1309 CLOSE(pipe_cmd_to_stdout[READ_FROM_PIPE]);
1310 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1311 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
1312
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)
1317 +std::string("\n");
1318
1319 result.addInt32(YARPRUN_ERROR);
1320 result.addString(out.c_str());
1321 fprintf(stderr, "%s", out.c_str());
1322
1323 return YARPRUN_ERROR;
1324 }
1325
1326 if (IS_NEW_PROCESS(pid_stdout)) // STDOUT IMPLEMENTED HERE
1327 {
1328 REDIRECT_TO(STDIN_FILENO, pipe_cmd_to_stdout[READ_FROM_PIPE]);
1329
1330 CLOSE(pipe_stdin_to_cmd[WRITE_TO_PIPE]);
1331 CLOSE(pipe_stdin_to_cmd[READ_FROM_PIPE]);
1332 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1333 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
1334
1335 //Why removing vectors and stop threads?
1336 //exec* never returns and memory is claimed by the system
1337 //furthermore after fork() only the thread which called fork() is forked!
1338 // cleanBeforeExec();
1339
1340 //yarp::os::impl::signal(SIGPIPE, SIG_DFL);
1341
1342 int ret = yarp::run::impl::execlp("yarprun", "yarprun", "--write", strStdioUUID.c_str(), static_cast<char*>(nullptr));
1343
1344 CLOSE(pipe_cmd_to_stdout[READ_FROM_PIPE]);
1345
1346 if (ret==YARPRUN_ERROR)
1347 {
1348 int error=errno;
1349
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)
1354 +std::string("\n");
1355
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);
1360
1361 fprintf(stderr, "%s", out.c_str());
1362 }
1363
1364 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1365
1366 std::exit(ret);
1367 }
1368
1369 if (IS_PARENT_OF(pid_stdout))
1370 {
1371 CLOSE(pipe_cmd_to_stdout[READ_FROM_PIPE]);
1372
1373 fprintf(stderr, "STARTED: server=%s alias=%s cmd=stdout pid=%d\n", mPortName.c_str(), strAlias.c_str(), pid_stdout);
1374
1375 int pid_stdin=yarp::run::impl::fork();
1376
1377 if (IS_INVALID(pid_stdin))
1378 {
1379 int error=errno;
1380
1381 CLOSE(pipe_stdin_to_cmd[WRITE_TO_PIPE]);
1382 CLOSE(pipe_stdin_to_cmd[READ_FROM_PIPE]);
1383 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1384 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1385 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
1386
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)
1391 +std::string("\n");
1392
1393 result.addInt32(YARPRUN_ERROR);
1394 result.addString(out.c_str());
1395 fprintf(stderr, "%s", out.c_str());
1396
1397 SIGNAL(pid_stdout, SIGTERM);
1398 fprintf(stderr, "TERMINATING stdout (%d)\n", pid_stdout);
1399
1400 return YARPRUN_ERROR;
1401 }
1402
1403 if (IS_NEW_PROCESS(pid_stdin)) // STDIN IMPLEMENTED HERE
1404 {
1405 yarp::conf::environment::set_string("YARP_QUIET", "1");
1406 REDIRECT_TO(STDOUT_FILENO, pipe_stdin_to_cmd[WRITE_TO_PIPE]);
1407 REDIRECT_TO(STDERR_FILENO, pipe_stdin_to_cmd[WRITE_TO_PIPE]);
1408
1409 CLOSE(pipe_stdin_to_cmd[READ_FROM_PIPE]);
1410 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1411 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
1412
1413 //Why removing vectors and stop threads?
1414 //exec* never returns and memory is claimed by the system
1415 //furthermore after fork() only the thread which called fork() is forked!
1416 // cleanBeforeExec();
1417
1418 //yarp::os::impl::signal(SIGPIPE, SIG_DFL);
1419
1420 int ret = yarp::run::impl::execlp("yarprun", "yarprun", "--read", strStdioUUID.c_str(), static_cast<char*>(nullptr));
1421
1422 CLOSE(pipe_stdin_to_cmd[WRITE_TO_PIPE]);
1423
1424 if (ret==YARPRUN_ERROR)
1425 {
1426 int error=errno;
1427
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)
1432 +std::string("\n");
1433
1434
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());
1440 }
1441
1442 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1443
1444 std::exit(ret);
1445 }
1446
1447 if (IS_PARENT_OF(pid_stdin))
1448 {
1449 // connect yarp read and write
1450 CLOSE(pipe_stdin_to_cmd[WRITE_TO_PIPE]);
1451
1452 fprintf(stderr, "STARTED: server=%s alias=%s cmd=stdin pid=%d\n", mPortName.c_str(), strAlias.c_str(), pid_stdin);
1453
1454 int pid_cmd=yarp::run::impl::fork();
1455
1456 if (IS_INVALID(pid_cmd))
1457 {
1458 int error=errno;
1459
1460 CLOSE(pipe_stdin_to_cmd[READ_FROM_PIPE]);
1461 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1462 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
1463
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)
1468 +std::string("\n");
1469
1470 result.addInt32(YARPRUN_ERROR);
1471 result.addString(out.c_str());
1472 fprintf(stderr, "%s", out.c_str());
1473
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);
1478
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);
1483
1484 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1485
1486 return YARPRUN_ERROR;
1487 }
1488
1489 if (IS_NEW_PROCESS(pid_cmd)) // RUN COMMAND HERE
1490 {
1491 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
1492
1493 char *cmd_str=new char[strCmd.length()+1];
1494 strcpy(cmd_str, strCmd.c_str());
1495 /*
1496 int nargs=CountArgs(cmd_str);
1497 char **arg_str=new char*[nargs+1];
1498 ParseCmd(cmd_str, arg_str);
1499 arg_str[nargs]=0;
1500 */
1501 int nargs = 0;
1502 char **arg_str = new char*[C_MAXARGS + 1];
1503 parseArguments(cmd_str, &nargs, arg_str);
1504 arg_str[nargs]=nullptr;
1505
1506 setvbuf(stdout, nullptr, _IONBF, 0);
1507
1508 REDIRECT_TO(STDIN_FILENO, pipe_stdin_to_cmd[READ_FROM_PIPE]);
1509 REDIRECT_TO(STDOUT_FILENO, pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1510 REDIRECT_TO(STDERR_FILENO, pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1511
1512 // Set the YARP_IS_YARPRUN environment variable to 1, so that the child
1513 // process will now that is running inside yarprun.
1514 yarp::conf::environment::set_string("YARP_IS_YARPRUN", "1");
1515
1516 // Set the YARPRUN_IS_FORWARDING_LOG environment variable to 1, so that
1517 // the child process will now that yarprun is not logging the output.
1518 yarp::conf::environment::set_string("YARPRUN_IS_FORWARDING_LOG", "1");
1519
1520 if (msg.check("env"))
1521 {
1522 auto ss = yarp::conf::string::split(msg.find("env").asString(), ';');
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); // putenv doesn't make copy of the string
1527 }
1528 //delete [] szenv;
1529 }
1530
1531 if (msg.check("workdir"))
1532 {
1533 int ret = yarp::os::impl::chdir(msg.find("workdir").asString().c_str());
1534
1535 if (ret!=0)
1536 {
1537 int error=errno;
1538
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)
1543 +std::string("\n");
1544
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());
1550
1551 std::exit(ret);
1552 }
1553 }
1554
1555 int ret=YARPRUN_ERROR;
1556
1557 char currWorkDirBuff[1024];
1558 char *currWorkDir=yarp::os::impl::getcwd(currWorkDirBuff, 1024);
1559
1560 if (currWorkDir)
1561 {
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];
1565 }
1566 cwd_arg_str[nargs]=nullptr;
1567 cwd_arg_str[0]=new char[strlen(currWorkDir)+strlen(arg_str[0])+16];
1568
1569 strcpy(cwd_arg_str[0], currWorkDir);
1570 strcat(cwd_arg_str[0], "/");
1571 strcat(cwd_arg_str[0], arg_str[0]);
1572
1573 //Why removing vectors and stop threads?
1574 //exec* never returns and memory is claimed by the system
1575 //furthermore after fork() only the thread which called fork() is forked!
1576 // cleanBeforeExec();
1577
1578 ret = yarp::run::impl::execvp(cwd_arg_str[0], cwd_arg_str);
1579
1580 delete [] cwd_arg_str[0];
1581 delete [] cwd_arg_str;
1582 }
1583
1584 if (ret==YARPRUN_ERROR)
1585 {
1586 //Why removing vectors and stop threads?
1587 //exec* never returns and memory is claimed by the system
1588 //furthermore after fork() only the thread which called fork() is forked!
1589 // cleanBeforeExec();
1590
1591 ret = yarp::run::impl::execvp(arg_str[0], arg_str);
1592 }
1593
1594 fflush(stdout);
1595
1596 CLOSE(pipe_stdin_to_cmd[READ_FROM_PIPE]);
1597 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1598
1599 if (ret==YARPRUN_ERROR)
1600 {
1601 int error=errno;
1602
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)
1607 +std::string("\n");
1608
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());
1614 }
1615
1616 delete [] cmd_str;
1617 delete [] arg_str;
1618
1619 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1620
1621 std::exit(ret);
1622 }
1623
1624
1625 if (IS_PARENT_OF(pid_cmd))
1626 {
1627 CLOSE(pipe_stdin_to_cmd[READ_FROM_PIPE]);
1628 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1629 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1630
1631 auto* pInf = new YarpRunCmdWithStdioInfo(
1632 strAlias,
1633 mPortName,
1634 strStdio,
1635 pid_cmd,
1636 strStdioUUID,
1637 mStdioVector,
1638 pid_stdin,
1639 pid_stdout,
1640 pipe_stdin_to_cmd[READ_FROM_PIPE],
1641 pipe_stdin_to_cmd[WRITE_TO_PIPE],
1642 pipe_cmd_to_stdout[READ_FROM_PIPE],
1643 pipe_cmd_to_stdout[WRITE_TO_PIPE],
1644 nullptr,
1645 false
1646 );
1647
1648 pInf->setCmd(strCmd);
1649
1650 if (msg.check("env"))
1651 {
1652 pInf->setEnv(msg.find("env").asString());
1653 }
1654
1655 mProcessVector->Add(pInf);
1656
1658
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);
1662
1663 std::string out;
1664
1665 if (in_from_child)
1666 {
1667 char buff[1024];
1668
1669 while(true)
1670 {
1671 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
1672 break;
1673 }
1674
1675 out+=std::string(buff);
1676 }
1677
1678 fclose(in_from_child);
1679 }
1680
1681 if (out.length()>0)
1682 {
1683 pid_cmd=YARPRUN_ERROR;
1684 }
1685 else
1686 {
1687 out=std::string("STARTED: server=")+mPortName
1688 +std::string(" alias=")+strAlias
1689 +std::string(" cmd=")+strCmd
1690 +std::string(" pid=")+int2String(pid_cmd)
1691 +std::string("\n");
1692 }
1693
1694 result.addInt32(pid_cmd);
1695 result.addString(out.c_str());
1696 result.addString(strStdioUUID.c_str());
1697
1698 fprintf(stderr, "%s", out.c_str());
1699
1700 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
1701
1702 return pid_cmd;
1703 }
1704 }
1705 }
1706
1707 result.addInt32(YARPRUN_ERROR);
1708 result.addString("I should never reach this point!!!\n");
1709
1710 return YARPRUN_ERROR;
1711}
1712
1713int yarp::run::Run::executeCmdStdout(const yarp::os::Bottle& msg, yarp::os::Bottle& result, std::string& loggerName)
1714{
1715 std::string proc_label = getProcLabel(msg);
1716
1717 std::string strAlias=msg.find("as").asString();
1718 std::string strCmd=msg.find("cmd").asString();
1719
1720 std::string portName="/log";
1721 portName+=mPortName+"/";
1722
1723 std::string command = strCmd;
1724 command = command.substr(0, command.find(' '));
1725 command = command.substr(command.find_last_of("\\/") + 1);
1726
1727 portName+=command;
1728 if (proc_label != "") { portName += "[" + proc_label + "]"; }
1729
1730
1731 int pipe_cmd_to_stdout[2];
1732 int ret_cmd_to_stdout=yarp::run::impl::pipe(pipe_cmd_to_stdout);
1733
1734 int pipe_child_to_parent[2];
1735 int ret_child_to_parent=yarp::run::impl::pipe(pipe_child_to_parent);
1736
1737 if (ret_child_to_parent!=0 || ret_cmd_to_stdout!=0)
1738 {
1739 int error=errno;
1740
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)
1745 +std::string("\n");
1746
1747 result.addInt32(YARPRUN_ERROR);
1748 result.addString(out.c_str());
1749 fprintf(stderr, "%s", out.c_str());
1750
1751 return YARPRUN_ERROR;
1752 }
1753
1754 int pid_stdout=yarp::run::impl::fork();
1755
1756 if (IS_INVALID(pid_stdout))
1757 {
1758 int error=errno;
1759
1760 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1761 CLOSE(pipe_cmd_to_stdout[READ_FROM_PIPE]);
1762 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1763 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
1764
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)
1769 +std::string("\n");
1770
1771 result.addInt32(YARPRUN_ERROR);
1772 result.addString(out.c_str());
1773 fprintf(stderr, "%s", out.c_str());
1774
1775 return YARPRUN_ERROR;
1776 }
1777
1778 if (IS_NEW_PROCESS(pid_stdout)) // STDOUT IMPLEMENTED HERE
1779 {
1780 REDIRECT_TO(STDIN_FILENO, pipe_cmd_to_stdout[READ_FROM_PIPE]);
1781
1782 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1783 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
1784
1785 //Why removing vectors and stop threads?
1786 //exec* never returns and memory is claimed by the system
1787 //furthermore after fork() only the thread which called fork() is forked!
1788 // cleanBeforeExec();
1789
1790 //yarp::os::impl::signal(SIGPIPE, SIG_DFL);
1791
1792 int ret = yarp::run::impl::execlp("yarprun", "yarprun", "--write", portName.c_str(), "--log", loggerName.c_str(), static_cast<char*>(nullptr));
1793
1794 CLOSE(pipe_cmd_to_stdout[READ_FROM_PIPE]);
1795
1796 if (ret==YARPRUN_ERROR)
1797 {
1798 int error=errno;
1799
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)
1804 +std::string("\n");
1805
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);
1810
1811 fprintf(stderr, "%s", out.c_str());
1812 }
1813
1814 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1815
1816 std::exit(ret);
1817 }
1818
1819 if (IS_PARENT_OF(pid_stdout))
1820 {
1821 CLOSE(pipe_cmd_to_stdout[READ_FROM_PIPE]);
1822
1823 fprintf(stderr, "STARTED: server=%s alias=%s cmd=stdout pid=%d\n", mPortName.c_str(), strAlias.c_str(), pid_stdout);
1824
1826
1827 //if (IS_PARENT_OF(pid_stdin))
1828 {
1829 int pid_cmd=yarp::run::impl::fork();
1830
1831 if (IS_INVALID(pid_cmd))
1832 {
1833 int error=errno;
1834
1835 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1836 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
1837
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)
1842 +std::string("\n");
1843
1844 result.addInt32(YARPRUN_ERROR);
1845 result.addString(out.c_str());
1846 fprintf(stderr, "%s", out.c_str());
1847
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);
1852
1853 SIGNAL(pid_stdout, SIGTERM);
1854 fprintf(stderr, "TERMINATING stdout (%d)\n", pid_stdout);
1855 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1856
1857 return YARPRUN_ERROR;
1858 }
1859
1860 if (IS_NEW_PROCESS(pid_cmd)) // RUN COMMAND HERE
1861 {
1862 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
1863
1864 char *cmd_str=new char[strCmd.length()+1];
1865 strcpy(cmd_str, strCmd.c_str());
1866 /*
1867 int nargs=CountArgs(cmd_str);
1868 char **arg_str=new char*[nargs+1];
1869 ParseCmd(cmd_str, arg_str);
1870 arg_str[nargs]=0;
1871 */
1872 int nargs = 0;
1873 char **arg_str = new char*[C_MAXARGS + 1];
1874 parseArguments(cmd_str, &nargs, arg_str);
1875 arg_str[nargs]=nullptr;
1876
1877 setvbuf(stdout, nullptr, _IONBF, 0);
1878
1879 REDIRECT_TO(STDOUT_FILENO, pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1880 REDIRECT_TO(STDERR_FILENO, pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1881
1882 // Set the YARP_IS_YARPRUN environment variable to 1, so that the child
1883 // process will now that is running inside yarprun.
1884 yarp::conf::environment::set_string("YARP_IS_YARPRUN", "1");
1885
1886 // Set the YARPRUN_IS_FORWARDING_LOG environment variable to 1, so that
1887 // the child process will now that yarprun is not logging the output.
1888 yarp::conf::environment::set_string("YARPRUN_IS_FORWARDING_LOG", "1");
1889
1890 if (msg.check("env"))
1891 {
1892 auto ss = yarp::conf::string::split(msg.find("env").asString(), ';');
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); // putenv doesn't make copy of the string
1897 }
1898 //delete [] szenv;
1899 }
1900
1901 if (msg.check("workdir"))
1902 {
1903 int ret = yarp::os::impl::chdir(msg.find("workdir").asString().c_str());
1904
1905 if (ret!=0)
1906 {
1907 int error=errno;
1908
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)
1913 +std::string("\n");
1914
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());
1920
1921 std::exit(ret);
1922 }
1923 }
1924
1925 int ret=YARPRUN_ERROR;
1926
1927 char currWorkDirBuff[1024];
1928 char *currWorkDir=getcwd(currWorkDirBuff, 1024);
1929
1930 if (currWorkDir)
1931 {
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];
1935 }
1936 cwd_arg_str[nargs]=nullptr;
1937 cwd_arg_str[0]=new char[strlen(currWorkDir)+strlen(arg_str[0])+16];
1938
1939 strcpy(cwd_arg_str[0], currWorkDir);
1940 strcat(cwd_arg_str[0], "/");
1941 strcat(cwd_arg_str[0], arg_str[0]);
1942
1943 //Why removing vectors and stop threads?
1944 //exec* never returns and memory is claimed by the system
1945 //furthermore after fork() only the thread which called fork() is forked!
1946 // cleanBeforeExec();
1947
1948 ret = yarp::run::impl::execvp(cwd_arg_str[0], cwd_arg_str);
1949
1950 delete [] cwd_arg_str[0];
1951 delete [] cwd_arg_str;
1952 }
1953
1954 if (ret==YARPRUN_ERROR)
1955 {
1956 //Why removing vectors and stop threads?
1957 //exec* never returns and memory is claimed by the system
1958 //furthermore after fork() only the thread which called fork() is forked!
1959 // cleanBeforeExec();
1960
1961 ret = yarp::run::impl::execvp(arg_str[0], arg_str);
1962 }
1963
1964 fflush(stdout);
1965
1966 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1967
1968 if (ret==YARPRUN_ERROR)
1969 {
1970 int error=errno;
1971
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)
1976 +std::string("\n");
1977
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());
1983 }
1984
1985 delete [] cmd_str;
1986 delete [] arg_str;
1987
1988 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1989
1990 std::exit(ret);
1991 }
1992
1993
1994 if (IS_PARENT_OF(pid_cmd))
1995 {
1996 CLOSE(pipe_cmd_to_stdout[WRITE_TO_PIPE]);
1997 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
1998
1999 auto* pInf = new YarpRunCmdWithStdioInfo(
2000 strAlias,
2001 mPortName,
2002 portName,
2003 pid_cmd,
2004 pid_stdout,
2005 pipe_cmd_to_stdout[READ_FROM_PIPE],
2006 pipe_cmd_to_stdout[WRITE_TO_PIPE],
2007 nullptr,
2008 false
2009 );
2010
2011 pInf->setCmd(strCmd);
2012
2013 if (msg.check("env"))
2014 {
2015 pInf->setEnv(msg.find("env").asString());
2016 }
2017
2018 mProcessVector->Add(pInf);
2019
2021
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);
2025
2026 std::string out;
2027
2028 if (in_from_child)
2029 {
2030 char buff[1024];
2031
2032 while(true)
2033 {
2034 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
2035 break;
2036 }
2037
2038 out+=std::string(buff);
2039 }
2040
2041 fclose(in_from_child);
2042 }
2043
2044 if (out.length()>0)
2045 {
2046 pid_cmd=YARPRUN_ERROR;
2047 }
2048 else
2049 {
2050 out=std::string("STARTED: server=")+mPortName
2051 +std::string(" alias=")+strAlias
2052 +std::string(" cmd=")+strCmd
2053 +std::string(" pid=")+int2String(pid_cmd)
2054 +std::string("\n");
2055 }
2056
2057 result.addInt32(pid_cmd);
2058 result.addString(out.c_str());
2059
2060 fprintf(stderr, "%s", out.c_str());
2061
2062 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
2063
2064 return pid_cmd;
2065 }
2066 }
2067 }
2068
2069 result.addInt32(YARPRUN_ERROR);
2070 result.addString("I should never reach this point!!!\n");
2071
2072 return YARPRUN_ERROR;
2073}
2074
2075int yarp::run::Run::userStdio(const yarp::os::Bottle& msg, yarp::os::Bottle& result)
2076{
2077 std::string strAlias=msg.find("as").asString();
2078 std::string strUUID=msg.find("stdiouuid").asString();
2079
2080 std::string strCmd;
2081
2082 if (msg.check("forward"))
2083 {
2084 strCmd=std::string("/bin/bash -l -c \"yarprun --readwrite ")+strUUID
2085 +std::string(" --forward ")+msg.findGroup("forward").get(1).asString()+std::string(" ")+msg.findGroup("forward").get(2).asString()+std::string("\"");
2086 }
2087 else
2088 {
2089 strCmd=std::string("/bin/bash -l -c \"yarprun --readwrite ")+strUUID+"\"";
2090 }
2091
2092 int pipe_child_to_parent[2];
2093
2094 if (yarp::run::impl::pipe(pipe_child_to_parent))
2095 {
2096 int error=errno;
2097
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)
2101 +std::string("\n");
2102
2103 result.clear();
2104 result.addInt32(YARPRUN_ERROR);
2105 result.addString(out.c_str());
2106 fprintf(stderr, "%s", out.c_str());
2107
2108 return YARPRUN_ERROR;
2109 }
2110
2111 int c=0;
2112 char *command[16];
2113 for (auto & i : command) {
2114 i = nullptr;
2115 }
2116
2117 cmdcpy(command[c++], "xterm");
2118 cmdcpy(command[c++], msg.check("hold")?"-hold":"+hold");
2119
2120 if (msg.check("geometry"))
2121 {
2122 cmdcpy(command[c++], "-geometry");
2123 cmdcpy(command[c++], msg.find("geometry").asString().c_str());
2124 }
2125
2126 cmdcpy(command[c++], "-title");
2127 cmdcpy(command[c++], strAlias.c_str());
2128
2129 cmdcpy(command[c++], "-e");
2130 cmdcpy(command[c++], strCmd.c_str());
2131
2132 int pid_cmd=yarp::run::impl::fork();
2133
2134 if (IS_INVALID(pid_cmd))
2135 {
2136 int error=errno;
2137
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)
2141 +std::string("\n");
2142
2143 result.clear();
2144 result.addInt32(YARPRUN_ERROR);
2145 result.addString(out.c_str());
2146 fprintf(stderr, "%s", out.c_str());
2147
2148 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
2149 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
2150
2151 cmdclean(command);
2152
2153 return YARPRUN_ERROR;
2154 }
2155
2156 if (IS_NEW_PROCESS(pid_cmd)) // RUN COMMAND HERE
2157 {
2158 //yarp::os::impl::signal(SIGPIPE, SIG_IGN);
2159
2160 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
2161
2162 REDIRECT_TO(STDERR_FILENO, pipe_child_to_parent[WRITE_TO_PIPE]);
2163
2164 //Why removing vectors and stop threads?
2165 //exec* never returns and memory is claimed by the system
2166 //furthermore after fork() only the thread which called fork() is forked!
2167 // cleanBeforeExec();
2168
2169 //yarp::os::impl::signal(SIGHUP, rwSighupHandler);
2170
2171 int ret = yarp::run::impl::execvp("xterm", command);
2172
2173 cmdclean(command);
2174
2175 if (ret==YARPRUN_ERROR)
2176 {
2177 int error=errno;
2178
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)
2182 +std::string("\n");
2183
2184 FILE* out_to_parent=fdopen(pipe_child_to_parent[WRITE_TO_PIPE], "w");
2185
2186 fprintf(out_to_parent, "%s", out.c_str());
2187 fflush(out_to_parent);
2188 fclose(out_to_parent);
2189
2190 fprintf(stderr, "%s", out.c_str());
2191 }
2192
2193 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
2194
2195 std::exit(ret);
2196 }
2197
2198 if (IS_PARENT_OF(pid_cmd))
2199 {
2200 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
2201
2202 mStdioVector->Add(new YarpRunProcInfo(strAlias, mPortName, pid_cmd, nullptr, msg.check("hold")));
2203
2204 result.clear();
2205
2206 cmdclean(command);
2207
2209
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);
2213 std::string out;
2214
2215 if (in_from_child)
2216 {
2217 char buff[1024];
2218
2219 while(true)
2220 {
2221 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
2222 break;
2223 }
2224
2225 out+=std::string(buff);
2226 }
2227
2228 fclose(in_from_child);
2229 }
2230
2231 result.clear();
2232
2233 //if (out.length()>0)
2234 if (out.substr(0, 14)=="xterm Xt error" || out.substr(0, 7)=="ABORTED")
2235 {
2236 pid_cmd=YARPRUN_ERROR;
2237 }
2238 else
2239 {
2240 out=std::string("STARTED: server=")+mPortName
2241 +std::string(" alias=")+strAlias
2242 +std::string(" cmd=xterm pid=")+int2String(pid_cmd)
2243 +std::string("\n");
2244
2245 }
2246
2247 fprintf(stderr, "%s", out.c_str());
2248
2249 result.addInt32(pid_cmd);
2250 result.addString(out.c_str());
2251
2252 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
2253
2254 return pid_cmd;
2255 }
2256
2257 result.clear();
2258 result.addInt32(YARPRUN_ERROR);
2259
2260 return YARPRUN_ERROR;
2261}
2262
2263int yarp::run::Run::executeCmd(const yarp::os::Bottle& msg, yarp::os::Bottle& result)
2264{
2265 std::string strAlias(msg.find("as").asString());
2266 std::string strCmd(msg.find("cmd").toString());
2267
2268 int pipe_child_to_parent[2];
2269 int ret_pipe_child_to_parent=yarp::run::impl::pipe(pipe_child_to_parent);
2270
2271 if (ret_pipe_child_to_parent!=0)
2272 {
2273 int error=errno;
2274
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)
2278 +std::string("\n");
2279
2280
2281 result.addInt32(YARPRUN_ERROR);
2282 result.addString(out.c_str());
2283 fprintf(stderr, "%s", out.c_str());
2284
2285 return YARPRUN_ERROR;
2286 }
2287
2288 int pid_cmd=yarp::run::impl::fork();
2289
2290 if (IS_INVALID(pid_cmd))
2291 {
2292 int error=errno;
2293
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)
2298 +std::string("\n");
2299
2300 result.addInt32(YARPRUN_ERROR);
2301 result.addString(out.c_str());
2302 fprintf(stderr, "%s", out.c_str());
2303
2304 return YARPRUN_ERROR;
2305 }
2306
2307 if (IS_NEW_PROCESS(pid_cmd)) // RUN COMMAND HERE
2308 {
2309 int saved_stderr = yarp::run::impl::dup(STDERR_FILENO);
2310 int null_file=open("/dev/null", O_WRONLY);
2311 if (null_file >= 0)
2312 {
2313 REDIRECT_TO(STDOUT_FILENO, null_file);
2314 REDIRECT_TO(STDERR_FILENO, null_file);
2315 close(null_file);
2316 }
2317 char *cmd_str=new char[strCmd.length()+1];
2318 strcpy(cmd_str, strCmd.c_str());
2319 /*
2320 int nargs=CountArgs(cmd_str);
2321 char **arg_str=new char*[nargs+1];
2322 ParseCmd(cmd_str, arg_str);
2323 arg_str[nargs]=0;
2324 */
2325 int nargs = 0;
2326 char **arg_str = new char*[C_MAXARGS + 1];
2327 parseArguments(cmd_str, &nargs, arg_str);
2328 arg_str[nargs]=nullptr;
2329
2330 // Set the YARP_IS_YARPRUN environment variable to 1, so that the child
2331 // process will now that is running inside yarprun.
2332 yarp::conf::environment::set_string("YARP_IS_YARPRUN", "1");
2333
2334 // Set the YARPRUN_IS_FORWARDING_LOG environment variable to 0, so that
2335 // the child process will now that yarprun is not logging the output.
2336 yarp::conf::environment::set_string("YARPRUN_IS_FORWARDING_LOG", "0");
2337
2338 if (msg.check("env"))
2339 {
2340 auto ss = yarp::conf::string::split(msg.find("env").asString(), ';');
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); // putenv doesn't make copy of the string
2345 }
2346 }
2347
2348 if (msg.check("workdir"))
2349 {
2350 int ret = yarp::os::impl::chdir(msg.find("workdir").asString().c_str());
2351
2352 if (ret!=0)
2353 {
2354 int error=errno;
2355
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)
2360 +std::string("\n");
2361
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);
2366
2367 REDIRECT_TO(STDERR_FILENO, saved_stderr);
2368 fprintf(stderr, "%s", out.c_str());
2369 }
2370 }
2371
2372 int ret=YARPRUN_ERROR;
2373
2374 char currWorkDirBuff[1024];
2375 char *currWorkDir=getcwd(currWorkDirBuff, 1024);
2376
2377 if (currWorkDir)
2378 {
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];
2382 }
2383 cwd_arg_str[nargs]=nullptr;
2384 cwd_arg_str[0]=new char[strlen(currWorkDir)+strlen(arg_str[0])+16];
2385
2386
2387 strcpy(cwd_arg_str[0], currWorkDir);
2388 strcat(cwd_arg_str[0], "/");
2389 strcat(cwd_arg_str[0], arg_str[0]);
2390
2391 //Why removing vectors and stop threads?
2392 //exec* never returns and memory is claimed by the system
2393 //furthermore after fork() only the thread which called fork() is forked!
2394// cleanBeforeExec();
2395
2396 ret = yarp::run::impl::execvp(cwd_arg_str[0], cwd_arg_str);
2397
2398 delete [] cwd_arg_str[0];
2399 delete [] cwd_arg_str;
2400 }
2401
2402 if (ret==YARPRUN_ERROR)
2403 {
2404 //Why removing vectors and stop threads?
2405 //exec* never returns and memory is claimed by the system
2406 //furthermore after fork() only the thread which called fork() is forked!
2407 // cleanBeforeExec();
2408 ret = yarp::run::impl::execvp(arg_str[0], arg_str);
2409 }
2410
2411 if (ret==YARPRUN_ERROR)
2412 {
2413 int error=errno;
2414
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)
2419 +std::string("\n");
2420
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);
2425
2426 if (saved_stderr >= 0)
2427 {
2428 REDIRECT_TO(STDERR_FILENO, saved_stderr);
2429 }
2430 fprintf(stderr, "%s", out.c_str());
2431 }
2432
2433 delete [] cmd_str;
2434 delete [] arg_str;
2435
2436 std::exit(ret);
2437 }
2438
2439 if (IS_PARENT_OF(pid_cmd))
2440 {
2441 auto* pInf = new YarpRunProcInfo(strAlias, mPortName, pid_cmd, nullptr, false);
2442 pInf->setCmd(strCmd);
2443 if (msg.check("env")) {
2444 pInf->setEnv(msg.find("env").asString());
2445 }
2446 mProcessVector->Add(pInf);
2447 char pidstr[16];
2448 sprintf(pidstr, "%d", pid_cmd);
2449
2451
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);
2455
2456 std::string out;
2457
2458 if (in_from_child)
2459 {
2460 char buff[1024];
2461
2462 while(true)
2463 {
2464 if (!fgets(buff, 1024, in_from_child) || ferror(in_from_child) || feof(in_from_child)) {
2465 break;
2466 }
2467
2468 out+=std::string(buff);
2469 }
2470
2471 fclose(in_from_child);
2472 }
2473
2474 if (out.length()>0)
2475 {
2476 pid_cmd=YARPRUN_ERROR;
2477 }
2478 else
2479 {
2480 out=std::string("STARTED: server=")+mPortName
2481 +std::string(" alias=")+strAlias
2482 +std::string(" cmd=")+strCmd
2483 +std::string(" pid=")+int2String(pid_cmd)
2484 +std::string("\n");
2485 }
2486
2487 fprintf(stderr, "%s", out.c_str());
2488
2489 result.addInt32(pid_cmd);
2490 result.addString(out.c_str());
2491
2492 CLOSE(pipe_child_to_parent[READ_FROM_PIPE]);
2493 CLOSE(pipe_child_to_parent[WRITE_TO_PIPE]);
2494
2495 return pid_cmd;
2496 }
2497
2498 result.addInt32(YARPRUN_ERROR);
2499
2500 return YARPRUN_ERROR;
2501}
2502
2503#endif
#define RUNLOG(msg)
int SIGNAL(int pid, int signum)
int CLOSE(int h)
#define YARPRUN_ERROR
std::string int2String(int x)
static void sigchld_handler(int sig)
Definition Run.cpp:457
void sigstdio_handler(int sig)
Definition Run.cpp:79
static std::string getProcLabel(const yarp::os::Bottle &msg)
Definition Run.cpp:93
void parseArguments(char *io_pLine, int *o_pArgc, char **o_pArgv)
Breaks up a line into multiple arguments.
Definition Run.cpp:1209
void splitLine(char *pLine, char **pArgs)
Split a line into separate words.
Definition Run.cpp:1189
static RunTerminator * pTerminator
Definition Run.cpp:77
#define C_MAXARGS
Definition Run.cpp:43
void setEnv(const std::string &env)
void setCmd(const std::string &cmd)
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
std::string getName() const override
Get name of port.
void close() override
Stop port activity.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
Definition Bottle.cpp:188
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:252
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:247
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition Bottle.cpp:294
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition Bottle.cpp:278
Bottle tail() const
Get all but the first element of a bottle.
Definition Bottle.cpp:362
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:140
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:176
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition Bottle.cpp:288
A mini-server for performing network communication in the background.
static bool getLocalMode()
Get current value of flag "localMode", see setLocalMode function.
Definition Network.cpp:1142
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.
Definition Network.cpp:784
static bool checkNetwork()
Check if the YARP Network is up and running.
Definition Network.cpp:1473
static bool isConnected(const std::string &src, const std::string &dest, bool quiet)
Check if a connection with tcp carrier exists between two ports.
Definition Network.cpp:829
A class for storing options and configuration information.
Definition Property.h:32
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.
Definition Property.cpp:980
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.
Definition RpcClient.h:22
A port that is specialized as an RPC server.
Definition RpcServer.h:23
static void delaySystem(double seconds)
bool start()
Start the new thread running.
Definition Thread.cpp:93
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition Value.cpp:204
std::string toString() const override
Return a standard text representation of the content of the object.
Definition Value.cpp:347
virtual std::string asString() const
Get string value.
Definition Value.cpp:240
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...
Definition RunClient.cpp:37
#define WRITE_TO_PIPE
#define READ_FROM_PIPE
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.
Definition string.h:26
void useSystemClock()
Configure YARP to use system time (this is the default).
Definition Time.cpp:137
char * getcwd(char *buf, size_t size)
Portable wrapper for the getcwd() function.
Definition Os.cpp:105
int getpid()
Portable wrapper for the getppid() function.
Definition Os.cpp:84
#define YARP_UNUSED(var)
Definition api.h:162