kernel+libc: Add dup2()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-04-25 20:37:30 +02:00
parent 188a97cf54
commit 97e9fceaa4
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 29 additions and 1 deletions

View File

@ -147,3 +147,22 @@ Result<u64> sys_ioctl(Registers*, SyscallArgs args)
return descriptor.inode->ioctl(request, arg); return descriptor.inode->ioctl(request, arg);
} }
Result<u64> sys_dup2(Registers*, SyscallArgs args)
{
int oldfd = (int)args[0];
int newfd = (int)args[1];
Thread* current = Scheduler::current();
if (newfd < 0 || newfd >= FD_MAX) return err(EBADF);
auto descriptor = *TRY(current->resolve_fd(oldfd));
if (newfd == oldfd) return (u64)newfd;
current->fd_table[newfd] = descriptor;
current->fd_table[newfd]->flags &= ~O_CLOEXEC;
return (u64)newfd;
}

View File

@ -103,6 +103,9 @@ extern "C"
/* Duplicate a file descriptor. */ /* Duplicate a file descriptor. */
int dup(int fd); int dup(int fd);
/* Replace a file descriptor with a copy of another one. */
int dup2(int oldfd, int newfd);
/* Change the current working directory. */ /* Change the current working directory. */
int chdir(const char* path); int chdir(const char* path);

View File

@ -292,6 +292,12 @@ extern "C"
return fcntl(fd, F_DUPFD, 0); return fcntl(fd, F_DUPFD, 0);
} }
int dup2(int oldfd, int newfd)
{
long rc = syscall(SYS_dup2, oldfd, newfd);
__errno_return(rc, int);
}
int chdir(const char* path) int chdir(const char* path)
{ {
long rc = syscall(SYS_chdir, path); long rc = syscall(SYS_chdir, path);

View File

@ -4,7 +4,7 @@
_e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(openat) _e(close) _e(read) _e(getpid) _e(write) \ _e(exit) _e(clock_gettime) _e(mmap) _e(munmap) _e(usleep) _e(openat) _e(close) _e(read) _e(getpid) _e(write) \
_e(lseek) _e(mkdir) _e(execve) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) \ _e(lseek) _e(mkdir) _e(execve) _e(mknod) _e(fork) _e(waitpid) _e(getppid) _e(fcntl) _e(getdents) _e(getuid) \
_e(geteuid) _e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(chmod) _e(chown) \ _e(geteuid) _e(getgid) _e(getegid) _e(setuid) _e(setgid) _e(seteuid) _e(setegid) _e(chmod) _e(chown) \
_e(ioctl) _e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(ioctl) _e(fstatat) _e(chdir) _e(getcwd) _e(unlinkat) _e(uname) _e(sethostname) _e(dup2)
enum Syscalls enum Syscalls
{ {