Recent

Author Topic: Low level OS API in Linux?  (Read 738 times)

Nimral

  • Full Member
  • ***
  • Posts: 107
Low level OS API in Linux?
« on: February 17, 2020, 01:13:28 pm »
Coming from Windows, I wonder what API I can use in Linux to get at low-level OS functions. Specifically I am after the following functionalities:

- shutdown the machine (shutdown -now equivalent)
- get the volume serial number of a drive (udevadm info --query=all --name=/dev/sda | grep ID_SERIAL equivalent)

currently I am using redirected shell commands, I am looking for a direct API or a management interface like WMI.

Thanx, Armin
Lazarus 1.8 on Windows 10/7, VMWare Workstation 12

Thaddy

  • Hero Member
  • *****
  • Posts: 10100
Re: Low level OS API in Linux?
« Reply #1 on: February 17, 2020, 04:34:16 pm »
 first unix shutdown:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses unix;
  3. begin
  4.   fpsystem('shutdown -P now');
  5. end.
I am more like donkey than shrek

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8373
  • FPC developer.
Re: Low level OS API in Linux?
« Reply #2 on: February 17, 2020, 04:48:36 pm »
Linux is only a kernel, with say 400 syscalls, and some info and tricks with /proc.

For the rest there are some libraries, but not even close to the winapi with its ten thousands + calls.

Some higher level desktop libraries communicate via DBus, that can be something to look into too.

Nimral

  • Full Member
  • ***
  • Posts: 107
Re: Low level OS API in Linux?
« Reply #3 on: February 17, 2020, 07:17:40 pm »
first unix shutdown:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses unix;
  3. begin
  4.   fpsystem('shutdown -P now');
  5. end.

Thanks, Thaddy, that's what I have now. I was after a binary API like in Windows instead of shell program calls. I'll try to see where following marcov's lead will bring me.

Armin.
« Last Edit: February 17, 2020, 07:26:07 pm by Nimral »
Lazarus 1.8 on Windows 10/7, VMWare Workstation 12

MarkMLl

  • Hero Member
  • *****
  • Posts: 858
Re: Low level OS API in Linux?
« Reply #4 on: February 17, 2020, 07:42:23 pm »
I think that setting the system to runlevel 0 should do it, but I've not investigated in depth whether that's done by a single kernel call, by sending some sort of message to the init process, or womthing else entirely. However expect the physical act of powering off to be system-specific and very much dependant on having the right hardware support in the kernel, I think it's related to ACPI on PCs.

Be warned that I've screwed an SDCard by powering off a Raspberry Pi abruptly (possibly teleinit 0, I forget). And I mean screwed: beyond all salvage even using engineering commands issued by an Arduino.

Link below looks like a fairly useful summary.

https://superuser.com/questions/108704/how-to-shutdown-a-computer-instantly-1-to-5-secs-without-using-a-physical-swit/108705

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

winni

  • Hero Member
  • *****
  • Posts: 1341
Re: Low level OS API in Linux?
« Reply #5 on: February 17, 2020, 08:44:18 pm »
Hi!

The setting of the runlevels and the initialization of the related units is the job of the init process.

So shutdown -h now does the same as init 0

And don't be astonished about runlevel 6: this means reboot

Winni

Kays

  • Full Member
  • ***
  • Posts: 188
  • Whasup!?
    • KaiBurghardt.de
Re: Low level OS API in Linux?
« Reply #6 on: May 20, 2020, 02:32:45 pm »
As far as I understand the notion of “runlevels” is specific to various init(1) implementations. It isn’t a “Linux thing”.

Coming from Windows, I wonder what API I can use in Linux to get at low-level OS functions. Specifically I am after the following functionalities:

- shutdown the machine (shutdown -now equivalent)
The API used, marcov already mentioned it, are system calls. It is really low level.
Code: Pascal  [Select][+][-]
  1. program shutdown0(input, output, stdErr);
  2. {$scopedEnums+}
  3. uses
  4.         sysUtils, errors, baseUnix, sysCall;
  5. type
  6.         reboot = (powerOff = $4321fedc);
  7. begin
  8.         if do_sysCall(sysCall_nr_reboot, ord(reboot.powerOff)) = -1 then
  9.         begin
  10.                 pError(applicationName(), fpGetErrNo());
  11.                 inc(exitCode);
  12.         end;
  13. end.

- get the volume serial number of a drive (udevadm info --query=all --name=/dev/sda | grep ID_SERIAL equivalent) […]
There is a libudev. It apparently became part of the systemd operating system though.

Doing an strace(1) on udevadm(8) you’ll see that it eventually just reads a file under /run/udev/data/.
Code: Pascal  [Select][+][-]
  1. program volumeSerialNumberOfDrive(input, output, stdErr);
  2. {$mode objFPC}
  3. uses
  4.         classes, sysUtils, errors, baseUnix;
  5. var
  6.         deviceInformation: stat;
  7.         udevInformation: tStringList;
  8. begin
  9.         if fpStat('/dev/sda', deviceInformation) <> 0 then
  10.         begin
  11.                 pError(applicationName(), fpGetErrNo());
  12.                 inc(exitCode);
  13.                 halt;
  14.         end;
  15.        
  16.         udevInformation := tStringList.create();
  17.         udevInformation.loadFromFile('/run/udev/data/b' +
  18.                 intToStr(byte(deviceInformation.st_rdev shr 8)) + ':' +
  19.                 intToStr(byte(deviceInformation.st_rdev)));
  20.         writeLn(udevInformation.values['E:ID_SERIAL_SHORT']);
  21.         udevInformation.destroy;
  22. end.
Yours Sincerely
Kai Burghardt

trev

  • Hero Member
  • *****
  • Posts: 654
  • Former Delphi 1-7 and 10.2 User
Re: Low level OS API in Linux?
« Reply #7 on: May 20, 2020, 03:04:17 pm »
Runlevels are SysV thing. Linux uses them, but the *BSDs, for example, do not.
o Lazarus v2.1.0 r63233, FPC v3.3.1 r44876, macOS 10.14.6 (with sup update), Xcode 11.3.1
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.1 (Parallels VM)
o FPC 3.0.4, FreeBSD 12-STABLE r358002
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)

MarkMLl

  • Hero Member
  • *****
  • Posts: 858
Re: Low level OS API in Linux?
« Reply #8 on: May 20, 2020, 03:09:05 pm »
As far as I understand the notion of “runlevels” is specific to various init(1) implementations. It isn’t a “Linux thing”.

More specifically, it's distro-specific even in the scope of the extended family of Linux (plus supporting utilities etc.) OSes.

I used to use the various runlevels heavily on Slackware, but after I moved to Debian they were far less useful.

Noting Winni's comment

> So shutdown -h now does the same as init 0

I normally use poweroff. I'm pretty sure that using  init 0  on a Raspberry Pi in an unguarded moment broke an SDCard beyond the capability of even low-level recovery: you might not believe me and it might need to hit the hardware at /just/ the right time but please be aware of it as a possibility.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

 

TinyPortal © 2005-2018