consider re-entrance issue.
authorjiehwan.park <jiehwan.park@samsung.com>
Wed, 27 Dec 2017 11:21:33 +0000 (20:21 +0900)
committerjiehwan.park <jiehwan.park@samsung.com>
Wed, 27 Dec 2017 11:21:33 +0000 (20:21 +0900)
delete config file if completed.
add each flag of configuration items because wifi has async return.

Signed-off-by: jiehwan.park <jiehwan.park@samsung.com>
packaging/setup-adaptor.service
src/adaptor.c
src/input_file.c
src/input_file.h
src/sa_types.h
src/setup_network.c
test/config.json

index fb3dd24c43931d57e06bac409b5bcda180885911..39d563a977beb87a90c0cbb90425092a40d4bf3f 100644 (file)
@@ -7,7 +7,7 @@ Requires=connman.service
 SmackProcessLabel=System
 Type=notify
 ExecStart=/usr/bin/setup-adaptor
-Restart=on-failure
+Restart=no
 RestartSec=0
 
 [Install]
index 75161ffb0eb6748666da8bb121993f57943995fb..843cf4188dc7230cdaac0cd51cde1be7604e3109 100644 (file)
@@ -30,22 +30,28 @@ static gboolean prepare(GSource *source, gint *timeout)
        return TRUE;
 }
 
-gboolean setup(gpointer data)
+gboolean setup_cb(gpointer data)
 {
        sa_error_e ret = SA_ERROR_NONE;
        sa_config_s config = {0,};
        sa_error_e file_read = SA_ERROR_NONE;
        
-       _D("callback>>>setup start !!!");
+       _D("callback>>>setup_cb start !!!");
 
        file_read = sa_inputfile_get_config_info(&config);
        if (file_read == SA_ERROR_NONE) {
-               if (config.systemData != NULL) {
+               _D("flag_completion(system=%d)(eth=%d)(wifi=%d)", sa_inputfile_get_completion_flag(SA_FILE_CONFIG_SYSTEM)
+                                       , sa_inputfile_get_completion_flag(SA_FILE_CONFIG_ETHERNET)
+                                       , sa_inputfile_get_completion_flag(SA_FILE_CONFIG_WIFI) );
+
+               if (config.systemData != NULL && !sa_inputfile_get_completion_flag(SA_FILE_CONFIG_SYSTEM) ) {
                        ret = sa_setup_system(config.systemData);
                        if(ret != SA_ERROR_NONE) {
                                _E("sa_setup_system return error(%d)", ret);
                        }
                }
+               sa_inputfile_set_completion_flag(SA_FILE_CONFIG_SYSTEM);
+
 
                if (config.networkData != NULL) {
                        ret = sa_setup_network(config.networkData);
@@ -53,25 +59,32 @@ gboolean setup(gpointer data)
                                _E("sa_setup_network return error(%d)", ret);
                        }
                }
+               else {
+                       sa_inputfile_set_completion_flag(SA_FILE_CONFIG_ETHERNET);
+                       sa_inputfile_set_completion_flag(SA_FILE_CONFIG_WIFI);
+               }
        }
 
        sa_inputfile_release_resource(&config);
+
+       sa_inputfile_clear_completion_flag();
+       sa_inputfile_remove();
 }
 
 static GSourceFuncs SourceFuncs =
 {
-       prepare,        // prepare function
-       NULL,           // function check
-       sa_inputfile_thread,    // callback start
+       prepare,                                // prepare function
+       NULL,                                   // function check
+       sa_inputfile_thread,    // main function to execute
        NULL
 };
 
 // config for main thread & callback function 
