boostorg / boostorg/process

Add support for not inheriting file descriptors on POSIX

Open
#80 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
145
Forks
151
PR merge metrics
No merged PRs in 30d

Description

I'd like to ask to support non-inheriting file descriptors in child processes on POSIX systems, in particular, on Linux.

Rationale:
This is an often desired feature, that is already available in Windows via `inherit_handles` flag. Although it can be implemented by user as `on_exec_setup` handler, I believe, it is common and tricky enough to benefit from being part of Boost.Process out of the box.

Note that I'm not considering marking all file descriptors with `CLOEXEC` flags in the user's code because that is not practical. Most third party libraries that open file descriptors, including the C/C++ standard libraries (e.g. `std::FILE`, iostreams) do not set `CLOEXEC` flags and there is no way to force all file descriptors to automatically have that flag process-wide.

Possible implementation:
Since POSIX does not define an API to not inherit file descriptors on `exec`, one suggestion is to do iterate over file descriptors, available in `/proc/self/fd`, in the child process after `fork` and before `exec`, and closing them. Note that file descriptors 0, 1 and 2 need to be skipped from closing, as these are stdin, stdout and stderr. These fds are also available as `STDIN_FILENO`, `STDOUT_FILENO` and `STDERR_FILENO` constants, which could be used instead of integers.

This functionality should be made optional, controllable by a flag (`inherit_handles` makes a precedent here).

This request comes from this Boost.Log ticket: https://github.com/boostorg/log/issues/83.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.