odyssey/sources/daemon.c

47 lines
667 B
C
Raw Normal View History

/*
2017-07-05 12:42:49 +00:00
* Odissey.
*
2017-07-05 12:42:49 +00:00
* Advanced PostgreSQL connection pooler.
*/
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
2017-05-31 15:47:15 +00:00
#include <inttypes.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <machinarium.h>
2017-06-07 11:50:58 +00:00
#include <shapito.h>
#include "sources/macro.h"
#include "sources/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;
}