diff options
Diffstat (limited to 'src/configurator.cpp')
-rw-r--r-- | src/configurator.cpp | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/src/configurator.cpp b/src/configurator.cpp new file mode 100644 index 0000000..bcf374b --- /dev/null +++ b/src/configurator.cpp @@ -0,0 +1,251 @@ +/** + * @file configurator.cpp + * + * @brief Source file containing implementation of the settingsd + * configuration manager. + * + * @author Ossama Othman @<ossama.othman@@intel.com@> + * + * @copyright @par + * Copyright 2012, 2013 Intel Corporation All Rights Reserved. + * @par + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License. + * @par + * This library 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 + * Lesser General Public License for more details. + * @par + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + + +#include "../lib/config.hpp" +#include "configurator.hpp" + +#include <unistd.h> +#include <fstream> +#include <cstdlib> + + +namespace po = boost::program_options; + +ivi::settings::configurator::configurator(int & argc, + char * argv[], + char const * /* logger_name */) + : vm_() +{ + parse_config(argc, argv); +} + +std::string +ivi::settings::configurator::settings_dir() const +{ + return vm_["settings-dir"].as<std::string>(); +} + +int +ivi::settings::configurator::websocket_port() const +{ + return vm_["websocket-port"].as<int>(); +} + +char const * +ivi::settings::configurator::ssl_cert_file() const +{ + return libwebsocket_ssl_filepath("ssl-cert-file"); +} + +char const * +ivi::settings::configurator::ssl_private_key_file() const +{ + return libwebsocket_ssl_filepath("ssl-private-key-file"); +} + +char const * +ivi::settings::configurator::ssl_ca_file() const +{ + return libwebsocket_ssl_filepath("ssl-ca-file"); +} + +void +ivi::settings::configurator::parse_config(int & argc, char * argv[]) +{ + /// Options recognized by settingsd. + boost::program_options::options_description desc( + "Allowed " PACKAGE " options"); + + desc.add_options() + ("settings-dir", + po::value<std::string>()->default_value(IVI_SETTINGS_DEFAULT_SETTINGS_DIR), + "settings plugin directory") + ("websocket-port", + po::value<int>()->default_value(IVI_SETTINGS_DEFAULT_WEBSOCKET_PORT), + "settings web socket server TCP/IP port") + ("ssl-cert-file", + po::value<std::string>(), + "SSL server certificate file") + ("ssl-private-key-file", + po::value<std::string>(), + "SSL private key file") + ("ssl-ca-file", + po::value<std::string>(), + "SSL CA certificate file") + ("version", "display version information") + ("help", "display help message"); + + // Option precedence - high to low: + // + // 1. Command line + // 2. User config + // 3. System config + // + // The store() function will only assign a value once, and will + // not change it's value subsequently. + po::store(po::parse_command_line(argc, argv, desc), vm_); + + /** + * @todo Check for config in $XDG_CONFIG_DIRS and $XDG_CONFIG_HOME. + */ + // User config is stored in ~/.config/ivi_settings. + std::ifstream user_config_file(std::string(std::getenv("HOME")) + + "/.config/" PACKAGE); + if (user_config_file) + po::store(po::parse_config_file(user_config_file, desc), vm_); + + // Configuration file name such as + // "/etc/settingsd/settingsd.conf" + static char const config_filename[] = + IVI_SETTINGS_CONFIG_DIR "/" PACKAGE ".conf"; + + std::ifstream system_config_file(config_filename); + if (system_config_file) + po::store(po::parse_config_file(system_config_file, desc), vm_); + + po::notify(vm_); + + if (vm_.count("version")) { + display_version_info(); + std::exit(EXIT_SUCCESS); + } else if (vm_.count("help")) { + std::cout << desc << std::endl; + std::exit(EXIT_SUCCESS); + } +} + +void +ivi::settings::configurator::display_version_info() const +{ + static char const copyright[] = + PACKAGE_STRING "\n" + "Copyright (c) 2012, 2013 Intel Corporation.\n" + "\n" + "This " PACKAGE_NAME " (\"Software\") is furnished under " + "license and may only be\n" + "used or copied in accordance with the terms of that license.\n" + "No license, express or implied, by estoppel or otherwise, to any\n" + "intellectual property rights is granted by this document. The\n" + "Software is subject to change without notice, and should not be\n" + "construed as a commitment by Intel Corporation to market, license,\n" + "sell or support any product or technology. Unless otherwise provided\n" + "for in the license under which this Software is provided, the\n" + "Software is provided AS IS, with no warranties of any kind, express\n" + "or implied. Except as expressly permitted by the Software license,\n" + "neither Intel Corporation nor its suppliers assumes any\n" + "responsibility or liability for any errors or inaccuracies that may\n" + "appear herein. Except as expressly permitted by the Software\n" + "license, no part of the Software may be reproduced, stored in a\n" + "retrieval system, transmitted in any form, or distributed by any\n" + "means without the express written consent of Intel Corporation.\n"; + + std::cout << copyright << std::endl; +} + +void +ivi::settings::configurator::configure_logger( + char const * /* logger_name */) +{ + // Start with log configuration file, if it exists, and override as + // needed with command line specifed options. + std::string const log_config_filename = + vm_["log-config-file"].as<std::string>(); + + if (access(log_config_filename.c_str(), R_OK) == 0) { + /** + * @bug There is a TOCTOU race here since the config file may + * be yanked out from under us before the + * @c log4plus::PropertyConfigurator pulls the config from + * the file. + */ + + // Reset configuration before pulling configuration from file. + // log4cplus::Logger::getDefaultHierarchy().resetConfiguration(); + + // Now pull the configuration from the file. + // log4cplus::PropertyConfigurator::doConfigure(log_config_filename); + } + + // logger_ = log4cplus::Logger::getInstance(logger_name); + + /// @todo Set the log file. + + // We have a logger. Now set the log level. + set_log_level(); +} + +void +ivi::settings::configurator::set_log_level() +{ +/* + log4cplus::LogLevel level; + + std::string const l = vm_["log-level"].as<std::string>(); + + if (l == "FATAL") + level = log4cplus::FATAL_LOG_LEVEL; + else if (l == "ERROR") + level = log4cplus::ERROR_LOG_LEVEL; + else if (l == "WARN") + level = log4cplus::WARN_LOG_LEVEL; + else if (l == "INFO") + level = log4cplus::INFO_LOG_LEVEL; + else if (l == "DEBUG") + level = log4cplus::DEBUG_LOG_LEVEL; + else if (l == "TRACE") + level = log4cplus::TRACE_LOG_LEVEL; + else if (l == "ALL") + level = log4cplus::ALL_LOG_LEVEL; + else if (l == "OFF") + level = log4cplus::OFF_LOG_LEVEL; + else { + LOG4CPLUS_WARN(logger_, "Unknown log option: \"" + l + "\""); + return; + } + + logger_.setLogLevel(level); +*/ +} + +char const * +ivi::settings::configurator::libwebsocket_ssl_filepath( + std::string const & option) const +{ + using boost::program_options::variable_value; + + variable_value const & value = vm_[option]; + + return value.empty() ? nullptr : value.as<std::string>().c_str(); +} + + +// Local Variables: +// mode:c++ +// c-basic-offset:2 +// indent-tabs-mode: nil +// End: |