-static gpointer adder_thread_output (gpointer data)
+static gpointer main_thread (gpointer data)
 {
        GSource* src = NULL;
 
-       _D("adder_thread_output ~~~");
+       _D("main_thread ~~~");
 
        GMainLoop* main_loop;
        GMainContext* context = NULL;
@@ -83,7 +96,10 @@ static gpointer adder_thread_output (gpointer data)
        
        g_source_set_name (src, "setup-adaptor-func");
        g_source_set_priority (src, G_PRIORITY_DEFAULT);
-       g_source_set_callback(src, (GSourceFunc) setup, (gpointer) main_loop, NULL);    // set callback function
+       g_source_set_callback(src,                              // GSourceFuncs prototype
+                                       (GSourceFunc) setup_cb,         // callabck function
+                                       (gpointer) main_loop,   // gpointer : set as GMainLoop handle to destroy in thread
+                                       NULL);
        g_source_attach (src, context);
 
        g_main_loop_run(main_loop);
@@ -92,6 +108,7 @@ static gpointer adder_thread_output (gpointer data)
        if(data) {
                g_main_loop_quit( (GMainLoop*)data );
        }
+       _D("main_thread exit~~~");
 
        return NULL;
 }
@@ -101,20 +118,20 @@ int main(int argc, char *argv[])
        sa_file_state_e file_state = SA_FILE_STATE_NOT_EXISTED;
        sa_config_s config = {0,};
 
-       _D("main function~~~");
        // check if exists config file
        file_state = sa_inputfile_get_config_state();
-       _D("first check for config enable(%d)", file_state);
-       if (SA_FILE_STATE_NOT_EXISTED == file_state) {
-               _D("config file is not existed, disable setup-adaptor");
+       if (SA_FILE_STATE_EXIST != file_state) {
+               _D("Unavailable config file, disable setup-adaptor");
                return 0;
        }
 
+       _D("setup-adaptor started !!!");
+
        GMainLoop *main_loop;
 
        main_loop = g_main_loop_new(NULL, FALSE);
 
-       g_thread_create((GThreadFunc) adder_thread_output, main_loop, TRUE, NULL);
+       g_thread_create((GThreadFunc) main_thread, main_loop, TRUE, NULL);
 
        g_main_loop_run(main_loop);
        g_main_loop_unref(main_loop);
index 38759e1553e59e9b4d6fbca7082624dc90797ea2..2c50417afcdcfef0fc8739c27c80d8a38c6d38d3 100644 (file)
@@ -22,6 +22,7 @@
 #include <sys/inotify.h>
 #include <fcntl.h>
 #include <json.h>
+#include <errno.h>
 #include "sa_common.h"
 #include "sa_types.h"
 #include "input_file.h"
@@ -561,7 +562,7 @@ sa_error_e sa_inputfile_get_config_info(sa_config_s *config)
        sa_error_e ret = SA_ERROR_NONE;
        // check file
        if (access(CONFIG_FILE, 0) != 0) {
-               return SA_ERROR_NOT_AVAILABLE;
+               return SA_ERROR_NOT_SUPPORTED;
        }
        // parsing and fill into struct
        ret = __parse_config(CONFIG_FILE, config);
@@ -657,6 +658,90 @@ void sa_inputfile_release_resource(sa_config_s *config)
        }
 }
 
+#define FLAG_FILE_SYSTEM       "/etc/setup-adaptor/system_executed"
+#define FLAG_FILE_ETH          "/etc/setup-adaptor/ethernet_executed"
+#define FLAG_FILE_WIFI         "/etc/setup-adaptor/wifi_executed"
 
