From 36c650399b51a09c8499a19f65275bddd7c67cbc Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Thu, 31 May 2018 15:00:10 +0200 Subject: [PATCH] rework app startup to search for config files in user's home directory --- lib/include/srslte/common/config_file.h | 59 +++++++++++++++++++++++ srsenb/src/main.cc | 62 ++++++++++++++++--------- srsepc/src/main.cc | 36 ++++++++------ srsepc/src/mbms-gw/main.cc | 29 ++++++------ srsue/src/main.cc | 25 +++++----- 5 files changed, 151 insertions(+), 60 deletions(-) create mode 100644 lib/include/srslte/common/config_file.h diff --git a/lib/include/srslte/common/config_file.h b/lib/include/srslte/common/config_file.h new file mode 100644 index 000000000..80391f19a --- /dev/null +++ b/lib/include/srslte/common/config_file.h @@ -0,0 +1,59 @@ +/** + * + * \section COPYRIGHT + * + * Copyright 2013-2015 Software Radio Systems Limited + * + * \section LICENSE + * + * This file is part of the srsUE library. + * + * srsUE is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * srsUE is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * A copy of the GNU Affero General Public License can be found in + * the LICENSE file in the top-level directory of this distribution + * and at http://www.gnu.org/licenses/. + * + */ + +#ifndef SRSLTE_CONFIG_FILE_H +#define SRSLTE_CONFIG_FILE_H + +#include +#include +#include "common.h" + +bool config_exists(std::string &filename, std::string default_name) +{ + std::ifstream conf(filename.c_str(), std::ios::in); + if(conf.fail()) { + const char *homedir = NULL; + char full_path[256]; + ZERO_OBJECT(full_path); + if ((homedir = getenv("HOME")) == NULL) { + homedir = getpwuid(getuid())->pw_dir; + } + if (!homedir) { + homedir = "."; + } + snprintf(full_path, sizeof(full_path), "%s/.srs/%s", homedir, default_name.c_str()); + filename = std::string(full_path); + + // try to open again + conf.open(filename.c_str()); + if (conf.fail()) { + return false; + } + } + return true; +} + +#endif // SRSLTE_CONFIG_FILE_H diff --git a/srsenb/src/main.cc b/srsenb/src/main.cc index 3d4af3501..8a2a5ce2e 100644 --- a/srsenb/src/main.cc +++ b/srsenb/src/main.cc @@ -30,11 +30,13 @@ #include #include +#include "srslte/common/config_file.h" + #include -#include #include #include #include +#include #include "srsenb/hdr/enb.h" #include "srsenb/hdr/metrics_stdout.h" @@ -224,30 +226,30 @@ void parse_args(all_args_t *args, int argc, char* argv[]) { } // print version number and exit - // print version number and exit - if (vm.count("version")) { - cout << "Version " << - srslte_get_version_major() << "." << - srslte_get_version_minor() << "." << - srslte_get_version_patch() << endl; - exit(0); - } + if (vm.count("version")) { + cout << "Version " << + srslte_get_version_major() << "." << + srslte_get_version_minor() << "." << + srslte_get_version_patch() << endl; + exit(0); + } - // no config file given - print usage and exit + // if no config file given, check users home path if (!vm.count("config_file")) { - cout << "Error: Configuration file not provided" << endl; - cout << "Usage: " << argv[0] << " [OPTIONS] config_file" << endl << endl; - exit(0); - } else { - cout << "Reading configuration file " << config_file << "..." << endl; - ifstream conf(config_file.c_str(), ios::in); - if(conf.fail()) { - cout << "Failed to read configuration file " << config_file << " - exiting" << endl; - exit(1); - } - bpo::store(bpo::parse_config_file(conf, common), vm); - bpo::notify(vm); + if (!config_exists(config_file, "enb.conf")) { + cout << "Failed to read eNB configuration file " << config_file << " - exiting" << endl; + exit(1); + } + } + + cout << "Reading configuration file " << config_file << "..." << endl; + ifstream conf(config_file.c_str(), ios::in); + if(conf.fail()) { + cout << "Failed to read configuration file " << config_file << " - exiting" << endl; + exit(1); } + bpo::store(bpo::parse_config_file(conf, common), vm); + bpo::notify(vm); // Convert hex strings { @@ -329,6 +331,22 @@ void parse_args(all_args_t *args, int argc, char* argv[]) { args->log.s1ap_hex_limit = args->log.all_hex_limit; } } + + // Check remaining eNB config files + if (!config_exists(args->enb_files.sib_config, "sib.conf")) { + cout << "Failed to read SIB configuration file " << args->enb_files.sib_config << " - exiting" << endl; + exit(1); + } + + if (!config_exists(args->enb_files.rr_config, "rr.conf")) { + cout << "Failed to read RR configuration file " << args->enb_files.rr_config << " - exiting" << endl; + exit(1); + } + + if (!config_exists(args->enb_files.drb_config, "drb.conf")) { + cout << "Failed to read DRB configuration file " << args->enb_files.drb_config << " - exiting" << endl; + exit(1); + } } static int sigcnt = 0; diff --git a/srsepc/src/main.cc b/srsepc/src/main.cc index e2c006750..beed67709 100644 --- a/srsepc/src/main.cc +++ b/srsepc/src/main.cc @@ -28,6 +28,7 @@ #include #include #include "srslte/common/bcd_helpers.h" +#include "srslte/common/config_file.h" #include "srsepc/hdr/mme/mme.h" #include "srsepc/hdr/hss/hss.h" #include "srsepc/hdr/spgw/spgw.h" @@ -154,23 +155,23 @@ parse_args(all_args_t *args, int argc, char* argv[]) { exit(0); } - //Parsing Config File + // if no config file given, check users home path if (!vm.count("config_file")) { - cout << "Error: Configuration file not provided" << endl; - cout << "Usage: " << argv[0] << " [OPTIONS] config_file" << endl << endl; - exit(0); - } else { - cout << "Reading configuration file " << config_file << "..." << endl; - ifstream conf(config_file.c_str(), ios::in); - if(conf.fail()) { - cout << "Failed to read configuration file " << config_file << " - exiting" << endl; - exit(1); - } - bpo::store(bpo::parse_config_file(conf, common), vm); - bpo::notify(vm); + if (!config_exists(config_file, "epc.conf")) { + cout << "Failed to read ePC configuration file " << config_file << " - exiting" << endl; + exit(1); + } } - + //Parsing Config File + cout << "Reading configuration file " << config_file << "..." << endl; + ifstream conf(config_file.c_str(), ios::in); + if(conf.fail()) { + cout << "Failed to read configuration file " << config_file << " - exiting" << endl; + exit(1); + } + bpo::store(bpo::parse_config_file(conf, common), vm); + bpo::notify(vm); //Concert hex strings { @@ -246,6 +247,13 @@ parse_args(all_args_t *args, int argc, char* argv[]) { args->log_args.hss_hex_limit = args->log_args.all_hex_limit; } } + + // Check user database + if (!config_exists(args->hss_args.db_file, "user_db.csv")) { + cout << "Failed to read HSS user database file " << args->hss_args.db_file << " - exiting" << endl; + exit(1); + } + return; } diff --git a/srsepc/src/mbms-gw/main.cc b/srsepc/src/mbms-gw/main.cc index 379d56a36..a54f071c8 100644 --- a/srsepc/src/mbms-gw/main.cc +++ b/srsepc/src/mbms-gw/main.cc @@ -27,6 +27,7 @@ #include #include #include +#include "srslte/common/config_file.h" #include "srsepc/hdr/mbms-gw/mbms-gw.h" using namespace std; @@ -131,22 +132,24 @@ parse_args(all_args_t *args, int argc, char* argv[]) { exit(0); } - //Parsing Config File + // if no config file given, check users home path if (!vm.count("config_file")) { - cout << "Error: Configuration file not provided" << endl; - cout << "Usage: " << argv[0] << " [OPTIONS] config_file" << endl << endl; - exit(0); - } else { - cout << "Reading configuration file " << config_file << "..." << endl; - ifstream conf(config_file.c_str(), ios::in); - if(conf.fail()) { - cout << "Failed to read configuration file " << config_file << " - exiting" << endl; - exit(1); - } - bpo::store(bpo::parse_config_file(conf, common), vm); - bpo::notify(vm); + if (!config_exists(config_file, "mbms.conf")) { + cout << "Failed to read MBMS-GW configuration file " << config_file << " - exiting" << endl; + exit(1); + } } + //Parsing Config File + cout << "Reading configuration file " << config_file << "..." << endl; + ifstream conf(config_file.c_str(), ios::in); + if(conf.fail()) { + cout << "Failed to read configuration file " << config_file << " - exiting" << endl; + exit(1); + } + bpo::store(bpo::parse_config_file(conf, common), vm); + bpo::notify(vm); + args->mbms_gw_args.name = mbms_gw_name; args->mbms_gw_args.sgi_mb_if_addr = mbms_gw_sgi_mb_if_addr; args->mbms_gw_args.m1u_multi_addr = mbms_gw_m1u_multi_addr; diff --git a/srsue/src/main.cc b/srsue/src/main.cc index e0a36ddc5..fd83596d4 100644 --- a/srsue/src/main.cc +++ b/srsue/src/main.cc @@ -37,6 +37,7 @@ #include #include "srsue/hdr/ue.h" +#include "srslte/common/config_file.h" #include "srslte/srslte.h" #include "srsue/hdr/metrics_stdout.h" #include "srsue/hdr/metrics_csv.h" @@ -343,22 +344,24 @@ void parse_args(all_args_t *args, int argc, char *argv[]) { exit(0); } - // no config file given - print usage and exit + // if no config file given, check users home path if (!vm.count("config_file")) { - cout << "Error: Configuration file not provided" << endl; - cout << "Usage: " << argv[0] << " [OPTIONS] config_file" << endl << endl; - exit(0); - } else { - cout << "Reading configuration file " << config_file << "..." << endl; - ifstream conf(config_file.c_str(), ios::in); - if (conf.fail()) { - cout << "Failed to read configuration file " << config_file << " - exiting" << endl; + + if (!config_exists(config_file, "ue.conf")) { + cout << "Failed to read UE configuration file " << config_file << " - exiting" << endl; exit(1); } - bpo::store(bpo::parse_config_file(conf, common), vm); - bpo::notify(vm); } + cout << "Reading configuration file " << config_file << "..." << endl; + ifstream conf(config_file.c_str(), ios::in); + if (conf.fail()) { + cout << "Failed to read configuration file " << config_file << " - exiting" << endl; + exit(1); + } + bpo::store(bpo::parse_config_file(conf, common), vm); + bpo::notify(vm); + // Apply all_level to any unset layers if (vm.count("log.all_level")) { if (!vm.count("log.phy_level")) {