Release memory for remote app info 26/138626/6
authorJiwoong Im <jiwoong.im@samsung.com>
Thu, 13 Jul 2017 02:36:13 +0000 (11:36 +0900)
committerJiwoong Im <jiwoong.im@samsung.com>
Thu, 20 Jul 2017 23:44:43 +0000 (08:44 +0900)
- Release memory for remote app info when the socket which is connected
  with remote app is closed.

Change-Id: Ic4c87809d8c5955b63d92b383a1b0c512f280485
Signed-off-by: Jiwoong Im <jiwoong.im@samsung.com>
src/message-port.c

index e25e5a2..9933467 100755 (executable)
@@ -123,18 +123,17 @@ typedef struct message_port_local_port_info {
 } message_port_local_port_info_s;
 
 typedef struct message_port_remote_port_info {
-       char *sender_id;
        char *remote_app_id;
        int certificate_info;
        GList *port_list;
 } message_port_remote_app_info_s;
 
 typedef struct port_list_info {
+       message_port_remote_app_info_s *remote_app_info;
        char *port_name;
        char *encoded_bus_name;
        bool is_trusted;
        int send_sock_fd;
-       int watcher_id;
        bool exist;
        GIOChannel *gio_read;
        int g_src_id;
@@ -377,35 +376,6 @@ static int __check_certificate(const char *local_appid, const char *remote_appid
        return MESSAGEPORT_ERROR_NONE;
 }
 
-static void on_name_appeared(GDBusConnection *connection,
-               const gchar     *name,
-               const gchar     *name_owner,
-               gpointer         user_data)
-{
-       _LOGD("name appeared : %s %s", __app_id, name);
-}
-
-static void on_name_vanished(GDBusConnection *connection,
-               const gchar     *name,
-               gpointer         user_data)
-{
-       _LOGI("name vanished : %s", name);
-       port_list_info_s *pli = (port_list_info_s *)user_data;
-       if (pli == NULL) {
-               LOGE("NULL port info");
-               return;
-       }
-
-       _LOGD("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;
-
-}
-
 static int __get_local_port_info(int id, message_port_local_port_info_s **info)
 {
        message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
@@ -484,7 +454,6 @@ static void __clear_disconnect_socket(port_list_info_s *port_info)
 
        if (port_info == NULL)
                return;
-       _LOGI("__clear_disconnect_socket : fd [%d]", port_info->send_sock_fd);
 
        if (port_info->gio_read != NULL) {
                g_io_channel_shutdown(port_info->gio_read, TRUE, &error);
@@ -516,36 +485,49 @@ static void __clear_disconnect_socket(port_list_info_s *port_info)
        port_info->send_sock_fd = 0;
 }
 
-static gboolean __socket_disconnect_handler(GIOChannel *gio,
-               GIOCondition cond,
-               gpointer data)
+/* LCOV_EXCL_START */
+void __free_port_info(gpointer data)
 {
-       /* It's sender socket's gio channel so, only EOF can be received */
        port_list_info_s *port_info = (port_list_info_s *)data;
-       _LOGI("__socket_disconnect_handler %d", cond);
-       __clear_disconnect_socket(port_info);
-       return FALSE;
-}
+       message_port_remote_app_info_s *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 {
-               LOGD("Already watched port info");
-               return;
+       remote_app_info = port_info->remote_app_info;
+
+       _LOGI("__free_port_info : remote_app_id : %s port_name : %s",
+                       remote_app_info->remote_app_id,
+                       port_info->port_name);
+
+       remote_app_info->port_list = g_list_remove(remote_app_info->port_list,
+                       port_info);
+
+       __clear_disconnect_socket(port_info);
+
+       if (port_info->encoded_bus_name)
+               free(port_info->encoded_bus_name);
+       if (port_info->port_name)
+               free(port_info->port_name);
+
+       free(port_info);
+
+       if (g_list_length(remote_app_info->port_list) == 0) {
+               g_hash_table_remove(__remote_app_info,
+                               remote_app_info->remote_app_id);
        }
 }
+/* LCOV_EXCL_STOP */
+
+static gboolean __socket_disconnect_handler(GIOChannel *gio,
+               GIOCondition cond,
+               gpointer data)
+{
+       _LOGI("__socket_disconnect_handler %d", cond);
+       __free_port_info(data);
+
+       return FALSE;
+}
 
 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)
@@ -581,6 +563,7 @@ static int __get_remote_port_info(const char *remote_app_id, const char *remote_
                        goto out;
                }
                remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
+               tmp->remote_app_info = remote_app_info;
                *pli = tmp;
        } else {
                *pli = (port_list_info_s *)cb_list->data;
@@ -1126,13 +1109,15 @@ 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:
        if (result)
                g_variant_unref(result);
 
+       if (ret_val != MESSAGEPORT_ERROR_NONE || !name_exist)
+               __free_port_info((gpointer)port_info);
+
        return ret_val;
 }
 
@@ -1370,17 +1355,6 @@ out:
 }
 
 /* LCOV_EXCL_START */
