From: Baptiste Daroussin Date: Fri, 17 Dec 2021 08:18:47 +0000 (+0100) Subject: mlmmj-maintd: use daemon(3) instead of home made version X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=103955bb046b290b79b5f13b0a3fd9cafa5bfc68;p=thirdparty%2Fmlmmj.git mlmmj-maintd: use daemon(3) instead of home made version --- diff --git a/src/mlmmj-maintd.c b/src/mlmmj-maintd.c index 9a0ea740..1c68933f 100644 --- a/src/mlmmj-maintd.c +++ b/src/mlmmj-maintd.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "xmalloc.h" #include "mlmmj-maintd.h" @@ -64,42 +65,14 @@ static void print_help(const char *prg) static int mydaemon(const char *rootdir) { - int i; - pid_t pid; - - if((pid = fork()) < 0) - return -1; - else if (pid) - exit(EXIT_SUCCESS); /* parent says bye bye */ - - if(setsid() < 0) { - log_error(LOG_ARGS, "Could not setsid()"); - return -1; + if (daemon(0, 1) != 0) { + log_error(LOG_ARGS, "Unable to become a daemon"); + err(EXIT_FAILURE, "Unable to become a daemon"); } - if(signal(SIGHUP, SIG_IGN) == SIG_ERR) { - log_error(LOG_ARGS, "Could not signal(SIGHUP, SIG_IGN)"); - return -1; - } - - if((pid = fork()) < 0) - return -1; - else if (pid) - exit(EXIT_SUCCESS); /* parent says bye bye */ - if(chdir(rootdir) < 0) log_error(LOG_ARGS, "Could not chdir(%s)", rootdir); - i = sysconf(_SC_OPEN_MAX); - if(i < 0) - i = 256; - while(i >= 0) - close(i--); - - open("/dev/null", O_RDONLY); - open("/dev/null", O_RDWR); - open("/dev/null", O_RDWR); - return 0; }