Merge branch 'master' of github.com:dpavlin/perl-fuse
authorDerrik Pates <demon@now.ai>
Wed, 21 Mar 2012 01:49:55 +0000 (19:49 -0600)
committerDerrik Pates <demon@now.ai>
Wed, 21 Mar 2012 01:49:55 +0000 (19:49 -0600)
examples/loopback.pl
test/helper.pm

index 88d81b9..cc395c4 100755 (executable)
@@ -27,7 +27,7 @@ eval {
 use blib;
 use Fuse;
 use IO::File;
-use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);
+use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT setsid);
 use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET S_ISREG S_ISFIFO S_IMODE S_ISCHR S_ISBLK S_ISSOCK);
 use Getopt::Long;
 use Lchown;
@@ -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',
index a259448..ca75dc3 100644 (file)
@@ -8,7 +8,7 @@ our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
 @ISA = "Exporter";
 @EXPORT_OK = qw($_loop $_opts $_point $_pidfile $_real);
 my $tmp = -d '/private' ? '/private/tmp' : '/tmp';
-our($_loop, $_point, $_pidfile, $_real, $_opts) = ('examples/loopback.pl',"$tmp/fusemnt-".$ENV{LOGNAME},"test/s/mounted.pid","$tmp/fusetest-".$ENV{LOGNAME}, '');
+our($_loop, $_point, $_pidfile, $_real, $_opts) = ('examples/loopback.pl',"$tmp/fusemnt-".$ENV{LOGNAME},$ENV{'PWD'} . "/test/s/mounted.pid","$tmp/fusetest-".$ENV{LOGNAME}, '');
 $_opts = '--pidfile ' . $_pidfile;
 $_opts .= $Config{useithreads} ? ' --use-threads' : '';
 if($0 !~ qr|s/u?mount\.t$|) {