mirror of https://github.com/Icinga/icinga2.git
49 lines
529 B
C++
49 lines
529 B
C++
|
#include "i2-base.h"
|
||
|
|
||
|
using namespace icinga;
|
||
|
|
||
|
/**
|
||
|
* Daemonize
|
||
|
*
|
||
|
* Detaches from the controlling terminal.
|
||
|
*/
|
||
|
void Utility::Daemonize(void) {
|
||
|
#ifndef _WIN32
|
||
|
pid_t pid;
|
||
|
pid_t sid;
|
||
|
int fd;
|
||
|
|
||
|
pid = fork();
|
||
|
if (pid == -1) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (pid)
|
||
|
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
|
||
|
}
|