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