ARC SDK
Run.h
1 // -*- indent-tabs-mode: nil -*-
2 
3 #ifndef __ARC_RUN_H__
4 #define __ARC_RUN_H__
5 
6 #include <glibmm.h>
7 #include <arc/Thread.h>
8 #include <arc/DateTime.h>
9 
10 namespace Arc {
11 
12  class RunPump;
13 
15 
19  class Run {
20  friend class RunPump;
21  public:
22  // Interface to container for data received/sent to process.
23  class Data {
24  public:
25  virtual ~Data() {};
26  virtual void Append(char const* data, unsigned int size) = 0;
27  virtual void Remove(unsigned int size) = 0;
28  virtual char const* Get() const = 0;
29  virtual unsigned int Size() const = 0;
30  };
31 
32  private:
33  Run(const Run&);
34  Run& operator=(Run&);
35  protected:
36  // Container for data pumped through std* channels.
37  class StringData: public Data {
38  public:
39  StringData();
40  virtual ~StringData();
41  void Assign(std::string& str, int content_max_size = 0);
42  virtual void Append(char const* data, unsigned int size);
43  virtual void Remove(unsigned int size);
44  virtual char const* Get() const;
45  virtual unsigned int Size() const;
46  private:
47  std::string* content_;
48  int content_max_size_;
49  };
50 
51  // working directory
52  std::string working_directory;
53  // Std* handles
54  int stdout_;
55  int stderr_;
56  int stdin_;
57  // Associated string containers
58  Data *stdout_str_;
59  Data *stderr_str_;
60  Data *stdin_str_;
61  StringData stdout_str_wrap_;
62  StringData stderr_str_wrap_;
63  StringData stdin_str_wrap_;
64  // Request to keep std* handles of process same as in parent
65  bool stdout_keep_;
66  bool stderr_keep_;
67  bool stdin_keep_;
68  // PID of child
69  pid_t pid_;
70  // Arguments to execute
71  std::list<std::string> argv_;
72  std::list<std::string> envp_;
73  std::list<std::string> envx_;
74  void (*initializer_func_)(void*);
75  void *initializer_arg_;
76  void (*kicker_func_)(void*);
77  void *kicker_arg_;
78  // IO handlers are called when data can be sent/received
79  typedef bool (Run::*HandleHandler)();
80  bool stdout_handler();
81  bool stderr_handler();
82  bool stdin_handler();
83  // Child exit handler
84  typedef bool (Run::*PidHandler)(int);
85  void child_handler(int result);
86  bool started_; // child process was stated
87  bool running_; // child process is running
88  bool abandoned_; // we are interested in this process anymore
89  int result_; // process execution result
90  Glib::Mutex lock_;
91  Glib::Cond cond_;
92  int user_id_;
93  int group_id_;
94  Time run_time_;
95  Time exit_time_;
96  public:
98  Run(const std::string& cmdline);
100  Run(const std::list<std::string>& argv);
102  ~Run(void);
104  operator bool(void) {
105  return argv_.size() != 0;
106  }
108  bool operator!(void) {
109  return argv_.size() == 0;
110  }
112 
113  bool Start(void);
115 
116  bool Wait(int timeout);
118 
120  bool Wait(void);
122 
125  int Result(void) {
126  return result_;
127  }
129  bool Running(void);
131  Time RunTime(void) {
132  return run_time_;
133  };
135  Time ExitTime(void) {
136  return exit_time_;
137  };
139 
146  int ReadStdout(int timeout, char *buf, int size);
148 
155  int ReadStderr(int timeout, char *buf, int size);
157 
164  int WriteStdin(int timeout, const char *buf, int size);
166 
168  void AssignStdout(std::string& str, int max_size = 102400);
169  void AssignStdout(Data& str);
171 
173  void AssignStderr(std::string& str, int max_size = 102400);
174  void AssignStderr(Data& str);
176 
178  void AssignStdin(std::string& str);
179  void AssignStdin(Data& str);
181  void KeepStdout(bool keep = true);
183  void KeepStderr(bool keep = true);
185  void KeepStdin(bool keep = true);
187  void CloseStdout(void);
189  void CloseStderr(void);
191  void CloseStdin(void);
193  void AssignInitializer(void (*initializer_func)(void*), void *initializer_arg);
195  void AssignKicker(void (*kicker_func)(void*), void *kicker_arg);
197  void AssignWorkingDirectory(std::string& wd) {
198  working_directory = wd;
199  }
201  void AssignUserId(int uid) {
202  user_id_ = uid;
203  }
205  void AssignGroupId(int gid) {
206  group_id_ = gid;
207  }
209  void AddEnvironment(const std::string& key, const std::string& value) {
210  AddEnvironment(key+"="+value);
211  }
213  void AddEnvironment(const std::string& var) {
214  envp_.push_back(var);
215  }
217  void RemoveEnvironment(const std::string& key) {
218  envx_.push_back(key);
219  }
221 
223  void Kill(int timeout);
225 
227  void Abandon(void);
229 
231  static void AfterFork(void);
232  };
233 
234 }
235 
236 #endif // __ARC_RUN_H__
Arc namespace contains all core ARC classes.
Definition: ArcConfig.h:11
bool Start(void)
Starts running executable. This method may be called only once.
void AssignGroupId(int gid)
Assign gid for the process to run under.
Definition: Run.h:205
void KeepStdout(bool keep=true)
Keep stdout same as parent&#39;s if keep = true. No pipe will be created.
void CloseStdout(void)
Closes pipe associated with stdout handle.
void Abandon(void)
Detach this object from running process.
void AssignWorkingDirectory(std::string &wd)
Assign working directory of the process to run.
Definition: Run.h:197
A class for storing and manipulating times.
Definition: DateTime.h:125
int ReadStdout(int timeout, char *buf, int size)
Read from stdout pipe of running executable.
Time ExitTime(void)
Return time when executable finished executing.
Definition: Run.h:135
~Run(void)
Destructor kills running executable and releases associated resources.
void AssignUserId(int uid)
Assign uid for the process to run under.
Definition: Run.h:201
void AssignStderr(std::string &str, int max_size=102400)
Associate stderr pipe of executable with string.
void KeepStderr(bool keep=true)
Keep stderr same as parent&#39;s if keep = true. No pipe will be created.
int Result(void)
Returns exit code of execution.
Definition: Run.h:125
void AddEnvironment(const std::string &var)
Add environment variable to be passed to process before it is run.
Definition: Run.h:213
void KeepStdin(bool keep=true)
Keep stdin same as parent&#39;s if keep = true. No pipe will be created.
void RemoveEnvironment(const std::string &key)
Remove environment variable to be passed to process before it is run.
Definition: Run.h:217
void AddEnvironment(const std::string &key, const std::string &value)
Add environment variable to be passed to process before it is run.
Definition: Run.h:209
void AssignStdin(std::string &str)
Associate stdin pipe of executable with string.
void AssignStdout(std::string &str, int max_size=102400)
Associate stdout pipe of executable with string.
void AssignInitializer(void(*initializer_func)(void *), void *initializer_arg)
Assign a function to be called just after process is forked but before execution starts.
void CloseStderr(void)
Closes pipe associated with stderr handle.
void AssignKicker(void(*kicker_func)(void *), void *kicker_arg)
Assign a function to be called just after execution ends. It is executed asynchronously.
int ReadStderr(int timeout, char *buf, int size)
Read from stderr pipe of running executable.
void Kill(int timeout)
Kill running executable.
bool operator!(void)
Returns true if object is invalid.
Definition: Run.h:108
Time RunTime(void)
Returns time when executable was started.
Definition: Run.h:131
static void AfterFork(void)
Call this method after fork() in child process.
Definition: Run.h:23
Definition: Run.h:37
void CloseStdin(void)
Closes pipe associated with stdin handle.
bool Wait(void)
Wait till execution finished.
bool Running(void)
Return true if execution is going on.
int WriteStdin(int timeout, const char *buf, int size)
Write to stdin pipe of running executable.
This class runs an external executable.
Definition: Run.h:19