Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
DobbyUtils.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: DobbyUtils.h
21  *
22  */
23 #ifndef DOBBYUTILS_H
24 #define DOBBYUTILS_H
25 
26 #include "IDobbyUtils.h"
27 
28 #include <map>
29 #include <mutex>
30 #include <functional>
31 #include <set>
32 
33 class DobbyTimer;
34 
35 // -----------------------------------------------------------------------------
42 class DobbyUtils : public virtual IDobbyUtils
43 {
44 public:
45  DobbyUtils();
46  ~DobbyUtils() final;
47 
48 public:
49  int loopDeviceAssociate(int fileFd, std::string* loopDevPath) const override;
50 
51 private:
52  int openLoopDevice(std::string* loopDevice) const;
53  bool attachFileToLoopDevice(int loopFd, int fileFd) const;
54 
55 public:
56  bool checkExtImageFile(int dirFd, const std::string& imageFileName,
57  bool repair) const override;
58  bool formatExtImageFile(int dirFd, const std::string& imageFileName,
59  const std::string& fsType) const override;
60 
61 private:
62  int runE2fsTool(int dirFd, std::list<std::string>* consoleOutput,
63  const char* e2fsTool, ...) const;
64 
65 public:
66  bool mkdirRecursive(const std::string& path, mode_t mode) const override;
67  bool mkdirRecursive(int dirFd, const std::string& path, mode_t mode) const override;
68 
69  bool rmdirRecursive(const std::string& path) const override;
70  bool rmdirRecursive(int dirFd, const std::string& path) const override;
71 
72  bool rmdirContents(const std::string& path) const override;
73  bool rmdirContents(int dirFd, const std::string& path) const override;
74  bool rmdirContents(int dirFd) const override;
75 
76  void cleanMountLostAndFound(const std::string& mountPoint,
77  const std::string& logTag) const override;
78 
79 private:
80  static bool deleteRecursive(int dirfd, int depth);
81 
82 public:
83  int getNamespaceFd(pid_t pid, int nsType) const override;
84 
85 private:
86  bool callInNamespaceImpl(pid_t pid, int nsType,
87  const std::function<bool()>& func) const override;
88 
89  bool callInNamespaceImpl(int namespaceFd,
90  const std::function<bool()>& func) const override;
91 
92  void nsThread(int newNsFd, int nsType, bool* success,
93  std::function<bool()>& func) const;
94 
95 public:
96  bool writeTextFileAt(int dirFd, const std::string& path,
97  const std::string& str,
98  int flags, mode_t mode) const override;
99  bool writeTextFile(const std::string& path,
100  const std::string& str,
101  int flags, mode_t mode) const override;
102 
103  std::string readTextFile(const std::string& path,
104  size_t maxLen) const override;
105  std::string readTextFileAt(int dirFd, const std::string& path,
106  size_t maxLen) const override;
107 
108 
109 public:
110  bool cancelTimer(int timerId) const override;
111 
112 private:
113  int startTimerImpl(const std::chrono::milliseconds& timeout,
114  bool oneShot,
115  const std::function<bool()>& handler) const override;
116 
117  std::shared_ptr<DobbyTimer> mTimerQueue;
118 
119 public:
120  unsigned int getDriverMajorNumber(const std::string &driverName) const override;
121 
122  bool deviceAllowed(dev_t device) const override;
123 
124 private:
125  void buildDeviceWhitelist();
126 
127  std::set<dev_t> mDeviceWhitelist;
128 
129  mutable std::mutex mMajorNumberLock;
130  mutable std::map<std::string, unsigned int> mMajorNumberCache;
131 
132 public:
133  void setIntegerMetaData(const ContainerId &id, const std::string &key,
134  int value) override;
135  int getIntegerMetaData(const ContainerId &id, const std::string &key,
136  int defaultValue) const override;
137 
138  void setStringMetaData(const ContainerId &id, const std::string &key,
139  const std::string &value) override;
140  std::string getStringMetaData(const ContainerId &id, const std::string &key,
141  const std::string &defaultValue) const override;
142 
143  void clearContainerMetaData(const ContainerId &id) override;
144 
145 public:
146  bool insertEbtablesRule(const std::string &args) const override;
147  bool deleteEbtablesRule(const std::string &args) const override;
148 
149 private:
150  bool executeCommand(const std::string &command) const;
151  int getGIDorUID(pid_t pid, const std::string& idType) const;
152 
153 public:
154  uid_t getUID(pid_t pid) const override;
155  gid_t getGID(pid_t pid) const override;
156 
157 private:
158  std::mutex mMetaDataLock;
159  std::map<std::pair<ContainerId, std::string>, int> mIntegerMetaData;
160  std::map<std::pair<ContainerId, std::string>, std::string> mStringMetaData;
161 
162 };
163 
164 
165 #endif // !defined(DOBBYUTILS_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
Utility object that can be used to register a callback function to execute in the future.
Definition: DobbyTimer.h:58
Utility methods for hooks and the general containiser daemon.
Definition: DobbyUtils.h:43
bool deleteEbtablesRule(const std::string &args) const override
Deletes the given ebtables rule from the existing set.
Definition: DobbyUtils.cpp:1631
void nsThread(int newNsFd, int nsType, bool *success, std::function< bool()> &func) const
Thread helper function that implements the setns syscall.
Definition: DobbyUtils.cpp:543
int startTimerImpl(const std::chrono::milliseconds &timeout, bool oneShot, const std::function< bool()> &handler) const override
Adds a new timer to the timer queue.
Definition: DobbyUtils.cpp:1360
void buildDeviceWhitelist()
Builds the whitelist of allowed device numbers.
Definition: DobbyUtils.cpp:1487
int openLoopDevice(std::string *loopDevice) const
Attempts to open an available loop device.
Definition: DobbyUtils.cpp:712
bool writeTextFileAt(int dirFd, const std::string &path, const std::string &str, int flags, mode_t mode) const override
Simply writes a string into a file.
Definition: DobbyUtils.cpp:1223
void clearContainerMetaData(const ContainerId &id) override
Clears all the meta data stored for a given container.
Definition: DobbyUtils.cpp:1576
std::string readTextFileAt(int dirFd, const std::string &path, size_t maxLen) const override
Simply read a string from a file.
Definition: DobbyUtils.cpp:1288
int getGIDorUID(pid_t pid, const std::string &idType) const
Returns the effective GID or UID for the given PID by parsing /proc/<PID>/status.
Definition: DobbyUtils.cpp:1674
int runE2fsTool(int dirFd, std::list< std::string > *consoleOutput, const char *e2fsTool,...) const
Run the E2FS tool inside the given directory with given args.
Definition: DobbyUtils.cpp:890
bool callInNamespaceImpl(pid_t pid, int nsType, const std::function< bool()> &func) const override
Utility function to run some code in a specific namespace of the container.
Definition: DobbyUtils.cpp:629
bool attachFileToLoopDevice(int loopFd, int fileFd) const
Attempts to attach the file to the loop device.
Definition: DobbyUtils.cpp:794
bool insertEbtablesRule(const std::string &args) const override
Inserts the given ebtables rule to the existing set.
Definition: DobbyUtils.cpp:1614
static bool deleteRecursive(int dirfd, int depth)
Recursive function that deletes everything within the supplied directory (as a descriptor).
Definition: DobbyUtils.cpp:180
Third version of the interface containing extra functions for working with ebtables.
Definition: IDobbyUtils.h:637
virtual unsigned int getDriverMajorNumber(const std::string &driverName) const=0
Returns the major number assigned to a given driver.
virtual bool formatExtImageFile(int dirFd, const std::string &imageFileName, const std::string &fsType="ext4") const=0
Runs the mke2fs tool to format a file system image.
virtual bool mkdirRecursive(const std::string &path, mode_t mode) const=0
Makes a directory and all parent directories as needed.
virtual bool rmdirRecursive(const std::string &path) const=0
Removes a directory and all it's contents.
virtual void setIntegerMetaData(const ContainerId &id, const std::string &key, int value)=0
Sets / Gets integer meta data for the given container.
virtual bool rmdirContents(const std::string &path) const=0
Removes the contents of a directory but leave the actual directory in place.
virtual void cleanMountLostAndFound(const std::string &mountPoint, const std::string &logTag=std::string()) const=0
Logs and deletes any files found in the lost+found directory of the mount point.
virtual gid_t getGID(pid_t pid) const=0
Returns the GID for the given PID.
virtual bool writeTextFile(const std::string &path, const std::string &str, int flags, mode_t mode=0644) const=0
Simply writes a string into a file.
virtual int getNamespaceFd(pid_t pid, int nsType) const=0
Returns a file descriptor to the given namespace of the process.
virtual std::string readTextFile(const std::string &path, size_t maxLen=4096) const=0
Simply read a string from a file.
virtual bool checkExtImageFile(int dirFd, const std::string &imageFileName, bool repair=true) const=0
Runs the e2fsck tool on a file system image to check it's integrity.
virtual bool deviceAllowed(dev_t device) const=0
Returns true if the given device is allowed in the container.
virtual bool cancelTimer(int timerId) const=0
Removes the given timer from the timer queue.
virtual void setStringMetaData(const ContainerId &id, const std::string &key, const std::string &value)=0
Sets / Gets string meta data for the given container.
virtual uid_t getUID(pid_t pid) const=0
Returns the UID for the given PID.
virtual int loopDeviceAssociate(int fileFd, std::string *loopDevPath=nullptr) const=0
Associates a give file descriptor with a loop device.