23 #ifndef DOBBYMANAGER_H
24 #define DOBBYMANAGER_H
26 #include "IDobbyUtils.h"
27 #include "IDobbyIPCUtils.h"
28 #include "DobbyConfig.h"
29 #include "IDobbyRdkPlugin.h"
30 #include "IDobbyRdkLoggingPlugin.h"
31 #include "ContainerId.h"
32 #include "DobbyLogger.h"
33 #include "DobbyRunC.h"
34 #include <IIpcService.h>
47 #include <netinet/in.h>
48 #include <semaphore.h>
51 # include <json/json.h>
53 # include <jsoncpp/json.h>
79 typedef std::function<void(int32_t cd,
const ContainerId&
id)> ContainerStartedFunc;
80 typedef std::function<void(int32_t cd,
const ContainerId&
id, int32_t status)> ContainerStoppedFunc;
81 typedef std::function<void(int32_t cd,
const ContainerId&
id)> ContainerHibernatedFunc;
85 const std::shared_ptr<IDobbyUtils>& utils,
86 const std::shared_ptr<IDobbyIPCUtils>& ipcUtils,
87 const std::shared_ptr<const IDobbySettings>& settings,
88 const ContainerStartedFunc& containerStartedCb,
89 const ContainerStoppedFunc& containerStoppedCb,
90 const ContainerHibernatedFunc& containerHibernatedCb,
91 const ContainerHibernatedFunc& containerAwokenCb);
102 #if defined(LEGACY_COMPONENTS)
103 int32_t startContainerFromSpec(
const ContainerId&
id,
104 const std::string& jsonSpec,
105 const std::list<int>& files,
106 const std::string& command,
107 const std::string& displaySocket,
108 const std::vector<std::string>& envVars);
112 const std::string& bundlePath,
113 const std::list<int>& files,
114 const std::string& command,
115 const std::string& displaySocket,
116 const std::vector<std::string>& envVars);
126 const std::string& source,
127 const std::string& destination,
128 const std::vector<std::string>& mountFlags,
129 const std::string& mountData);
131 bool removeMount(int32_t cd,
const std::string& source);
134 const std::string& options,
135 const std::string& command);
137 std::list<std::pair<int32_t, ContainerId>>
listContainers()
const;
146 #if defined(LEGACY_COMPONENTS)
148 std::string specOfContainer(int32_t cd)
const;
149 bool createBundle(
const ContainerId&
id,
const std::string& jsonSpec);
157 std::shared_ptr<IDobbyRdkLoggingPlugin>
161 const std::unique_ptr<DobbyContainer> &container,
162 const std::list<int> &files);
165 const std::string &command,
166 const std::string &displaySocket,
167 const std::vector<std::string> &envVars);
170 const std::unique_ptr<DobbyContainer>& container,
171 const std::list<int>& files);
174 const std::unique_ptr<DobbyContainer>& container);
177 ContainerStartedFunc mContainerStartedCb;
178 ContainerStoppedFunc mContainerStoppedCb;
179 ContainerHibernatedFunc mContainerHibernatedCb;
180 ContainerHibernatedFunc mContainerAwokenCb;
183 mutable std::mutex mLock;
184 std::map<ContainerId, std::unique_ptr<DobbyContainer>> mContainers;
185 std::multimap<ContainerId, pid_t> mContainerExecPids;
190 bool onPostHaltHook(
const std::unique_ptr<DobbyContainer> &container);
192 #if defined(LEGACY_COMPONENTS)
195 const std::shared_ptr<DobbyStartState>& startState,
196 const std::unique_ptr<DobbyContainer>& container);
198 const std::unique_ptr<DobbyContainer>& container);
200 const std::unique_ptr<DobbyContainer>& container);
202 const std::unique_ptr<DobbyContainer>& container);
204 const std::unique_ptr<DobbyContainer>& container);
207 enum class HookType {
208 PostConstruction, PreStart, PostStart, PostStop, PreDestruction };
218 bool shouldEnableSTrace(
const std::shared_ptr<DobbyConfig> &config)
const;
220 const std::shared_ptr<IDobbyEnv> mEnvironment;
221 const std::shared_ptr<IDobbyUtils> mUtilities;
222 const std::shared_ptr<IDobbyIPCUtils> mIPCUtilities;
223 const std::shared_ptr<const IDobbySettings> mSettings;
226 std::unique_ptr<DobbyLogger> mLogger;
227 std::unique_ptr<DobbyRunC> mRunc;
230 sem_t mRuncMonitorThreadStartedSem;
231 std::thread mRuncMonitorThread;
232 std::atomic<bool> mRuncMonitorTerminate;
233 int mCleanupTaskTimerId;
235 #if defined(LEGACY_COMPONENTS)
237 std::unique_ptr<DobbyLegacyPluginManager> mLegacyPlugins;
A wrapper around a std::string, used to add some type definition to to an id and also to sanity check...
Definition: ContainerId.h:41
Interface that configuration file parser classes have to implement.
Definition: DobbyConfig.h:66
Wrapper object used to store container resources.
Definition: DobbyContainer.h:59
Class that manages all the plugin hook libraries.
Definition: DobbyLegacyPluginManager.h:65
The main object which starts / stops / manages the containers.
Definition: DobbyManager.h:77
bool restartContainer(const ContainerId &id, const std::unique_ptr< DobbyContainer > &container)
Attempts to restart the container.
Definition: DobbyManager.cpp:1243
void cleanupContainersShutdown()
Gracefully stops and cleans up any running containers. Will emit the container stop event when a cont...
Definition: DobbyManager.cpp:476
bool execInContainer(int32_t cd, const std::string &options, const std::string &command)
Executes a command in a running container.
Definition: DobbyManager.cpp:2144
bool resumeContainer(int32_t cd)
Thaws a frozen container.
Definition: DobbyManager.cpp:1491
void stopRuncMonitorThread()
Stops the monitor thread and cleans up it's resources.
Definition: DobbyManager.cpp:3125
void cleanupContainers()
Gets a list of running containers and tries to kill and delete them.
Definition: DobbyManager.cpp:375
std::string statsOfContainer(int32_t cd) const
Gets the stats for the container.
Definition: DobbyManager.cpp:2337
bool addMount(int32_t cd, const std::string &source, const std::string &destination, const std::vector< std::string > &mountFlags, const std::string &mountData)
adds a mount to a running container
Definition: DobbyManager.cpp:1761
void runcMonitorThread()
Thread function that monitors for any SIGCHILD signals and if detected loops through the running cont...
Definition: DobbyManager.cpp:3164
bool wakeupContainer(int32_t cd)
Wakeup a checkpointed container from existing dump.
Definition: DobbyManager.cpp:1664
int32_t startContainerFromBundle(const ContainerId &id, const std::string &bundlePath, const std::list< int > &files, const std::string &command, const std::string &displaySocket, const std::vector< std::string > &envVars)
Where the magic begins ... attempts to create a container from an OCI bundle*.
Definition: DobbyManager.cpp:993
bool cleanupContainer(const DobbyRunC::ContainerListItem &container)
Cleans up a container that is in an unknown state - used at Dobby startup to ensure the box is in a c...
Definition: DobbyManager.cpp:249
bool removeMount(int32_t cd, const std::string &source)
removes a mount from a running container
Definition: DobbyManager.cpp:2043
std::string ociConfigOfContainer(int32_t cd) const
Debugging method to allow you to retrieve the OCI config.json spec used to create the container.
Definition: DobbyManager.cpp:2408
bool createAndStartContainer(const ContainerId &id, const std::unique_ptr< DobbyContainer > &container, const std::list< int > &files)
Creates and attempts to start the container.
Definition: DobbyManager.cpp:694
bool onPostHaltHook(const std::unique_ptr< DobbyContainer > &container)
Called at the post-halt stage of container startup.
Definition: DobbyManager.cpp:2624
void setupSystem()
Configures the linux system for enabling features needed for runc.
Definition: DobbyManager.cpp:153
bool createAndStart(const ContainerId &id, const std::unique_ptr< DobbyContainer > &container, const std::list< int > &files)
Create and start a container. Set up and capture logs from all container hooks if an RDK logging plug...
Definition: DobbyManager.cpp:549
std::list< std::pair< int32_t, ContainerId > > listContainers() const
Returns a list of all the containers.
Definition: DobbyManager.cpp:2222
void setupWorkspace(const std::shared_ptr< IDobbyEnv > &env)
Configures the workspace directory.
Definition: DobbyManager.cpp:208
bool customiseConfig(const std::shared_ptr< DobbyConfig > &config, const std::string &command, const std::string &displaySocket, const std::vector< std::string > &envVars)
Updates the container config with custom options provided by the start command.
Definition: DobbyManager.cpp:634
bool onPreCreationHook(const std::unique_ptr< DobbyContainer > &container)
Called at the pre-create stage of container startup.
Definition: DobbyManager.cpp:2587
std::shared_ptr< IDobbyRdkLoggingPlugin > GetContainerLogger(const std::unique_ptr< DobbyContainer > &container)
Get the instance of the logging plugin for the current container (if one is loaded)
Definition: DobbyManager.cpp:522
void onChildExit()
Called when we detect a child process has terminated.
Definition: DobbyManager.cpp:2961
bool stopContainer(int32_t cd, bool withPrejudice)
Stops a running container.
Definition: DobbyManager.cpp:1310
int32_t stateOfContainer(int32_t cd) const
Returns the state of a given container.
Definition: DobbyManager.cpp:2247
bool onPostInstallationHook(const std::unique_ptr< DobbyContainer > &container)
Called at the post-installation stage of container startup.
Definition: DobbyManager.cpp:2540
bool hibernateContainer(int32_t cd, const std::string &options)
Dumps a running container's processes.
Definition: DobbyManager.cpp:1545
void handleContainerTerminate(const ContainerId &id, const std::unique_ptr< DobbyContainer > &container, const int status)
Perform all the necessary cleanup and run plugins required when a container has terminated.
Definition: DobbyManager.cpp:2864
void startRuncMonitorThread()
Starts a thread that monitors for SIGCHILD signals.
Definition: DobbyManager.cpp:3103
bool pauseContainer(int32_t cd)
Freezes a running container.
Definition: DobbyManager.cpp:1430
bool invalidContainerCleanupTask()
Task that will try and cleanup invalid/unknown state containers periodically - if the container can b...
Definition: DobbyManager.cpp:3220
Stores the start state of the container.
Definition: DobbyStartState.h:49
Interface that exports the environment of the daemon to plugins.
Definition: IDobbyEnv.h:46
Interface provided to the library at startup, contains the configuration options for Dobby.
Definition: IDobbySettings.h:50
Definition: DobbyRunC.h:67