Remove package-recovery-helper service 01/202801/6
authorSangyoon Jang <jeremy.jang@samsung.com>
Thu, 4 Apr 2019 06:12:34 +0000 (15:12 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Tue, 16 Apr 2019 01:49:33 +0000 (01:49 +0000)
Now package-recovery service handles all recovery process directly.

Change-Id: Ic599c965c832538b4bf28d212f955e8a92b29736
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
packaging/app-installers.spec
src/pkg_recovery/CMakeLists.txt
src/pkg_recovery/org.tizen.pkg_recovery.conf [deleted file]
src/pkg_recovery/org.tizen.pkg_recovery.service [deleted file]
src/pkg_recovery/package-recovery-helper.service [deleted file]
src/pkg_recovery/package-recovery.service
src/pkg_recovery/pkg_recovery.cc
src/pkg_recovery/pkg_recovery_helper.cc [deleted file]

index 013df8d..28b5fe0 100644 (file)
@@ -89,8 +89,8 @@ make %{?_smp_mflags}
 %make_install
 
 mkdir -p %{buildroot}%{unpackdir}
-mkdir -p %{buildroot}%{_unitdir_user}/default.target.wants
-ln -sf ../package-recovery-helper.service %{buildroot}%{_unitdir_user}/default.target.wants/package-recovery-helper.service
+mkdir -p %{buildroot}%{_unitdir}/default.target.wants
+ln -sf ../package-recovery.service %{buildroot}%{_unitdir}/default.target.wants/package-recovery.service
 mkdir -p %{buildroot}%{rosignaturedir}
 mkdir -p %{buildroot}%{rwsignaturedir}
 
@@ -112,12 +112,8 @@ chsmack -a System %{unpackdir}
 %attr(0755,root,root) %{_sysconfdir}/gumd/useradd.d/10_package-manager-add.post
 %attr(0755,root,root) %{_sysconfdir}/gumd/userdel.d/11_package-manager-del.post
 %{_bindir}/pkg_recovery
-%{_bindir}/pkg_recovery_helper
-%{_prefix}/share/dbus-1/system-services/org.tizen.pkg_recovery.service
-%{_sysconfdir}/dbus-1/system.d/org.tizen.pkg_recovery.conf
 %{_unitdir}/package-recovery.service
-%{_unitdir_user}/package-recovery-helper.service
-%{_unitdir_user}/default.target.wants/package-recovery-helper.service
+%{_unitdir}/default.target.wants/package-recovery.service
 %{unpackdir}
 %license LICENSE
 %{rosignaturedir}
index 5077d4b..8e3ac2f 100644 (file)
@@ -2,25 +2,13 @@
 SET(PKG_RECOVERY_SRCS
   pkg_recovery.cc
 )
-SET(PKG_RECOVERY_HELPER_SRCS
-  pkg_recovery_helper.cc
-)
 
 # Target - definition
 ADD_EXECUTABLE(${TARGET_PKG_RECOVERY} ${PKG_RECOVERY_SRCS})
-ADD_EXECUTABLE(${TARGET_PKG_RECOVERY_HELPER} ${PKG_RECOVERY_HELPER_SRCS})
 # Target - includes
 TARGET_INCLUDE_DIRECTORIES(${TARGET_PKG_RECOVERY} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../")
 # Target - deps
 APPLY_PKG_CONFIG(${TARGET_PKG_RECOVERY} PUBLIC
-  GDBUS_DEPS
-  TZPLATFORM_CONFIG_DEPS
-  Boost
-)
-TARGET_INCLUDE_DIRECTORIES(${TARGET_PKG_RECOVERY_HELPER} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../")
-# Target - deps
-APPLY_PKG_CONFIG(${TARGET_PKG_RECOVERY_HELPER} PUBLIC
-  GDBUS_DEPS
   TZPLATFORM_CONFIG_DEPS
   Boost
 )
@@ -29,14 +17,7 @@ APPLY_PKG_CONFIG(${TARGET_PKG_RECOVERY_HELPER} PUBLIC
 TARGET_LINK_LIBRARIES(${TARGET_PKG_RECOVERY} PUBLIC ${TARGET_LIBNAME_COMMON})
 SET_TARGET_PROPERTIES(${TARGET_PKG_RECOVERY} PROPERTIES COMPILE_FLAGS ${CFLAGS} "-fPIE")
 SET_TARGET_PROPERTIES(${TARGET_PKG_RECOVERY} PROPERTIES LINK_FLAGS "-pie")
-TARGET_LINK_LIBRARIES(${TARGET_PKG_RECOVERY_HELPER} PUBLIC ${TARGET_LIBNAME_COMMON})
-SET_TARGET_PROPERTIES(${TARGET_PKG_RECOVERY_HELPER} PROPERTIES COMPILE_FLAGS ${CFLAGS} "-fPIE")
-SET_TARGET_PROPERTIES(${TARGET_PKG_RECOVERY_HELPER} PROPERTIES LINK_FLAGS "-pie")
 
 # Install
 INSTALL(TARGETS ${TARGET_PKG_RECOVERY} DESTINATION ${BINDIR})
-INSTALL(TARGETS ${TARGET_PKG_RECOVERY_HELPER} DESTINATION ${BINDIR})
-INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/org.tizen.pkg_recovery.service DESTINATION ${PREFIX}/share/dbus-1/system-services/)
-INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/org.tizen.pkg_recovery.conf DESTINATION ${SYSCONF_INSTALL_DIR}/dbus-1/system.d/)
 INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/package-recovery.service DESTINATION ${UNITDIR})
-INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/package-recovery-helper.service DESTINATION ${UNITDIR_USER})
diff --git a/src/pkg_recovery/org.tizen.pkg_recovery.conf b/src/pkg_recovery/org.tizen.pkg_recovery.conf
deleted file mode 100644 (file)
index 284ba75..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-<?xml version="1.0"?>
-<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
-    "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
-
-<busconfig>
-  <policy user="root">
-    <allow own="org.tizen.pkg_recovery"/>
-  </policy>
-  <policy user="app_fw">
-    <allow own="org.tizen.pkg_recovery"/>
-  </policy>
-  <policy context="default">
-    <deny own="org.tizen.pkg_recovery"/>
-    <deny send_destination="org.tizen.pkg_recovery"/>
-    <check send_destination="org.tizen.pkg_recovery" send_interface="org.tizen.pkg_recovery" privilege="http://tizen.org/privilege/packagemanager.admin"/>
-  </policy>
-</busconfig>
diff --git a/src/pkg_recovery/org.tizen.pkg_recovery.service b/src/pkg_recovery/org.tizen.pkg_recovery.service
deleted file mode 100644 (file)
index 5379d9f..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-[D-BUS Service]
-Name=org.tizen.pkg_recovery
-Exec=/bin/false
-SystemdService=package-recovery.service
diff --git a/src/pkg_recovery/package-recovery-helper.service b/src/pkg_recovery/package-recovery-helper.service
deleted file mode 100644 (file)
index 106cdf4..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-[Unit]
-Description=Installation Recovery Helper Service
-
-[Service]
-ExecStart=/usr/bin/pkg_recovery_helper
index 71ffd3d..cd15e5d 100644 (file)
@@ -1,8 +1,11 @@
 [Unit]
 Description=Installation Recovery Service
