X-Git-Url: http://git.rot13.org/?p=perl-fuse.git;a=blobdiff_plain;f=examples%2Floopback.pl;h=b81ec08fbce60ab70984c822a65fbe393b95a332;hp=88d81b9f7f1a197cd0c326e0e28a9f0ebcdf542c;hb=39b9a017017c805f0421f9d8b55788ccb1ec8516;hpb=5db105210ac5acbc268736362148359c9cc8158c diff --git a/examples/loopback.pl b/examples/loopback.pl index 88d81b9..b81ec08 100755 --- a/examples/loopback.pl +++ b/examples/loopback.pl @@ -175,6 +175,25 @@ sub x_statfs { } return 255,1000000,500000,1000000,500000,4096; } + +# Required for some edge cases where a simple fork() won't do. +# from http://perldoc.perl.org/perlipc.html#Complete-Dissociation-of-Child -from-Parent +sub daemonize { + chdir("/") || die "can't chdir to /: $!"; + open(STDIN, "< /dev/null") || die "can't read /dev/null: $!"; + open(STDOUT, "> /dev/null") || die "can't write to /dev/null: $!"; + defined(my $pid = fork()) || die "can't fork: $!"; + exit if $pid; # non-zero now means I am the parent + (setsid() != -1) || die "Can't start a new session: $!"; + open(STDERR, ">&STDOUT") || die "can't dup stdout: $!"; + + if ($pidfile) { + open(PIDFILE, '>', $pidfile); + print PIDFILE $$, "\n"; + close(PIDFILE); + } +} + my ($mountpoint) = ''; $mountpoint = shift(@ARGV) if @ARGV; @@ -182,18 +201,9 @@ if (! -d $mountpoint) { print STDERR "ERROR: attempted to mount to non-directory\n"; return -&ENOTDIR } -my $pid = fork(); -die("fork() failed: $!") unless defined $pid; -if ($pid > 0) { - # parent process - exit(0); -} -if ($pidfile) { - open(PIDFILE, '>', $pidfile); - print PIDFILE $$, "\n"; - close(PIDFILE); -} +daemonize(); + Fuse::main( 'mountpoint' => $mountpoint, 'getattr' => 'main::x_getattr',