Add mutex encapsulation 61/178961/4
authorjusung son <jusung07.son@samsung.com>
Tue, 15 May 2018 05:53:29 +0000 (14:53 +0900)
committerjusung son <jusung07.son@samsung.com>
Tue, 15 May 2018 06:00:25 +0000 (15:00 +0900)
 - The mutex global variable can be interfered by other modules.

Change-Id: I2f2ff916f9a70ab25f84599ef19759d19d5faa80
Signed-off-by: jusung son <jusung07.son@samsung.com>
src/inc/message_port_common.h
src/message_port.c
src/message_port_common.c
src/message_port_local.c
src/message_port_remote.c

index 914aacbbd4178ee378362134cbb1da3dd8538041..035d094ea40cf1d141f9b806c89654f85c2d8057 100755 (executable)
@@ -77,6 +77,8 @@ bool initialize_common();
 bool is_preloaded(const char *local_appid, const char *remote_appid);
 int check_certificate(const char *local_appid, const char *remote_appid);
 char *get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted);
+void message_port_lock_mutex();
+void message_port_unlock_mutex();
 
 extern bool initialized_common;
 extern GDBusConnection *gdbus_conn;
index 89778b6b33d53dc7f10f2730b4ced248bb43263a..56212b7c6d4df1cbb112fa554d66f6b88568ec3b 100755 (executable)
@@ -25,8 +25,6 @@
 #include "message_port_log.h"
 #include "message_port.h"
 
-pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-
 int message_port_register_local_port(const char *local_port, message_port_message_cb callback, void *user_data)
 {
        int local_port_id;
@@ -36,9 +34,9 @@ int message_port_register_local_port(const char *local_port, message_port_messag
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        local_port_id = register_message_port(local_port, false, callback, user_data);
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        if (local_port_id > 0)
                _LOGD("Register local port ID (%d).", local_port_id);
@@ -57,9 +55,9 @@ int message_port_register_trusted_local_port(const char *local_port, message_por
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        trusted_local_port_id = register_message_port(local_port, true, callback, user_data);
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        if (trusted_local_port_id > 0)
                _LOGD("Register trusted local port ID (%d).", trusted_local_port_id);
@@ -78,9 +76,9 @@ int message_port_unregister_local_port(int local_port_id)
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        res = unregister_local_port(local_port_id, false);
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        return res;
 }
@@ -94,9 +92,9 @@ int message_port_unregister_trusted_local_port(int trusted_local_port_id)
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        res = unregister_local_port(trusted_local_port_id, true);
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        return res;
 }
@@ -111,9 +109,9 @@ int message_port_check_remote_port(const char *remote_app_id, const char *remote
        }
        _LOGD("Check remote port (%s):(%s).", remote_app_id, remote_port);
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        ret = check_remote_port(remote_app_id, remote_port, false, exist);
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        return ret;
 }
@@ -127,9 +125,9 @@ int message_port_check_trusted_remote_port(const char *remote_app_id, const char
                return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
        }
        _LOGD("Check trusted remote port (%s):(%s).", remote_app_id, remote_port);
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        ret = check_remote_port(remote_app_id, remote_port, true, exist);
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        return ret;
 }
@@ -144,9 +142,9 @@ int message_port_send_message(const char *remote_app_id, const char *remote_port
        }
        _SECURE_LOGI("Send a message to (%s):(%s).", remote_app_id, remote_port);
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        ret = send_message(remote_app_id, remote_port, NULL, false, false, false, message);
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        return ret;
 }
@@ -161,9 +159,9 @@ int message_port_send_trusted_message(const char *remote_app_id, const char *rem
        }
        _SECURE_LOGI("Send a trusted message to (%s):(%s).", remote_app_id, remote_port);
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        ret = send_message(remote_app_id, remote_port, NULL, true, false, false, message);
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        return ret;
 }
@@ -181,9 +179,9 @@ int message_port_send_message_with_local_port(const char *remote_app_id, const c
        }
        _SECURE_LOGI("Send a message to (%s):(%s) - (%d).", remote_app_id, remote_port, local_port_id);
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        ret = send_bidirectional_message(local_port_id, remote_app_id, remote_port, false, message);
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        return ret;
 }
@@ -201,9 +199,9 @@ int message_port_send_trusted_message_with_local_port(const char *remote_app_id,
        }
        _SECURE_LOGI("Send a trusted message to (%s):(%s) - (%d).", remote_app_id, remote_port, local_port_id);
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        ret = send_bidirectional_message(local_port_id, remote_app_id, remote_port, true, message);
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        return ret;
 }
index b025aa740b89254129f24f66423a9a42035f1621..36fbad7463400bb748d20867de66d22232df2aca 100755 (executable)
@@ -44,6 +44,7 @@ GDBusConnection *gdbus_conn;
 char *app_id;
 
 static const int MAX_MESSAGE_SIZE = 16 * 1024;
