forked from Bananymous/banan-os
Kernel: Remove SYS_DUP and implement it using fcntl F_DUPFD
This commit is contained in:
parent
82978da5e3
commit
9893c90e74
|
@ -29,7 +29,6 @@ namespace Kernel
|
||||||
|
|
||||||
BAN::ErrorOr<void> pipe(int fds[2]);
|
BAN::ErrorOr<void> pipe(int fds[2]);
|
||||||
|
|
||||||
BAN::ErrorOr<int> dup(int);
|
|
||||||
BAN::ErrorOr<int> dup2(int, int);
|
BAN::ErrorOr<int> dup2(int, int);
|
||||||
|
|
||||||
BAN::ErrorOr<int> fcntl(int fd, int cmd, int extra);
|
BAN::ErrorOr<int> fcntl(int fd, int cmd, int extra);
|
||||||
|
|
|
@ -138,7 +138,6 @@ namespace Kernel
|
||||||
BAN::ErrorOr<long> sys_pselect(sys_pselect_t* arguments);
|
BAN::ErrorOr<long> sys_pselect(sys_pselect_t* arguments);
|
||||||
|
|
||||||
BAN::ErrorOr<long> sys_pipe(int fildes[2]);
|
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_dup2(int fildes, int fildes2);
|
||||||
|
|
||||||
BAN::ErrorOr<long> sys_fcntl(int fildes, int cmd, int extra);
|
BAN::ErrorOr<long> sys_fcntl(int fildes, int cmd, int extra);
|
||||||
|
|
|
@ -159,22 +159,6 @@ namespace Kernel
|
||||||
return {};
|
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)
|
BAN::ErrorOr<int> OpenFileDescriptorSet::dup2(int fildes, int fildes2)
|
||||||
{
|
{
|
||||||
if (fildes2 < 0 || fildes2 >= (int)m_open_files.size())
|
if (fildes2 < 0 || fildes2 >= (int)m_open_files.size())
|
||||||
|
@ -204,6 +188,26 @@ namespace Kernel
|
||||||
|
|
||||||
switch (cmd)
|
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:
|
case F_GETFD:
|
||||||
return m_open_files[fd]->flags & FD_CLOEXEC;
|
return m_open_files[fd]->flags & FD_CLOEXEC;
|
||||||
case F_SETFD:
|
case F_SETFD:
|
||||||
|
@ -220,6 +224,7 @@ namespace Kernel
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dwarnln("TODO: fcntl command {}", cmd);
|
||||||
return BAN::Error::from_errno(ENOTSUP);
|
return BAN::Error::from_errno(ENOTSUP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1462,12 +1462,6 @@ namespace Kernel
|
||||||
return 0;
|
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)
|
BAN::ErrorOr<long> Process::sys_dup2(int fildes, int fildes2)
|
||||||
{
|
{
|
||||||
LockGuard _(m_process_lock);
|
LockGuard _(m_process_lock);
|
||||||
|
|
|
@ -36,7 +36,6 @@ __BEGIN_DECLS
|
||||||
O(SYS_SET_PWD, setpwd) \
|
O(SYS_SET_PWD, setpwd) \
|
||||||
O(SYS_CLOCK_GETTIME, clock_gettime) \
|
O(SYS_CLOCK_GETTIME, clock_gettime) \
|
||||||
O(SYS_PIPE, pipe) \
|
O(SYS_PIPE, pipe) \
|
||||||
O(SYS_DUP, dup) \
|
|
||||||
O(SYS_DUP2, dup2) \
|
O(SYS_DUP2, dup2) \
|
||||||
O(SYS_KILL, kill) \
|
O(SYS_KILL, kill) \
|
||||||
O(SYS_TCGETPGRP, tcgetpgrp) \
|
O(SYS_TCGETPGRP, tcgetpgrp) \
|
||||||
|
|
|
@ -125,7 +125,7 @@ int fsync(int fildes)
|
||||||
|
|
||||||
int dup(int fildes)
|
int dup(int fildes)
|
||||||
{
|
{
|
||||||
return syscall(SYS_DUP, fildes);
|
return fcntl(fildes, F_DUPFD, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int dup2(int fildes, int fildes2)
|
int dup2(int fildes, int fildes2)
|
||||||
|
|
Loading…
Reference in New Issue