From ee63f20cc66072bcded926bf4603a82034b0380f Mon Sep 17 00:00:00 2001 From: "jiehwan.park" Date: Wed, 27 Dec 2017 20:21:33 +0900 Subject: [PATCH] consider re-entrance issue. delete config file if completed. add each flag of configuration items because wifi has async return. Signed-off-by: jiehwan.park --- packaging/setup-adaptor.service | 2 +- src/adaptor.c | 45 +++++++++++----- src/input_file.c | 91 +++++++++++++++++++++++++++++++-- src/input_file.h | 39 +++++++++++++- src/sa_types.h | 9 ++++ src/setup_network.c | 24 +++++---- test/config.json | 8 ++- 7 files changed, 188 insertions(+), 30 deletions(-) diff --git a/packaging/setup-adaptor.service b/packaging/setup-adaptor.service index fb3dd24..39d563a 100644 --- a/packaging/setup-adaptor.service +++ b/packaging/setup-adaptor.service @@ -7,7 +7,7 @@ Requires=connman.service SmackProcessLabel=System Type=notify ExecStart=/usr/bin/setup-adaptor -Restart=on-failure +Restart=no RestartSec=0 [Install] diff --git a/src/adaptor.c b/src/adaptor.c index 75161ff..843cf41 100644 --- a/src/adaptor.c +++ b/src/adaptor.c @@ -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); diff --git a/src/input_file.c b/src/input_file.c index 38759e1..2c50417 100644 --- a/src/input_file.c +++ b/src/input_file.c @@ -22,6 +22,7 @@ #include #include #include +#include #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; + } +} diff --git a/src/input_file.h b/src/input_file.h index bdc4e09..10cf9cf 100644 --- a/src/input_file.h +++ b/src/input_file.h @@ -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 diff --git a/src/sa_types.h b/src/sa_types.h index b891dc7..9d46d05 100644 --- a/src/sa_types.h +++ b/src/sa_types.h @@ -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 * diff --git a/src/setup_network.c b/src/setup_network.c index af51179..76ac6b6 100644 --- a/src/setup_network.c +++ b/src/setup_network.c @@ -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; } diff --git a/test/config.json b/test/config.json index 9c36e8d..bf0ac9a 100644 --- a/test/config.json +++ b/test/config.json @@ -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 -- 2.34.1