Module linux
Various Linux kernel facilities.
This library includes functions for random number generation, task scheduling, time retrieval, kernel symbol lookup, network interface information, access to kernel constants like file modes, task states, and error numbers.
Functions
| difftime (t2, t1) | Calculates the difference between two timestamps. |
| errname (err) | Returns the symbolic name of a kernel error number. |
| ifaddr (The) | Gets the HW address for the network interface index. |
| ifindex (interface_name) | Gets the interface index for a network device name. |
| lookup (symbol_name) | Looks up a kernel symbol by name. |
| random ([m[, n]]) | Generates pseudo-random integers. |
| schedule ([timeout[, state]]) | Puts the current task to sleep. |
| time () | Gets the current real time. |
| tracing ([enable]) | Controls kernel tracing. |
Tables
| task | Table of task state constants. |
Functions
- difftime (t2, t1)
-
Calculates the difference between two timestamps.
Parameters:
- t2
integer
The later timestamp (e.g., from
linux.time()). - t1
integer
The earlier timestamp (e.g., from
linux.time()).
Returns:
-
integer
The difference
t2 - t1in nanoseconds. - t2
integer
The later timestamp (e.g., from
- errname (err)
-
Returns the symbolic name of a kernel error number.
For example, it converts
2to"ENOENT".Parameters:
- err integer The error number (e.g., 2).
Returns:
-
string
The symbolic name of the error (e.g., "ENOENT").
Returns "unknown" (or the error number as a string) if the name cannot be resolved.
Usage:
local name = linux.errname(2) print("Error name:", name) -- "ENOENT"
- ifaddr (The)
-
Gets the HW address for the network interface index.
Parameters:
- The integer interface index number.
Returns:
-
string
The interface HW address.
Raises:
Error if the device is not found.Usage:
local addr = linux.ifaddr(index) print(string.byte(addr,1,6))
- ifindex (interface_name)
-
Gets the interface index for a network device name.
Parameters:
- interface_name string The name of the network interface (e.g., "eth0").
Returns:
-
integer
The interface index.
Raises:
Error if the device is not found.Usage:
local index = linux.ifindex("lo") print("Index of lo:", index)
- lookup (symbol_name)
-
Looks up a kernel symbol by name.
Uses
kallsyms_lookup_name(potentially via kprobes) to find the address of a kernel symbol.Parameters:
- symbol_name string The name of the kernel symbol to look up.
Returns:
-
lightuserdata
The address of the symbol if found, otherwise
nil(represented as a NULL lightuserdata).Usage:
local addr = linux.lookup("jiffies") if addr then print("Address of jiffies:", addr) end
- random ([m[, n]])
-
Generates pseudo-random integers.
Mimics the behavior of Lua's math.random but uses kernel's random number
generation facilities (
get_random_u32orget_random_u64).Parameters:
- m
integer
Lower bound of the range. If only one argument
nis provided,mdefaults to 1. (optional) - n integer Upper bound of the range. (optional)
Returns:
-
integer
A pseudo-random integer.
- If called without arguments, returns an integer with all bits pseudo-random.
- If called with one integer
n, returns a pseudo-random integer in the range[1, n]. - If called with two integersmandn, returns a pseudo-random integer in the range[m, n].Raises:
Error ifm > nor if the interval is too large.Usage:
local r1 = linux.random() -- Full range random integer local r2 = linux.random(100) -- Random integer between 1 and 100 local r3 = linux.random(50, 60) -- Random integer between 50 and 60
- m
integer
Lower bound of the range. If only one argument
- schedule ([timeout[, state]])
-
Puts the current task to sleep.
Sets the current task's state and schedules it out until a timeout occurs
or it is woken up.
Parameters:
- timeout
integer
Duration in milliseconds to sleep.
Defaults to
MAX_SCHEDULE_TIMEOUT(effectively indefinite sleep until woken). (optional) - state
integer
The task state to set before sleeping.
See linux.task for possible values. Defaults to
linux.task.INTERRUPTIBLE. (optional)
Returns:
-
integer
The remaining time in milliseconds
if the sleep was interrupted before the full timeout, or 0 if the full timeout elapsed.
Raises:
Error if an invalid task state is provided.See also:
Usage:
linux.schedule(1000) -- Sleep for 1 second (interruptible) linux.schedule(500, linux.task.UNINTERRUPTIBLE) -- Sleep for 0.5 seconds (uninterruptible)
- timeout
integer
Duration in milliseconds to sleep.
Defaults to
- time ()
-
Gets the current real time.
Returns:
-
integer
The current time in nanoseconds since the epoch (from
ktime_get_real_ns). - tracing ([enable])
-
Controls kernel tracing.
Turns kernel tracing on or off via
tracing_on()andtracing_off().Parameters:
- enable
boolean
If
true, turns tracing on. Iffalse, turns tracing off. If omitted, does not change the state. (optional)
Returns:
-
boolean
The current state of kernel tracing (
trueif on,falseif off) after any change.Usage:
local was_tracing = linux.tracing(true) -- Enable tracing if was_tracing then print("Tracing is now on") end local current_state = linux.tracing() -- Get current state linux.tracing(false) -- Disable tracing
- enable
boolean
If
Tables
- task
-
Table of task state constants.
Exports task state flags from
<linux/sched.h>. These are used withlinux.schedule().Fields:
- INTERRUPTIBLE integer Task is waiting for a signal or a resource (sleeping), can be interrupted.
- UNINTERRUPTIBLE integer Task is waiting (sleeping), cannot be interrupted by signals (except fatal ones if KILLABLE is also implied by context).
- KILLABLE integer Task is waiting (sleeping) like UNINTERRUPTIBLE, but can be interrupted by fatal signals.
- IDLE integer Task is idle, similar to UNINTERRUPTIBLE but avoids loadavg accounting.
See also: