Refactor the init script and remove the -d option.

Fixes #3666
This commit is contained in:
Gunnar Beutner 2013-03-12 11:48:45 +01:00
parent b19eedf8ec
commit 4b2d828b99
5 changed files with 19 additions and 63 deletions

View File

@ -22,6 +22,7 @@ localstatedir=@localstatedir@
DAEMON=$sbindir/icinga2
ICINGA2_CONFIG_FILE=$sysconfdir/icinga2/icinga2.conf
ICINGA2_PID_FILE=$localstatedir/run/icinga2/icinga2.pid
ICINGA2_ERROR_LOG=$localstatedir/log/icinga2/error.log
test -x $DAEMON || exit 0
@ -38,9 +39,18 @@ fi
# Start Icinga 2
start() {
mkdir -p `dirname -- $ICINGA2_PID_FILE`
mkdir -p `dirname -- $ICINGA2_ERROR_LOG`
echo "Validating the configuration file:"
if ! $DAEMON -c $ICINGA2_CONFIG_FILE -v; then
echo "Not starting Icinga 2 due to configuration errors."
exit 1
fi
printf "Starting Icinga 2: "
$DAEMON -c $ICINGA2_CONFIG_FILE -d
$DAEMON -c $ICINGA2_CONFIG_FILE </dev/null >/dev/null 2>$ICINGA2_ERROR_LOG &
disown
echo "Done"
echo
}
@ -55,7 +65,7 @@ stop() {
pid=`cat $ICINGA2_PID_FILE`
if kill -TERM $pid >/dev/null 2>&1; then
if kill -INT $pid >/dev/null 2>&1; then
for i in 1 2 3 4 5 6 7 8 9 10; do
if ! kill -CHLD $pid >/dev/null 2&>1; then
break
@ -66,7 +76,11 @@ stop() {
sleep 1
done
fi
if kill -CHLD $pid >/dev/null 2&>1; then
kill -KILL $pid
fi
echo "Done"
}

View File

@ -152,7 +152,6 @@ int main(int argc, char **argv)
("config,c", po::value<vector<String> >(), "parse a configuration file")
("validate,v", "exit after validating the configuration")
("debug,x", "enable debugging")
("daemonize,d", "daemonize after reading the configuration files")
;
try {
@ -238,7 +237,7 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
if (validateOnly) {
Logger::Write(LogInformation, "icinga-app", "Terminating as requested by --validate.");
Logger::Write(LogInformation, "icinga-app", "Finished validating the configuration file(s).");
return EXIT_SUCCESS;
}
@ -247,11 +246,6 @@ int main(int argc, char **argv)
if (!app)
BOOST_THROW_EXCEPTION(runtime_error("Configuration must create an Application object."));
if (g_AppParams.count("daemonize")) {
Logger::Write(LogInformation, "icinga", "Daemonizing.");
Utility::Daemonize();
}
#ifndef _WIN32
struct sigaction sa;
memset(&sa, 0, sizeof(sa));

View File

@ -464,7 +464,7 @@ void Application::UpdatePidFile(const String& filename)
/* There's just no sane way of getting a file descriptor for a
* C++ ofstream which is why we're using FILEs here. */
m_PidFile = fopen(filename.CStr(), "w");
m_PidFile = fopen(filename.CStr(), "r+");
if (m_PidFile == NULL)
BOOST_THROW_EXCEPTION(runtime_error("Could not open PID file '" + filename + "'"));

View File

@ -58,56 +58,6 @@ String Utility::GetTypeName(const type_info& ti)
return DemangleSymbolName(ti.name());
}
/**
* Detaches from the controlling terminal.
*/
void Utility::Daemonize(void) {
#ifndef _WIN32
pid_t pid;
int fd;
pid = fork();
if (pid < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< errinfo_api_function("fork")
<< errinfo_errno(errno));
}
if (pid)
_exit(0);
fd = open("/dev/null", O_RDWR);
if (fd < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< errinfo_api_function("open")
<< errinfo_errno(errno)
<< errinfo_file_name("/dev/null"));
}
if (fd != STDIN_FILENO)
dup2(fd, STDIN_FILENO);
if (fd != STDOUT_FILENO)
dup2(fd, STDOUT_FILENO);
if (fd != STDERR_FILENO)
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO)
close(fd);
if (setsid() < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< errinfo_api_function("setsid")
<< errinfo_errno(errno));
}
#endif
}
/**
* Initializes the OpenSSL library.
*/

View File

@ -34,8 +34,6 @@ public:
static String DemangleSymbolName(const String& sym);
static String GetTypeName(const type_info& ti);
static void Daemonize(void);
static shared_ptr<SSL_CTX> MakeSSLContext(const String& pubkey, const String& privkey, const String& cakey);
static String GetCertificateCN(const shared_ptr<X509>& certificate);
static shared_ptr<X509> GetX509Certificate(const String& pemfile);