MIRA
Process.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_EXECUTEPROCESS_H_
48 #define _MIRA_EXECUTEPROCESS_H_
49 
50 #include <string>
51 #include <vector>
52 
53 #ifndef Q_MOC_RUN
54 #include <boost/noncopyable.hpp>
55 #include <boost/optional.hpp>
56 #include <boost/iostreams/device/file_descriptor.hpp>
57 #include <boost/iostreams/stream.hpp>
58 #endif
59 
60 #include <platform/Platform.h>
61 
62 #include <utils/EnumToFlags.h>
63 #include <utils/Time.h>
64 
65 #include <unistd.h>
66 
67 namespace mira {
68 
70 
78 int executeProcess(const std::string& cmd,
79  std::string* oStdOut=NULL,
80  std::string* oErrOut=NULL);
81 
83 
93 class Process : boost::noncopyable
94 {
95 public:
97  {
101  };
102 
103 
104 
105 public:
106 
114  {
115  public:
117  Environment(const Environment& other);
118  Environment(Environment&& other) noexcept;
119 
120  Environment& operator=(const Environment& other);
121  Environment& operator=(Environment&& other) noexcept;
122 
123  public:
125  void clear();
126 
127  bool empty() const;
128 
130  void insert(const std::string& var, const std::string& value);
131 
139  void insert(const std::string& string);
140 
147  void insertList(const std::string& string);
148 
150  void insert(const Environment& env);
151 
153  void remove(const std::string& var);
154 
156  const std::string& value(const std::string& var, const std::string& defaultValue = std::string()) const;
157 
163  std::vector<std::string> envp();
164 
165 
166  public:
167 
168  const std::map<std::string,std::string>& variables() const { return mVariables; }
169 
170  public:
171 
172  // Returns the environment of the calling process.
174 
175  private:
176  std::map<std::string,std::string> mVariables;
177 
178  };
179 
180 
181 
182 public:
183 
184 
186  Process();
187 
189  ~Process();
190 
191  // move constructor and move assignment op to support move semantics.
192  Process(Process&& other) noexcept;
193  Process& operator=(Process&& other) noexcept;
194 
195 public:
196 
198  void swap(Process& other);
199 
200 public:
201 
203  noflags = 0x00,
204 
207 
213 
214  };
216 
218  none = 0x00,
219  in = 0x01,
220  out = 0x02,
221  err = 0x04
222  };
224 
225 
226 
249  static Process createProcess(const std::string& commandLine,
250  CreationFlags flags,
251  RedirectionFlags streamRedirection = none);
252 
254  static Process createProcess(const std::string& commandLine,
255  RedirectionFlags streamRedirection = none);
256 
263  static Process createProcess(const std::string& applicationName,
264  const std::vector<std::string>& args,
265  CreationFlags flags = noflags,
266  RedirectionFlags streamRedirection = none,
267  boost::optional<Environment> env = boost::optional<Environment>());
268 
269 
270 
271 
272 public:
273 
279  bool wait(Duration maxWait = Duration::infinity());
280 
285  int getExitCode();
286 
295  bool isRunning();
296 
300  void interrupt();
304  void terminate();
305 
309  void kill();
310 
315  void setRecursiveShutdown(bool recursive);
316 
328  void shutdown(Duration timeout = Duration::seconds(5));
329 
334  uint32 getPID() const;
335 
336 public:
337 
338  typedef boost::iostreams::stream<boost::iostreams::file_descriptor_sink> ostream;
339  typedef boost::iostreams::stream<boost::iostreams::file_descriptor_source> istream;
340 
346  ostream& cin();
347 
353  istream& cout();
354 
360  istream& cerr();
361 
362 protected:
363 
364  bool wait(int pid, std::vector<int>& children, Duration timeout = Duration::seconds(5));
365  std::vector<int> getRecursiveChildProcesses(int pid, int maxDepth = -1);
366  bool interrupt(int pid);
367  bool terminate(int pid);
368  bool kill(int pid);
369  void shutdown(int pid, Duration timeout = Duration::seconds(5));
370 
371 private:
372  pid_t mProcess; // handle of the process
373 
374  int mExitCode;
375  ExitStatus mExitStatus;
376 
377  bool mShutdownRecursively;
378 
379  boost::shared_ptr<ostream> mCinFd;
380  boost::shared_ptr<istream> mCoutFd;
381  boost::shared_ptr<istream> mCerrFd;
382 
383 };
384 
386 
387 }
388 
389 #endif
ostream & cin()
Output stream for writing to the stdin of the child process.
Process()
Creates an invalid process object.
static Process createProcess(const std::string &commandLine, CreationFlags flags, RedirectionFlags streamRedirection=none)
Spawns a new process and executes the given command line.
Environment()
Definition: Process.h:116
Definition: Process.h:218
Macros for generating logical operators for using enum values as flags.
const std::map< std::string, std::string > & variables() const
Definition: Process.h:168
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Definition: Process.h:219
void insert(const std::string &var, const std::string &value)
inserts or replaces the environment variable var with the given value.
Time and Duration wrapper class.
void clear()
Removes all environment variables.
uint32_t uint32
Definition: Types.h:64
bool isRunning()
Returns if the process that is represented by this Process object is still running.
const std::string & value(const std::string &var, const std::string &defaultValue=std::string()) const
returns the value of the environment variable var, or the default value, if it does not exist...
Definition: Process.h:100
#define MIRA_ENUM_TO_FLAGS_INCLASS(EnumType)
Macro that can be used with enums that contain flags.
Definition: EnumToFlags.h:143
Environment & operator=(const Environment &other)
Encapsulates a process, that was launched from the current process.
Definition: Process.h:93
int getExitCode()
Return the exit code of the process.
Definition: Process.h:203
void shutdown(Duration timeout=Duration::seconds(5))
Performs an orderly shutdown by executing the following steps.
boost::iostreams::stream< boost::iostreams::file_descriptor_sink > ostream
Definition: Process.h:338
sec_type seconds() const
Returns normalized number of seconds (0..59)
Definition: Time.h:280
Use this class to represent time durations.
Definition: Time.h:106
uint32 getPID() const
Returns the process id (pid) of the process.
static Environment systemEnvironment()
std::vector< std::string > envp()
Generates and returns an vector of strings, conventionally of the form key=value, which can be used t...
Definition: Process.h:221
void setRecursiveShutdown(bool recursive)
Enable recursive shutdown.
static Duration infinity()
Returns a special duration time representing positive infinity.
Definition: Time.h:242
void insertList(const std::string &string)
Inserts all variables from the given string which has the format.
istream & cout()
Input stream for reading from the stdout of the child process.
CreationFlags
Definition: Process.h:202
If true, shutdown will determine the complete progeny of the spawned process and eradicate it (top do...
Definition: Process.h:212
int executeProcess(const std::string &cmd, std::string *oStdOut=NULL, std::string *oErrOut=NULL)
Executes a given command/process.
void interrupt()
Stops (e.g.
void terminate()
Terminates (e.g.
Definition: Process.h:98
Definition: Process.h:99
istream & cerr()
Input stream for reading from the stderr of the child process.
ExitStatus getExitStatus()
Returns the exit status of the process only valid if not running.
std::vector< int > getRecursiveChildProcesses(int pid, int maxDepth=-1)
ExitStatus
Definition: Process.h:96
~Process()
Destructor, which will terminate the process, if it is still running.
Will send the SIGINT signal to the child when the parent gets killed.
Definition: Process.h:206
RedirectionFlags
Definition: Process.h:217
void kill()
Kills (e.g.
Definition: Process.h:220
boost::iostreams::stream< boost::iostreams::file_descriptor_source > istream
Definition: Process.h:339
Holds the environment variables that can be passed to a process.
Definition: Process.h:113
Process & operator=(Process &&other) noexcept
void swap(Process &other)
Swaps the ownership of the process context within this and other.
Platform dependent defines and macros.
bool wait(Duration maxWait=Duration::infinity())
Blocks the current thread of the calling process, until the process that is represented by this Proce...