Module lunatik

Core Lunatik module.

Provides functionalities to create and manage Lunatik runtimes, which are isolated Lua environments running within the Linux kernel. This module exposes the lunatik global table in Lua, which can be used to create new runtimes. Runtime objects themselves have methods to control their execution (e.g., resume, stop).

If a global Lunatik environment (lunatik_env) is configured at the C level, it can be exposed to Lua scripts as lunatik._ENV. This allows for a shared environment across multiple scripts or runtimes.

lunatik

_ENV Shared environment
runtime (script[, sleep=true]) Creates and starts a new Lunatik runtime environment.

Class runtime

runtime:resume (...) Resumes a Lunatik runtime that has yielded.
runtime:stop () Stops and releases a Lunatik runtime environment.


lunatik

_ENV
Shared environment
  • _ENV points to a shared global Lunatik runtime object. Scripts can share RCU tables or other Lunatik objects through this mechanism.
runtime (script[, sleep=true])
Creates and starts a new Lunatik runtime environment. A Lunatik runtime is an isolated Lua state that can execute Lua scripts within the kernel. Each runtime operates independently.

The userdata object returned by lunatik.runtime() encapsulates an isolated Lua state running within the Linux kernel. Each runtime can be configured as sleepable (allowing blocking operations, using GFP_KERNEL for allocations and mutexes for synchronization) or non-sleepable/atomic (prohibiting blocking operations, using GFP_ATOMIC for allocations and spinlocks for synchronization).

Parameters:

  • script string The name of the Lua script to load and execute (e.g., "myscript"). The system will look for "myscript.lua" in the Lua root path.
  • sleep boolean If true (default), the runtime can sleep (e.g., for I/O operations) and uses GFP_KERNEL for allocations. If false, the runtime operates in an atomic context, cannot sleep, and uses GFP_ATOMIC for allocations. This is crucial for runtimes used in contexts that cannot sleep, like Netfilter hooks. (default true)

Returns:

    runtime A Lunatik runtime object. This object can be used to interact with the runtime, for example, to resume it if it yields or to stop it.

Raises:

Error if the Lua state or runtime cannot be allocated, or if the script fails to load or execute.

Class runtime

Represents a Lunatik runtime environment. This is a userdata object returned by lunatik.runtime(). It encapsulates an isolated Lua state running within the Linux kernel. Each runtime can be configured as sleepable (allowing blocking operations, using GFP_KERNEL for allocations and mutexes for synchronization) or non-sleepable/atomic (prohibiting blocking operations, using GFP_ATOMIC for allocations and spinlocks for synchronization).
runtime:resume (...)
Resumes a Lunatik runtime that has yielded. This function is used to continue the execution of a Lua script within a Lunatik runtime from the point where it last yielded. It's analogous to Lua's coroutine.resume. The resume method is called on a Lunatik runtime object.

Parameters:

  • ... Arguments to pass to the Lua script upon resumption. These arguments will be received by the coroutine.yield() call that suspended the script.

Returns:

    vararg Values returned by the coroutine.yield() call if the script yielded again, or values returned by the script if it completed.

Raises:

Error if the runtime cannot be resumed or if an error occurs within the resumed script. The error message will be propagated from the Lua C API error.

Usage:

    -- Assuming 'rt' is a Lunatik runtime object that has yielded.
    local success, res_or_err = pcall(function() return rt:resume("arg_to_script") end)
runtime:stop ()
Stops and releases a Lunatik runtime environment. This method is called on a Lunatik runtime object. Once stopped, the runtime cannot be resumed or used further. It ensures that the associated Lua state is closed and all kernel resources are freed.

This method provides an explicit way to release the runtime.

Returns:

    nil Does not return any value to Lua.

Raises:

Error May raise an error if the underlying C function encounters a critical issue during cleanup and calls lua_error.

Usage:

    -- Assuming 'rt' is a Lunatik runtime object:
    rt:stop()
generated by LDoc 1.5.0 Last updated 2025-06-27 17:53:55