Making syscall
Objective: Invoke getpid
syscall. Print returned PID.
Python
There is no dedicated binding for syscall
in standard library. However syscall
from libc
can be utilized:
# inspiredy by: https://stackoverflow.com/a/37032683
import ctypes
from enum import IntEnum
class Syscall(IntEnum):
GETPID = 39
libc = ctypes.CDLL(None)
# or alternatively:
#libc = ctypes.cdll.LoadLibrary("libc.so.6")
syscall = libc.syscall
pid = syscall(Syscall.GETPID)
print(pid)
Rust
Example below uses syscalls crate.
use syscalls::{Sysno, syscall}; fn main() { match unsafe { syscall!(Sysno::getpid) } { Ok(pid) => { println!("{}", pid); } Err(err) => { eprintln!("getpid() failed: {}", err); } } }
Crystal
Dedicated module Syscall is available. Bindings to individual syscalls must be created manually.
require "syscall"
module OurSysCalls
Syscall.def_syscall getpid, Int32
end
pid = OurSysCalls.getpid
puts(pid)