diff --git a/src/linyaps_box/container.cpp b/src/linyaps_box/container.cpp index 683a622..53c56e3 100644 --- a/src/linyaps_box/container.cpp +++ b/src/linyaps_box/container.cpp @@ -25,7 +25,6 @@ #include #include -#include #include #include #include @@ -1564,10 +1563,7 @@ void set_capabilities(const linyaps_box::config &config, int last_cap) } // keep current capabilities, we need these caps on later - ret = prctl(PR_SET_KEEPCAPS, 1); - if (ret < 0) { - throw std::system_error(errno, std::system_category(), "keep current capabilities"); - } + std::ignore = linyaps_box::utils::prctl(PR_SET_KEEPCAPS, 1); const auto &process = config.process; ret = setresuid(process.user.uid, process.user.uid, process.user.uid); @@ -1586,16 +1582,10 @@ void set_capabilities(const linyaps_box::config &config, int last_cap) } #ifdef PR_CAP_AMBIENT - ret = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0L, 0L, 0L); - if (ret < 0) { - throw std::system_error(errno, std::system_category(), "cap_ambient_clear_all"); - } + std::ignore = linyaps_box::utils::prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0L, 0L, 0L); std::for_each(capabilities.ambient.cbegin(), capabilities.ambient.cend(), [](cap_value_t cap) { - auto ret = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0L, 0L); - if (ret < 0) { - throw std::system_error(errno, std::system_category(), "cap_ambient_raise"); - } + std::ignore = linyaps_box::utils::prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0L, 0L); }); #endif @@ -1603,9 +1593,7 @@ void set_capabilities(const linyaps_box::config &config, int last_cap) if (config.process.no_new_privileges) { LINYAPS_BOX_DEBUG() << "Set no new privileges"; - if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { - throw std::system_error(errno, std::system_category(), "prctl"); - } + std::ignore = linyaps_box::utils::prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); } } @@ -2477,7 +2465,7 @@ int linyaps_box::container::run(run_container_options_t options) { int container_process_exit_code{ -1 }; - utils::prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0); + std::ignore = utils::prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0); try { // TODO: there are some thing that should be done before starting the container process diff --git a/src/linyaps_box/container_ref.cpp b/src/linyaps_box/container_ref.cpp index 2ad17ab..08119c1 100644 --- a/src/linyaps_box/container_ref.cpp +++ b/src/linyaps_box/container_ref.cpp @@ -48,7 +48,7 @@ auto linyaps_box::container_ref::exec(exec_container_option option) -> int auto target = std::to_string(this->status().PID); // TODO: support detach later - utils::prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0); + std::ignore = utils::prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0); std::optional recv_socketpair; if (option.proc.terminal && !option.console_socket) { diff --git a/src/linyaps_box/terminal.cpp b/src/linyaps_box/terminal.cpp index eb91b3f..d009a45 100644 --- a/src/linyaps_box/terminal.cpp +++ b/src/linyaps_box/terminal.cpp @@ -29,7 +29,7 @@ auto create_pty_pair() -> std::pair auto terminal_master::resize(struct winsize size) -> void { - utils::ioctl(master_, TIOCSWINSZ, &size); + std::ignore = utils::ioctl(master_, TIOCSWINSZ, &size); } terminal_slave::terminal_slave(terminal_slave &&other) noexcept @@ -56,17 +56,17 @@ auto terminal_slave::setup_stdio() -> void slave_.duplicate_to(STDIN_FILENO, 0); slave_.duplicate_to(STDOUT_FILENO, 0); slave_.duplicate_to(STDERR_FILENO, 0); - utils::ioctl(slave_, TIOCSCTTY, 0); + std::ignore = utils::ioctl(slave_, TIOCSCTTY, 0); } auto terminal_slave::set_size(struct winsize size) -> void { if (size.ws_col == 0 || size.ws_row == 0) { auto default_tty = utils::open("/dev/tty", O_RDWR | O_CLOEXEC); - utils::ioctl(default_tty, TIOCGWINSZ, &size); + std::ignore = utils::ioctl(default_tty, TIOCGWINSZ, &size); } - utils::ioctl(slave_, TIOCSWINSZ, &size); + std::ignore = utils::ioctl(slave_, TIOCSWINSZ, &size); } auto terminal_slave::set_raw() -> void @@ -91,7 +91,7 @@ auto terminal_slave::set_raw() -> void auto terminal_slave::get_size() -> struct winsize { struct winsize size{ }; - utils::ioctl(slave_, TIOCGWINSZ, &size); + std::ignore = utils::ioctl(slave_, TIOCGWINSZ, &size); return size; } diff --git a/src/linyaps_box/utils/ioctl.h b/src/linyaps_box/utils/ioctl.h index e522f5b..870fa7c 100644 --- a/src/linyaps_box/utils/ioctl.h +++ b/src/linyaps_box/utils/ioctl.h @@ -11,14 +11,20 @@ namespace linyaps_box::utils { +using request_t = extract_t(::ioctl))>; + template -auto ioctl(const file_descriptor &fd, - decltype(get_n_params_type<1>(ioctl))::type request, - Args... args) -> unsigned int +[[nodiscard]] auto ioctl(const file_descriptor &fd, request_t request, Args... args) -> int { auto ret = ::ioctl(fd.get(), request, std::forward(args)...); if (ret != 0) { - throw std::system_error(errno, std::system_category(), "ioctl"); + auto msg = + "ioctl fd " + std::to_string(fd.get()) + ", request op " + std::to_string(request); + bool first = true; + ((msg += (first ? "" : ", ") + stringify_arg(args), first = false), ...); + msg.push_back(']'); + + throw std::system_error(errno, std::system_category(), msg); } return ret; diff --git a/src/linyaps_box/utils/process.h b/src/linyaps_box/utils/process.h index 3d939fb..14febc6 100644 --- a/src/linyaps_box/utils/process.h +++ b/src/linyaps_box/utils/process.h @@ -4,6 +4,8 @@ #pragma once +#include "utils.h" + #include #include @@ -27,11 +29,17 @@ struct WaitResult auto waitpid(pid_t pid, int options) -> WaitResult; template -auto prctl(int option, Args... args) -> uint +[[nodiscard]] auto prctl(int option, Args... args) -> int { auto ret = ::prctl(option, std::forward(args)...); if (ret < 0) { - throw std::system_error(errno, std::system_category(), "prctl"); + auto msg = "prctl op " + std::to_string(option) + " with args: ["; + + bool first = true; + ((msg += (first ? "" : ", ") + stringify_arg(args), first = false), ...); + msg.push_back(']'); + + throw std::system_error(errno, std::system_category(), msg); } return ret; diff --git a/src/linyaps_box/utils/utils.h b/src/linyaps_box/utils/utils.h index a4777e5..87457cf 100644 --- a/src/linyaps_box/utils/utils.h +++ b/src/linyaps_box/utils/utils.h @@ -1,10 +1,14 @@ -// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: LGPL-3.0-or-later #pragma once +#include +#include #include +#include +#include #include #if defined(__GNUC__) || defined(__clang__) @@ -23,18 +27,21 @@ struct type_entity using type = T; }; +template +using extract_t = typename Entity::type; + template constexpr auto get_n_params_type([[maybe_unused]] R (*ptr)(Args...)) { - static_assert(N >= 1 && N <= sizeof...(Args), "index out of range"); - return type_entity>>{}; + static_assert(N < sizeof...(Args), "index out of range"); + return type_entity>>{ }; } template constexpr auto get_n_params_type([[maybe_unused]] R (*ptr)(Args..., ...)) { - static_assert(N >= 1 && N <= sizeof...(Args), "index out of range"); - return type_entity>>{}; + static_assert(N < sizeof...(Args), "index out of range"); + return type_entity>>{ }; } template @@ -46,4 +53,27 @@ struct Overload : T... template Overload(T...) -> Overload; +template +std::string stringify_arg(T arg) +{ + if constexpr (std::is_convertible_v) { + if (arg == nullptr) { + return "nullptr"; + } + + return std::string(arg); + } else if constexpr (std::is_pointer_v) { + if (arg == nullptr) { + return "nullptr"; + } + + std::array buf{ }; + auto [ptr, ec] = + std::to_chars(buf.begin(), buf.end(), reinterpret_cast(arg), 16); + return "0x" + std::string(buf.begin(), ptr); + } else { + return std::to_string(arg); + } +} + } // namespace linyaps_box::utils