linux provides Linux-oriented native bindings using Magnolia's sys wrapper layer.
It supports:
- resolving symbols from
libc/libdl - resolving and calling symbols from
libX11 - process and runtime helpers (
getpid,getppid,sysconf) - errno and strerror helpers
- virtual memory wrappers (
mmap,munmap,mprotect) - convenience page allocation wrappers
- dynamic loader wrappers (
dlopen,dlsym,dlclose) - X11 window creation, event loops, and basic painting
All public calls are OS-gated and return structured error objects on non-Linux hosts.
Both names are supported:
linux := import('linux')
// or
linux := import('Linux')
The Linux interop surface is split into focused modules and re-exported by
linux/Linux.
- linux-constants: constants and shared-library candidate lists
- linux-core: OS guard, C-string helpers, symbol resolution/call dispatch
- linux-loader:
dlopen/dlsymhelpers and cached call-by-symbol - linux-windowing: X11 display/window/event/drawing helpers
- linux-libc: process, errno, file descriptor, and virtual-memory wrappers
You can import these modules directly when you only need part of the Linux API:
constants := import('linux-constants')
core := import('linux-core')
loader := import('linux-loader')
x11 := import('linux-windowing')
libc := import('linux-libc')
LibCLibDLLibX11
PROT_NONEPROT_READPROT_WRITEPROT_EXEC
MAP_SHAREDMAP_PRIVATEMAP_FIXEDMAP_ANONYMOUS
O_RDONLYO_WRONLYO_RDWRO_CREATO_TRUNCO_APPEND
SEEK_SETSEEK_CURSEEK_END
F_OKR_OKW_OKX_OK
RTLD_LAZYRTLD_NOWRTLD_GLOBALRTLD_LOCAL
KeyPressMaskButtonPressMaskExposureMaskStructureNotifyMaskKeyPressExposeDestroyNotifyClientMessage
Returns true when the host OS is Linux.
Appends a null terminator to s for C-style API calls.
Little-endian typed integer helpers built on top of memread/memwrite.
Resolves symbol from the first available libc candidate.
Resolves symbol from an explicit library.
Calls a resolved proc or raw address via sys.call.
Resolve + call convenience wrappers for libc/libdl candidate sets.
Loads and memoizes a shared library handle using dlopen.
librarycan be a single string path/name, or a candidate list.- returns
{type: :ok, handle: <int>, library: <string>}on success.
Loads a shared library (if needed) and resolves symbol via dlsym.
Load + resolve + call helper for Linux shared libraries.
Calls symbol from the first available libX11 candidate.
Calls getpid (-1 on non-Linux).
Compatibility alias for currentProcessId().
Compatibility helper for Windows-style module-handle lookup:
- pass
?to get the current process image handle viadlopen(NULL, ...) - pass a shared library name/path to load and return a handle
Compatibility alias for moduleHandle(?) value extraction.
Calls getppid (-1 on non-Linux).
Calls sysconf(_SC_PAGESIZE) (-1 on non-Linux).
Returns the current thread-local errno value (-1 on non-Linux or lookup failure).
Returns a best-effort error string for a numeric errno code (or ? on failure).
Compatibility alias for errno().
Compatibility alias for strerror(errorCode).
Convenience helper for strerror(errno()).
Returns real/effective UID and GID via libc wrappers.
Calls gethostname(2) with caller-managed memory.
Calls getcwd(3) with caller-managed memory.
Changes process working directory.
Calls access(2); use F_OK, R_OK, W_OK, X_OK.
Thin wrappers over POSIX fd APIs (open, close, read, write, lseek,
unlink).
openFile(..., mode?)defaults mode to decimal420(0644) when omitted.closeHandle(handle)is a compatibility alias forcloseFile(handle).
Thin wrappers over libc memory APIs.
Convenience wrapper over mmap with anonymous private mapping.
- defaults
prottoPROT_READ | PROT_WRITEwhen omitted - uses
MAP_PRIVATE | MAP_ANONYMOUS
Convenience wrapper over munmap.
Convenience wrapper over mprotect.
Compatibility aliases for Windows-style virtual-memory APIs:
virtualAlloc(...)maps toallocPages(size, protection)virtualFree(...)maps tofreePages(address, size)virtualProtect(...)maps toprotectPages(address, size, newProtect)virtualAllocEx(...),virtualFreeEx(...),virtualQuery(...),virtualQueryEx(...),openProcess(...),readProcessMemory(...), andwriteProcessMemory(...)are exported compatibility stubs and currently return{type: :error, ...}on Linux.- Stub errors include
api,platform, andsupportedfields for consistent cross-platform capability checks.
Thin wrappers over dynamic loader APIs.
Compatibility aliases for Windows-style loader naming:
loadLibrary(path)callsdlopen(path, RTLD_NOW | RTLD_LOCAL)procAddress(module, symbol)callsdlsym(module, symbol)freeLibrary(handle)callsdlclose(handle)
runWindowLoop(...) exits with 0 on ClientMessage or DestroyNotify.
Creates a simple top-level X11 window and returns:
{
type: :ok
display: <int>
window: <int>
screen: <int>
black: <int>
white: <int>
}
Destroys/tears down a window created by openDefaultWindow(...).
linux := import('linux')
if linux.isLinux?() {
true -> {
pid := linux.currentProcessId()
pagesize := linux.pageSize()
println('PID: ' + string(pid))
println('Page size: ' + string(pagesize))
println('ENOENT text: ' + string(linux.strerror(2)))
}
_ -> println('linux library is inactive on this host')
}
See samples/linux-window.oak for a runnable X11 window sample and samples/linux-draw.oak for basic drawing.
linux := import('Linux')
if linux.isLinux?() {
true -> {
win := linux.openDefaultWindow('Magnolia Linux Window', 800, 480)
if win.type = :ok {
true -> {
eventBuf := linux.createXEventBuffer()
linux.runWindowLoop(win.display, addr(eventBuf))
linux.closeWindow(win)
}
}
}
}
- Interop calls are unsafe by nature; validate pointers and lengths.
- Symbol availability varies by distro and runtime environment.
- Keep wrappers narrow and validate arguments near call boundaries.
- X11 calls require a running X server and a valid
DISPLAY.