Add Steps for handling privileged shared resources 21/263321/3
authorJunghyun Yeon <jungh.yeon@samsung.com>
Tue, 31 Aug 2021 07:40:53 +0000 (16:40 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Wed, 1 Sep 2021 06:40:42 +0000 (15:40 +0900)
Add new steps responsible for removing privileged shared resources
while uninstall and its recovery requests.

Change-Id: Iec89a431e415b6abc84c2bf6409fc30445818536
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
src/common/installer/app_installer.cc
src/common/step/pkgmgr/step_recover_priv_sharedres.cc [new file with mode: 0644]
src/common/step/pkgmgr/step_recover_priv_sharedres.h [new file with mode: 0644]
src/common/step/pkgmgr/step_remove_priv_sharedres.cc [new file with mode: 0644]
src/common/step/pkgmgr/step_remove_priv_sharedres.h [new file with mode: 0644]

index 2d064ac..0f767c3 100644 (file)
@@ -76,7 +76,9 @@
 #include "common/step/pkgmgr/step_recover_application.h"
 #include "common/step/pkgmgr/step_recover_parser_plugins.h"
 #include "common/step/pkgmgr/step_register_app.h"
+#include "common/step/pkgmgr/step_recover_priv_sharedres.h"
 #include "common/step/pkgmgr/step_remove_manifest.h"
+#include "common/step/pkgmgr/step_remove_priv_sharedres.h"
 #include "common/step/pkgmgr/step_run_parser_plugins.h"
 #include "common/step/pkgmgr/step_unregister_app.h"
 #include "common/step/pkgmgr/step_update_app.h"
@@ -401,6 +403,7 @@ void AppInstaller::UninstallSteps() {
   AddStep<ci::security::StepPrivacyPrivilege>(
       ci::security::StepPrivacyPrivilege::ActionType::Uninstall);
   AddStep<ci::filesystem::StepRemoveFiles>();
+  AddStep<ci::pkgmgr::StepRemovePrivSharedres>();
   AddStep<ci::filesystem::StepRemoveZipImage>();
   AddStep<ci::filesystem::StepRemoveIcons>();
   AddStep<ci::filesystem::StepRemoveTep>();
@@ -514,6 +517,7 @@ void AppInstaller::RecoverySteps() {
   AddStep<ci::filesystem::StepRecoverStorageDirectories>();
   AddStep<ci::filesystem::StepRecoverGlobalAppSymlinks>();
   AddStep<ci::filesystem::StepRecoverFiles>();
+  AddStep<ci::pkgmgr::StepRecoverPrivSharedres>();
   AddStep<ci::mount::StepMountRecover>();
   AddStep<ci::filesystem::StepRecoverChangeOwner>();
   AddStep<ci::pkgmgr::StepRecoverApplication>();
@@ -692,6 +696,7 @@ void AppInstaller::PartialUninstallSteps() {
   AddStep<ci::filesystem::StepRemoveGlobalAppSymlinks>();
   AddStep<ci::filesystem::StepOptionalAcquireExternalStorage>();
   AddStep<ci::filesystem::StepRemovePerUserStorageDirectories>();
+  AddStep<ci::pkgmgr::StepRemovePrivSharedres>();
   AddStep<ci::pkgmgr::StepUnregisterApplication>();
   AddStep<ci::security::StepUnregisterTrustAnchor>();
   AddStep<ci::security::StepPrivacyPrivilege>(
@@ -759,6 +764,7 @@ void AppInstaller::ReadonlyUpdateUninstallSteps() {
   AddStep<ci::security::StepPrivacyPrivilege>(
       ci::security::StepPrivacyPrivilege::ActionType::Update);
   AddStep<ci::filesystem::StepRemoveFiles>();
+  AddStep<ci::pkgmgr::StepRemovePrivSharedres>();
   AddStep<ci::filesystem::StepRemoveZipImage>();
   AddStep<ci::filesystem::StepRemoveIcons>();
   AddStep<ci::filesystem::StepRemoveTep>();
@@ -777,6 +783,7 @@ void AppInstaller::DisablePkgSteps() {
       ci::configuration::StepParseManifest::ManifestLocation::INSTALLED,
       ci::configuration::StepParseManifest::StoreLocation::NORMAL);
   AddStep<ci::pkgmgr::StepKillApps>();
+  AddStep<ci::pkgmgr::StepRemovePrivSharedres>();
   AddStep<ci::pkgmgr::StepUpdatePkgDisableInfo>(
     ci::pkgmgr::StepUpdatePkgDisableInfo::ActionType::Disable);
   AddStep<ci::pkgmgr::StepRunParserPlugin>(
diff --git a/src/common/step/pkgmgr/step_recover_priv_sharedres.cc b/src/common/step/pkgmgr/step_recover_priv_sharedres.cc
new file mode 100644 (file)
index 0000000..3478e6d
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright (c) 2021 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 "common/step/pkgmgr/step_recover_priv_sharedres.h"
+
+#include <boost/filesystem.hpp>
+
+#include "common/utils/paths.h"
+#include "common/utils/pkgmgr_query.h"
+#include "common/pkgmgr_registration.h"
+#include "common/step/pkgmgr/step_remove_priv_sharedres.h"
+
+namespace common_installer {
+namespace pkgmgr {
+
+Step::Status StepRecoverPrivSharedres::RecoveryUninstall() {
+  StepRemovePrivSharedres instance(context_);
+  return instance.process();
+}
+
+}  // namespace pkgmgr
+}  // namespace common_installer
diff --git a/src/common/step/pkgmgr/step_recover_priv_sharedres.h b/src/common/step/pkgmgr/step_recover_priv_sharedres.h
new file mode 100644 (file)
index 0000000..cea9cc6
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright (c) 2021 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.
+
+#ifndef COMMON_STEP_PKGMGR_STEP_RECOVER_PRIV_SHAREDRES_H_
+#define COMMON_STEP_PKGMGR_STEP_RECOVER_PRIV_SHAREDRES_H_
+
+#include <manifest_parser/utils/logging.h>
+
+#include "common/installer_context.h"
+#include "common/step/recovery/step_recovery.h"
+
+namespace common_installer {
+namespace pkgmgr {
+
+class StepRecoverPrivSharedres : public recovery::StepRecovery {
+ public:
+  using StepRecovery::StepRecovery;
+
+  Status RecoveryNew() override { return Status::OK; }
+  Status RecoveryUpdate() override { return Status::OK; }
+  Status RecoveryUninstall() override;
+
+  STEP_NAME(RecoverPrivSharedres)
+};
+
+}  // namespace pkgmgr
+}  // namespace common_installer
+
+#endif  // COMMON_STEP_PKGMGR_STEP_RECOVER_PRIV_SHAREDRES_H_
diff --git a/src/common/step/pkgmgr/step_remove_priv_sharedres.cc b/src/common/step/pkgmgr/step_remove_priv_sharedres.cc
new file mode 100644 (file)
index 0000000..503d335
--- /dev/null
@@ -0,0 +1,95 @@
+// Copyright (c) 2021 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 "common/step/pkgmgr/step_remove_priv_sharedres.h"
+
+#include <pkgmgr-info.h>
+#include <tzplatform_config.h>
+
+#include <boost/filesystem.hpp>
+
+#include "common/utils/file_util.h"
+#include "common/utils/user_util.h"
+
+namespace bf = boost::filesystem;
+
+namespace {
+
+bf::path GetRootPathForUid(uid_t uid) {
+  tzplatform_set_user(uid);
+  const char* rootpath = tzplatform_getenv(TZ_USER_HOME);
+  tzplatform_reset_user();
+  return rootpath;
+}
+
+bool RemoveResForUid(const std::string& pkgid, uid_t uid) {
+  bf::path target_path = GetRootPathForUid(uid) / pkgid;
+
+  if (bf::exists(target_path))
+    return true;
+
+  if (!common_installer::RemoveAll(target_path))
+    return false;
+
+  return true;
+}
+
+bool SendUninstallRequest(const std::string& pkgid, uid_t uid) {
+  pkgmgr_client *pc = pkgmgr_client_new(PC_REQUEST);
+
+  int ret = pkgmgr_client_res_usr_uninstall(pc, pkgid.c_str(), uid);
+  pkgmgr_client_free(pc);
+
+  return (ret == PKGMGR_R_OK) ? true : false;
+}
+
+}  // namespace
+
+namespace common_installer {
+namespace pkgmgr {
+
+Step::Status StepRemovePrivSharedres::process() {
+  if (context_->installation_mode.get() == InstallationMode::OFFLINE)
+    return OfflineRemove();
+  else
+    return RequestRemove();
+}
+
+Step::Status StepRemovePrivSharedres::OfflineRemove() {
+  if (context_->request_mode.get() == RequestMode::GLOBAL) {
+    common_installer::UserList user_list = common_installer::GetUserList();
+    for (auto l : user_list) {
+      uid_t uid = std::get<0>(l);
+      if (!RemoveResForUid(context_->pkgid.get(), uid))
+        return Status::ERROR;
+    }
+  } else {
+    if (!RemoveResForUid(context_->pkgid.get(), context_->uid.get()))
+      return Status::ERROR;
+  }
+
+  return Status::OK;
+}
+
+Step::Status StepRemovePrivSharedres::RequestRemove() {
+  if (context_->request_mode.get() == RequestMode::GLOBAL) {
+    common_installer::UserList user_list = common_installer::GetUserList();
+    for (auto l : user_list) {
+      uid_t uid = std::get<0>(l);
+      if (!SendUninstallRequest(context_->pkgid.get(), uid)) {
+        LOG(ERROR) << "Failed to remove privileged shared resources";
+        return Status::ERROR;
+      }
+    }
+  } else {
+    if (!SendUninstallRequest(context_->pkgid.get(), context_->uid.get())) {
+      LOG(ERROR) << "Failed to remove privileged shared resources";
+      return Status::ERROR;
+    }
+  }
+  return Status::OK;
+}
+
+}  // namespace pkgmgr
+}  // namespace common_installer
diff --git a/src/common/step/pkgmgr/step_remove_priv_sharedres.h b/src/common/step/pkgmgr/step_remove_priv_sharedres.h
new file mode 100644 (file)
index 0000000..403072f
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by a apache 2.0 license that can be
+// found in the LICENSE file.
+
+#ifndef COMMON_STEP_PKGMGR_STEP_REMOVE_PRIV_SHAREDRES_H_
+#define COMMON_STEP_PKGMGR_STEP_REMOVE_PRIV_SHAREDRES_H_
+
+#include <manifest_parser/utils/logging.h>
+
+#include "common/installer_context.h"
+#include "common/step/step.h"
+
+namespace common_installer {
+namespace pkgmgr {
+
+class StepRemovePrivSharedres : public Step {
+ public:
+  using Step::Step;
+
+  Status process() override;
+  Status clean() override { return Status::OK; }
+  Status undo() override { return Status::OK; }
+  Status precheck() override { return Status::OK; }
+
+ private:
+  Status OfflineRemove();
+  Status RequestRemove();
+
+  STEP_NAME(RemovePrivSharedres)
+};
+
+}  // namespace pkgmgr
+}  // namespace common_installer
+
+#endif  // COMMON_STEP_PKGMGR_STEP_REMOVE_PRIV_SHAREDRES_H_