std.ps — Lilush API

←index

← std

Overview

Process control and execution utilities. Provides wrappers around POSIX process management, signals, file descriptors, pipes, PTY operations, and command execution with output capture.

Functions

NameSignature
setenvsetenv(name, value)
unsetenvunsetenv(name)
environenviron() -> vars
killkill(pid, signal)
dupdup(oldfd) -> newfd
dup2dup2(oldfd, newfd) -> fd
forkfork() -> pid
setpgidsetpgid(pid, pgid)
getpgidgetpgid(pid) -> pgid
setsidsetsid() -> sid
tcsetpgrptcsetpgrp(fd, pgid)
tcgetpgrptcgetpgrp(fd) -> pgid
tiocsttytiocstty(fd)
pty_openpty_open() -> pty
pty_attachpty_attach(master, detach_key) -> status
pipepipe() -> pipe, err
pipe_filepipe_file() -> pipe, err
getpidgetpid() -> pid
execexec(pathname, ...)
launchlaunch(cmd, stdin, stdout, stderr, ...) -> pid, err
waitpidwaitpid(pid) -> pid, status
waitwait(pid) -> pid, status
exec_simpleexec_simple(cmd, nowait) -> result
exec_one_lineexec_one_line(cmd) -> line
find_by_inodefind_by_inode(inode) -> process
register_signalregister_signal(signum)
remove_signalremove_signal(signum)
sleep_mssleep_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.

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.

pty_open() -> pty

Open a new pseudoterminal pair

Opens a new pseudoterminal (PTY) master/slave pair. Returns a table with master and slave file descriptors, along with the slave's path.

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.

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.

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.