diff --git a/libs/linglong/src/linglong/package_manager/package_manager.cpp b/libs/linglong/src/linglong/package_manager/package_manager.cpp index 635bf254c..6fe430b59 100644 --- a/libs/linglong/src/linglong/package_manager/package_manager.cpp +++ b/libs/linglong/src/linglong/package_manager/package_manager.cpp @@ -23,6 +23,7 @@ #include "linglong/repo/config.h" #include "linglong/repo/ostree_repo.h" #include "linglong/runtime/run_context.h" +#include "linglong/utils/bash_quote.h" #include "linglong/utils/command/env.h" #include "linglong/utils/error/error.h" #include "linglong/utils/finally/finally.h" @@ -2402,15 +2403,22 @@ utils::error::Result PackageManager::generateCache(const package::Referenc if (!currentArch) { return LINGLONG_ERR(currentArch); } - const auto ldGenerateCmd = "/sbin/ldconfig -X -C " + appCacheDest + "/ld.so.cache"; + auto ldGenerateCmd = + std::vector{ "/sbin/ldconfig", "-X", "-C", appCacheDest + "/ld.so.cache" }; #ifdef LINGLONG_FONT_CACHE_GENERATOR // Usage: font-cache-generator [cacheRoot] [id] - const std::string fontGenerateCmd = - fontGenerator + " " + appCacheDest + " " + ref.id.toStdString(); - process.args = std::vector{ "bash", "-c", ldGenerateCmd + ";" + fontGenerateCmd }; + const std::string fontGenerateCmd = utils::quoteBashArg(fontGenerator) + " " + + utils::quoteBashArg(appCacheDest) + " " + utils::quoteBashArg(ref.id.toStdString()); + auto ldGenerateCmdstr; + for (const auto &c : ldGenerateCmd) { + ldGenerateCmdstr.append(utils::quoteBashArg(c)); + ldGenerateCmdstr.append(" "); + } + process.args = + std::vector{ "bash", "-c", ldGenerateCmdstr + ";" + fontGenerateCmd }; #endif - process.args = std::vector{ ldGenerateCmd }; + process.args = std::move(ldGenerateCmd); // Note: XDG_RUNTIME_DIR is not set in PM, the ll-box will finally fallback to /run/ll-box. // But PM has no write permission in that place, so we should specific the root path. diff --git a/libs/linglong/src/linglong/runtime/container.cpp b/libs/linglong/src/linglong/runtime/container.cpp index c24d4926b..296de5579 100644 --- a/libs/linglong/src/linglong/runtime/container.cpp +++ b/libs/linglong/src/linglong/runtime/container.cpp @@ -7,6 +7,7 @@ #include "linglong/runtime/container.h" #include "configure.h" +#include "linglong/utils/bash_quote.h" #include "linglong/utils/finally/finally.h" #include "ocppi/runtime/RunOption.hpp" #include "ocppi/runtime/config/types/Generators.hpp" @@ -207,23 +208,8 @@ utils::error::Result Container::run(const ocppi::runtime::config::types::P // environment variables ofs << "exec "; - // quote the argument to avoid the space in the argument and use single quote to avoid the - // shell to expand the argument - // example: - // arg: "let's go" - // quoteArg: "'let'\''s go'" - auto quoteArg = [](std::string arg) { - const std::string quotePrefix = "'\\"; - for (auto it = arg.begin(); it != arg.end(); it++) { - if (*it == '\'') { - it = arg.insert(it, quotePrefix.cbegin(), quotePrefix.cend()); - it = arg.insert(it + quotePrefix.size() + 1, 1, '\''); - } - } - return "'" + arg + "'"; - }; for (auto arg : originalArgs) { - ofs << quoteArg(arg) << " "; + ofs << utils::quoteBashArg(arg) << " "; } } diff --git a/libs/utils/src/linglong/utils/bash_quote.h b/libs/utils/src/linglong/utils/bash_quote.h new file mode 100644 index 000000000..8c0a605b1 --- /dev/null +++ b/libs/utils/src/linglong/utils/bash_quote.h @@ -0,0 +1,28 @@ +// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: LGPL-3.0-or-later + +#pragma once + +#include + +namespace linglong::utils { + +// quote the bash argument to avoid the space in the argument +// and use single quote to avoid the shell to expand the argument +// example: +// arg: "let's go" +// quoted arg: "'let'\''s go'" +static inline std::string quoteBashArg(std::string arg) noexcept +{ + const std::string quotePrefix = "'\\"; + for (auto it = arg.begin(); it != arg.end(); it++) { + if (*it == '\'') { + it = arg.insert(it, quotePrefix.cbegin(), quotePrefix.cend()); + it = arg.insert(it + quotePrefix.size() + 1, 1, '\''); + } + } + return "'" + arg + "'"; +} + +} // namespace linglong::utils \ No newline at end of file