LibCore: Open files with O_CLOEXEC by default

This changes Core::File::open() to specify O_CLOEXEC by default
so that we don't leak file descriptors into child processes. The
new behavior can be overriden by specifying OpenMode::KeepOnExec.
This commit is contained in:
Gunnar Beutner 2021-05-16 11:57:17 +02:00 committed by Andreas Kling
parent 67ed580532
commit fbfd0ed5ab
Notes: sideshowbarker 2024-07-18 17:58:04 +09:00
2 changed files with 3 additions and 0 deletions

View file

@ -79,6 +79,8 @@ bool File::open_impl(OpenMode mode, mode_t permissions)
flags |= O_TRUNC;
if (has_flag(mode, OpenMode::MustBeNew))
flags |= O_EXCL;
if (!has_flag(mode, OpenMode::KeepOnExec))
flags |= O_CLOEXEC;
int fd = ::open(m_filename.characters(), flags, permissions);
if (fd < 0) {
set_error(errno);

View file

@ -42,6 +42,7 @@ enum class OpenMode : unsigned {
Append = 4,
Truncate = 8,
MustBeNew = 16,
KeepOnExec = 32,
};
enum class SeekMode {