Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
IDobbyUtils.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: IDobbyUtils.h
21 *
22 */
23#ifndef IDOBBYUTILS_H
24#define IDOBBYUTILS_H
25
26#include <ContainerId.h>
27
28#include <cstdint>
29#include <string>
30#include <list>
31#include <chrono>
32#include <functional>
33#include <memory>
34#include <map>
35#include <string.h>
36
37#include <sys/types.h>
38#include <sys/sysmacros.h>
39
40
41// -----------------------------------------------------------------------------
52{
53public:
54 virtual ~IDobbyUtils_v1() = default;
55
56public:
57
58 // -------------------------------------------------------------------------
79 virtual bool mkdirRecursive(const std::string& path, mode_t mode) const = 0;
80 virtual bool mkdirRecursive(int dirFd, const std::string& path, mode_t mode) const = 0;
81
82 // -------------------------------------------------------------------------
104 virtual bool rmdirRecursive(const std::string& path) const = 0;
105 virtual bool rmdirRecursive(int dirFd, const std::string& path) const = 0;
106
107 // -------------------------------------------------------------------------
130 virtual bool rmdirContents(const std::string& path) const = 0;
131 virtual bool rmdirContents(int dirFd, const std::string& path) const = 0;
132 virtual bool rmdirContents(int dirFd) const = 0;
133
134
135 // -------------------------------------------------------------------------
152 virtual int loopDeviceAssociate(int fileFd, std::string* loopDevPath = nullptr) const = 0;
153
154
155 // -------------------------------------------------------------------------
177 virtual bool checkExtImageFile(int dirFd, const std::string& imageFileName,
178 bool repair = true) const = 0;
179
180 // -------------------------------------------------------------------------
201 virtual bool formatExtImageFile(int dirFd, const std::string& imageFileName,
202 const std::string& fsType = "ext4") const = 0;
203
204 // -------------------------------------------------------------------------
220 virtual void cleanMountLostAndFound(const std::string& mountPoint,
221 const std::string& logTag = std::string()) const = 0;
222
223 // -------------------------------------------------------------------------
243 virtual bool writeTextFile(const std::string& path,
244 const std::string& str,
245 int flags, mode_t mode = 0644) const = 0;
246 virtual bool writeTextFileAt(int dirFd, const std::string& path,
247 const std::string& str,
248 int flags, mode_t mode = 0644) const = 0;
249
250 // -------------------------------------------------------------------------
268 virtual std::string readTextFile(const std::string& path,
269 size_t maxLen = 4096) const = 0;
270 virtual std::string readTextFileAt(int dirFd, const std::string& path,
271 size_t maxLen = 4096) const = 0;
272
273 // -------------------------------------------------------------------------
291 virtual int getNamespaceFd(pid_t pid, int nsType) const = 0;
292
293 // -------------------------------------------------------------------------
327 template< class Function >
328 inline bool callInNamespace(pid_t pid, int nsType, Function func) const
329 {
330 return this->callInNamespaceImpl(pid, nsType, func);
331 }
332
333 // -------------------------------------------------------------------------
348 template< class Function, class... Args >
349 inline bool callInNamespace(pid_t pid, int nsType, Function&& f, Args&&... args) const
350 {
351 return this->callInNamespaceImpl(pid, nsType, std::bind(std::forward<Function>(f),
352 std::forward<Args>(args)...));
353 }
354
355
356 // -------------------------------------------------------------------------
380 template< class Rep, class Period >
381 inline int startTimer(const std::chrono::duration<Rep, Period>& timeout,
382 bool oneShot,
383 const std::function<bool()>& handler) const
384 {
385 return this->startTimerImpl(std::chrono::duration_cast<std::chrono::milliseconds>(timeout),
386 oneShot,
387 handler);
388 }
389
390 // -------------------------------------------------------------------------
407 virtual bool cancelTimer(int timerId) const = 0;
408
409
410 // -------------------------------------------------------------------------
423 template< class Function >
424 inline bool callInNamespace(int namespaceFd, Function func) const
425 {
426 return this->callInNamespaceImpl(namespaceFd, func);
427 }
428
429 template< class Function, class... Args >
430 inline bool callInNamespace(int namespaceFd, Function&& f, Args&&... args) const
431 {
432 return this->callInNamespaceImpl(namespaceFd, std::bind(std::forward<Function>(f),
433 std::forward<Args>(args)...));
434 }
435
436
437 // -------------------------------------------------------------------------
453 virtual unsigned int getDriverMajorNumber(const std::string &driverName) const = 0;
454
455 // -------------------------------------------------------------------------
474 virtual bool deviceAllowed(dev_t device) const = 0;
475
476 inline bool deviceAllowed(unsigned int major, unsigned int minor) const
477 {
478 return this->deviceAllowed(makedev(major, minor));
479 }
480
481protected:
482
483 // -------------------------------------------------------------------------
497 virtual bool callInNamespaceImpl(pid_t pid, int nsType, const std::function<bool()>& func) const = 0;
498
499 // -------------------------------------------------------------------------
511 virtual bool callInNamespaceImpl(int namespaceFd, const std::function<bool()>& func) const = 0;
512
513 // -------------------------------------------------------------------------
529 virtual int startTimerImpl(const std::chrono::milliseconds& timeout,
530 bool oneShot,
531 const std::function<bool()>& handler) const = 0;
532
533 // -------------------------------------------------------------------------
543 virtual gid_t getGID(pid_t pid) const = 0;
544
545 // -------------------------------------------------------------------------
555 virtual uid_t getUID(pid_t pid) const = 0;
556
557};
558
559
560// -----------------------------------------------------------------------------
569class IDobbyUtils_v2 : public virtual IDobbyUtils_v1
570{
571public:
580 using IDobbyUtils_v1::writeTextFileAt;
582 using IDobbyUtils_v1::readTextFileAt;
589
590public:
591
592 // -------------------------------------------------------------------------
601 virtual void setIntegerMetaData(const ContainerId &id, const std::string &key, int value) = 0;
602 virtual int getIntegerMetaData(const ContainerId &id, const std::string &key, int defaultValue) const = 0;
603 inline int getIntegerMetaData(const ContainerId &id, const std::string &key)
604 {
605 return getIntegerMetaData(id, key, -1);
606 }
607
608 // -------------------------------------------------------------------------
617 virtual void setStringMetaData(const ContainerId &id, const std::string &key, const std::string &value) = 0;
618 virtual std::string getStringMetaData(const ContainerId &id, const std::string &key, const std::string &defaultValue) const = 0;
619 inline std::string getStringMetaData(const ContainerId &id, const std::string &key)
620 {
621 return getStringMetaData(id, key, std::string());
622 }
623 virtual void clearContainerMetaData(const ContainerId &id) = 0;
624
625};
626
627// -----------------------------------------------------------------------------
636class IDobbyUtils_v3 : public virtual IDobbyUtils_v2
637{
638public:
647 using IDobbyUtils_v1::writeTextFileAt;
649 using IDobbyUtils_v1::readTextFileAt;
656
658 using IDobbyUtils_v2::getIntegerMetaData;
660 using IDobbyUtils_v2::getStringMetaData;
661 using IDobbyUtils_v2::clearContainerMetaData;
662
663public:
664
665 // -------------------------------------------------------------------------
676 virtual bool insertEbtablesRule(const std::string &args) const = 0;
677
678 // -------------------------------------------------------------------------
689 virtual bool deleteEbtablesRule(const std::string &args) const = 0;
690
691};
692
693
695
696
697
698#endif // !defined(IDOBBYUTILS_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
Interface that exports some utilities that plugins may find useful.
Definition IDobbyUtils.h:52
bool callInNamespace(int namespaceFd, Function func) const
Call the given function in the namespace of the descriptor.
Definition IDobbyUtils.h:424
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.
int startTimer(const std::chrono::duration< Rep, Period > &timeout, bool oneShot, const std::function< bool()> &handler) const
Adds a new timer to the timer queue.
Definition IDobbyUtils.h:381
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 int loopDeviceAssociate(int fileFd, std::string *loopDevPath=nullptr) const =0
Associates a give file descriptor with a loop device.
virtual std::string readTextFile(const std::string &path, size_t maxLen=4096) const =0
Simply read a string from a file.
virtual bool mkdirRecursive(const std::string &path, mode_t mode) const =0
Makes a directory and all parent directories as needed.
virtual bool deviceAllowed(dev_t device) const =0
Returns true if the given device is allowed in the container.
virtual bool rmdirContents(const std::string &path) const =0
Removes the contents of a directory but leave the actual directory in place.
virtual bool rmdirRecursive(const std::string &path) const =0
Removes a directory and all it's contents.
virtual uid_t getUID(pid_t pid) const =0
Returns the UID for the given PID.
virtual gid_t getGID(pid_t pid) const =0
Returns the GID for the given PID.
virtual int getNamespaceFd(pid_t pid, int nsType) const =0
Returns a file descriptor to the given namespace of the process.
virtual unsigned int getDriverMajorNumber(const std::string &driverName) const =0
Returns the major number assigned to a given driver.
virtual bool callInNamespaceImpl(pid_t pid, int nsType, const std::function< bool()> &func) const =0
Implementation of the callInNamespace public interface.
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 int startTimerImpl(const std::chrono::milliseconds &timeout, bool oneShot, const std::function< bool()> &handler) const =0
Adds a new timer to the timer queue.
virtual bool cancelTimer(int timerId) const =0
Removes the given timer from the timer queue.
bool callInNamespace(pid_t pid, int nsType, Function func) const
Calls the given function in the namespace of given pid.
Definition IDobbyUtils.h:328
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 bool callInNamespaceImpl(int namespaceFd, const std::function< bool()> &func) const =0
Implementation of the callInNamespace public interface.
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 IDobbyUtils.h:349
Second version of the interface containing extra functions for working with iptables.
Definition IDobbyUtils.h:570
virtual void setIntegerMetaData(const ContainerId &id, const std::string &key, int value)=0
Sets / Gets integer meta data for the given container.
virtual void setStringMetaData(const ContainerId &id, const std::string &key, const std::string &value)=0
Sets / Gets string meta data for the given container.
Third version of the interface containing extra functions for working with ebtables.
Definition IDobbyUtils.h:637
virtual bool deleteEbtablesRule(const std::string &args) const =0
Deletes the given ebtables rule from the existing set.
virtual bool insertEbtablesRule(const std::string &args) const =0
Inserts the given ebtables rule to the existing set.