Skip to content

Commit 93935da

Browse files
committed
refactor(config): restructure OCI config parser and mount subsystem
- Rename `config` → `Config`, move to PascalCase per C++ conventions - Split `mount_t::flags` into `vfs_flags`, `propagation_flags`, `rec_attr` - Move `id_mapping_t` to `Config` scope for reuse across mount and linux - Extract `resources_t` under `linux_t` matching OCI spec hierarchy - Make `root`, `process`, `user` optional per spec (user defaults to 0/0) - Fix `netDevices` type from `vector` to `unordered_map` per spec - Fix hooks.env from `unordered_map` to `vector<string>` with KEY=VALUE validation per POSIX environ semantics Extract: - `config/kernel_fallbacks.h` — kernel constant compat for older headers - `config/mount_options.h` — mount flag lookup tables and dump() - `utils/enum_traits.h` — generic enum_table + SFINAE bitmask operators Seccomp: - Replace string fields with typed action_t/arch_t/flag_t enums - Validate errnoRet only valid with ERRNO/TRACE actions - Validate NOTIFY requires listenerPath - Decouple arch_t values from libseccomp constants for 2.3.3 compat Performance: - close_other_fds uses close_range() syscall instead of per-fd loop - set_capabilities bounding set uses vector<bool> mask for O(n) drop - get_last_cap uses static local for one-time /proc read Safety: - Remove __builtin_unreachable() from to_string_view (UB on unknown) - Add null check for cap_init() before unique_ptr construction - Explicit long args for prctl() to avoid sign-extension issues - Add overflow validation in parse_range_list - UNLIKELY annotations on all validation/error branches Removed: - All to_json functions (one-way parse only) - Old per-enum bitwise operator overloads (replaced by enum_traits) - Unused includes: <set>, <dirent.h>, platform.h Signed-off-by: Yuming He <ComixHe1895@outlook.com>
1 parent e59f4c5 commit 93935da

17 files changed

Lines changed: 1942 additions & 2233 deletions

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,11 @@ find_package(PkgConfig REQUIRED)
251251
if(linyaps-box_ENABLE_SECCOMP)
252252
pkg_check_modules(libseccomp REQUIRED IMPORTED_TARGET libseccomp>=2.3.3)
253253
list(APPEND linyaps-box_LIBRARY_LINK_LIBRARIES PUBLIC PkgConfig::libseccomp)
254+
#TODO: detect seccomp_arch_resolve_name and use if available
254255
endif()
255256

256257
if(linyaps-box_ENABLE_CAP)
258+
#TODO: remove libcap and implement it by ourself
257259
pkg_check_modules(libcap REQUIRED IMPORTED_TARGET libcap>=2.25)
258260
list(APPEND linyaps-box_LIBRARY_LINK_LIBRARIES PUBLIC PkgConfig::libcap)
259261
endif()
@@ -341,6 +343,10 @@ endif()
341343
if(linyaps-box_ENABLE_SECCOMP)
342344
target_compile_definitions("${linyaps-box_LIBRARY}"
343345
PUBLIC LINYAPS_BOX_ENABLE_SECCOMP)
346+
if(linyaps-box_HAVE_SECCOMP_ARCH_RESOLVE_NAME)
347+
target_compile_definitions("${linyaps-box_LIBRARY}"
348+
PUBLIC SECCOMP_ARCH_RESOLVE_NAME=1)
349+
endif()
344350
endif()
345351

346352
if(linyaps-box_ENABLE_CAP)

src/linyaps_box/app.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

@@ -44,22 +44,22 @@ try {
4444
return opts.global.return_code;
4545
}
4646

