Low level OS API in Linux? lazarus / 32-Linux ---------------------------------------------------------------------- ArminLinder · 2020-02-17 12:13 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 ---------------------------------------------------------------------- Thaddy · 2020-02-17 15:34 first unix shutdown: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}uses unix;begin  fpsystem('shutdown -P now');end. ---------------------------------------------------------------------- marcov · 2020-02-17 15:48 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 · 2020-02-17 18:17 Quote from: Thaddy on February 17, 2020, 04:34:16 pm first unix shutdown: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}uses unix;begin  fpsystem('shutdown -P now');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. ---------------------------------------------------------------------- MarkMLl · 2020-02-17 18:42 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 ---------------------------------------------------------------------- winni · 2020-02-17 19:44 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 · 2020-05-20 12:32 As far as I understand the notion of “runlevels” is specific to various init(1) implementations. It isn’t a “Linux thing”. Quote from: Nimral on February 17, 2020, 01:13:28 pmComing 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program shutdown0(input, output, stdErr);{$scopedEnums+}uses        sysUtils, errors, baseUnix, sysCall;type        reboot = (powerOff = $4321fedc);begin        if do_sysCall(sysCall_nr_reboot, ord(reboot.powerOff)) = -1 then        begin                pError(applicationName(), fpGetErrNo());                inc(exitCode);        end;end. Quote from: Nimral on February 17, 2020, 01:13:28 pm- 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program volumeSerialNumberOfDrive(input, output, stdErr);{$mode objFPC}uses      classes, sysUtils, errors, baseUnix;var        deviceInformation: stat;        udevInformation: tStringList;begin        if fpStat('/dev/sda', deviceInformation) <> 0 then        begin              pError(applicationName(), fpGetErrNo());                inc(exitCode);                halt;        end;                udevInformation := tStringList.create();        udevInformation.loadFromFile('/run/udev/data/b' +                intToStr(byte(deviceInformation.st_rdev shr 8)) + ':' +              intToStr(byte(deviceInformation.st_rdev)));        writeLn(udevInformation.values['E:ID_SERIAL_SHORT']);        udevInformation.destroy;end. ---------------------------------------------------------------------- trev · 2020-05-20 13:04 Runlevels are SysV thing. Linux uses them, but the *BSDs, for example, do not. ---------------------------------------------------------------------- MarkMLl · 2020-05-20 13:09 Quote from: Kays on May 20, 2020, 02:32:45 pmAs 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 ---------------------------------------------------------------------- marcov · 2020-07-29 06:44 Quote from: trev on May 20, 2020, 03:04:17 pmRunlevels 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.