23 #ifndef DOBBYRDKPLUGINUTILS_H
24 #define DOBBYRDKPLUGINUTILS_H
26 #include "rt_dobby_schema.h"
27 #include "rt_state_schema.h"
29 #if defined (DOBBY_BUILD)
30 #include <IDobbyStartState.h>
32 #include <Dobby/rdkPlugins/IDobbyStartState.h>
35 #include <sys/types.h>
42 #include <arpa/inet.h>
48 #define ADDRESS_FILE_DIR "/tmp/dobby/plugin/networking/"
50 #define MOUNT_TUNNEL_CONTAINER_PATH "/mnt/.containermnttunnel"
51 #define MOUNT_TUNNEL_HOST_PATH "/tmp/.hostmnttunnel"
56 std::string ipAddress;
57 in_addr_t ipAddressRaw;
58 std::string containerId;
62 if (containerId.empty() || rhs.containerId.empty())
64 return ipAddressRaw == rhs.ipAddressRaw;
66 return containerId == rhs.containerId;
81 const std::string &containerId);
83 const std::shared_ptr<IDobbyStartState> &startState,
84 const std::string &containerId);
86 const std::shared_ptr<const rt_state_schema> &state,
87 const std::string &containerId);
89 const std::shared_ptr<const rt_state_schema> &state,
90 const std::shared_ptr<IDobbyStartState> &startState,
91 const std::string &containerId);
119 template<
class Function,
class... Args >
120 inline bool callInNamespace(pid_t pid,
int nsType, Function&& f, Args&&... args)
const
123 std::forward<Args>(args)...));
127 const std::function<
bool()>& func)
const;
129 void nsThread(
int newNsFd,
int nsType,
bool* success,
130 std::function<
bool()>& func)
const;
139 const std::string &str,
143 std::string
readTextFile(
const std::string &path)
const;
145 bool addMount(
const std::string &source,
146 const std::string &target,
147 const std::string &fsType,
148 const std::list<std::string> &mountOptions)
const;
156 std::list<int>
files()
const;
158 std::list<int>
files(
const std::string& pluginName)
const;
163 std::string ipAddressToString(
const in_addr_t &ipAddress);
166 mutable std::mutex mLock;
168 std::shared_ptr<rt_dobby_schema> mConf;
169 std::shared_ptr<const rt_state_schema> mState;
170 std::shared_ptr<IDobbyStartState> mStartState;
172 const std::string mContainerId;
Class for useful utility methods for plugins such as adding mounts and environment variables.
Definition: DobbyRdkPluginUtils.h:78
bool callInNamespaceImpl(pid_t pid, int nsType, const std::function< bool()> &func) const
Utility function to run some code in a specific namespace of the container.
Definition: DobbyRdkPluginUtils.cpp:305
std::string readTextFile(const std::string &path) const
Simply reads a file into a string.
Definition: DobbyRdkPluginUtils.cpp:446
pid_t getContainerPid() const
Gets the container pid from the stdin string of a hook.
Definition: DobbyRdkPluginUtils.cpp:107
bool getContainerNetworkInfo(ContainerNetworkInfo &networkInfo)
Gets network info about the container (veth/IP)
Definition: DobbyRdkPluginUtils.cpp:146
static bool mkdirRecursive(const std::string &path, mode_t mode)
Makes a directory and all parent directories as needed.
Definition: DobbyRdkPluginUtils.cpp:530
void nsThread(int newNsFd, int nsType, bool *success, std::function< bool()> &func) const
Thread helper function that implements the setns syscall.
Definition: DobbyRdkPluginUtils.cpp:254
bool getTakenVeths(std::vector< std::string > &takenVeths)
Gets allocated veth devices.
Definition: DobbyRdkPluginUtils.cpp:191
std::list< int > files() const
Gets all file descriptor registered by any client.
Definition: DobbyRdkPluginUtils.cpp:683
int addFileDescriptor(const std::string &pluginName, int fd)
Adds another file descriptor to be passed into the container.
Definition: DobbyRdkPluginUtils.cpp:659
std::string getContainerId() const
Gets the container ID.
Definition: DobbyRdkPluginUtils.cpp:130
bool writeTextFile(const std::string &path, const std::string &str, int flags, mode_t mode) const
Simply writes a string into a file.
Definition: DobbyRdkPluginUtils.cpp:395
bool addMount(const std::string &source, const std::string &target, const std::string &fsType, const std::list< std::string > &mountOptions) const
Public api to allow for adding additional mounts to a container's config file.
Definition: DobbyRdkPluginUtils.cpp:480
bool callInNamespace(pid_t pid, int nsType, Function &&f, Args &&... args) const
Slightly nicer version of callInNamespace, handles the function bind for you automatically.
Definition: DobbyRdkPluginUtils.h:120
bool addEnvironmentVar(const std::string &envVar) const
Public api to allow for adding additional environment variables.
Definition: DobbyRdkPluginUtils.cpp:586
Definition: DobbyRdkPluginUtils.h:54