When daemonizing, change umask to 0 and change working directory to /.

Patch provided by Tim Bruylants (https://github.com/tbr)
This commit is contained in:
Daniel Adolfsson 2016-04-18 09:37:56 +02:00
parent e49b71c0b7
commit 96ab05dade

View File

@ -24,6 +24,7 @@
#include <getopt.h> #include <getopt.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
@ -42,11 +43,16 @@ int daemonize()
if (pid > 0) if (pid > 0)
exit(0); exit(0);
umask(0);
pid_t sid = setsid(); pid_t sid = setsid();
if (sid < 0) if (sid < 0)
return -1; return -1;
if (chdir("/") < 0)
return -1;
close(STDIN_FILENO); close(STDIN_FILENO);
close(STDOUT_FILENO); close(STDOUT_FILENO);
close(STDERR_FILENO); close(STDERR_FILENO);