softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontainer.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 "observableproperty.h"
23 #include "gatewayconfig.h"
24 #include "signalconnectionshandler.h"
25 #include "config/softwarecontainerconfig.h"
26 #include "filetoolkitwithundo.h"
27 #include "softwarecontainererror.h"
29 
30 #include <glibmm.h>
31 
32 #include <vector>
33 #include <sys/stat.h>
34 #include <sys/wait.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 
39 #include "functionjob.h"
40 #include "commandjob.h"
41 
42 namespace softwarecontainer {
43 
44 class Gateway;
45 class ContainerAbstractInterface;
46 
47 
48 enum class ContainerState
49 {
50  READY,
51  SUSPENDED,
52  TERMINATED,
53  INVALID
54 };
55 
56 
63 {
64 public:
66  SoftwareContainerError("Invalid operation on container")
67  {
68  }
69 
70  InvalidOperationError(const std::string &message):
71  SoftwareContainerError(message)
72  {
73  }
74 };
75 
76 
83 {
84 public:
86  SoftwareContainerError("Error in container implementation")
87  {
88  }
89 
90  ContainerError(const std::string &message):
91  SoftwareContainerError(message)
92  {
93  }
94 };
95 
96 
103 {
104 public:
106  SoftwareContainerError("Container is in an invalid state")
107  {
108  }
109 
110  InvalidContainerError(const std::string &message):
111  SoftwareContainerError(message)
112  {
113  }
114 };
115 
116 
166  private FileToolkitWithUndo,
168 {
169 public:
170  LOG_DECLARE_CLASS_CONTEXT("PCL", "SoftwareContainer library");
171 
184  SoftwareContainer(const ContainerID id,
185  std::unique_ptr<const SoftwareContainerConfig> config);
186 
188 
204  bool startGateways(const GatewayConfiguration &configs);
205 
214  std::shared_ptr<FunctionJob> createFunctionJob(const std::function<int()> fun);
215 
224  std::shared_ptr<CommandJob> createCommandJob(const std::string &command);
225 
243  void shutdown();
244 
253  void shutdown(unsigned int timeout);
254 
270  void suspend();
271 
286  void resume();
287 
294  bool bindMount(const std::string &pathOnHost,
295  const std::string &pathInContainer,
296  bool readonly = true);
297 
303  ObservableProperty<ContainerState> &getContainerState();
304 
315  bool previouslyConfigured();
316 
317 private:
318  /*
319  * Add gateways and create and initialize the underlying container
320  * implementation.
321  */
322  bool init();
323 
324  bool start();
325 
326  bool configureGateways(const GatewayConfiguration &gwConfig);
327  bool activateGateways();
328  bool shutdownGateways();
329 
330  std::string getContainerDir();
331  std::string getGatewayDir();
332 
333  void assertValidState();
334 
335  // Check if the workspace is sound, and set it up if it isn't
336  void checkContainerRoot(std::string rootDir);
337 #ifdef ENABLE_NETWORKGATEWAY
338  void checkNetworkSettings();
339 #endif // ENABLE_NETWORKGATEWAY
340 
341  ContainerID m_containerID;
342  std::shared_ptr<ContainerAbstractInterface> m_container;
343  pid_t m_pcPid = INVALID_PID;
345 
346  std::vector<std::unique_ptr<Gateway>> m_gateways;
347  std::unique_ptr<const SoftwareContainerConfig> m_config;
348 
349  std::string m_containerRoot;
350  unsigned int m_tmpfsSize;
351 
352  Glib::RefPtr<Glib::MainContext> m_mainLoopContext;
353  SignalConnectionsHandler m_connections;
354 
355  // Keeps track of if startGateways has been called on this instance
356  bool m_previouslyConfigured;
357 };
358 
359 } // namespace softwarecontainer
A method was called which is inappropriate in the current state.
Contains the softwarecontainer::SoftwareContainerAbstractInterface class.
The container instance is in an invalid state and should not be used.
The SignalConnectionsHandler class contains references to sigc++ connections and automatically discon...
An error has occured in the underlying container implementation.
An abstraction of concrete container implementations.
Developers guide to adding a config item: