Skip to content
Open
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
34 changes: 34 additions & 0 deletions score/launch_manager/src/daemon/src/osal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************
load("@rules_cc//cc:defs.bzl", "cc_library")
load("//tests/utils/bazel:unit_test.bzl", "lm_cc_test")

cc_library(
name = "return_types",
Expand All @@ -33,6 +34,38 @@ cc_library(
deps = [":return_types"],
)

cc_library(
name = "wait_for_file",
srcs = ["details/posix/wait_for_file.cpp"],
hdrs = [
"return_types.hpp",
"wait_for_file.hpp",
],
include_prefix = "score/mw/launch_manager/osal",
strip_include_prefix = "/score/launch_manager/src/daemon/src/osal",
visibility = ["//score:__subpackages__"],
deps = [
":return_types",
"//score/launch_manager/src/daemon/src/configuration:component_config",
"@score_baselibs//score/language/futurecpp",
"@score_baselibs//score/os:errno",
"@score_baselibs//score/os:stat",
],
)

lm_cc_test(
name = "wait_for_file_UT",
srcs = ["details/posix/wait_for_file_UT.cpp"],
deps = [
":wait_for_file",
"//score/launch_manager/src/daemon/src/configuration:component_config",
"@googletest//:gtest_main",
"@score_baselibs//score/os:errno",
"@score_baselibs//score/os:stat",
"@score_baselibs//score/os/mocklib:stat_mock",
],
)

cc_library(
name = "sys_exit",
srcs = ["details/posix/sys_exit.cpp"],
Expand Down Expand Up @@ -127,5 +160,6 @@ cc_library(
":set_affinity",
":set_groups",
":sys_exit",
":wait_for_file",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/********************************************************************************
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

#include <algorithm>
#include <chrono>
#include <string_view>
#include <thread>

#include "score/os/errno.h"
#include "score/os/stat.h"

#include "score/mw/launch_manager/osal/wait_for_file.hpp"

namespace score::mw::lifecycle::internal::osal
{

OsalReturnType wait_for_file(
std::string_view path,
configuration::FileExistenceState condition,
std::chrono::milliseconds timeout,
std::chrono::milliseconds poll_interval,
const score::os::Stat& stat_os) noexcept
{
// note: QNX has a wait_for API, however in the future a stop_token should
// be used, when using the API call we wouldn't be able to check the
// stop_token between stat calls.

// required null terminator
if (path.empty() || (path.data()[path.size()] != '\0'))
{
return OsalReturnType::kFail;
}

const bool wait_for_existence = (condition == configuration::FileExistenceState::Exists);
const auto deadline = std::chrono::steady_clock::now() + timeout;

while (true)
{
score::os::StatBuffer info{};

const auto result = stat_os.stat(path.data(), info);
if (result.has_value())
{
if (wait_for_existence)
{
return OsalReturnType::kSuccess;
}
}
// treat file or dir not existing as the same
else if (
(result.error() == score::os::Error::Code::kNoSuchFileOrDirectory) ||
(result.error() == score::os::Error::Code::kNotADirectory))
{
if (!wait_for_existence)
{
return OsalReturnType::kSuccess;
}
}
else if (result.error() == score::os::Error::Code::kOperationWasInterruptedBySignal)
{
// retry
}
else
{
return OsalReturnType::kFail;
}

const auto now = std::chrono::steady_clock::now();
if (now >= deadline)
{
return OsalReturnType::kTimeout;
}

// never sleep past the deadline
const auto remaining = deadline - now;
std::this_thread::sleep_for(std::min<std::chrono::steady_clock::duration>(poll_interval, remaining));
}
}

} // namespace score::mw::lifecycle::internal::osal
Loading
Loading