Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
DobbyRdkPluginUtils.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 2020 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: DobbyRdkPluginUtils.h
21  *
22  */
23 #ifndef DOBBYRDKPLUGINUTILS_H
24 #define DOBBYRDKPLUGINUTILS_H
25 
26 #include "rt_dobby_schema.h"
27 #include "rt_state_schema.h"
28 
29 #if defined (DOBBY_BUILD)
30  #include <IDobbyStartState.h>
31 #else
32  #include <Dobby/rdkPlugins/IDobbyStartState.h>
33 #endif
34 
35 #include <sys/types.h>
36 #include <string>
37 #include <fstream>
38 #include <functional>
39 #include <memory>
40 #include <list>
41 #include <mutex>
42 #include <arpa/inet.h>
43 #include <vector>
44 
45 
46 // TODO:: This would be better stored in the dobby workspace dir rather than /tmp,
47 // but we don't programatically know the workspace dir in this code.
48 #define ADDRESS_FILE_DIR "/tmp/dobby/plugin/networking/"
49 
50 #define MOUNT_TUNNEL_CONTAINER_PATH "/mnt/.containermnttunnel"
51 #define MOUNT_TUNNEL_HOST_PATH "/tmp/.hostmnttunnel"
52 
53 typedef struct ContainerNetworkInfo
54 {
55  std::string vethName;
56  std::string ipAddress;
57  in_addr_t ipAddressRaw;
58  std::string containerId;
59 
60  bool operator==(const ContainerNetworkInfo &rhs) const
61  {
62  if (containerId.empty() || rhs.containerId.empty())
63  {
64  return ipAddressRaw == rhs.ipAddressRaw;
65  }
66  return containerId == rhs.containerId;
67  }
69 
70 // -----------------------------------------------------------------------------
78 {
79 public:
80  DobbyRdkPluginUtils(const std::shared_ptr<rt_dobby_schema> &cfg,
81  const std::string &containerId);
82  DobbyRdkPluginUtils(const std::shared_ptr<rt_dobby_schema> &cfg,
83  const std::shared_ptr<IDobbyStartState> &startState,
84  const std::string &containerId);
85  DobbyRdkPluginUtils(const std::shared_ptr<rt_dobby_schema> &cfg,
86  const std::shared_ptr<const rt_state_schema> &state,
87  const std::string &containerId);
88  DobbyRdkPluginUtils(const std::shared_ptr<rt_dobby_schema> &cfg,
89  const std::shared_ptr<const rt_state_schema> &state,
90  const std::shared_ptr<IDobbyStartState> &startState,
91  const std::string &containerId);
93 
94  // -------------------------------------------------------------------------
119  template< class Function, class... Args >
120  inline bool callInNamespace(pid_t pid, int nsType, Function&& f, Args&&... args) const
121  {
122  return this->callInNamespaceImpl(pid, nsType, std::bind(std::forward<Function>(f),
123  std::forward<Args>(args)...));
124  }
125 
126  bool callInNamespaceImpl(pid_t pid, int nsType,
127  const std::function<bool()>& func) const;
128 
129  void nsThread(int newNsFd, int nsType, bool* success,
130  std::function<bool()>& func) const;
131 
132 
133  pid_t getContainerPid() const;
134  std::string getContainerId() const;
136  bool getTakenVeths(std::vector<std::string> &takenVeths);
137 
138  bool writeTextFile(const std::string &path,
139  const std::string &str,
140  int flags,
141  mode_t mode) const;
142 
143  std::string readTextFile(const std::string &path) const;
144 
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;
149 
150  static bool mkdirRecursive(const std::string& path, mode_t mode);
151 
152  bool addEnvironmentVar(const std::string& envVar) const;
153 
154  int addFileDescriptor(const std::string& pluginName, int fd);
155 
156  std::list<int> files() const;
157 
158  std::list<int> files(const std::string& pluginName) const;
159 
160  int exitStatus;
161 
162 private:
163  std::string ipAddressToString(const in_addr_t &ipAddress);
164 
165 private:
166  mutable std::mutex mLock;
167 
168  std::shared_ptr<rt_dobby_schema> mConf;
169  std::shared_ptr<const rt_state_schema> mState;
170  std::shared_ptr<IDobbyStartState> mStartState;
171 
172  const std::string mContainerId;
173 };
174 
175 #endif // !defined(DOBBYRDKPLUGINUTILS_H)
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