+static pthread_mutex_t _message_port_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 int write_socket(int fd,
                const char *buffer,
@@ -353,3 +354,13 @@ char *get_encoded_name(const char *remote_app_id, const char *port_name, bool is
 
        return md5_interface;
 }
+
+void message_port_lock_mutex()
+{
+       pthread_mutex_lock(&_message_port_mutex);
+}
+
+void message_port_unlock_mutex()
+{
+       pthread_mutex_unlock(&_message_port_mutex);
+}
index 27b3739c9b751d3af8749229115c66843c3a4f38..a4d83cb8fe44af67f81c4028dcf13c34f3d6375e 100755 (executable)
@@ -114,8 +114,6 @@ typedef struct delay_message {
        bundle_raw *data;
 } delay_message_info_s;
 
-extern pthread_mutex_t mutex;
-
 /* LCOV_EXCL_START */
 static void __free_delay_message_info(delay_message_info_s *message)
 {
@@ -383,9 +381,9 @@ static gboolean __socket_disconnect_handler(GIOChannel *gio,
                gpointer data)
 {
        _LOGI("__socket_disconnect_handler %d", cond);
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        __free_port_info_by_key((port_key_info_s *)data);
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        return FALSE;
 }
@@ -744,28 +742,28 @@ static gboolean __process_delayed_message(gint fd, GIOCondition cond, gpointer d
        if (port_info == NULL)
                return G_SOURCE_REMOVE;
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
 
        if (__validate_delay_port_info(delay_info) == false) {
-               pthread_mutex_unlock(&mutex);
+               message_port_unlock_mutex();
                return G_SOURCE_REMOVE;
        }
 
        if (port_info->delayed_message_list == NULL) {
                port_info->delayed_message_size = 0;
                port_info->delay_src_id = 0;
-               pthread_mutex_unlock(&mutex);
+               message_port_unlock_mutex();
                return G_SOURCE_REMOVE;
        } else {
                message = g_list_nth_data(port_info->delayed_message_list, 0);
                ret = __send_delayed_message(port_info->send_sock_fd, message);
 
                if (ret == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) {
-                       pthread_mutex_unlock(&mutex);
+                       message_port_unlock_mutex();
                        return G_SOURCE_CONTINUE;
                } else if (ret == MESSAGE_PORT_ERROR_IO_ERROR) {
                        port_info->delay_src_id = 0;
-                       pthread_mutex_unlock(&mutex);
+                       message_port_unlock_mutex();
                        return G_SOURCE_REMOVE;
                }
 
@@ -775,7 +773,7 @@ static gboolean __process_delayed_message(gint fd, GIOCondition cond, gpointer d
                __free_delay_message_info(message);
        }
 
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        return G_SOURCE_CONTINUE;
 }
index 33834ab45a50d340eae2d144b748ccfa89236742..da0a6fe0dcd9b6841f4c0925f8f2c127a21d89d1 100755 (executable)
@@ -50,8 +50,6 @@ static GHashTable *__trusted_app_list_hash;
 static GHashTable *__callback_info_hash;
 static GHashTable *__sender_appid_hash;
 
-extern pthread_mutex_t mutex;
-
 typedef struct message_port_pkt {
        int remote_port_name_len;
        char *remote_port_name;
@@ -341,20 +339,20 @@ static gboolean __socket_request_handler(GIOChannel *gio,
        if (key_info == NULL)
                return FALSE;
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        if (__validate_callback_info(key_info) == false) {
                ret = FALSE;
                existed = FALSE;
-               pthread_mutex_unlock(&mutex);
+               message_port_unlock_mutex();
                goto out;
        }
 
        if (__validate_local_info(key_info) == false) {
                ret = FALSE;
-               pthread_mutex_unlock(&mutex);
+               message_port_unlock_mutex();
                goto out;
        }
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        mi = key_info->callback_info;
 
@@ -411,9 +409,9 @@ out:
        }
 
        if (mi && ret == FALSE && existed == TRUE) {
-               pthread_mutex_lock(&mutex);
+               message_port_lock_mutex();
                __callback_info_free_by_info(mi);
-               pthread_mutex_unlock(&mutex);
+               message_port_unlock_mutex();
        }
 
        return ret;
@@ -582,19 +580,19 @@ static bool __receive_message(GVariant *parameters, GDBusMethodInvocation *invoc
                goto out;
        }
 
-       pthread_mutex_lock(&mutex);
+       message_port_lock_mutex();
        if (!is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
                _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
-               pthread_mutex_unlock(&mutex);
+               message_port_unlock_mutex();
                goto out;
        }
 
        callback_info = __create_callback_info(mi, local_appid);
        if (callback_info == NULL) {
-               pthread_mutex_unlock(&mutex);
+               message_port_unlock_mutex();
                goto out;
        }
-       pthread_mutex_unlock(&mutex);
+       message_port_unlock_mutex();
 
        if (!local_port) {
                _LOGE("Invalid argument : local_port");
@@ -687,12 +685,12 @@ static bool __receive_message(GVariant *parameters, GDBusMethodInvocation *invoc
                                goto out;
                        }
 
-                       pthread_mutex_lock(&mutex);
+                       message_port_lock_mutex();
                        if (__callback_info_append(callback_info) == false) {
                                _LOGE("fail to append callback_info");
                                ret = false;
                        }
-                       pthread_mutex_unlock(&mutex);
+                       message_port_unlock_mutex();
                }
        }