2 * Copyright (c) 2021 - 2022 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 <cpu-boosting.h>
19 #include <glib-unix.h>
26 #include "cache_flag.hh"
27 #include "create_cache_request.hh"
28 #include "cynara_checker.hh"
29 #include "pkg_request.hh"
30 #include "remove_all_cache_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 constexpr const char PRIVILEGE_PACKAGE_MANAGER_ADMIN[] =
47 "http://tizen.org/privilege/packagemanager.admin";
48 constexpr const char DEST_PROCESS_NAME[] = "pkgmgr-info";
52 Runner::Runner(unsigned int thread_num) {
53 /* thread_num_ <= hardware_concurrency */
54 thread_num_ = std::min(thread_num, std::thread::hardware_concurrency());
55 CynaraChecker::GetInst().Init();
56 server_ = std::make_unique<pkgmgr_common::socket::ServerSocket>(SOCK_PATH);
57 thread_pool_ = std::make_unique<WorkerThread>(thread_num_);
59 auto condition = static_cast<GIOCondition>(G_IO_IN);
60 sid_ = g_unix_fd_add(server_->GetFd(), condition, OnReceiveRequest, this);
61 default_uid_ = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
62 pkgmgr_common::SystemLocale::GetInst().RegisterEvent(this);
63 thread_pool_->SetLocale(pkgmgr_common::SystemLocale::GetInst().Get());
65 if (CacheFlag::SetPreparing())
66 QueueRequest(std::make_shared<CreateCacheRequest>(default_uid_, this));
72 g_source_remove(sid_);
73 CynaraChecker::GetInst().Fini();
74 pkgmgr_common::SystemLocale::GetInst().UnRegisterEvent();
77 int Runner::OnReceiveRequest(int fd, GIOCondition cond, void* user_data) {
78 auto runner = static_cast<Runner*>(user_data);
79 int client_fd = runner->server_->Accept();
81 LOG(ERROR) << "Failed to Accept. errno:" << errno;
82 return G_SOURCE_CONTINUE;
85 auto req = std::make_shared<PkgRequest>(client_fd);
87 if (CacheFlag::SetPreparing()) {
89 std::make_shared<CreateCacheRequest>(runner->default_uid_, runner));
91 runner->QueueRequest(std::move(req));
93 return G_SOURCE_CONTINUE;
96 void Runner::OnChanged(const std::string& locale) {
97 thread_pool_->SetLocale(locale);
98 QueueRequest(std::make_shared<RemoveAllCacheRequest>(default_uid_));
101 bool Runner::QueueRequest(std::shared_ptr<PkgRequest> req) {
102 thread_pool_->PushQueue(std::move(req));
106 void Runner::OnCreateCacheDone(bool success) {
107 static bool ready = false;
108 if (success && !ready) {
110 LOG(WARNING) << "pkginfo-server is ready";
112 [](gpointer data) -> gboolean {
113 int fd = creat("/run/.pkginfo_server_ready",
114 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
118 return G_SOURCE_REMOVE;
124 void Runner::SetCPUInheritance() {
126 resource_pid_t resource_st = { 0, };
127 resource_st.pid = getpid();
129 ret = resource_register_cpu_inheritance_destination(
130 DEST_PROCESS_NAME, resource_st);
132 LOG(ERROR) << "Fail to register cpu inheritance destination ret : " << ret;
135 } // namespace pkgmgr_server