From b09b3e5cae5e8f25a29e256b6265f0e50b6280b1 Mon Sep 17 00:00:00 2001 From: Junghyun Yeon Date: Tue, 26 Jan 2021 18:32:29 +0900 Subject: [PATCH] Fix pkg_initdb tool to check backend execution result Now pkg_initdb tool will check execution result of installer backends and create /tmp/.preload_install_error. It may cause failure of entire mic build. Change-Id: I6bf99350853e5585df96f567b77e0e9a1f7c42a0 Signed-off-by: Junghyun Yeon --- src/common/utils/subprocess.cc | 9 ++++++--- src/common/utils/subprocess.h | 2 +- src/pkg_initdb/init_pkg_db.cc | 28 +++++++++++++++++++--------- src/pkg_initdb/init_pkg_db.h | 4 ++-- src/pkg_initdb/pkg_initdb.cc | 11 ++++++++++- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/common/utils/subprocess.cc b/src/common/utils/subprocess.cc index 3c0b7a5..d5ca6d2 100644 --- a/src/common/utils/subprocess.cc +++ b/src/common/utils/subprocess.cc @@ -53,14 +53,17 @@ bool Subprocess::RunWithArgs(const std::vector& args) { } } -int Subprocess::Wait() { +bool Subprocess::Wait() { if (!started_) { LOG(WARNING) << "Process is not started. Cannot wait"; - return -1; + return false; } int status; waitpid(pid_, &status, 0); - return status; + if (WIFEXITED(status) == 0 || WEXITSTATUS(status) != 0) + return false; + + return true; } void Subprocess::Kill() { diff --git a/src/common/utils/subprocess.h b/src/common/utils/subprocess.h index 42527ee..8807a29 100644 --- a/src/common/utils/subprocess.h +++ b/src/common/utils/subprocess.h @@ -22,7 +22,7 @@ class Subprocess { bool RunWithArgs( const std::vector& args = std::vector()); - int Wait(); + bool Wait(); void set_uid(int uid) { uid_ = uid; diff --git a/src/pkg_initdb/init_pkg_db.cc b/src/pkg_initdb/init_pkg_db.cc index 7023902..26b363b 100644 --- a/src/pkg_initdb/init_pkg_db.cc +++ b/src/pkg_initdb/init_pkg_db.cc @@ -64,7 +64,7 @@ bool InitPkgDB::Init(int argc, char *argv[]) { return option_checker_.Init(argc, argv); } -void InitPkgDB::RunBackend(const std::string& pkgid, +bool InitPkgDB::RunBackend(const std::string& pkgid, const std::string& type, bool preload) { bf::path backend_path(kBackendDirectoryPath); backend_path /= type; @@ -73,16 +73,21 @@ void InitPkgDB::RunBackend(const std::string& pkgid, if (preload) { if (option_checker_.GetUid() != kRootUserUid) { std::cerr << "Preload request for non-root user" << std::endl; - return; + return false; } params.emplace_back("--preload"); } backend.RunWithArgs(params); - backend.Wait(); + if (!backend.Wait()) { + std::cerr << "Failed to run backend" << std::endl; + return false; + } + + return true; } -void InitPkgDB::LoadDirectory(const std::string& path, +bool InitPkgDB::LoadDirectory(const std::string& path, bool preload) { ManifestLoader manifest_loader(path); std::list manifest_list = manifest_loader.LoadManifest(); @@ -90,9 +95,11 @@ void InitPkgDB::LoadDirectory(const std::string& path, for (auto &manifest_info : manifest_list) { std::cerr << "Manifest : " << std::get<0>(manifest_info) << (option_checker_.IsPartialRW() ? " (rw-only)":" (all)") << std::endl; - RunBackend(std::get<1>(manifest_info), std::get<2>(manifest_info), - preload); + if (!RunBackend(std::get<1>(manifest_info), + std::get<2>(manifest_info), preload)) + return false; } + return true; } bool InitPkgDB::Run() { @@ -101,18 +108,21 @@ bool InitPkgDB::Run() { if (option_checker_.IsGlobal()) { if (!option_checker_.IsRWOnly()) - LoadDirectory(tzplatform_getenv(TZ_SYS_RO_PACKAGES), true); + if (!LoadDirectory(tzplatform_getenv(TZ_SYS_RO_PACKAGES), true)) + return false; if (option_checker_.IsROOnly()) return true; - LoadDirectory(tzplatform_getenv(TZ_SYS_RW_PACKAGES), false); + if (!LoadDirectory(tzplatform_getenv(TZ_SYS_RW_PACKAGES), false)) + return false; } else { // Specified user location tzplatform_set_user(option_checker_.GetUid()); std::string user_dir(tzplatform_getenv(TZ_USER_PACKAGES)); tzplatform_reset_user(); - LoadDirectory(user_dir, false); + if (!LoadDirectory(user_dir, false)) + return false; } return true; diff --git a/src/pkg_initdb/init_pkg_db.h b/src/pkg_initdb/init_pkg_db.h index f41e99b..79878ae 100644 --- a/src/pkg_initdb/init_pkg_db.h +++ b/src/pkg_initdb/init_pkg_db.h @@ -17,8 +17,8 @@ class InitPkgDB { private: bool HandleDatabase(); - void LoadDirectory(const std::string& path, bool preload); - void RunBackend(const std::string& pkgid, + bool LoadDirectory(const std::string& path, bool preload); + bool RunBackend(const std::string& pkgid, const std::string& type, bool preload); diff --git a/src/pkg_initdb/pkg_initdb.cc b/src/pkg_initdb/pkg_initdb.cc index 680c2b9..a7b7ab5 100644 --- a/src/pkg_initdb/pkg_initdb.cc +++ b/src/pkg_initdb/pkg_initdb.cc @@ -5,6 +5,7 @@ #include #include +#include #include #include "pkg_initdb/init_pkg_db.h" @@ -13,6 +14,12 @@ namespace { const uid_t kRootUserUid = 0; +void AddErrorFlag() { + std::ofstream ofs("/tmp/.preload_install_error", std::ios::app); + ofs << "pkg_initdb error\n"; + ofs.close(); +} + } // namespace int main(int argc, char *argv[]) { @@ -25,8 +32,10 @@ int main(int argc, char *argv[]) { if (!init_pkg_db.Init(argc, argv)) return -1; - if (!init_pkg_db.Run()) + if (!init_pkg_db.Run()) { + AddErrorFlag(); return -1; + } return 0; } -- 2.7.4