Use socket activation with TIDL
[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() = default;
56
57 std::string Connector::GenerateRequestId() const {
58   struct timeval tv;
59   gettimeofday(&tv, NULL);
60   long curtime = tv.tv_sec * 1000000 + tv.tv_usec;
61
62   return std::to_string(getpid()) + "_" + std::to_string(curtime);
63 }
64
65 void Connector::SetTep(std::string tep_path, bool tep_move) {
66   tep_path_ = std::move(tep_path);
67   tep_move_ = tep_move;
68 }
69
70 void Connector::SetTepArgs() {
71   argv_.push_back("-e");
72   argv_.push_back(tep_path_);
73   argv_.push_back("-M");
74   argv_.push_back(tep_move_ ? "tep_move" : "tep_copy");
75 }
76
77 void Connector::SetDebugMode() {
78   argv_.push_back("-G");
79 }
80
81 void Connector::SetSkipOptimization() {
82   argv_.push_back("-S");
83 }
84
85 const std::vector<std::string>& Connector::GetArgv() const {
86   return argv_;
87 }
88
89 pkg_proxy::PkgMgrAdmin* Connector::GetAdminProxy() {
90   if (!ConnectForAdmin())
91     return nullptr;
92
93   return admin_proxy_.get();
94 }
95
96 pkg_proxy::PkgMgr* Connector::GetInfoProxy() {
97   if (!ConnectForInfo())
98     return nullptr;
99
100   return info_proxy_.get();
101 }
102
103 pkg_proxy::PkgMgrForClearCache* Connector::GetCacheProxy() {
104   if (!ConnectForCache())
105     return nullptr;
106
107   return cache_proxy_.get();
108 }
109
110 pkg_proxy::DelayedResult* Connector::GetDelayedResultProxy() {
111   if (!ConnectForDelayedResult())
112     return nullptr;
113
114   return delayed_result_proxy_.get();
115 }
116
117 bool Connector::ConnectForAdmin() {
118   if (!admin_proxy_) {
119     admin_proxy_.reset(new pkg_proxy::PkgMgrAdmin(&conn_admin_listener_,
120         SERVER_PROC_NAME));
121   }
122
123   if (conn_admin_listener_.GetState() == ConnectionState::Connected)
124     return true;
125
126   try {
127     admin_proxy_->Connect(true);
128   } catch (const pkg_proxy::Exception& e) {
129     return false;
130   }
131
132   return true;
133 }
134
135 bool Connector::ConnectForInfo() {
136   if (!info_proxy_) {
137     info_proxy_.reset(new pkg_proxy::PkgMgr(&conn_info_listener_,
138         SERVER_PROC_NAME));
139   }
140
141   if (conn_info_listener_.GetState() == ConnectionState::Connected)
142     return true;
143
144   try {
145     info_proxy_->Connect(true);
146   } catch (const pkg_proxy::Exception& e) {
147     return false;
148   }
149
150   return true;
151 }
152
153 bool Connector::ConnectForCache() {
154   if (!cache_proxy_) {
155     cache_proxy_.reset(new pkg_proxy::PkgMgrForClearCache(&conn_cache_listener_,
156         SERVER_PROC_NAME));
157   }
158
159   if (conn_cache_listener_.GetState() == ConnectionState::Connected)
160     return true;
161
162   try {
163     cache_proxy_->Connect(true);
164   } catch (const pkg_proxy::Exception& e) {
165     return false;
166   }
167
168   return true;
169 }
170
171 bool Connector::ConnectForDelayedResult() {
172   if (!delayed_result_proxy_) {
173     delayed_result_proxy_.reset(new pkg_proxy::DelayedResult(
174         &conn_delayed_result_listener_, SERVER_PROC_NAME));
175   }
176
177   if (conn_delayed_result_listener_.GetState() == ConnectionState::Connected)
178     return true;
179
180   try {
181     delayed_result_proxy_->Connect(true);
182   } catch (const pkg_proxy::Exception& e) {
183     return false;
184   }
185
186   return true;
187 }
188
189 //const std::unique_ptr<SignalReceiver>& Connector::GetSignalReceiver() {
190 //  if (!signal_receiver_)
191 //    signal_receiver_.reset(new SignalReceiver(__is_system_type(pc_type_)));
192 //
193 //  return signal_receiver_;
194 //}
195
196 pkgmgr_client_type Connector::GetPcType() const {
197   return pc_type_;
198 }
199
200 std::vector<std::string>& Connector::GetResRemovePath() {
201   return res_remove_path_;
202 }
203
204 std::vector<std::string>& Connector::GetResCreateDir() {
205   return res_create_dir_;
206 }
207
208 std::vector<pp::ResPath>& Connector::GetResCopyPath() {
209   return res_copy_path_;
210 }
211
212 const std::string& Connector::GetTepPath() {
213   return tep_path_;
214 }
215
216 bool Connector::GetTepMove() {
217   return tep_move_;
218 }
219
220 pkgmgr_client_t* Connector::GetRawPc() {
221   return raw_pc_;
222 }
223
224 }  // namespace client
225 }  // namespace pkgmgr