mirror of
https://github.com/Icinga/icinga2.git
synced 2025-04-08 17:05:25 +02:00
Implemented Application::Daemonize method
This commit is contained in:
parent
4a636d92fe
commit
238e02b56a
@ -108,6 +108,50 @@ void Application::RunEventLoop(void)
|
||||
}
|
||||
}
|
||||
|
||||
bool Application::Daemonize(void) {
|
||||
#ifndef _WIN32
|
||||
pid_t pid;
|
||||
pid_t sid;
|
||||
int fd;
|
||||
|
||||
pid = fork();
|
||||
if (pid == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pid) {
|
||||
fprintf(stdout, "DONE\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
fd = open("/dev/null", O_RDWR);
|
||||
if (fd) {
|
||||
if (fd != 0) {
|
||||
dup2(fd, 0);
|
||||
}
|
||||
|
||||
if (fd != 1) {
|
||||
dup2(fd, 1);
|
||||
}
|
||||
|
||||
if (fd != 2) {
|
||||
dup2(fd, 2);
|
||||
}
|
||||
|
||||
if (fd > 2) {
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
sid = setsid();
|
||||
if (sid == -1) {
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Application::Shutdown(void)
|
||||
{
|
||||
m_ShuttingDown = true;
|
||||
|
@ -22,6 +22,7 @@ public:
|
||||
virtual int Main(const vector<string>& args) = 0;
|
||||
|
||||
void RunEventLoop(void);
|
||||
bool Daemonize(void);
|
||||
void Shutdown(void);
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user