Recent

Author Topic: [SOLVED] Accessing Disks in Linux seems no longer possible with FileOpen  (Read 3048 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Hi guys

I have a program which, for some years, has been able to access disks in a Linux system (when run as root) using FileOpen which was written in FPC 3.0.4. I am still currently using FPC3.0.4 though I appreciate I really need to get round to updating to FPC 3.2.0.

Anyway, here is my FPC 3.0.4 example:

Code: Pascal  [Select][+][-]
  1. hSelectedDisk : THandle;
  2. ...
  3.     hSelectedDisk := FileOpen(SourceDevice, fmOpenRead OR fmShareDenyNone);
  4. if hSelectedDisk = -1 then
  5.     begin
  6.      ShowMessage('Could not get exclusive disk access ' +
  7.      'OS error and code : ' + SysErrorMessageUTF8(GetLastOSError));
  8.     end
  9.     else    
  10. ....
  11.  

However, users began to report access violation errors and having looked into it today, it seems FileOpen constantly returns -1 when trying to access /dev/sd devices now, even when run as root. I'm assuming this is something new to Linux where it is no longer possible to obtain a handle to physical disks using whatever powers FileOpen uses? 

Is there a new (new to me at least as I am clearly out of date) way to do this now? Have I missed some news somewhere (quite probably), or is it just no longer possible in FPC? Is FPC 3.2.0 the way to go to achieve this? 

Thanks
« Last Edit: August 28, 2020, 07:01:56 pm by Gizmo »

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #1 on: August 27, 2020, 05:44:57 pm »
Minimal standalone test program please. I've got a laptop next to me with a fairly old Debian on it so might be able to at least confirm your observation.

It would probably also be useful if you could get an idea of the kernel/distro version that the behaviour changed. As specific points, is it related to Linux starting to call all discs /dev/sd (rather than the older /dev/hd for IDE/ATA), or is it related to the introduction of support for SATA or something even newer?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 2435
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #2 on: August 27, 2020, 05:50:20 pm »
@Gizmo:
Small correction with regards to your error description: fmShareDenyNone does not mean exclusive access.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #3 on: August 27, 2020, 05:53:08 pm »
Hi guys

I’ve just looked at another program where I use this which I know does work and I noticed it just hash fnOpenRead without the share sent etc. So I’ll make a change and see if it is related to the share permissions system. Perhaps that has changed in more recent times and that kind of access is no longer possible. I’ll report back later...

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #4 on: August 27, 2020, 06:21:41 pm »
I'd not be at all surprised if it were something that the kernel has tightened up, I was looking at an ICMP library a few days ago which appears to not be available to root by default.

It might be worth your while investigating whether POSIX capabilities are relevant, but I /thought/ that by default all were enabled for the root user.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #5 on: August 27, 2020, 06:47:24 pm »
Hi!

Just tested as root:

No errors!

Suse Tumbleweed,kernel 5.8-0-1

Winni

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #6 on: August 27, 2020, 06:58:56 pm »
Raspbian also does not give errors. I thought about systemd but that does not seem to matter.
Specialize a type, not a var.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #7 on: August 27, 2020, 08:16:30 pm »
Mmm. Must be something to do with my program then. I am using Linux Mint 19.3 .

I tried it without the other share permission and still got same problem yet my other program seems to work. How very bizarre. I wouldn’t mind but nothing has changed  in code for about 4 years So I am confused as to why it now won’t play nicely.

Anyway at least you guys have confirmed the method is right and it hasn’t become an obselete method so thanks for that

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #8 on: August 28, 2020, 01:17:20 pm »
OK, so I just created a bare bones basic new project from scratch, compiled, ran as root, and it does not work for me on Linux Mint 19.3, kernel 5.3.0-62-generic with Lazarus 2.0.4 and FPC 3.0.4. I attach it for reference in the hope someone else with a GUI\Debian based distro could try it for me? I am yet to upgrade my FPC\Lazarus etc but if anyone can try with the latest to see if its different, I'd appreciate it. 

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   SourceDevice : widestring;
  4.   hSelectedDisk : THandle;
  5. begin
  6.   hSelectedDisk := -1;
  7.   SourceDevice := '/dev/sde';   // Adjust accordingly! sde is a USB drive for me.
  8.   hSelectedDisk := FileOpen(SourceDevice, fmOpenRead);
  9.   if hSelectedDisk > -1 then Showmessage('Success') else ShowMessage('Failed');
  10. end;
  11.  
« Last Edit: August 28, 2020, 01:21:23 pm by Gizmo »

bytebites

  • Hero Member
  • *****
  • Posts: 632
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #9 on: August 28, 2020, 01:27:37 pm »
widestring really?

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #10 on: August 28, 2020, 01:47:13 pm »
It’s the same with string or unicodestring. (It’s widestring because in the full tool that var is used with windows functions too though I should probably switch it to unicodestring)
« Last Edit: August 28, 2020, 03:03:42 pm by Gizmo »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #11 on: August 28, 2020, 02:11:13 pm »
Hi!

Does /dev/sde exist?
Code: Pascal  [Select][+][-]
  1. ....  
  2. SourceDevice := '/dev/sde';
  3. if FileExists (SourceDevice) then showMessage ('OK') else
  4.   begin
  5.     showMessage (SourceDevice +' not found!'); exit;
  6.   end;
  7. ....

And what is the rights situation of /dev/sde ??
It should be
Code: Bash  [Select][+][-]
  1. brw-rw---- 1 root disk 8, 32 Aug 28 11:58 sde

Winni

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #12 on: August 28, 2020, 03:01:31 pm »
Winni...ah, yes, schoolboy error by me. Tried with a different USB and the demo project DOES work! So that is good news. So, back to the drawing board of why my main program refuses to play ball even with a valid USB dev/sde.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #13 on: August 28, 2020, 03:16:24 pm »
Hi!

I know that situation:
3 sticks and one harddisk in the USB hub.
And no one knows what is connected to which device file ...
In those situations you got a friend:

Code: Bash  [Select][+][-]
  1. df -h

Winni

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Accessing Disks in Linux seems no longer possible with FileOpen
« Reply #14 on: August 28, 2020, 07:01:45 pm »
Admins...you can delete this if you like. The problem was 100% unrelated to the post title. I hadn't noticed but it was yet another form that I'd not noticed was not being created when it needed to be, so the output that the program was trying to show was intended for a form that was not available, thus the "Access Denied" message. As soon as I spotted that (after placing ShowMessage all over the place so I could run the compiled binary as sudo outside of Lazarus because I couldn't run the program as sudo within Lazarus with the debugger and that was obviously what was cuasing -1 by FileOpen)

 

TinyPortal © 2005-2018