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);
257 if (read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGE_PORT_ERROR_NONE) {
258 /* LCOV_EXCL_START */
259 LOGE("read socket fail: port_name");
260 free(pkt->remote_port_name);
266 if (read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGE_PORT_ERROR_NONE) {
267 /* LCOV_EXCL_START */
268 LOGE("read socket fail: is_bidirection");
269 free(pkt->remote_port_name);
275 if (read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGE_PORT_ERROR_NONE) {
276 /* LCOV_EXCL_START */
277 LOGE("read socket fail: is_trusted");
278 free(pkt->remote_port_name);
284 if (read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGE_PORT_ERROR_NONE) {
285 /* LCOV_EXCL_START */
286 LOGE("read socket fail: data");
289 free(pkt->remote_port_name);
298 static bool __validate_callback_info(callback_key_info_s *key_info)
302 cb_list = g_hash_table_lookup(__callback_info_hash,
303 GUINT_TO_POINTER(key_info->local_id));
304 if (cb_list == NULL) {
305 _LOGI("local_info : %d is already released", key_info->local_id);
309 cb_list = g_list_find(cb_list, key_info->callback_info);
310 if (cb_list == NULL) {
311 _LOGI("local_info : %d is already released from list",
319 static bool __validate_local_info(callback_key_info_s *key_info)
323 cb_list = g_hash_table_lookup(__local_port_info,
324 GUINT_TO_POINTER(key_info->local_id));
325 if (cb_list == NULL) {
326 _LOGI("local_info : %d is already released", key_info->local_id);
333 static gboolean __socket_request_handler(GIOChannel *gio,
338 message_port_callback_info_s *mi = NULL;
339 callback_key_info_s *key_info;
340 message_port_pkt_s *pkt = NULL;
341 message_port_local_port_info_s *local_port_info;
346 key_info = (callback_key_info_s *)data;
347 if (key_info == NULL)
350 message_port_lock_mutex();
351 if (__validate_callback_info(key_info) == false) {
354 message_port_unlock_mutex();
358 if (__validate_local_info(key_info) == false) {
360 message_port_unlock_mutex();
363 message_port_unlock_mutex();
365 mi = key_info->callback_info;
367 local_port_info = mi->local_info;
368 if (local_port_info == NULL || local_port_info->callback == NULL) {
369 _LOGE("Failed to get callback info");
374 if (cond == G_IO_HUP) {
375 _LOGI("socket G_IO_HUP");
380 fd = g_io_channel_unix_get_fd(gio);
382 _LOGE("fail to get fd from io channel");
387 pkt = __message_port_recv_raw(fd);
389 _LOGE("recv error on SOCKET");
394 kb = bundle_decode(pkt->data, pkt->data_len);
396 _LOGE("Invalid argument : message");
401 if (pkt->is_bidirection)
402 local_port_info->callback(mi->local_id, mi->remote_app_id,
403 pkt->remote_port_name, pkt->is_trusted, kb, local_port_info->user_data);
405 local_port_info->callback(mi->local_id, mi->remote_app_id,
406 NULL, pkt->is_trusted, kb, local_port_info->user_data);
412 if (pkt->remote_port_name)
413 free(pkt->remote_port_name);
419 if (mi && ret == FALSE && existed == TRUE) {
420 message_port_lock_mutex();
421 __callback_info_free_by_info(mi);
422 message_port_unlock_mutex();
429 static void __socket_destroy_handler(gpointer data)
431 _LOGI("__socket_destroy_handler");
432 callback_key_info_s *key_info = (callback_key_info_s *)data;
436 static callback_key_info_s *__create_callback_key_info(message_port_callback_info_s *callback_info)
438 callback_key_info_s *_key_info = (callback_key_info_s *)
439 calloc(1, sizeof(callback_key_info_s));
441 if (_key_info == NULL) {
442 _LOGE("out of memory");
446 _key_info->local_id = callback_info->local_id;
447 _key_info->callback_info = callback_info;
452 static message_port_callback_info_s *__create_callback_info(message_port_local_port_info_s *mi, char *local_appid)
454 message_port_local_port_info_s *local_info = NULL;
455 message_port_callback_info_s *callback_info = NULL;
458 callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
459 if (callback_info == NULL) {
460 _LOGE("out of memory");
464 local_info = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
465 if (local_info == NULL) {
467 _LOGE("out of memory");
471 callback_info->local_id = mi->local_id;
472 callback_info->local_info = local_info;
473 callback_info->remote_app_id = strdup(local_appid);
474 if (callback_info->remote_app_id == NULL) {
476 _LOGE("out of memory");
480 local_info->port_name = strdup(mi->port_name);
481 if (local_info->port_name == NULL) {
483 _LOGE("out of memory");
487 local_info->callback = mi->callback;
488 local_info->is_trusted = mi->is_trusted;
489 local_info->local_id = mi->local_id;
490 local_info->user_data = mi->user_data;
494 __callback_info_free(callback_info);
498 return callback_info;
501 static bool __callback_info_append(message_port_callback_info_s *callback_info)
503 GList *callback_info_list = NULL;
504 message_port_callback_info_s *head_callback_info;
506 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
507 if (callback_info_list == NULL) {
508 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
509 if (head_callback_info == NULL) {
510 _LOGE("fail to alloc head_callback_info");
513 head_callback_info->local_id = 0;
514 head_callback_info->remote_app_id = NULL;
515 head_callback_info->local_info = NULL;
516 head_callback_info->gio_read = NULL;
517 head_callback_info->g_src_id = 0;
518 callback_info_list = g_list_append(callback_info_list, head_callback_info);
519 callback_info_list = g_list_append(callback_info_list, callback_info);
520 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id), callback_info_list);
522 callback_info_list = g_list_append(callback_info_list, callback_info);
528 static void __callback_info_update_user_data(int local_id, message_port_message_cb callback, void *user_data)
530 GList *callback_info_list;
532 message_port_callback_info_s *callback_info;
534 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(local_id));
535 if (callback_info_list != NULL) {
536 for (iter = callback_info_list; iter != NULL; iter = iter->next) {
537 callback_info = (message_port_callback_info_s *)iter->data;
538 if (callback_info->local_info != NULL) {
539 callback_info->local_info->callback = callback;
540 callback_info->local_info->user_data = user_data;
544 _LOGE("fail to find local_id %d ", local_id);
548 static bool __receive_message(GVariant *parameters, GDBusMethodInvocation *invocation)
550 char *local_port = NULL;
551 char *local_appid = NULL;
552 char *remote_appid = NULL;
553 char *remote_port = NULL;
554 gboolean local_trusted = false;
555 gboolean remote_trusted = false;
556 gboolean bi_dir = false;
560 bundle_raw *raw = NULL;
561 message_port_local_port_info_s *mi;
562 int local_reg_id = 0;
563 message_port_callback_info_s *callback_info = NULL;
564 callback_key_info_s *key_info;
568 GUnixFDList *fd_list;
570 int *returned_fds = NULL;
575 g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
576 &remote_appid, &remote_port, &remote_trusted, &len, &raw);
579 _LOGE("Invalid argument : remote_port is NULL");
583 _LOGE("Invalid argument : remote_appid is NULL");
588 _LOGE("Invalid argument : local_appid");
592 message_port_lock_mutex();
593 if (!is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
594 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
595 message_port_unlock_mutex();
599 callback_info = __create_callback_info(mi, local_appid);
600 if (callback_info == NULL) {
601 message_port_unlock_mutex();
604 message_port_unlock_mutex();
607 _LOGE("Invalid argument : local_port");
610 if (strcmp(remote_appid, app_id) != 0) {
611 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
614 if (strcmp(remote_port, callback_info->local_info->port_name) != 0) {
615 _LOGE("Invalid argument : remote_port (%s)", remote_port);
619 _LOGE("Invalid argument : data_len");
622 if (remote_trusted) {
623 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
624 if (!is_preloaded(local_appid, remote_appid)) {
625 int ret = check_certificate(local_appid, remote_appid);
626 if (ret == MESSAGE_PORT_ERROR_NONE) {
627 key_appid = strdup(local_appid);
629 g_hash_table_insert(__trusted_app_list_hash, key_appid, "TRUE");
631 _LOGE("The application (%s) is not signed with the same certificate",
639 data = bundle_decode(raw, len);
641 _LOGE("Invalid argument : message");
645 LOGD("call calback %s", local_appid);
647 callback_info->local_info->callback(callback_info->local_info->local_id,
648 local_appid, local_port, local_trusted, data, callback_info->local_info->user_data);
650 callback_info->local_info->callback(callback_info->local_info->local_id,
651 local_appid, NULL, false, data, callback_info->local_info->user_data);
656 msg = g_dbus_method_invocation_get_message(invocation);
657 fd_list = g_dbus_message_get_unix_fd_list(msg);
659 /* When application send message to self fd_list is NULL */
660 if (fd_list != NULL) {
661 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
662 if (returned_fds == NULL) {
663 _LOGE("fail to get fds");
667 fd = returned_fds[0];
669 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
671 callback_info->gio_read = g_io_channel_unix_new(fd);
672 if (!callback_info->gio_read) {
673 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
678 key_info = __create_callback_key_info(callback_info);
679 if (key_info == NULL) {
680 _LOGE("out of memory");
685 callback_info->g_src_id = g_io_add_watch_full(
686 callback_info->gio_read,
689 __socket_request_handler,
691 __socket_destroy_handler);
692 if (callback_info->g_src_id == 0) {
693 _LOGE("fail to add watch on socket");
699 message_port_lock_mutex();
700 if (__callback_info_append(callback_info) == false) {
701 _LOGE("fail to append callback_info");
704 message_port_unlock_mutex();
710 __callback_info_free(callback_info);
713 g_free(returned_fds);
718 static void __on_sender_name_appeared(GDBusConnection *connection,
720 const gchar *name_owner,
723 _LOGI("sender name appeared : %s", name);
726 static void __on_sender_name_vanished(GDBusConnection *connection,
730 int *watcher_id = (int *)user_data;
733 local_appid = (char *)g_hash_table_lookup(__sender_appid_hash, name);
735 g_hash_table_remove(__trusted_app_list_hash, (gpointer)local_appid);
736 g_hash_table_remove(__sender_appid_hash, (gpointer)name);
741 g_bus_unwatch_name(*watcher_id);
743 LOGE("Invalid watcher_id %d", *watcher_id);
746 LOGE("watcher_id is NULL");
750 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
753 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
754 char *local_appid = NULL;
755 int pid = __get_sender_pid(conn, sender);
756 int *watcher_id = (int *)calloc(1, sizeof(int));
759 retvm_if(!watcher_id, false, "Malloc failed");
761 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
762 if (ret != AUL_R_OK) {
763 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
768 g_variant_get_child(parameters, 0, "&s", &local_appid);
769 if (local_appid == NULL) {
770 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
775 if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
776 _LOGD("insert sender !!!!! %s", sender);
777 _sender = strdup(sender);
778 if (_sender == NULL) {
779 _LOGE("out of memory");
784 _appid = strdup(local_appid);
785 if (_appid == NULL) {
786 _LOGE("out of memory");
792 g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, (gpointer)_appid);
793 *watcher_id = g_bus_watch_name_on_connection(
796 G_BUS_NAME_WATCHER_FLAGS_NONE,
797 __on_sender_name_appeared,
798 __on_sender_name_vanished,
808 static void __close_socket(GDBusMethodInvocation *invocation)
811 GUnixFDList *fd_list;
813 int *returned_fds = NULL;
815 msg = g_dbus_method_invocation_get_message(invocation);
816 fd_list = g_dbus_message_get_unix_fd_list(msg);
817 if (fd_list != NULL) {
818 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
819 if (returned_fds != NULL) {
820 close(returned_fds[0]);
821 g_free(returned_fds);
825 static void __dbus_method_call_handler(GDBusConnection *conn,
826 const gchar *sender, const gchar *object_path,
827 const gchar *iface_name, const gchar *method_name,
828 GVariant *parameters, GDBusMethodInvocation *invocation,
831 _LOGI("method_name: %s, sender: %s", method_name, sender);
832 gpointer sender_appid = g_hash_table_lookup(__sender_appid_hash, sender);
833 if (sender_appid == NULL) {
834 if (!__check_sender_validation(parameters, sender, conn)) {
835 _LOGE("Failed to validate");
836 __close_socket(invocation);
841 if (g_strcmp0(method_name, "send_message") == 0)
842 __receive_message(parameters, invocation);
844 g_dbus_method_invocation_return_value(invocation, NULL);
847 static const GDBusInterfaceVTable interface_vtable = {
848 __dbus_method_call_handler,
853 static int __register_dbus_interface(const char *port_name, bool is_trusted)
856 GDBusNodeInfo *introspection_data = NULL;
857 int registration_id = 0;
859 static gchar introspection_prefix[] =
861 " <interface name='";
863 static gchar introspection_postfix[] =
865 " <method name='send_message'>"
866 " <arg type='s' name='local_appid' direction='in'/>"
867 " <arg type='s' name='local_port' direction='in'/>"
868 " <arg type='b' name='local_trusted' direction='in'/>"
869 " <arg type='b' name='bi_dir' direction='in'/>"
870 " <arg type='s' name='remote_appid' direction='in'/>"
871 " <arg type='s' name='remote_port' direction='in'/>"
872 " <arg type='b' name='remote_trusted' direction='in'/>"
873 " <arg type='u' name='data_len' direction='in'/>"
874 " <arg type='s' name='data' direction='in'/>"
879 char *introspection_xml = NULL;
880 int introspection_xml_len = 0;
884 GError *error = NULL;
885 char *bus_name = NULL;
886 char *interface_name = NULL;
887 GVariant *result = NULL;
889 bus_name = get_encoded_name(app_id, port_name, is_trusted);
891 _LOGE("Fail to get bus name");
894 interface_name = bus_name;
896 introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
897 strlen(introspection_postfix) + 1;
899 introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
900 if (!introspection_xml) {
901 _LOGE("out of memory");
906 result = g_dbus_connection_call_sync(
912 g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
913 G_VARIANT_TYPE("(u)"),
914 G_DBUS_CALL_FLAGS_NONE,
919 _LOGE("RequestName fail : %s", error->message);
923 if (result == NULL) {
924 _LOGE("fail to get name NULL");
927 g_variant_get(result, "(u)", &owner_id);
929 _LOGE("Acquiring the own name is failed");
933 _LOGD("Acquiring the own name : %d", owner_id);
935 snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
937 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
938 if (!introspection_data) {
939 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
943 registration_id = g_dbus_connection_register_object(gdbus_conn,
944 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
945 &interface_vtable, NULL, NULL, NULL);
947 _LOGD("registration_id %d", registration_id);
949 if (registration_id == 0) {
950 _LOGE("Failed to g_dbus_connection_register_object");
955 if (introspection_data)
956 g_dbus_node_info_unref(introspection_data);
957 if (introspection_xml)
958 free(introspection_xml);
962 g_variant_unref(result);
965 return registration_id;
968 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)
970 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
971 retvm_if(!mi, false, "Malloc failed");
973 mi->callback = callback;
974 mi->is_trusted = is_trusted;
975 mi->port_name = strdup(local_port);
976 if (mi->port_name == NULL) {
977 _LOGE("Malloc failed (%s)", local_port);
981 mi->local_id = local_id;
982 mi->user_data = user_data;
984 g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
988 int get_local_port_info(int id, message_port_local_port_info_s **info)
990 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
993 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
996 return MESSAGE_PORT_ERROR_NONE;
999 int register_message_port(const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
1001 _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
1004 message_port_local_port_info_s *port_info;
1005 if (!_initialized) {
1006 if (!__initialize())
1007 return MESSAGE_PORT_ERROR_IO_ERROR;
1010 /* Check the message port is already registed */
1011 if (is_local_port_registed(local_port, is_trusted, &local_id, &port_info)) {
1012 port_info->callback = callback;
1013 port_info->user_data = user_data;
1014 __callback_info_update_user_data(local_id, callback, user_data);
1018 local_id = __register_dbus_interface(local_port, is_trusted);
1020 _LOGE("register_dbus_interface fail !!");
1021 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1024 if (!__message_port_register_port(local_id, local_port, is_trusted, callback, user_data))
1025 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1030 int unregister_local_port(int local_port_id, bool trusted_port)
1034 char *bus_name = NULL;
1037 message_port_local_port_info_s *mi;
1039 _LOGI("unregister : %d", local_port_id);
1041 if (!_initialized) {
1042 if (!__initialize())
1043 return MESSAGE_PORT_ERROR_IO_ERROR;
1046 mi = (message_port_local_port_info_s *)
1047 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
1049 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1051 if (mi->is_trusted != trusted_port)
1052 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1054 g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
1056 bus_name = get_encoded_name(app_id, mi->port_name, mi->is_trusted);
1057 if (bus_name == NULL)
1058 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1060 g_dbus_connection_unregister_object(gdbus_conn, local_port_id);
1062 result = g_dbus_connection_call_sync(
1066 DBUS_INTERFACE_DBUS,
1068 g_variant_new("(s)", bus_name),
1069 G_VARIANT_TYPE("(u)"),
1070 G_DBUS_CALL_FLAGS_NONE,
1079 _LOGE("RequestName fail : %s", err->message);
1081 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1083 g_variant_get(result, "(u)", &ret);
1086 g_variant_unref(result);
1088 if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1090 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1091 _LOGE("Port Not exist");
1092 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1093 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1094 _LOGE("Try to release not owned name. MESSAGE_PORT_ERROR_INVALID_PARAMETER");
1095 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1098 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1100 return MESSAGE_PORT_ERROR_NONE;