add daemonize() function. Needs to be platform-dependent. :-/

JSON-15
Roker 6 years ago
parent c9df333a9d
commit f67af7b2ce

@ -30,6 +30,7 @@ libjson-adapter.a: json-adapter.o registry.o nfc.o json_rpc.o \
security-token.o \
nfc_sets.o base64.o \
nulllogger.o \
daemonize.o \
json_spirit/json_spirit_reader.o json_spirit/json_spirit_value.o json_spirit/json_spirit_writer.o
ar rcs $@ $^

@ -0,0 +1,51 @@
#include "daemonize.hh"
// Unix/Linux implementation
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdexcept>
void daemonize()
{
/* already a daemon */
if ( getppid() == 1 ) return;
/* Fork off the parent process */
pid_t pid = fork();
if (pid < 0)
{
throw std::runtime_error("Cannot fork!");
}
if (pid > 0)
{
exit(EXIT_SUCCESS); /*Killing the Parent Process*/
}
/* At this point we are executing as the child process */
/* Create a new SID for the child process */
pid_t sid = setsid();
if (sid < 0)
{
throw std::runtime_error("Cannot call setsid()");
}
int fd = open("/dev/null",O_RDWR, 0);
if (fd != -1)
{
dup2 (fd, STDIN_FILENO);
dup2 (fd, STDOUT_FILENO);
dup2 (fd, STDERR_FILENO);
if (fd > 2)
{
close (fd);
}
}
}

@ -0,0 +1,8 @@
#ifndef JSON_SERVER_ADAPTER_DAEMONIZE_HH
#define JSON_SERVER_ADAPTER_DAEMONIZE_HH
// fork(), go into background, close all ttys etc...
// system-specific! (POSIX, Windows, ...?)
void daemonize();
#endif
Loading…
Cancel
Save