-void __list_free_port_list(gpointer data)
-{
-       port_list_info_s *n = (port_list_info_s *)data;
-
-       FREE_AND_NULL(n->encoded_bus_name);
-       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;
@@ -1397,10 +1371,9 @@ static void __hash_destory_remote_value(gpointer data)
 {
        message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
        if (mri) {
-               FREE_AND_NULL(mri->sender_id);
                FREE_AND_NULL(mri->remote_app_id);
                if (mri->port_list)
-                       g_list_free_full(mri->port_list, __list_free_port_list);
+                       g_list_free_full(mri->port_list, __free_port_info);
 
                free(mri);
        }
@@ -1660,7 +1633,7 @@ static gboolean __process_delayed_message(gint fd, GIOCondition cond, gpointer d
                        pthread_mutex_unlock(&mutex);
                        return G_SOURCE_CONTINUE;
                } else if (ret == MESSAGEPORT_ERROR_IO_ERROR) {
-                       __clear_disconnect_socket(port_info);
+                       __free_port_info((gpointer)port_info);
                        pthread_mutex_unlock(&mutex);
                        return G_SOURCE_REMOVE;
                }
@@ -1853,12 +1826,10 @@ static int __message_port_send_message(const char *remote_appid, const char *rem
                bool exist = false;
                _LOGD("port exist check !!");
                ret =  __check_remote_port(remote_appid, remote_port, trusted_message, &exist);
-               if (ret != MESSAGEPORT_ERROR_NONE) {
-                       goto out;
-               } else if (!exist) {
-                       ret = MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
-                       goto out;
-               }
+               if (ret != MESSAGEPORT_ERROR_NONE)
+                       return ret;
+               else if (!exist)
+                       return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
        }
 
        if (port_info->send_sock_fd > 0) {
@@ -1911,8 +1882,11 @@ static int __message_port_send_message(const char *remote_appid, const char *rem
                                        goto out;
                                }
 
-                               port_info->g_src_id = g_io_add_watch(port_info->gio_read, G_IO_IN | G_IO_HUP,
-                                               __socket_disconnect_handler, (gpointer)port_info);
+                               port_info->g_src_id = g_io_add_watch(
+                                               port_info->gio_read,
+                                               G_IO_IN | G_IO_HUP,
+                                               __socket_disconnect_handler,
+                                               (gpointer)port_info);
                                if (port_info->g_src_id == 0) {
                                        _LOGE("fail to add watch on socket");
                                        ret = MESSAGEPORT_ERROR_IO_ERROR;
@@ -1938,8 +1912,6 @@ static int __message_port_send_message(const char *remote_appid, const char *rem
                        ret = MESSAGEPORT_ERROR_IO_ERROR;
                        goto out;
                }
-               __watch_remote_port_info(port_info);
-
        }
 
 out:
@@ -1951,7 +1923,7 @@ out:
                g_object_unref(fd_list);
 
        if (ret != MESSAGEPORT_ERROR_NONE) {
-               __clear_disconnect_socket(port_info);
+               __free_port_info((gpointer)port_info);
                if (sock_pair[SOCK_PAIR_SENDER])
                        close(sock_pair[SOCK_PAIR_SENDER]);
                if (sock_pair[SOCK_PAIR_RECEIVER])