1 // Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache 2.0 license that can be
3 // found in the LICENSE file.
5 #include "common/step/pkgmgr/step_remove_priv_sharedres.h"
7 #include <pkgmgr-info.h>
8 #include <tzplatform_config.h>
10 #include <boost/filesystem.hpp>
12 #include "common/utils/file_util.h"
13 #include "common/utils/user_util.h"
15 namespace bf = boost::filesystem;
19 bf::path GetRootPathForUid(uid_t uid) {
20 tzplatform_set_user(uid);
21 const char* rootpath = tzplatform_getenv(TZ_USER_HOME);
22 tzplatform_reset_user();
26 bool RemoveResForUid(const std::string& pkgid, uid_t uid) {
27 bf::path target_path = GetRootPathForUid(uid) / pkgid;
29 if (bf::exists(target_path))
32 if (!common_installer::RemoveAll(target_path))
38 int SendUninstallRequest(const std::string& pkgid, uid_t uid) {
39 pkgmgr_client *pc = pkgmgr_client_new(PC_REQUEST);
41 int ret = pkgmgr_client_res_usr_uninstall(pc, pkgid.c_str(), uid);
42 pkgmgr_client_free(pc);
49 namespace common_installer {
52 Step::Status StepRemovePrivSharedres::process() {
53 if (context_->installation_mode.get() == InstallationMode::OFFLINE)
54 return OfflineRemove();
56 return RequestRemove();
59 Step::Status StepRemovePrivSharedres::OfflineRemove() {
60 if (context_->request_mode.get() == RequestMode::GLOBAL) {
61 common_installer::UserList user_list = common_installer::GetUserList();
62 for (const auto& l : user_list) {
63 uid_t uid = std::get<0>(l);
64 if (!RemoveResForUid(context_->pkgid.get(), uid))
68 if (!RemoveResForUid(context_->pkgid.get(), context_->uid.get()))
75 Step::Status StepRemovePrivSharedres::RequestRemove() {
76 if (context_->request_mode.get() == RequestMode::GLOBAL) {
77 common_installer::UserList user_list = common_installer::GetUserList();
78 for (const auto& l : user_list) {
79 uid_t uid = std::get<0>(l);
80 int ret = SendUninstallRequest(context_->pkgid.get(), uid);
81 if (ret == PKGMGR_R_OK) {
83 } else if (ret == PKGMGR_R_ECOMM) {
84 return OfflineRemove();
86 LOG(ERROR) << "Failed to remove privileged shared resources";
91 int ret = SendUninstallRequest(context_->pkgid.get(), context_->uid.get());
92 if (ret == PKGMGR_R_OK) {
94 } else if (ret == PKGMGR_R_ECOMM) {
95 return OfflineRemove();
97 LOG(ERROR) << "Failed to remove privileged shared resources";
104 } // namespace pkgmgr
105 } // namespace common_installer