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. --- ChangeLog | 4 ++++ lib/helper.c | 34 +++++++++++++++++++++++++++++----- util/ulockmgr_server.c | 17 +++++++++++++++-- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 655bee8..eedab2f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2011-09-23 Miklos Szeredi + + * Replace daemon() function with fork(). Patch by Anatol Pomozov + 2011-08-26 Miklos Szeredi * If configured with --disable-mtab then don't call mount(8) from 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; } diff --git a/util/ulockmgr_server.c b/util/ulockmgr_server.c index 583fcf9..baef45d 100644 --- a/util/ulockmgr_server.c +++ b/util/ulockmgr_server.c @@ -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); -- cgit v1.2.3