From: Hyunho Kang Date: Thu, 19 May 2016 13:33:07 +0000 (+0900) Subject: Fix memory leak X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bb654db366b9f4e3fbd62470e464c5a55c6f8c65;p=platform%2Fcore%2Fappfw%2Fmessage-port.git Fix memory leak Signed-off-by: Hyunho Kang --- diff --git a/src/message-port.c b/src/message-port.c index fb1163a..a85010e 100755 --- a/src/message-port.c +++ b/src/message-port.c @@ -81,8 +81,8 @@ static bool _initialized = false; static GDBusConnection *__gdbus_conn = NULL; static char *__app_id; static GHashTable *__local_port_info = NULL; -static GHashTable *__remote_port_info = NULL;; -static GHashTable *__sender_appid_hash = NULL;; +static GHashTable *__remote_app_info = NULL; +static GHashTable *__sender_appid_hash = NULL; static GHashTable *__trusted_app_list_hash = NULL; static const int MAX_MESSAGE_SIZE = 16 * 1024; @@ -300,6 +300,7 @@ static void on_name_vanished(GDBusConnection *connection, port_list_info_s *pli = (port_list_info_s *)user_data; g_bus_unwatch_name(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); @@ -353,7 +354,6 @@ out: static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted) { - port_list_info_s *port_info = NULL; message_port_remote_app_info_s *remote_app_info = NULL; int ret_val = MESSAGEPORT_ERROR_NONE; @@ -369,14 +369,6 @@ static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_ goto out; } - port_info = __set_remote_port_info(remote_app_id, remote_port, is_trusted); - if (port_info == NULL) { - ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY; - goto out; - } - - remote_app_info->port_list = g_list_append(remote_app_info->port_list, port_info); - out: if (ret_val != MESSAGEPORT_ERROR_NONE) { if (remote_app_info) { @@ -396,7 +388,7 @@ static int __get_remote_port_info(const char *remote_app_id, const char *remote_ GList *cb_list = NULL; int ret_val = MESSAGEPORT_ERROR_NONE; - remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_port_info, remote_app_id); + remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id); if (remote_app_info == NULL) { remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted); @@ -405,8 +397,7 @@ static int __get_remote_port_info(const char *remote_app_id, const char *remote_ ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY; goto out; } - g_hash_table_insert(__remote_port_info, remote_app_info->remote_app_id, remote_app_info); - + g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info); } *mri = remote_app_info; @@ -418,18 +409,26 @@ static int __get_remote_port_info(const char *remote_app_id, const char *remote_ free(port_info.port_name); if (cb_list == NULL) { port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted); - if (tmp == NULL) { ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY; goto out; } remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp); *pli = tmp; - g_hash_table_insert(__remote_port_info, (*pli)->encoded_bus_name, *pli); } 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; @@ -716,16 +715,17 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation bundle *data = NULL; bundle_raw *raw = NULL; message_port_local_port_info_s *mi; - message_port_callback_info_s *callback_info; int local_reg_id = 0; + message_port_callback_info_s *callback_info; + char buf[1024]; GDBusMessage *msg; GUnixFDList *fd_list; int fd_len; - int *returned_fds; + int *returned_fds = NULL; int fd; - g_variant_get(parameters, "(ssbbssbus)", &local_appid, &local_port, &local_trusted, &bi_dir, + g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir, &remote_appid, &remote_port, &remote_trusted, &len, &raw); if (!remote_port) { @@ -763,7 +763,6 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation if (remote_trusted) { if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) { if (!__is_preloaded(local_appid, remote_appid)) { - /* Check the certificate */ int ret = __check_certificate(local_appid, remote_appid); if (ret == MESSAGEPORT_ERROR_NONE) g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE"); @@ -810,8 +809,6 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation } data = bundle_decode(raw, len); - bundle_free_encoded_rawdata(&raw); - if (!data) { _LOGE("Invalid argument : message"); goto out; @@ -823,8 +820,11 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation else mi->callback(mi->local_id, local_appid, NULL, false, data, NULL); -out: + bundle_free(data); +out: + if (returned_fds) + free(returned_fds); return true; } @@ -848,7 +848,6 @@ static int __check_remote_port(const char *remote_app_id, const char *remote_por if (ret_val != MESSAGEPORT_ERROR_NONE) return ret_val; - /* self check */ if (strcmp(remote_app_id, __app_id) == 0) { @@ -904,22 +903,10 @@ static int __check_remote_port(const char *remote_app_id, const char *remote_por remote_app_info->certificate_info = CERTIFICATE_MATCH; } } - - 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); - port_info->exist = true; *exist = true; ret_val = MESSAGEPORT_ERROR_NONE; - _LOGI("Exist port: %s", bus_name); } - } out: if (result) @@ -928,24 +915,53 @@ out: return ret_val; } +static void __on_sender_name_appeared(GDBusConnection *connection, + const gchar *name, + const gchar *name_owner, + gpointer user_data) +{ + _LOGI("sender name appeared : %s", name); +} + +static void __on_sender_name_vanished(GDBusConnection *connection, + const gchar *name, + gpointer user_data) +{ + _LOGI("sender name vanished : %s", name); + int *watcher_id = (int *)user_data; + g_bus_unwatch_name(*watcher_id); + free(watcher_id); + + g_hash_table_remove_all(__sender_appid_hash); + _LOGI("sender name vanished hash size : %d", g_hash_table_size(__sender_appid_hash)); +} + static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn) { int ret = 0; char buffer[MAX_PACKAGE_STR_SIZE] = {0, }; char *local_appid = NULL; int pid = __get_sender_pid(conn, sender); + int *watcher_id = (int *)calloc(1, sizeof(int)); ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer)); retvm_if(ret != AUL_R_OK, false, "Failed to get the sender ID: (%s) (%d)", sender, pid); - g_variant_get_child(parameters, 0, "s", &local_appid); + g_variant_get_child(parameters, 0, "&s", &local_appid); retvm_if(!local_appid, false, "remote_appid is NULL (%s) (%d)", sender, pid); if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) { - g_hash_table_insert(__sender_appid_hash, strdup(sender), GINT_TO_POINTER(pid)); - g_free(local_appid); + _LOGI("insert sender !!!!! %s", sender); + g_hash_table_insert(__sender_appid_hash, (gpointer)strdup(sender), GINT_TO_POINTER(pid)); + *watcher_id = g_bus_watch_name_on_connection( + __gdbus_conn, + sender, + G_BUS_NAME_WATCHER_FLAGS_NONE, + __on_sender_name_appeared, + __on_sender_name_vanished, + watcher_id, + NULL); } else { - g_free(local_appid); return false; } return true; @@ -957,15 +973,17 @@ static void __dbus_method_call_handler(GDBusConnection *conn, GVariant *parameters, GDBusMethodInvocation *invocation, gpointer user_data) { - _LOGI("method_name: %s", method_name); + _LOGI("method_name: %s, sender: %s", method_name, sender); gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender); if (sender_pid == NULL) { if (!__check_sender_validation(parameters, sender, conn)) - return; + goto out; } - if (g_strcmp0(method_name, "send_message") == 0) send_message(parameters, invocation); +out: + g_object_unref(invocation); + g_dbus_connection_flush(conn, NULL, NULL, NULL); } @@ -999,7 +1017,6 @@ out: } - int __register_dbus_interface(const char *port_name, bool is_trusted) { @@ -1166,13 +1183,13 @@ static bool __initialize(void) retvm_if(!__local_port_info, false, "fail to create __local_port_info"); } - if (__remote_port_info == NULL) { - __remote_port_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value); - retvm_if(!__remote_port_info, false, "fail to create __remote_port_info"); + if (__remote_app_info == NULL) { + __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value); + retvm_if(!__remote_app_info, false, "fail to create __remote_app_info"); } if (__sender_appid_hash == NULL) { - __sender_appid_hash = g_hash_table_new(g_str_hash, g_str_equal); + __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash"); } @@ -1333,6 +1350,7 @@ static int __message_port_send_message(const char *remote_appid, const char *rem if (MAX_MESSAGE_SIZE < len) { _LOGE("The size of message (%d) has exceeded the maximum limit.", len); ret = MESSAGEPORT_ERROR_MAX_EXCEEDED; + goto out; } body = g_variant_new("(ssbbssbus)", __app_id, (local_port) ? local_port : "", local_trusted, bi_dir, @@ -1383,6 +1401,8 @@ static int __message_port_send_message(const char *remote_appid, const char *rem out: if (msg) g_object_unref(msg); + if (body) + g_variant_unref(body); if (raw) bundle_free_encoded_rawdata(&raw); if (fd_list) diff --git a/src/message_port.c b/src/message_port.c index 69e7755..b6d22e7 100644 --- a/src/message_port.c +++ b/src/message_port.c @@ -34,12 +34,10 @@ static pthread_mutex_t __mutex = PTHREAD_MUTEX_INITIALIZER; static void do_callback(message_port_message_cb callback, int local_port_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, bundle *message, void *user_data) { - if (callback) { + if (callback) callback(local_port_id, remote_app_id, remote_port, trusted_remote_port, message, user_data); - bundle_free(message); - } else { + else _LOGI("Ignored"); - } } static void message_dispatcher(int local_port_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, bundle *message, void *user_data)