Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions src/linyaps_box/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
#include "linyaps_box/utils/signal.h"
#include "linyaps_box/utils/symlink.h"

#include <linux/magic.h>

Check warning on line 26 in src/linyaps_box/container.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <linux/magic.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sys/mount.h>

Check warning on line 27 in src/linyaps_box/container.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <sys/mount.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sys/prctl.h>
#include <sys/signalfd.h>

Check warning on line 28 in src/linyaps_box/container.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <sys/signalfd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sys/statfs.h>

Check warning on line 29 in src/linyaps_box/container.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <sys/statfs.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sys/sysmacros.h>

Check warning on line 30 in src/linyaps_box/container.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <sys/sysmacros.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -1564,10 +1563,7 @@
}

// 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);
Expand All @@ -1586,26 +1582,18 @@
}

#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

#endif

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);
}
}

Expand Down Expand Up @@ -2477,7 +2465,7 @@
{
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
Expand Down
2 changes: 1 addition & 1 deletion src/linyaps_box/container_ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unix_socket> recv_socketpair;
if (option.proc.terminal && !option.console_socket) {
Expand Down
10 changes: 5 additions & 5 deletions src/linyaps_box/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ auto create_pty_pair() -> std::pair<terminal_master, terminal_slave>

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
Expand All @@ -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
Expand All @@ -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;
}

Expand Down
14 changes: 10 additions & 4 deletions src/linyaps_box/utils/ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@

namespace linyaps_box::utils {

using request_t = extract_t<decltype(get_n_params_type<1>(::ioctl))>;

template<typename... Args>
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>(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;
Expand Down
12 changes: 10 additions & 2 deletions src/linyaps_box/utils/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#pragma once

#include "utils.h"

#include <sys/prctl.h>

#include <cstdint>
Expand All @@ -27,11 +29,17 @@ struct WaitResult
auto waitpid(pid_t pid, int options) -> WaitResult;

template<typename... Args>
auto prctl(int option, Args... args) -> uint
[[nodiscard]] auto prctl(int option, Args... args) -> int
{
auto ret = ::prctl(option, std::forward<Args>(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;
Expand Down
40 changes: 35 additions & 5 deletions src/linyaps_box/utils/utils.h
Original file line number Diff line number Diff line change
@@ -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 <array>
#include <charconv>
#include <cstddef>
#include <cstdint>
#include <string>
#include <tuple>

#if defined(__GNUC__) || defined(__clang__)
Expand All @@ -23,18 +27,21 @@ struct type_entity
using type = T;
};

template<typename Entity>
using extract_t = typename Entity::type;

template<std::size_t N, typename R, typename... Args>
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<std::tuple_element_t<N - 1, std::tuple<Args...>>>{};
static_assert(N < sizeof...(Args), "index out of range");
return type_entity<std::tuple_element_t<N, std::tuple<Args...>>>{ };
}

template<std::size_t N, typename R, typename... Args>
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<std::tuple_element_t<N - 1, std::tuple<Args...>>>{};
static_assert(N < sizeof...(Args), "index out of range");
return type_entity<std::tuple_element_t<N, std::tuple<Args...>>>{ };
}

template<typename... T>
Expand All @@ -46,4 +53,27 @@ struct Overload : T...
template<typename... T>
Overload(T...) -> Overload<T...>;

template<typename T>
std::string stringify_arg(T arg)
{
if constexpr (std::is_convertible_v<T, std::string_view>) {
if (arg == nullptr) {
return "nullptr";
}

return std::string(arg);
} else if constexpr (std::is_pointer_v<T>) {
if (arg == nullptr) {
return "nullptr";
}

std::array<char, 20> buf{ };
auto [ptr, ec] =
std::to_chars(buf.begin(), buf.end(), reinterpret_cast<uintptr_t>(arg), 16);
return "0x" + std::string(buf.begin(), ptr);
} else {
return std::to_string(arg);
}
}

} // namespace linyaps_box::utils
Loading