Recent

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

ArminLinder

  • Sr. Member
  • ****
  • Posts: 314
  • Keep it simple.
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 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
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.
Specialize a type, not a var.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • 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.

ArminLinder

  • Sr. Member
  • ****
  • Posts: 314
  • Keep it simple.
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 3.3.2 on Windows 7,10,11, Debian 10.8 "Buster", macOS Catalina, macOS BigSur, VMWare Workstation 15, Raspberry Pi

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
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
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: 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

  • Hero Member
  • *****
  • Posts: 569
  • 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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • Former Delphi 1-7, 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.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
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
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

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Low level OS API in Linux?
« Reply #9 on: July 29, 2020, 08:44:58 am »
Runlevels are SysV thing. Linux uses them, but the *BSDs, for example, do not.

In the distant past that is true for both, in those last century times, having a SysV init system was a separate option in both BSDs and Linux(at least in e.g. Slackware)

However in the late nineties/early 2000s, iirc X started relying on sysv init, and most adopted at least some form of SysV init.

 

TinyPortal © 2005-2018