*sigh* some linux implementations have the function daemon.
This commit is contained in:
+17
-14
@@ -28,8 +28,10 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef daemon
|
||||||
|
|
||||||
int
|
int
|
||||||
daemon()
|
daemon(int nochdir, int noclose)
|
||||||
{
|
{
|
||||||
int maxfd, fd = 0;
|
int maxfd, fd = 0;
|
||||||
|
|
||||||
@@ -61,22 +63,23 @@ daemon()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
chdir("/"); /* Change working directory. */
|
if (!nochdir)
|
||||||
|
chdir("/"); /* Change working directory. */
|
||||||
|
|
||||||
/* Close all open handles. */
|
if (!noclose)
|
||||||
maxfd = sysconf(_SC_OPEN_MAX);
|
{
|
||||||
while (fd < maxfd)
|
/* Close all open handles. */
|
||||||
close(fd++);
|
maxfd = sysconf(_SC_OPEN_MAX);
|
||||||
|
while (fd < maxfd)
|
||||||
|
close(fd++);
|
||||||
|
|
||||||
/* Use /dev/null as stdin/stdout/stderr. */
|
/* Use /dev/null as stdin/stdout/stderr. */
|
||||||
open("/dev/null", O_RDWR);
|
open("/dev/null", O_RDWR);
|
||||||
dup(0);
|
dup(0);
|
||||||
dup(0);
|
dup(0);
|
||||||
|
}
|
||||||
/* Set default permissions for created files. */
|
|
||||||
umask(027);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -58,7 +58,10 @@ TerminateHandler(int signal)
|
|||||||
|
|
||||||
// TODO: Hack
|
// TODO: Hack
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
int daemon();
|
#include <unistd.h>
|
||||||
|
#ifndef daemon
|
||||||
|
int daemon(int, int);
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int
|
int
|
||||||
@@ -70,7 +73,7 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
// TODO: Hack
|
// TODO: Hack
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
daemon();
|
daemon(0, 0);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
signal(SIGTERM, TerminateHandler);
|
signal(SIGTERM, TerminateHandler);
|
||||||
|
|||||||
Reference in New Issue
Block a user