YARP
Yet Another Robot Platform
Run.h
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 #ifndef YARP_RUN_RUN_H
8 #define YARP_RUN_RUN_H
9 
10 #include <cstring>
11 #include <yarp/run/api.h>
12 #include <yarp/os/RpcServer.h>
13 #include <yarp/os/Property.h>
14 
15 
16 class YarpRunInfoVector;
17 class ZombieHunterThread;
18 
19 
20 /*
21  * Typical YARP applications consist of several intercommunicating modules distributed on different machines.
22  * If a yarprun server is running on each machine, distributed applications can be remotely launched,
23  * monitored and terminated by yarprun commands.
24  *
25  * - To run a yarprun server on a machine:
26  * $ yarprun --server /SERVERPORT
27  *
28  * /SERVERPORT must be unique and identifies the remote machine.
29  *
30  * - The basic command to run a command/application on a remote machine is:
31  * $ yarprun --on /SERVERPORT --as TAG --cmd COMMAND [ARGLIST]
32  *
33  * /SERVERPORT is the name of the server that actually runs the command
34  * TAG identifies the application process set, and must be unique
35  * COMMAND is the application that has to be executed, followed by the optional argument list
36  *
37  * Some options can be added to the basic format of yarprun:
38  * $ yarprun --on /SERVERPORT1 --as TAG --cmd COMMAND [ARGLIST] --stdio /SERVERPORT2
39  *
40  * opens a remote shell window where the stdin, stdout and stderr of the application will be redirected.
41  * /SERVERPORT2 specifies the machine where the IO shell will be executed, and can be either a remote machine or
42  * be equal to /SERVERPORT1 itself.
43  *
44  * If --stdio is specified, there are two useful sub-options (linux only):
45  * - --hold keep the stdio window open even if the command is terminated or aborted.
46  * - --geometry WxH+X+Y set the stdio window size and position. Example: --geometry 320x240+80+20
47  *
48  * Other yarprun commands:
49  *
50  * - To terminate an application, the yarprun syntax is:
51  * $ yarprun --on /SERVERPORT --sigterm TAG
52  *
53  * - To send a signal to an application (usually SIGKILL) use:
54  * $ yarprun --on /SERVERPORT --kill TAG SIGNUM
55  *
56  * - To terminate all the applications managed by a yarprun server, use:
57  * $ yarprun --on /SERVERPORT --sigtermall
58  *
59  * - To check if an application is still running on a yarprun server, use:
60  * $ yarprun --on /SERVERPORT --isrunning TAG
61  *
62  * - To get a report of applications running on a yarprun server, use:
63  * $ yarprun --on /SERVERPORT --ps
64  *
65  * - To shutdown a yarprun server, use:
66  * $ yarprun --on /SERVERPORT --exit
67  *
68  */
69 
70 namespace yarp {
71 namespace run {
72 
79 {
80 public:
81  // API
82 
96  static int start(const std::string &node, yarp::os::Property &command, std::string &keyv);
103  static int sigterm(const std::string &node, const std::string &keyv);
109  static int sigterm(const std::string &node);
117  static int kill(const std::string &node, const std::string &keyv, int s);
126  // static int ps(const std::string &node, std::string** &processes, int &num_processes);
133  static bool isRunning(const std::string &node, std::string &keyv);
134 
135  // end API
136 
148  static int client(yarp::os::Property& config);
149 
150 #ifndef DOXYGEN_SHOULD_SKIP_THIS
151 
152  static int main(int argc, char *argv[]);
153  static yarp::os::RpcServer *pServerPort;
154 
155  static bool mStresstest;
156 
157  static bool mLogged;
160  static std::string mLoggerPort;
161 
162 #if defined(_WIN32)
163  static YarpRunInfoVector mProcessVector;
164  static YarpRunInfoVector mStdioVector;
165 #else
166  static YarpRunInfoVector *mProcessVector;
167  static YarpRunInfoVector *mStdioVector;
168  static ZombieHunterThread *mBraveZombieHunter;
169  static void CleanZombie(int pid);
170 #endif
172  static yarp::os::Bottle sendMsg(yarp::os::Bottle& msg, std::string target, int RETRY=20, double DELAY=0.5);
173 
174 protected:
175  static void Help(const char* msg="");
176  static int server();
177  static int executeCmdAndStdio(yarp::os::Bottle& msg, yarp::os::Bottle& result);
178  static int executeCmdStdout(yarp::os::Bottle& msg, yarp::os::Bottle& result, std::string& loggerName);
179  static int executeCmd(yarp::os::Bottle& msg, yarp::os::Bottle& result);
180  static int userStdio(yarp::os::Bottle& msg, yarp::os::Bottle& result);
181 
182  static inline bool IS_PARENT_OF(int pid){ return pid>0; }
183  static inline bool IS_NEW_PROCESS(int pid){ return !pid; }
184  static inline bool IS_INVALID(int pid){ return pid<0; }
185 
187  static std::string mPortName;
188  static int mProcCNT;
189 
190 #if !defined(_WIN32)
191  static void cleanBeforeExec();
192  static void writeToPipe(int fd, std::string str);
193  static int readFromPipe(int fd, char* &data, int& buffsize);
194 #endif
195 
196  static void cmdcpy(char* &dst, const char* src)
197  {
198  dst=new char[(strlen(src)/8+2)*16];
199  strcpy(dst, src);
200  }
201 
202  static void cmdclean(char **cmd)
203  {
204  while (*cmd)
205  {
206  delete [] *cmd++;
207  }
208  }
209 
210 #endif /*DOXYGEN_SHOULD_SKIP_THIS*/
211 };
212 
213 } // namespace run
214 } // namespace yarp
215 
216 
217 #endif // YARP_RUN_RUN_H
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
A class for storing options and configuration information.
Definition: Property.h:34
A port that is specialized as an RPC server.
Definition: RpcServer.h:24
The main, catch-all namespace for YARP.
Definition: dirs.h:16
#define YARP_DISABLE_DLL_INTERFACE_WARNING
Disable MSVC C4251 warnings in the following code.
Definition: system.h:334
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:332
#define YARP_SUPPRESS_DLL_INTERFACE_WARNING
Suppress MSVC C4251 warning for the next line.
Definition: system.h:335
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:331
#define YARP_run_API
Definition: api.h:17
int main(int argc, char *argv[])
Definition: yarpros.cpp:263