Remove thread-scope variables for sharing handles between threads 89/249989/1 accepted/tizen_6.5_unified accepted/tizen/6.5/unified/20211028.100936 accepted/tizen/unified/20210216.125201 submit/tizen/20210215.060411 submit/tizen_6.5/20211028.161801 tizen_6.5.m2_release
authorNishant Chaprana <n.chaprana@samsung.com>
Fri, 18 Dec 2020 07:10:04 +0000 (12:40 +0530)
committerNishant Chaprana <n.chaprana@samsung.com>
Fri, 18 Dec 2020 07:26:27 +0000 (12:56 +0530)
Change-Id: I2d848f9f3d643bf4476f5be41d51c4bb81db08bd
Signed-off-by: Nishant Chaprana <n.chaprana@samsung.com>
include/softap_private.h
packaging/capi-network-softap.spec
src/softap.c
src/softap_private.c

index 2c6a8fd1f49493d36a0834c965667b75dbbf9180..e8d33fca1488c9d52b79c2646c54a4118a7d251a 100644 (file)
@@ -1,18 +1,18 @@
 /*
-* Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 
 #ifndef __SOFTAP_PRIVATE_H__
 #define __SOFTAP_PRIVATE_H__
@@ -91,23 +91,40 @@ typedef enum {
 int _softap_check_feature_supported(int key);
 
 /**
-* Start of mobileap-agent common values
-* When these values are changed, mobileap-agent should be also changed.
-* But some of those will be removed.
-*/
+ * To lock and unlock Mutex
+ */
+
+#define SOFTAP_LOCK \
+       do { \
+               _softap_lock(); \
+       } while(0)
+
+#define SOFTAP_UNLOCK \
+       do { \
+               _softap_unlock(); \
+       } while(0)
+
+void _softap_lock(void);
+void _softap_unlock(void);
+
+/**
+ * Start of mobileap-agent common values
+ * When these values are changed, mobileap-agent should be also changed.
+ * But some of those will be removed.
+ */
 
 /*
-* from mobileap_lib.h
-*/
+ * from mobileap_lib.h
+ */
 
 /**
-* Common configuration
-*/
+ * Common configuration
+ */
 #define SOFTAP_STR_INFO_LEN            20      /**< length of the ip or mac address */
 
 /**
-* Mobile AP error code
-*/
+ * Mobile AP error code
+ */
 typedef enum {
        MOBILE_AP_ERROR_NONE,                   /**< No error */
        MOBILE_AP_ERROR_RESOURCE,               /**< Socket creation error, file open error */
@@ -126,8 +143,8 @@ typedef enum {
 } mobile_ap_error_code_e;
 
 /**
-* Event type on callback
-*/
+ * Event type on callback
+ */
 typedef enum {
        MOBILE_AP_ENABLE_CFM = 0,
        MOBILE_AP_DISABLE_CFM = 1,
@@ -195,8 +212,8 @@ typedef enum {
 #define MAX_ALIAS_LEN  256
 
 /**
-* End of mobileap-agent common values
-*/
+ * End of mobileap-agent common values
+ */
 
 #define SOFTAP_DEFAULT_SSID    "Tizen"
 #define SOFTAP_DEFAULT_CHANNEL 1
index 7fc0bc4430dd75ae34b453181607398c7eb4e7d9..cc03c576340f7cdf23a12f07d3c368c3279fec1e 100644 (file)
@@ -1,6 +1,6 @@
 Name:          capi-network-softap
 Summary:       Softap Framework
-Version:       0.0.30
+Version:       0.0.31
 Release:       1
 Group:         System/Network
 License:       Apache-2.0
index 18911808c0299a981528c54588d6bd6ddd5d4fc1..0d1adcb575a98370a7d0e666ce624db5334c2056 100755 (executable)
@@ -262,8 +262,13 @@ static void __handle_dhcp(GDBusConnection *connection, const gchar *sender_name,
                GVariant *parameters, gpointer user_data)
 {
        DBG("+");
+       SOFTAP_LOCK;
 
-       _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
+       if (user_data == NULL) {
+               ERR("parameter(user_data) is NULL");
+               SOFTAP_UNLOCK;
+               return;
+       }
 
        __softap_h *sa = (__softap_h *)user_data;
        bool opened = false;
@@ -306,6 +311,8 @@ DONE:
        g_free(ip);
        g_free(mac);
        g_free(name);
+
+       SOFTAP_UNLOCK;
        DBG("-");
 }
 
@@ -314,8 +321,13 @@ static void __handle_softap_on(GDBusConnection *connection, const gchar *sender_
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+");
+       SOFTAP_LOCK;
 
-       _retm_if(user_data == NULL, "parameter(user_data) is NULL");
+       if (user_data == NULL) {
+               ERR("parameter(user_data) is NULL");
+               SOFTAP_UNLOCK;
+               return;
+       }
 
        __softap_h *sa = (__softap_h *)user_data;
        bool is_requested = false;
@@ -323,10 +335,15 @@ static void __handle_softap_on(GDBusConnection *connection, const gchar *sender_
        void *data = NULL;
 
        ecb = sa->enabled_cb;
-       if (ecb == NULL)
+       if (ecb == NULL) {
+               SOFTAP_UNLOCK;
                return;
+       }
+
        data = sa->enabled_user_data;
        ecb(SOFTAP_ERROR_NONE, is_requested, data);
+
+       SOFTAP_UNLOCK;
        DBG("-");
 }
 
@@ -335,8 +352,13 @@ static void __handle_softap_off(GDBusConnection *connection, const gchar *sender
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+");
+       SOFTAP_LOCK;
 
-       _retm_if(user_data == NULL, "parameter(user_data) is NULL");
+       if (user_data == NULL) {
+               ERR("parameter(user_data) is NULL");
+               SOFTAP_UNLOCK;
+               return;
+       }
 
        __softap_h *sa = (__softap_h *)user_data;
        softap_disabled_cause_e code = SOFTAP_DISABLED_BY_OTHERS;
@@ -345,8 +367,11 @@ static void __handle_softap_off(GDBusConnection *connection, const gchar *sender
        char *buf = NULL;
 
        dcb = sa->disabled_cb;
-       if (dcb == NULL)
+       if (dcb == NULL) {
+               SOFTAP_UNLOCK;
                return;
+       }
+
        data = sa->disabled_user_data;
        g_variant_get(parameters, "(s)", &buf);
        if (!g_strcmp0(buf, SIGNAL_MSG_NOT_AVAIL_INTERFACE))
@@ -357,6 +382,7 @@ static void __handle_softap_off(GDBusConnection *connection, const gchar *sender
        g_free(buf);
        dcb(SOFTAP_ERROR_NONE, code, data);
 
+       SOFTAP_UNLOCK;
        DBG("-");
 }
 
@@ -366,8 +392,13 @@ static void __handle_low_battery_mode(GDBusConnection *connection, const gchar *
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+");
+       SOFTAP_LOCK;
 
-       _retm_if(user_data == NULL, "parameter(user_data) is NULL");
+       if (user_data == NULL) {
+               ERR("parameter(user_data) is NULL");
+               SOFTAP_UNLOCK;
+               return;
+       }
 
        __softap_h *sa = (__softap_h *)user_data;
        softap_disabled_cb dcb = NULL;
@@ -375,12 +406,16 @@ static void __handle_low_battery_mode(GDBusConnection *connection, const gchar *
        softap_disabled_cause_e code = SOFTAP_DISABLED_BY_LOW_BATTERY;
 
        dcb = sa->disabled_cb;
-       if (dcb == NULL)
+       if (dcb == NULL) {
+               SOFTAP_UNLOCK;
                return;
+       }
 
        data = sa->disabled_user_data;
 
        dcb(SOFTAP_ERROR_NONE, code, data);
+
+       SOFTAP_UNLOCK;
        DBG("-");
 }
 
@@ -389,8 +424,13 @@ static void __handle_flight_mode(GDBusConnection *connection, const gchar *sende
                        GVariant *parameters, gpointer user_data)
 {
        DBG("+");
+       SOFTAP_LOCK;
 
-       _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
+       if (user_data == NULL) {
+               ERR("parameter(user_data) is NULL");
+               SOFTAP_UNLOCK;
+               return;
+       }
 
        __softap_h *sa = (__softap_h *)user_data;
        softap_disabled_cb dcb = NULL;
@@ -398,11 +438,16 @@ static void __handle_flight_mode(GDBusConnection *connection, const gchar *sende
        softap_disabled_cause_e code = SOFTAP_DISABLED_BY_FLIGHT_MODE;
 
        dcb = sa->disabled_cb;
-       if (dcb == NULL)
+       if (dcb == NULL) {
+               SOFTAP_UNLOCK;
                return;
+       }
+
        data = sa->disabled_user_data;
 
        dcb(SOFTAP_ERROR_NONE, code, data);
+
+       SOFTAP_UNLOCK;
        DBG("-");
 }
 
@@ -412,22 +457,30 @@ static void __handle_security_type_changed(GDBusConnection *connection, const gc
 
 {
        DBG("+");
+       SOFTAP_LOCK;
 
-       _retm_if(user_data == NULL, "parameter(user_data) is NULL");
-       __softap_h *sa = (__softap_h *)user_data;
+       if (user_data == NULL) {
+               ERR("parameter(user_data) is NULL");
+               SOFTAP_UNLOCK;
+               return;
+       }
 
+       __softap_h *sa = (__softap_h *)user_data;
        softap_security_type_changed_cb scb = NULL;
        void *data = NULL;
        int security_type;
 
        scb = sa->security_type_changed_cb;
-       if (scb == NULL)
+       if (scb == NULL) {
+               SOFTAP_UNLOCK;
                return;
+       }
 
        g_variant_get(parameters, "(i)", &security_type);
        data = sa->security_type_user_data;
        scb((softap_security_type_e)security_type, data);
 
+       SOFTAP_UNLOCK;
        return;
 }
 
@@ -436,22 +489,30 @@ static void __handle_ssid_visibility_changed(GDBusConnection *connection, const
                GVariant *parameters, gpointer user_data)
 {
        DBG("+");
+       SOFTAP_LOCK;
 
-       _retm_if(user_data == NULL, "parameter(user_data) is NULL");
-       __softap_h *sa = (__softap_h *)user_data;
+       if (user_data == NULL) {
+               ERR("parameter(user_data) is NULL");
+               SOFTAP_UNLOCK;
+               return;
+       }
 
+       __softap_h *sa = (__softap_h *)user_data;
        softap_ssid_visibility_changed_cb scb = NULL;
        void *data = NULL;
        int visible = 0;
 
        scb = sa->ssid_visibility_changed_cb;
        if (scb == NULL) {
+               SOFTAP_UNLOCK;
                DBG("-");
                return;
        }
        g_variant_get(parameters, "(i)", &visible);
        data = sa->ssid_visibility_user_data;
        scb(visible, data);
+
+       SOFTAP_UNLOCK;
        DBG("-");
 }
 
@@ -460,27 +521,44 @@ static void __handle_passphrase_changed(GDBusConnection *connection, const gchar
                GVariant *parameters, gpointer user_data)
 {
        DBG("+");
+       SOFTAP_LOCK;
 
-       _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
-       __softap_h *sa = (__softap_h *)user_data;
+       if (user_data == NULL) {
+               ERR("parameter(user_data) is NULL");
+               SOFTAP_UNLOCK;
+               return;
+       }
 
+       __softap_h *sa = (__softap_h *)user_data;
        softap_passphrase_changed_cb pcb = NULL;
        void *data = NULL;
 
        pcb = sa->passphrase_changed_cb;
-       if (pcb == NULL)
+       if (pcb == NULL) {
+               SOFTAP_UNLOCK;
                return;
+       }
 
        data = sa->passphrase_user_data;
 
        pcb(data);
+
+       SOFTAP_UNLOCK;
        DBG("-");
 }
 
 static void __enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                gpointer user_data)
 {
-       _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
+       DBG("+");
+       SOFTAP_LOCK;
+
+       if (user_data == NULL) {
+               ERR("parameter(user_data) is NULL");
+               SOFTAP_UNLOCK;
+               return;
+       }
+
        __softap_h *sa = (__softap_h *)user_data;
        GError *g_error = NULL;
        GVariant *g_var;
@@ -491,6 +569,7 @@ static void __enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
 
        if (!_softap_check_handle((softap_h)user_data)) {
                DBG("Softap handle is already destroyed");
+               SOFTAP_UNLOCK;
                return;
        }
 
@@ -501,6 +580,7 @@ static void __enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                                ++retry < SOFTAP_ERROR_RECOVERY_MAX) {
                        g_error_free(g_error);
                        softap_enable((softap_h)sa);
+                       SOFTAP_UNLOCK;
                        DBG("-");
                        return;
                }
@@ -528,6 +608,8 @@ static void __enabled_cfm_cb(GObject *source_object, GAsyncResult *res,
        if (ecb)
                ecb(error, true, data);
 
+       SOFTAP_UNLOCK;
+       DBG("-");
        return;
 }
 
@@ -535,6 +617,14 @@ static void __disabled_cfm_cb(GObject *source_object, GAsyncResult *res,
                gpointer user_data)
 {
        DBG("+");
+       SOFTAP_LOCK;
+
+       if (user_data == NULL) {
+               ERR("parameter(user_data) is NULL");
+               SOFTAP_UNLOCK;
+               return;
+       }
+
        __softap_h *sa = (__softap_h *)user_data;
        GError *g_error = NULL;
        GVariant *g_var;
@@ -546,6 +636,7 @@ static void __disabled_cfm_cb(GObject *source_object, GAsyncResult *res,
 
        if (!_softap_check_handle((softap_h) sa)) {
                DBG("Softap handle is already destroyed");
+               SOFTAP_UNLOCK;
                return;
        }
 
@@ -553,6 +644,7 @@ static void __disabled_cfm_cb(GObject *source_object, GAsyncResult *res,
        if (g_error) {
                ERR("DBus error [%s]", g_error->message);
                g_error_free(g_error);
+               SOFTAP_UNLOCK;
                DBG("-");
                return;
        } else {
@@ -571,6 +663,7 @@ static void __disabled_cfm_cb(GObject *source_object, GAsyncResult *res,
        if (dcb)
                dcb(error, code, data);
 
+       SOFTAP_UNLOCK;
        DBG("-");
        return;
 }
@@ -580,8 +673,14 @@ static void __settings_reloaded_cb(GObject *source_object, GAsyncResult *res,
                gpointer user_data)
 {
        DBG("+\n");
+       SOFTAP_LOCK;
+
+       if (user_data == NULL) {
+               ERR("parameter(user_data) is NULL");
+               SOFTAP_UNLOCK;
+               return;
+       }
 
-       _retm_if(user_data == NULL, "parameter(user_data) is NULL\n");
        GError *g_error = NULL;
        GVariant *g_var;
        guint info;
@@ -604,6 +703,7 @@ static void __settings_reloaded_cb(GObject *source_object, GAsyncResult *res,
 
        if (sa->settings_reloaded_cb == NULL) {
                DBG("There is no settings_reloaded_cb\n");
+               SOFTAP_UNLOCK;
                return;
        }
 
@@ -612,6 +712,8 @@ static void __settings_reloaded_cb(GObject *source_object, GAsyncResult *res,
 
        sa->settings_reloaded_cb = NULL;
        sa->settings_reloaded_user_data = NULL;
+
+       SOFTAP_UNLOCK;
        DBG("-\n");
 }
 
index 06dded2263ae11b8bca4737a675c323a7a7cc155..f92131066acb48c6d27b4c7e11058c6c2de501e7 100755 (executable)
@@ -1,19 +1,21 @@
 /*
-* Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*/
-
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+#include <pthread.h>
 #include <glib.h>
 #include <stdio.h>
 #include <stdlib.h>
 #define SOFTAP_FEATURE "http://tizen.org/feature/network.wifi.softap"
 #define MODEL_NAME_FEATURE "http://tizen.org/system/model_name"
 
-static __thread bool is_feature_checked[SUPPORTED_FEATURE_MAX] = { 0, };
-static __thread bool feature_supported[SUPPORTED_FEATURE_MAX] = { 0, };
-static __thread GSList *softap_handle_list = NULL;
+static pthread_mutex_t g_softap_thread_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+static bool is_feature_checked[SUPPORTED_FEATURE_MAX] = { 0, };
+static bool feature_supported[SUPPORTED_FEATURE_MAX] = { 0, };
+static GSList *softap_handle_list = NULL;
 
-static __thread bool is_privilege_checked = 0;
-static __thread bool access_allowed = 0;
+static bool is_privilege_checked = 0;
+static bool access_allowed = 0;
 
 static void __get_feature_info(int feature)
 {
@@ -65,44 +68,69 @@ static void __get_feature_info(int feature)
        free(model_name);
 }
 
+void _softap_lock(void)
+{
+       pthread_mutex_lock(&g_softap_thread_mutex);
+}
+
+void _softap_unlock(void)
+{
+       pthread_mutex_unlock(&g_softap_thread_mutex);
+}
+
 int _softap_check_feature_supported(int feature)
 {
+       SOFTAP_LOCK;
+
        if (!is_feature_checked[feature])
                __get_feature_info(feature);
 
        if (!feature_supported[feature]) {
                ERR("Not supported feature!");
+               SOFTAP_UNLOCK;
                return SOFTAP_ERROR_NOT_SUPPORTED;
        }
 
+       SOFTAP_UNLOCK;
        return SOFTAP_ERROR_NONE;
 }
 
 int _softap_check_permission(void)
 {
+       SOFTAP_LOCK;
+
        FILE *fd;
        int ret;
        char smack_label[SMACK_LABEL_LEN + 1] = {0, };
        char uid[10] = {0, };
        char *client_session = "";
        cynara *p_cynara;
+       int is_access_allowed = 0;
 
-       if (is_privilege_checked)
-               return access_allowed ? SOFTAP_ERROR_NONE : SOFTAP_ERROR_PERMISSION_DENIED;
+       if (is_privilege_checked) {
+               is_access_allowed = access_allowed ? SOFTAP_ERROR_NONE : SOFTAP_ERROR_PERMISSION_DENIED;
+               SOFTAP_UNLOCK;
+               return is_access_allowed;
+       }
 
-       if (CYNARA_API_SUCCESS != cynara_initialize(&p_cynara, NULL))
+       if (CYNARA_API_SUCCESS != cynara_initialize(&p_cynara, NULL)) {
+               SOFTAP_UNLOCK;
                return SOFTAP_ERROR_PERMISSION_DENIED;
+       }
 
        bzero(smack_label, SMACK_LABEL_LEN + 1);
 
        fd = fopen("/proc/self/attr/current", "r");
-       if (fd == NULL)
+       if (fd == NULL) {
+               SOFTAP_UNLOCK;
                return SOFTAP_ERROR_PERMISSION_DENIED;
+       }
 
        ret = fread(smack_label, SMACK_LABEL_LEN, 1, fd);
        if (ret < 0) {
                ERR("Failed[%d] to read /proc/self/attr/current\n", ferror(fd));
                fclose(fd);
+               SOFTAP_UNLOCK;
                return SOFTAP_ERROR_PERMISSION_DENIED;
        }
        fclose(fd);
@@ -114,26 +142,36 @@ int _softap_check_permission(void)
 
        is_privilege_checked = true;
        access_allowed = (ret == CYNARA_API_ACCESS_ALLOWED) ? true : false;
+       is_access_allowed = access_allowed ? SOFTAP_ERROR_NONE : SOFTAP_ERROR_PERMISSION_DENIED;
 
        DBG("check permission[%s/%d]", SOFTAP_PRIVILEGE, access_allowed);
 
-       return access_allowed ? SOFTAP_ERROR_NONE : SOFTAP_ERROR_PERMISSION_DENIED;
+       SOFTAP_UNLOCK;
+       return is_access_allowed;
 }
 
 void _softap_add_handle(softap_h handle)
 {
+       SOFTAP_LOCK;
        softap_handle_list = g_slist_append(softap_handle_list, handle);
+       SOFTAP_UNLOCK;
 }
 
 void _softap_remove_handle(softap_h handle)
 {
+       SOFTAP_LOCK;
        softap_handle_list = g_slist_remove(softap_handle_list, handle);
+       SOFTAP_UNLOCK;
 }
 
 bool _softap_check_handle(softap_h handle)
 {
-       if (g_slist_find(softap_handle_list, handle) != NULL)
+       SOFTAP_LOCK;
+       if (g_slist_find(softap_handle_list, handle) != NULL) {
+               SOFTAP_UNLOCK;
                return true;
-       else
-               return false;
+       }
+
+       SOFTAP_UNLOCK;
+       return false;
 }