Skip to content

Commit

Permalink
Added a daemonize() function to account for certain conditions where …
Browse files Browse the repository at this point in the history
…the forked process would still die (e.g. if mounted as a home directory and the user logs in multiple times).
  • Loading branch information
LinuxMercedes authored and demonfoo committed Mar 19, 2012
1 parent 5db1052 commit 39b9a01
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions examples/loopback.pl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -175,25 +175,35 @@ sub x_statfs {
} }
return 255,1000000,500000,1000000,500000,4096; 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) = ''; my ($mountpoint) = '';
$mountpoint = shift(@ARGV) if @ARGV; $mountpoint = shift(@ARGV) if @ARGV;


if (! -d $mountpoint) { if (! -d $mountpoint) {
print STDERR "ERROR: attempted to mount to non-directory\n"; print STDERR "ERROR: attempted to mount to non-directory\n";
return -&ENOTDIR return -&ENOTDIR
} }
my $pid = fork();
die("fork() failed: $!") unless defined $pid;


if ($pid > 0) { daemonize();
# parent process
exit(0);
}
if ($pidfile) {
open(PIDFILE, '>', $pidfile);
print PIDFILE $$, "\n";
close(PIDFILE);
}
Fuse::main( Fuse::main(
'mountpoint' => $mountpoint, 'mountpoint' => $mountpoint,
'getattr' => 'main::x_getattr', 'getattr' => 'main::x_getattr',
Expand Down

0 comments on commit 39b9a01

Please sign in to comment.