Fix dbus timing issue 86/308186/3 tizen
authorjusung son <jusung07.son@samsung.com>
Tue, 19 Mar 2024 07:55:04 +0000 (16:55 +0900)
committerjusung son <jusung07.son@samsung.com>
Tue, 19 Mar 2024 08:14:09 +0000 (17:14 +0900)
- Register the callback first before registering the bus name

Change-Id: I94394cf7568bc2b58a6b8d0ca5f344698db8a44a
Signed-off-by: jusung son <jusung07.son@samsung.com>
src/message_port_remote.c

index c18dc48..e0dd26c 100644 (file)
@@ -873,11 +873,35 @@ static const GDBusInterfaceVTable interface_vtable = {
        NULL
 };
 
-static int __register_dbus_interface(const char *port_name, bool is_trusted)
+static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
+{
+       message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
+       retvm_if(!mi, false, "Malloc failed");
+
+       mi->callback = callback;
+       mi->is_trusted = is_trusted;
+       mi->port_name = strdup(local_port);
+       if (mi->port_name == NULL) {
+/* LCOV_EXCL_START */
+               _LOGE("Malloc failed (%s)", local_port);
+               free(mi);
+               return false;
+/* LCOV_EXCL_STOP */
+       }
+       mi->local_id = local_id;
+       mi->user_data = user_data;
+
+       g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
+       return true;
+}
+
+static int __register_dbus_interface(const char *port_name,
+               bool is_trusted, message_port_message_cb callback, void *user_data)
 {
 
        GDBusNodeInfo *introspection_data = NULL;
        int registration_id = 0;
+       int ret = MESSAGE_PORT_ERROR_NONE;
 
        static gchar introspection_prefix[] =
                "<node>"
@@ -902,7 +926,6 @@ static int __register_dbus_interface(const char *port_name, bool is_trusted)
        char *introspection_xml = NULL;
        int introspection_xml_len = 0;
 
-
        int owner_id = 0;
        GError *error = NULL;
        char *bus_name = NULL;
@@ -927,6 +950,32 @@ static int __register_dbus_interface(const char *port_name, bool is_trusted)
 /* LCOV_EXCL_STOP */
        }
 
+       snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
+
+       introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
+       if (!introspection_data) {
+               ret = MESSAGE_PORT_ERROR_IO_ERROR;
+               _LOGE("g_dbus_node_info_new_for_xml() is failed.");
+               goto out;
+       }
+
+       registration_id = g_dbus_connection_register_object(gdbus_conn,
+                                               MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
+                                               &interface_vtable, NULL, NULL, NULL);
+
+       _LOGD("registration_id %d", registration_id);
+
+       if (registration_id == 0) {
+               ret = MESSAGE_PORT_ERROR_IO_ERROR;
+               _LOGE("Failed to g_dbus_connection_register_object");
+               goto out;
+       }
+
+       if (!__message_port_register_port(registration_id, port_name, is_trusted,
+                               callback, user_data)) {
+               ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
+               goto out;
+       }
 
        result = g_dbus_connection_call_sync(
                        gdbus_conn,
@@ -942,39 +991,23 @@ static int __register_dbus_interface(const char *port_name, bool is_trusted)
                        &error);
        if (error) {
                _LOGE("RequestName fail : %s", error->message);
+               ret = MESSAGE_PORT_ERROR_IO_ERROR;
                g_error_free(error);
                goto out;
        }
        if (result == NULL) {
+               ret = MESSAGE_PORT_ERROR_IO_ERROR;
                _LOGE("fail to get name NULL");
                goto out;
        }
        g_variant_get(result, "(u)", &owner_id);
        if (owner_id == 0) {
+               ret = MESSAGE_PORT_ERROR_IO_ERROR;
                _LOGE("Acquiring the own name is failed");
                goto out;
        }
 
-       _LOGD("Acquiring the own name : %d", owner_id);
-
-       snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
-
-       introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
-       if (!introspection_data) {
-               _LOGE("g_dbus_node_info_new_for_xml() is failed.");
-               goto out;
-       }
-
-       registration_id = g_dbus_connection_register_object(gdbus_conn,
-                                               MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
-                                               &interface_vtable, NULL, NULL, NULL);
-
-       _LOGD("registration_id %d", registration_id);
-
-       if (registration_id == 0) {
-               _LOGE("Failed to g_dbus_connection_register_object");
-               goto out;
-       }
+       _LOGI("Acquiring the own name : %d", owner_id);
 
 out:
        if (introspection_data)
@@ -986,30 +1019,13 @@ out:
        if (result)
                g_variant_unref(result);
 
-
-       return registration_id;
-}
-
-static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
-{
-       message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
-       retvm_if(!mi, false, "Malloc failed");
-
-       mi->callback = callback;
-       mi->is_trusted = is_trusted;
-       mi->port_name = strdup(local_port);
-       if (mi->port_name == NULL) {
-/* LCOV_EXCL_START */
-               _LOGE("Malloc failed (%s)", local_port);
-               free(mi);
-               return false;
-/* LCOV_EXCL_STOP */
+       if (ret != MESSAGE_PORT_ERROR_NONE) {
+               if (registration_id != 0)
+                       g_hash_table_remove(__local_port_info, GINT_TO_POINTER(registration_id));
+               registration_id = ret;
        }
-       mi->local_id = local_id;
-       mi->user_data = user_data;
 
-       g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
-       return true;
+       return registration_id;
 }
 
 int get_local_port_info(int id, message_port_local_port_info_s **info)
@@ -1042,15 +1058,11 @@ int register_message_port(const char *local_port, bool is_trusted, message_port_
                return local_id;
        }
 
-       local_id = __register_dbus_interface(local_port, is_trusted);
+       local_id = __register_dbus_interface(local_port, is_trusted, callback, user_data);
        if (local_id < 1) {
                _LOGE("register_dbus_interface fail !!");
-               return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
        }
 
-       if (!__message_port_register_port(local_id, local_port, is_trusted, callback, user_data))
-               return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
-
        return local_id;
 }