-/* Sample Config */
-/*{"version":"0.1","wifi":{"ssid":"XXXX","password":"XXXXXX","enabled":true,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDNSServer":"127.0.0.1","secondaryDNSServer":"127.0.0.1"},"ethernet":{"enabled":false,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDnsServer":"127.0.0.1","secondaryDnsServer":"127.0.0.1"},"httpProxyHost":"127.0.0.1","httpProxyPort":8080}*/
\ No newline at end of file
+void sa_inputfile_remove(void)
+{
+       if (remove(CONFIG_FILE) != 0) {
+               _E("Can't remove file{%s}! %s", CONFIG_FILE, strerror(errno));
+       }
+       else {
+               _D("file removed successfully..{%s}", CONFIG_FILE);
+       }
+}
+
+void sa_inputfile_clear_completion_flag(void)
+{
+       if (remove(FLAG_FILE_SYSTEM) == 0) {
+               _D("file removed successfully..{%s}", FLAG_FILE_SYSTEM);
+       }
+       if (remove(FLAG_FILE_ETH) == 0) {
+               _D("file removed successfully..{%s}", FLAG_FILE_ETH);
+       }
+       if (remove(FLAG_FILE_WIFI) == 0) {
+               _D("file removed successfully..{%s}", FLAG_FILE_WIFI);
+       }
+}
+
+void sa_inputfile_set_completion_flag(sa_file_config_e config_type)
+{
+       int fd;
+       
+       switch(config_type)
+       {
+               case SA_FILE_CONFIG_SYSTEM:
+                       fd = creat(FLAG_FILE_SYSTEM, NULL);
+                       break;
+               case SA_FILE_CONFIG_ETHERNET:
+                       fd = creat(FLAG_FILE_ETH, NULL);
+                       break;
+               case SA_FILE_CONFIG_WIFI:
+                       fd = creat(FLAG_FILE_WIFI, NULL);
+                       break;
+               default:
+                       _E("unknown parameter (%d)", config_type);
+                       break;
+       }
+       
+       if(fd == -1) {
+               _E("Can't create file for (%d)! %s", config_type, strerror(errno));
+       }
+}
+
+// return TRUE if set
+gboolean sa_inputfile_get_completion_flag(sa_file_config_e config_type)
+{
+       int ret = 0;
+
+       switch(config_type)
+       {
+               case SA_FILE_CONFIG_SYSTEM:
+                       ret = access(FLAG_FILE_SYSTEM, 0);
+                       break;
+               case SA_FILE_CONFIG_ETHERNET:
+                       ret = access(FLAG_FILE_ETH, 0);
+                       break;
+               case SA_FILE_CONFIG_WIFI:
+                       ret = access(FLAG_FILE_WIFI, 0);
+                       break;
+               case SA_FILE_CONFIG_ALL:
+                       ret = access(FLAG_FILE_SYSTEM, 0);
+                       if(ret == 0) {
+                               ret = access(FLAG_FILE_ETH, 0);
+                       }
+                       if(ret == 0) {
+                               ret = access(FLAG_FILE_WIFI, 0);
+                       }
+                       break;
+               default:
+                       _E("unknown parameter (%d)", config_type);
+                       break;
+       }
+       if (ret == 0) {
+               return TRUE;
+       } else {
+               return FALSE;
+       }
+}
index bdc4e094afed4502adb56e079fb86d36e8564d0a..10cf9cf4c87d0d8ee92194fe3b3fa80594390890 100644 (file)
@@ -42,7 +42,44 @@ sa_file_state_e sa_inputfile_get_config_state(void);
  */
 gboolean sa_inputfile_thread(GSource *source, GSourceFunc callbackFuntion, gpointer user_data);
 
-
+/**
+ * @fn        void sa_inputfile_release_resource
+ * @brief     This function to free allocated resource (memory)
+ * @param     sa_config_s
+ * @return    void
+ */
 void sa_inputfile_release_resource(sa_config_s *config);
 
+/**
+ * @fn        void sa_inputfile_remove
+ * @brief     This function to delete config file to prevent from execution next time.
+ * @param     void
+ * @return    void
+ */
+void sa_inputfile_remove(void);
+
+/**
+ * @fn        void sa_inputfile_clear_completion_flag
+ * @brief     This function to clear all file flag.
+ * @param     void
+ * @return    void
+ */
+void sa_inputfile_clear_completion_flag(void);
+
+/**
+ * @fn        void sa_inputfile_set_completion_flag
+ * @brief     This function to set completion flag to prevent from re-entrance.
+ * @param     sa_file_config_e for each items
+ * @return    void
+ */
+void sa_inputfile_set_completion_flag(sa_file_config_e config_type);
+
+/**
+ * @fn        void sa_inputfile_get_completion_flag
+ * @brief     This function to get the completion status.
+ * @param     sa_file_config_e for each items
+ * @return    TRUE (if completed), FALSE (need to config)
+ */
+gboolean sa_inputfile_get_completion_flag(sa_file_config_e config_type);
+
 #endif/* __INPUT_FILE_H__ */
