kernel: Store FD_CLOEXEC in the file descriptor itself
All checks were successful
continuous-integration/drone/pr Build is passing

Closes #39.
This commit is contained in:
apio 2023-08-03 17:46:45 +02:00
parent 72c94d1e57
commit 3021fffb07
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 15 additions and 13 deletions

View File

@ -100,7 +100,7 @@ Result<u64> sys_execve(Registers* regs, SyscallArgs args)
{ {
auto& descriptor = current->fd_table[i]; auto& descriptor = current->fd_table[i];
if (!descriptor.has_value()) continue; 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; if (VFS::is_setuid(inode)) current->auth.euid = current->auth.suid = inode->metadata().uid;

View File

@ -124,28 +124,28 @@ Result<u64> sys_fcntl(Registers*, SyscallArgs args)
current->fd_table[new_fd] = descriptor; 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 else
current->fd_table[new_fd]->flags() &= ~O_CLOEXEC; current->fd_table[new_fd]->flags &= ~O_CLOEXEC;
return (u64)new_fd; 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: { case F_SETFD: {
int arg = (int)args[2]; int arg = (int)args[2];
if (arg == FD_CLOEXEC) descriptor.flags() |= O_CLOEXEC; if (arg == FD_CLOEXEC) descriptor.flags |= O_CLOEXEC;
else else
descriptor.flags() &= ~O_CLOEXEC; descriptor.flags &= ~O_CLOEXEC;
return 0; return 0;
} }
case F_GETFL: return (u64)(descriptor.flags() & ~O_CLOEXEC); case F_GETFL: return (u64)descriptor.status_flags();
case F_SETFL: { case F_SETFL: {
int arg = (int)args[2]; int arg = (int)args[2];
descriptor.flags() &= ~(O_APPEND | O_NONBLOCK); descriptor.status_flags() &= ~(O_APPEND | O_NONBLOCK);
arg &= (O_APPEND | O_NONBLOCK); arg &= (O_APPEND | O_NONBLOCK);
descriptor.flags() |= arg; descriptor.status_flags() |= arg;
return 0; return 0;
} }
@ -189,7 +189,7 @@ Result<u64> sys_dup2(Registers*, SyscallArgs args)
if (newfd == oldfd) return (u64)newfd; if (newfd == oldfd) return (u64)newfd;
current->fd_table[newfd] = descriptor; current->fd_table[newfd] = descriptor;
current->fd_table[newfd]->flags() &= ~O_CLOEXEC; current->fd_table[newfd]->flags &= ~O_CLOEXEC;
return (u64)newfd; return (u64)newfd;
} }

View File

@ -7,7 +7,7 @@
#include <bits/open-flags.h> #include <bits/open-flags.h>
// These flags are needed after open(), the rest only affect open(). // 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<u64> sys_openat(Registers*, SyscallArgs args) Result<u64> sys_openat(Registers*, SyscallArgs args)
{ {
@ -87,7 +87,8 @@ Result<u64> 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); kdbgln("openat: opening file %s from dirfd %d, flags %d, mode %#o = fd %d", path.chars(), dirfd, flags, mode, fd);
#endif #endif
current->fd_table[fd] = FileDescriptor { TRY(make_shared<OpenFileDescription>(inode, flags & FLAGS_TO_KEEP)), 0 }; current->fd_table[fd] =
FileDescriptor { TRY(make_shared<OpenFileDescription>(inode, flags & FLAGS_TO_KEEP)), 0, flags & O_CLOEXEC };
return (u64)fd; return (u64)fd;
} }

View File

@ -41,6 +41,7 @@ struct FileDescriptor
{ {
SharedPtr<OpenFileDescription> description; SharedPtr<OpenFileDescription> description;
usize offset { 0 }; usize offset { 0 };
int flags { 0 };
bool should_append(); bool should_append();
bool should_block(); bool should_block();
@ -52,7 +53,7 @@ struct FileDescriptor
return description->inode; return description->inode;
} }
int& flags() int& status_flags()
{ {
return description->flags; return description->flags;
} }