summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnatol Pomozov <anatol.pomozov@gmail.com>2011-09-02 16:26:09 -0700
committerMiklos Szeredi <mszeredi@suse.cz>2011-09-23 13:28:51 +0200
commit7728b36a83fe20b366b1b6e72f3d0906ca89840c (patch)
treeaa6144ee508e80d4a6dd8c0b6a56606c32fd94bd
parent0131407f4e1a376b9165adca656d799c30af6311 (diff)
downloadfuse-7728b36a83fe20b366b1b6e72f3d0906ca89840c.tar.gz
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.
-rw-r--r--ChangeLog4
-rw-r--r--lib/helper.c34
-rw-r--r--util/ulockmgr_server.c17
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 <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
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);