From 2b0783187c6432796e557adb58f89c61b67b6b74 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Wed, 3 Feb 2016 06:09:42 +0100 Subject: [PATCH] Spectrum manager: Catch boost::lexical_cast exceptions. --- spectrum_manager/src/main.cpp | 54 -------------------------------- spectrum_manager/src/methods.cpp | 24 ++++++++++++-- 2 files changed, 21 insertions(+), 57 deletions(-) diff --git a/spectrum_manager/src/main.cpp b/spectrum_manager/src/main.cpp index f97a853e..1d560823 100644 --- a/spectrum_manager/src/main.cpp +++ b/spectrum_manager/src/main.cpp @@ -23,54 +23,6 @@ using namespace boost::filesystem; using namespace boost; - -// static void ask_local_servers(ManagerConfig *config, Swift::BoostNetworkFactories &networkFactories, const std::string &message) { -// path p(CONFIG_STRING(config, "service.config_directory")); -// -// try { -// if (!exists(p)) { -// std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n"; -// exit(6); -// } -// -// if (!is_directory(p)) { -// std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n"; -// exit(7); -// } -// -// directory_iterator end_itr; -// for (directory_iterator itr(p); itr != end_itr; ++itr) { -// if (is_regular(itr->path()) && extension(itr->path()) == ".cfg") { -// Config cfg; -// if (cfg.load(itr->path().string()) == false) { -// std::cerr << "Can't load config file " << itr->path().string() << ". Skipping...\n"; -// continue; -// } -// -// if (CONFIG_VECTOR(&cfg, "service.admin_jid").empty() || CONFIG_STRING(&cfg, "service.admin_password").empty()) { -// std::cerr << itr->path().string() << ": service.admin_jid or service.admin_password empty. This server can't be queried over XMPP.\n"; -// continue; -// } -// -// finished++; -// Swift::Client *client = new Swift::Client(CONFIG_VECTOR(&cfg, "service.admin_jid")[0], CONFIG_STRING(&cfg, "service.admin_password"), &networkFactories); -// client->setAlwaysTrustCertificates(); -// client->onConnected.connect(boost::bind(&handleConnected, client, CONFIG_STRING(&cfg, "service.jid"))); -// client->onDisconnected.connect(bind(&handleDisconnected, client, _1, CONFIG_STRING(&cfg, "service.jid"))); -// client->onMessageReceived.connect(bind(&handleMessageReceived, client, _1, CONFIG_STRING(&cfg, "service.jid"))); -// Swift::ClientOptions opt; -// opt.allowPLAINWithoutTLS = true; -// client->connect(opt); -// } -// } -// } -// catch (const filesystem_error& ex) { -// std::cerr << "boost filesystem error\n"; -// exit(5); -// } -// } - - int main(int argc, char **argv) { ManagerConfig config; @@ -172,14 +124,8 @@ int main(int argc, char **argv) } ask_local_server(&config, networkFactories, jid, cmd); -// std::string message = command; -// m = &message; - -// ask_local_server(&config, networkFactories, message); - eventLoop.runUntilEvents(); - struct timeval td_start,td_end; float elapsed = 0; gettimeofday(&td_start, NULL); diff --git a/spectrum_manager/src/methods.cpp b/spectrum_manager/src/methods.cpp index 208fbc06..b510b565 100644 --- a/spectrum_manager/src/methods.cpp +++ b/spectrum_manager/src/methods.cpp @@ -106,7 +106,16 @@ int getPort(const std::string &portfile) { if (port.empty()) return 0; - return boost::lexical_cast(port); + int iport; + try { + iport = boost::lexical_cast(port); + } + catch (const boost::bad_lexical_cast &e) { + std::cout << "Error: Error converting port \"" << port << "\" to integer."; + return 0; + } + + return iport; } int isRunning(const std::string &pidfile) { @@ -122,10 +131,19 @@ int isRunning(const std::string &pidfile) { if (pid.empty()) return 0; - if (kill(boost::lexical_cast(pid), 0) != 0) + int ipid = 0; + try { + ipid = boost::lexical_cast(pid); + } + catch (const boost::bad_lexical_cast &e) { + std::cout << "Error: Error converting pid \"" << pid << "\" to integer."; + return -1; + } + + if (kill(ipid, 0) != 0) return 0; - return boost::lexical_cast(pid); + return ipid; } int start_instances(ManagerConfig *config, const std::string &_jid) {