Fix watch on not exist name bug
[platform/core/appfw/message-port.git] / src / message-port.c
index 579c9f1..eb86b2f 100755 (executable)
 } while (0)
 
 static bool _initialized = false;
-static GDBusConnection *__gdbus_conn = NULL;
+static GDBusConnection *__gdbus_conn;
 static char *__app_id;
-static GHashTable *__local_port_info = NULL;
-static GHashTable *__remote_app_info = NULL;
-static GHashTable *__sender_appid_hash = NULL;
-static GHashTable *__trusted_app_list_hash = NULL;
+static GHashTable *__local_port_info;
+static GHashTable *__remote_app_info;
+static GHashTable *__sender_appid_hash;
+static GHashTable *__trusted_app_list_hash;
+static GHashTable *__callback_info_hash;
 static const int MAX_MESSAGE_SIZE = 16 * 1024;
 
 enum __certificate_info_type {
@@ -132,14 +133,15 @@ typedef struct port_list_info {
        bool exist;
 } port_list_info_s;
 
-static void __callback_info_free(message_port_callback_info_s *callback_info)
+static void __callback_info_free(gpointer data)
 {
+       message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
        GError *error = NULL;
        if (callback_info == NULL)
                return;
 
        if (callback_info->remote_app_id)
-               free(callback_info->remote_app_id);
+               FREE_AND_NULL(callback_info->remote_app_id);
 
        if (callback_info->gio_read != NULL) {
                g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
@@ -156,7 +158,32 @@ static void __callback_info_free(message_port_callback_info_s *callback_info)
                callback_info->g_src_id = 0;
        }
 
-       free(callback_info);
+       FREE_AND_NULL(callback_info);
+}
+
+static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
+{
+       GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
+       GList *find_list;
+
+       if (callback_info_list == NULL)
+               return;
+
+       find_list = g_list_find(callback_info_list, callback_info);
+       if (find_list == NULL)
+               return;
+
+       callback_info_list = g_list_remove_link(callback_info_list, find_list);
+       __callback_info_free(callback_info);
+       g_list_free(find_list);
+}
+
+static void __hash_destroy_callback_info(gpointer data)
+{
+
+       GList *callback_list = (GList *)data;
+       if (callback_list != NULL)
+               g_list_free_full(callback_list, __callback_info_free);
 }
 
 static char *__get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
@@ -298,9 +325,19 @@ static void on_name_vanished(GDBusConnection *connection,
 {
        _LOGI("name vanished : %s", name);
        port_list_info_s *pli = (port_list_info_s *)user_data;
-       g_bus_unwatch_name(pli->watcher_id);
+       if (pli == NULL) {
+               LOGE("NULL port info");
+               return;
+       }
+
+       _LOGI("watcher_id :%d", pli->watcher_id);
+       if (pli->watcher_id > 0)
+               g_bus_unwatch_name(pli->watcher_id);
+       else
+               _LOGE("Invalid watcher_id %d", pli->watcher_id);
        pli->exist = false;
        pli->watcher_id = 0;
+
        _LOGI("name vanished socket : %d", pli->send_sock_fd);
        if (pli->send_sock_fd > 0) {
                close(pli->send_sock_fd);
@@ -380,6 +417,26 @@ out:
        return remote_app_info;
 }
 
+static void __watch_remote_port_info(port_list_info_s *port_info)
+{
+       if (port_info == NULL)
+               return;
+
+       if (port_info->watcher_id < 1) {
+               port_info->watcher_id = g_bus_watch_name_on_connection(
+                               __gdbus_conn,
+                               port_info->encoded_bus_name,
+                               G_BUS_NAME_WATCHER_FLAGS_NONE,
+                               on_name_appeared,
+                               on_name_vanished,
+                               port_info,
+                               NULL);
+       } else {
+               LOGI("Already watched port info");
+               return;
+       }
+}
+
 static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
                message_port_remote_app_info_s **mri, port_list_info_s **pli)
 {
@@ -418,17 +475,6 @@ static int __get_remote_port_info(const char *remote_app_id, const char *remote_
        } else {
                *pli = (port_list_info_s *)cb_list->data;
        }
-       if ((*pli)->watcher_id < 1) {
-               LOGI("watch remote port : %s", (*pli)->encoded_bus_name);
-               (*pli)->watcher_id = g_bus_watch_name_on_connection(
-                                       __gdbus_conn,
-                                       (*pli)->encoded_bus_name,
-                                       G_BUS_NAME_WATCHER_FLAGS_NONE,
-                                       on_name_appeared,
-                                       on_name_vanished,
-                                       *pli,
-                                       NULL);
-       }
 out:
 
        return ret_val;
@@ -579,7 +625,7 @@ static int __read_string_from_socket(int fd, char **buffer, int *string_len)
                LOGE("read socket fail");
                return MESSAGEPORT_ERROR_IO_ERROR;
        }
-       if (*string_len > 0) {
+       if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
                *buffer = (char *)calloc(*string_len, sizeof(char));
                if (*buffer == NULL) {
                        LOGE("Out of memory.");
@@ -589,6 +635,9 @@ static int __read_string_from_socket(int fd, char **buffer, int *string_len)
                        LOGE("read socket fail");
                        return MESSAGEPORT_ERROR_IO_ERROR;
                }
+       } else {
+               LOGE("Invalid string len %d", &string_len);
+               return MESSAGEPORT_ERROR_IO_ERROR;
        }
        return MESSAGEPORT_ERROR_NONE;
 }
@@ -660,20 +709,20 @@ static gboolean __socket_request_handler(GIOChannel *gio,
        if (cond == G_IO_HUP) {
 
                _LOGI("socket G_IO_HUP");
-               __callback_info_free(mi);
+               __callback_info_free_by_info(mi);
                return FALSE;
 
        } else {
 
                if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
                        _LOGE("fail to get fd from io channel");
-                       __callback_info_free(mi);
+                       __callback_info_free_by_info(mi);
                        return FALSE;
                }
 
                if ((pkt = __message_port_recv_raw(fd)) == NULL) {
                        _LOGE("recv error on SOCKET");
-                       __callback_info_free(mi);
+                       __callback_info_free_by_info(mi);
                        return FALSE;
                }
 
@@ -712,6 +761,8 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation
        message_port_local_port_info_s *mi;
        int local_reg_id = 0;
        message_port_callback_info_s *callback_info;
+       message_port_callback_info_s *head_callback_info;
+       GList *callback_info_list = NULL;
 
        char buf[1024];
        GDBusMessage *msg;
@@ -780,27 +831,55 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation
 
        msg = g_dbus_method_invocation_get_message(invocation);
        fd_list = g_dbus_message_get_unix_fd_list(msg);
-       returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
-       fd = returned_fds[0];
-
-       LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
-       if (fd > 0) {
 
-               callback_info->gio_read = g_io_channel_unix_new(fd);
-               if (!callback_info->gio_read) {
-                       _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
+       /* When application send message to self fd_list is NULL */
+       if (fd_list != NULL) {
+               returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
+               if (returned_fds == NULL) {
+                       _LOGE("fail to get fds");
                        __callback_info_free(callback_info);
                        return -1;
                }
+               fd = returned_fds[0];
 
-               callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
-                               __socket_request_handler, (gpointer)callback_info);
-               if (callback_info->g_src_id == 0) {
-                       _LOGE("fail to add watch on socket");
-                       __callback_info_free(callback_info);
-                       return -1;
-               }
+               LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
+               if (fd > 0) {
+
+                       callback_info->gio_read = g_io_channel_unix_new(fd);
+                       if (!callback_info->gio_read) {
+                               _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
+                               __callback_info_free(callback_info);
+                               return -1;
+                       }
+
+                       callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
+                                       __socket_request_handler, (gpointer)callback_info);
+                       if (callback_info->g_src_id == 0) {
+                               _LOGE("fail to add watch on socket");
+                               __callback_info_free(callback_info);
+                               return -1;
+                       }
 
+                       callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
+                       if (callback_info_list == NULL) {
+                               head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
+                               if (head_callback_info == NULL) {
+                                       _LOGE("fail to alloc head_callback_info");
+                                       __callback_info_free(callback_info);
+                                       return -1;
+                               }
+                               head_callback_info->local_id = 0;
+                               head_callback_info->remote_app_id = NULL;
+                               head_callback_info->callback = NULL;
+                               head_callback_info->gio_read = NULL;
+                               head_callback_info->g_src_id = 0;
+                               callback_info_list = g_list_append(callback_info_list, head_callback_info);
+                               callback_info_list = g_list_append(callback_info_list, callback_info);
+                               g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
+                       } else {
+                               callback_info_list = g_list_append(callback_info_list, callback_info);
+                       }
+               }
        }
 
        data = bundle_decode(raw, len);
@@ -900,6 +979,7 @@ static int __check_remote_port(const char *remote_app_id, const char *remote_por
                        port_info->exist = true;
                        *exist = true;
                        ret_val = MESSAGEPORT_ERROR_NONE;
+                       __watch_remote_port_info(port_info);
                }
        }
 out:
@@ -921,11 +1001,21 @@ static void __on_sender_name_vanished(GDBusConnection *connection,
                const gchar     *name,
                gpointer         user_data)
 {
-       _LOGI("sender name vanished : %s", name);
+       gboolean remove_result = FALSE;
        int *watcher_id = (int *)user_data;
-       g_bus_unwatch_name(*watcher_id);
-       free(watcher_id);
-       g_hash_table_remove(__sender_appid_hash, name);
+       remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
+       if (!remove_result)
+               _LOGE("Fail to remove sender appid from hash : %s", name);
+
+       if (watcher_id) {
+               if (*watcher_id > 0)
+                       g_bus_unwatch_name(*watcher_id);
+               else
+                       LOGE("Invalid watcher_id %d", *watcher_id);
+               free(watcher_id);
+       } else {
+               LOGE("watcher_id is NULL");
+       }
 }
 
 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
@@ -1130,7 +1220,7 @@ out:
        return registration_id;
 }
 
