4e4589b7dcf33182906dba4284eccea1972578fc
[platform/core/appfw/pkgmgr-info.git] / src / server / runner.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 <fcntl.h>
18 #include <glib-unix.h>
19 #include <stdlib.h>
20 #include <vconf.h>
21
22 #include <algorithm>
23 #include <string>
24
25 #include "cache_flag.hh"
26 #include "create_cache_request.hh"
27 #include "cynara_checker.hh"
28 #include "pkg_request.hh"
29 #include "runner.hh"
30 #include "utils/logging.hh"
31
32 #include "pkgmgrinfo_debug.h"
33 #include "pkgmgrinfo_private.h"
34
35 #ifdef LOG_TAG
36 #undef LOG_TAG
37 #endif
38 #define LOG_TAG "PKGMGR_INFO"
39
40 namespace pkgmgr_server {
41 namespace {
42
43 static const std::string SOCK_PATH = "/run/pkgmgr-info-server";
44 const char PRIVILEGE_PACKAGE_MANAGER_ADMIN[] =
45     "http://tizen.org/privilege/packagemanager.admin";
46
47 }  // namespace
48
49 Runner::Runner(unsigned int thread_num) {
50   /* thread_num_ <= hardware_concurrency */
51   thread_num_ = std::min(thread_num, std::thread::hardware_concurrency());
52   CynaraChecker::GetInst().Init();
53   server_ = std::make_unique<pkgmgr_common::socket::ServerSocket>(SOCK_PATH);
54   thread_pool_ = std::make_unique<WorkerThread>(thread_num_);
55   auto condition = static_cast<GIOCondition>(G_IO_IN);
56   sid_ = g_unix_fd_add(server_->GetFd(), condition, OnReceiveRequest, this);
57   pkgmgr_common::SystemLocale::GetInst().RegisterEvent(this);
58   thread_pool_->SetLocale(pkgmgr_common::SystemLocale::GetInst().Get());
59
60   if (CacheFlag::SetPreparing()) {
61     QueueRequest(
62         std::make_shared<CreateCacheRequest>(
63             tzplatform_getuid(TZ_SYS_DEFAULT_USER)));
64   }
65   LOGI("Start Runner");
66 }
67
68 Runner::~Runner() {
69   g_source_remove(sid_);
70   CynaraChecker::GetInst().Fini();
71   pkgmgr_common::SystemLocale::GetInst().UnRegisterEvent();
72 }
73
74 int Runner::OnReceiveRequest(int fd, GIOCondition cond, void* user_data) {
75   auto runner = static_cast<Runner*>(user_data);
76   int client_fd = runner->server_->Accept();
77   if (client_fd < 0) {
78     LOG(ERROR) << "Failed to Accept. errno:" << errno;
79     return G_SOURCE_CONTINUE;
80   }
81
82   auto req = std::make_shared<PkgRequest>(client_fd);
83
84   if (CacheFlag::SetPreparing()) {
85     runner->QueueRequest(
86         std::make_shared<CreateCacheRequest>(req->GetSenderUID()));
87   }
88   runner->QueueRequest(std::move(req));
89
90   return G_SOURCE_CONTINUE;
91 }
92
93 void Runner::OnChanged(const std::string& locale) {
94   thread_pool_->SetLocale(locale);
95 }
96
97 bool Runner::QueueRequest(std::shared_ptr<PkgRequest> req) {
98   thread_pool_->PushQueue(std::move(req));
99   return true;
100 }
101
102 }  // namespace pkgmgr_server