diff options
author | Anatol Pomozov <anatol.pomozov@gmail.com> | 2011-09-02 16:26:09 -0700 |
---|---|---|
committer | Miklos Szeredi <mszeredi@suse.cz> | 2011-09-23 13:28:51 +0200 |
commit | 7728b36a83fe20b366b1b6e72f3d0906ca89840c (patch) | |
tree | aa6144ee508e80d4a6dd8c0b6a56606c32fd94bd /lib | |
parent | 0131407f4e1a376b9165adca656d799c30af6311 (diff) | |
download | android_external_fuse-7728b36a83fe20b366b1b6e72f3d0906ca89840c.tar.gz android_external_fuse-7728b36a83fe20b366b1b6e72f3d0906ca89840c.tar.bz2 android_external_fuse-7728b36a83fe20b366b1b6e72f3d0906ca89840c.zip |
Replace daemon() function with fork()
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/helper.c | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/lib/helper.c b/lib/helper.c index 0ba6d4f..ace19dd 100644 --- a/lib/helper.c +++ b/lib/helper.c @@ -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; } |