Error messages: Replace exceptions with log/exit in main application.

Refs #6070
This commit is contained in:
Michael Friedrich 2014-06-05 16:36:03 +02:00
parent 0c021d94cb
commit f0b0420256
1 changed files with 36 additions and 18 deletions

View File

@ -32,6 +32,7 @@
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple.hpp>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <iostream>
#ifndef _WIN32 #ifndef _WIN32
# include <sys/types.h> # include <sys/types.h>
@ -187,9 +188,10 @@ static bool Daemonize(void)
Log(LogCritical, "icinga-app", "The daemon could not be started. See logfile for details."); Log(LogCritical, "icinga-app", "The daemon could not be started. See logfile for details.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else if (ret == -1) { } else if (ret == -1) {
BOOST_THROW_EXCEPTION(posix_error() std::ostringstream msgbuf;
<< boost::errinfo_api_function("waitpid") msgbuf << "waitpid() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
<< boost::errinfo_errno(errno)); Log(LogCritical, "icinga-app", msgbuf.str());
exit(EXIT_FAILURE);
} }
exit(0); exit(0);
@ -389,18 +391,23 @@ int Main(void)
if (!gr) { if (!gr) {
if (errno == 0) { if (errno == 0) {
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid group specified: " + group)); std::ostringstream msgbuf;
msgbuf << "Invalid group specified: " + group;
Log(LogCritical, "icinga-app", msgbuf.str());
return EXIT_FAILURE;
} else { } else {
BOOST_THROW_EXCEPTION(posix_error() std::ostringstream msgbuf;
<< boost::errinfo_api_function("getgrnam") msgbuf << "getgrnam() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
<< boost::errinfo_errno(errno)); Log(LogCritical, "icinga-app", msgbuf.str());
return EXIT_FAILURE;
} }
} }
if (setgid(gr->gr_gid) < 0) { if (setgid(gr->gr_gid) < 0) {
BOOST_THROW_EXCEPTION(posix_error() std::ostringstream msgbuf;
<< boost::errinfo_api_function("setgid") msgbuf << "setgid() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
<< boost::errinfo_errno(errno)); Log(LogCritical, "icinga-app", msgbuf.str());
return EXIT_FAILURE;
} }
} }
@ -412,18 +419,23 @@ int Main(void)
if (!pw) { if (!pw) {
if (errno == 0) { if (errno == 0) {
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid user specified: " + user)); std::ostringstream msgbuf;
msgbuf << "Invalid user specified: " + user;
Log(LogCritical, "icinga-app", msgbuf.str());
return EXIT_FAILURE;
} else { } else {
BOOST_THROW_EXCEPTION(posix_error() std::ostringstream msgbuf;
<< boost::errinfo_api_function("getpwnam") msgbuf << "getpwnam() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
<< boost::errinfo_errno(errno)); Log(LogCritical, "icinga-app", msgbuf.str());
return EXIT_FAILURE;
} }
} }
if (setuid(pw->pw_uid) < 0) { if (setuid(pw->pw_uid) < 0) {
BOOST_THROW_EXCEPTION(posix_error() std::ostringstream msgbuf;
<< boost::errinfo_api_function("setuid") msgbuf << "setuid() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
<< boost::errinfo_errno(errno)); Log(LogCritical, "icinga-app", msgbuf.str());
return EXIT_FAILURE;
} }
} }
#endif /* _WIN32 */ #endif /* _WIN32 */
@ -527,7 +539,12 @@ int Main(void)
if (g_AppParams.count("daemonize")) { if (g_AppParams.count("daemonize")) {
if (!g_AppParams.count("reload-internal")) { if (!g_AppParams.count("reload-internal")) {
// no additional fork neccessary on reload // no additional fork neccessary on reload
try {
Daemonize(); Daemonize();
} catch (std::exception&) {
Log(LogCritical, "icinga-app", "Daemonize failed. Exiting.");
return EXIT_FAILURE;
}
} }
String errorLog; String errorLog;
@ -752,5 +769,6 @@ int main(int argc, char **argv)
#endif /* _WIN32 */ #endif /* _WIN32 */
int rc = Main(); int rc = Main();
exit(rc); exit(rc);
} }