Kernel/LibC: add dup() syscall and function
This commit is contained in:
@@ -25,6 +25,7 @@ namespace Kernel
|
||||
|
||||
BAN::ErrorOr<void> pipe(int fds[2]);
|
||||
|
||||
BAN::ErrorOr<int> dup(int);
|
||||
BAN::ErrorOr<int> dup2(int, int);
|
||||
|
||||
BAN::ErrorOr<void> seek(int fd, off_t offset, int whence);
|
||||
|
||||
@@ -86,6 +86,7 @@ namespace Kernel
|
||||
BAN::ErrorOr<long> sys_creat(BAN::StringView name, mode_t);
|
||||
|
||||
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_seek(int fd, off_t offset, int whence);
|
||||
|
||||
@@ -85,6 +85,19 @@ 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]->flags & O_WRONLY && m_open_files[result]->inode->is_pipe())
|
||||
((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())
|
||||
|
||||
@@ -596,6 +596,12 @@ namespace Kernel
|
||||
return 0;
|
||||
}
|
||||
|
||||
BAN::ErrorOr<long> Process::sys_dup(int fildes)
|
||||
{
|
||||
LockGuard _(m_lock);
|
||||
return TRY(m_open_file_descriptors.dup(fildes));
|
||||
}
|
||||
|
||||
BAN::ErrorOr<long> Process::sys_dup2(int fildes, int fildes2)
|
||||
{
|
||||
LockGuard _(m_lock);
|
||||
|
||||
@@ -149,6 +149,9 @@ namespace Kernel
|
||||
case SYS_PIPE:
|
||||
ret = Process::current().sys_pipe((int*)arg1);
|
||||
break;
|
||||
case SYS_DUP:
|
||||
ret = Process::current().sys_dup((int)arg1);
|
||||
break;
|
||||
case SYS_DUP2:
|
||||
ret = Process::current().sys_dup2((int)arg1, (int)arg2);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user