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