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 typedef struct ContainerNetworkInfo
51 {
52  std::string vethName;
53  std::string ipAddress;
54  in_addr_t ipAddressRaw;
55  std::string containerId;
56 
57  bool operator==(const ContainerNetworkInfo &rhs) const
58  {
59  if (containerId.empty() || rhs.containerId.empty())
60  {
61  return ipAddressRaw == rhs.ipAddressRaw;
62  }
63  return containerId == rhs.containerId;
64  }
66 
67 // -----------------------------------------------------------------------------
75 {
76 public:
77  DobbyRdkPluginUtils(const std::shared_ptr<rt_dobby_schema> &cfg,
78  const std::string &containerId);
79  DobbyRdkPluginUtils(const std::shared_ptr<rt_dobby_schema> &cfg,
80  const std::shared_ptr<IDobbyStartState> &startState,
81  const std::string &containerId);
82  DobbyRdkPluginUtils(const std::shared_ptr<rt_dobby_schema> &cfg,
83  const std::shared_ptr<const rt_state_schema> &state,
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::shared_ptr<IDobbyStartState> &startState,
88  const std::string &containerId);
90 
91  // -------------------------------------------------------------------------
116  template< class Function, class... Args >
117  inline bool callInNamespace(pid_t pid, int nsType, Function&& f, Args&&... args) const
118  {
119  return this->callInNamespaceImpl(pid, nsType, std::bind(std::forward<Function>(f),
120  std::forward<Args>(args)...));
121  }
122 
123  bool callInNamespaceImpl(pid_t pid, int nsType,
124  const std::function<bool()>& func) const;
125 
126  void nsThread(int newNsFd, int nsType, bool* success,
127  std::function<bool()>& func) const;
128 
129 
130  pid_t getContainerPid() const;
131  std::string getContainerId() const;
133  bool getTakenVeths(std::vector<std::string> &takenVeths);
134 
135  bool writeTextFile(const std::string &path,
136  const std::string &str,
137  int flags,
138  mode_t mode) const;
139 
140  std::string readTextFile(const std::string &path) const;
141 
142  bool addMount(const std::string &source,
143  const std::string &target,
144  const std::string &fsType,
145  const std::list<std::string> &mountOptions) const;
146 
147  static bool mkdirRecursive(const std::string& path, mode_t mode);
148 
149  bool addEnvironmentVar(const std::string& envVar) const;
150 
151  int addFileDescriptor(const std::string& pluginName, int fd);
152 
153  std::list<int> files() const;
154 
155  std::list<int> files(const std::string& pluginName) const;
156 
157  int exitStatus;
158 
159 private:
160  std::string ipAddressToString(const in_addr_t &ipAddress);
161 
162 private:
163  mutable std::mutex mLock;
164 
165  std::shared_ptr<rt_dobby_schema> mConf;
166  std::shared_ptr<const rt_state_schema> mState;
167  std::shared_ptr<IDobbyStartState> mStartState;
168 
169  const std::string mContainerId;
170 };
171 
172 #endif // !defined(DOBBYRDKPLUGINUTILS_H)
Class for useful utility methods for plugins such as adding mounts and environment variables.
Definition: DobbyRdkPluginUtils.h:75
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:117
bool addEnvironmentVar(const std::string &envVar) const
Public api to allow for adding additional environment variables.
Definition: DobbyRdkPluginUtils.cpp:586
Definition: DobbyRdkPluginUtils.h:51