Release version 0.20.13
[platform/core/appfw/slp-pkgmgr.git] / client / src / connector.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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 "connector.hh"
18
19 #include <fcntl.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include <sys/socket.h>
23
24 #include "log.hh"
25
26 #define REGULAR_USER 5000
27
28 namespace {
29
30 constexpr const char SERVER_PROC_NAME[] = "d::org.tizen.appfw.pkgmgr";
31
32 static int _is_system_user(void)
33 {
34   uid_t uid = getuid();
35
36   if (uid < REGULAR_USER)
37     return 1;
38   else
39     return 0;
40 }
41
42 static bool __is_system_type(pkgmgr_client_type type)
43 {
44   if (type == PC_REQUEST || _is_system_user())
45     return true;
46   else
47     return false;
48 }
49
50 }
51
52 namespace pkgmgr {
53 namespace client {
54
55 Connector::~Connector() {
56   if (signal_receiver_)
57     signal_receiver_->StopReceiving();
58 }
59
60 std::string Connector::GenerateRequestId() const {
61   struct timeval tv;
62   gettimeofday(&tv, NULL);
63   long curtime = tv.tv_sec * 1000000 + tv.tv_usec;
64
65   return std::to_string(getpid()) + "_" + std::to_string(curtime);
66 }
67
68 void Connector::SetTep(std::string tep_path, bool tep_move) {
69   tep_path_ = std::move(tep_path);
70   tep_move_ = tep_move;
71 }
72
73 void Connector::SetTepArgs() {
74   argv_.push_back("-e");
75   argv_.push_back(tep_path_);
76   argv_.push_back("-M");
77   argv_.push_back(tep_move_ ? "tep_move" : "tep_copy");
78 }
79
80 void Connector::SetDebugMode() {
81   argv_.push_back("-G");
82 }
83
84 void Connector::SetSkipOptimization() {
85   argv_.push_back("-S");
86 }
87
88 void Connector::SetStatusType(int status_type) {
89   status_type_ = status_type;
90 }
91
92 const std::vector<std::string>& Connector::GetArgv() const {
93   return argv_;
94 }
95
96 pkg_proxy::PkgMgrAdmin* Connector::GetAdminProxy() {
97   if (!ConnectForAdmin())
98     return nullptr;
99
100   return admin_proxy_.get();
101 }
102
103 pkg_proxy::PkgMgr* Connector::GetInfoProxy() {
104   if (!ConnectForInfo())
105     return nullptr;
106
107   return info_proxy_.get();
108 }
109
110 pkg_proxy::PkgMgrForClearCache* Connector::GetCacheProxy() {
111   if (!ConnectForCache())
112     return nullptr;
113
114   return cache_proxy_.get();
115 }
116
117 pkg_proxy::DelayedResult* Connector::GetDelayedResultProxy() {
118   if (!ConnectForDelayedResult())
119     return nullptr;
120
121   return delayed_result_proxy_.get();
122 }
123
124 bool Connector::ConnectForAdmin() {
125   if (!admin_proxy_) {
126     admin_proxy_.reset(new pkg_proxy::PkgMgrAdmin(&conn_admin_listener_,
127         SERVER_PROC_NAME));
128   }
129
130   if (conn_admin_listener_.GetState() == ConnectionState::Connected)
131     return true;
132
133   try {
134     admin_proxy_->Connect(true);
135   } catch (const pkg_proxy::Exception& e) {
136     return false;
137   }
138
139   return true;
140 }
141
142 bool Connector::ConnectForInfo() {
143   if (!info_proxy_) {
144     info_proxy_.reset(new pkg_proxy::PkgMgr(&conn_info_listener_,
145         SERVER_PROC_NAME));
146   }
147
148   if (conn_info_listener_.GetState() == ConnectionState::Connected)
149     return true;
150
151   try {
152     info_proxy_->Connect(true);
153   } catch (const pkg_proxy::Exception& e) {
154     return false;
155   }
156
157   return true;
158 }
159
160 bool Connector::ConnectForCache() {
161   if (!cache_proxy_) {
162     cache_proxy_.reset(new pkg_proxy::PkgMgrForClearCache(&conn_cache_listener_,
163         SERVER_PROC_NAME));
164   }
165
166   if (conn_cache_listener_.GetState() == ConnectionState::Connected)
167     return true;
168
169   try {
170     cache_proxy_->Connect(true);
171   } catch (const pkg_proxy::Exception& e) {
172     return false;
173   }
174
175   return true;
176 }
177
178 bool Connector::ConnectForDelayedResult() {
179   if (!delayed_result_proxy_) {
180     delayed_result_proxy_.reset(new pkg_proxy::DelayedResult(
181         &conn_delayed_result_listener_, SERVER_PROC_NAME));
182   }
183
184   if (conn_delayed_result_listener_.GetState() == ConnectionState::Connected)
185     return true;
186
187   try {
188     delayed_result_proxy_->Connect(true);
189   } catch (const pkg_proxy::Exception& e) {
190     return false;
191   }
192
193   return true;
194 }
195
196 const std::shared_ptr<SignalReceiver>& Connector::GetSignalReceiver() {
197   if (!signal_receiver_)
198     signal_receiver_ = SignalReceiver::Create(
199         __is_system_type(pc_type_), status_type_);
200
201   return signal_receiver_;
202 }
203
204 void Connector::RemoveSignalReceiver() {
205   if (signal_receiver_)
206     signal_receiver_.reset();
207 }
208
209 pkgmgr_client_type Connector::GetPcType() const {
210   return pc_type_;
211 }
212
213 std::vector<std::string>& Connector::GetResRemovePath() {
214   return res_remove_path_;
215 }
216
217 std::vector<std::string>& Connector::GetResCreateDir() {
218   return res_create_dir_;
219 }
220
221 std::vector<pp::ResPath>& Connector::GetResCopyPath() {
222   return res_copy_path_;
223 }
224
225 const std::string& Connector::GetTepPath() {
226   return tep_path_;
227 }
228
229 bool Connector::GetTepMove() {
230   return tep_move_;
231 }
232
233 }  // namespace client
234 }  // namespace pkgmgr