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
20 changes: 15 additions & 5 deletions libs/linglong/src/linglong/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@
#include "linglong/utils/runtime_config.h"
#include "linglong/utils/signal/signal_blocker.h"
#include "linglong/utils/temporary_directory.h"
#include "linglong/utils/terminal/terminal_guard.h"

Check warning on line 49 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/terminal/terminal_guard.h" not found.
#include "linglong/utils/unique_fd.h"

Check warning on line 50 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/unique_fd.h" not found.
#include "linglong/utils/xdp.h"

Check warning on line 51 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/xdp.h" not found.
#include "ocppi/cli/CommandFailedError.hpp"

Check warning on line 52 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "ocppi/runtime/ExecOption.hpp" not found.
#include "ocppi/runtime/ExecOption.hpp"

Check warning on line 53 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "ocppi/runtime/RunOption.hpp" not found.
#include "ocppi/runtime/RunOption.hpp" // IWYU pragma: keep

Check warning on line 54 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "ocppi/types/ContainerListItem.hpp" not found.
#include "ocppi/types/ContainerListItem.hpp"

#include <fmt/ranges.h>
Expand Down Expand Up @@ -1210,11 +1211,14 @@
auto result =
ociCLI.exec(id, "/run/linglong/container-init", reuseCommand, ioSetup->option);
if (!result) {
// TODO: modify ocppi to expose CommandFailedError::exitStatus()
// so that non-zero exit codes from reused commands are preserved
// instead of collapsed to EXIT_FAILURE.
LogE("failed to exec container: {}", LINGLONG_ERRV(result));
_exit(EXIT_FAILURE);
try {
std::rethrow_exception(result.error());
} catch (const ocppi::cli::CommandFailedError &e) {
_exit(e.exitStatus());
} catch (...) {
_exit(EXIT_FAILURE);
}
}

_exit(EXIT_SUCCESS);
Expand Down Expand Up @@ -1987,7 +1991,13 @@
if (!result) {
auto err = LINGLONG_ERRV(result);
this->printer.printErr(err);
return -1;
try {
std::rethrow_exception(result.error());
} catch (const ocppi::cli::CommandFailedError &e) {
return e.exitStatus();
} catch (...) {
return -1;
}
}
return 0;
}
Expand Down
36 changes: 36 additions & 0 deletions libs/linglong/tests/ll-tests/src/ocppi/cli/common_cli_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
* SPDX-License-Identifier: LGPL-3.0-or-later
*/

#include <gtest/gtest.h>

Check warning on line 7 in libs/linglong/tests/ll-tests/src/ocppi/cli/common_cli_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

#include "../../common/tempdir.h"
#include "ocppi/cli/CommandFailedError.hpp"

Check warning on line 10 in libs/linglong/tests/ll-tests/src/ocppi/cli/common_cli_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "ocppi/cli/crun/Crun.hpp" not found.
#include "ocppi/cli/crun/Crun.hpp"

Check warning on line 11 in libs/linglong/tests/ll-tests/src/ocppi/cli/common_cli_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "ocppi/runtime/ListOption.hpp" not found.
#include "ocppi/runtime/ExecOption.hpp"

Check warning on line 12 in libs/linglong/tests/ll-tests/src/ocppi/cli/common_cli_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "ocppi/types/ContainerListItem.hpp" not found.
#include "ocppi/runtime/ListOption.hpp"
#include "ocppi/types/ContainerListItem.hpp"

Expand Down Expand Up @@ -56,3 +58,37 @@
};
EXPECT_EQ(arguments, expectedArguments);
}

TEST(CommonCLITest, ExecPreservesCommandExitStatus)
{
TempDir tempDir("ocppi-common-cli-");

const auto runtimePath = tempDir.path() / "runtime";
{
std::ofstream runtime(runtimePath);
ASSERT_TRUE(runtime.is_open());
runtime << "#!/bin/sh\n"
"exit 42\n";
}
std::filesystem::permissions(runtimePath,
std::filesystem::perms::owner_read
| std::filesystem::perms::owner_write
| std::filesystem::perms::owner_exec,
std::filesystem::perm_options::replace);

auto cli = ocppi::cli::crun::Crun::New(runtimePath);
ASSERT_TRUE(cli.has_value());

ocppi::runtime::ExecOption option{};
auto result = cli.value()->exec(ocppi::runtime::ContainerID{ "test" }, "/bin/true", {}, option);
ASSERT_FALSE(result.has_value());

try {
std::rethrow_exception(result.error());
FAIL() << "expected CommandFailedError to be thrown";
} catch (const ocppi::cli::CommandFailedError &e) {
EXPECT_EQ(e.exitStatus(), 42);
} catch (...) {
FAIL() << "expected CommandFailedError";
}
}
26 changes: 25 additions & 1 deletion libs/ocppi/include/ocppi/cli/CommandFailedError.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <sys/wait.h>

#include <stdexcept>
#include <string>

Expand All @@ -11,16 +13,38 @@ class CommandFailedError : public std::runtime_error {

static constexpr const char *prefix = "run command failed: ";

[[nodiscard]]
static auto exitCode(int ret) noexcept -> int
{
if (WIFEXITED(ret)) {
return WEXITSTATUS(ret);
}
if (WIFSIGNALED(ret)) {
return WTERMSIG(ret) + 128;
}
return ret;
}

public:
explicit CommandFailedError(int ret, const std::string &command)
: std::runtime_error(prefix + command +
" retval=" + std::to_string(ret))
" retval=" + std::to_string(exitCode(ret)))
, ret_(exitCode(ret))
{
}
explicit CommandFailedError(int ret, const char *arg)
: CommandFailedError(ret, std::string(arg))
{
}

[[nodiscard]]
auto exitStatus() const noexcept -> int
{
return this->ret_;
}

private:
int ret_{ 0 };
};

}
Loading