odyssey/sources/daemon.c

32 lines
380 B
C
Raw Normal View History

/*
2018-03-12 14:03:15 +00:00
* Odyssey.
*
2018-04-04 13:19:58 +00:00
* Scalable PostgreSQL connection pooler.
*/
2020-11-03 08:12:06 +00:00
#include "daemon.h"
int od_daemonize(void)
{
pid_t pid = fork();
if (pid < 0)
return -1;
if (pid > 0) {
/* shutdown parent */
_exit(0);
}
setsid();
int fd;
fd = open("/dev/null", O_RDWR);
if (fd < 0)
return -1;
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
if (fd > 2) {
close(fd);
}
return 0;
}