From 8dfcbc87728f609211c1317e0e2d519cfc079f3e Mon Sep 17 00:00:00 2001 From: HanzZ Date: Tue, 11 Sep 2012 09:13:18 +0200 Subject: [PATCH] Return proper exit code in spectrum2_manager when instance starts --- spectrum/src/main.cpp | 7 +++++-- spectrum_manager/src/main.cpp | 5 ++--- spectrum_manager/src/methods.cpp | 22 ++++++++++++++-------- spectrum_manager/src/methods.h | 2 +- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/spectrum/src/main.cpp b/spectrum/src/main.cpp index c56aef33..7efe1e98 100644 --- a/spectrum/src/main.cpp +++ b/spectrum/src/main.cpp @@ -315,7 +315,6 @@ int main(int argc, char **argv) } #endif - Logging::initMainLogging(&config); #ifndef WIN32 if (!CONFIG_STRING(&config, "service.group").empty() ||!CONFIG_STRING(&config, "service.user").empty() ) { @@ -373,11 +372,15 @@ int main(int argc, char **argv) return -2; } } - else if (!storageBackend->connect()) { + else if (!storageBackend->connect()) { std::cerr << "Can't connect to database. Check the log to find out the reason.\n"; return -1; } + // Logging has to be initialized after all std:cerr output here, because + // it forwards std::cerr to log file. + Logging::initMainLogging(&config); + UserManager userManager(&transport, &userRegistry, storageBackend); userManager_ = &userManager; diff --git a/spectrum_manager/src/main.cpp b/spectrum_manager/src/main.cpp index 233f55b3..636172a6 100644 --- a/spectrum_manager/src/main.cpp +++ b/spectrum_manager/src/main.cpp @@ -126,7 +126,7 @@ int main(int argc, char **argv) } if (command[0] == "start") { - start_instances(&config); + return start_instances(&config); } else if (command[0] == "stop") { stop_instances(&config); @@ -155,8 +155,7 @@ int main(int argc, char **argv) std::string cmd = boost::algorithm::join(command, " "); if (cmd == "start") { - start_instances(&config, jid); - return 0; + return start_instances(&config, jid); } else if (cmd == "stop") { stop_instances(&config, jid); diff --git a/spectrum_manager/src/methods.cpp b/spectrum_manager/src/methods.cpp index d3197739..eca476c8 100644 --- a/spectrum_manager/src/methods.cpp +++ b/spectrum_manager/src/methods.cpp @@ -72,7 +72,7 @@ std::string searchForBinary(const std::string &binary) { } // Executes new backend -unsigned long exec_(std::string path, std::string config, std::string jid) { +unsigned long exec_(std::string path, std::string config, std::string jid, int &rc) { // fork and exec pid_t pid = fork(); if ( pid == 0 ) { @@ -87,7 +87,7 @@ unsigned long exec_(std::string path, std::string config, std::string jid) { // fork failed } else { - waitpid(pid, 0, 0); + waitpid(pid, &rc, 0); } return (unsigned long) pid; @@ -128,25 +128,26 @@ int isRunning(const std::string &pidfile) { return boost::lexical_cast(pid); } -void start_instances(ManagerConfig *config, const std::string &_jid) { +int start_instances(ManagerConfig *config, const std::string &_jid) { + int rv = 0; response = ""; 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); + return 6; } if (!is_directory(p)) { std::cerr << "Config directory " << CONFIG_STRING(config, "service.config_directory") << " does not exist\n"; - exit(7); + return 7; } std::string spectrum2_binary = searchForBinary("spectrum2"); if (spectrum2_binary.empty()) { std::cerr << "spectrum2 binary not found in PATH\n"; - exit(8); + return 8; } directory_iterator end_itr; @@ -177,9 +178,13 @@ void start_instances(ManagerConfig *config, const std::string &_jid) { int pid = isRunning(CONFIG_STRING(&vhostCfg, "service.pidfile")); if (pid == 0) { + int rc; response = "Starting " + itr->path().string() + ": OK\n"; std::cout << "Starting " << itr->path() << ": OK\n"; - exec_(spectrum2_binary, itr->path().string(), vhost); + exec_(spectrum2_binary, itr->path().string(), vhost, rc); + if (rv == 0) { + rv = rc; + } } else { response = "Starting " + itr->path().string() + ": Already started (PID=" + boost::lexical_cast(pid) + ")\n"; @@ -191,8 +196,9 @@ void start_instances(ManagerConfig *config, const std::string &_jid) { } catch (const filesystem_error& ex) { std::cerr << "boost filesystem error\n"; - exit(5); + return 6; } + return rv; } void stop_instances(ManagerConfig *config, const std::string &_jid) { diff --git a/spectrum_manager/src/methods.h b/spectrum_manager/src/methods.h index 401cd115..34ac270b 100644 --- a/spectrum_manager/src/methods.h +++ b/spectrum_manager/src/methods.h @@ -44,7 +44,7 @@ unsigned long exec_(std::string path, std::string config, std::string jid = ""); int getPort(const std::string &portfile); int isRunning(const std::string &pidfile); -void start_instances(ManagerConfig *config, const std::string &_jid = ""); +int start_instances(ManagerConfig *config, const std::string &_jid = ""); void stop_instances(ManagerConfig *config, const std::string &_jid = ""); int show_status(ManagerConfig *config);