diff options
Diffstat (limited to 'src/adaptor_setting.c')
-rw-r--r-- | src/adaptor_setting.c | 224 |
1 files changed, 224 insertions, 0 deletions
diff --git a/src/adaptor_setting.c b/src/adaptor_setting.c new file mode 100644 index 0000000..93f6937 --- /dev/null +++ b/src/adaptor_setting.c @@ -0,0 +1,224 @@ +/* + * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd. + * + * 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. + */ +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <pthread.h> +#include "sa_common.h" +#include "sa_types.h" +#include "setup_system.h" +#include "input_file.h" +#include "parse_file.h" +#include "adaptor_util.h" + +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <unistd.h> +#include <string.h> +#include <fcntl.h> + +#include "json_util.h" + +#define CONFIG_NUM 2 +#define ORIGIN_CONFIG_FILE "/opt/beluga/setup-adaptor/config/config.json" +#define ENVIRONMENT_DIR_PATH "/opt/beluga/system/env/" + +#define MAXLINE 1024 + +char sa_config_file_path[CONFIG_NUM][256] = { + "/opt/beluga/setup-adaptor/config/devconfig_system.json", + "/opt/beluga/setup-adaptor/config/devconfig_target.json" +}; + +char sa_applied_config_file_path[CONFIG_NUM][256] = { + "/opt/beluga/setup-adaptor/config/devconfig_system_applied.json", + "/opt/beluga/setup-adaptor/config/devconfig_target_applied.json" +}; + +int flagInitialized = 0; + +sa_error_e __prepare_config_file(void) +{ + sa_error_e ret = SA_ERROR_NONE; + + //exit if config file exists already + if ((SA_FILE_STATE_NOT_EXISTED == sa_inputfile_is_file_exist(sa_config_file_path[0])) + || (SA_FILE_STATE_NOT_EXISTED == sa_inputfile_is_file_exist(sa_config_file_path[1]))) { + _D("split into each config files"); + sa_split_files(ORIGIN_CONFIG_FILE, sa_config_file_path[0], sa_config_file_path[1]); + } else { + _D("skip split files."); + ret = SA_ERROR_NOT_AVAILABLE; + } + + return ret; +} + +sa_error_e sa_remove_config_file(void) +{ + int index; + sa_error_e ret = SA_ERROR_NONE; + + for (index = 0; index < CONFIG_NUM; index++) { + if (sa_inputfile_is_file_exist(sa_config_file_path[index]) == SA_FILE_STATE_EXIST) { + if (remove(sa_config_file_path[index]) != 0) { + _D("Error!!! remove [%s] file", sa_config_file_path[index]); + ret = SA_ERROR_UNKNOWN; + } + } + if (sa_inputfile_is_file_exist(sa_applied_config_file_path[index]) == SA_FILE_STATE_EXIST) { + if (remove(sa_applied_config_file_path[index]) != 0) { + _D("Error!!! remove [%s] file", sa_applied_config_file_path[index]); + ret = SA_ERROR_UNKNOWN; + } + } + } + return ret; +} + +int setup_cb(void *data) +{ + char *file_path = (char *)data; + + sa_error_e ret = SA_ERROR_NONE; + sa_config_s config = { 0, }; + sa_error_e file_read = SA_ERROR_NONE; + + _D("setup_cb called... >>>>>>>>>>>>>>>>>>>>>>>"); + _D("file_path : %s", file_path); + + // Apply the modified setting value. + file_read = sa_inputfile_get_config_info(&config, file_path); + + if (file_read == SA_ERROR_NONE) { + if (config.systemData != NULL) { + ret = sa_setup_system(config.systemData, flagInitialized); + if (ret != SA_ERROR_NONE) + _E("sa_setup_system return error(%d)", ret); + _D("sa_setup_system() completed !!! ret(%d)", ret); + } + } + _D("setup_cb completed <<<<<<<<<<<<<<<<<<<<<<<<"); + + return ret; +} + +// send notification to systemd when finished starting up +// it have to be sent once after starting service +// launcher use setting value from adaptor as EnvironmentFile +void completed_notify_cb(void) +{ + if (send_message_to_launcher(SA_START_LAUNCHER_SVC) < 0) + _E("send error : "); + + flagInitialized = 1; + _D("send notification to launcher to start itself..."); +} + +void __free_main_thread_param(sa_main_thread_s * param) +{ + int index = 0; + + if (param != NULL) { + // free parameter + for (index = 0; index < param->config_file_num; index++) { + if ((char *)param->config_file_path[index] != NULL) { + free((char *)param->config_file_path[index]); + param->config_file_path[index] = NULL; + } + } + if (param->config_file_path != NULL) { + free((char **)param->config_file_path); + param->config_file_path = NULL; + } + // free param structure + free(param); + param = NULL; + } +} + +sa_main_thread_s *__create_main_thread_param(void) +{ + int index = 0; + sa_main_thread_s *param = (sa_main_thread_s *) malloc(sizeof(sa_main_thread_s)); + + if (param != NULL) { + memset(param, 0x00, sizeof(sa_main_thread_s)); + param->config_file_num = CONFIG_NUM; + param->config_file_path = (char **)malloc(sizeof(char *) * param->config_file_num); + if (param->config_file_path != NULL) { + // set config file path as a parameter + for (index = 0; index < param->config_file_num; index++) { + param->config_file_path[index] = (char *)malloc(strlen(sa_config_file_path[index]) + 1); + if (param->config_file_path[index] != NULL) { + memcpy(param->config_file_path[index], sa_config_file_path[index], strlen(sa_config_file_path[index]) + 1); + _D("path [%d]: %s", index, param->config_file_path[index]); + } else { + _E("malloc failed for config_file_path"); + __free_main_thread_param(param); + return NULL; + } + } + param->setup_cb = (void *)&setup_cb; + param->completed_notify_cb = (void *)&completed_notify_cb; + } + } else { + _E("malloc failed for param->config_file_path"); + } + return param; +} + +// config for main thread & callback function +void *setting_main_thread(void *pv) +{ + sa_main_thread_s *param = NULL; + sa_error_e ret = SA_ERROR_UNKNOWN; + pthread_t thread = pthread_self(); + int status; + int rv; + + _D("setting_main_thread ~~~"); + // 1. if no processing json files, create from config.json in first booting time + ret = __prepare_config_file(); + if (ret == SA_ERROR_NONE) { + // add user ca-certificates files in first booting time + setup_user_certificates(); + } + // 2. create beluga folder usded for docker engine + sa_generate_folder(ENVIRONMENT_DIR_PATH); + + // 3. configure parameters + param = __create_main_thread_param(); + if (param != NULL) { + // create pthread + rv = pthread_create(&thread, NULL, &sa_inputfile_thread, (void *)param); + if (rv < 0) { + _E("[ADAPTOR_SETTING] thread create error (rv=%d)", rv); + return NULL; + } + pthread_join(thread, (void **)&status); + + // free allocated memory + __free_main_thread_param(param); + } else { + _E("param is NULL "); + return NULL; + } + _D("setting_main_thread exit~~~"); + return NULL; +} |