37namespace fs = std::filesystem;
45ENUM(Unshare,ALL,USER,IPC,PID,NET,UTS,CGROUP);
48inline std::map<Unshare,Bits>
const unshare_mask =
50 {Unshare::USER, Bits{1} << 0},
51 {Unshare::IPC, Bits{1} << 1},
52 {Unshare::PID, Bits{1} << 2},
53 {Unshare::NET, Bits{1} << 3},
54 {Unshare::UTS, Bits{1} << 4},
55 {Unshare::CGROUP, Bits{1} << 5},
68 auto it = unshare_mask.find(unshare);
69 return_if(it == unshare_mask.end(), Error(
"E::Unshare option '{}' not found", unshare));
70 Bits mask = it->second;
71 if (value) { bits |= mask; }
72 else { bits &= ~mask; }
82[[nodiscard]]
inline std::set<std::string>
to_strings(Bits
const& bits)
noexcept
84 std::set<std::string> out;
85 for(
auto&& [unshare,mask] : unshare_mask)
89 std::string str = unshare;
90 std::ranges::transform(str, str.begin(), ::tolower);
106 static_assert(
sizeof(Bits) ==
sizeof(uint16_t),
"Bits size must be 2 bytes");
107 uint64_t offset_begin = ns_reserved::FIM_RESERVED_OFFSET_UNSHARE_BEGIN;
108 uint64_t offset_end = ns_reserved::FIM_RESERVED_OFFSET_UNSHARE_END;
109 return ns_reserved::write(path_file_binary, offset_begin, offset_end,
reinterpret_cast<char const*
>(&bits),
sizeof(bits));
120 static_assert(
sizeof(Bits) ==
sizeof(uint16_t),
"Bits size must be 2 bytes");
121 uint64_t offset_begin = ns_reserved::FIM_RESERVED_OFFSET_UNSHARE_BEGIN;
122 uint64_t size = ns_reserved::FIM_RESERVED_OFFSET_UNSHARE_END - offset_begin;
123 constexpr size_t const size_bits =
sizeof(Bits);
124 return_if(size_bits != size, Error(
"E::Trying to read an exceeding number of bytes: {} vs {}", size_bits, size));
126 Pop(
ns_reserved::read(path_file_binary, offset_begin,
reinterpret_cast<char*
>(&bits), size_bits));
136 fs::path m_path_file_binary;
138 Value<void> set_unshares(Bits bits, std::set<Unshare>
const& unshares,
bool value)
140 if(unshares.contains(Unshare::NONE))
142 return Error(
"E::Invalid unshare option 'NONE'");
144 if(unshares.contains(Unshare::ALL))
146 return_if(unshares.size() > 1, Error(
"E::Unshare option 'all' should not be used with others"));
149 for(Unshare
const& unshare : unshares)
151 Pop(
bit_set(bits, unshare, value));
153 Pop(
write(m_path_file_binary, bits));
162 : m_path_file_binary(path_file_binary)
172 auto unshares = unshare_mask
173 | std::views::transform([](
auto&& e){
return e.first; })
174 | std::views::filter([](
auto&& e){
return e != Unshare::ALL; })
175 | std::ranges::to<std::set<Unshare>>();
176 return set_unshares(Bits{}, unshares, value);
186 return set_unshares(Bits{}, unshares,
true);
196 return set_unshares(Pop(
read(m_path_file_binary)), unshares,
true);
206 return set_unshares(Pop(
read(m_path_file_binary)), unshares,
false);
214 [[nodiscard]]
inline bool contains(Unshare
const& unshare)
const noexcept
216 return_if(unshare == Unshare::NONE or unshare == Unshare::ALL,
false);
217 Bits bits =
read(m_path_file_binary).value_or(0);
218 auto it = unshare_mask.find(unshare);
219 return_if(it == unshare_mask.end(),
false);
220 return (bits & it->second) != 0;
229 return write(m_path_file_binary, Bits{});
238 return ::ns_reserved::ns_unshare::to_strings(Pop(
read(m_path_file_binary)));
Unshares(fs::path const &path_file_binary)
Constructs an Unshares manager for the given binary.
Value< void > set(std::set< Unshare > const &unshares)
Sets the specified unshare options (replaces existing)
Value< void > del(std::set< Unshare > const &unshares)
Removes unshare options from existing configuration.
bool contains(Unshare const &unshare) const noexcept
Checks if a specific unshare option is enabled.
Value< void > clear() noexcept
Clears all unshare options.
Value< std::set< std::string > > to_strings() const noexcept
Converts enabled unshare options to string representations.
Value< void > add(std::set< Unshare > const &unshares)
Adds unshare options to existing configuration.
Value< void > set_all(bool value)
Sets all unshare options to the specified value.
Custom enumeration class.
Enhanced error handling framework built on std::expected.
Unshare namespace options bitfield management in reserved space.
std::set< std::string > to_strings(Bits const &bits) noexcept
Creates a set of lowercase string unshare option representations.
Value< void > bit_set(Bits &bits, Unshare const &unshare, bool value) noexcept
Sets a bit unshare option with the target value.
Value< Bits > read(fs::path const &path_file_binary) noexcept
Read the Bits struct from the given binary.
Value< void > write(fs::path const &path_file_binary, Bits const &bits) noexcept
Write the Bits struct to the given binary.
Value< void > write(fs::path const &path_file_binary, uint64_t offset_begin, uint64_t offset_end, const char *data, uint64_t length) noexcept
Writes data to a file in binary format.
Value< std::streamsize > read(fs::path const &path_file_binary, uint64_t offset, char *data, uint64_t length) noexcept
Reads data from a file in binary format.
Enhanced expected type with integrated logging capabilities.