Check null before add match if dbus was closed or not 16/203616/1 accepted/tizen/unified/20190425.014612 submit/tizen/20190424.011236
authorwn.jang <wn.jang@samsung.com>
Tue, 16 Apr 2019 08:13:55 +0000 (17:13 +0900)
committerwn.jang <wn.jang@samsung.com>
Tue, 16 Apr 2019 08:13:55 +0000 (17:13 +0900)
Change-Id: I61726c900d2638ebc330a97643e3afe16276ab15
Signed-off-by: wn.jang <wn.jang@samsung.com>
client/vc_dbus.c
client/vc_mgr_dbus.c
client/vc_widget_dbus.c

index 5ea2841..996a188 100644 (file)
 #include "vc_dbus.h"
 #include "vc_main.h"
 
+static pthread_mutex_t g_dbus_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static int g_waiting_time = 3000;
+static bool g_is_connection_opened = false;
 
 static Ecore_Fd_Handler* g_fd_handler = NULL;
 
@@ -243,8 +245,11 @@ static void __vc_dbus_connection_free()
 
 int vc_dbus_open_connection()
 {
+       pthread_mutex_lock(&g_dbus_mutex);
+
        if (NULL != g_conn_sender && NULL != g_conn_listener) {
                SLOG(LOG_WARN, TAG_VCC, "already existed connection "); //LCOV_EXCL_LINE
+               pthread_mutex_unlock(&g_dbus_mutex);
                return 0;
        }
 
@@ -264,6 +269,7 @@ int vc_dbus_open_connection()
 
        if (NULL == g_conn_sender) {
                SLOG(LOG_ERROR, TAG_VCC, "Fail to get dbus connection "); //LCOV_EXCL_LINE
+               pthread_mutex_unlock(&g_dbus_mutex);
                return VC_ERROR_OPERATION_FAILED;
        }
 
@@ -279,6 +285,7 @@ int vc_dbus_open_connection()
        if (NULL == g_conn_listener) {
                SLOG(LOG_ERROR, TAG_VCC, "Fail to get dbus connection "); //LCOV_EXCL_LINE
                __vc_dbus_connection_free();
+               pthread_mutex_unlock(&g_dbus_mutex);
                return VC_ERROR_OPERATION_FAILED;
        }
 
@@ -303,13 +310,15 @@ int vc_dbus_open_connection()
        if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
                SLOG(LOG_ERROR, TAG_VCC, "fail dbus_bus_request_name()"); //LCOV_EXCL_LINE
                __vc_dbus_connection_free();
-               return -2;
+               pthread_mutex_unlock(&g_dbus_mutex);
+               return VC_ERROR_OPERATION_FAILED;
        }
 
        if (NULL != g_fd_handler) {
                SLOG(LOG_WARN, TAG_VCC, "The handler already exists."); //LCOV_EXCL_LINE
                __vc_dbus_connection_free();
-               return 0;
+               pthread_mutex_unlock(&g_dbus_mutex);
+               return VC_ERROR_NONE;
        }
 
        char rule[256] = {0, };
@@ -322,6 +331,7 @@ int vc_dbus_open_connection()
                SLOG(LOG_ERROR, TAG_VCC, "Match Error (%s)", err.message); //LCOV_EXCL_LINE
                dbus_error_free(&err);
                __vc_dbus_connection_free();
+               pthread_mutex_unlock(&g_dbus_mutex);
                return VC_ERROR_OPERATION_FAILED;
        }
 
