From 7fe250a19749a259782b98a26ce17085de7d6976 Mon Sep 17 00:00:00 2001 From: Josh Fried Date: Sat, 22 Jun 2024 21:00:03 -0400 Subject: [PATCH] close-on-exec fixes --- junction/fs/file.cc | 12 ++++++++---- junction/fs/file.h | 2 +- junction/fs/procfs/procfs.cc | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/junction/fs/file.cc b/junction/fs/file.cc index f7ab9d9..982ebf0 100644 --- a/junction/fs/file.cc +++ b/junction/fs/file.cc @@ -84,7 +84,7 @@ std::shared_ptr FileTable::Dup(int fd) { return tbl->files[fd]; } -int FileTable::Insert(std::shared_ptr f, size_t lowest, bool cloexec) { +int FileTable::Insert(std::shared_ptr f, bool cloexec, size_t lowest) { rt::SpinGuard g(lock_); size_t i; auto fin = finally([this, cloexec, &i] { @@ -567,13 +567,17 @@ long usys_fcntl(int fd, unsigned int cmd, unsigned long arg) { std::shared_ptr fdup; fdup = ftbl.Dup(fd); if (!fdup) return -EBADF; - return ftbl.Insert(std::move(fdup), arg, cmd == F_DUPFD_CLOEXEC); + return ftbl.Insert(std::move(fdup), cmd == F_DUPFD_CLOEXEC, arg); } case F_GETFD: return ftbl.TestCloseOnExec(fd) ? FD_CLOEXEC : 0; case F_SETFD: - if (arg != FD_CLOEXEC) return -EINVAL; - ftbl.SetCloseOnExec(fd); + if (arg == FD_CLOEXEC) + ftbl.SetCloseOnExec(fd); + else if (arg == 0) + ftbl.ClearCloseOnExec(fd); + else + return -EINVAL; return 0; case F_GETFL: return ToFlags(f->get_mode()) | f->get_flags(); diff --git a/junction/fs/file.h b/junction/fs/file.h index a8e58c0..3f46cac 100644 --- a/junction/fs/file.h +++ b/junction/fs/file.h @@ -356,7 +356,7 @@ class FileTable { std::shared_ptr Dup(int fd); // Inserts a file into the file table and refcounts it. Returns the fd number. - int Insert(std::shared_ptr f, size_t lowest = 0, bool cloexec = false); + int Insert(std::shared_ptr f, bool cloexec = false, size_t lowest = 0); // Inserts a file into the file table at a specific fd number and refcounts // it. If a file already exists for the fd number, it will be replaced diff --git a/junction/fs/procfs/procfs.cc b/junction/fs/procfs/procfs.cc index a8a3020..7047453 100644 --- a/junction/fs/procfs/procfs.cc +++ b/junction/fs/procfs/procfs.cc @@ -68,7 +68,7 @@ class ProcFSInode : public Inode { std::shared_ptr parent_; }; -constexpr std::optional ParsePid(std::string_view s) { +std::optional ParsePid(std::string_view s) { pid_t result; if (std::from_chars(s.data(), s.data() + s.size(), result).ec == std::errc{}) return result;