softwarecontainer  0.18.0-739e8d7 2017-05-04
container.h
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #pragma once
21 
22 #include <string>
23 #include <vector>
24 
25 #include <lxc/lxccontainer.h>
26 
27 #include "filetoolkitwithundo.h"
28 
29 #include "softwarecontainer-common.h"
30 #include "containerabstractinterface.h"
31 
32 namespace softwarecontainer {
33 
42 class Container :
44 {
45  LOG_DECLARE_CLASS_CONTEXT("CONT", "Container");
46 
47  static constexpr const char *GATEWAYS_PATH = "/gateways";
48 
49  enum class LXCContainerState
50  {
51  STOPPED, STARTING, RUNNING, STOPPING, ABORTING, FREEZING, FROZEN, THAWED, ELEMENT_COUNT
52  };
53 
54  static const char *toString(LXCContainerState state)
55  {
56  return s_LXCContainerStates[static_cast<int>(state)];
57  }
58 
59  static std::vector<const char *> s_LXCContainerStates;
60  static const char *s_LXCRoot;
61 
67  static void init_lxc();
68 
69 public:
70 
81  Container(const std::string id,
82  const std::string &configFile,
83  const std::string &containerRoot,
84  bool writeBufferEnabled = false,
85  int shutdownTimeout = 1);
86 
87  ~Container();
88 
94  bool create();
95 
100  bool start(pid_t *pid);
101 
102  bool setCgroupItem(std::string subsys, std::string value);
103 
109  bool execute(const std::string &commandLine,
110  pid_t *pid,
111  const EnvironmentVariables &variables,
112  const std::string &workingDirectory = "/",
113  int stdin = -1,
114  int stdout = 1,
115  int stderr = 2);
116 
117  /*
118  * @brief Start a function inside the container.
119  *
120  * The function executes as a separate process.
121  */
122  bool execute(ExecFunction function,
123  pid_t *pid,
124  const EnvironmentVariables &variables = EnvironmentVariables(),
125  int stdin = -1,
126  int stdout = 1,
127  int stderr = 2);
128 
132  bool executeSync(ExecFunction function,
133  pid_t *pid,
134  const EnvironmentVariables &variables = EnvironmentVariables(),
135  int stdin = -1,
136  int stdout = 1,
137  int stderr = 2);
138 
150  bool bindMountInContainer(const std::string &pathInHost,
151  const std::string &pathInContainer,
152  bool readOnly = true);
153 
154 
155  bool mountDevice(const std::string &pathInHost);
156 
160  bool destroy();
161  bool destroy(unsigned int timeout);
162 
166  bool shutdown();
167  bool shutdown(unsigned int timeout);
168 
169  /*
170  * @brief calls freeze() on the LXC container
171  *
172  * This only works if the container is currently running and is not already
173  * suspended.
174  *
175  * @return true if the container was successfully suspended
176  * @return false otherwise
177  */
178  bool suspend();
179 
180  /*
181  * @brief calls unfreeze() on the LXC container
182  *
183  * This only works if the container was already suspended. This sets the container
184  * into running state again.
185  *
186  * @return true if the container was successfully resumed
187  * @return false otherwise
188  */
189  bool resume();
190 
194  bool stop();
195 
196  bool waitForState(LXCContainerState state, int timeout = 20);
197  bool ensureContainerRunning();
198 
206  bool initialize();
207 
208  std::string toString();
209 
210  const char *id() const;
211  std::string gatewaysDirInContainer() const;
212  std::string gatewaysDir() const;
213 
214  bool setEnvironmentVariable(const std::string &var, const std::string &val);
215 
216 private:
222  static int unlimitCoreDump();
223 
228  static int executeInContainerEntryFunction(void *param);
229 
233  bool bindMountCore(const std::string &pathInHost,
234  const std::string &pathInContainer,
235  const std::string &tempDir,
236  bool readonly);
237 
238  bool remountReadOnlyInContainer(const std::string &path);
239 
243  bool rollbackCreate();
244 
248  std::string m_configFile;
249 
253  const std::string m_id;
254 
255  std::string m_rootFSPath;
256 
260  struct lxc_container *m_container = nullptr;
261 
262  std::string m_containerRoot;
263 
264  bool m_writeBufferEnabled;
265 
266  // All environment variables set by gateways
267  EnvironmentVariables m_gatewayEnvironmentVariables;
268 
269  int m_shutdownTimeout = 1;
270 
271  enum class ContainerState : unsigned int {
272  DEFAULT = 0,
273  PREPARED = 1,
274  DESTROYED = 2,
275  CREATED = 3,
276  STARTED = 4,
277  FROZEN = 5,
278  };
279  ContainerState m_state = ContainerState::DEFAULT;
280 };
281 
282 } // namespace softwarecontainer
bool create()
create Creates a new lxc_container and creates it with all the initialization.
Definition: container.cpp:145
Container(const std::string id, const std::string &configFile, const std::string &containerRoot, bool writeBufferEnabled=false, int shutdownTimeout=1)
Constructor.
Definition: container.cpp:75
bool execute(const std::string &commandLine, pid_t *pid, const EnvironmentVariables &variables, const std::string &workingDirectory="/", int stdin=-1, int stdout=1, int stderr=2)
Start a process from the given command line, with an environment consisting of the variables previous...
Definition: container.cpp:429
The Container class is an abstraction of the specific containment technology used.
Definition: container.h:42
bool stop()
Calls stop on the lxc container(force stop)
Definition: container.cpp:477
bool executeSync(ExecFunction function, pid_t *pid, const EnvironmentVariables &variables=EnvironmentVariables(), int stdin=-1, int stdout=1, int stderr=2)
synchronous version of execute
Definition: container.cpp:335
bool shutdown()
Calls shutdown on the lxc container.
Definition: container.cpp:497
bool bindMountInContainer(const std::string &pathInHost, const std::string &pathInContainer, bool readOnly=true)
Tries to bind mount a path from host to container.
Definition: container.cpp:571
bool start(pid_t *pid)
Start the container.
Definition: container.cpp:265
bool initialize()
Setup the container for startup.
Definition: container.cpp:109
bool destroy()
Calls shutdown, and then destroys the container.
Definition: container.cpp:530
Developers guide to adding a config item: