Release version 1.3.4
[platform/core/appfw/message-port.git] / src / message-port.c
index 53a0f73..3e6525b 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;
@@ -299,7 +298,7 @@ static char *__get_encoded_name(const char *remote_app_id, const char *port_name
        if (bus_name)
                free(bus_name);
 
-       _LOGI("encoded_bus_name : %s ", md5_interface);
+       _LOGD("encoded_bus_name : %s ", md5_interface);
 
        return md5_interface;
 }
@@ -318,7 +317,7 @@ static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
 
 static bool __is_preloaded(const char *local_appid, const char *remote_appid)
 {
-       _LOGI("IsPreloaded");
+       _LOGD("IsPreloaded");
 
        bool preload_local = false;
        bool preload_remote = false;
@@ -361,7 +360,7 @@ static bool __is_preloaded(const char *local_appid, const char *remote_appid)
 
 static int __check_certificate(const char *local_appid, const char *remote_appid)
 {
-       _LOGI("CheckCertificate");
+       _LOGD("CheckCertificate");
 
        pkgmgrinfo_cert_compare_result_type_e res;
        int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
@@ -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)
-{
-       _LOGI("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;
-       }
-
-       _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;
-
-}
-
 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 {
-               LOGI("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)
@@ -569,6 +551,10 @@ static int __get_remote_port_info(const char *remote_app_id, const char *remote_
        *mri = remote_app_info;
 
        port_info.port_name = strdup(remote_port);
+       if (port_info.port_name == NULL) {
+               ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
        port_info.is_trusted = is_trusted;
        cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
                                        (GCompareFunc)__remote_port_compare_cb);
@@ -581,6 +567,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;
@@ -907,7 +894,7 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation
        bundle_raw *raw = NULL;
        message_port_local_port_info_s *mi;
        int local_reg_id = 0;
-       message_port_callback_info_s *callback_info;
+       message_port_callback_info_s *callback_info = NULL;
        message_port_callback_info_s *head_callback_info;
        GList *callback_info_list = NULL;
 
@@ -917,6 +904,7 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation
        int fd_len;
        int *returned_fds = NULL;
        int fd;
+       bool ret = false;
 
        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);
@@ -974,6 +962,10 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation
 
        callback_info->local_id = mi->local_id;
        callback_info->remote_app_id = strdup(local_appid);
+       if (callback_info->remote_app_id == NULL) {
+               _LOGE("out of memory");
+               goto out;
+       }
        callback_info->callback = mi->callback;
 
        msg = g_dbus_method_invocation_get_message(invocation);
@@ -984,8 +976,7 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation
                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 false;
+                       goto out;
                }
                fd = returned_fds[0];
 
@@ -995,16 +986,14 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation
                        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 false;
+                               goto out;
                        }
 
                        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 false;
+                               goto out;
                        }
 
                        callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
@@ -1012,8 +1001,7 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation
                                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 false;
+                                       goto out;
                                }
                                head_callback_info->local_id = 0;
                                head_callback_info->remote_app_id = NULL;
@@ -1035,22 +1023,27 @@ static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation
                goto out;
        }
 
-       LOGI("call calback %s", local_appid);
+       LOGD("call calback %s", local_appid);
        if (bi_dir)
                mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
        else
                mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
        bundle_free(data);
+
+       ret = true;
 out:
+       if (ret == false)
+               __callback_info_free(callback_info);
+
        if (returned_fds)
                free(returned_fds);
 
