GNU/Linux >> Znalost Linux >  >> Linux

Zavolejte systémové volání Linuxu ze skriptovacího jazyka

Perl to umožňuje svým syscall funkce:

$ perldoc -f syscall
    syscall NUMBER, LIST
            Calls the system call specified as the first element of the list,
            passing the remaining elements as arguments to the system call. If
⋮

Dokumentace také uvádí příklad volání write(2):

require 'syscall.ph';        # may need to run h2ph
my $s = "hi there\n";
syscall(SYS_write(), fileno(STDOUT), $s, length $s);

Nemůžu říct, že jsem někdy tuto funkci ale využil. No, právě teď pro potvrzení příklad skutečně funguje.

Zdá se, že to funguje s getrandom :

$ perl -E 'require "syscall.ph"; $v = " "x8; syscall(SYS_getrandom(), $v, length $v, 0); print $v' | xxd
00000000: 5790 8a6d 714f 8dbe                      W..mqO..

A pokud nemáte getrandom ve svém syscall.ph, můžete místo toho použít číslo. Je to 318 na mém boxu testování Debianu (amd64). Pozor, čísla systémových volání Linuxu jsou specifická pro architekturu.


V Pythonu můžete použít ctypes modul pro přístup k libovolným funkcím v dynamických knihovnách, včetně syscall() z libc:

import ctypes

SYS_getrandom = 318 # You need to check the syscall number for your target architecture

libc = ctypes.CDLL(None)
_getrandom_syscall = libc.syscall
_getrandom_syscall.restypes = ctypes.c_int
_getrandom_syscall.argtypes = ctypes.c_int, ctypes.POINTER(ctypes.c_char), ctypes.c_size_t, ctypes.c_uint

def getrandom(size, flags=0):
    buf = (ctypes.c_char * size)()
    result = _getrandom_syscall(SYS_getrandom, buf, size, flags)
    if result < 0:
        raise OSError(ctypes.get_errno(), 'getrandom() failed')
    return bytes(buf)

Pokud vaše knihovna libc obsahuje getrandom() wrapper funkce, můžete ji také volat:

import ctypes

libc = ctypes.CDLL(None)
_getrandom = libc.getrandom
_getrandom.restypes = ctypes.c_int
_getrandom.argtypes = ctypes.POINTER(ctypes.c_char), ctypes.c_size_t, ctypes.c_uint

def getrandom(size, flags=0):
    buf = (ctypes.c_char * size)()
    result = _getrandom(buf, size, flags)
    if result < 0:
        raise OSError(ctypes.get_errno(), 'getrandom() failed')
    return bytes(buf)

Ruby má syscall(num [, args...]) → integer funkce.

Například:

irb(main):010:0> syscall 1, 1, "hello\n", 6
hello
=> 6

S getrandom() :

irb(main):001:0> a = "aaaaaaaa"
=> "aaaaaaaa"
irb(main):002:0> syscall 318,a,8,0
=> 8
irb(main):003:0> a
=> "\x9Cq\xBE\xD6|\x87\u0016\xC6"
irb(main):004:0> 

Linux
  1. Nainstalujte Apache 2 ze zdroje na Linux

  2. Nejlepší postupy kódování pro programování systému Linux v jazyce C – část 1

  3. linuxový obrázek ze schránky

  1. x86_64 Assembly Linux System Call Zmatek

  2. Volání funkce uživatelského prostoru z modulu jádra Linuxu

  3. Odstranění .rbenv z Linuxu

  1. Použití Windows DLL z Linuxu

  2. Jak zavolat Wine dll z pythonu na Linuxu?

  3. Spuštění skriptu z stdin (Linux/Shell Scripting)