47-
return std::visit(utils::Overload{ [](const command::list_options &options) {
47+
return std::visit(utils::Overload{ [](const command::list_options &options) -> int {
4848
command::list(options);
4949
return 0;
5050
},
5151
[](const command::exec_options &options) -> int {
5252
return command::exec(options);
5353
},
54-
[](const command::kill_options &options) {
54+
[](const command::kill_options &options) -> int {
5555
command::kill(options);
5656
return 0;
5757
},
58-
[](const command::run_options &options) {
58+
[](const command::run_options &options) -> int {
5959
return command::run(options);
6060
},
61-
[](const std::monostate &) {
62-
return 0;
61+
[](const std::monostate &) -> int {
62+
__builtin_unreachable();
6363
} },
6464
opts.subcommand_opt);
6565
} catch (const std::exception &e) {

src/linyaps_box/command/exec.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "linyaps_box/runtime.h"
88
#include "linyaps_box/status_directory_manager.h"
99

10+
#ifdef LINYAPS_BOX_ENABLE_CAP
11+
# include <sys/capability.h>
12+
#endif
13+
1014
auto linyaps_box::command::exec(const struct exec_options &options) -> int
1115
{
1216
status_directory_manager mgr(options.global_.get().root);
@@ -37,30 +41,21 @@ auto linyaps_box::command::exec(const struct exec_options &options) -> int
3741
option.proc.capabilities.emplace();
3842
}
3943

40-
auto transform_cap = [&caps](std::optional<std::vector<cap_value_t>> &cap_set) {
41-
if (!cap_set) {
42-
cap_set.emplace();
44+
std::vector<cap_value_t> parsed;
45+
parsed.reserve(caps.size());
46+
for (const auto &name : caps) {
47+
cap_value_t val{ };
48+
if (cap_from_name(name.c_str(), &val) < 0) {
49+
throw std::system_error(errno, std::system_category(), "cap_from_name");
4350
}
4451

45-
cap_set->reserve(caps.size());
46-
std::transform(
47-
caps.cbegin(),
48-
caps.cend(),
49-
std::back_inserter(*cap_set),
50-
[](const std::string &cap) {
51-
cap_value_t val{ 0 };
52-
if (cap_from_name(cap.c_str(), &val) < 0) {
53-
throw std::system_error(errno, std::system_category(), "cap_from_name");
54-
}
55-
56-
return val;
57-
});
58-
};
52+
parsed.push_back(val);
53+
}
5954

60-
transform_cap(option.proc.capabilities->effective);
61-
transform_cap(option.proc.capabilities->ambient);
62-
transform_cap(option.proc.capabilities->bounding);
63-
transform_cap(option.proc.capabilities->permitted);
55+
option.proc.capabilities->effective = parsed;
56+
option.proc.capabilities->ambient = parsed;
57+
option.proc.capabilities->bounding = parsed;
58+
option.proc.capabilities->permitted = parsed;
6459
}
6560
#endif
6661

src/linyaps_box/command/options.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ linyaps_box::command::options linyaps_box::command::parse(int argc, char *argv[]
1919
app.set_version_flag("-v,--version", [] {
2020
std::stringstream ss;
2121
ss << "ll-box version " << LINYAPS_BOX_VERSION << "\n";
22-
ss << "spec " << linyaps_box::config::oci_version;
22+
ss << "spec " << linyaps_box::Config::oci_version;
2323
return ss.str();
2424
});
2525
app.require_subcommand();
@@ -155,7 +155,7 @@ linyaps_box::command::options linyaps_box::command::parse(int argc, char *argv[]
155155
}
156156

157157
if (cmd_list->parsed()) {
158-
options.subcommand_opt = std::move(list_opt);
158+
options.subcommand_opt = list_opt;
159159
} else if (cmd_run->parsed()) {
160160
options.subcommand_opt = std::move(run_opt);
161161
} else if (cmd_exec->parsed()) {

src/linyaps_box/command/run.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "linyaps_box/runtime.h"
88
#include "linyaps_box/status_directory_manager.h"
9+
#include "linyaps_box/utils/utils.h"
910

1011
auto linyaps_box::command::run(const struct run_options &options) -> int
1112
{
@@ -20,7 +21,12 @@ auto linyaps_box::command::run(const struct run_options &options) -> int
2021
run_container_options_t run_options;
2122
run_options.preserve_fds = options.preserve_fds;
2223

23-
if (container.get_config().process.terminal && options.console_socket) {
24+
const auto &cfg = container.get_config();
25+
if (UNLIKELY(!cfg.process || !cfg.root)) {
26+
throw std::runtime_error("'process' and 'root' are required for run a container");
27+
}
28+
29+
if (container.get_config().process->terminal && options.console_socket) {
2430
run_options.console_socket = unix_socket::connect(options.console_socket.value());
2531
}
2632

0 commit comments

Comments
 (0)