Subordination
A framework for distributed programming
application.hh
1 #ifndef SUBORDINATION_PPL_APPLICATION_HH
2 #define SUBORDINATION_PPL_APPLICATION_HH
3 
4 #include <iosfwd>
5 #include <vector>
6 
7 #include <unistdx/fs/canonical_path>
8 #include <unistdx/io/two_way_pipe>
9 #include <unistdx/ipc/execute>
10 #include <unistdx/ipc/identity>
11 #include <unistdx/net/pstream>
12 
13 namespace sbn {
14 
15  typedef uint64_t application_type;
16 
17  enum class process_role_type {
18  master,
19  slave
20  };
21 
23  operator<<(std::ostream& out, process_role_type rhs);
24 
25  class application {
26 
27  public:
28  typedef std::string path_type;
29  typedef application_type id_type;
31 
32  private:
33  id_type _id = 0;
34  sys::uid_type _uid = -1;
35  sys::gid_type _gid = -1;
36  container_type _args, _env;
37  sys::canonical_path _workdir;
38  mutable bool _allowroot = false;
39  mutable process_role_type _processrole = process_role_type::master;
40 
41  static_assert(sizeof(_uid) <= sizeof(uint32_t), "bad uid_type");
42  static_assert(sizeof(_gid) <= sizeof(uint32_t), "bad gid_type");
43 
44  public:
45  application(const container_type& args, const container_type& env);
46 
47  application() = default;
48 
49  application(application&& rhs) = default;
50 
51  application(const application& rhs) = default;
52 
53  inline void
54  workdir(const sys::canonical_path& rhs) {
55  this->_workdir = rhs;
56  }
57 
58  inline void
59  set_credentials(sys::uid_type uid, sys::gid_type gid) noexcept {
60  this->_uid = uid;
61  this->_gid = gid;
62  }
63 
64  inline void
65  allow_root(bool rhs) const noexcept {
66  this->_allowroot = rhs;
67  }
68 
69  inline id_type
70  id() const noexcept {
71  return this->_id;
72  }
73 
74  inline sys::uid_type
75  uid() const noexcept {
76  return this->_uid;
77  }
78 
79  inline sys::gid_type
80  gid() const noexcept {
81  return this->_gid;
82  }
83 
84  const std::string&
85  filename() const noexcept {
86  return this->_args.front();
87  }
88 
89  inline void
90  make_master() const noexcept {
91  this->_processrole = process_role_type::master;
92  }
93 
94  inline void
95  make_slave() const noexcept {
96  this->_processrole = process_role_type::slave;
97  }
98 
99  inline bool
100  is_master() const noexcept {
101  return this->_processrole == process_role_type::master;
102  }
103 
104  inline bool
105  is_slave() const noexcept {
106  return this->_processrole == process_role_type::slave;
107  }
108 
109  inline process_role_type
110  role() const noexcept {
111  return this->_processrole;
112  }
113 
114  int
115  execute(const sys::two_way_pipe& pipe) const;
116 
117  void
118  write(sys::pstream& out) const;
119 
120  void
121  read(sys::pstream& in);
122 
123  friend std::ostream&
124  operator<<(std::ostream& out, const application& rhs);
125 
126  friend void
127  swap(application& lhs, application& rhs);
128 
129  };
130 
131  std::ostream&
132  operator<<(std::ostream& out, const application& rhs);
133 
134  inline sys::pstream&
135  operator<<(sys::pstream& out, const application& rhs) {
136  rhs.write(out);
137  return out;
138  }
139 
140  inline sys::pstream&
141  operator>>(sys::pstream& in, application& rhs) {
142  rhs.read(in);
143  return in;
144  }
145 
146  void
147  swap(application& lhs, application& rhs);
148 
149  namespace this_application {
150 
152  application_type
153  get_id() noexcept;
154 
155  sys::fd_type
156  get_input_fd() noexcept;
157 
158  sys::fd_type
159  get_output_fd() noexcept;
160 
161  bool
162  is_master() noexcept;
163 
164  bool
165  is_slave() noexcept;
166 
167  }
168 
169 }
170 
171 #endif // vim:filetype=cpp
T swap(T... args)
T front(T... args)
Definition: application.hh:25
T get_id(T... args)
T read(T... args)