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  class Pid;
14 
16 
20  class Run {
21  friend class RunPump;
22  private:
23  Run(const Run&);
24  Run& operator=(Run&);
25  protected:
26  // working directory
27  std::string working_directory;
28  // Handles
29  int stdout_;
30  int stderr_;
31  int stdin_;
32  // Associated string containers
33  std::string *stdout_str_;
34  std::string *stderr_str_;
35  std::string *stdin_str_;
36  //
37  bool stdout_keep_;
38  bool stderr_keep_;
39  bool stdin_keep_;
40  // Signal connections
41  sigc::connection stdout_conn_;
42  sigc::connection stderr_conn_;
43  sigc::connection stdin_conn_;
44  sigc::connection child_conn_;
45  // PID of child
46  Pid *pid_;
47  // Arguments to execute
48  Glib::ArrayHandle<std::string> argv_;
49  void (*initializer_func_)(void*);
50  void *initializer_arg_;
51  void (*kicker_func_)(void*);
52  void *kicker_arg_;
53  // IO handlers
54  bool stdout_handler(Glib::IOCondition cond);
55  bool stderr_handler(Glib::IOCondition cond);
56  bool stdin_handler(Glib::IOCondition cond);
57  // Child exit handler
58  void child_handler(Glib::Pid pid, int result);
59  bool started_;
60  bool running_;
61  bool abandoned_;
62  int result_;
63  Glib::Mutex lock_;
64  Glib::Cond cond_;
65  int user_id_;
66  int group_id_;
67  Time run_time_;
68  Time exit_time_;
69  public:
71  Run(const std::string& cmdline);
73  Run(const std::list<std::string>& argv);
75  ~Run(void);
77  operator bool(void) {
78  return argv_.size() != 0;
79  }
81  bool operator!(void) {
82  return argv_.size() == 0;
83  }
85 
86  bool Start(void);
88 
89  bool Wait(int timeout);
91 
93  bool Wait(void);
95 
98  int Result(void) {
99  return result_;
100  }
102  bool Running(void);
104  Time RunTime(void) {
105  return run_time_;
106  };
108  Time ExitTime(void) {
109  return exit_time_;
110  };
112 
119  int ReadStdout(int timeout, char *buf, int size);
121 
128  int ReadStderr(int timeout, char *buf, int size);
130 
137  int WriteStdin(int timeout, const char *buf, int size);
139 
141  void AssignStdout(std::string& str);
143 
145  void AssignStderr(std::string& str);
147 
149  void AssignStdin(std::string& str);
151  void KeepStdout(bool keep = true);
153  void KeepStderr(bool keep = true);
155  void KeepStdin(bool keep = true);
157  void CloseStdout(void);
159  void CloseStderr(void);
161  void CloseStdin(void);
162  //void DumpStdout(void);
163  //void DumpStderr(void);
165  void AssignInitializer(void (*initializer_func)(void*), void *initializer_arg);
167  void AssignKicker(void (*kicker_func)(void*), void *kicker_arg);
169  void AssignWorkingDirectory(std::string& wd) {
170  working_directory = wd;
171  }
173  void AssignUserId(int uid) {
174  user_id_ = uid;
175  }
177  void AssignGroupId(int gid) {
178  group_id_ = gid;
179  }
181 
184  void Kill(int timeout);
186 
188  void Abandon(void);
190 
192  static void AfterFork(void);
193  };
194 
195 }
196 
197 #endif // __ARC_RUN_H__