Fix finalize request routine
[platform/core/uifw/tts.git] / server / ttsd_dbus.c
old mode 100644 (file)
new mode 100755 (executable)
index 5ae57fb..4b2ab38
@@ -271,18 +271,51 @@ static Eina_Bool listener_event_callback(void* data, Ecore_Fd_Handler *fd_handle
                        ttsd_dbus_server_get_private_data(g_conn_listener, msg);
 
                } else {
-                       SLOG(LOG_DEBUG, TAG_TTSC, "Message is NOT valid");
+                       SLOG(LOG_DEBUG, tts_tag(), "Message is NOT valid");
                        /* Invalid method */
                }
+               /* free the message */
+               dbus_message_unref(msg);
        }
-       /* free the message */
-       dbus_message_unref(msg);
 
        return ECORE_CALLBACK_RENEW;
 }
 
+void __ttsd_dbus_service_free()
+{
+       if (NULL != g_service_name) {
+               free(g_service_name);
+               g_service_name = NULL;
+       }
+
+       if (NULL != g_service_object) {
+               free(g_service_object);
+               g_service_object = NULL;
+       }
+
+       if (NULL != g_service_interface) {
+               free(g_service_interface);
+               g_service_interface = NULL;
+       }
+}
+
+void __ttsd_dbus_connection_free()
+{
+       if (NULL != g_conn_listener) {
+               dbus_connection_close(g_conn_listener);
+               dbus_connection_unref(g_conn_listener);
+               g_conn_listener = NULL;
+       }
+       if (NULL != g_conn_sender) {
+               dbus_connection_close(g_conn_sender);
+               dbus_connection_unref(g_conn_sender);
+               g_conn_sender = NULL;
+       }
+}
+
 int ttsd_dbus_open_connection()
 {
+       SLOG(LOG_DEBUG, tts_tag(), "=== start dbus open connection");
        DBusError err;
        dbus_error_init(&err);
 
@@ -305,19 +338,30 @@ int ttsd_dbus_open_connection()
        if (dbus_error_is_set(&err)) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail dbus_bus_get : %s", err.message);
                dbus_error_free(&err);
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
        if (NULL == g_conn_listener) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get dbus connection");
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
+       __ttsd_dbus_service_free();
+
        if (TTSD_MODE_SCREEN_READER == ttsd_get_mode()) {
                g_service_name = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_NAME) + 1, sizeof(char));
                g_service_object = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
                g_service_interface = (char*)calloc(strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
 
+               if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
+                       SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
+                       __ttsd_dbus_service_free();
+                       __ttsd_dbus_connection_free();
+                       return -1;
+               }
+
                snprintf(g_service_name, strlen(TTS_SR_SERVER_SERVICE_NAME) + 1, "%s", TTS_SR_SERVER_SERVICE_NAME);
                snprintf(g_service_object, strlen(TTS_SR_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_SR_SERVER_SERVICE_OBJECT_PATH);
                snprintf(g_service_interface, strlen(TTS_SR_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SR_SERVER_SERVICE_INTERFACE);
@@ -326,6 +370,13 @@ int ttsd_dbus_open_connection()
                g_service_object = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
                g_service_interface = (char*)calloc(strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
 
+               if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
+                       SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
+                       __ttsd_dbus_service_free();
+                       __ttsd_dbus_connection_free();
+                       return -1;
+               }
+
                snprintf(g_service_name, strlen(TTS_NOTI_SERVER_SERVICE_NAME) + 1, "%s", TTS_NOTI_SERVER_SERVICE_NAME);
                snprintf(g_service_object, strlen(TTS_NOTI_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_NOTI_SERVER_SERVICE_OBJECT_PATH);
                snprintf(g_service_interface, strlen(TTS_NOTI_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_NOTI_SERVER_SERVICE_INTERFACE);
@@ -334,6 +385,13 @@ int ttsd_dbus_open_connection()
                g_service_object = (char*)calloc(strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, sizeof(char));
                g_service_interface = (char*)calloc(strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, sizeof(char));
 
+               if (NULL == g_service_name || NULL == g_service_object || NULL == g_service_interface) {
+                       SLOG(LOG_ERROR, tts_tag(), "[ERROR] Fail to allocate memory");
+                       __ttsd_dbus_service_free();
+                       __ttsd_dbus_connection_free();
+                       return -1;
+               }
+
                snprintf(g_service_name, strlen(TTS_SERVER_SERVICE_NAME) + 1, "%s", TTS_SERVER_SERVICE_NAME);
                snprintf(g_service_object, strlen(TTS_SERVER_SERVICE_OBJECT_PATH) + 1, "%s", TTS_SERVER_SERVICE_OBJECT_PATH);
                snprintf(g_service_interface, strlen(TTS_SERVER_SERVICE_INTERFACE) + 1, "%s", TTS_SERVER_SERVICE_INTERFACE);
@@ -343,12 +401,14 @@ int ttsd_dbus_open_connection()
        ret = dbus_bus_request_name(g_conn_listener, g_service_name, DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
        if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to be primary owner");
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
        if (dbus_error_is_set(&err)) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to request dbus name : %s", err.message);
                dbus_error_free(&err);
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
@@ -362,6 +422,7 @@ int ttsd_dbus_open_connection()
 
        if (dbus_error_is_set(&err)) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_add_match() : %s", err.message);
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
@@ -371,14 +432,17 @@ int ttsd_dbus_open_connection()
        g_dbus_fd_handler = ecore_main_fd_handler_add(fd, ECORE_FD_READ, (Ecore_Fd_Cb)listener_event_callback, g_conn_listener, NULL, NULL);
        if (NULL == g_dbus_fd_handler) {
                SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] Fail to get fd handler");
+               __ttsd_dbus_connection_free();
                return -1;
        }
 
+       SLOG(LOG_DEBUG, tts_tag(), "===");
        return 0;
 }
 
 int ttsd_dbus_close_connection()
 {
+       SLOG(LOG_DEBUG, tts_tag(), "=== start dbus close connection");
        DBusError err;
        dbus_error_init(&err);
 
@@ -387,24 +451,18 @@ int ttsd_dbus_close_connection()
                g_dbus_fd_handler = NULL;
        }
 
-       dbus_bus_release_name(g_conn_listener, g_service_name, &err);
-       if (dbus_error_is_set(&err)) {
-               SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
-               dbus_error_free(&err);
+       if (NULL != g_conn_listener) {
+               dbus_bus_release_name(g_conn_listener, g_service_name, &err);
+               if (dbus_error_is_set(&err)) {
+                       SLOG(LOG_ERROR, tts_tag(), "[Dbus ERROR] dbus_bus_release_name() : %s", err.message);
+                       dbus_error_free(&err);
+               }
        }
 
-       dbus_connection_close(g_conn_sender);
-       dbus_connection_close(g_conn_listener);
-
-       dbus_connection_unref(g_conn_sender);
-       dbus_connection_unref(g_conn_listener);
-
-       g_conn_listener = NULL;
-       g_conn_sender = NULL;
+       __ttsd_dbus_connection_free();
+       __ttsd_dbus_service_free();
 
-       if (NULL != g_service_name)     free(g_service_name);
-       if (NULL != g_service_object)   free(g_service_object);
-       if (NULL != g_service_interface)free(g_service_interface);
+       SLOG(LOG_DEBUG, tts_tag(), "===");
 
        return 0;
 }