-
+/* LCOV_EXCL_START */
 void __list_free_port_list(gpointer data)
 {
        port_list_info_s *n = (port_list_info_s *)data;
@@ -1139,7 +1229,9 @@ void __list_free_port_list(gpointer data)
        FREE_AND_NULL(n->port_name);
        FREE_AND_NULL(n);
 }
+/* LCOV_EXCL_STOP */
 
+/* LCOV_EXCL_START */
 static void __hash_destory_local_value(gpointer data)
 {
        message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
@@ -1149,7 +1241,9 @@ static void __hash_destory_local_value(gpointer data)
                free(mli);
        }
 }
+/* LCOV_EXCL_STOP */
 
+/* LCOV_EXCL_START */
 static void __hash_destory_remote_value(gpointer data)
 {
        message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
@@ -1162,6 +1256,7 @@ static void __hash_destory_remote_value(gpointer data)
                free(mri);
        }
 }
+/* LCOV_EXCL_STOP */
 
 static bool __initialize(void)
 {
@@ -1193,7 +1288,7 @@ static bool __initialize(void)
        }
 
        if (__sender_appid_hash == NULL) {
-               __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
+               __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
                retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
        }
 
@@ -1202,6 +1297,11 @@ static bool __initialize(void)
                retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
        }
 
