Change pid related bahavior
[platform/core/appfw/pkgmgr-info.git] / src / server / worker_thread.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "worker_thread.hh"
18
19 #include "pkgmgrinfo_debug.h"
20 #include "get_appinfo_request_handler.hh"
21 #include "get_cert_request_handler.hh"
22 #include "get_pkginfo_request_handler.hh"
23 #include "get_depinfo_request_handler.hh"
24 #include "query_request_handler.hh"
25 #include "set_cert_request_handler.hh"
26 #include "set_pkginfo_request_handler.hh"
27 #include "abstract_parcelable.hh"
28 #include "command_request_handler.hh"
29
30 #ifdef LOG_TAG
31 #undef LOG_TAG
32 #endif
33 #define LOG_TAG "PKGMGR_INFO"
34
35 namespace pkgmgr_server {
36
37 WorkerThread::WorkerThread(int num) : stop_all_(false) {
38   threads_.reserve(num);
39   for (int i = 0; i < num; ++i)
40     threads_.emplace_back([this]() -> void { this->Run(); });
41   LOGD("%d Worker threads are created", num);
42 }
43
44 WorkerThread::~WorkerThread() {
45   stop_all_ = true;
46   cv_.notify_all();
47
48   for (auto& t : threads_) t.join();
49 }
50
51 bool WorkerThread::PushQueue(std::shared_ptr<PkgRequest> req) {
52   {
53     std::unique_lock<std::mutex> u(lock_);
54     queue_.push(req);
55     cv_.notify_one();
56   }
57   return true;
58 }
59
60 void WorkerThread::Run() {
61   std::unique_ptr<request_handler::AbstractRequestHandler> handler[pkgmgr_common::ReqType::MAX];
62   handler[pkgmgr_common::ReqType::GET_PKG_INFO].reset(
63       new request_handler::GetPkginfoRequestHandler());
64   handler[pkgmgr_common::ReqType::GET_APP_INFO].reset(
65       new request_handler::GetAppinfoRequestHandler());
66   handler[pkgmgr_common::ReqType::SET_PKG_INFO].reset(
67       new request_handler::SetPkginfoRequestHandler());
68   handler[pkgmgr_common::ReqType::SET_CERT_INFO].reset(
69       new request_handler::SetCertRequestHandler());
70   handler[pkgmgr_common::ReqType::GET_CERT_INFO].reset(
71       new request_handler::GetCertRequestHandler());
72   handler[pkgmgr_common::ReqType::GET_PKG_DEP_INFO].reset(
73       new request_handler::GetDepinfoRequestHandler());
74   handler[pkgmgr_common::ReqType::QUERY].reset(
75       new request_handler::QueryRequestHandler());
76   handler[pkgmgr_common::ReqType::COMMAND].reset(
77       new request_handler::CommandRequestHandler());
78
79   LOGD("Initialize request handlers");
80   while (true) {
81     std::shared_ptr<PkgRequest> req;
82     {
83       std::unique_lock<std::mutex> u(lock_);
84       cv_.wait(u, [this] { return !this->queue_.empty() || stop_all_; });
85       if (stop_all_ && queue_.empty()) return;
86       req = PopQueue();
87     }
88
89     if (req->ReceiveData() == false) {
90       LOGE("Failed to ReceiveData");
91       continue;
92     }
93     pkgmgr_common::ReqType type = req->GetRequestType();
94     LOGD("Request type(%d)", static_cast<int>(type));
95     if (type <= pkgmgr_common::ReqType::REQ_TYPE_NONE
96             || type >= pkgmgr_common::ReqType::MAX) {
97       LOGE("Request type is invalid (%d)", static_cast<int>(type));
98
99       pkgmgr_common::parcel::AbstractParcelable parcelable(
100           0, pkgmgr_common::parcel::ParcelableType::Unknown, PMINFO_R_ERROR);
101       tizen_base::Parcel p;
102       p.WriteParcelable(parcelable);
103       std::vector<uint8_t> raw = p.GetRaw();
104       req->SendData(&raw[0], raw.size());
105       continue;
106     }
107
108     try {
109       handler[type]->SetPID(req->GetSenderPID());
110       if (!handler[type]->HandleRequest(req->GetData(), req->GetSize(),
111                                         locale_.GetObject()))
112         LOGE("Failed to handle request");
113     } catch (const std::exception& err) {
114       LOGE("Exception occurred (%s)", err.what());
115       pkgmgr_common::parcel::AbstractParcelable parcelable(
116           0, pkgmgr_common::parcel::ParcelableType::Unknown, PMINFO_R_ERROR);
117       tizen_base::Parcel p;
118       p.WriteParcelable(parcelable);
119       std::vector<uint8_t> raw = p.GetRaw();
120       req->SendData(&raw[0], raw.size());
121       continue;
122     } catch (...) {
123       LOGE("Exception occurred");
124       pkgmgr_common::parcel::AbstractParcelable parcelable(
125           0, pkgmgr_common::parcel::ParcelableType::Unknown, PMINFO_R_ERROR);
126       tizen_base::Parcel p;
127       p.WriteParcelable(parcelable);
128       std::vector<uint8_t> raw = p.GetRaw();
129       req->SendData(&raw[0], raw.size());
130       continue;
131     }
132
133     std::vector<uint8_t> result_data = handler[type]->GetResult();
134     req->SendData(result_data.data(), result_data.size());
135   }
136 }
137
138 std::shared_ptr<PkgRequest> WorkerThread::PopQueue() {
139   auto req = queue_.front();
140   queue_.pop();
141   return req;
142 }
143
144 void WorkerThread::SetLocale(std::string locale) {
145   LOGD("Change locale (%s) -> (%s)", locale_.GetObject().c_str(),
146        locale.c_str());
147   locale_.SetObject(std::move(locale));
148 }
149
150 }  // namespace pkgmgr_server