On Wed, Apr 22, 2020 at 7:19 AM Josh Triplett [off-list ref] wrote:
Some applications want to prevent the usual "lowest available fd"
allocation from allocating certain file descriptors. For instance, they
may want to prevent allocation of a closed fd 0, 1, or 2 other than via
dup2/dup3, or reserve some low file descriptors for other purposes.
Add a prctl to increase the minimum fd and return the previous minimum.
System calls that allocate a specific file descriptor, such as
dup2/dup3, ignore this minimum.
exec resets the minimum fd, to prevent one program from interfering with
another program's expectations about fd allocation.
Have you considered making this a separate system call rather than
a part of prctl()?
At the moment, there are certain classes of things controlled by prctl,
e.g. capabilities, floating point handling and timer behavior, but nothing
that relates to file descriptors as such, so it's not an obvious decision.
Another option would be prlimit(), as it already controls the maximum
file descriptor number with RLIMIT_NOFILE, and adding a minimum
there would let you set min/max atomically.
Arnd