+       if (__callback_info_hash == NULL) {
+               __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
+               retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
+       }
+
        if (!__dbus_init())
                return false;
        _initialized = true;
@@ -1308,7 +1408,6 @@ static int __message_port_send_message(const char *remote_appid, const char *rem
 
        int ret = MESSAGEPORT_ERROR_NONE;
        GUnixFDList *fd_list = NULL;
-       GError *error = NULL;
 
        int len = 0;
        bundle_raw *raw = NULL;
@@ -1337,6 +1436,7 @@ static int __message_port_send_message(const char *remote_appid, const char *rem
                        goto out;
                }
        }
+       __watch_remote_port_info(port_info);
 
        if (port_info->send_sock_fd > 0) {
                ret = __message_port_send_async(port_info->send_sock_fd, message,
@@ -1371,7 +1471,7 @@ static int __message_port_send_message(const char *remote_appid, const char *rem
                                fd_list = g_unix_fd_list_new();
                                g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
                                if (err != NULL) {
-                                       _LOGE("g_unix_fd_list_append [%s]", error->message);
+                                       _LOGE("g_unix_fd_list_append [%s]", err->message);
                                        ret = MESSAGEPORT_ERROR_IO_ERROR;
                                        g_error_free(err);
                                        goto out;
@@ -1390,7 +1490,6 @@ static int __message_port_send_message(const char *remote_appid, const char *rem
 
                g_dbus_message_set_unix_fd_list(msg, fd_list);
                g_dbus_message_set_body(msg, body);
-               g_dbus_message_set_flags(msg, G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED);
                g_dbus_connection_send_message(__gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
                if (err != NULL) {
                        _LOGE("No reply. error = %s", err->message);
@@ -1445,6 +1544,8 @@ int messageport_unregister_local_port(int local_port_id, bool trusted_port)
        if (mi->is_trusted != trusted_port)
                return MESSAGEPORT_ERROR_INVALID_PARAMETER;
 
+       g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
+
        bus_name = __get_encoded_name(__app_id, mi->port_name, mi->is_trusted);
        if (bus_name == NULL)
                return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
@@ -1588,32 +1689,3 @@ int messageport_send_bidirectional_trusted_message(int id, const char *remote_ap
        return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);
 }
 
-int messageport_get_local_port_name(int id, char **name)
-{
-       message_port_local_port_info_s *local_info;
-       int ret = __get_local_port_info(id, &local_info);
-
-       if (ret != MESSAGEPORT_ERROR_NONE)
-               return ret;
-
-       *name = strdup(local_info->port_name);
-
-       if (*name == NULL)
-               return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-
-       return MESSAGEPORT_ERROR_NONE;
-}
-
-int messageport_check_trusted_local_port(int id, bool *trusted)
-{
-       message_port_local_port_info_s *local_info;
-       int ret = __get_local_port_info(id, &local_info);
-
-       if (ret != MESSAGEPORT_ERROR_NONE)
-               return ret;
-
-       *trusted = local_info->is_trusted;
-
-       return MESSAGEPORT_ERROR_NONE;;
-}
-