Fix pkg_initdb tool to check backend execution result 50/252650/1
authorJunghyun Yeon <jungh.yeon@samsung.com>
Tue, 26 Jan 2021 09:32:29 +0000 (18:32 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Mon, 1 Feb 2021 06:03:16 +0000 (15:03 +0900)
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 <jungh.yeon@samsung.com>
(cherry picked from commit b09b3e5cae5e8f25a29e256b6265f0e50b6280b1)

src/common/utils/subprocess.cc
src/common/utils/subprocess.h
src/pkg_initdb/init_pkg_db.cc
src/pkg_initdb/init_pkg_db.h
src/pkg_initdb/pkg_initdb.cc

index 3c0b7a5..d5ca6d2 100644 (file)
@@ -53,14 +53,17 @@ bool Subprocess::RunWithArgs(const std::vector<std::string>& 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() {
index 42527ee..8807a29 100644 (file)
@@ -22,7 +22,7 @@ class Subprocess {
 
   bool RunWithArgs(
       const std::vector<std::string>& args = std::vector<std::string>());
-  int Wait();
+  bool Wait();
 
   void set_uid(int uid) {
     uid_ = uid;
index 7023902..26b363b 100644 (file)
@@ -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<ManifestInfo> 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;
index f41e99b..79878ae 100644 (file)
@@ -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);
 
index 680c2b9..a7b7ab5 100644 (file)
@@ -5,6 +5,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <fstream>
 #include <iostream>
 
 #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;
 }