aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/helper.c34
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;
}