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
18 changes: 15 additions & 3 deletions src/linyaps_box/container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1594,8 +1594,13 @@ void processing_extensions(const linyaps_box::container &container)
LINYAPS_BOX_DEBUG() << "Processing container extensions";

// ext_ns_last_pid
if (auto it = config.annotations->find("cn.org.linyaps.runtime.ns_last_pid");
it != config.annotations->end()) {
// This file may not exist if the kernel config CONFIG_CHECKPOINT_RESTORE is not enabled
// and this feature originally was used for userspace checkpoint/restore
// we use this feature for avoiding two process has the same pid.
// e.g some application will register a tray through dbus and use the pid as the part of
// dbus object path, if two process has the same pid, the dbus object path will conflict
auto it = config.annotations->find("cn.org.linyaps.runtime.ns_last_pid");
while (it != config.annotations->end()) {
LINYAPS_BOX_DEBUG() << "Processing ns_last_pid extension: " << it->second;

// Validate input is a valid pid_t number
Expand All @@ -1613,7 +1618,13 @@ void processing_extensions(const linyaps_box::container &container)
throw std::runtime_error("parse ns_last_pid " + it->second + " failed: " + e.what());
}

std::ofstream ofs("/proc/sys/kernel/ns_last_pid");
// ignore ns_last_pid if the file does not exist
auto ns_last_pid = std::filesystem::path{ "/proc/sys/kernel/ns_last_pid" };
if (!std::filesystem::exists(ns_last_pid)) {
break;
}

std::ofstream ofs(ns_last_pid);
if (!ofs) {
throw std::system_error(errno,
std::generic_category(),
Expand All @@ -1628,6 +1639,7 @@ void processing_extensions(const linyaps_box::container &container)
}

LINYAPS_BOX_DEBUG() << "Successfully set ns_last_pid to " << it->second;
break;
}

LINYAPS_BOX_DEBUG() << "Container extensions processing completed";
Expand Down
5 changes: 3 additions & 2 deletions tests/ll-box-ut/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma GCC diagnostic pop
#endif

TEST(LINYAPS_BOX, Placeholder1) {
EXPECT_EQ(1, 1);
TEST(LINYAPS_BOX, Placeholder1)
{
EXPECT_EQ(1, 1);
}
83 changes: 83 additions & 0 deletions tools/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2022-2025 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: LGPL-3.0-or-later

# tools/format.sh
# Format all C/C++ source files in the project using clang-format.
#
# Usage:
# ./tools/format.sh # Use system default clang-format
# ./tools/format.sh clang-format-17 # Use a specific clang-format binary
#
# The script automatically skips common build and third-party directories.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
CLANG_FORMAT="${1:-clang-format}"

# Check clang-format availability
if ! command -v "${CLANG_FORMAT}" >/dev/null 2>&1; then
echo "Error: ${CLANG_FORMAT} not found. Please install clang-format." >&2
exit 1
fi

# --- Project root validation ---
# Criteria for identifying a valid project root:
# 1. Has CMakeLists.txt, or
# 2. Has .clang-format, or
# 3. Contains app/, src/, or tests/ directories

VALID_ROOT=false
if [[ -f "${ROOT_DIR}/CMakeLists.txt" ]]; then
VALID_ROOT=true
elif [[ -f "${ROOT_DIR}/.clang-format" ]]; then
VALID_ROOT=true
else
for d in app src tests; do
if [ -d "${ROOT_DIR}/${d}" ]; then
VALID_ROOT=true
break
fi
done
fi

if [[ ${VALID_ROOT} != true ]]; then
echo "Error: This script must be run inside a valid C++ project root."
echo "Expected to find one of the following in ${ROOT_DIR}:"
echo " - CMakeLists.txt"
echo " - .clang-format or _clang-format"
echo " - app/, src/, or tests/ directories"
exit 1
fi
# --- End project root validation ---

CLANG_FORMAT_VERSION=$("${CLANG_FORMAT}" --version | head -n 1 | cut -d ' ' -f 3)
echo "Using clang-format: ${CLANG_FORMAT_VERSION}"
echo "Project root: ${ROOT_DIR}"

# Only search for .cpp and .h files under app, src, and tests directories
dirs=()
for d in app src tests; do
if [[ -d "${ROOT_DIR}/${d}" ]]; then
dirs+=("${ROOT_DIR}/${d}")
fi
done

if [[ ${#dirs[@]} -eq 0 ]]; then
echo "Warning: No app/, src/, or tests/ directories found under ${ROOT_DIR}."
exit 0
fi

find "${dirs[@]}" \
\( -name "*.cpp" -o -name "*.h" \) \
-type f -print |
while read -r file; do
echo "Formatting: ${file}"
"${CLANG_FORMAT}" -i "${file}"
done

echo "Formatting completed."