1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
5 #include "common/pkgmgr_signal.h"
7 #include <manifest_parser/utils/logging.h>
10 #include <sys/types.h>
11 #include <systemd/sd-login.h>
12 #include <tzplatform_config.h>
18 #include "common/pkgmgr_query.h"
22 namespace ci = common_installer;
24 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
25 const std::map<ci::RequestType, const char*> kEventStr = {
26 {ci::RequestType::Install, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
27 {ci::RequestType::Recovery, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
28 {ci::RequestType::Reinstall, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
29 {ci::RequestType::Uninstall, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR},
30 {ci::RequestType::PartialUninstall, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR},
31 {ci::RequestType::Update, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
32 {ci::RequestType::ReadonlyUpdateInstall, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
33 {ci::RequestType::ReadonlyUpdateUninstall,
34 PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
35 {ci::RequestType::Delta, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
36 {ci::RequestType::Move, PKGMGR_INSTALLER_MOVE_EVENT_STR},
37 {ci::RequestType::MountInstall, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
38 {ci::RequestType::MountUpdate, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
39 {ci::RequestType::ManifestDirectInstall, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
40 {ci::RequestType::ManifestDirectUpdate, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
41 {ci::RequestType::ManifestPartialInstall, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
42 {ci::RequestType::ManifestPartialUpdate, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
43 {ci::RequestType::DisablePkg, PKGMGR_INSTALLER_UNINSTALL_EVENT_STR},
44 {ci::RequestType::EnablePkg, PKGMGR_INSTALLER_INSTALL_EVENT_STR},
45 {ci::RequestType::MigrateExtImg, PKGMGR_INSTALLER_UPGRADE_EVENT_STR},
46 {ci::RequestType::Unknown, PKGMGR_INSTALLER_UNKNOWN_EVENT_STR}
51 namespace common_installer {
53 PkgmgrSignal::State PkgmgrSignal::state_ = PkgmgrSignal::State::NOT_SENT;
55 PkgmgrSignal::PkgmgrSignal(pkgmgr_installer* pi, RequestType req_type)
56 : pi_(pi), request_type_(req_type), error_message_sent_(false) {
57 uid_ = pkgmgr_installer_get_uid(pi_);
58 request_mode_ = GetRequestMode(uid_);
61 bool PkgmgrSignal::SendStarted(
62 const std::string& type, const std::string& pkgid) {
63 if (state_ != State::NOT_SENT) {
67 if (!SetupUserList(pkgid))
68 LOG(WARNING) << "Failed to setup user list";
70 auto key = kEventStr.find(request_type_);
71 if (key == kEventStr.end()) {
74 if (!SendSignal(PKGMGR_INSTALLER_START_KEY_STR, key->second, type, pkgid)) {
77 for (auto l : user_list_) {
78 key = kEventStr.find(l.second);
79 if (key == kEventStr.end())
81 SendSignal(l.first, PKGMGR_INSTALLER_START_KEY_STR, key->second, type,
85 state_ = State::STARTED;
87 // workaround for pkgmgr client to know all appids which are uninstalled
88 if (request_type_ == ci::RequestType::Uninstall)
89 if (!SendAppids(type, pkgid))
91 for (auto l : user_list_)
92 SendAppids(l.first, type, pkgid);
97 bool PkgmgrSignal::SendProgress(int progress,
98 const std::string& type, const std::string& pkgid) {
99 if (state_ != State::STARTED) {
103 if (!SendSignal(PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR,
104 std::to_string(progress).c_str(), type, pkgid))
106 for (auto l : user_list_)
108 SendSignal(l.first, PKGMGR_INSTALLER_INSTALL_PERCENT_KEY_STR,
109 std::to_string(progress).c_str(), type, pkgid);
114 bool PkgmgrSignal::SendFinished(
115 Step::Status result, const std::string& type, const std::string& pkgid) {
116 if (state_ != State::STARTED) {
119 if (result != Step::Status::OK && !error_message_sent_) {
121 PKGMGR_INSTALLER_ERROR_KEY_STR,
122 std::to_string(static_cast<int>(result)).c_str(), type, pkgid)) {
125 for (auto l : user_list_)
127 PKGMGR_INSTALLER_ERROR_KEY_STR,
128 std::to_string(static_cast<int>(result)).c_str(), type, pkgid);
131 PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid)) {
134 for (auto l : user_list_)
136 PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid);
137 state_ = State::FINISHED;
141 bool PkgmgrSignal::SendError(
143 const std::string& error_message,
144 const std::string& type,
145 const std::string& pkgid) {
146 if (state_ != State::STARTED) {
149 std::string error_value = std::to_string(static_cast<int>(result));
150 if (!error_message.empty()) {
151 error_value = error_value + ":" + error_message;
153 LOG(ERROR) << "PkgmgrSignal error_value: (" << error_value << ")";
154 error_message_sent_ = true;
156 PKGMGR_INSTALLER_ERROR_KEY_STR,
161 for (auto l : user_list_)
163 PKGMGR_INSTALLER_ERROR_KEY_STR,
170 bool PkgmgrSignal::SendSignal(
173 const std::string& type,
174 const std::string& pkgid) const {
175 // send pkgmgr signal
176 if (pkgmgr_installer_send_signal(
178 !type.empty() ? type.c_str(): "",
179 !pkgid.empty() ? pkgid.c_str() : "",
182 LOG(ERROR) << "Fail to send pkgmgr signal";
186 LOG(DEBUG) << "Success to send pkgmgr signal"
187 << " PKGID=" << pkgid
189 << " VALUE=" << value;
193 bool PkgmgrSignal::SendSignal(
197 const std::string& type,
198 const std::string& pkgid) const {
199 // send pkgmgr signal
200 if (pkgmgr_installer_send_signal_for_uid(
203 !type.empty() ? type.c_str(): "",
204 !pkgid.empty() ? pkgid.c_str() : "",
207 LOG(ERROR) << "Fail to send pkgmgr signal";
211 LOG(DEBUG) << "Success to send pkgmgr signal"
213 << " PKGID=" << pkgid
215 << " VALUE=" << value;
219 const char* PkgmgrSignal::GetResultKey(Step::Status result) const {
221 case Step::Status::OK:
222 return PKGMGR_INSTALLER_OK_EVENT_STR;
224 return PKGMGR_INSTALLER_FAIL_EVENT_STR;
228 bool PkgmgrSignal::SendAppids(const std::string& type,
229 const std::string& pkgid) const {
230 std::vector<std::string> appids;
231 if (!QueryAppidsForPkgId(pkgid, &appids, pkgmgr_installer_get_uid(pi_)))
233 for (auto& appid : appids) {
234 if (pkgmgr_installer_send_app_uninstall_signal(pi_, type.c_str(),
242 bool PkgmgrSignal::SendAppids(uid_t uid,
243 const std::string& type,
244 const std::string& pkgid) const {
245 std::vector<std::string> appids;
246 if (!QueryAppidsForPkgId(pkgid, &appids, pkgmgr_installer_get_uid(pi_)))
248 for (auto& appid : appids) {
249 if (pkgmgr_installer_send_app_uninstall_signal_for_uid(
250 pi_, uid, type.c_str(), pkgid.c_str(), appid.c_str()))
256 bool PkgmgrSignal::SetupUserList(const std::string& pkgid) {
258 uid_t* uids = nullptr;
259 int n = sd_get_uids(&uids);
264 for (i = 0; i < n; i++) {
265 switch (request_mode_) {
266 case RequestMode::GLOBAL:
267 // if user pkg is installed, installer will not send signal to user.
268 if (QueryIsPackageInstalled(pkgid, RequestMode::USER, uids[i]))
271 user_list_.emplace_back(uids[i], request_type_);
273 case RequestMode::USER:
276 // if global pkg is installed,
277 // user pkg operation will be handled as update.
278 if (QueryIsPackageInstalled(pkgid, kGlobalUserUid))
279 user_list_.emplace_back(uids[i], RequestType::Update);
281 user_list_.emplace_back(uids[i], request_type_);
284 user_list_.emplace_back(uids[i], request_type_);
295 } // namespace common_installer