Go to the documentation of this file.00001 #ifndef UTILMM_PROCESS_H
00002 #define UTILMM_PROCESS_H
00003 #include "utilmm/config/config.h"
00004 #include <utilmm/system/system.hh>
00005 #include <boost/noncopyable.hpp>
00006 #include <boost/filesystem/path.hpp>
00007
00008 #include <string>
00009 #include <map>
00010 #include <list>
00011 #include <signal.h>
00012
00013 namespace utilmm
00014 {
00020 class process
00021 {
00022 public:
00023 static const int InvalidHandle = -1;
00025 enum Stream { Stdout = 1, Stderr = 2 };
00026
00027 private:
00028 typedef std::list<process*>::iterator ProcessHandle;
00029 ProcessHandle m_handle;
00030
00031 boost::filesystem::path m_wdir;
00032 typedef std::list<std::string> CommandLine;
00033 CommandLine m_cmdline;
00034
00035 typedef std::map<std::string, std::string> Env;
00036 Env m_env;
00037
00038 struct output_file : public utilmm::auto_close
00039 {
00040 bool is_null() const;
00041 void redirect(FILE* stream);
00042 };
00043 output_file m_stdout, m_stderr;
00044 output_file& get_stream(Stream stream);
00045
00046 bool m_running;
00047 pid_t m_pid;
00048 bool m_normalexit;
00049 int m_status;
00050
00051 bool m_do_setpgid;
00052 pid_t m_pgid;
00053
00054 bool wait(bool hang);
00055 void send_child_error(int fd, int error_type);
00056 void process_child_error(int fd);
00057
00058 public:
00059 class already_running : public std::exception {};
00060
00061 process();
00062 ~process();
00063
00065 void set_workdir(boost::filesystem::path const& dir);
00069 boost::filesystem::path workdir() const;
00070
00073 std::list<std::string> cmdline() const;
00075 void push(const std::string& arg);
00079 process& operator << (std::string const& newarg);
00081 void clear();
00082
00087 void start();
00088
00095 void signal(int signo = SIGINT);
00096
00098 static void install_sigint_handler();
00099
00102 static void killall();
00103
00106 void wait();
00107
00115 void detach();
00116
00120 void erase_redirection(Stream stream);
00121
00144 void redirect_to( Stream stream, int handle, bool auto_close = true);
00146 void redirect_to( Stream stream, FILE* handle, bool auto_close = true);
00148 void redirect_to( Stream stream, boost::filesystem::path const& file);
00149
00150
00157 void set_environment(const std::string& key, const std::string& value);
00162 std::string environment(const std::string& key) const;
00164 void clear_environment();
00165
00167 void set_pgid(pid_t pid);
00168
00170 bool exit_normal() const;
00172 int exit_status() const;
00173
00175 pid_t pid() const;
00177 bool running();
00178 };
00179 }
00180
00181 #endif
00182