2d29a93cde178d3cdd01a7eb53b80bf87438be75
[platform/core/appfw/pkgmgr-info.git] / src / server / runner.cc
1 /*
2  * Copyright (c) 2021 - 2022 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 <cpu-boosting.h>
18 #include <fcntl.h>
19 #include <glib-unix.h>
20 #include <stdlib.h>
21 #include <vconf.h>
22
23 #include <algorithm>
24 #include <string>
25
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"
31 #include "runner.hh"
32 #include "utils/logging.hh"
33
34 #include "pkgmgrinfo_debug.h"
35 #include "pkgmgrinfo_private.h"
36
37 #ifdef LOG_TAG
38 #undef LOG_TAG
39 #endif
40 #define LOG_TAG "PKGMGR_INFO"
41
42 namespace pkgmgr_server {
43 namespace {
44
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";
49
50 }  // namespace
51
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_);
58   SetCPUInheritance();
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   pkgmgr_common::DbChangeObserver::GetInst().Listen();
64   pkgmgr_common::DbChangeObserver::GetInst().RegisterEvent(this);
65   thread_pool_->SetLocale(pkgmgr_common::SystemLocale::GetInst().Get());
66
67   if (CacheFlag::SetPreparing())
68     QueueRequest(std::make_shared<CreateCacheRequest>(default_uid_, this));
69
70   LOGI("Start Runner");
71 }
72
73 Runner::~Runner() {
74   g_source_remove(sid_);
75   if (timer_ > 0)
76     g_source_remove(timer_);
77   CynaraChecker::GetInst().Fini();
78   pkgmgr_common::SystemLocale::GetInst().UnRegisterEvent();
79   pkgmgr_common::DbChangeObserver::GetInst().UnRegisterEvent();
80 }
81
82 int Runner::OnReceiveRequest(int fd, GIOCondition cond, void* user_data) {
83   auto runner = static_cast<Runner*>(user_data);
84   int client_fd = runner->server_->Accept();
85   if (client_fd < 0) {
86     LOG(ERROR) << "Failed to Accept. errno:" << errno;
87     return G_SOURCE_CONTINUE;
88   }
89
90   auto req = std::make_shared<PkgRequest>(client_fd);
91
92   if (CacheFlag::SetPreparing()) {
93     runner->QueueRequest(
94         std::make_shared<CreateCacheRequest>(runner->default_uid_, runner));
95   }
96   runner->QueueRequest(std::move(req));
97
98   return G_SOURCE_CONTINUE;
99 }
100
101 void Runner::OnChanged(const std::string& locale) {
102   thread_pool_->SetLocale(locale);
103   QueueRequest(std::make_shared<RemoveAllCacheRequest>(default_uid_));
104 }
105
106 void Runner::OnDbChanged() {
107   QueueRequest(std::make_shared<RemoveAllCacheRequest>(default_uid_));
108   SetCreateCacheTimer();
109 }
110
111 bool Runner::QueueRequest(std::shared_ptr<PkgRequest> req) {
112   thread_pool_->PushQueue(std::move(req));
113   return true;
114 }
115
116 void Runner::OnCreateCacheDone(bool success) {
117   static bool ready = false;
118   if (ready)
119     return;
120
121   if (success) {
122     ready = true;
123     LOG(WARNING) << "pkginfo-server is ready";
124     g_idle_add(
125         [](gpointer data) -> gboolean {
126           int fd = creat("/run/.pkginfo_server_ready",
127                          S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
128           if (fd != -1)
129             close(fd);
130
131           return G_SOURCE_REMOVE;
132         },
133         nullptr);
134   } else {
135     LOG(WARNING) << "Fail to create cache";
136     SetCreateCacheTimer();
137   }
138 }
139
140 void Runner::SetCPUInheritance() {
141   int ret;
142   resource_pid_t resource_st = { 0, };
143   resource_st.pid = getpid();
144
145   ret = resource_register_cpu_inheritance_destination(
146       DEST_PROCESS_NAME, resource_st);
147   if (ret != 0)
148     LOG(ERROR) << "Fail to register cpu inheritance destination ret : " << ret;
149 }
150
151 void Runner::SetCreateCacheTimer() {
152   if (timer_ > 0)
153     g_source_remove(timer_);
154
155   timer_ = g_timeout_add_seconds_full(G_PRIORITY_LOW, 10,
156       RetryCreateCache, this, NULL);
157 }
158
159 gboolean Runner::RetryCreateCache(void* data) {
160   LOG(WARNING) << "Retry to create cache";
161   auto* runner = static_cast<Runner*>(data);
162   runner->timer_ = 0;
163
164   pkgmgr_common::DbChangeObserver::GetInst().SetChanged(false);
165   pkgmgr_common::DbChangeObserver::GetInst().Listen();
166
167   if (CacheFlag::SetPreparing()) {
168     runner->QueueRequest(
169         std::make_shared<CreateCacheRequest>(runner->default_uid_, runner));
170   }
171   return G_SOURCE_REMOVE;
172 }
173
174 }  // namespace pkgmgr_server