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 #ifdef MIRA_LINUX
66 # include <unistd.h>
67 #endif
68 
69 
70 namespace mira {
71 
73 
81 MIRA_BASE_EXPORT int executeProcess(const std::string& cmd,
82  std::string* oStdOut=NULL,
83  std::string* oErrOut=NULL);
84 
86 
96 class MIRA_BASE_EXPORT Process : boost::noncopyable
97 {
98 public:
100  {
104  };
105 
106 
107 
108 public:
109 
117  {
118  public:
120  Environment(const Environment& other);
121  Environment(Environment&& other) noexcept;
122 
123  Environment& operator=(const Environment& other);
124  Environment& operator=(Environment&& other) noexcept;
125 
126  public:
128  void clear();
129 
130  bool empty() const;
131 
133  void insert(const std::string& var, const std::string& value);
134 
142  void insert(const std::string& string);
143 
150  void insertList(const std::string& string);
151 
153  void insert(const Environment& env);
154 
156  void remove(const std::string& var);
157 
159  const std::string& value(const std::string& var, const std::string& defaultValue = std::string()) const;
160 
166  std::vector<std::string> envp();
167 
168 
169  public:
170 
171  const std::map<std::string,std::string>& variables() const { return mVariables; }
172 
173  public:
174 
175  // Returns the environment of the calling process.
176  static Environment systemEnvironment();
177 
178  private:
179  std::map<std::string,std::string> mVariables;
180 
181  };
182 
183 
184 
185 public:
186 
187 
189  Process();
190 
192  ~Process();
193 
194  // move constructor and move assignment op to support move semantics.
195  Process(Process&& other) noexcept;
196  Process& operator=(Process&& other) noexcept;
197 
198 public:
199 
201  void swap(Process& other);
202 
203 public:
204 
206  noflags = 0x00,
207 
209  interruptChildOnParentDeath = 0x01,
210 
215  shutdownRecursively = 0x02
216 
217  };
218  MIRA_ENUM_TO_FLAGS_INCLASS(CreationFlags)
219 
221  none = 0x00,
222  in = 0x01,
223  out = 0x02,
224  err = 0x04
225  };
226  MIRA_ENUM_TO_FLAGS_INCLASS(RedirectionFlags)
227 
228 
229 
258  static Process createProcess(const std::string& commandLine,
259  CreationFlags flags,
260  RedirectionFlags streamRedirection = none);
261 
263  static Process createProcess(const std::string& commandLine,
264  RedirectionFlags streamRedirection = none);
265 
272  static Process createProcess(const std::string& applicationName,
273  const std::vector<std::string>& args,
274  CreationFlags flags = noflags,
275  RedirectionFlags streamRedirection = none,
276  boost::optional<Environment> env = boost::optional<Environment>());
277 
278 
279 
280 
281 public:
282 
288  bool wait(Duration maxWait = Duration::infinity());
289 
294  int getExitCode();
295 
299  ExitStatus getExitStatus();
304  bool isRunning();
305 
309  void interrupt();
313  void terminate();
314 
318  void kill();
319 
324  void setRecursiveShutdown(bool recursive);
325 
337  void shutdown(Duration timeout = Duration::seconds(5));
338 
343  uint32 getPID() const;
344 
345 public:
346 
347  typedef boost::iostreams::stream<boost::iostreams::file_descriptor_sink> ostream;
348  typedef boost::iostreams::stream<boost::iostreams::file_descriptor_source> istream;
349 
355  ostream& cin();
356 
362  istream& cout();
363 
369  istream& cerr();
370 
371 protected:
372 
373  std::map<int, std::vector<int>> getProcessAncestry();
374  bool wait(int pid, std::vector<int>& children, Duration timeout = Duration::seconds(5));
375  std::vector<int> getRecursiveChildProcesses(int pid, int maxDepth = -1);
376  bool interrupt(int pid);
377  bool terminate(int pid);
378  bool kill(int pid);
379  void shutdown(int pid, Duration timeout = Duration::seconds(5));
380 
381 private:
382 
383 #ifdef MIRA_WINDOWS
384  //PROCESS_INFORMATION mProcess; // process information (handle, etc)
385 #endif
386 
387 #ifdef MIRA_LINUX
388  pid_t mProcess; // handle of the process
389 #endif
390 
391  int mExitCode;
392  ExitStatus mExitStatus;
393 
394  bool mShutdownRecursively;
395 
396  boost::shared_ptr<ostream> mCinFd;
397  boost::shared_ptr<istream> mCoutFd;
398  boost::shared_ptr<istream> mCerrFd;
399 
400 };
401 
403 
404 }
405 
406 #endif
Environment()
Definition: Process.h:119
Macros for generating logical operators for using enum values as flags.
const std::map< std::string, std::string > & variables() const
Definition: Process.h:171
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Time and Duration wrapper class.
Definition: Process.h:103
#define MIRA_ENUM_TO_FLAGS_INCLASS(EnumType)
Macro that can be used with enums that contain flags.
Definition: EnumToFlags.h:143
Encapsulates a process, that was launched from the current process.
Definition: Process.h:96
MIRA_BASE_EXPORT int executeProcess(const std::string &cmd, std::string *oStdOut=NULL, std::string *oErrOut=NULL)
Executes a given command/process.
boost::iostreams::stream< boost::iostreams::file_descriptor_sink > ostream
Definition: Process.h:347
sec_type seconds() const
Returns normalized number of seconds (0..59)
Definition: Time.h:283
Use this class to represent time durations.
Definition: Time.h:104
static Duration infinity()
Returns a special duration time representing positive infinity.
Definition: Time.h:245
CreationFlags
Definition: Process.h:205
Definition: Process.h:101
Definition: Process.h:102
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
ExitStatus
Definition: Process.h:99
RedirectionFlags
Definition: Process.h:220
boost::iostreams::stream< boost::iostreams::file_descriptor_source > istream
Definition: Process.h:348
Holds the environment variables that can be passed to a process.
Definition: Process.h:116
Platform dependent defines and macros.