Real processes. Real pipes. One verified round trip.
Emilua is useful even when no sandbox is involved. This example drives GPG symmetric encryption and decryption through in-memory pipes, with a temporary GPG home and concurrent host I/O.
The final assertion compares the decrypted bytes with the original message. A Lua error or a nonzero GPG exit fails the demo.
1. Do not touch the user’s normal keyring
The example creates a temporary directory with user-only permissions and points GNUPGHOME there. It uses symmetric encryption with a known demonstration passphrase, so no personal key is generated, read, or modified.
init.lua—isolated process environment
local gnupg_home = fs.temp_directory_path() /
('emilua-gpg-' .. system.getpid())
fs.create_directories(gnupg_home)
fs.chmod(gnupg_home, fs.mode(7, 0, 0))
local environment = system.environment
environment.GNUPGHOME = tostring(gnupg_home)
Cleanup happens after the operation, including the error path, through fs.remove_all(gnupg_home).
2. Connect the child with two pipes
One pipe carries host output to child stdin. The other carries child stdout back to the host. Only the child-side descriptors are released for transfer to system.spawn().
init.lua—process construction
local process = system.spawn{
program = 'gpg',
arguments = arguments,
environment = environment,
stdin = child_input,
stdout = child_output,
stderr = 'share',
}
child_input:close()
child_output:close()
The host closes its copies immediately after spawning, so EOF and process lifetime reflect the actual producer and consumer.
3. Stream with fibers while the process runs
A writer fiber feeds the child’s input while a reader fiber collects its output. This avoids waiting for the process before draining its pipes and avoids buffering the whole operation in an external shell script.
init.lua—concurrent producer and consumer
local writer = spawn(function()
stream.write_all(host_output, input)
host_output:close()
end)
local reader = spawn(function()
return util.read_to_eof(host_input)
end)
process:wait()
writer:join()
local output = reader:join()
Processes use wait(); fibers use join(). The latter also re-raises a fiber error, which prevents a failed read or write from being mistaken for success.
4. Check the process status
Once the process and both fibers have completed, process.exit_code must be zero. Only then does the function return the output bytes.
if process.exit_code ~= 0 then
error('gpg exited with status ' .. process.exit_code)
end
return output
The same run_filter() function handles both encryption and decryption. The second call receives the first call’s binary output directly, without a temporary ciphertext file.
5. Verified result
mise run demo:gpg
GPG round trip: Emilua can orchestrate processes and pipes.
GPG may additionally print its normal diagnostics about creating the temporary keybox and the cipher used. The essential check is the successful plaintext assertion and zero process statuses.
Scope of the example
The demonstration passphrase is intentionally public and fixed. Do not use it for real data. Production integrations also need a deliberate secret-input path, resource limits, signal handling, and cleanup policy.