Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Dobby.h
1 /*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2016 Sky UK
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 /*
20  * File: Dobby.h
21  *
22  */
23 #ifndef DOBBY_H
24 #define DOBBY_H
25 
26 #include "ContainerId.h"
27 
28 #include <IIpcService.h>
29 
30 #include <signal.h>
31 
32 #include <list>
33 #include <mutex>
34 #include <atomic>
35 #include <memory>
36 #include <string>
37 #include <condition_variable>
38 
39 class DobbyEnv;
40 class DobbyUtils;
41 class DobbyIPCUtils;
42 class DobbyManager;
43 class DobbyWorkQueue;
44 class DobbyLogger;
45 class IDobbySettings;
46 
47 // -----------------------------------------------------------------------------
55 class Dobby
56 {
57 public:
58  Dobby(const std::string& dbusAddress,
59  const std::shared_ptr<AI_IPC::IIpcService>& ipcService,
60  const std::shared_ptr<const IDobbySettings>& settings);
61  ~Dobby();
62 
63 public:
64  void run() const;
65 
66 public:
67  static void configSignals();
68 
69 public:
70  enum LogTarget : unsigned { Console = 0x1, SysLog = 0x2, EthanLog = 0x4, Journald = 0x8 };
71  static void setupLogging(unsigned targets = LogTarget::Console);
72 
73 public:
74  void setDefaultAIDbusAddresses(const std::string& aiPrivateBusAddress,
75  const std::string& aiPublicBusAddress);
76 
77 private:
78  #define DOBBY_DBUS_METHOD(x) \
79  void x(std::shared_ptr<AI_IPC::IAsyncReplySender> reply)
80 
81  DOBBY_DBUS_METHOD(ping);
82  DOBBY_DBUS_METHOD(shutdown);
83  DOBBY_DBUS_METHOD(setLogMethod);
84  DOBBY_DBUS_METHOD(setLogLevel);
85  DOBBY_DBUS_METHOD(setAIDbusAddress);
86 
87  DOBBY_DBUS_METHOD(startFromSpec);
88  DOBBY_DBUS_METHOD(startFromBundle);
89  DOBBY_DBUS_METHOD(stop);
90  DOBBY_DBUS_METHOD(pause);
91  DOBBY_DBUS_METHOD(resume);
92  DOBBY_DBUS_METHOD(hibernate);
93  DOBBY_DBUS_METHOD(wakeup);
94  DOBBY_DBUS_METHOD(addMount);
95  DOBBY_DBUS_METHOD(removeMount);
96  DOBBY_DBUS_METHOD(exec);
97  DOBBY_DBUS_METHOD(list);
98  DOBBY_DBUS_METHOD(getState);
99  DOBBY_DBUS_METHOD(getInfo);
100 
101 #if defined(LEGACY_COMPONENTS) && (AI_BUILD_TYPE == AI_DEBUG)
102  DOBBY_DBUS_METHOD(createBundle);
103  DOBBY_DBUS_METHOD(getSpec);
104 #endif //defined(LEGACY_COMPONENTS) && (AI_BUILD_TYPE == AI_DEBUG)
105 
106 #if (AI_BUILD_TYPE == AI_DEBUG)
107  DOBBY_DBUS_METHOD(getOCIConfig);
108 #endif //(AI_BUILD_TYPE == AI_DEBUG)
109 
110 #if defined(AI_ENABLE_TRACING)
111  DOBBY_DBUS_METHOD(startInProcessTracing);
112  DOBBY_DBUS_METHOD(stopInProcessTracing);
113 #endif
114 
115  #undef DOBBY_DBUS_METHOD
116 
117 private:
118  void initIpcMethods();
119 
120 #if defined(RDK)
121  void initWatchdog();
122  bool onWatchdogTimer();
123 #endif
124 
125 
126 private:
127  void onContainerStarted(int32_t cd, const ContainerId& id);
128  void onContainerStopped(int32_t cd, const ContainerId& id, int status);
129  void onContainerHibernated(int32_t cd, const ContainerId& id);
130  void onContainerAwoken(int32_t cd, const ContainerId& id);
131 
132 private:
133  void runWorkQueue() const;
134 
135 private:
136  std::shared_ptr<DobbyEnv> mEnvironment;
137  std::shared_ptr<DobbyUtils> mUtilities;
138  std::shared_ptr<DobbyIPCUtils> mIPCUtilities;
139  std::shared_ptr<DobbyManager> mManager;
140  std::unique_ptr<DobbyWorkQueue> mWorkQueue;
141 
142 private:
143  const std::shared_ptr<AI_IPC::IIpcService> mIpcService;
144  const std::string mService;
145  const std::string mObjectPath;
146  std::list<std::string> mHandlers;
147 
148 private:
149  std::atomic<bool> mShutdown;
150 
151  int mWatchdogTimerId;
152 
153 private:
154  static void nullSigChildHandler(int sigNum, siginfo_t *info, void *context);
155 
156  static volatile sig_atomic_t mSigTerm;
157  static void sigTermHandler(int sigNum);
158 
159 private:
160  static void logPrinter(int level, const char *file, const char *func,
161  int line, const char *message);
162  static void logConsolePrinter(int level, const char *file, const char *func,
163  int line, const char *message);
164 #if defined(RDK)
165  static void logJournaldPrinter(int level, const char *file, const char *func,
166  int line, const char *message);
167 #endif
168 
169  static std::atomic<unsigned> mLogTargets;
170  static int mEthanLogPipeFd;
171 };
172 
173 
174 #endif // !defined(DOBBY_H)
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
Basic class used to store the stb environment.
Definition: DobbyEnv.h:46
Utility methods for IPC in Dobby.
Definition: DobbyIPCUtils.h:44
Definition: DobbyLogger.h:41
The main object which starts / stops / manages the containers.
Definition: DobbyManager.h:77
Utility methods for hooks and the general containiser daemon.
Definition: DobbyUtils.h:43
Definition: DobbyWorkQueue.h:37
The root Dobby object, runs the dbus loop.
Definition: Dobby.h:56
static std::atomic< unsigned > mLogTargets
The target for logging, can be dynamically changed via dbus.
Definition: Dobby.h:169
static void setupLogging(unsigned targets=LogTarget::Console)
Static method must be called early in the startup before object is instantiated.
Definition: Dobby.cpp:479
void onContainerStopped(int32_t cd, const ContainerId &id, int status)
Called by the DobbyManager code when a container has stopped.
Definition: Dobby.cpp:2090
static void configSignals()
Utility function that MUST be called at startup from the main thread before any other threads are spa...
Definition: Dobby.cpp:213
static void sigTermHandler(int sigNum)
Signal handler for SIGTERM.
Definition: Dobby.cpp:181
static void logPrinter(int level, const char *file, const char *func, int line, const char *message)
Logging callback, called every time a log message needs to be emitted.
Definition: Dobby.cpp:414
void runWorkQueue() const
Runs the Dobby work queue to handle API calls.
Definition: Dobby.cpp:531
static int mEthanLogPipeFd
Definition: Dobby.h:170
void setDefaultAIDbusAddresses(const std::string &aiPrivateBusAddress, const std::string &aiPublicBusAddress)
Debugging function for manually setting the AI dbus addresses.
Definition: Dobby.cpp:605
void onContainerAwoken(int32_t cd, const ContainerId &id)
Called by the DobbyManager code when a container returns back from hibernated.
Definition: Dobby.cpp:2157
void initIpcMethods()
Installs handlers for all the dbus/ipc methods.
Definition: Dobby.cpp:623
void onContainerStarted(int32_t cd, const ContainerId &id)
Called by the DobbyManager code when a container has started.
Definition: Dobby.cpp:2057
static void nullSigChildHandler(int sigNum, siginfo_t *info, void *context)
Signal handler that does nothing.
Definition: Dobby.cpp:196
void onContainerHibernated(int32_t cd, const ContainerId &id)
Called by the DobbyManager code when a container gets hibernated.
Definition: Dobby.cpp:2131
static void logConsolePrinter(int level, const char *file, const char *func, int line, const char *message)
Writes logging output to the console.
Definition: Dobby.cpp:272
void run() const
Issues a 'ready' signal over dbus and then blocks until either a shutdown request is received or SIGT...
Definition: Dobby.cpp:559
Interface provided to the library at startup, contains the configuration options for Dobby.
Definition: IDobbySettings.h:50