Replace daemon() function with fork()
authorAnatol Pomozov <anatol.pomozov@gmail.com>
Fri, 2 Sep 2011 23:26:09 +0000 (16:26 -0700)
committerMiklos Szeredi <mszeredi@suse.cz>
Fri, 23 Sep 2011 11:28:51 +0000 (13:28 +0200)
daemon() is a BSD-ism. Although it is available on many platforms
it is not a standard function. Some platforms (e.g. MacOSX) deprecated
it.

It is safer just to use fork() function that is a part of POSIX.

ChangeLog
lib/helper.c
util/ulockmgr_server.c

index 655bee8ec3be87d384f469e8c35aac086e7f7ae5..eedab2f1d62845ace76963e50f63f70b26746950 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2011-09-23  Miklos Szeredi <miklos@szeredi.hu>
+
+       * Replace daemon() function with fork().  Patch by Anatol Pomozov
+
 2011-08-26  Miklos Szeredi <miklos@szeredi.hu>
 
        * If configured with --disable-mtab then don't call mount(8) from
index 0ba6d4f5d299a90c2f17229e94bf0dd35b3c0c0d..ace19dd7098bf50027c22c7a6aae5b3c455b58bf 100644 (file)
@@ -179,14 +179,38 @@ err:
 
 int fuse_daemonize(int foreground)
 {
-       int res;
-
        if (!foreground) {
-               res = daemon(0, 0);
-               if (res == -1) {
-                       perror("fuse: failed to daemonize program\n");
+               int nullfd;
+
+               /*
+                * demonize current process by forking it and killing the
+                * parent.  This makes current process as a child of 'init'.
+                */
+               switch(fork()) {
+               case -1:
+                       perror("fuse_daemonize: fork");
+                       return -1;
+               case 0:
+                       break;
+               default:
+                       _exit(0);
+               }
+
+               if (setsid() == -1) {
+                       perror("fuse_daemonize: setsid");
                        return -1;
                }
+
+               (void) chdir("/");
+
+               nullfd = open("/dev/null", O_RDWR, 0);
+               if (nullfd != -1) {
+                       (void) dup2(nullfd, 0);
+                       (void) dup2(nullfd, 1);
+                       (void) dup2(nullfd, 2);
+                       if (nullfd > 2)
+                               close(nullfd);
+               }
        }
        return 0;
 }
index 583fcf9f9ec54b6ad5cc540b1365b7042bc1d7f2..baef45dccc5d842c510b480d097be659051c726c 100644 (file)
@@ -352,11 +352,24 @@ int main(int argc, char *argv[])
        if (*end)
                goto out_inval;
 
-       if (daemon(0, 1) == -1) {
-               perror("ulockmgr_server: daemon");
+       /* demonize current process */
+       switch(fork()) {
+       case -1:
+               perror("ulockmgr_server: fork");
                exit(1);
+       case 0:
+               break;
+       default:
+               _exit(0);
        }
 
+       if (setsid() == -1) {
+               perror("ulockmgr_server: setsid");
+               exit(1);
+       }
+
+       (void) chdir("/");
+
        sigemptyset(&empty);
        sigprocmask(SIG_SETMASK, &empty, NULL);