From 7728b36a83fe20b366b1b6e72f3d0906ca89840c Mon Sep 17 00:00:00 2001 From: Anatol Pomozov Date: Fri, 2 Sep 2011 16:26:09 -0700 Subject: 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. --- lib/helper.c | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'lib/helper.c') 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; } -- cgit v1.2.3