Util--  1.1
process.hh
Go to the documentation of this file.
1 #ifndef UTILMM_PROCESS_H
2 #define UTILMM_PROCESS_H
3 #include "utilmm/config/config.h"
5 #include <boost/noncopyable.hpp>
6 #include <boost/filesystem/path.hpp>
7 
8 #include <string>
9 #include <map>
10 #include <list>
11 #include <signal.h>
12 
13 namespace utilmm
14 {
20  class process
21  {
22  public:
23  static const int InvalidHandle = -1;
25  enum Stream { Stdout = 1, Stderr = 2 };
26 
27  private:
28  typedef std::list<process*>::iterator ProcessHandle;
29  ProcessHandle m_handle;
30 
31  boost::filesystem::path m_wdir;
32  typedef std::list<std::string> CommandLine;
33  CommandLine m_cmdline;
34 
35  typedef std::map<std::string, std::string> Env;
36  Env m_env;
37 
38  struct output_file : public utilmm::auto_close
39  {
40  bool is_null() const;
41  void redirect(FILE* stream);
42  };
43  output_file m_stdout, m_stderr;
44  output_file& get_stream(Stream stream);
45 
46  bool m_running;
47  pid_t m_pid;
48  bool m_normalexit;
49  int m_status;
50 
51  bool m_do_setpgid;
52  pid_t m_pgid;
53 
54  bool wait(bool hang);
55  void send_child_error(int fd, int error_type);
56  void process_child_error(int fd);
57 
58  public:
59  class already_running : public std::exception {};
60 
61  process();
62  ~process();
63 
65  void set_workdir(boost::filesystem::path const& dir);
69  boost::filesystem::path workdir() const;
70 
73  std::list<std::string> cmdline() const;
75  void push(const std::string& arg);
79  process& operator << (std::string const& newarg);
81  void clear();
82 
87  void start();
88 
95  void signal(int signo = SIGINT);
96 
98  static void install_sigint_handler();
99 
102  static void killall();
103 
106  void wait();
107 
115  void detach();
116 
120  void erase_redirection(Stream stream);
121 
144  void redirect_to( Stream stream, int handle, bool auto_close = true);
146  void redirect_to( Stream stream, FILE* handle, bool auto_close = true);
148  void redirect_to( Stream stream, boost::filesystem::path const& file);
149 
150 
157  void set_environment(const std::string& key, const std::string& value);
162  std::string environment(const std::string& key) const;
164  void clear_environment();
165 
167  void set_pgid(pid_t pid);
168 
170  bool exit_normal() const;
172  int exit_status() const;
173 
175  pid_t pid() const;
177  bool running();
178  };
179 }
180 
181 #endif
182 
std::list< std::string > cmdline() const
void set_environment(const std::string &key, const std::string &value)
Definition: system.hh:39
void redirect_to(Stream stream, int handle, bool auto_close=true)
void set_pgid(pid_t pid)
void clear_environment()
void push(const std::string &arg)
void set_workdir(boost::filesystem::path const &dir)
void erase_redirection(Stream stream)
std::string environment(const std::string &key) const
Stream
Definition: process.hh:25
Definition: process.hh:20
static void install_sigint_handler()
process & operator<<(std::string const &newarg)
static const int InvalidHandle
Definition: process.hh:23
int exit_status() const
static void killall()
Definition: process.hh:25
Definition: process.hh:25
void signal(int signo=SIGINT)
pid_t pid() const
Definition: process.hh:59
boost::filesystem::path workdir() const
bool exit_normal() const

Generated on Tue Oct 9 2018 10:12:45 for Util-- by doxygen 1.8.6
SourceForge.net Project Page