2 * Copyright (c) 2018 - 2021 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 <bundle_cpp.h>
18 #include <bundle_internal.h>
21 #include <sys/types.h>
27 #include "app_request.h"
30 #include "include/aul.h"
31 #include "include/aul_app_com.h"
32 #include "include/aul_error.h"
33 #include "include/aul_rpc_port.h"
34 #include "include/aul_sock.h"
35 #include "include/aul_svc.h"
37 using namespace aul::internal;
41 constexpr const char kEndpoint[] = "org.tizen.rpcport";
42 constexpr const char kInterfacePrefix[] = "org.tizen.rpcport._";
43 constexpr const char kPathRunAulRpcPort[] = "/run/aul/rpcport/";
47 WatchInfo(std::string app_id, std::string port_name,
48 aul_rpc_port_appeared_cb appeared_cb,
49 aul_rpc_port_vanished_cb vanished_cb,
50 void* user_data, uid_t uid)
51 : app_id_(std::move(app_id)),
52 port_name_(std::move(port_name)),
53 appeared_cb_(appeared_cb),
54 vanished_cb_(vanished_cb),
55 user_data_(user_data),
62 int ret = aul_app_com_create(kEndpoint, nullptr, AppComCb, this, &conn_);
64 _E("aul_app_com_create() is failed. error(%d)", ret);
71 aul_app_com_leave(conn_);
77 static int AppComCb(const char* endpoint, aul_app_com_result_e result,
78 bundle* envelope, void* user_data) {
79 auto* info = static_cast<WatchInfo*>(user_data);
80 tizen_base::Bundle b(envelope, false, false);
81 std::string app_id = b.GetString(AUL_K_APPID);
82 if (info->app_id_ != app_id)
85 std::string port_name = b.GetString(AUL_K_RPC_PORT);
86 if (info->port_name_ != port_name)
89 std::string uid_str = b.GetString(AUL_K_CALLER_UID);
91 static_cast<uid_t>(std::stoul(uid_str, nullptr, 10) & UINT32_MAX);
92 if (info->uid_ != uid)
95 std::string pid_str = b.GetString(AUL_K_PID);
96 int pid = std::stoi(pid_str);
97 std::string event_name = b.GetString(AUL_K_EVENT_NAME);
98 if (event_name == "Appeared") {
99 info->appeared_cb_(app_id.c_str(), port_name.c_str(), pid,
102 info->vanished_cb_(app_id.c_str(), port_name.c_str(), pid,
111 std::string port_name_;
112 aul_rpc_port_appeared_cb appeared_cb_;
113 aul_rpc_port_vanished_cb vanished_cb_;
116 aul_app_com_connection_h conn_ = nullptr;
119 std::string GetInterfaceName(const std::string& app_id,
120 const std::string& port_name, uid_t uid) {
121 std::string name = kInterfacePrefix + app_id + "_" + port_name;
122 char* checksum = g_compute_checksum_for_string(G_CHECKSUM_SHA1,
123 name.c_str(), name.length());
124 std::unique_ptr<char, decltype(g_free)*> checksum_auto(checksum, g_free);
125 return std::to_string(uid) + "@" + std::string(checksum);
128 std::string GetPath(const std::string& interface_name) {
129 return std::string(kPathRunAulRpcPort) + "." + interface_name;
134 extern "C" API int aul_rpc_port_prepare_stub(const char* app_id,
135 const char* port_name) {
136 return aul_rpc_port_usr_prepare_stub(app_id, port_name, getuid());
139 extern "C" API int aul_rpc_port_usr_prepare_stub(const char* app_id,
140 const char* port_name, uid_t uid) {
141 if (app_id == nullptr || port_name == nullptr) {
142 _E("Invalid parameter");
146 tizen_base::Bundle b {{AUL_K_RPC_PORT, port_name}};
147 int ret = AppRequest(RPC_PORT_PREPARE_STUB, uid)
150 .SendSimply(AUL_SOCK_QUEUE);
152 _E("Failed to send request. app_id(%s), port_name(%s)", app_id, port_name);
159 extern "C" API int aul_rpc_port_create_socket_pair(const char* app_id,
160 const char* port_name, int (*fds)[2]) {
161 if (app_id == nullptr || port_name == nullptr || fds == nullptr) {
162 _E("Invalid parameter");
166 tizen_base::Bundle b {{AUL_K_RPC_PORT, port_name}};
167 int fd = AppRequest(RPC_PORT_CREATE_SOCKET_PAIR, getuid())
170 .SendSimply(AUL_SOCK_ASYNC);
171 if (fd <= 0 || fd >= sysconf(_SC_OPEN_MAX)) {
172 _E("Failed to send socket pair creation request. error(%d)", fd);
176 int ret = aul_sock_recv_reply_sock_fd(fd, fds, 2);
178 _E("Failed to receive socket fds. error(%d)", ret);
179 return aul_error_convert(ret);
185 extern "C" API int aul_rpc_port_notify_rpc_finished(void) {
186 int ret = AppRequest(RPC_PORT_NOTIFY_RPC_FINISHED, getuid())
188 .SendSimply(AUL_SOCK_NOREPLY);
190 _E("Failed to notify rpc finished. error(%d)", ret);
197 extern "C" API int aul_rpc_port_set_private_sharing(const char* app_id,
198 const char* paths[], unsigned int size) {
199 if (app_id == nullptr || paths == nullptr) {
200 _E("Invalid parameter");
204 tizen_base::Bundle b {{AUL_K_CALLEE_APPID, app_id}};
205 std::vector<std::string> v;
206 for (unsigned int i = 0; i < size; ++i)
207 v.push_back(paths[i]);
208 b.Add(AUL_SVC_DATA_PATH, v);
210 int ret = AppRequest(SET_PRIVATE_SHARING, getuid())
213 .SendSimply(AUL_SOCK_NOREPLY);
215 _E("Failed to set private sharing. error(%d)", ret);
222 extern "C" API int aul_rpc_port_unset_private_sharing(const char* app_id) {
223 if (app_id == nullptr) {
224 _E("Invalid parameter");
228 tizen_base::Bundle b {{AUL_K_CALLEE_APPID, app_id}};
229 int ret = AppRequest(UNSET_PRIVATE_SHARING, getuid())
232 .SendSimply(AUL_SOCK_NOREPLY);
234 _E("Failed to unset private sharing. error(%d)", ret);
241 extern "C" API int aul_rpc_port_create(const char* port_name, int* fd) {
242 return aul_rpc_port_usr_create(port_name, getuid(), fd);
245 extern "C" API int aul_rpc_port_usr_create(const char* port_name, uid_t uid,
247 if (port_name == nullptr || fd == nullptr) {
248 _E("Invalid paramter");
252 tizen_base::Bundle b {{AUL_K_RPC_PORT, port_name}};
253 int req_fd = AppRequest(RPC_PORT_CREATE, uid)
255 .SendSimply(AUL_SOCK_ASYNC);
256 if (req_fd <= 0 || req_fd >= sysconf(_SC_OPEN_MAX)) {
257 _E("Failed to send socket creation request. error(%d)", req_fd);
261 int fds[2] = { -1, };
262 int ret = aul_sock_recv_reply_sock_fd(req_fd, &fds, 1);
264 _E("Failed to receive socket fds. error(%d)", ret);
265 return aul_error_convert(ret);
272 extern "C" API int aul_rpc_port_destroy(const char* port_name) {
273 return aul_rpc_port_usr_destroy(port_name, getuid());
276 extern "C" API int aul_rpc_port_usr_destroy(const char* port_name, uid_t uid) {
277 if (port_name == nullptr) {
278 _E("Invalid parameter");
282 tizen_base::Bundle b {{AUL_K_RPC_PORT, port_name}};
283 int ret = AppRequest(RPC_PORT_DESTROY, uid)
285 .SendSimply(AUL_SOCK_NOREPLY);
287 _E("Failed to send socket desctruction request. error(%d)", ret);
294 extern "C" API int aul_rpc_port_add_watch(const char* app_id,
295 const char* port_name, aul_rpc_port_appeared_cb appeared_cb,
296 aul_rpc_port_vanished_cb vanished_cb, void* user_data,
297 aul_rpc_port_watch_h* handle) {
298 return aul_rpc_port_usr_add_watch(app_id, port_name, appeared_cb, vanished_cb,
299 user_data, getuid(), handle);
302 extern "C" API int aul_rpc_port_usr_add_watch(const char* app_id,
303 const char* port_name, aul_rpc_port_appeared_cb appeared_cb,
304 aul_rpc_port_vanished_cb vanished_cb, void* user_data, uid_t uid,
305 aul_rpc_port_watch_h* handle) {
306 if (app_id == nullptr || port_name == nullptr || appeared_cb == nullptr ||
307 vanished_cb == nullptr || handle == nullptr) {
308 _E("Invalid parameter");
312 auto* info = new (std::nothrow) WatchInfo(app_id, port_name, appeared_cb,
313 vanished_cb, user_data, uid);
314 if (info == nullptr) {
319 int ret = info->Watch();
320 if (ret != AUL_R_OK) {
325 *handle = static_cast<aul_rpc_port_watch_h>(info);
329 extern "C" API int aul_rpc_port_remove_watch(aul_rpc_port_watch_h handle) {
330 if (handle == nullptr) {
331 _E("Invalid parameter");
335 auto* info = static_cast<WatchInfo*>(handle);
341 extern "C" API int aul_rpc_port_exist(const char* app_id, const char* port_name,
343 return aul_rpc_port_usr_exist(app_id, port_name, getuid(), exist);
346 extern "C" API int aul_rpc_port_usr_exist(const char* app_id,
347 const char* port_name, uid_t uid, bool* exist) {
348 if (app_id == nullptr || port_name == nullptr || exist == nullptr) {
349 _E("Invalid parameter");
354 if (aul_rpc_port_usr_get_path(app_id, port_name, uid, &port_path) != AUL_R_OK)
357 int ret = access(port_path, F_OK);
359 *exist = (ret == 0) ? true : false;
363 extern "C" API int aul_rpc_port_get_path(const char* app_id,
364 const char* port_name, char** path) {
365 return aul_rpc_port_usr_get_path(app_id, port_name, getuid(), path);
368 extern "C" API int aul_rpc_port_usr_get_path(const char* app_id,
369 const char* port_name, uid_t uid, char** path) {
370 if (app_id == nullptr || port_name == nullptr || path == nullptr) {
371 _E("Invalid parameter");
375 std::string interface_name = GetInterfaceName(app_id, port_name, uid);
376 std::string port_path = GetPath(interface_name);
377 struct sockaddr_un addr = { 0, };
378 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", port_path.c_str());
379 *path = strdup(addr.sun_path);
380 if (*path == nullptr) {