-       return true;
+       return ret;
 }
 
 static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
 {
-       _LOGI("Check a remote port : [%s:%s]", remote_app_id, remote_port);
+       _LOGD("Check a remote port : [%s:%s]", remote_app_id, remote_port);
 
        GVariant *result = NULL;
        GError *err = NULL;
@@ -1062,7 +1055,7 @@ static int __check_remote_port(const char *remote_app_id, const char *remote_por
        message_port_local_port_info_s *mi = NULL;
        gboolean name_exist = false;
 
-       _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
+       _LOGD("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
 
        ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
        if (ret_val != MESSAGEPORT_ERROR_NONE)
@@ -1071,13 +1064,13 @@ static int __check_remote_port(const char *remote_app_id, const char *remote_por
        /* self check */
        if (strcmp(remote_app_id, __app_id) == 0) {
 
-               _LOGI("__is_local_port_registed ");
+               _LOGD("__is_local_port_registed ");
                if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
                        *exist = false;
                else
                        *exist = true;
 
-               _LOGI("__is_local_port_registed : %d ", *exist);
+               _LOGD("__is_local_port_registed : %d ", *exist);
                return MESSAGEPORT_ERROR_NONE;
        }
 
@@ -1126,13 +1119,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;
 }
 
@@ -1172,6 +1167,8 @@ static bool __check_sender_validation(GVariant *parameters, const char *sender,
        char *local_appid = NULL;
        int pid = __get_sender_pid(conn, sender);
        int *watcher_id = (int *)calloc(1, sizeof(int));
+       char *_sender;
+       retvm_if(!watcher_id, false, "Malloc failed");
 
        ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
        if (ret != AUL_R_OK) {
@@ -1188,8 +1185,14 @@ static bool __check_sender_validation(GVariant *parameters, const char *sender,
        }
 
        if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
-               _LOGI("insert sender !!!!! %s", sender);
-               g_hash_table_insert(__sender_appid_hash, (gpointer)strdup(sender), GINT_TO_POINTER(pid));
+               _LOGD("insert sender !!!!! %s", sender);
+               _sender = strdup(sender);
+               if (_sender == NULL) {
+                       _LOGE("out of memory");
+                       free(watcher_id);
+                       return false;
+               }
+               g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, GINT_TO_POINTER(pid));
                *watcher_id = g_bus_watch_name_on_connection(
                                        __gdbus_conn,
                                        sender,
@@ -1333,7 +1336,7 @@ int __register_dbus_interface(const char *port_name, bool is_trusted)
                goto out;
        }
 
-       _LOGI("Acquiring the own name : %d", owner_id);
+       _LOGD("Acquiring the own name : %d", owner_id);
 
        snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
 
@@ -1347,7 +1350,7 @@ int __register_dbus_interface(const char *port_name, bool is_trusted)
                                                MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
                                                &interface_vtable, NULL, NULL, NULL);
 
-       _LOGI("registration_id %d", registration_id);
+       _LOGD("registration_id %d", registration_id);
 
        if (registration_id == 0) {
                _LOGE("Failed to g_dbus_connection_register_object");
@@ -1369,17 +1372,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;
@@ -1396,10 +1388,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);
        }
@@ -1417,7 +1408,6 @@ static bool __initialize(void)
        int ret = 0;
        char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
 
-       _LOGI("initialize");
        ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
        retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
 
@@ -1447,7 +1437,7 @@ static bool __initialize(void)
 
        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");
+               retvm_if(!__callback_info_hash, false, "fail to create __callback_info_hash");
        }
 
        if (!__dbus_init())
@@ -1479,7 +1469,7 @@ static bool __message_port_register_port(const int local_id, const char *local_p
 
 static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
 {
-       _SECURE_LOGI("Register a message port : [%s:%s]", __app_id, local_port);
+       _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
 
        int local_id = 0;
 
@@ -1660,7 +1650,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;
                }
@@ -1851,14 +1841,12 @@ static int __message_port_send_message(const char *remote_appid, const char *rem
 
        if (port_info->exist == false) {
                bool exist = false;
-               _LOGI("port exist check !!");
+               _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 +1899,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 +1929,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 +1940,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])
@@ -1968,7 +1957,7 @@ int __message_send_bidirectional_message(int id, const char *remote_app_id, cons
        if (ret != MESSAGEPORT_ERROR_NONE)
                return ret;
 
-       _LOGI("bidirectional_message %s", local_info->port_name);
+       _LOGD("bidirectional_message %s", local_info->port_name);
        return __message_port_send_message(remote_app_id, remote_port,
                        local_info->port_name, trusted_message, local_info->is_trusted, true, message);
 }
@@ -2022,6 +2011,8 @@ int __messageport_watch_remote_port(int *watcher_id, const char *remote_app_id,
                __registered_callback_info_hash = g_hash_table_new_full(g_direct_hash,  g_direct_equal, NULL, __registered_callback_info_free);
 
        registered_callback_info_s *registered_cb_info = (registered_callback_info_s *)calloc(1, sizeof(registered_callback_info_s));
+       retvm_if(!registered_cb_info, MESSAGEPORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
+
        registered_cb_info->registered_cb = registered_cb;
        registered_cb_info->unregistered_cb = unregistered_cb;
        registered_cb_info->user_data = user_data;