Skip to content

Commit 9192396

Browse files
committed
fix(hooks): cache status before pivot_root
Status file becomes inaccessible after pivot_root. Cache it beforehand and pass to create/start container hooks. Signed-off-by: ComixHe <ComixHe1895@outlook.com>
1 parent 7ceb4aa commit 9192396

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/linyaps_box/container.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,7 @@ void wait_create_runtime_result(const linyaps_box::config &config, linyaps_box::
13781378
}
13791379

13801380
void create_container_hooks(const linyaps_box::container &container,
1381+
const linyaps_box::container_status_t &status,
13811382
linyaps_box::unix_socket &socket)
13821383
{
13831384
const auto &config = container.get_config();
@@ -1387,9 +1388,8 @@ void create_container_hooks(const linyaps_box::container &container,
13871388

13881389
LINYAPS_BOX_DEBUG() << "Execute create container hooks";
13891390

1390-
auto state = container.status();
13911391
for (const auto &hook : config.hooks.create_container.value()) {
1392-
execute_hook(hook, state);
1392+
execute_hook(hook, status);
13931393
}
13941394

13951395
LINYAPS_BOX_DEBUG() << "Create container hooks executed";
@@ -1598,6 +1598,7 @@ void set_capabilities(const linyaps_box::config &config, int last_cap)
15981598
}
15991599

16001600
void start_container_hooks(const linyaps_box::container &container,
1601+
const linyaps_box::container_status_t &status,
16011602
linyaps_box::unix_socket &socket)
16021603
{
16031604
const auto &config = container.get_config();
@@ -1607,9 +1608,8 @@ void start_container_hooks(const linyaps_box::container &container,
16071608

16081609
LINYAPS_BOX_DEBUG() << "Execute start container hooks";
16091610

1610-
auto state = container.status();
16111611
for (const auto &hook : config.hooks.start_container.value()) {
1612-
execute_hook(hook, state);
1612+
execute_hook(hook, status);
16131613
}
16141614

16151615
LINYAPS_BOX_DEBUG() << "Start container hooks executed";
@@ -1789,7 +1789,9 @@ try {
17891789
configure_mounts(container, rootfs);
17901790
wait_prestart_hooks_result(config, socket);
17911791
wait_create_runtime_result(config, socket);
1792-
create_container_hooks(container, socket);
1792+
1793+
auto status = container.status();
1794+
create_container_hooks(container, status, socket);
17931795
// TODO: selinux label/apparmor profile
17941796
do_pivot_root(container, rootfs);
17951797

@@ -1809,7 +1811,7 @@ try {
18091811
linyaps_box::utils::sigprocmask(SIG_UNBLOCK, set, nullptr);
18101812
linyaps_box::utils::reset_signals(set);
18111813

1812-
start_container_hooks(container, socket);
1814+
start_container_hooks(container, status, socket);
18131815
execute_process(config);
18141816
} catch (const std::system_error &e) {
18151817
LINYAPS_BOX_ERR() << "clone failed: " << e.what();

src/linyaps_box/impl/status_directory.cpp

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

@@ -23,7 +23,9 @@ auto read_status(const std::filesystem::path &path) -> linyaps_box::container_st
2323
{
2424
std::ifstream istrm(path);
2525
if (istrm.fail()) {
26-
throw std::runtime_error("failed to open status file:" + path.string());
26+
throw std::system_error(errno,
27+
std::system_category(),
28+
"failed to open status file:" + path.string());
2729
}
2830

2931
istrm >> j;
@@ -71,6 +73,11 @@ void linyaps_box::impl::status_directory::remove(const std::string &id) const
7173
{
7274
auto file_path = this->path / (id + ".json");
7375
LINYAPS_BOX_DEBUG() << "Remove " << file_path;
76+
if (!std::filesystem::exists(file_path)) {
77+
LINYAPS_BOX_WARNING() << "Status file " << file_path << " does not exist";
78+
return;
79+
}
80+
7481
if (!std::filesystem::remove(file_path)) {
7582
LINYAPS_BOX_WARNING() << "Failed to remove " << file_path;
7683
}

src/linyaps_box/utils/ringbuffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ auto ring_buffer::create(std::size_t requested_capacity) -> ptr
6868
auto *data_base = static_cast<std::byte *>(addr) + meta_size;
6969
auto *rb = new (addr) ring_buffer(cap, data_base);
7070

71-
auto deleter = ring_buffer::deleter{ .total_size = total_vma };
71+
auto deleter = ring_buffer::deleter{ total_vma };
7272
return { rb, std::move(deleter) };
7373
}
7474

0 commit comments

Comments
 (0)