Kernel/LibC: add dup() syscall and function

This commit is contained in:
Bananymous 2023-08-17 12:03:29 +03:00
parent 5bd7099b96
commit 8e4216215e
7 changed files with 37 additions and 7 deletions

View File

@ -25,6 +25,7 @@ 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<void> seek(int fd, off_t offset, int whence); BAN::ErrorOr<void> seek(int fd, off_t offset, int whence);

View File

@ -86,6 +86,7 @@ namespace Kernel
BAN::ErrorOr<long> sys_creat(BAN::StringView name, mode_t); BAN::ErrorOr<long> sys_creat(BAN::StringView name, mode_t);
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_seek(int fd, off_t offset, int whence); BAN::ErrorOr<long> sys_seek(int fd, off_t offset, int whence);

View File

@ -85,6 +85,19 @@ 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]->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) 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())

View File

@ -596,6 +596,12 @@ namespace Kernel
return 0; 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) BAN::ErrorOr<long> Process::sys_dup2(int fildes, int fildes2)
{ {
LockGuard _(m_lock); LockGuard _(m_lock);

View File

@ -149,6 +149,9 @@ namespace Kernel
case SYS_PIPE: case SYS_PIPE:
ret = Process::current().sys_pipe((int*)arg1); ret = Process::current().sys_pipe((int*)arg1);
break; break;
case SYS_DUP:
ret = Process::current().sys_dup((int)arg1);
break;
case SYS_DUP2: case SYS_DUP2:
ret = Process::current().sys_dup2((int)arg1, (int)arg2); ret = Process::current().sys_dup2((int)arg1, (int)arg2);
break; break;

View File

@ -40,13 +40,14 @@ __BEGIN_DECLS
#define SYS_SET_PWD 33 #define SYS_SET_PWD 33
#define SYS_CLOCK_GETTIME 34 #define SYS_CLOCK_GETTIME 34
#define SYS_PIPE 35 #define SYS_PIPE 35
#define SYS_DUP2 36 #define SYS_DUP 36
#define SYS_RAISE 37 #define SYS_DUP2 37
#define SYS_KILL 38 #define SYS_RAISE 38
#define SYS_SIGNAL 39 #define SYS_KILL 39
#define SYS_SIGNAL_DONE 40 #define SYS_SIGNAL 40
#define SYS_TCSETPGRP 41 #define SYS_SIGNAL_DONE 41
#define SYS_GET_PID 42 #define SYS_TCSETPGRP 42
#define SYS_GET_PID 43
__END_DECLS __END_DECLS

View File

@ -61,6 +61,11 @@ ssize_t write(int fildes, const void* buf, size_t nbyte)
return syscall(SYS_WRITE, fildes, buf, nbyte); return syscall(SYS_WRITE, fildes, buf, nbyte);
} }
int dup(int fildes)
{
return syscall(SYS_DUP, fildes);
}
int dup2(int fildes, int fildes2) int dup2(int fildes, int fildes2)
{ {
return syscall(SYS_DUP2, fildes, fildes2); return syscall(SYS_DUP2, fildes, fildes2);