X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ffmsdaemon.cpp;fp=src%2Ffmsdaemon.cpp;h=0086a02d197a6339f015038a9ff7404ac60ede8f;hb=f60495a029c54358f82956482fe203fe2b7b5b23;hp=0000000000000000000000000000000000000000;hpb=b9c3763a932cebaa015a27fe111017f6f34dfbaa;p=fms.git diff --git a/src/fmsdaemon.cpp b/src/fmsdaemon.cpp new file mode 100644 index 0000000..0086a02 --- /dev/null +++ b/src/fmsdaemon.cpp @@ -0,0 +1,52 @@ +#include "../include/fmsdaemon.h" + +#include +#include +#include +#ifndef _WIN32 + #include + #include +#endif + +#ifdef XMEM + #include +#endif + +/* + modified from http://www-theorie.physik.unizh.ch/~dpotter/howto/daemonize +*/ +void Daemonize() +{ +#ifndef _WIN32 + pid_t pid, sid; + + /* already a daemon */ + if ( getppid() == 1 ) return; + + /* Fork off the parent process */ + pid = fork(); + if (pid < 0) { + exit(EXIT_FAILURE); + } + /* If we got a good PID, then we can exit the parent process. */ + if (pid > 0) { + exit(EXIT_SUCCESS); + } + + /* At this point we are executing as the child process */ + + /* Change the file mode mask */ + umask(0); + + /* Create a new SID for the child process */ + sid = setsid(); + if (sid < 0) { + exit(EXIT_FAILURE); + } + + /* Redirect standard files to /dev/null */ + freopen( "/dev/null", "r", stdin); + freopen( "/dev/null", "w", stdout); + freopen( "/dev/null", "w", stderr); +#endif +}