CandyAngel has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking into using FUSE for a quick project but I have come across, what I think is, a major issue.

With the FUSE module, you can provide an 'init' callback where you can return a scalar to be put into fuse_get_context() as 'private'. I want to use this to store the scripts "main" object so I can access the information needed to respond to the various calls (getattr, read, write etc.).

However, as soon as I try to use get_fuse_context with this, Perl gives me a lot of various errors like "Attempt to copy freed scalar" and "Attempt to free unreferenced scalar" and eventually panic-exits. Printing out fuse_get_context with Data::Dumper, shows that the private key is a "dangling pointer" (sorry, I'm not sure if this is the actual term).

It looks as though when fuse_get_context is called, it copies the structure in such a way that the refcount is not increased, so when it exits scope, it is being freed despite still being in use (it works okay the first time, as shown below).

I think this is the minimal code to reproduce the problem:

#!/usr/bin/env perl use strict; use warnings; use Fuse; my $priv_data = 'PRIVDAT'; Fuse::main ( debug => 1, init => sub { return $priv_data }, mountpoint => 'mnt', getattr => sub { Fuse::fuse_get_context(); return qw[0 0 0040700 1 0 0 0 0 0 0 0 4096 0] }, );

(making $priv_data a global was just to make sure it was always in a scope)

On the fuller code where more callbacks are implemented, it results in values like this:

$ fuse_init_unref.pl | grep private 'private' => 'PRIVDAT' 'private' => 2, 'private' => 0 'private' => ' => ', 'private' => 0, 'private' => [ 'private' => undef, 'private' => undef, 'private' => undef, 'private' => '',

Any ideas of whether it is FUSE or myself at fault? The only workarounds I can think of would involve wrapping all the callbacks so I can inject the object or having singletons..

Replies are listed 'Best First'.
Re: FUSE, fuse_get_context and private = dangling pointer woes
by thanos1983 (Parson) on Oct 09, 2014 at 23:48 UTC

    Hello CandyAngel,

    Welcome to the community. Well I am not really familiar with Fuse to be more exact I never used it before. Well one reason that I can imagine as I was reading the documentation could be access problem access. I went online and I found also this possible solution that might also be a reason that you are having this problems.

    Update: the source link Installing Fuse

    In order for ordinary (non-superuser) users to use Fuse two things must be done: you must change the permissions on the fusermount utility and add users to the fuse group.

    $ sudo chmod 4755 /usr/bin/fusermount

    $ sudo usermod -a -G fuse username

    Update 2: just in case that someone also has the same problem on latest versions of linux OS. I was not able to install Fuse.pm and I manage to found solution to my problem here Cannot install on Ubuntu 12.10.

    The Perl module FUSE has a bug which manifest itself in that sometimes file "fusermount" doesn't have execute permissions after a successful install.

    This file can be in one or more folders:

    /bin/

    /usr/bin/

    /usr/local/bin/

    So, we run 3 commands:

    chmod a+x /bin/fusermount

    chmod a+x /usr/bin/fusermount

    chmod a+x /usr/local/bin/fusermount

    This fix works fine. But it can show up 1 or 2 errors that the file does not exist.

    In my case by applying chmod a+x /bin/fusermount worked perfectly and I manage to install the module.

    Update 3: I think the code that you have copy and paste you have some errors. I execute your code and I get:

    fuse: failed to access mountpoint mnt: No such file or directory could not mount fuse filesystem!

    Because mountpoint  => 'mnt', does not exist, so I modify it to mountpoint  => '/mnt', and then I get:

    fusermount: failed to open /etc/fuse.conf: Permission denied fusermount: user has no write access to mountpoint /mnt

    I assume that you have permission access, just for future reference solution to people who do not have access. Follow the documentation Error Message “fusermount – failed to open /etc/fuse.conf – Permission denied” in Linux.

    Check also the my $priv_data = 'PRIVDAT'; it dose not seem correct. I will try to play tomorrow with mounting and unmounting a usb stick just for the fun of the module. It should be sufficient to test and experiment the code. Let me know if the solution worked for you.

    Update 4: forgot to mention, I also found Fuse::Simple which might give you all the features that you need and based on the documentation is much easier and simpler than Fuse.

    I hope this is a possible solution to your problem, but I am might be wrong. As I said I never worked with Fuse before.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Thank you for taking the time looking into this, despite not using Fuse. I wasn't really expecting someone to go that far to help me! Thank you :)

      The Fuse module itself is installed properly and the code works okay (as in, it mounts and you can watch filesystem events happen on the mountpoint due to the 'debug => 1') if you comment out the 'init' line in Fuse::main().

      I started by writing a little test as I wasn't sure if non-threaded Fuse would allow me to do this:

      $ cat mnt/source > mnt/destination

      where "mnt/" is the directory the Fuse filesystem is mounted, which I have in the scripts directory.

      This meant implementing getattr, getdir, read, write, truncate and they work without issue (and the above command does work okay). I then tried to add 'init' and this issue cropped up.

      I did see Fuse::Simple, but it doesn't allow some things that I want to do (renaming, deleting).

      I'm going to put the "fuller" code below for reference (I hope that is okay). Again, it works fine if 'init' is commented out in Fuse::main().

      No judging, please! This is just scratchcode to test if I could read/write at the same time :) Shortcuts were taken.

      NOTE: This code will sometimes fail to run by spamming this error:

      Use of each() on hash after insertion without resetting hash iterator results in undefined behavior, Perl interpreter: 0x14ae010 at /usr/lib/perl5/core_perl/Data/Dumper.pm line 222.

      This is part of this "init" bug! You either have to rerun it until it starts correctly, or comment out/replace line #49 with simpler print.

      #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use Fcntl ':mode'; use Fuse; use POSIX qw(ENOENT ENOSYS); Fuse::main ( debug => 1, mountpoint => 'mnt', getattr => sub { fuse('getattr', @_); }, getdir => sub { fuse('getdir', @_); }, init => sub { fuse('init', @_); }, read => sub { fuse('read', @_); }, truncate => sub { fuse('truncate', @_); }, write => sub { fuse('write', @_); }, ); sub fuse { my $function = shift; my $callback_for = { getattr => \&fuse_getattr, getdir => \&fuse_getdir, init => \&fuse_init, read => \&fuse_read, truncate => \&fuse_truncate, write => \&fuse_write, }; if (exists $callback_for->{$function}) { return $callback_for->{$function} -> (@_); } else { return -(ENOSYS); } die; } sub fuse_getattr { my $path = shift; my $context = Fuse::fuse_get_context(); print Dumper ($context), "\n"; my $stat = undef; if ($path eq '/') { my $mode = S_IFDIR | S_IRUSR | S_IXUSR; $stat = [ 0, # dev 0, # inode $mode, # mode 1, # nlink $context->{uid}, # uid $context->{gid}, # gid 0, # rdev 0, # size 0, # atime 0, # mtime 0, # ctime 4096, # blksie 0, # blocks ]; } elsif ($path eq '/save') { my $mode = S_IFREG | S_IRUSR; $stat = [ 0, # dev 0, # inode $mode, # mode 1, # nlink $context->{uid}, # uid $context->{gid}, # gid 0, # rdev 1_024_000, # size 0, # atime 0, # mtime 0, # ctime 4096, # blksie 1, # blocks ]; } elsif ($path eq '/load') { my $mode = S_IFREG | S_IWUSR; $stat = [ 0, # dev 0, # inode $mode, # mode 1, # nlink $context->{uid}, # uid $context->{gid}, # gid 0, # rdev 0, # size 0, # atime 0, # mtime 0, # ctime 4096, # blksie 0, # blocks ]; } if (defined $stat) { return @{$stat}; } else { return -(ENOENT); } die; } sub fuse_getdir { my $path = shift; if ($path eq '/') { return '.', '..', 'load', 'save', 0; } return -(ENOENT); } sub fuse_init { print Dumper [@_], "\n"; return 'PRIVDAT'; } sub fuse_read { my $path = shift; if ($path eq '/save') { return 1 x 4096; } return -(ENOENT); } sub fuse_truncate { my $path = shift; if ($path eq '/load') { return 0; } return -(ENOENT); } sub fuse_write { print Dumper (@_), "\n"; return length $_[1]; return -(ENOENT); }

      As you can see, the functions are kind-of-wrapped already..

        Hello CandyAngel,

        First of all sorry for the late reply I got busy with something else and I just found a few hours to spend with this script.

        I am afraid that I can not be much of assistance with your problem. No matter what I tried I was not able to make my sample script to work.

        I keep getting this error:

        unique: 71, opcode: RELEASEDIR (29), nodeid: 1, insize: 64, pid: 0 unique: 71, success, outsize: 16 unique: 72, opcode: LOOKUP (1), nodeid: 1, insize: 52, pid: 26177 LOOKUP /autorun.inf unique: 72, error: -38 (Function not implemented), outsize: 16

        Where it gets stack, for some unknown reason to me. I tried anything that could come to my mind but so far I did not find a solution to this problem.

        But I found some example codes, that might give a few ideas to experiment with or play around with your code. Take a look here dpavlin/perl-fuse if you have not done it yet.

        This is a sample of my code that I was trying to make it work, just in case that can help you by any means.

        #!/usr/bin/perl use Fuse; use strict; use warnings; use threads::shared; use Fuse "fuse_get_context"; use POSIX "ENOENT"; my (%files) = ( 'thanos' => { type => 0040, mode => 0755, ctime => time()-1000 } ); sub filename_fixup { my ($file) = shift; $file =~ s,^/,,; $file = '.' unless length($file); return $file; } sub e_getattr { my ($file) = filename_fixup(shift); $file =~ s,^/,,; $file = '.' unless length($file); return -ENOENT() unless exists($files{$file}); my ($size) = exists($files{$file}{cont}) ? length($files{$file}{co +nt}) : 0; $size = $files{$file}{size} if exists $files{$file}{size}; my ($modes) = ($files{$file}{type}<<9) + $files{$file}{mode}; my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = (0 +,0,0,1,0,0,1,1024); my ($atime, $ctime, $mtime); $atime = $ctime = $mtime = $files{$file}{ctime}; # 2 possible types of return values: #return -ENOENT(); # or any other error you care to #print(join(",",($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$at +ime,$mtime,$ctime,$blksize,$blocks)),"\n"); return ($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtim +e,$ctime,$blksize,$blocks); } sub e_getdir { # return as many text filenames as you like, followed by the retva +l. print((scalar keys %files)."\n"); return (keys %files),0; } my $mountpoint = ""; $mountpoint = $ARGV[1] or die "You forgot to provide the mountpoint!\n +"; #$mountpoint = shift(@ARGV) if @ARGV; croak("Fuse doesn't have ioctl") unless Fuse::fuse_version() >= 2.8; Fuse::main( debug => 1, mountpoint => $mountpoint, mountopts => "", # if 'user_allow_other' in /etc/fuse.conf as per + the FUSE documention threaded => 0, getattr => "main::e_getattr", #getdir => "main::e_getdir", #open => "main::e_open", #statfs => "main::e_statfs", #read => "main::e_read", );

        Just in case that you faced the same problem with me and you found a solution, let me know how you worked your way out of it. Any way, I hope by now you have solved your problems alternatively I am sure the examples will give you a few hints. Best of luck with your script.

        Seeking for Perl wisdom...on the process of learning...not there...yet!