]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
mlmmj-maintd: use daemon(3) instead of home made version
authorBaptiste Daroussin <bapt@FreeBSD.org>
Fri, 17 Dec 2021 08:18:47 +0000 (09:18 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Fri, 17 Dec 2021 08:18:47 +0000 (09:18 +0100)
src/mlmmj-maintd.c

index 9a0ea740a8a62b791e00d26552b3b73ba6a37869..1c68933f47895b83c5001d83d9292c33dbbe7d4d 100644 (file)
@@ -32,6 +32,7 @@
 #include <fcntl.h>
 #include <sys/wait.h>
 #include <signal.h>
+#include <err.h>
 
 #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;
 }