3 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * @file message-port.cpp
20 * @brief This is the implementation file for the MessagePort.
23 #include <sys/socket.h>
29 #include <openssl/md5.h>
31 #include <bundle_internal.h>
32 #include <pkgmgr-info.h>
35 #include <gio/gunixfdlist.h>
37 #include "message-port.h"
38 #include "message-port-log.h"
40 #define MAX_PACKAGE_STR_SIZE 512
41 #define MESSAGEPORT_BUS_NAME_PREFIX "org.tizen.messageport._"
42 #define MESSAGEPORT_OBJECT_PATH "/org/tizen/messageport"
43 #define MESSAGEPORT_INTERFACE_PREFIX "org.tizen.messageport._"
45 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
46 #define DBUS_PATH_DBUS "/org/freedesktop/DBus"
47 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
49 #define DBUS_RELEASE_NAME_REPLY_RELEASED 1 /* *< Service was released from the given name */
50 #define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT 2 /* *< The given name does not exist on the bus */
51 #define DBUS_RELEASE_NAME_REPLY_NOT_OWNER 3 /* *< Service is not an owner of the given name */
53 #define MAX_RETRY_CNT 2
54 #define SOCK_PAIR_SENDER 0
55 #define SOCK_PAIR_RECEIVER 1
58 #define retvm_if(expr, val, fmt, arg...) do { \
61 _LOGE("(%s) -> %s() return", #expr, __func__); \
66 #define retv_if(expr, val) do { \
68 _LOGE("(%s) -> %s() return", #expr, __func__); \
73 #define FREE_AND_NULL(ptr) do { \
80 static bool _initialized = false;
81 static GDBusConnection *__gdbus_conn;
82 static char *__app_id;
83 static GHashTable *__local_port_info;
84 static GHashTable *__remote_app_info;
85 static GHashTable *__sender_appid_hash;
86 static GHashTable *__trusted_app_list_hash;
87 static GHashTable *__callback_info_hash;
88 static const int MAX_MESSAGE_SIZE = 16 * 1024;
90 enum __certificate_info_type {
93 CERTIFICATE_NOT_MATCH,
96 typedef struct message_port_pkt {
97 int remote_port_name_len;
98 char *remote_port_name;
103 } message_port_pkt_s;
105 typedef struct message_port_callback_info {
106 messageport_message_cb callback;
109 GIOChannel *gio_read;
111 } message_port_callback_info_s;
113 typedef struct message_port_local_port_info {
114 messageport_message_cb callback;
118 } message_port_local_port_info_s;
120 typedef struct message_port_remote_port_info {
123 int certificate_info;
125 } message_port_remote_app_info_s;
127 typedef struct port_list_info {
129 char *encoded_bus_name;
136 static void __callback_info_free(gpointer data)
138 message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
139 GError *error = NULL;
140 if (callback_info == NULL)
143 if (callback_info->remote_app_id)
144 free(callback_info->remote_app_id);
146 if (callback_info->gio_read != NULL) {
147 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
149 _LOGE("g_io_channel_shutdown error : %s", error->message);
152 g_io_channel_unref(callback_info->gio_read);
153 callback_info->gio_read = NULL;
156 if (callback_info->g_src_id != 0) {
157 g_source_remove(callback_info->g_src_id);
158 callback_info->g_src_id = 0;
164 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
167 GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
170 if (callback_info_list == NULL)
173 find_list = g_list_find(callback_info_list, callback_info);
174 if (find_list == NULL)
177 callback_info_list = g_list_remove_link(callback_info_list, find_list);
178 __callback_info_free(callback_info);
181 static void __hash_destroy_callback_info(gpointer data)
184 GList *callback_list = (GList *)data;
185 if (callback_list != NULL)
186 g_list_free_full(callback_list, __callback_info_free);
189 static char *__get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
192 int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
194 char *postfix = is_trusted ? "1" : "0";
196 unsigned char c[MD5_DIGEST_LENGTH] = {0};
197 char *md5_interface = NULL;
201 int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
202 int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
203 char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
204 if (bus_name == NULL) {
205 _LOGE("bus_name calloc failed");
209 snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
211 MD5_Init(&mdContext);
212 MD5_Update(&mdContext, bus_name, bus_name_len);
213 MD5_Final(c, &mdContext);
215 md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
216 if (md5_interface == NULL) {
220 _LOGE("md5_interface calloc failed!!");
224 snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
225 temp = md5_interface;
228 for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
229 snprintf(temp, 3, "%02x", c[index]);
233 if (postfix && postfix_len > 0)
234 snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
238 _LOGI("encoded_bus_name : %s ", md5_interface);
240 return md5_interface;
243 static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
245 port_list_info_s *key1 = (port_list_info_s *)a;
246 port_list_info_s *key2 = (port_list_info_s *)b;
248 if (key1->is_trusted == key2->is_trusted)
249 return strcmp(key1->port_name, key2->port_name);
255 static bool __is_preloaded(const char *local_appid, const char *remote_appid)
257 _LOGI("IsPreloaded");
259 bool preload_local = false;
260 bool preload_remote = false;
262 pkgmgrinfo_appinfo_h handle = NULL;
263 int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
264 if (ret != PMINFO_R_OK) {
265 _LOGE("Failed to get the appinfo. %d", ret);
266 pkgmgrinfo_appinfo_destroy_appinfo(handle);
269 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
270 if (ret != PMINFO_R_OK) {
271 _LOGE("Failed to check the preloaded application. %d", ret);
272 pkgmgrinfo_appinfo_destroy_appinfo(handle);
275 ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
276 if (ret != PMINFO_R_OK) {
277 _LOGE("Failed to get the appinfo. %d", ret);
278 pkgmgrinfo_appinfo_destroy_appinfo(handle);
281 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
282 if (ret != PMINFO_R_OK) {
283 _LOGE("Failed to check the preloaded application. %d", ret);
284 pkgmgrinfo_appinfo_destroy_appinfo(handle);
288 if (preload_local && preload_remote) {
289 pkgmgrinfo_appinfo_destroy_appinfo(handle);
292 pkgmgrinfo_appinfo_destroy_appinfo(handle);
296 static int __check_certificate(const char *local_appid, const char *remote_appid)
298 _LOGI("CheckCertificate");
300 pkgmgrinfo_cert_compare_result_type_e res;
301 int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
303 _LOGE(":CheckCertificate() Failed");
304 return MESSAGEPORT_ERROR_IO_ERROR;
306 if (res != PMINFO_CERT_COMPARE_MATCH) {
307 _LOGE("CheckCertificate() Failed : MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH");
308 return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
311 return MESSAGEPORT_ERROR_NONE;
314 static void on_name_appeared(GDBusConnection *connection,
316 const gchar *name_owner,
319 _LOGI("name appeared : %s %s", __app_id, name);
322 static void on_name_vanished(GDBusConnection *connection,
326 _LOGI("name vanished : %s", name);
327 port_list_info_s *pli = (port_list_info_s *)user_data;
328 g_bus_unwatch_name(pli->watcher_id);
331 _LOGI("name vanished socket : %d", pli->send_sock_fd);
332 if (pli->send_sock_fd > 0) {
333 close(pli->send_sock_fd);
334 pli->send_sock_fd = 0;
338 static int __get_local_port_info(int id, message_port_local_port_info_s **info)
340 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
343 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
346 return MESSAGEPORT_ERROR_NONE;
349 static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
351 int ret_val = MESSAGEPORT_ERROR_NONE;
352 port_list_info_s *port_info = (port_list_info_s *)calloc(1, sizeof(port_list_info_s));
355 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
358 port_info->port_name = strdup(remote_port);
359 if (!port_info->port_name) {
360 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
363 port_info->is_trusted = is_trusted;
364 port_info->encoded_bus_name = __get_encoded_name(remote_app_id, remote_port, is_trusted);
365 if (port_info->encoded_bus_name == NULL) {
366 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
369 port_info->send_sock_fd = 0;
371 if (ret_val != MESSAGEPORT_ERROR_NONE) {
373 FREE_AND_NULL(port_info->port_name);
374 FREE_AND_NULL(port_info->encoded_bus_name);
382 static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
384 message_port_remote_app_info_s *remote_app_info = NULL;
385 int ret_val = MESSAGEPORT_ERROR_NONE;
387 remote_app_info = (message_port_remote_app_info_s *)calloc(1, sizeof(message_port_remote_app_info_s));
388 if (!remote_app_info) {
389 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
393 remote_app_info->remote_app_id = strdup(remote_app_id);
394 if (remote_app_info->remote_app_id == NULL) {
395 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;;
400 if (ret_val != MESSAGEPORT_ERROR_NONE) {
401 if (remote_app_info) {
402 FREE_AND_NULL(remote_app_info->remote_app_id);
403 FREE_AND_NULL(remote_app_info);
407 return remote_app_info;
410 static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
411 message_port_remote_app_info_s **mri, port_list_info_s **pli)
413 message_port_remote_app_info_s *remote_app_info = NULL;
414 port_list_info_s port_info;
415 GList *cb_list = NULL;
416 int ret_val = MESSAGEPORT_ERROR_NONE;
418 remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
420 if (remote_app_info == NULL) {
421 remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
423 if (remote_app_info == NULL) {
424 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
427 g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
429 *mri = remote_app_info;
431 port_info.port_name = strdup(remote_port);
432 port_info.is_trusted = is_trusted;
433 cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
434 (GCompareFunc)__remote_port_compare_cb);
435 if (port_info.port_name)
436 free(port_info.port_name);
437 if (cb_list == NULL) {
438 port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted);
440 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
443 remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
446 *pli = (port_list_info_s *)cb_list->data;
448 if ((*pli)->watcher_id < 1) {
449 LOGI("watch remote port : %s", (*pli)->encoded_bus_name);
450 (*pli)->watcher_id = g_bus_watch_name_on_connection(
452 (*pli)->encoded_bus_name,
453 G_BUS_NAME_WATCHER_FLAGS_NONE,
464 static bool __is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
469 g_hash_table_iter_init(&iter, __local_port_info);
470 while (g_hash_table_iter_next(&iter, &key, &value)) {
471 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
473 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
474 *local_id = mi->local_id;
483 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
485 GDBusMessage *msg = NULL;
486 GDBusMessage *reply = NULL;
491 msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
492 "org.freedesktop.DBus", "GetConnectionUnixProcessID");
494 _LOGE("Can't allocate new method call");
498 g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
499 reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
500 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
504 _LOGE("Failed to get pid [%s]", err->message);
510 body = g_dbus_message_get_body(reply);
511 g_variant_get(body, "(u)", &pid);
517 g_object_unref(reply);
522 static int __write_socket(int fd,
525 unsigned int *bytes_write)
527 unsigned int left = nbytes;
532 while (left && (retry_cnt < MAX_RETRY_CNT)) {
533 nb = write(fd, buffer, left);
535 if (errno == EINTR) {
536 LOGE("__write_socket: EINTR error continue ...");
540 LOGE("__write_socket: ...error fd %d: errno %d\n", fd, errno);
541 return MESSAGEPORT_ERROR_IO_ERROR;
549 return MESSAGEPORT_ERROR_NONE;
552 static int __write_string_to_socket(int fd, const char *buffer, int string_len)
555 if (__write_socket(fd, (char *)&string_len, sizeof(string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
556 _LOGE("write string_len fail");
557 return MESSAGEPORT_ERROR_IO_ERROR;
560 if (string_len > 0) {
561 if (__write_socket(fd, buffer, string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
562 _LOGE("wirte buffer fail");
563 return MESSAGEPORT_ERROR_IO_ERROR;
566 return MESSAGEPORT_ERROR_NONE;
569 static int __read_socket(int fd,
572 unsigned int *bytes_read)
574 unsigned int left = nbytes;
579 while (left && (retry_cnt < MAX_RETRY_CNT)) {
580 nb = read(fd, buffer, left);
582 LOGE("__read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
583 return MESSAGEPORT_ERROR_IO_ERROR;
584 } else if (nb == -1) {
585 if (errno == EINTR) {
586 LOGE("__read_socket: EINTR error continue ...");
590 LOGE("__read_socket: ...error fd %d: errno %d\n", fd, errno);
591 return MESSAGEPORT_ERROR_IO_ERROR;
599 return MESSAGEPORT_ERROR_NONE;
602 static int __read_string_from_socket(int fd, char **buffer, int *string_len)
605 if (__read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
606 LOGE("read socket fail");
607 return MESSAGEPORT_ERROR_IO_ERROR;
609 if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
610 *buffer = (char *)calloc(*string_len, sizeof(char));
611 if (*buffer == NULL) {
612 LOGE("Out of memory.");
613 return MESSAGEPORT_ERROR_IO_ERROR;
615 if (__read_socket(fd, *buffer, *string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
616 LOGE("read socket fail");
617 return MESSAGEPORT_ERROR_IO_ERROR;
620 LOGE("Invalid string len %d", &string_len);
621 return MESSAGEPORT_ERROR_IO_ERROR;
623 return MESSAGEPORT_ERROR_NONE;
626 message_port_pkt_s *__message_port_recv_raw(int fd)
628 message_port_pkt_s *pkt = NULL;
631 pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
637 if (__read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGEPORT_ERROR_NONE) {
638 LOGE("read socket fail: port_name");
639 free(pkt->remote_port_name);
644 if (__read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
645 LOGE("read socket fail: is_bidirection");
646 free(pkt->remote_port_name);
651 if (__read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
652 LOGE("read socket fail: is_trusted");
653 free(pkt->remote_port_name);
658 if (__read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGEPORT_ERROR_NONE) {
659 LOGE("read socket fail: data");
660 free(pkt->remote_port_name);
668 static gboolean __socket_request_handler(GIOChannel *gio,
673 message_port_callback_info_s *mi;
674 message_port_pkt_s *pkt;
676 GError *error = NULL;
678 mi = (message_port_callback_info_s *)data;
681 g_io_channel_shutdown(gio, TRUE, &error);
683 _LOGE("g_io_channel_shutdown error : %s", error->message);
686 g_io_channel_unref(gio);
690 if (cond == G_IO_HUP) {
692 _LOGI("socket G_IO_HUP");
693 __callback_info_free_by_info(mi);
698 if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
699 _LOGE("fail to get fd from io channel");
700 __callback_info_free_by_info(mi);
704 if ((pkt = __message_port_recv_raw(fd)) == NULL) {
705 _LOGE("recv error on SOCKET");
706 __callback_info_free_by_info(mi);
710 kb = bundle_decode(pkt->data, pkt->data_len);
711 if (pkt->is_bidirection)
712 mi->callback(mi->local_id, mi->remote_app_id, pkt->remote_port_name, pkt->is_trusted, kb, NULL);
714 mi->callback(mi->local_id, mi->remote_app_id, NULL, pkt->is_trusted, kb, NULL);
718 if (pkt->remote_port_name)
719 free(pkt->remote_port_name);
729 static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation)
731 char *local_port = NULL;
732 char *local_appid = NULL;
733 char *remote_appid = NULL;
734 char *remote_port = NULL;
735 gboolean local_trusted = false;
736 gboolean remote_trusted = false;
737 gboolean bi_dir = false;
741 bundle_raw *raw = NULL;
742 message_port_local_port_info_s *mi;
743 int local_reg_id = 0;
744 message_port_callback_info_s *callback_info;
745 GList *callback_info_list = NULL;
749 GUnixFDList *fd_list;
751 int *returned_fds = NULL;
754 g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
755 &remote_appid, &remote_port, &remote_trusted, &len, &raw);
758 _LOGE("Invalid argument : remote_port is NULL");
762 _LOGE("Invalid argument : remote_appid is NULL");
765 if (!__is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
766 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
770 _LOGE("Invalid argument : local_appid");
774 _LOGE("Invalid argument : local_port");
777 if (strcmp(remote_appid, __app_id) != 0) {
778 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
781 if (strcmp(remote_port, mi->port_name) != 0) {
782 _LOGE("Invalid argument : remote_port (%s)", remote_port);
786 _LOGE("Invalid argument : data_len");
789 if (remote_trusted) {
790 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
791 if (!__is_preloaded(local_appid, remote_appid)) {
792 int ret = __check_certificate(local_appid, remote_appid);
793 if (ret == MESSAGEPORT_ERROR_NONE)
794 g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
796 _LOGE("The application (%s) is not signed with the same certificate",
804 callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
805 if (callback_info == NULL)
808 callback_info->local_id = mi->local_id;
809 callback_info->remote_app_id = strdup(local_appid);
810 callback_info->callback = mi->callback;
812 msg = g_dbus_method_invocation_get_message(invocation);
813 fd_list = g_dbus_message_get_unix_fd_list(msg);
815 /* When application send message to self fd_list is NULL */
816 if (fd_list != NULL) {
817 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
818 if (returned_fds == NULL) {
819 _LOGE("fail to get fds");
820 __callback_info_free(callback_info);
823 fd = returned_fds[0];
825 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
828 callback_info->gio_read = g_io_channel_unix_new(fd);
829 if (!callback_info->gio_read) {
830 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
831 __callback_info_free(callback_info);
835 callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
836 __socket_request_handler, (gpointer)callback_info);
837 if (callback_info->g_src_id == 0) {
838 _LOGE("fail to add watch on socket");
839 __callback_info_free(callback_info);
843 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
844 if (callback_info_list == NULL) {
845 callback_info_list = g_list_append(callback_info_list, callback_info);
846 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
848 callback_info_list = g_list_append(callback_info_list, callback_info);
853 data = bundle_decode(raw, len);
855 _LOGE("Invalid argument : message");
859 LOGI("call calback %s", local_appid);
861 mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
863 mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
872 static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
874 _LOGI("Check a remote port : [%s:%s]", remote_app_id, remote_port);
876 GVariant *result = NULL;
878 int ret_val = MESSAGEPORT_ERROR_NONE;
879 char *bus_name = NULL;
880 message_port_remote_app_info_s *remote_app_info = NULL;
881 port_list_info_s *port_info = NULL;
882 int local_reg_id = 0;
883 message_port_local_port_info_s *mi = NULL;
884 gboolean name_exist = false;
886 _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
888 ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
889 if (ret_val != MESSAGEPORT_ERROR_NONE)
893 if (strcmp(remote_app_id, __app_id) == 0) {
895 _LOGI("__is_local_port_registed ");
896 if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
901 _LOGI("__is_local_port_registed : %d ", *exist);
902 return MESSAGEPORT_ERROR_NONE;
905 port_info->exist = false;
906 bus_name = port_info->encoded_bus_name;
908 result = g_dbus_connection_call_sync(
914 g_variant_new("(s)", bus_name),
915 G_VARIANT_TYPE("(b)"),
916 G_DBUS_CALL_FLAGS_NONE,
921 if (err || (result == NULL)) {
923 _LOGE("No reply. error = %s", err->message);
926 ret_val = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
928 g_variant_get(result, "(b)", &name_exist);
931 LOGE("Name not exist %s", bus_name);
933 ret_val = MESSAGEPORT_ERROR_NONE;
937 if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
938 if (!__is_preloaded(__app_id, remote_app_id)) {
939 if (__check_certificate(__app_id, remote_app_id) != MESSAGEPORT_ERROR_NONE) {
940 ret_val = MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
944 remote_app_info->certificate_info = CERTIFICATE_MATCH;
947 port_info->exist = true;
949 ret_val = MESSAGEPORT_ERROR_NONE;
954 g_variant_unref(result);
959 static void __on_sender_name_appeared(GDBusConnection *connection,
961 const gchar *name_owner,
964 _LOGI("sender name appeared : %s", name);
967 static void __on_sender_name_vanished(GDBusConnection *connection,
971 gboolean remove_result = FALSE;
972 int *watcher_id = (int *)user_data;
973 remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
975 _LOGE("Fail to remove sender appid from hash : %s", name);
977 g_bus_unwatch_name(*watcher_id);
981 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
984 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
985 char *local_appid = NULL;
986 int pid = __get_sender_pid(conn, sender);
987 int *watcher_id = (int *)calloc(1, sizeof(int));
989 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
990 if (ret != AUL_R_OK) {
991 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
996 g_variant_get_child(parameters, 0, "&s", &local_appid);
997 if (local_appid == NULL) {
998 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
1003 if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
1004 _LOGI("insert sender !!!!! %s", sender);
1005 g_hash_table_insert(__sender_appid_hash, (gpointer)strdup(sender), GINT_TO_POINTER(pid));
1006 *watcher_id = g_bus_watch_name_on_connection(
1009 G_BUS_NAME_WATCHER_FLAGS_NONE,
1010 __on_sender_name_appeared,
1011 __on_sender_name_vanished,
1021 static void __dbus_method_call_handler(GDBusConnection *conn,
1022 const gchar *sender, const gchar *object_path,
1023 const gchar *iface_name, const gchar *method_name,
1024 GVariant *parameters, GDBusMethodInvocation *invocation,
1027 _LOGI("method_name: %s, sender: %s", method_name, sender);
1028 gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
1029 if (sender_pid == NULL) {
1030 if (!__check_sender_validation(parameters, sender, conn))
1033 if (g_strcmp0(method_name, "send_message") == 0)
1034 send_message(parameters, invocation);
1036 g_dbus_method_invocation_return_value(invocation, NULL);
1039 static const GDBusInterfaceVTable interface_vtable = {
1040 __dbus_method_call_handler,
1045 static int __dbus_init(void)
1048 GError *error = NULL;
1050 __gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
1051 if (__gdbus_conn == NULL) {
1052 if (error != NULL) {
1053 _LOGE("Failed to get dbus [%s]", error->message);
1054 g_error_free(error);
1063 g_object_unref(__gdbus_conn);
1069 int __register_dbus_interface(const char *port_name, bool is_trusted)
1072 GDBusNodeInfo *introspection_data = NULL;
1073 int registration_id = 0;
1075 static gchar introspection_prefix[] =
1077 " <interface name='";
1079 static gchar introspection_postfix[] =
1081 " <method name='send_message'>"
1082 " <arg type='s' name='local_appid' direction='in'/>"
1083 " <arg type='s' name='local_port' direction='in'/>"
1084 " <arg type='b' name='local_trusted' direction='in'/>"
1085 " <arg type='b' name='bi_dir' direction='in'/>"
1086 " <arg type='s' name='remote_appid' direction='in'/>"
1087 " <arg type='s' name='remote_port' direction='in'/>"
1088 " <arg type='b' name='remote_trusted' direction='in'/>"
1089 " <arg type='u' name='data_len' direction='in'/>"
1090 " <arg type='s' name='data' direction='in'/>"
1095 char *introspection_xml = NULL;
1096 int introspection_xml_len = 0;
1100 GError *error = NULL;
1101 char *bus_name = NULL;
1102 char *interface_name = NULL;
1103 GVariant *result = NULL;
1105 bus_name = __get_encoded_name(__app_id, port_name, is_trusted);
1107 _LOGE("Fail to get bus name");
1110 interface_name = bus_name;
1112 introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
1113 strlen(introspection_postfix) + 1;
1115 introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
1116 if (!introspection_xml) {
1117 _LOGE("out of memory");
1122 result = g_dbus_connection_call_sync(
1126 DBUS_INTERFACE_DBUS,
1128 g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
1129 G_VARIANT_TYPE("(u)"),
1130 G_DBUS_CALL_FLAGS_NONE,
1135 _LOGE("RequestName fail : %s", error->message);
1138 if (result == NULL) {
1139 _LOGE("fail to get name NULL");
1142 g_variant_get(result, "(u)", &owner_id);
1143 if (owner_id == 0) {
1144 _LOGE("Acquiring the own name is failed");
1148 _LOGI("Acquiring the own name : %d", owner_id);
1150 snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
1152 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
1153 if (!introspection_data) {
1154 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
1158 registration_id = g_dbus_connection_register_object(__gdbus_conn,
1159 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
1160 &interface_vtable, NULL, NULL, NULL);
1162 _LOGI("registration_id %d", registration_id);
1164 if (registration_id == 0) {
1165 _LOGE("Failed to g_dbus_connection_register_object");
1170 if (introspection_data)
1171 g_dbus_node_info_unref(introspection_data);
1172 if (introspection_xml)
1173 free(introspection_xml);
1177 g_variant_unref(result);
1180 return registration_id;
1183 /* LCOV_EXCL_START */
1184 void __list_free_port_list(gpointer data)
1186 port_list_info_s *n = (port_list_info_s *)data;
1188 FREE_AND_NULL(n->encoded_bus_name);
1189 FREE_AND_NULL(n->port_name);
1192 /* LCOV_EXCL_STOP */
1194 /* LCOV_EXCL_START */
1195 static void __hash_destory_local_value(gpointer data)
1197 message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
1200 free(mli->port_name);
1204 /* LCOV_EXCL_STOP */
1206 /* LCOV_EXCL_START */
1207 static void __hash_destory_remote_value(gpointer data)
1209 message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
1211 FREE_AND_NULL(mri->sender_id);
1212 FREE_AND_NULL(mri->remote_app_id);
1214 g_list_free_full(mri->port_list, __list_free_port_list);
1219 /* LCOV_EXCL_STOP */
1221 static bool __initialize(void)
1224 #if !GLIB_CHECK_VERSION(2, 35, 0)
1230 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1232 _LOGI("initialize");
1233 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1234 retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
1236 __app_id = strdup(buffer);
1237 retvm_if(!__app_id, false, "Malloc failed");
1238 _LOGI("init : %s", __app_id);
1240 if (__local_port_info == NULL) {
1241 __local_port_info = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destory_local_value);
1242 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
1245 if (__remote_app_info == NULL) {
1246 __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
1247 retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
1250 if (__sender_appid_hash == NULL) {
1251 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
1252 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
1255 if (__trusted_app_list_hash == NULL) {
1256 __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
1257 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1260 if (__callback_info_hash == NULL) {
1261 __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
1262 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1267 _initialized = true;
1273 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, messageport_message_cb callback)
1275 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
1276 retvm_if(!mi, false, "Malloc failed");
1278 mi->callback = callback;
1279 mi->is_trusted = is_trusted;
1280 mi->port_name = strdup(local_port);
1281 if (mi->port_name == NULL) {
1282 _LOGE("Malloc failed (%s)", local_port);
1286 mi->local_id = local_id;
1288 g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
1292 static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
1294 _SECURE_LOGI("Register a message port : [%s:%s]", __app_id, local_port);
1298 /* Check the message port is already registed */
1299 if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL))
1302 local_id = __register_dbus_interface(local_port, is_trusted);
1304 _LOGE("register_dbus_interface fail !!");
1305 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1308 if (!__message_port_register_port(local_id, local_port, is_trusted, callback))
1309 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1314 int __message_port_send_async(int sockfd, bundle *kb, const char *local_port,
1315 bool local_trusted, bool is_bidirection)
1319 int local_port_len = 0;
1321 bundle_raw *kb_data = NULL;
1323 if (local_port != NULL)
1324 local_port_len = strlen(local_port) + 1;
1326 if (__write_string_to_socket(sockfd, local_port, local_port_len) != MESSAGEPORT_ERROR_NONE) {
1327 _LOGE("write local_port fail");
1328 return MESSAGEPORT_ERROR_IO_ERROR;
1331 if (__write_socket(sockfd, (char *)&is_bidirection, sizeof(is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
1332 _LOGE("write is_bidirection fail");
1333 return MESSAGEPORT_ERROR_IO_ERROR;
1336 if (__write_socket(sockfd, (char *)&local_trusted, sizeof(local_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
1337 _LOGE("write local_trusted fail");
1338 return MESSAGEPORT_ERROR_IO_ERROR;
1341 bundle_encode(kb, &kb_data, &data_len);
1342 if (kb_data == NULL) {
1343 _LOGE("bundle encode fail");
1344 ret = MESSAGEPORT_ERROR_IO_ERROR;
1348 if (data_len > MAX_MESSAGE_SIZE) {
1349 _LOGE("bigger than max size\n");
1350 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1354 if (__write_string_to_socket(sockfd, (void *)kb_data, data_len) != MESSAGEPORT_ERROR_NONE) {
1355 _LOGE("write kb_data fail");
1356 ret = MESSAGEPORT_ERROR_IO_ERROR;
1365 static int __message_port_send_message(const char *remote_appid, const char *remote_port,
1366 const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
1369 int ret = MESSAGEPORT_ERROR_NONE;
1370 GUnixFDList *fd_list = NULL;
1373 bundle_raw *raw = NULL;
1374 char *bus_name = NULL;
1375 char *interface_name = NULL;
1377 message_port_remote_app_info_s *remote_app_info = NULL;
1378 port_list_info_s *port_info = NULL;
1379 GDBusMessage *msg = NULL;
1381 GVariant *body = NULL;
1382 int sock_pair[2] = {0,};
1384 ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
1385 if (ret != MESSAGEPORT_ERROR_NONE)
1388 if (port_info->exist == false) {
1390 _LOGI("port exist check !!");
1391 ret = __check_remote_port(remote_appid, remote_port, trusted_message, &exist);
1392 if (ret != MESSAGEPORT_ERROR_NONE) {
1394 } else if (!exist) {
1395 ret = MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1400 if (port_info->send_sock_fd > 0) {
1401 ret = __message_port_send_async(port_info->send_sock_fd, message,
1402 (local_port) ? local_port : "", local_trusted, bi_dir);
1405 bus_name = port_info->encoded_bus_name;
1406 interface_name = bus_name;
1408 if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
1409 ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
1413 if (MAX_MESSAGE_SIZE < len) {
1414 _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
1415 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1419 body = g_variant_new("(ssbbssbus)", __app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
1420 remote_appid, remote_port, trusted_message, len, raw);
1421 if (strcmp(remote_appid, __app_id) != 0) { /* self send */
1423 /* if message-port fail to get socket pair, communicate using GDBus */
1424 if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
1425 _LOGE("error create socket pair");
1428 _LOGI("sock pair : %d, %d",
1429 sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
1430 fd_list = g_unix_fd_list_new();
1431 g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
1433 _LOGE("g_unix_fd_list_append [%s]", err->message);
1434 ret = MESSAGEPORT_ERROR_IO_ERROR;
1438 port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
1439 close(sock_pair[SOCK_PAIR_RECEIVER]);
1443 msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
1445 _LOGE("Can't allocate new method call");
1446 ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1450 g_dbus_message_set_unix_fd_list(msg, fd_list);
1451 g_dbus_message_set_body(msg, body);
1452 g_dbus_connection_send_message(__gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
1454 _LOGE("No reply. error = %s", err->message);
1456 ret = MESSAGEPORT_ERROR_IO_ERROR;
1465 g_object_unref(msg);
1467 bundle_free_encoded_rawdata(&raw);
1469 g_object_unref(fd_list);
1475 int __message_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port, bool trusted_message, bundle *message)
1477 message_port_local_port_info_s *local_info;
1478 int ret = __get_local_port_info(id, &local_info);
1479 if (ret != MESSAGEPORT_ERROR_NONE)
1482 _LOGI("bidirectional_message %s", local_info->port_name);
1483 return __message_port_send_message(remote_app_id, remote_port,
1484 local_info->port_name, trusted_message, local_info->is_trusted, true, message);
1487 int messageport_unregister_local_port(int local_port_id, bool trusted_port)
1491 char *bus_name = NULL;
1495 _LOGI("unregister : %d", local_port_id);
1497 message_port_local_port_info_s *mi =
1498 (message_port_local_port_info_s *)
1499 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
1501 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1503 if (mi->is_trusted != trusted_port)
1504 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1506 g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
1508 bus_name = __get_encoded_name(__app_id, mi->port_name, mi->is_trusted);
1509 if (bus_name == NULL)
1510 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1512 g_dbus_connection_unregister_object(__gdbus_conn, local_port_id);
1514 result = g_dbus_connection_call_sync(
1518 DBUS_INTERFACE_DBUS,
1520 g_variant_new("(s)", bus_name),
1521 G_VARIANT_TYPE("(u)"),
1522 G_DBUS_CALL_FLAGS_NONE,
1531 _LOGE("RequestName fail : %s", err->message);
1533 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1535 g_variant_get(result, "(u)", &ret);
1538 g_variant_unref(result);
1540 if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1542 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1543 _LOGE("Port Not exist");
1544 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1545 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1546 _LOGE("Try to release not owned name. MESSAGEPORT_ERROR_INVALID_PARAMETER");
1547 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1552 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1554 return MESSAGEPORT_ERROR_NONE;
1557 int messageport_register_local_port(const char *local_port, messageport_message_cb callback)
1559 if (!_initialized) {
1560 if (!__initialize())
1561 return MESSAGEPORT_ERROR_IO_ERROR;
1564 return __register_message_port(local_port, false, callback);
1567 int messageport_register_trusted_local_port(const char *local_port, messageport_message_cb callback)
1569 if (!_initialized) {
1570 if (!__initialize())
1571 return MESSAGEPORT_ERROR_IO_ERROR;
1574 return __register_message_port(local_port, true, callback);
1578 int messageport_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1580 if (!_initialized) {
1581 if (!__initialize())
1582 return MESSAGEPORT_ERROR_IO_ERROR;
1585 int ret = __check_remote_port(remote_app_id, remote_port, false, exist);
1586 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1588 ret = MESSAGEPORT_ERROR_NONE;
1594 int messageport_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1596 if (!_initialized) {
1597 if (!__initialize())
1598 return MESSAGEPORT_ERROR_IO_ERROR;
1601 int ret = __check_remote_port(remote_app_id, remote_port, true, exist);
1602 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1604 ret = MESSAGEPORT_ERROR_NONE;
1610 int messageport_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
1612 if (!_initialized) {
1613 if (!__initialize())
1614 return MESSAGEPORT_ERROR_IO_ERROR;
1617 return __message_port_send_message(remote_app_id, remote_port, NULL, false, false, false, message);
1620 int messageport_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
1622 if (!_initialized) {
1623 if (!__initialize())
1624 return MESSAGEPORT_ERROR_IO_ERROR;
1627 return __message_port_send_message(remote_app_id, remote_port, NULL, true, false, false, message);
1630 int messageport_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,
1633 if (!_initialized) {
1634 if (!__initialize())
1635 return MESSAGEPORT_ERROR_IO_ERROR;
1638 return __message_send_bidirectional_message(id, remote_app_id, remote_port, false, message);
1641 int messageport_send_bidirectional_trusted_message(int id, const char *remote_app_id, const char *remote_port,
1644 if (!_initialized) {
1645 if (!__initialize())
1646 return MESSAGEPORT_ERROR_IO_ERROR;
1648 return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);