From 3021fffb079c1db895e1f30f321482477622f27d Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 3 Aug 2023 17:46:45 +0200 Subject: [PATCH] kernel: Store FD_CLOEXEC in the file descriptor itself Closes #39. --- kernel/src/sys/exec.cpp | 2 +- kernel/src/sys/file.cpp | 18 +++++++++--------- kernel/src/sys/open.cpp | 5 +++-- kernel/src/thread/Thread.h | 3 ++- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/kernel/src/sys/exec.cpp b/kernel/src/sys/exec.cpp index 038f4b30..280ce407 100644 --- a/kernel/src/sys/exec.cpp +++ b/kernel/src/sys/exec.cpp @@ -100,7 +100,7 @@ Result sys_execve(Registers* regs, SyscallArgs args) { auto& descriptor = current->fd_table[i]; if (!descriptor.has_value()) continue; - if (descriptor->description->flags & O_CLOEXEC) { descriptor = {}; } + if (descriptor->flags & O_CLOEXEC) { descriptor = {}; } } if (VFS::is_setuid(inode)) current->auth.euid = current->auth.suid = inode->metadata().uid; diff --git a/kernel/src/sys/file.cpp b/kernel/src/sys/file.cpp index 66c22c83..85fa7691 100644 --- a/kernel/src/sys/file.cpp +++ b/kernel/src/sys/file.cpp @@ -124,28 +124,28 @@ Result sys_fcntl(Registers*, SyscallArgs args) current->fd_table[new_fd] = descriptor; - if (is_cloexec) current->fd_table[new_fd]->flags() |= O_CLOEXEC; + if (is_cloexec) current->fd_table[new_fd]->flags |= O_CLOEXEC; else - current->fd_table[new_fd]->flags() &= ~O_CLOEXEC; + current->fd_table[new_fd]->flags &= ~O_CLOEXEC; return (u64)new_fd; } - case F_GETFD: return (u64) !!(descriptor.flags() & O_CLOEXEC); + case F_GETFD: return (u64) !!(descriptor.flags & O_CLOEXEC); case F_SETFD: { int arg = (int)args[2]; - if (arg == FD_CLOEXEC) descriptor.flags() |= O_CLOEXEC; + if (arg == FD_CLOEXEC) descriptor.flags |= O_CLOEXEC; else - descriptor.flags() &= ~O_CLOEXEC; + descriptor.flags &= ~O_CLOEXEC; return 0; } - case F_GETFL: return (u64)(descriptor.flags() & ~O_CLOEXEC); + case F_GETFL: return (u64)descriptor.status_flags(); case F_SETFL: { int arg = (int)args[2]; - descriptor.flags() &= ~(O_APPEND | O_NONBLOCK); + descriptor.status_flags() &= ~(O_APPEND | O_NONBLOCK); arg &= (O_APPEND | O_NONBLOCK); - descriptor.flags() |= arg; + descriptor.status_flags() |= arg; return 0; } @@ -189,7 +189,7 @@ Result sys_dup2(Registers*, SyscallArgs args) if (newfd == oldfd) return (u64)newfd; current->fd_table[newfd] = descriptor; - current->fd_table[newfd]->flags() &= ~O_CLOEXEC; + current->fd_table[newfd]->flags &= ~O_CLOEXEC; return (u64)newfd; } diff --git a/kernel/src/sys/open.cpp b/kernel/src/sys/open.cpp index 48a78596..cec57de4 100644 --- a/kernel/src/sys/open.cpp +++ b/kernel/src/sys/open.cpp @@ -7,7 +7,7 @@ #include // These flags are needed after open(), the rest only affect open(). -constexpr int FLAGS_TO_KEEP = O_RDWR | O_APPEND | O_NONBLOCK | O_CLOEXEC; +constexpr int FLAGS_TO_KEEP = O_RDWR | O_APPEND | O_NONBLOCK; Result sys_openat(Registers*, SyscallArgs args) { @@ -87,7 +87,8 @@ Result sys_openat(Registers*, SyscallArgs args) kdbgln("openat: opening file %s from dirfd %d, flags %d, mode %#o = fd %d", path.chars(), dirfd, flags, mode, fd); #endif - current->fd_table[fd] = FileDescriptor { TRY(make_shared(inode, flags & FLAGS_TO_KEEP)), 0 }; + current->fd_table[fd] = + FileDescriptor { TRY(make_shared(inode, flags & FLAGS_TO_KEEP)), 0, flags & O_CLOEXEC }; return (u64)fd; } diff --git a/kernel/src/thread/Thread.h b/kernel/src/thread/Thread.h index 98014448..d73e498e 100644 --- a/kernel/src/thread/Thread.h +++ b/kernel/src/thread/Thread.h @@ -41,6 +41,7 @@ struct FileDescriptor { SharedPtr description; usize offset { 0 }; + int flags { 0 }; bool should_append(); bool should_block(); @@ -52,7 +53,7 @@ struct FileDescriptor return description->inode; } - int& flags() + int& status_flags() { return description->flags; }