-Requires=dbus.service
+After=pkg-db-recovery.service
+Requires=pkg-db-recovery.service
 
 [Service]
+Type=oneshot
+RemainAfterExit=yes
 User=app_fw
 Group=app_fw
 SmackProcessLabel=System
index 3653123..7724c8d 100644 (file)
@@ -2,89 +2,48 @@
 // Use of this source code is governed by an apache-2.0 license that can be
 // found in the LICENSE file.
 
-#include <glib.h>
-#include <gio/gio.h>
+#include <boost/exception/diagnostic_information.hpp>
+#include <boost/filesystem/operations.hpp>
+#include <boost/filesystem/path.hpp>
 
-#include <manifest_parser/utils/logging.h>
+#include <glib.h>
+#include <common/request.h>
 #include <common/utils/subprocess.h>
+#include <common/utils/user_util.h>
+#include <manifest_parser/utils/logging.h>
+#include <sys/types.h>
+#include <tzplatform_config.h>
 
-#define UNUSED(expr) (void)(expr)
+#include <regex>
+#include <string>
+#include <vector>
 
+namespace bf = boost::filesystem;
 namespace ci = common_installer;
 
 namespace {
 
-const char kDBusInstropectionXml[] =
-  "<node>"
-  "  <interface name='org.tizen.pkg_recovery'>"
-  "    <method name='RecoverPkg'>"
-  "      <arg type='u' name='uid' direction='in'/>"
-  "      <arg type='s' name='type' direction='in'/>"
-  "      <arg type='s' name='file' direction='in'/>"
-  "      <arg type='b' name='result' direction='out'/>"
-  "    </method>"
-  "  </interface>"
-  "</node>";
-const char kDBusServiceName[] = "org.tizen.pkg_recovery";
-const char kDBusObjectPath[] = "/org/tizen/pkg_recovery";
+typedef std::pair<std::string, std::string> RecoverEntry;
+
+const char kRecoveryFilePattern[] = "^(.*)-recovery-(.*)";
+const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
 
 class PkgRecoveryService {
  public:
   PkgRecoveryService();
   ~PkgRecoveryService();
-  bool Init();
   void Run();
 
  private:
+  std::vector<RecoverEntry> SearchRecoveryFiles(uid_t uid);
+  void ProcessRecovery(uid_t uid, const std::vector<RecoverEntry>& entries);
   bool RunBackend(uid_t uid, const char* type, const char* file);
-  void Finish();
-  void RenewTimeout(int ms);
-  void HandleMethodCall(GDBusConnection* connection,
-      const gchar* sender, const gchar* object_path,
-      const gchar* interface_name, const gchar* method_name,
-      GVariant* parameters, GDBusMethodInvocation* invocation,
-      gpointer user_data);
-  void OnBusAcquired(GDBusConnection* connection, const gchar* name,
-      gpointer user_data);
-
-  GDBusNodeInfo* node_info_;
-  guint owner_id_;
-  GMainLoop* loop_;
-  guint sid_;
 };
 
-PkgRecoveryService::PkgRecoveryService() :
-    node_info_(nullptr), owner_id_(0), loop_(nullptr), sid_(0) {
+PkgRecoveryService::PkgRecoveryService() {
 }
 
 PkgRecoveryService::~PkgRecoveryService() {
-  Finish();
-}
-
-bool PkgRecoveryService::Init() {
-  node_info_ = g_dbus_node_info_new_for_xml(kDBusInstropectionXml, nullptr);
-  if (!node_info_) {
-    LOG(ERROR) << "Failed to create DBus node info";
-    return false;
-  }
-  owner_id_ = g_bus_own_name(G_BUS_TYPE_SYSTEM, kDBusServiceName,
-      G_BUS_NAME_OWNER_FLAGS_NONE,
-      [](GDBusConnection* connection, const gchar* name,
-          gpointer user_data) {
-        reinterpret_cast<PkgRecoveryService*>(user_data)->OnBusAcquired(
-            connection, name, user_data);
-      },
-      nullptr, nullptr, this, nullptr);
-
-  loop_ = g_main_loop_new(nullptr, FALSE);
-  if (!loop_) {
-    LOG(ERROR) << "Failed to create main loop";
-    return false;
-  }
-
-  RenewTimeout(5000);
-
-  return true;
 }
 
 bool PkgRecoveryService::RunBackend(uid_t uid, const char* type,
@@ -100,85 +59,55 @@ bool PkgRecoveryService::RunBackend(uid_t uid, const char* type,
 }
 
 void PkgRecoveryService::Run() {
-  g_main_loop_run(loop_);
-}
-
-void PkgRecoveryService::Finish() {
-  if (owner_id_ > 0)
-    g_bus_unown_name(owner_id_);
-  if (node_info_)
-    g_dbus_node_info_unref(node_info_);
-  if (loop_)
-    g_main_loop_unref(loop_);
-}
-
-void PkgRecoveryService::RenewTimeout(int ms) {
-  if (sid_)
-    g_source_remove(sid_);
-  sid_ = g_timeout_add(ms,
-      [](gpointer user_data) {
-        g_main_loop_quit(
-            reinterpret_cast<PkgRecoveryService*>(user_data)->loop_);
-        return FALSE;
-      },
-      this);
+  // recover global packages
+  LOG(INFO) << "Searching recovery files for user " << kGlobalUserUid;
+  std::vector<RecoverEntry> globalentries = SearchRecoveryFiles(kGlobalUserUid);
+  ProcessRecovery(kGlobalUserUid, globalentries);
+
+  // recover normal user packages
+  ci::UserList list = ci::GetUserList();
+  for (auto userinfo : list) {
+    uid_t uid = std::get<0>(userinfo);
+    LOG(INFO) << "Searching recovery files for user " << std::get<0>(userinfo);
+    std::vector<RecoverEntry> entries = SearchRecoveryFiles(uid);
+    ProcessRecovery(uid, entries);
+  }
 }
 
-void PkgRecoveryService::HandleMethodCall(GDBusConnection* connection,
-    const gchar* sender, const gchar* object_path, const gchar* interface_name,
-    const gchar* method_name, GVariant* parameters,
-    GDBusMethodInvocation* invocation, gpointer user_data) {
-  UNUSED(connection);
-  UNUSED(sender);
-  UNUSED(object_path);
-  UNUSED(interface_name);
-  UNUSED(user_data);
-  bool r = false;
-
-  LOG(INFO) << "Incomming method call: " << method_name;
-  if (g_strcmp0(method_name, "RecoverPkg") == 0) {
-    uid_t uid;
-    char* type;
-    char* file;
-    g_variant_get(parameters, "(u&s&s)", &uid, &type, &file);
-    LOG(DEBUG) << "Recover pkg. uid: " << uid << ", type: " << type
-               << ", file: " << file;
-    r = RunBackend(uid, type, file);
-  } else {
-    LOG(ERROR) << "Unknown method call: " << method_name;
+std::vector<RecoverEntry> PkgRecoveryService::SearchRecoveryFiles(uid_t uid) {
+  std::vector<RecoverEntry> list;
+  const bf::path recovery_dir = ci::GetRootAppPath(false, uid);
+  LOG(INFO) << "RootAppPath: " << recovery_dir;
+  for (bf::directory_iterator iter(recovery_dir);
+      iter != bf::directory_iterator();
+      ++iter) {
+    try {
+      std::string file = iter->path().filename().string();
+      std::regex recovery_regex(kRecoveryFilePattern);
+      std::smatch match;
+      if (std::regex_search(file, match, recovery_regex)) {
+        LOG(INFO) << "Found recovery file: " << file;
+        std::string type(match[1]);
+        list.emplace_back(type, iter->path().string());
+      }
+    } catch (...) {
+      LOG(WARNING) << "Exception occurred: "
+                   << boost::current_exception_diagnostic_information();
+      continue;
+    }
   }
-  g_dbus_method_invocation_return_value(invocation, g_variant_new("(b)", r));
 
-  RenewTimeout(5000);
+  return list;
 }
 
-void PkgRecoveryService::OnBusAcquired(
-    GDBusConnection* connection, const gchar* name, gpointer user_data) {
-  UNUSED(name);
-  UNUSED(user_data);
-  GError* err = nullptr;
-  GDBusInterfaceVTable vtable = {
-    [](GDBusConnection* connection, const gchar* sender,
-        const gchar* object_path, const gchar* interface_name,
-        const gchar* method_name, GVariant* parameters,
-        GDBusMethodInvocation* invocation, gpointer user_data) {
-      reinterpret_cast<PkgRecoveryService*>(user_data)->HandleMethodCall(
-          connection, sender, object_path, interface_name, method_name,
-          parameters, invocation, user_data);
-    },
-    nullptr, nullptr, {0, }
-  };
-
-  guint reg_id = g_dbus_connection_register_object(connection, kDBusObjectPath,
-      node_info_->interfaces[0], &vtable, this, nullptr, &err);
-  if (reg_id == 0) {
-    LOG(ERROR) << "Register failed";
-    if (err) {
-      LOG(ERROR) << "Error message: " << err->message;
-      g_error_free(err);
-    }
-  } else {
-    LOG(INFO) << "DBus service registered";
+void PkgRecoveryService::ProcessRecovery(uid_t uid,
+    const std::vector<RecoverEntry>& entries) {
+  LOG(INFO) << "Process recovery for user " << uid;
+  for (auto entry : entries) {
+    const char* type = entry.first.c_str();
+    const char* file = entry.second.c_str();
+    if (!RunBackend(uid, type, file))
+      LOG(ERROR) << "Recovery process for " << file << " failed";
   }
 }
 
@@ -186,10 +115,6 @@ void PkgRecoveryService::OnBusAcquired(
 
 int main() {
   PkgRecoveryService service;
-  if (!service.Init()) {
-    LOG(ERROR) << "Failed to initialize service";
-    return -1;
-  }
   service.Run();
   return 0;
 }
diff --git a/src/pkg_recovery/pkg_recovery_helper.cc b/src/pkg_recovery/pkg_recovery_helper.cc
deleted file mode 100644 (file)
index d04bcdb..0000000
+++ /dev/null
@@ -1,138 +0,0 @@
-// Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
-// Use of this source code is governed by an apache-2.0 license that can be
-// found in the LICENSE file.
-
-#include <boost/exception/diagnostic_information.hpp>
-#include <boost/filesystem/operations.hpp>
-#include <boost/filesystem/path.hpp>
-
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <glib.h>
-#include <gio/gio.h>
-
-#include <tzplatform_config.h>
-#include <manifest_parser/utils/logging.h>
-#include <common/request.h>
-#include <common/utils/user_util.h>
-
-#include <regex>
-#include <string>
-#include <iostream>
-#include <utility>
-#include <vector>
-
-namespace bf = boost::filesystem;
-namespace bs = boost::system;
-namespace ci = common_installer;
-
-namespace {
-
-typedef std::pair<std::string, std::string> RecoverEntry;
-
-const char kRecoveryFilePattern[] = "^(.*)-recovery-(.*)";
-const char kRecoveryMethodName[] = "RecoverPkg";
-const char kDBusServiceName[] = "org.tizen.pkg_recovery";
-const char kDBusObjectPath[] = "/org/tizen/pkg_recovery";
-const char kDBusInterfaceName[] = "org.tizen.pkg_recovery";
-const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
-
-bool RequestRecoveryService(uid_t uid, const char* type, const char* file) {
-  GError* err = nullptr;
-  GDBusConnection* con = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &err);
-  if (!con || err) {
-    LOG(WARNING) << "Failed to get dbus connection: " << err->message;
-    g_error_free(err);
-    return false;
-  }
-  GDBusProxy* proxy = g_dbus_proxy_new_sync(con, G_DBUS_PROXY_FLAGS_NONE,
-      nullptr, kDBusServiceName, kDBusObjectPath, kDBusInterfaceName, nullptr,
-      &err);
-  if (!proxy) {
-    std::string err_msg;
-    if (err) {
-      err_msg = std::string(err->message);
-      g_error_free(err);
-    }
-    LOG(ERROR) << "Failed to get dbus proxy: " << err_msg;
-    g_object_unref(con);
-    return false;
-  }
-  GVariant* r = g_dbus_proxy_call_sync(proxy, kRecoveryMethodName,
-      g_variant_new("(uss)", uid, type, file), G_DBUS_CALL_FLAGS_NONE, -1,
-      nullptr, &err);
-  if (!r) {
-    std::string err_msg;
-    if (err) {
-      err_msg = std::string(err->message);
-      g_error_free(err);
-    }
-    LOG(ERROR) << "Failed to request: " << err_msg;
-    g_object_unref(proxy);
-    g_object_unref(con);
-    return false;
-  }
-  bool result;
-  g_variant_get(r, "(b)", &result);
-
-  g_variant_unref(r);
-  g_object_unref(proxy);
-  g_object_unref(con);
-
-  return result;
-}
-
-std::vector<RecoverEntry> SearchRecoveryFiles(uid_t uid) {
-  std::vector<RecoverEntry> list;
-  const bf::path recovery_dir = ci::GetRootAppPath(false, uid);
-  for (bf::directory_iterator iter(recovery_dir);
-      iter != bf::directory_iterator();
-      ++iter) {
-    try {
-      std::string file = iter->path().filename().string();
-      std::regex recovery_regex(kRecoveryFilePattern);
-      std::smatch match;
-      if (std::regex_search(file, match, recovery_regex)) {
-        LOG(INFO) << "Found recovery file: " << file;
-        std::string type(match[1]);
-        list.emplace_back(type, iter->path().string());
-      }
-    } catch (...) {
-      LOG(WARNING) << "Exception occurred: "
-                   << boost::current_exception_diagnostic_information();
-      continue;
-    }
-  }
-
-  return list;
-}
-
-}  // namespace
-
-int main() {
-  uid_t uid = getuid();
-  std::vector<std::pair<std::string, std::string>> list;
-  try {
-    list = SearchRecoveryFiles(uid);
-  } catch (...) {
-    LOG(WARNING) << "Exception occurred: "
-                 << boost::current_exception_diagnostic_information();
-    return 0;
-  }
-  for (auto entry : list) {
-    if (!RequestRecoveryService(uid, entry.first.c_str(),
-        entry.second.c_str()))
-      LOG(ERROR) << "Failed to recover installation: " << entry.second;
-  }
-
-  if (ci::IsAdminUser(uid)) {
-    list = SearchRecoveryFiles(kGlobalUserUid);
-    for (auto entry : list) {
-      if (!RequestRecoveryService(uid, entry.first.c_str(),
-          entry.second.c_str()))
-        LOG(ERROR) << "Failed to recover installation: " << entry.second;
-    }
-  }
-  return 0;
-}