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 g_hash_table_iter_init(&iter, __local_port_info);
187 while (g_hash_table_iter_next(&iter, &key, &value)) {
188 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
190 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
191 *local_id = mi->local_id;
200 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
202 GDBusMessage *msg = NULL;
203 GDBusMessage *reply = NULL;
208 msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
209 "org.freedesktop.DBus", "GetConnectionUnixProcessID");
211 _LOGE("Can't allocate new method call");
215 g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
216 reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
217 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
221 _LOGE("Failed to get pid [%s]", err->message);
227 body = g_dbus_message_get_body(reply);
228 g_variant_get(body, "(u)", &pid);
234 g_object_unref(reply);
239 static message_port_pkt_s *__message_port_recv_raw(int fd)
241 message_port_pkt_s *pkt = NULL;
244 pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
250 if (read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGE_PORT_ERROR_NONE) {
251 /* LCOV_EXCL_START */
252 LOGE("read socket fail: port_name");
253 free(pkt->remote_port_name);
259 if (read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGE_PORT_ERROR_NONE) {
260 /* LCOV_EXCL_START */
261 LOGE("read socket fail: is_bidirection");
262 free(pkt->remote_port_name);
268 if (read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGE_PORT_ERROR_NONE) {
269 /* LCOV_EXCL_START */
270 LOGE("read socket fail: is_trusted");
271 free(pkt->remote_port_name);
277 if (read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGE_PORT_ERROR_NONE) {
278 /* LCOV_EXCL_START */
279 LOGE("read socket fail: data");
282 free(pkt->remote_port_name);
291 static bool __validate_callback_info(callback_key_info_s *key_info)
295 cb_list = g_hash_table_lookup(__callback_info_hash,
296 GUINT_TO_POINTER(key_info->local_id));
297 if (cb_list == NULL) {
298 _LOGI("local_info : %d is already released", key_info->local_id);
302 cb_list = g_list_find(cb_list, key_info->callback_info);
303 if (cb_list == NULL) {
304 _LOGI("local_info : %d is already released", key_info->local_id);
311 static bool __validate_local_info(callback_key_info_s *key_info)
315 cb_list = g_hash_table_lookup(__local_port_info,
316 GUINT_TO_POINTER(key_info->local_id));
317 if (cb_list == NULL) {
318 _LOGI("local_info : %d is already released", key_info->local_id);
325 static gboolean __socket_request_handler(GIOChannel *gio,
330 message_port_callback_info_s *mi = NULL;
331 callback_key_info_s *key_info;
332 message_port_pkt_s *pkt = NULL;
333 message_port_local_port_info_s *local_port_info;
338 key_info = (callback_key_info_s *)data;
339 if (key_info == NULL)
342 message_port_lock_mutex();
343 if (__validate_callback_info(key_info) == false) {
346 message_port_unlock_mutex();
350 if (__validate_local_info(key_info) == false) {
352 message_port_unlock_mutex();
355 message_port_unlock_mutex();
357 mi = key_info->callback_info;
359 local_port_info = mi->local_info;
360 if (local_port_info == NULL || local_port_info->callback == NULL) {
361 _LOGE("Failed to get callback info");
366 if (cond == G_IO_HUP) {
367 _LOGI("socket G_IO_HUP");
372 fd = g_io_channel_unix_get_fd(gio);
374 _LOGE("fail to get fd from io channel");
379 pkt = __message_port_recv_raw(fd);
381 _LOGE("recv error on SOCKET");
386 kb = bundle_decode(pkt->data, pkt->data_len);
388 _LOGE("Invalid argument : message");
393 if (pkt->is_bidirection)
394 local_port_info->callback(mi->local_id, mi->remote_app_id,
395 pkt->remote_port_name, pkt->is_trusted, kb, local_port_info->user_data);
397 local_port_info->callback(mi->local_id, mi->remote_app_id,
398 NULL, pkt->is_trusted, kb, local_port_info->user_data);
404 if (pkt->remote_port_name)
405 free(pkt->remote_port_name);
411 if (mi && ret == FALSE && existed == TRUE) {
412 message_port_lock_mutex();
413 __callback_info_free_by_info(mi);
414 message_port_unlock_mutex();
421 static void __socket_destroy_handler(gpointer data)
423 _LOGI("__socket_destroy_handler");
424 callback_key_info_s *key_info = (callback_key_info_s *)data;
428 static callback_key_info_s *__create_callback_key_info(message_port_callback_info_s *callback_info)
430 callback_key_info_s *_key_info = (callback_key_info_s *)
431 calloc(1, sizeof(callback_key_info_s));
433 if (_key_info == NULL) {
434 _LOGE("out of memory");
438 _key_info->local_id = callback_info->local_id;
439 _key_info->callback_info = callback_info;
444 static message_port_callback_info_s *__create_callback_info(message_port_local_port_info_s *mi, char *local_appid)
446 message_port_local_port_info_s *local_info = NULL;
447 message_port_callback_info_s *callback_info = NULL;
450 callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
451 if (callback_info == NULL) {
452 _LOGE("out of memory");
456 local_info = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
457 if (local_info == NULL) {
459 _LOGE("out of memory");
463 callback_info->local_id = mi->local_id;
464 callback_info->local_info = local_info;
465 callback_info->remote_app_id = strdup(local_appid);
466 if (callback_info->remote_app_id == NULL) {
468 _LOGE("out of memory");
472 local_info->port_name = strdup(mi->port_name);
473 if (local_info->port_name == NULL) {
475 _LOGE("out of memory");
479 local_info->callback = mi->callback;
480 local_info->is_trusted = mi->is_trusted;
481 local_info->local_id = mi->local_id;
482 local_info->user_data = mi->user_data;
486 __callback_info_free(callback_info);
490 return callback_info;
493 static bool __callback_info_append(message_port_callback_info_s *callback_info)
495 GList *callback_info_list = NULL;
496 message_port_callback_info_s *head_callback_info;
498 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
499 if (callback_info_list == NULL) {
500 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
501 if (head_callback_info == NULL) {
502 _LOGE("fail to alloc head_callback_info");
505 head_callback_info->local_id = 0;
506 head_callback_info->remote_app_id = NULL;
507 head_callback_info->local_info = NULL;
508 head_callback_info->gio_read = NULL;
509 head_callback_info->g_src_id = 0;
510 callback_info_list = g_list_append(callback_info_list, head_callback_info);
511 callback_info_list = g_list_append(callback_info_list, callback_info);
512 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id), callback_info_list);
514 callback_info_list = g_list_append(callback_info_list, callback_info);
520 static void __callback_info_update_user_data(int local_id, message_port_message_cb callback, void *user_data)
522 GList *callback_info_list;
524 message_port_callback_info_s *callback_info;
526 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(local_id));
527 if (callback_info_list != NULL) {
528 for (iter = callback_info_list; iter != NULL; iter = iter->next) {
529 callback_info = (message_port_callback_info_s *)iter->data;
530 if (callback_info->local_info != NULL) {
531 callback_info->local_info->callback = callback;
532 callback_info->local_info->user_data = user_data;
536 _LOGE("fail to find local_id %d ", local_id);
540 static bool __receive_message(GVariant *parameters, GDBusMethodInvocation *invocation)
542 char *local_port = NULL;
543 char *local_appid = NULL;
544 char *remote_appid = NULL;
545 char *remote_port = NULL;
546 gboolean local_trusted = false;
547 gboolean remote_trusted = false;
548 gboolean bi_dir = false;
552 bundle_raw *raw = NULL;
553 message_port_local_port_info_s *mi;
554 int local_reg_id = 0;
555 message_port_callback_info_s *callback_info = NULL;
556 callback_key_info_s *key_info;
560 GUnixFDList *fd_list;
562 int *returned_fds = NULL;
567 g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
568 &remote_appid, &remote_port, &remote_trusted, &len, &raw);
571 _LOGE("Invalid argument : remote_port is NULL");
575 _LOGE("Invalid argument : remote_appid is NULL");
580 _LOGE("Invalid argument : local_appid");
584 message_port_lock_mutex();
585 if (!is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
586 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
587 message_port_unlock_mutex();
591 callback_info = __create_callback_info(mi, local_appid);
592 if (callback_info == NULL) {
593 message_port_unlock_mutex();
596 message_port_unlock_mutex();
599 _LOGE("Invalid argument : local_port");
602 if (strcmp(remote_appid, app_id) != 0) {
603 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
606 if (strcmp(remote_port, callback_info->local_info->port_name) != 0) {
607 _LOGE("Invalid argument : remote_port (%s)", remote_port);
611 _LOGE("Invalid argument : data_len");
614 if (remote_trusted) {
615 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
616 if (!is_preloaded(local_appid, remote_appid)) {
617 int ret = check_certificate(local_appid, remote_appid);
618 if (ret == MESSAGE_PORT_ERROR_NONE) {
619 key_appid = strdup(local_appid);
621 g_hash_table_insert(__trusted_app_list_hash, key_appid, "TRUE");
623 _LOGE("The application (%s) is not signed with the same certificate",
631 data = bundle_decode(raw, len);
633 _LOGE("Invalid argument : message");
637 LOGD("call calback %s", local_appid);
639 callback_info->local_info->callback(callback_info->local_info->local_id,
640 local_appid, local_port, local_trusted, data, callback_info->local_info->user_data);
642 callback_info->local_info->callback(callback_info->local_info->local_id,
643 local_appid, NULL, false, data, callback_info->local_info->user_data);
648 msg = g_dbus_method_invocation_get_message(invocation);
649 fd_list = g_dbus_message_get_unix_fd_list(msg);
651 /* When application send message to self fd_list is NULL */
652 if (fd_list != NULL) {
653 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
654 if (returned_fds == NULL) {
655 _LOGE("fail to get fds");
659 fd = returned_fds[0];
661 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
663 callback_info->gio_read = g_io_channel_unix_new(fd);
664 if (!callback_info->gio_read) {
665 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
670 key_info = __create_callback_key_info(callback_info);
671 if (key_info == NULL) {
672 _LOGE("out of memory");
677 callback_info->g_src_id = g_io_add_watch_full(
678 callback_info->gio_read,
681 __socket_request_handler,
683 __socket_destroy_handler);
684 if (callback_info->g_src_id == 0) {
685 _LOGE("fail to add watch on socket");
691 message_port_lock_mutex();
692 if (__callback_info_append(callback_info) == false) {
693 _LOGE("fail to append callback_info");
696 message_port_unlock_mutex();
702 __callback_info_free(callback_info);
710 static void __on_sender_name_appeared(GDBusConnection *connection,
712 const gchar *name_owner,
715 _LOGI("sender name appeared : %s", name);
718 static void __on_sender_name_vanished(GDBusConnection *connection,
722 int *watcher_id = (int *)user_data;
725 local_appid = (char *)g_hash_table_lookup(__sender_appid_hash, name);
727 g_hash_table_remove(__trusted_app_list_hash, (gpointer)local_appid);
728 g_hash_table_remove(__sender_appid_hash, (gpointer)name);
733 g_bus_unwatch_name(*watcher_id);
735 LOGE("Invalid watcher_id %d", *watcher_id);
738 LOGE("watcher_id is NULL");
742 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
745 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
746 char *local_appid = NULL;
747 int pid = __get_sender_pid(conn, sender);
748 int *watcher_id = (int *)calloc(1, sizeof(int));
751 retvm_if(!watcher_id, false, "Malloc failed");
753 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
754 if (ret != AUL_R_OK) {
755 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
760 g_variant_get_child(parameters, 0, "&s", &local_appid);
761 if (local_appid == NULL) {
762 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
767 if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
768 _LOGD("insert sender !!!!! %s", sender);
769 _sender = strdup(sender);
770 if (_sender == NULL) {
771 _LOGE("out of memory");
776 _appid = strdup(local_appid);
777 if (_appid == NULL) {
778 _LOGE("out of memory");
784 g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, (gpointer)_appid);
785 *watcher_id = g_bus_watch_name_on_connection(
788 G_BUS_NAME_WATCHER_FLAGS_NONE,
789 __on_sender_name_appeared,
790 __on_sender_name_vanished,
800 static void __dbus_method_call_handler(GDBusConnection *conn,
801 const gchar *sender, const gchar *object_path,
802 const gchar *iface_name, const gchar *method_name,
803 GVariant *parameters, GDBusMethodInvocation *invocation,
806 _LOGI("method_name: %s, sender: %s", method_name, sender);
807 gpointer sender_appid = g_hash_table_lookup(__sender_appid_hash, sender);
808 if (sender_appid == NULL) {
809 if (!__check_sender_validation(parameters, sender, conn))
812 if (g_strcmp0(method_name, "send_message") == 0)
813 __receive_message(parameters, invocation);
815 g_dbus_method_invocation_return_value(invocation, NULL);
818 static const GDBusInterfaceVTable interface_vtable = {
819 __dbus_method_call_handler,
824 static int __register_dbus_interface(const char *port_name, bool is_trusted)
827 GDBusNodeInfo *introspection_data = NULL;
828 int registration_id = 0;
830 static gchar introspection_prefix[] =
832 " <interface name='";
834 static gchar introspection_postfix[] =
836 " <method name='send_message'>"
837 " <arg type='s' name='local_appid' direction='in'/>"
838 " <arg type='s' name='local_port' direction='in'/>"
839 " <arg type='b' name='local_trusted' direction='in'/>"
840 " <arg type='b' name='bi_dir' direction='in'/>"
841 " <arg type='s' name='remote_appid' direction='in'/>"
842 " <arg type='s' name='remote_port' direction='in'/>"
843 " <arg type='b' name='remote_trusted' direction='in'/>"
844 " <arg type='u' name='data_len' direction='in'/>"
845 " <arg type='s' name='data' direction='in'/>"
850 char *introspection_xml = NULL;
851 int introspection_xml_len = 0;
855 GError *error = NULL;
856 char *bus_name = NULL;
857 char *interface_name = NULL;
858 GVariant *result = NULL;
860 bus_name = get_encoded_name(app_id, port_name, is_trusted);
862 _LOGE("Fail to get bus name");
865 interface_name = bus_name;
867 introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
868 strlen(introspection_postfix) + 1;
870 introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
871 if (!introspection_xml) {
872 _LOGE("out of memory");
877 result = g_dbus_connection_call_sync(
883 g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
884 G_VARIANT_TYPE("(u)"),
885 G_DBUS_CALL_FLAGS_NONE,
890 _LOGE("RequestName fail : %s", error->message);
894 if (result == NULL) {
895 _LOGE("fail to get name NULL");
898 g_variant_get(result, "(u)", &owner_id);
900 _LOGE("Acquiring the own name is failed");
904 _LOGD("Acquiring the own name : %d", owner_id);
906 snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
908 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
909 if (!introspection_data) {
910 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
914 registration_id = g_dbus_connection_register_object(gdbus_conn,
915 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
916 &interface_vtable, NULL, NULL, NULL);
918 _LOGD("registration_id %d", registration_id);
920 if (registration_id == 0) {
921 _LOGE("Failed to g_dbus_connection_register_object");
926 if (introspection_data)
927 g_dbus_node_info_unref(introspection_data);
928 if (introspection_xml)
929 free(introspection_xml);
933 g_variant_unref(result);
936 return registration_id;
939 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)
941 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
942 retvm_if(!mi, false, "Malloc failed");
944 mi->callback = callback;
945 mi->is_trusted = is_trusted;
946 mi->port_name = strdup(local_port);
947 if (mi->port_name == NULL) {
948 _LOGE("Malloc failed (%s)", local_port);
952 mi->local_id = local_id;
953 mi->user_data = user_data;
955 g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
959 int get_local_port_info(int id, message_port_local_port_info_s **info)
961 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
964 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
967 return MESSAGE_PORT_ERROR_NONE;
970 int register_message_port(const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
972 _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
975 message_port_local_port_info_s *port_info;
978 return MESSAGE_PORT_ERROR_IO_ERROR;
981 /* Check the message port is already registed */
982 if (is_local_port_registed(local_port, is_trusted, &local_id, &port_info)) {
983 port_info->callback = callback;
984 port_info->user_data = user_data;
985 __callback_info_update_user_data(local_id, callback, user_data);
989 local_id = __register_dbus_interface(local_port, is_trusted);
991 _LOGE("register_dbus_interface fail !!");
992 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
995 if (!__message_port_register_port(local_id, local_port, is_trusted, callback, user_data))
996 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1001 int unregister_local_port(int local_port_id, bool trusted_port)
1005 char *bus_name = NULL;
1008 message_port_local_port_info_s *mi;
1010 _LOGI("unregister : %d", local_port_id);
1012 if (!_initialized) {
1013 if (!__initialize())
1014 return MESSAGE_PORT_ERROR_IO_ERROR;
1017 mi = (message_port_local_port_info_s *)
1018 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
1020 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1022 if (mi->is_trusted != trusted_port)
1023 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1025 g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
1027 bus_name = get_encoded_name(app_id, mi->port_name, mi->is_trusted);
1028 if (bus_name == NULL)
1029 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1031 g_dbus_connection_unregister_object(gdbus_conn, local_port_id);
1033 result = g_dbus_connection_call_sync(
1037 DBUS_INTERFACE_DBUS,
1039 g_variant_new("(s)", bus_name),
1040 G_VARIANT_TYPE("(u)"),
1041 G_DBUS_CALL_FLAGS_NONE,
1050 _LOGE("RequestName fail : %s", err->message);
1052 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1054 g_variant_get(result, "(u)", &ret);
1057 g_variant_unref(result);
1059 if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1061 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1062 _LOGE("Port Not exist");
1063 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1064 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1065 _LOGE("Try to release not owned name. MESSAGE_PORT_ERROR_INVALID_PARAMETER");
1066 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1069 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1071 return MESSAGE_PORT_ERROR_NONE;