2 * Copyright (c) 2017 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.
20 #include <bundle_internal.h>
23 #include <sys/socket.h>
29 #include <gio/gunixfdlist.h>
30 #include <glib-unix.h>
32 #include "message_port_log.h"
33 #include "message_port_common.h"
34 #include "message_port_remote.h"
37 #define MAX_PACKAGE_STR_SIZE 512
39 #define DBUS_RELEASE_NAME_REPLY_RELEASED 1 /* *< Service was released from the given name */
40 #define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT 2 /* *< The given name does not exist on the bus */
41 #define DBUS_RELEASE_NAME_REPLY_NOT_OWNER 3 /* *< Service is not an owner of the given name */
43 #define MAX_RETRY_CNT 10
44 #define SOCK_PAIR_SENDER 0
45 #define SOCK_PAIR_RECEIVER 1
47 static bool _initialized = false;
48 static GHashTable *__local_port_info;
49 static GHashTable *__trusted_app_list_hash;
50 static GHashTable *__callback_info_hash;
51 static GHashTable *__sender_appid_hash;
53 typedef struct message_port_pkt {
54 int remote_port_name_len;
55 char *remote_port_name;
62 typedef struct message_port_callback_info {
63 message_port_local_port_info_s *local_info;
68 } message_port_callback_info_s;
70 typedef struct callback_key_info {
72 message_port_callback_info_s *callback_info;
73 } callback_key_info_s;
75 static void __callback_info_free(gpointer data)
77 message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
79 if (callback_info == NULL)
82 if (callback_info->remote_app_id)
83 FREE_AND_NULL(callback_info->remote_app_id);
85 if (callback_info->local_info) {
86 if (callback_info->local_info->port_name)
87 FREE_AND_NULL(callback_info->local_info->port_name);
89 FREE_AND_NULL(callback_info->local_info);
92 if (callback_info->gio_read != NULL) {
93 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
95 _LOGE("g_io_channel_shutdown error : %s", error->message);
98 g_io_channel_unref(callback_info->gio_read);
99 callback_info->gio_read = NULL;
102 if (callback_info->g_src_id != 0) {
103 g_source_remove(callback_info->g_src_id);
104 callback_info->g_src_id = 0;
107 FREE_AND_NULL(callback_info);
110 /* LCOV_EXCL_START */
111 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
113 GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
116 if (callback_info_list == NULL)
119 find_list = g_list_find(callback_info_list, callback_info);
120 if (find_list == NULL)
123 callback_info_list = g_list_remove_link(callback_info_list, find_list);
124 __callback_info_free(callback_info);
125 g_list_free(find_list);
129 static void __hash_destroy_callback_info(gpointer data)
131 GList *callback_list = (GList *)data;
133 if (callback_list != NULL)
134 g_list_free_full(callback_list, __callback_info_free);
137 /* LCOV_EXCL_START */
138 static void __hash_destory_local_value(gpointer data)
140 message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
143 free(mli->port_name);
149 static bool __initialize(void)
151 if (!initialized_common) {
152 if (!initialize_common())
156 if (__local_port_info == NULL) {
157 __local_port_info = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destory_local_value);
158 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
161 if (__sender_appid_hash == NULL) {
162 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
163 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
166 if (__trusted_app_list_hash == NULL) {
167 __trusted_app_list_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
168 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
171 if (__callback_info_hash == NULL) {
172 __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
173 retvm_if(!__callback_info_hash, false, "fail to create __callback_info_hash");
181 bool is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
186 if (__local_port_info == NULL) {
187 _LOGI("There is no registed local port");
191 g_hash_table_iter_init(&iter, __local_port_info);
192 while (g_hash_table_iter_next(&iter, &key, &value)) {
193 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
195 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
196 *local_id = mi->local_id;
205 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
207 GDBusMessage *msg = NULL;
208 GDBusMessage *reply = NULL;
213 msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
214 "org.freedesktop.DBus", "GetConnectionUnixProcessID");
216 _LOGE("Can't allocate new method call");
220 g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
221 reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
222 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
226 _LOGE("Failed to get pid [%s]", err->message);
229 _LOGE("Failed to get pid");
234 body = g_dbus_message_get_body(reply);
235 g_variant_get(body, "(u)", &pid);
241 g_object_unref(reply);
246 static message_port_pkt_s *__message_port_recv_raw(int fd)
248 message_port_pkt_s *pkt = NULL;
251 pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
253 /* LCOV_EXCL_START */
259 if (read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGE_PORT_ERROR_NONE) {
260 /* LCOV_EXCL_START */
261 LOGE("read socket fail: port_name");
262 free(pkt->remote_port_name);
268 if (read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGE_PORT_ERROR_NONE) {
269 /* LCOV_EXCL_START */
270 LOGE("read socket fail: is_bidirection");
271 free(pkt->remote_port_name);
277 if (read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGE_PORT_ERROR_NONE) {
278 /* LCOV_EXCL_START */
279 LOGE("read socket fail: is_trusted");
280 free(pkt->remote_port_name);
286 if (read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGE_PORT_ERROR_NONE) {
287 /* LCOV_EXCL_START */
288 LOGE("read socket fail: data");
291 free(pkt->remote_port_name);
300 static bool __validate_callback_info(callback_key_info_s *key_info)
304 cb_list = g_hash_table_lookup(__callback_info_hash,
305 GUINT_TO_POINTER(key_info->local_id));
306 if (cb_list == NULL) {
307 _LOGI("local_info : %d is already released", key_info->local_id);
311 cb_list = g_list_find(cb_list, key_info->callback_info);
312 if (cb_list == NULL) {
313 _LOGI("local_info : %d is already released from list",
321 static bool __validate_local_info(callback_key_info_s *key_info)
325 cb_list = g_hash_table_lookup(__local_port_info,
326 GUINT_TO_POINTER(key_info->local_id));
327 if (cb_list == NULL) {
328 _LOGI("local_info : %d is already released", key_info->local_id);
335 static gboolean __socket_request_handler(GIOChannel *gio,
340 message_port_callback_info_s *mi = NULL;
341 callback_key_info_s *key_info;
342 message_port_pkt_s *pkt = NULL;
343 message_port_local_port_info_s *local_port_info;
348 key_info = (callback_key_info_s *)data;
349 if (key_info == NULL)
352 message_port_lock_mutex();
353 if (__validate_callback_info(key_info) == false) {
356 message_port_unlock_mutex();
360 if (__validate_local_info(key_info) == false) {
362 message_port_unlock_mutex();
365 message_port_unlock_mutex();
367 mi = key_info->callback_info;
369 local_port_info = mi->local_info;
370 if (local_port_info == NULL || local_port_info->callback == NULL) {
371 _LOGE("Failed to get callback info");
376 if (cond == G_IO_HUP) {
377 _LOGI("socket G_IO_HUP");
382 fd = g_io_channel_unix_get_fd(gio);
384 _LOGE("fail to get fd from io channel");
389 pkt = __message_port_recv_raw(fd);
391 _LOGE("recv error on SOCKET");
396 kb = bundle_decode(pkt->data, pkt->data_len);
398 _LOGE("Invalid argument : message");
403 if (pkt->is_bidirection)
404 local_port_info->callback(mi->local_id, mi->remote_app_id,
405 pkt->remote_port_name, pkt->is_trusted, kb, local_port_info->user_data);
407 local_port_info->callback(mi->local_id, mi->remote_app_id,
408 NULL, pkt->is_trusted, kb, local_port_info->user_data);
414 if (pkt->remote_port_name)
415 free(pkt->remote_port_name);
421 if (mi && ret == FALSE && existed == TRUE) {
422 message_port_lock_mutex();
423 __callback_info_free_by_info(mi);
424 message_port_unlock_mutex();
431 static void __socket_destroy_handler(gpointer data)
433 _LOGI("__socket_destroy_handler");
434 callback_key_info_s *key_info = (callback_key_info_s *)data;
438 static callback_key_info_s *__create_callback_key_info(message_port_callback_info_s *callback_info)
440 callback_key_info_s *_key_info = (callback_key_info_s *)
441 calloc(1, sizeof(callback_key_info_s));
443 if (_key_info == NULL) {
444 /* LCOV_EXCL_START */
445 _LOGE("out of memory");
450 _key_info->local_id = callback_info->local_id;
451 _key_info->callback_info = callback_info;
456 static message_port_callback_info_s *__create_callback_info(message_port_local_port_info_s *mi, char *local_appid)
458 message_port_local_port_info_s *local_info = NULL;
459 message_port_callback_info_s *callback_info = NULL;
462 callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
463 if (callback_info == NULL) {
464 /* LCOV_EXCL_START */
465 _LOGE("out of memory");
470 local_info = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
471 if (local_info == NULL) {
472 /* LCOV_EXCL_START */
474 _LOGE("out of memory");
479 callback_info->local_id = mi->local_id;
480 callback_info->local_info = local_info;
481 callback_info->remote_app_id = strdup(local_appid);
482 if (callback_info->remote_app_id == NULL) {
483 /* LCOV_EXCL_START */
485 _LOGE("out of memory");
490 local_info->port_name = strdup(mi->port_name);
491 if (local_info->port_name == NULL) {
492 /* LCOV_EXCL_START */
494 _LOGE("out of memory");
499 local_info->callback = mi->callback;
500 local_info->is_trusted = mi->is_trusted;
501 local_info->local_id = mi->local_id;
502 local_info->user_data = mi->user_data;
506 __callback_info_free(callback_info);
510 return callback_info;
513 static bool __callback_info_append(message_port_callback_info_s *callback_info)
515 GList *callback_info_list = NULL;
516 message_port_callback_info_s *head_callback_info;
518 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
519 if (callback_info_list == NULL) {
520 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
521 if (head_callback_info == NULL) {
522 /* LCOV_EXCL_START */
523 _LOGE("fail to alloc head_callback_info");
527 head_callback_info->local_id = 0;
528 head_callback_info->remote_app_id = NULL;
529 head_callback_info->local_info = NULL;
530 head_callback_info->gio_read = NULL;
531 head_callback_info->g_src_id = 0;
532 callback_info_list = g_list_append(callback_info_list, head_callback_info);
533 callback_info_list = g_list_append(callback_info_list, callback_info);
534 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id), callback_info_list);
536 callback_info_list = g_list_append(callback_info_list, callback_info);
542 static void __callback_info_update_user_data(int local_id, message_port_message_cb callback, void *user_data)
544 GList *callback_info_list;
546 message_port_callback_info_s *callback_info;
548 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(local_id));
549 if (callback_info_list != NULL) {
550 /* LCOV_EXCL_START */
551 for (iter = callback_info_list; iter != NULL; iter = iter->next) {
552 callback_info = (message_port_callback_info_s *)iter->data;
553 if (callback_info->local_info != NULL) {
554 callback_info->local_info->callback = callback;
555 callback_info->local_info->user_data = user_data;
560 _LOGE("fail to find local_id %d ", local_id);
564 static bool __receive_message(GVariant *parameters, GDBusMethodInvocation *invocation)
566 char *local_port = NULL;
567 char *local_appid = NULL;
568 char *remote_appid = NULL;
569 char *remote_port = NULL;
570 gboolean local_trusted = false;
571 gboolean remote_trusted = false;
572 gboolean bi_dir = false;
576 bundle_raw *raw = NULL;
577 message_port_local_port_info_s *mi;
578 int local_reg_id = 0;
579 message_port_callback_info_s *callback_info = NULL;
580 callback_key_info_s *key_info;
584 GUnixFDList *fd_list;
586 int *returned_fds = NULL;
591 g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
592 &remote_appid, &remote_port, &remote_trusted, &len, &raw);
595 _LOGE("Invalid argument : remote_port is NULL");
599 _LOGE("Invalid argument : remote_appid is NULL");
604 _LOGE("Invalid argument : local_appid");
608 message_port_lock_mutex();
609 if (!is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
610 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
611 message_port_unlock_mutex();
615 callback_info = __create_callback_info(mi, local_appid);
616 if (callback_info == NULL) {
617 message_port_unlock_mutex();
620 message_port_unlock_mutex();
623 _LOGE("Invalid argument : local_port");
626 if (strcmp(remote_appid, app_id) != 0) {
627 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
630 if (strcmp(remote_port, callback_info->local_info->port_name) != 0) {
631 _LOGE("Invalid argument : remote_port (%s)", remote_port);
635 _LOGE("Invalid argument : data_len");
638 if (remote_trusted) {
639 /* LCOV_EXCL_START */
640 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
641 if (!is_preloaded(local_appid, remote_appid)) {
642 int ret = check_certificate(local_appid, remote_appid);
643 if (ret == MESSAGE_PORT_ERROR_NONE) {
644 key_appid = strdup(local_appid);
646 g_hash_table_insert(__trusted_app_list_hash, key_appid, "TRUE");
648 _LOGE("The application (%s) is not signed with the same certificate",
657 data = bundle_decode(raw, len);
659 _LOGE("Invalid argument : message");
663 LOGD("call calback %s", local_appid);
665 callback_info->local_info->callback(callback_info->local_info->local_id,
666 local_appid, local_port, local_trusted, data, callback_info->local_info->user_data);
668 callback_info->local_info->callback(callback_info->local_info->local_id,
669 local_appid, NULL, false, data, callback_info->local_info->user_data);
674 msg = g_dbus_method_invocation_get_message(invocation);
675 fd_list = g_dbus_message_get_unix_fd_list(msg);
677 /* When application send message to self fd_list is NULL */
678 if (fd_list != NULL) {
679 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
680 if (returned_fds == NULL) {
681 _LOGE("fail to get fds");
685 fd = returned_fds[0];
687 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
689 callback_info->gio_read = g_io_channel_unix_new(fd);
690 if (!callback_info->gio_read) {
691 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
696 key_info = __create_callback_key_info(callback_info);
697 if (key_info == NULL) {
698 _LOGE("out of memory");
703 callback_info->g_src_id = g_io_add_watch_full(
704 callback_info->gio_read,
707 __socket_request_handler,
709 __socket_destroy_handler);
710 if (callback_info->g_src_id == 0) {
711 _LOGE("fail to add watch on socket");
717 message_port_lock_mutex();
718 if (__callback_info_append(callback_info) == false) {
719 _LOGE("fail to append callback_info");
722 message_port_unlock_mutex();
728 __callback_info_free(callback_info);
731 g_free(returned_fds);
736 static void __on_sender_name_appeared(GDBusConnection *connection,
738 const gchar *name_owner,
741 _LOGI("sender name appeared : %s", name);
744 static void __on_sender_name_vanished(GDBusConnection *connection,
748 int *watcher_id = (int *)user_data;
751 local_appid = (char *)g_hash_table_lookup(__sender_appid_hash, name);
753 g_hash_table_remove(__trusted_app_list_hash, (gpointer)local_appid);
754 g_hash_table_remove(__sender_appid_hash, (gpointer)name);
759 g_bus_unwatch_name(*watcher_id);
761 LOGE("Invalid watcher_id %d", *watcher_id);
764 LOGE("watcher_id is NULL");
768 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
771 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
772 char *local_appid = NULL;
773 int pid = __get_sender_pid(conn, sender);
774 int *watcher_id = (int *)calloc(1, sizeof(int));
777 retvm_if(!watcher_id, false, "Malloc failed");
779 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
780 if (ret != AUL_R_OK) {
781 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
786 g_variant_get_child(parameters, 0, "&s", &local_appid);
787 if (local_appid == NULL) {
788 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
793 if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
794 _LOGD("insert sender !!!!! %s", sender);
795 _sender = strdup(sender);
796 if (_sender == NULL) {
797 /* LCOV_EXCL_START */
798 _LOGE("out of memory");
804 _appid = strdup(local_appid);
805 if (_appid == NULL) {
806 /* LCOV_EXCL_START */
807 _LOGE("out of memory");
814 g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, (gpointer)_appid);
815 *watcher_id = g_bus_watch_name_on_connection(
818 G_BUS_NAME_WATCHER_FLAGS_NONE,
819 __on_sender_name_appeared,
820 __on_sender_name_vanished,
829 /* LCOV_EXCL_START */
830 static void __close_socket(GDBusMethodInvocation *invocation)
833 GUnixFDList *fd_list;
835 int *returned_fds = NULL;
837 msg = g_dbus_method_invocation_get_message(invocation);
838 fd_list = g_dbus_message_get_unix_fd_list(msg);
839 if (fd_list != NULL) {
840 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
841 if (returned_fds != NULL) {
842 close(returned_fds[0]);
843 g_free(returned_fds);
848 static void __dbus_method_call_handler(GDBusConnection *conn,
849 const gchar *sender, const gchar *object_path,
850 const gchar *iface_name, const gchar *method_name,
851 GVariant *parameters, GDBusMethodInvocation *invocation,
854 _LOGI("method_name: %s, sender: %s", method_name, sender);
855 gpointer sender_appid = g_hash_table_lookup(__sender_appid_hash, sender);
856 if (sender_appid == NULL) {
857 if (!__check_sender_validation(parameters, sender, conn)) {
858 _LOGE("Failed to validate");
859 __close_socket(invocation);
864 if (g_strcmp0(method_name, "send_message") == 0)
865 __receive_message(parameters, invocation);
867 g_dbus_method_invocation_return_value(invocation, NULL);
870 static const GDBusInterfaceVTable interface_vtable = {
871 __dbus_method_call_handler,
876 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
878 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
879 retvm_if(!mi, false, "Malloc failed");
881 mi->callback = callback;
882 mi->is_trusted = is_trusted;
883 mi->port_name = strdup(local_port);
884 if (mi->port_name == NULL) {
885 /* LCOV_EXCL_START */
886 _LOGE("Malloc failed (%s)", local_port);
891 mi->local_id = local_id;
892 mi->user_data = user_data;
894 g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
898 static int __register_dbus_interface(const char *port_name,
899 bool is_trusted, message_port_message_cb callback, void *user_data)
902 GDBusNodeInfo *introspection_data = NULL;
903 int registration_id = 0;
904 int ret = MESSAGE_PORT_ERROR_NONE;
906 static gchar introspection_prefix[] =
908 " <interface name='";
910 static gchar introspection_postfix[] =
912 " <method name='send_message'>"
913 " <arg type='s' name='local_appid' direction='in'/>"
914 " <arg type='s' name='local_port' direction='in'/>"
915 " <arg type='b' name='local_trusted' direction='in'/>"
916 " <arg type='b' name='bi_dir' direction='in'/>"
917 " <arg type='s' name='remote_appid' direction='in'/>"
918 " <arg type='s' name='remote_port' direction='in'/>"
919 " <arg type='b' name='remote_trusted' direction='in'/>"
920 " <arg type='u' name='data_len' direction='in'/>"
921 " <arg type='s' name='data' direction='in'/>"
926 char *introspection_xml = NULL;
927 int introspection_xml_len = 0;
930 GError *error = NULL;
931 char *bus_name = NULL;
932 char *interface_name = NULL;
933 GVariant *result = NULL;
935 bus_name = get_encoded_name(app_id, port_name, is_trusted);
937 _LOGE("Fail to get bus name");
940 interface_name = bus_name;
942 introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
943 strlen(introspection_postfix) + 1;
945 introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
946 if (!introspection_xml) {
947 /* LCOV_EXCL_START */
948 _LOGE("out of memory");
953 snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
955 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
956 if (!introspection_data) {
957 ret = MESSAGE_PORT_ERROR_IO_ERROR;
958 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
962 registration_id = g_dbus_connection_register_object(gdbus_conn,
963 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
964 &interface_vtable, NULL, NULL, NULL);
966 _LOGD("registration_id %d", registration_id);
968 if (registration_id == 0) {
969 ret = MESSAGE_PORT_ERROR_IO_ERROR;
970 _LOGE("Failed to g_dbus_connection_register_object");
974 if (!__message_port_register_port(registration_id, port_name, is_trusted,
975 callback, user_data)) {
976 ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
980 result = g_dbus_connection_call_sync(
986 g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
987 G_VARIANT_TYPE("(u)"),
988 G_DBUS_CALL_FLAGS_NONE,
993 _LOGE("RequestName fail : %s", error->message);
994 ret = MESSAGE_PORT_ERROR_IO_ERROR;
998 if (result == NULL) {
999 ret = MESSAGE_PORT_ERROR_IO_ERROR;
1000 _LOGE("fail to get name NULL");
1003 g_variant_get(result, "(u)", &owner_id);
1004 if (owner_id == 0) {
1005 ret = MESSAGE_PORT_ERROR_IO_ERROR;
1006 _LOGE("Acquiring the own name is failed");
1010 _LOGI("Acquiring the own name : %d", owner_id);
1013 if (introspection_data)
1014 g_dbus_node_info_unref(introspection_data);
1015 if (introspection_xml)
1016 free(introspection_xml);
1020 g_variant_unref(result);
1022 if (ret != MESSAGE_PORT_ERROR_NONE) {
1023 if (registration_id != 0)
1024 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(registration_id));
1025 registration_id = ret;
1028 return registration_id;
1031 int get_local_port_info(int id, message_port_local_port_info_s **info)
1033 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
1036 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1039 return MESSAGE_PORT_ERROR_NONE;
1042 int register_message_port(const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
1044 _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
1047 message_port_local_port_info_s *port_info;
1048 if (!_initialized) {
1049 if (!__initialize())
1050 return MESSAGE_PORT_ERROR_IO_ERROR;
1053 /* Check the message port is already registed */
1054 if (is_local_port_registed(local_port, is_trusted, &local_id, &port_info)) {
1055 port_info->callback = callback;
1056 port_info->user_data = user_data;
1057 __callback_info_update_user_data(local_id, callback, user_data);
1061 local_id = __register_dbus_interface(local_port, is_trusted, callback, user_data);
1063 _LOGE("register_dbus_interface fail !!");
1069 int unregister_local_port(int local_port_id, bool trusted_port)
1073 char *bus_name = NULL;
1076 message_port_local_port_info_s *mi;
1078 _LOGI("unregister : %d", local_port_id);
1080 if (!_initialized) {
1081 if (!__initialize())
1082 return MESSAGE_PORT_ERROR_IO_ERROR;
1085 mi = (message_port_local_port_info_s *)
1086 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
1088 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1090 if (mi->is_trusted != trusted_port)
1091 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1093 g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
1095 bus_name = get_encoded_name(app_id, mi->port_name, mi->is_trusted);
1096 if (bus_name == NULL)
1097 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1099 g_dbus_connection_unregister_object(gdbus_conn, local_port_id);
1101 result = g_dbus_connection_call_sync(
1105 DBUS_INTERFACE_DBUS,
1107 g_variant_new("(s)", bus_name),
1108 G_VARIANT_TYPE("(u)"),
1109 G_DBUS_CALL_FLAGS_NONE,
1118 _LOGE("RequestName fail : %s", err->message);
1120 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1122 g_variant_get(result, "(u)", &ret);
1125 g_variant_unref(result);
1126 /* LCOV_EXCL_START */
1127 if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1128 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1129 _LOGE("Port Not exist");
1130 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1131 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1132 _LOGE("Try to release not owned name. MESSAGE_PORT_ERROR_INVALID_PARAMETER");
1133 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1136 /* LCOV_EXCL_STOP */
1137 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1139 return MESSAGE_PORT_ERROR_NONE;