2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "worker_thread.hh"
22 #include "abstract_parcelable.hh"
23 #include "command_request_handler.hh"
24 #include "create_cache_request_handler.hh"
25 #include "create_db_request_handler.hh"
26 #include "db_handle_provider.hh"
27 #include "get_appinfo_request_handler.hh"
28 #include "get_cert_request_handler.hh"
29 #include "get_depinfo_request_handler.hh"
30 #include "get_pkginfo_request_handler.hh"
31 #include "query_request_handler.hh"
32 #include "set_cert_request_handler.hh"
33 #include "set_pkginfo_request_handler.hh"
34 #include "utils/logging.hh"
36 #include "pkgmgrinfo_debug.h"
41 #define LOG_TAG "PKGMGR_INFO"
43 #ifndef SQLITE_ENABLE_MEMORY_MANAGEMENT
44 #define SQLITE_ENABLE_MEMORY_MANAGEMENT
47 namespace pkgmgr_server {
49 WorkerThread::WorkerThread(unsigned int num) : stop_all_(false) {
50 threads_.reserve(num);
51 for (unsigned int i = 0; i < num; ++i)
52 threads_.emplace_back([this]() -> void { this->Run(); });
54 LOG(DEBUG) << num << " Worker threads are created";
57 WorkerThread::~WorkerThread() {
61 for (auto& t : threads_)
65 bool WorkerThread::PushQueue(std::shared_ptr<PkgRequest> req) {
67 std::unique_lock<std::mutex> u(lock_);
74 void WorkerThread::Run() {
75 std::unique_ptr<request_handler::AbstractRequestHandler>
76 handler[pkgmgr_common::ReqType::MAX];
77 handler[pkgmgr_common::ReqType::GET_PKG_INFO].reset(
78 new request_handler::GetPkginfoRequestHandler());
79 handler[pkgmgr_common::ReqType::GET_APP_INFO].reset(
80 new request_handler::GetAppinfoRequestHandler());
81 handler[pkgmgr_common::ReqType::SET_PKG_INFO].reset(
82 new request_handler::SetPkginfoRequestHandler());
83 handler[pkgmgr_common::ReqType::SET_CERT_INFO].reset(
84 new request_handler::SetCertRequestHandler());
85 handler[pkgmgr_common::ReqType::GET_CERT_INFO].reset(
86 new request_handler::GetCertRequestHandler());
87 handler[pkgmgr_common::ReqType::GET_PKG_DEP_INFO].reset(
88 new request_handler::GetDepinfoRequestHandler());
89 handler[pkgmgr_common::ReqType::QUERY].reset(
90 new request_handler::QueryRequestHandler());
91 handler[pkgmgr_common::ReqType::COMMAND].reset(
92 new request_handler::CommandRequestHandler());
93 handler[pkgmgr_common::ReqType::CREATE_DB].reset(
94 new request_handler::CreateDBRequestHandler());
95 handler[pkgmgr_common::ReqType::CREATE_CACHE].reset(
96 new request_handler::CreateCacheRequestHandler());
98 LOG(DEBUG) << "Initialize request handlers";
100 std::shared_ptr<PkgRequest> req;
102 std::unique_lock<std::mutex> u(lock_);
103 cv_.wait(u, [this] { return !this->queue_.empty() || stop_all_; });
104 if (stop_all_ && queue_.empty())
109 pkgmgr_common::ReqType type = req->GetRequestType();
110 LOG(WARNING) << "Request type: " << pkgmgr_common::ReqTypeToString(type)
111 << " pid: " << req->GetSenderPID();
112 if (type <= pkgmgr_common::ReqType::REQ_TYPE_NONE
113 || type >= pkgmgr_common::ReqType::MAX) {
114 LOG(ERROR) << "Request type is invalid: " << static_cast<int>(type)
115 << ", pid:" << req->GetSenderPID();
122 handler[type]->SetPID(req->GetSenderPID());
123 if (!handler[type]->HandleRequest(req->GetData(), req->GetSize(),
124 locale_.GetObject()))
125 LOG(ERROR) << "Failed to handle request";
126 } catch (const std::exception& err) {
127 LOG(ERROR) << "Exception occurred: " << err.what()
128 << ", pid: " << req->GetSenderPID();
133 LOG(ERROR) << "Exception occurred pid: " << req->GetSenderPID();
139 std::vector<uint8_t> result_data = handler[type]->ExtractResult();
140 if (req->SendData(result_data.data(), result_data.size()) == false) {
141 LOG(ERROR) << "Failed to send response pid: " << req->GetSenderPID();
144 LOG(WARNING) << "Success response pid: " << req->GetSenderPID();
148 void WorkerThread::SetMemoryTrimTimer() {
149 static guint timer = 0;
151 g_source_remove(timer);
153 timer = g_timeout_add_seconds_full(G_PRIORITY_LOW, 3,
154 TrimMemory, &timer, NULL);
157 gboolean WorkerThread::TrimMemory(void* data) {
158 LOG(DEBUG) << "Trim memory";
159 guint* timer = static_cast<guint*>(data);
160 sqlite3_release_memory(-1);
164 if (database::DBHandleProvider::IsCrashedWriteRequest())
165 database::DBHandleProvider::
166 GetInst(getuid()).UnsetMemoryMode(getpid());
168 database::DBHandleProvider::
169 GetInst(getuid()).TrimCache();
174 std::shared_ptr<PkgRequest> WorkerThread::PopQueue() {
175 SetMemoryTrimTimer();
176 auto req = queue_.front();
181 void WorkerThread::SetLocale(std::string locale) {
182 LOG(DEBUG) << "Change locale : " << locale_.GetObject()
184 locale_.SetObject(std::move(locale));
187 void WorkerThread::SendError(std::shared_ptr<PkgRequest> req) {
188 pkgmgr_common::parcel::AbstractParcelable parcelable(
189 0, pkgmgr_common::parcel::ParcelableType::Unknown, PMINFO_R_ERROR);
190 tizen_base::Parcel p;
191 p.WriteParcelable(parcelable);
192 std::vector<uint8_t> raw = p.GetRaw();
193 req->SendData(&raw[0], raw.size());
196 } // namespace pkgmgr_server