Fix watch on not exist name bug
[platform/core/appfw/message-port.git] / src / message-port.c
index 15f94be..eb86b2f 100755 (executable)
@@ -141,7 +141,7 @@ static void __callback_info_free(gpointer data)
                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);
@@ -158,12 +158,11 @@ static void __callback_info_free(gpointer data)
                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;
 
@@ -176,6 +175,7 @@ static void __callback_info_free_by_info(message_port_callback_info_s *callback_
 
        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)
@@ -325,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);
@@ -407,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)
 {
@@ -445,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;
@@ -742,6 +761,7 @@ 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];
@@ -842,6 +862,18 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation
 
                        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 {
@@ -947,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:
@@ -974,8 +1007,15 @@ static void __on_sender_name_vanished(GDBusConnection *connection,
        if (!remove_result)
                _LOGE("Fail to remove sender appid from hash : %s", name);
 
-       g_bus_unwatch_name(*watcher_id);
-       free(watcher_id);
+       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)
@@ -1396,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,