From db41d0482f3a9489d97b94f6eaf72b2339d9256c Mon Sep 17 00:00:00 2001 From: nmzik Date: Thu, 8 Oct 2026 02:56:29 +0400 Subject: [PATCH] kernel: retain POSIX file descriptor close-on-exec flags --- src/kernel/fileSystem.cpp | 33 ++++++++++++++++++++++++++++----- src/kernel/fileSystem.h | 1 + src/libs/libKernel.cpp | 7 +++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/kernel/fileSystem.cpp b/src/kernel/fileSystem.cpp index 1f74e6cd7..d0e10909b 100644 --- a/src/kernel/fileSystem.cpp +++ b/src/kernel/fileSystem.cpp @@ -70,6 +70,7 @@ struct File { std::atomic_bool writable; std::atomic_bool append; std::atomic_bool sync_writes; + std::atomic_bool close_on_exec; SpecialFile special; Common::Mutex mutex; std::vector dirents; @@ -449,11 +450,12 @@ int KYTY_SYSV_ABI KernelOpen(const char* path, int flags, uint16_t mode) { EXIT_IF(file == nullptr || file->opened || file->directory); - file->name = path; - file->readable = rw_mode != Common::File::Mode::Write; - file->writable = rw_mode != Common::File::Mode::Read; - file->append = append; - file->sync_writes = fsync || sync || dsync; + file->name = path; + file->readable = rw_mode != Common::File::Mode::Write; + file->writable = rw_mode != Common::File::Mode::Read; + file->append = append; + file->sync_writes = fsync || sync || dsync; + file->close_on_exec = (static_cast(flags) & 0x00100000u) != 0; if (!directory && IsRandomDevice(file->name)) { file->real_name = file->name; @@ -585,6 +587,27 @@ int KYTY_SYSV_ABI KernelClose(int d) { return OK; } +int KYTY_SYSV_ABI KernelFcntl(int d, int command, int arg) { + PRINT_NAME(); + + if (d < DESCRIPTOR_MIN) { + return KERNEL_ERROR_EBADF; + } + + auto* file = g_files->GetFile(d); + if (file == nullptr || !file->opened) { + return KERNEL_ERROR_EBADF; + } + + switch (command) { + case 1: return file->close_on_exec.load(std::memory_order_relaxed); // F_GETFD + case 2: // F_SETFD, FD_CLOEXEC + file->close_on_exec.store((arg & 1) != 0, std::memory_order_relaxed); + return OK; + default: return KERNEL_ERROR_EINVAL; + } +} + int64_t KYTY_SYSV_ABI KernelRead(int d, void* buf, size_t nbytes) { PRINT_NAME(); diff --git a/src/kernel/fileSystem.h b/src/kernel/fileSystem.h index 5ac13fedf..ef6237239 100644 --- a/src/kernel/fileSystem.h +++ b/src/kernel/fileSystem.h @@ -59,6 +59,7 @@ std::filesystem::path GetRealFilename(const std::string& mounted_file_name); int KYTY_SYSV_ABI KernelOpen(const char* path, int flags, uint16_t mode); int KYTY_SYSV_ABI KernelClose(int d); +int KYTY_SYSV_ABI KernelFcntl(int d, int command, int arg); int64_t KYTY_SYSV_ABI KernelRead(int d, void* buf, size_t nbytes); int64_t KYTY_SYSV_ABI KernelPread(int d, void* buf, size_t nbytes, int64_t offset); int64_t KYTY_SYSV_ABI KernelPreadv(int d, const KernelIovec* iov, int iovcnt, int64_t offset); diff --git a/src/libs/libKernel.cpp b/src/libs/libKernel.cpp index a5c63618a..352482169 100644 --- a/src/libs/libKernel.cpp +++ b/src/libs/libKernel.cpp @@ -1974,6 +1974,12 @@ int64_t KYTY_SYSV_ABI fstat(int d, LibKernel::FileSystem::FileStat* sb) { return POSIX_N_CALL(LibKernel::FileSystem::KernelFstat(d, sb)); } +int KYTY_SYSV_ABI fcntl(int d, int command, int arg) { + PRINT_NAME(); + + return POSIX_N_CALL(LibKernel::FileSystem::KernelFcntl(d, command, arg)); +} + int KYTY_SYSV_ABI ftruncate(int d, int64_t length) { PRINT_NAME(); @@ -2163,6 +2169,7 @@ LIB_DEFINE(InitLibKernel_1_Posix) { LIB_FUNC("E6ao34wPw+U", stat); LIB_FUNC("JGMio+21L4c", mkdir); LIB_FUNC("ih4CD9-gghM", Posix::ftruncate); + LIB_FUNC("8nY19bKoiZk", Posix::fcntl); LIB_FUNC("pDuPEf3m4fI", Posix::sem_init); LIB_FUNC("cDW233RAwWo", Posix::sem_destroy); LIB_FUNC("YCV5dGGBcCo", Posix::sem_wait);