00001
00002
00003 #ifndef __ARC_RUN_H__
00004 #define __ARC_RUN_H__
00005
00006 #include <glibmm.h>
00007 #include <arc/Thread.h>
00008 #include <arc/DateTime.h>
00009
00010 namespace Arc {
00011
00012 class RunPump;
00013 class Pid;
00014
00018 class Run {
00019 friend class RunPump;
00020 private:
00021 Run(const Run&);
00022 Run& operator=(Run&);
00023 protected:
00024
00025 std::string working_directory;
00026
00027 int stdout_;
00028 int stderr_;
00029 int stdin_;
00030
00031 std::string *stdout_str_;
00032 std::string *stderr_str_;
00033 std::string *stdin_str_;
00034
00035 bool stdout_keep_;
00036 bool stderr_keep_;
00037 bool stdin_keep_;
00038
00039 sigc::connection stdout_conn_;
00040 sigc::connection stderr_conn_;
00041 sigc::connection stdin_conn_;
00042 sigc::connection child_conn_;
00043
00044 Pid *pid_;
00045
00046 Glib::ArrayHandle<std::string> argv_;
00047 void (*initializer_func_)(void*);
00048 void *initializer_arg_;
00049 void (*kicker_func_)(void*);
00050 void *kicker_arg_;
00051
00052 bool stdout_handler(Glib::IOCondition cond);
00053 bool stderr_handler(Glib::IOCondition cond);
00054 bool stdin_handler(Glib::IOCondition cond);
00055
00056 void child_handler(Glib::Pid pid, int result);
00057 bool started_;
00058 bool running_;
00059 bool abandoned_;
00060 int result_;
00061 Glib::Mutex lock_;
00062 Glib::Cond cond_;
00063 int user_id_;
00064 int group_id_;
00065 Time run_time_;
00066 Time exit_time_;
00067 public:
00069 Run(const std::string& cmdline);
00071 Run(const std::list<std::string>& argv);
00073 ~Run(void);
00075 operator bool(void) {
00076 return argv_.size() != 0;
00077 }
00079 bool operator!(void) {
00080 return argv_.size() == 0;
00081 }
00084 bool Start(void);
00087 bool Wait(int timeout);
00089 bool Wait(void);
00091 int Result(void) {
00092 return result_;
00093 }
00095 bool Running(void);
00097 Time RunTime(void) {
00098 return run_time_;
00099 };
00101 Time ExitTime(void) {
00102 return exit_time_;
00103 };
00110 int ReadStdout(int timeout, char *buf, int size);
00117 int ReadStderr(int timeout, char *buf, int size);
00124 int WriteStdin(int timeout, const char *buf, int size);
00128 void AssignStdout(std::string& str);
00132 void AssignStderr(std::string& str);
00136 void AssignStdin(std::string& str);
00138 void KeepStdout(bool keep = true);
00140 void KeepStderr(bool keep = true);
00142 void KeepStdin(bool keep = true);
00144 void CloseStdout(void);
00146 void CloseStderr(void);
00148 void CloseStdin(void);
00149
00150
00151 void AssignInitializer(void (*initializer_func)(void*), void *initializer_arg);
00152 void AssignKicker(void (*kicker_func)(void*), void *kicker_arg);
00154 void AssignWorkingDirectory(std::string& wd) {
00155 working_directory = wd;
00156 }
00157 void AssignUserId(int uid) {
00158 user_id_ = uid;
00159 }
00160 void AssignGroupId(int gid) {
00161 group_id_ = gid;
00162 }
00167 void Kill(int timeout);
00171 void Abandon(void);
00175 static void AfterFork(void);
00176 };
00177
00178 }
00179
00180 #endif // __ARC_RUN_H__