Kernel API
This page contains documentation for the public kernel module API.
This is a work-in-progress! Some parts may be broken or don’t make sense.
version: string
Version number of Phoenix.
do_syscall(call: string, ...: any): any...
Small function to execute a syscall and error if it fails.
Arguments
call
: The syscall to execute...
: The arguments to pass to the syscall
Return Values
The values returned from the syscall
deepcopy(tab: any): any
Copies a value. If the value is a table, copies all of its contents too.
Arguments
tab
: The value to copy
Return Values
The new copied value
split(str: string[, sep: string = "%s"]): {string}
Splits a string by a separator.
Arguments
str
: The string to splitsep
: The separator pattern to split by (defaults to “%s”)
Return Values
A list of items in the string
executeThread(process: Process, thread: Thread, ev: table, dead: boolean, allWaiting: boolean): boolean, boolean
Resumes a thread’s coroutine, handling different yield types.
Arguments
process
: The process that owns the threadthread
: The thread to resumeev
: An event to pass to the thread, if presentdead
: Whether a thread in the current run cycle has diedallWaiting
: Whether all previous threads were waiting for an event
Return Values
This function may return the following values:
- Whether this thread or a previous thread has died
- Whether all threads (including this one) are waiting for an event
userModeCallback(process: Process, func: function, ...: any): boolean, any
Executes a function in user mode from kernel code.
Arguments
process
: The process to execute asfunc
: The function to execute...
: Any parameters to pass to the function
Return Values
This function may return the following values:
- Whether the function returned successfully
- The value that the function returned.
make_ENV(env: table): table
Creates a new _ENV shadow environment for a table. The resulting table can have its environment set through t._ENV = val
.
Arguments
env
: The environment table to use
Return Values
A new _ENV-ized table
args
Stores all kernel arguments passed on the command line.
Fields
init
:root
:rootfstype
:preemptive
:quantum
:splitkernpath
:loglevel
:console
:traceback
:
syscalls
Contains every syscall defined in the kernel.
processes
Stores all currently running processes.
Fields
[0]
:id
:user
:dir
:dependents
:
modules
Stores all currently loaded kernel modules.
eventHooks
Stores a list of hooks to call on certain CraftOS events. Each entry has the event name as a key, and a list of functions to call as the value. The functions are called with a single table parameter with the event parameters.
process
Process API
filesystem
Filesystem API
terminal
Terminal API
user
User API
syslog
System logger API
hardware
Hardware API
PHOENIX_BUILD
Build string of Phoenix.
Fields
This function does not take any arguments.
systemStartTime
Stores the start time of the kernel.
Fields
This function does not take any arguments.
KERNEL
Stores a quick reference to the kernel process object.
Fields
This function does not take any arguments.
filesystem
mounts
Stores the current mounts as a key-value table of paths to filesystem objects.
filesystems
This table contains all filesystem types. Use this to insert more filesystem types into the system.
A filesystem type has to implement one method for each function in the filesystem API, with the exception of mounting-related functions and combine
, as well as a new
method that is called with the process, the source device, and the options table (if present). Paths passed to these methods (outside new
) take a relative path to the mountpoint, NOT the absolute path.
Fields
craftos
:owner
:permissions
:write
:execute
:
filesystem.open(process: Process, path: string, mode: string): Handle / nil, string
Opens a file for reading or writing.
Arguments
process
: The process to operate aspath
: The file path to open, which may be absolute or relative to the process’s working directorymode
: The mode to open the file as
Return Values
This function may return the following values:
- The new file handle
Or:
- If an error occurred
- An error message describing why the file couldn’t be opened
filesystem.list(process: Process, path: string): {string}
Returns a list of file names in the directory.
Arguments
process
: The process to operate aspath
: The file path to list, which may be absolute or relative to the process’s working directory
Return Values
A list of file names present in the directory
filesystem.stat(process: Process, path: string): table / nil, string
Returns a table with information about the selected path.
Arguments
process
: The process to operate aspath
: The file path to stat, which may be absolute or relative to the process’s working directory
Return Values
This function may return the following values:
- A table with information about the path (see the docs for the
stat
syscall for more info)
Or:
- If an error occurred
- An error message describing why the file couldn’t be opened
filesystem.remove(process: Process, path: string)
Removes a file or directory.
Arguments
process
: The process to operate aspath
: The file path to remove, which may be absolute or relative to the process’s working directory
Return Values
This function does not return anything.
filesystem.rename(process: Process, path: string, new: The)
Renames (moves) a file or directory.
Arguments
process
: The process to operate aspath
: The file path to rename, which may be absolute or relative to the process’s working directorynew
: path the file will be at, which may be in another directory but must be on the same mountpoint
Return Values
This function does not return anything.
filesystem.mkdir(process: Process, path: string)
Creates a new directory and any parent directories.
Arguments
process
: The process to operate aspath
: The directory to create, which may be absolute or relative to the process’s working directory
Return Values
This function does not return anything.
filesystem.chmod(process: Process, path: string, user: string|nil, boolean: number|string|{read)
Changes the permissions (mode) of a file or directory for the specified user.
Arguments
process
: The process to operate aspath
: The file path to modify, which may be absolute or relative to the process’s working directoryuser
: The user to change the permissions for, ornil
for all usersboolean
: ?, write = boolean?, execute = boolean?} mode The new permissions for the user (see the docs for thechmod
syscall for more info)
Return Values
This function does not return anything.
filesystem.chown(process: Process, path: string, user: string)
Changes the owner of a file or directory.
Arguments
process
: The process to operate aspath
: The file path to modify, which may be absolute or relative to the process’s working directoryuser
: The user that will own the file
Return Values
This function does not return anything.
filesystem.mount(process: Process, type: string, src: string, dest: string, options: table?)
Mounts a disk device to a path using the specified filesystem and options.
Arguments
process
: The process to operate astype
: The type of filesystem to mountsrc
: The source device to mountdest
: The destination mountpointoptions
: Any options to pass to the mounter (optional)
Return Values
This function does not return anything.
filesystem.unmount(process: Process, path: string)
Unmounts a filesystem at a mountpoint.
Arguments
process
: The process to operate aspath
: The mountpoint to remove, which may be absolute or relative to the process’s working directory
Return Values
This function does not return anything.
filesystem.combine(first: string, ...: string): string
Combines the specified path components into a single path.
Arguments
first
: The first path component...
: Any additional path components to add
Return Values
The final combined path
createLuaLib(process: Process): _G
Creates a new global table with a Lua 5.2 standard library installed.
Arguments
process
: The process to generate for
Return Values
A new global table for the process
terminal
terminal.makeTTY(term: table, width: number, height: number): TTY
Returns a new TTY object.
Arguments
term
: The CraftOS terminal object to render onwidth
: The width of the TTYheight
: The height of the TTY
Return Values
The new TTY object
TTY
Stores all virtual TTYs for the main screen.
Fields
terminal
:terminal
:terminal
:terminal
:terminal
:terminal
:terminal
:terminal
:
currentTTY
Stores the TTY that is currently shown on screen.
Fields
This function does not take any arguments.
terminal.userTTYs
Stores all TTYs that have been created in user mode.
keysHeld
Stores what modifier keys are currently being held.
Fields
ctrl
:alt
:shift
:
terminal.redraw(tty: TTY, full: boolean)
Redraws the specified TTY if on-screen.
Arguments
tty
: The TTY to redrawfull
: Whether to draw the full screen, or just the changed regions
Return Values
This function does not return anything.
terminal.resize(tty: TTY, width: number, height: number)
Resizes the TTY.
Arguments
tty
: The TTY to resizewidth
: The new widthheight
: The new height
Return Values
This function does not return anything.
terminal.write(tty: TTY, text: string)
Writes some text to a TTY’s text buffer, allowing ANSI escapes.
Arguments
tty
: The TTY to write totext
: The text to write
Return Values
This function does not return anything.
syslog
syslogs
Stores all open system logs.
Fields
default
: file = filesystem.open(KERNEL, “/var/log/default.log”, “a”),
panic(message: any?)
Immediately halts the system and shows an error message on screen. This function can be called either standalone or from within xpcall. This function never returns.
Arguments
message
: A message to display on screen (optional)
Return Values
This function does not return anything.
createRequire(process: Process, G: _G)
Creates a new package
and require
set in a global table for the specified process.
Arguments
process
: The process to make the functions forG
: The global environment to install in
Return Values
This function does not return anything.
timerMap
Stores a list of used timers.
alarmMap
Stores a list of used alarms.
reap_process(process: Process)
Finishes a process’s resources so it can be removed cleanly.
Arguments
process
: The process to reap
Return Values
This function does not return anything.
hardware
deviceTreeRoot
Stores the root device for hardware.
Fields
id
:uuid
:parent
:displayName
:drivers
:
hardware.get(path: string): Device...
Returns all devices that match a path specifier.
Arguments
path
: The path to query
Return Values
The device objects that match
hardware.find(type: string): Device...
Returns all devices that match a type.
Arguments
type
: The type to find
Return Values
The device objects that match
hardware.path(node: Device): string
Returns the absolute path to a device node.
Arguments
node
: The node to lookup
Return Values
The path to the node
hardware.add(parent: Device, name: string): Device
Adds a new child to the specified node.
Arguments
parent
: The parent of the new nodename
: The name of the new node
Return Values
The newly added node
hardware.remove(node: Device): boolean, string?
Removes a node from its parent and the device tree. The device node should no longer be used.
Arguments
node
: The node to remove
Return Values
This function may return the following values:
- Whether the operation succeeded
- An error message if it failed
hardware.register(node: Device, driver: Driver): boolean, string?
Registers a device driver on a node.
Arguments
node
: The node to modifydriver
: The driver to add
Return Values
This function may return the following values:
- Whether the operation succeeded
- An error message if it failed
hardware.register_callback(driver: Driver): function
Returns a function that automatically attaches a driver to a node.
Arguments
driver
: The driver to use
Return Values
A function that takes a node and registers the driver on it
hardware.deregister(node: Device, driver: Driver): boolean, string?
Deregisters a driver from a node.
Arguments
node
: The node to modifydriver
: The driver to remove
Return Values
This function may return the following values:
- Whether the operation succeeded
- An error message if it failed
hardware.listen(callback: function, parent: Device?, pattern: string?)
Adds a function that is called when either a parent node or pattern matches on a new node.
Arguments
callback
: A function that is called with a node when the pattern matchesparent
: A parent node to watch on (optional)pattern
: A Lua pattern to match on the device name (optional)
Return Values
This function does not return anything.
hardware.unlisten(callback: function)
Removes a listener callback from the listener list.
Arguments
callback
: The function to remove
Return Values
This function does not return anything.
hardware.broadcast(node: Device, event: string, param: table)
Broadcasts an event to all processes listening to events on a node.
Arguments
node
: The node to broadcast forevent
: The event to broadcastparam
: The parameters to pass for the event
Return Values
This function does not return anything.
hardware.call(process: Process, node: Device, method: string, ...: any): any...
Calls a method on a device.
Arguments
process
: The process to run asnode
: The node to call onmethod
: The method to call...
: Any arguments to pass
Return Values
Any return values from the method
uriSchemes
Stores all URI scheme handlers using Lua patterns as keys.
Fields
[https?]
:[wss?]
:[rednet]
:[rednet%+%a+]
:[psp]
: