Process control and execution utilities. Provides wrappers around POSIX process management, signals, file descriptors, pipes, PTY operations, and command execution with output capture.
| Name | Signature |
|---|---|
| setenv | setenv(name, value) |
| unsetenv | unsetenv(name) |
| environ | environ() -> vars |
| kill | kill(pid, signal) |
| dup | dup(oldfd) -> newfd |
| dup2 | dup2(oldfd, newfd) -> fd |
| fork | fork() -> pid |
| setpgid | setpgid(pid, pgid) |
| getpgid | getpgid(pid) -> pgid |
| setsid | setsid() -> sid |
| tcsetpgrp | tcsetpgrp(fd, pgid) |
| tcgetpgrp | tcgetpgrp(fd) -> pgid |
| tiocstty | tiocstty(fd) |
| tiocswinsz | tiocswinsz(fd, rows, cols, xpixel, ypixel) -> ok, err |
| tiocgwinsz | tiocgwinsz(fd) -> rows, cols, xpixel, ypixel |
| pty_open | pty_open() -> pty |
| pty_attach | pty_attach(master, detach_key) -> status |
| pipe | pipe() -> pipe, err |
| pipe_file | pipe_file() -> pipe, err |
| getpid | getpid() -> pid |
| exec | exec(pathname, ...) |
| launch | launch(cmd, stdin, stdout, stderr, ...) -> pid, err |
| launch_detached | launch_detached(cmd, stdin, stdout, stderr, ...) -> ok, err |
| waitpid | waitpid(pid) -> pid, status |
| wait | wait(pid) -> pid, status |
| exec_simple | exec_simple(cmd, nowait) -> result |
| exec_one_line | exec_one_line(cmd) -> line |
| find_by_inode | find_by_inode(inode) -> process |
| register_signal | register_signal(signum) |
| remove_signal | remove_signal(signum) |
| sleep_ms | sleep_ms(ms) |
setenv(
name,value)
Set an environment variable
Sets the environment variable name to value in the current process.
Changes are visible to child processes spawned afterwards. Wraps
setenv(3).
unsetenv(
name)
Remove an environment variable
Removes the variable name from the current process environment. If
name does not exist, the call is a no-op. Wraps unsetenv(3).
environ() ->
vars
Get raw environment strings from the process
Returns the raw process environment as an array of "NAME=VALUE" strings
(one per variable). To get a parsed name → value table, use
std.environ() instead.
kill(
pid,signal)
Send a signal to a process
Sends signal to the process identified by pid. Common signal numbers:
SIGHUP=1, SIGINT=2, SIGTERM=15, SIGKILL=9, SIGSTOP=19, SIGCONT=18.
Wraps kill(2).
dup(
oldfd) ->newfd
Duplicate a file descriptor
Duplicates a file descriptor. The new descriptor is the lowest-numbered available file descriptor.
dup2(
oldfd,newfd) ->fd
Duplicate a file descriptor to a specific number
Duplicates a file descriptor to a specific number. If newfd is already
open, it is closed first. Returns the new file descriptor.
fork() ->
pid
Create a child process
Creates a child process by duplicating the calling process. Returns 0 in the child process and the child's PID in the parent process.
The child keeps the caller's signal handlers and its signal mask. Under a
lev loop that holds a signal through on_signal, the child holds it
blocked too, and a kill with that signal does not end it. A program the
child starts through exec starts clean.
setpgid(
pid,pgid)
Set the process group ID of a process
Sets the process group ID of the process specified by pid. If pid is 0,
the calling process's PID is used. The pgid parameter specifies the new
process group ID.
getpgid(
pid) ->pgid
Get the process group ID of a process
Returns the process group ID of the process specified by pid. If pid is 0,
returns the PGID of the calling process.
setsid() ->
sid
Create a new session and set the process as session leader
Creates a new session and sets the calling process as the session leader. Returns the session ID (which equals the calling process's PID).
tcsetpgrp(
fd,pgid)
Set the foreground process group of a terminal
Sets the foreground process group of the terminal associated with the
file descriptor fd to the process group specified by pgid.
tcgetpgrp(
fd) ->pgid
Get the foreground process group of a terminal
Returns the process group ID of the foreground process group of the
terminal associated with the file descriptor fd.
tiocstty(
fd)
Set the controlling terminal for the current process
Sets the controlling terminal for the current process to the terminal
associated with the file descriptor fd. This is typically used in
process managers and shells.
tiocswinsz(
fd,rows,cols,xpixel,ypixel) ->ok,err
Set the window size of a terminal
Sets the window size of the terminal on fd, usually a PTY master: rows
and cols of the grid, and its width and height in pixels. The pixel
fields default to 0, which means unknown. Each value must lie in 0..65535,
or the call raises. A change sends SIGWINCH to the terminal's foreground
process group. Returns true, or nil and the error. Wraps the
TIOCSWINSZ ioctl.
tiocgwinsz(
fd) ->rows,cols,xpixel,ypixel
Get the window size of a terminal
Returns the window size of the terminal on fd: rows, cols, xpixel
and ypixel. On failure returns nil and the error. Wraps the
TIOCGWINSZ ioctl.
pty_open() ->
pty
Open a new pseudoterminal pair
Opens a new pseudoterminal (PTY) master/slave pair. Returns a table with
master, the master's file descriptor, and slave, the slave's path.
The caller opens the slave path, usually in a child that makes it its
controlling terminal.
The master is close-on-exec. A forked child keeps it; a program the
caller execs does not. A dup2 onto stdio clears the flag on the copy.
pty_attach(
master,detach_key) ->status
Attach to a pseudoterminal master for interactive I/O
Attaches the current process to a pseudoterminal master for interactive
I/O operations. The detach_key parameter specifies the key sequence
to detach from the attached session.
pipe() ->
pipe,err
Create a raw file descriptor pipe
Returns a pipe object with read(count), write(data, count),
close_out(), and close_inn() methods. Uses raw file descriptors;
for Lua FILE* objects, use pipe_file() instead.
pipe_file() ->
pipe,err
Create a FILE*-based pipe compatible with io.* functions
Creates a pipe using Lua FILE* objects instead of raw file descriptors.
Returns a table with read and write methods compatible with io.* functions.
getpid() ->
pid
Get the current process ID
exec(
pathname,...)
Replace the current process with a new program
Replaces the current process image with the program at pathname.
Additional arguments are passed as command-line arguments.
The basename of pathname is used as argv[0]. Does not return on success.
The program starts with an empty signal mask and default handlers,
whatever the caller blocked or caught. A signal the caller ignores stays
ignored. When the exec fails, the call returns nil and an error, and the
caller's handlers and mask are as they were.
launch(
cmd,stdin,stdout,stderr,...) ->pid,err
Fork and execute a command with optional I/O redirection
Forks a child process and executes cmd with optional I/O redirection
via raw file descriptors. cmd can also be a table with func, name,
and extra fields to execute a Lua function in a child process.
A command starts as exec starts it: an empty signal mask and default
handlers. A func child never execs, and runs under the caller's handlers
and mask, as a child of fork does.
launch_detached(
cmd,stdin,stdout,stderr,...) ->ok,err
Fork and execute a command that nobody will wait for
Like launch, but the command starts from a short-lived intermediate
child in a session of its own, so it is reparented to init and reaped
there. Use it for processes the caller never waits for -- external
openers, delayed notifications -- which would otherwise linger as
zombies for as long as the caller lives, and would take the caller's
terminal signals with them.
The command's own PID is not reported: it is a grandchild, not a child.
waitpid(
pid) ->pid,status
Wait for a specific child process (non-blocking)
Waits for the child process specified by pid and returns its PID and
exit status. This is a non-blocking variant that returns immediately.
wait(
pid) ->pid,status
Wait for a child process to terminate
Waits for the child process specified by pid to terminate and returns
its PID and exit status. This is a blocking call.
exec_simple(
cmd,nowait) ->result
Execute a shell command and capture its output
Runs a command string, captures stdout and stderr as arrays of lines,
and returns a table with status, stdout, and stderr fields.
When nowait is true, uses non-blocking wait.
exec_one_line(
cmd) ->line
Execute a command and return its first line of stdout
Executes a shell command and returns only the first line of its stdout. Returns an empty string if the command fails or produces no output.
find_by_inode(
inode) ->process
Find the process holding a socket by its inode number
Searches /proc for the process holding a socket by its inode number. Returns a string in the format "process_name(pid)" or an empty string if no process is found.
register_signal(
signum)
Register a no-op handler for a signal
Installs a no-op C handler for signum, preventing the default
disposition (e.g. process termination for SIGTERM). Useful for
making blocking syscalls like wait() return EINTR instead of
killing the process.
remove_signal(
signum)
Restore default handler for a signal
Restores the default signal disposition for signum.
sleep_ms(
ms)
Sleep for a number of milliseconds
Suspends the calling process for ms milliseconds. Uses
nanosleep(2) internally and restarts on EINTR.