Kernel: Remove SYS_DUP and implement it using fcntl F_DUPFD

This commit is contained in:
Bananymous 2025-01-17 19:22:53 +02:00
parent 82978da5e3
commit 9893c90e74
6 changed files with 22 additions and 26 deletions

View File

@ -29,7 +29,6 @@ namespace Kernel
BAN::ErrorOr<void> pipe(int fds[2]);
BAN::ErrorOr<int> dup(int);
BAN::ErrorOr<int> dup2(int, int);
BAN::ErrorOr<int> fcntl(int fd, int cmd, int extra);

View File

@ -138,7 +138,6 @@ namespace Kernel
BAN::ErrorOr<long> sys_pselect(sys_pselect_t* arguments);
BAN::ErrorOr<long> sys_pipe(int fildes[2]);
BAN::ErrorOr<long> sys_dup(int fildes);
BAN::ErrorOr<long> sys_dup2(int fildes, int fildes2);
BAN::ErrorOr<long> sys_fcntl(int fildes, int cmd, int extra);

View File

@ -159,22 +159,6 @@ namespace Kernel
return {};
}
BAN::ErrorOr<int> OpenFileDescriptorSet::dup(int fildes)
{
TRY(validate_fd(fildes));
int result = TRY(get_free_fd());
m_open_files[result] = m_open_files[fildes];
if (m_open_files[result]->path() == "<pipe wr>"_sv)
{
ASSERT(m_open_files[result]->inode()->is_pipe());
static_cast<Pipe*>(m_open_files[result]->inode().ptr())->clone_writing();
}
return result;
}
BAN::ErrorOr<int> OpenFileDescriptorSet::dup2(int fildes, int fildes2)
{
if (fildes2 < 0 || fildes2 >= (int)m_open_files.size())
@ -204,6 +188,26 @@ namespace Kernel
switch (cmd)
{
case F_DUPFD:
case F_DUPFD_CLOEXEC:
{
const int new_fd = TRY(get_free_fd());
auto new_file = TRY(BAN::RefPtr<OpenFileDescription>::create(
TRY(m_open_files[fd]->file.clone()),
m_open_files[fd]->offset,
m_open_files[fd]->flags | (cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0)
));
if (new_file->path() == "<pipe wr>"_sv)
{
ASSERT(new_file->inode()->is_pipe());
static_cast<Pipe*>(new_file->inode().ptr())->clone_writing();
}
m_open_files[new_fd] = BAN::move(new_file);
return new_fd;
}
case F_GETFD:
return m_open_files[fd]->flags & FD_CLOEXEC;
case F_SETFD:
@ -220,6 +224,7 @@ namespace Kernel
break;
}
dwarnln("TODO: fcntl command {}", cmd);
return BAN::Error::from_errno(ENOTSUP);
}

View File

@ -1462,12 +1462,6 @@ namespace Kernel
return 0;
}
BAN::ErrorOr<long> Process::sys_dup(int fildes)
{
LockGuard _(m_process_lock);
return TRY(m_open_file_descriptors.dup(fildes));
}
BAN::ErrorOr<long> Process::sys_dup2(int fildes, int fildes2)
{
LockGuard _(m_process_lock);

View File

@ -36,7 +36,6 @@ __BEGIN_DECLS
O(SYS_SET_PWD, setpwd) \
O(SYS_CLOCK_GETTIME, clock_gettime) \
O(SYS_PIPE, pipe) \
O(SYS_DUP, dup) \
O(SYS_DUP2, dup2) \
O(SYS_KILL, kill) \
O(SYS_TCGETPGRP, tcgetpgrp) \

View File

@ -125,7 +125,7 @@ int fsync(int fildes)
int dup(int fildes)
{
return syscall(SYS_DUP, fildes);
return fcntl(fildes, F_DUPFD, 0);
}
int dup2(int fildes, int fildes2)