\ No newline at end of file
index b891dc7b716f79a6c33d06b13b15b6fd95b4b220..9d46d050503e446583562fb1ebe6c9d80faf5fcb 100644 (file)
@@ -95,6 +95,15 @@ typedef enum {
        SA_FILE_STATE_CHANGED,                  /**< Changed  */
 } sa_file_state_e;
 
+
+typedef enum {
+       SA_FILE_CONFIG_SYSTEM = 0,              /**< system configuration type */
+       SA_FILE_CONFIG_ETHERNET,                /**< ethernet configuration type */
+       SA_FILE_CONFIG_WIFI,                    /**< wifi configuration type */
+       SA_FILE_CONFIG_ALL,
+       SA_FILE_CONFIG_UNKNOWN
+} sa_file_config_e;
+
 /**
  * @brief  This type is definition of status file change callback
  *
index af5117991cfb55c101fa31fe578519b7244460be..76ac6b649a2c6ece418868879d5af8d44944fd75 100644 (file)
@@ -20,6 +20,7 @@
 #include "sa_common.h"
 #include "sa_types.h"
 #include "setup_network.h"
+#include "input_file.h"
 #include "net_connection.h"
 #include "wifi-manager.h"
 
@@ -466,6 +467,8 @@ static sa_error_e __network_get_state(sa_network_state_e *conn_state, sa_network
 }
 
 
+extern void sa_inputfile_flag(void);
+
 static sa_error_e __network_connect(sa_network_s *info)
 {
        sa_error_e ret = SA_ERROR_NONE;
@@ -476,23 +479,24 @@ static sa_error_e __network_connect(sa_network_s *info)
                return SA_ERROR_INVALID_PARAMETER;
        }
        
+       if (info->eth != NULL) {
+               // decide whether it will be set according to policy
+               if ( (info->eth->enabled == TRUE) && !sa_inputfile_get_completion_flag(SA_FILE_CONFIG_ETHERNET) ){
+                       retEth = __ethernet_connect_main(info->eth);
+                       _D("return ethernet [%d]", retEth);
+               }
+       }
+       sa_inputfile_set_completion_flag(SA_FILE_CONFIG_ETHERNET);
+
        if (info->wifi != NULL) {
-               if (info->wifi->enabled == TRUE) {
+               if ( (info->wifi->enabled == TRUE) && !sa_inputfile_get_completion_flag(SA_FILE_CONFIG_WIFI) ) {
                        retWifi = __wifi_connect_main(info->wifi);
                        _D("return wifi [%d]", retWifi);
-
                } else {
                        _D("wifi enabled flag is false");
                }
        }
-
-       if (info->eth != NULL) {
-               // decide whether it will be set according to policy
-               if (info->eth->enabled == TRUE){
-                       retEth = __ethernet_connect_main(info->eth);
-                       _D("return ethernet [%d]", retEth);
-               }
-       }
+       sa_inputfile_set_completion_flag(SA_FILE_CONFIG_WIFI);
 
        return ret;
 }
index 9c36e8d339ca7f224af12ce68d900be361767c68..bf0ac9a5354bd49a25e4abf6bb67d37bfb904c10 100644 (file)
@@ -1 +1,7 @@
-{"version":"0.1","wifi":{"ssid":"docker","password":"dockerdocker","enabled":true,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDNSServer":"127.0.0.1","secondaryDNSServer":"127.0.0.1"},"ethernet":{"enabled":false,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDnsServer":"127.0.0.1","secondaryDnsServer":"127.0.0.1"},"httpProxyHost":"127.0.0.1","httpProxyPort":8080}
\ No newline at end of file
+{
+"version":"0.1",
+"wifi":{"ssid":"docker","password":"dockerdocker","enabled":true,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDNSServer":"127.0.0.1","secondaryDNSServer":"127.0.0.1"},
+"ethernet":{"enabled":false,"dhcpEnabled":true,"ipAddress":"127.0.0.1","netmask":"127.0.0.1","defaultGateway":"127.0.0.1","primaryDnsServer":"127.0.0.1","secondaryDnsServer":"127.0.0.1"},
+"httpProxyHost":"127.0.0.1",
+"httpProxyPort":8080
+}
\ No newline at end of file