Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
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
33class DobbyTimer;
34
35// -----------------------------------------------------------------------------
42class DobbyUtils : public virtual IDobbyUtils
43{
44public:
45 DobbyUtils();
46 ~DobbyUtils() final;
47
48public:
49 int loopDeviceAssociate(int fileFd, std::string* loopDevPath) const override;
50
51private:
52 int openLoopDevice(std::string* loopDevice) const;
53 bool attachFileToLoopDevice(int loopFd, int fileFd) const;
54
55public:
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
61private:
62 int runE2fsTool(int dirFd, std::list<std::string>* consoleOutput,
63 const char* e2fsTool, ...) const;
64
65public:
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
79private:
80 static bool deleteRecursive(int dirfd, int depth);
81
82public:
83 int getNamespaceFd(pid_t pid, int nsType) const override;
84
85private:
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
95public:
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
109public:
110 bool cancelTimer(int timerId) const override;
111
112private:
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
119public:
120 unsigned int getDriverMajorNumber(const std::string &driverName) const override;
121
122 bool deviceAllowed(dev_t device) const override;
123
124private:
126
127 std::set<dev_t> mDeviceWhitelist;
128
129 mutable std::mutex mMajorNumberLock;
130 mutable std::map<std::string, unsigned int> mMajorNumberCache;
131
132public:
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
145public:
146 bool insertEbtablesRule(const std::string &args) const override;
147 bool deleteEbtablesRule(const std::string &args) const override;
148
149private:
150 bool executeCommand(const std::string &command) const;
151 int getGIDorUID(pid_t pid, const std::string& idType) const;
152
153public:
154 uid_t getUID(pid_t pid) const override;
155 gid_t getGID(pid_t pid) const override;
156
157private:
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 checkExtImageFile(int dirFd, const std::string &imageFileName, bool repair) const override
Runs the e2fsck tool on a file system image to check it's integrity.
Definition DobbyUtils.cpp:1067
bool deleteEbtablesRule(const std::string &args) const override
Deletes the given ebtables rule from the existing set.
Definition DobbyUtils.cpp:1631
void setStringMetaData(const ContainerId &id, const std::string &key, const std::string &value) override
Sets / Gets string meta data for the given container.
Definition DobbyUtils.cpp:1550
bool rmdirContents(const std::string &path) const override
Removes the contents of a directory but leave the actual directory in place.
Definition DobbyUtils.cpp:346
void setIntegerMetaData(const ContainerId &id, const std::string &key, int value) override
Sets / Gets integer meta data for the given container.
Definition DobbyUtils.cpp:1522
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
std::string readTextFile(const std::string &path, size_t maxLen) const override
Simply read a string from a file.
Definition DobbyUtils.cpp:1330
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
uid_t getUID(pid_t pid) const override
Returns the UID for the given PID.
Definition DobbyUtils.cpp:1729
void buildDeviceWhitelist()
Builds the whitelist of allowed device numbers.
Definition DobbyUtils.cpp:1487
bool cancelTimer(int timerId) const override
Removes the given timer from the timer queue.
Definition DobbyUtils.cpp:1384
bool rmdirRecursive(const std::string &path) const override
Removes a directory and all it's contents.
Definition DobbyUtils.cpp:298
gid_t getGID(pid_t pid) const override
Returns the GID for the given PID.
Definition DobbyUtils.cpp:1717
int openLoopDevice(std::string *loopDevice) const
Attempts to open an available loop device.
Definition DobbyUtils.cpp:712
bool formatExtImageFile(int dirFd, const std::string &imageFileName, const std::string &fsType) const override
Runs the mke2fs tool to format a file system image.
Definition DobbyUtils.cpp:1164
bool mkdirRecursive(const std::string &path, mode_t mode) const override
Makes a directory and all parent directories as needed.
Definition DobbyUtils.cpp:166
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
int getNamespaceFd(pid_t pid, int nsType) const override
Returns a file descriptor to the given namespace of the process.
Definition DobbyUtils.cpp:488
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
bool deviceAllowed(dev_t device) const override
Returns true if the given device is allowed in the container.
Definition DobbyUtils.cpp:1470
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 writeTextFile(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:1263
void cleanMountLostAndFound(const std::string &mountPoint, const std::string &logTag) const override
Logs and deletes any files found in the lost+found directory of the mount point.
Definition DobbyUtils.cpp:388
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
int loopDeviceAssociate(int fileFd, std::string *loopDevPath) const override
Associates a give file descriptor with a loop device.
Definition DobbyUtils.cpp:844
unsigned int getDriverMajorNumber(const std::string &driverName) const override
Returns the major number assigned to a given driver.
Definition DobbyUtils.cpp:1404
Third version of the interface containing extra functions for working with ebtables.
Definition IDobbyUtils.h:637