NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.08k stars 14.08k forks source link

Brscan4 scanimage segfault #84819

Closed NorfairKing closed 4 years ago

NorfairKing commented 4 years ago

After enabling brscan4 as described here: https://nixos.wiki/wiki/Scanners#Brother scanimage -L produces this segfault:

scanimage: error while loading libusb-1.0.so.0 from libusb-0.1.so.4: libusb-1.0.so.0: cannot open shared object file: No such file or directory
[1]    18263 segmentation fault (core dumped)  scanimage -L

@jraygauthier do you have any idea why?

jraygauthier commented 4 years ago

Is it possible you're attempting to use a usb connected scanner? If so, it might be that it attempts to dynamically load the libusb so. This is not something I tested when I added support for brscan4 as my scanner is connected via ethernet. This might not be be too hard to support. One first step would be to run scanimage with LD_LIBRARY_PATH=/nix/store/path/to/libusb2/lib:$LD_LIBRARY_PATH scanimage -L and see if it helps.

NorfairKing commented 4 years ago

Is it possible you're attempting to use a usb connected scanner?

I'm not, but I do have a webcam connected via usb... I only want my scanner to work via my local network because it's only connected via wifi.

One first step would be to run scanimage with LD_LIBRARY_PATH=/nix/store/path/to/libusb2/lib:$LD_LIBRARY_PATH scanimage -L and see if it helps.

I'll try that real quick.

NorfairKing commented 4 years ago

This seems to work when using brscan4:

LD_LIBRARY_PATH="/nix/store/qrjn0dxx0qls6ddzdgw5hgd4d4wjj2kb-libusb-1.0.23/lib:$LD_LIBRARY_PATH" scanimage -L
device `brother4:net1;dev0' is a Brother *tijl MFC-6490CW
device `v4l:/dev/video0' is a Noname Integrated Camera: Integrated C virtual device

But not when using brscan3 (which I'm ~writing~ packaging atm because brscan4 doesn't support my scanner model, see https://github.com/NixOS/nixpkgs/issues/84802):

 LD_LIBRARY_PATH="/nix/store/qrjn0dxx0qls6ddzdgw5hgd4d4wjj2kb-libusb-1.0.23/lib:$LD_LIBRARY_PATH" scanimage -L
bugchk_free(ptr=(nil))@brother_modelinf.c(473)
[1]    23948 abort (core dumped)  LD_LIBRARY_PATH= scanimage -L

That means that I can go on from here, but brscan4 is still broken for me too.

jraygauthier commented 4 years ago

Excellent, I should try to look into this for brscan4 issue when I get some free time, I am still using a 19.09 system here, so someone might have updated the package, or one of its dependencies.

As to the remaining segfault you observe with brscan3, here are some things you might want to check for each binaries:

Otherwise, try to run your scanimage command under strace might help you better understand what is occurring (immediatly before the segfault occurs).

If i remember well, I also was forced to build some c wrapper to redirect system calls for binaries that could not get patched. However details are not fresh as this was a long time ago.

NorfairKing commented 4 years ago

Thanks for the guidance! I will try again tonight while my memory of this problem is still fresh. I will put up my attempt as well, because there are some differences between brscan3 and brscan4. For example, brscan3 looks in /usr/local/Brother/sane instead of /etc/opt/brother/scanner/brscan4.

jraygauthier commented 4 years ago

No trouble at all, this is nice you're attempting to add support for brscan3 to nixos.

NorfairKing commented 4 years ago

@jraygauthier To be clear: I'll need to know how you fixed the segfault to make the same fix in brscan3 so don't hesitate to tell me when yo u figure it out.

NorfairKing commented 4 years ago

@jraygauthier I decided to fix the problem with brscan4 first. Here's my solution:

      nixpkgs.config.packageOverrides = pkgs: with pkgs; {
        sane-backends = sane-backends.overrideAttrs (oldAttrs: {
            nativeBuildInputs = oldAttrs.nativeBuildInputs or [] ++ [ makeWrapper ];
            postInstall = ''
              ${oldAttrs.postInstall or ""}

              mv $out/bin/scanimage $out/bin/scanimage.old
              makeWrapper $out/bin/scanimage.old $out/bin/scanimage \
                --prefix LD_LIBRARY_PATH ":" ${libusb1}/lib
            '';
          }
        );
      };
NorfairKing commented 4 years ago

It turns out that this stuff "just works" with brscan4 now. I have no clue why or how, because it's supposedly not the right brscan version. Now I'm praying to the nix gods that it keeps working. We'll see. If anyone is looking for the brscan3 packaging and finds this issue, come talk to me.

jraygauthier commented 4 years ago

Alright that's nice to hear. I'm still curious about this libusb issue you have with brscan4 as I am unable to reproduce.

My config is:

  hardware.sane = {
    enable = true;
    brscan4.enable = true;

    brscan4.netDevices = {
      livingRoom = { model = "MFC-7860DW"; nodename = "BRW0080927AFBCE"; };
    };
  };

  networking.extraHosts = ''
    192.168.1.2 BRW0080927AFBCE
  '';

On my 19.09 setup:

$ printenv | grep LD_L
LD_LIBRARY_PATH=/nix/store/mz7caxfch1k5g0wxmrnl96shcyk0cmm1-sane-config/lib/sane:/run/opengl-driver/lib

$ scanimage -L
device `brother4:net1;dev0' is a Brother livingRoom MFC-7860DW
device `v4l:/dev/video0' is a Noname UVC Camera (046d:081b) virtual device

Is it possible when you perform the scanimage the brscan3 is still part of the environment? See the above LD_LIBRARY_PATH which is introduced by the hardware.sane nixos module. A change in config might require at least a fresh terminal / potentially a session refresh.

You might verify that fromthe terminal itself:

$ printenv LD_LIBRARY_PATH | awk -F: '{ print $1 }' | xargs ls -la | grep libsane-br
lrwxrwxrwx 1 root root    88 Dec 31  1969 libsane-brother4.so -> /nix/store/zbfkb1ijqyhlx97j4jg9435fdn969w4l-brscan4-0.4.8-1/lib/sane/libsane-brother4.so
lrwxrwxrwx 1 root root    90 Dec 31  1969 libsane-brother4.so.1 -> /nix/store/zbfkb1ijqyhlx97j4jg9435fdn969w4l-brscan4-0.4.8-1/lib/sane/libsane-brother4.so.1
lrwxrwxrwx 1 root root    94 Dec 31  1969 libsane-brother4.so.1.0.7 -> /nix/store/zbfkb1ijqyhlx97j4jg9435fdn969w4l-brscan4-0.4.8-1/lib/sane/libsane-brother4.so.1.0.7
NorfairKing commented 4 years ago

Is it possible when you perform the scanimage the brscan3 is still part of the environment?

I don't think so because I removed everything related to brscan3 (I don't need it anymore). And when I removed the 'fix', I get the same segfault again. I made sure to re-log-in every time I made a change.

Could it be that I have something plugged in via usb that matters?

In any case I'll try again without the fix and see if it breaks again.

NorfairKing commented 4 years ago

@jraygauthier I can't reproduce this problem anymore either, so I will close this issue.

I guess I wasn't as careful as I thought I had been :) Thanks for trying to reproduce this.

NorfairKing commented 4 years ago

@jraygauthier I saw this segfault again recently but now I can't reproduce it anymore again. It's a bit flaky it seems.

EDIT: oh that's probably because I hadn't re-logged.