Files
darling-lol/tools/exec-guest.py
T
6b753e87c5 Close inherited host file descriptors before starting the guest
A terminal, IDE or service manager can leave descriptors open in the
launcher, and the guest inherited all of them. tools/exec-guest.py closes
every descriptor above stderr and then execs the guest command.

A shell loop over /proc/self/fd cannot do this portably: dash only parses
single-digit descriptors in redirections, so "exec 10>&-" runs a command
named 10 and ends the launcher on Debian and Ubuntu.

The launcher offline check now leaks descriptors 3, 4, 12 and 200 into the
launcher and asserts the guest sees only 0, 1 and 2. It runs in CI.

Split from #61.

Co-authored-by: DevWebeloper <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-18 17:11:36 +02:00

31 lines
825 B
Python
Executable File

#!/usr/bin/env python3
"""Run a command without the host file descriptors this launcher inherited (anything above stderr)."""
import os
import sys
def close_inherited_fds():
"""A terminal, IDE or service manager can leak descriptors; the guest must not inherit them."""
try:
names = os.listdir('/proc/self/fd')
except OSError:
os.closerange(3, 65536)
return
for name in names:
if name.isdigit() and int(name) > 2:
try:
os.close(int(name))
except OSError:
pass # the descriptor listdir used is already closed
def main(argv):
if len(argv) < 2:
sys.exit('usage: exec-guest.py COMMAND [ARGUMENT]...')
close_inherited_fds()
os.execvp(argv[1], argv[1:])
if __name__ == '__main__':
main(sys.argv)