@@ -329,6 +339,7 @@ int vc_dbus_open_connection()
        if (1 != dbus_connection_get_unix_fd(g_conn_listener, &fd)) {
                SLOG(LOG_ERROR, TAG_VCC, "fail to get fd from dbus "); //LCOV_EXCL_LINE
                __vc_dbus_connection_free();
+               pthread_mutex_unlock(&g_dbus_mutex);
                return VC_ERROR_OPERATION_FAILED;
        } else {
                SLOG(LOG_DEBUG, TAG_VCC, "Get fd from dbus : %d", fd);
@@ -338,14 +349,23 @@ int vc_dbus_open_connection()
        if (NULL == g_fd_handler) {
                SLOG(LOG_ERROR, TAG_VCC, "fail to get fd handler from ecore "); //LCOV_EXCL_LINE
                __vc_dbus_connection_free();
+               pthread_mutex_unlock(&g_dbus_mutex);
                return VC_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       g_is_connection_opened = true;
+
+       pthread_mutex_unlock(&g_dbus_mutex);
+
+       SLOG(LOG_INFO, TAG_VCC, "[INFO] vc client dbus connection is opened");
+
+       return VC_ERROR_NONE;
 }
 
 int vc_dbus_close_connection()
 {
+       pthread_mutex_lock(&g_dbus_mutex);
+
        DBusError err;
        dbus_error_init(&err);
 
@@ -371,6 +391,12 @@ int vc_dbus_close_connection()
 
        __vc_dbus_connection_free();
 
+       g_is_connection_opened = false;
+
+       pthread_mutex_unlock(&g_dbus_mutex);
+
+       SLOG(LOG_INFO, TAG_VCC, "[INFO] vc client dbus connection is closed");
+
        return 0;
 }
 
@@ -527,6 +553,15 @@ int vc_dbus_request_initialize(int pid, int* mgr_pid, int* service_state, int* d
                        /* add a rule for daemon error */
                        char rule[256] = {0, };
                        snprintf(rule, 256, "sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',type='signal',arg0='%s'", VC_SERVER_SERVICE_INTERFACE);
+                       if (NULL == g_conn_listener) {
+                               if (g_is_connection_opened) {
+                                       SLOG(LOG_ERROR, TAG_VCC, "[ERROR] g_conn_listener is NULL abnormally");
+                                       return VC_ERROR_OPERATION_FAILED;
+                               } else {
+                                       SLOG(LOG_INFO, TAG_VCC, "[INFO] g_conn_listener is NULL and DBUS connection was closed");
+                                       return VC_ERROR_NONE;
+                               }
+                       }
                        dbus_bus_add_match(g_conn_listener, rule, &err);
 
                        if (dbus_error_is_set(&err)) {
index 195d2c7..8a6eff3 100644 (file)
 #include "vc_command.h"
 
 
+static pthread_mutex_t g_m_dbus_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 static int g_m_waiting_time = 3000;
+static bool g_is_connection_opened = false;
 
 static Ecore_Fd_Handler* g_m_fd_handler = NULL;
 
@@ -646,9 +649,12 @@ static void __vc_mgr_dbus_connection_free()
 
 int vc_mgr_dbus_open_connection()
 {
+       pthread_mutex_lock(&g_m_dbus_mutex);
+
        if (NULL != g_m_conn_sender && NULL != g_m_conn_listener) {
                SLOG(LOG_WARN, TAG_VCM, "already existed connection ");
-               return 0;
+               pthread_mutex_unlock(&g_m_dbus_mutex);
+               return VC_ERROR_NONE;
        }
 
        DBusError err;
@@ -667,6 +673,7 @@ int vc_mgr_dbus_open_connection()
 
        if (NULL == g_m_conn_sender) {
                SLOG(LOG_ERROR, TAG_VCM, "Fail to get dbus connection ");
+               pthread_mutex_unlock(&g_m_dbus_mutex);
                return VC_ERROR_OPERATION_FAILED;
        }
 
@@ -683,6 +690,7 @@ int vc_mgr_dbus_open_connection()
        if (NULL == g_m_conn_listener) {
                SLOG(LOG_ERROR, TAG_VCM, "Fail to get dbus connection ");
                __vc_mgr_dbus_connection_free();
+               pthread_mutex_unlock(&g_m_dbus_mutex);
                return VC_ERROR_OPERATION_FAILED;
        }
 
@@ -701,13 +709,15 @@ int vc_mgr_dbus_open_connection()
        if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
                SLOG(LOG_ERROR, TAG_VCM, "fail dbus_bus_request_name()");
                __vc_mgr_dbus_connection_free();
-               return -2;
+               pthread_mutex_unlock(&g_m_dbus_mutex);
+               return VC_ERROR_OPERATION_FAILED;
        }
 
        if (NULL != g_m_fd_handler) {
                SLOG(LOG_WARN, TAG_VCM, "The handler already exists.");
                __vc_mgr_dbus_connection_free();
-               return 0;
+               pthread_mutex_unlock(&g_m_dbus_mutex);
+               return VC_ERROR_NONE;
        }
 
        char rule[128] = {0, };
@@ -720,6 +730,7 @@ int vc_mgr_dbus_open_connection()
                SLOG(LOG_ERROR, TAG_VCM, "Match Error (%s)", err.message);
                dbus_error_free(&err);
                __vc_mgr_dbus_connection_free();
+               pthread_mutex_unlock(&g_m_dbus_mutex);
                return VC_ERROR_OPERATION_FAILED;
        }
 
@@ -727,6 +738,7 @@ int vc_mgr_dbus_open_connection()
        if (1 != dbus_connection_get_unix_fd(g_m_conn_listener, &fd)) {
                SLOG(LOG_ERROR, TAG_VCM, "fail to get fd from dbus ");
                __vc_mgr_dbus_connection_free();
+               pthread_mutex_unlock(&g_m_dbus_mutex);
                return VC_ERROR_OPERATION_FAILED;
        } else {
                SLOG(LOG_DEBUG, TAG_VCM, "Get fd from dbus : %d", fd);
@@ -737,14 +749,23 @@ int vc_mgr_dbus_open_connection()
        if (NULL == g_m_fd_handler) {
                SLOG(LOG_ERROR, TAG_VCM, "fail to get fd handler from ecore ");
                __vc_mgr_dbus_connection_free();
+               pthread_mutex_unlock(&g_m_dbus_mutex);
                return VC_ERROR_OPERATION_FAILED;
        }
 
-       return 0;
+       g_is_connection_opened = true;
+
+       pthread_mutex_unlock(&g_m_dbus_mutex);
+
+       SLOG(LOG_INFO, TAG_VCM, "[INFO] manager dbus connection is opened");
+
+       return VC_ERROR_NONE;
 }
 
 int vc_mgr_dbus_close_connection()
 {
+       pthread_mutex_lock(&g_m_dbus_mutex);
+
        DBusError err;
        dbus_error_init(&err);
 
@@ -770,7 +791,13 @@ int vc_mgr_dbus_close_connection()
 
        __vc_mgr_dbus_connection_free();
 
-       return 0;
+       g_is_connection_opened = false;
+
+       pthread_mutex_unlock(&g_m_dbus_mutex);
+
+       SLOG(LOG_INFO, TAG_VCM, "[INFO] manager dbus connection is closed");
+
+       return VC_ERROR_NONE;
 }
 
 int vc_mgr_dbus_reconnect()
@@ -969,6 +996,15 @@ int vc_mgr_dbus_request_initialize(int pid, int audio_streaming_mode, int* servi
                        /* add a rule for daemon error */
                        char rule_err[256] = {0, };
                        snprintf(rule_err, 256, "sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',type='signal',arg0='%s'", VC_SERVER_SERVICE_INTERFACE);
+                       if (NULL == g_m_conn_listener) {
+                               if (g_is_connection_opened) {
+                                       SLOG(LOG_ERROR, TAG_VCM, "[ERROR] g_m_conn_listener is NULL abnormally");
+                                       return VC_ERROR_OPERATION_FAILED;
+                               } else {
+                                       SLOG(LOG_INFO, TAG_VCM, "[INFO] g_m_conn_listener is NULL and DBUS connection was closed");
+                                       return VC_ERROR_NONE;
+                               }
+                       }
                        dbus_bus_add_match(g_m_conn_listener, rule_err, &err);
 
                        if (dbus_error_is_set(&err)) {
index 52bee5e..adc05f5 100644 (file)
@@ -25,6 +25,7 @@
 static pthread_mutex_t g_w_dbus_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static int g_w_waiting_time = 3000;
+static bool g_is_connection_opened = false;
 
 static Ecore_Fd_Handler* g_w_fd_handler = NULL;
 
@@ -279,7 +280,7 @@ int vc_widget_dbus_open_connection()
        if (NULL != g_w_conn_sender && NULL != g_w_conn_listener) {
                SLOG(LOG_WARN, TAG_VCW, "Already existed connection ");
                pthread_mutex_unlock(&g_w_dbus_mutex);
-               return 0;
+               return VC_ERROR_NONE;
        }
 
        DBusError err;
@@ -383,7 +384,12 @@ int vc_widget_dbus_open_connection()
                return VC_ERROR_OPERATION_FAILED;
        }
 
+       g_is_connection_opened = true;
+
        pthread_mutex_unlock(&g_w_dbus_mutex);
+
+       SLOG(LOG_INFO, TAG_VCW, "[INFO] widget dbus connection is opened");
+
        return 0;
 }
 
@@ -416,8 +422,12 @@ int vc_widget_dbus_close_connection()
 
        __vc_widget_dbus_connection_free();
 
+       g_is_connection_opened = false;
+
        pthread_mutex_unlock(&g_w_dbus_mutex);
 
+       SLOG(LOG_INFO, TAG_VCW, "[INFO] widget dbus connection is closed");
+
        return 0;
 }
 
@@ -572,6 +582,15 @@ int vc_widget_dbus_request_initialize(int pid, int* service_state, int* daemon_p
                        /* add a rule for daemon error */
                        char rule_err[256] = {0, };
                        snprintf(rule_err, 256, "sender='org.freedesktop.DBus',path='/org/freedesktop/DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',type='signal',arg0='%s'", VC_SERVER_SERVICE_INTERFACE);
+                       if (NULL == g_w_conn_listener) {
+                               if (g_is_connection_opened) {
+                                       SLOG(LOG_ERROR, TAG_VCW, "[ERROR] g_w_conn_listener is NULL abnormally");
+                                       return VC_ERROR_OPERATION_FAILED;
+                               } else {
+                                       SLOG(LOG_INFO, TAG_VCW, "[INFO] g_w_conn_listener is NULL and DBUS connection was closed");
+                                       return VC_ERROR_NONE;
+                               }
+                       }
                        dbus_bus_add_match(g_w_conn_listener, rule_err, &err);
 
                        if (dbus_error_is_set(&err)) {