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.
18 #include <glib-unix.h>
24 #include <unordered_map>
27 #include "cache_flag.hh"
28 #include "create_cache_request.hh"
29 #include "cynara_checker.hh"
30 #include "pkg_request.hh"
32 #include "utils/logging.hh"
34 #include "pkgmgrinfo_debug.h"
35 #include "pkgmgrinfo_private.h"
40 #define LOG_TAG "PKGMGR_INFO"
42 namespace pkgmgr_server {
45 static const std::string SOCK_PATH = "/run/pkgmgr-info-server";
46 const char PRIVILEGE_PACKAGE_MANAGER_ADMIN[] =
47 "http://tizen.org/privilege/packagemanager.admin";
49 std::vector<std::string> GetPrivileges(pkgmgr_common::ReqType type) {
50 std::vector<std::string> ret;
51 if (type == pkgmgr_common::SET_CERT_INFO)
52 ret.emplace_back(PRIVILEGE_PACKAGE_MANAGER_ADMIN);
53 else if (type == pkgmgr_common::SET_PKG_INFO)
54 ret.emplace_back(PRIVILEGE_PACKAGE_MANAGER_ADMIN);
61 Runner::Runner(unsigned int thread_num) {
62 /* thread_num_ <= hardware_concurrency */
63 thread_num_ = std::min(thread_num, std::thread::hardware_concurrency());
64 CynaraChecker::GetInst().Init();
65 server_ = std::make_unique<pkgmgr_common::socket::ServerSocket>(SOCK_PATH);
66 thread_pool_ = std::make_unique<WorkerThread>(thread_num_);
67 auto condition = static_cast<GIOCondition>(
68 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL);
69 sid_ = g_unix_fd_add(server_->GetFd(), condition, OnReceiveRequest, this);
70 pkgmgr_common::SystemLocale::GetInst().RegisterEvent(this);
71 thread_pool_->SetLocale(pkgmgr_common::SystemLocale::GetInst().Get());
76 g_source_remove(sid_);
77 CynaraChecker::GetInst().Fini();
78 pkgmgr_common::SystemLocale::GetInst().UnRegisterEvent();
81 int Runner::OnReceiveRequest(int fd, GIOCondition cond, void* user_data) {
82 if (static_cast<int>(cond) & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
83 LOG(ERROR) << "Invalid condition fd:" << fd
84 << ", condition:" << static_cast<int>(cond);
86 return G_SOURCE_REMOVE;
89 auto runner = static_cast<Runner*>(user_data);
90 int client_fd = runner->server_->Accept();
92 LOG(ERROR) << "Failed to Accept. errno:" << errno;
93 return G_SOURCE_CONTINUE;
96 auto req = std::make_shared<PkgRequest>(client_fd);
97 if (req->ReceiveData()) {
98 pkgmgr_common::ReqType type = req->GetRequestType();
99 if (CacheFlag::SetPreparing()) {
100 runner->QueueRequest(
101 std::make_shared<CreateCacheRequest>(req->GetSenderUID()));
103 std::vector<std::string>&& privileges = GetPrivileges(type);
104 CynaraChecker::GetInst().CheckPrivilege(runner, req, privileges);
107 return G_SOURCE_CONTINUE;
110 void Runner::OnChanged(const std::string& locale) {
111 thread_pool_->SetLocale(locale);
114 bool Runner::QueueRequest(std::shared_ptr<PkgRequest> req) {
115 thread_pool_->PushQueue(std::move(req));
119 } // namespace pkgmgr_server