Skip to content

Commit

Permalink
Kernel+LibC+UserspaceEmulator: Remove sys$dup() and sys$dup2()
Browse files Browse the repository at this point in the history
We can just implement these in userspace, so yay two less syscalls!
  • Loading branch information
awesomekling committed Aug 14, 2020
1 parent 5f3d384 commit bf247fb
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 74 deletions.
7 changes: 0 additions & 7 deletions DevTools/UserspaceEmulator/Emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ u32 Emulator::virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3)
switch (function) {
case SC_chdir:
return virt$chdir(arg1, arg2);
case SC_dup2:
return virt$dup2(arg1, arg2);
case SC_access:
return virt$access(arg1, arg2, arg3);
case SC_waitid:
Expand Down Expand Up @@ -1350,9 +1348,4 @@ int Emulator::virt$chdir(FlatPtr path, size_t path_length)
return syscall(SC_chdir, host_path.data(), host_path.size());
}

int Emulator::virt$dup2(int old_fd, int new_fd)
{
return syscall(SC_dup2, old_fd, new_fd);
}

}
1 change: 0 additions & 1 deletion DevTools/UserspaceEmulator/Emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ class Emulator {
ssize_t virt$getrandom(FlatPtr buffer, size_t buffer_size, unsigned int flags);
int virt$sleep(unsigned);
int virt$chdir(FlatPtr, size_t);
int virt$dup2(int, int);
int virt$getpgrp();
int virt$getpgid(pid_t);
int virt$setpgid(pid_t pid, pid_t pgid);
Expand Down
2 changes: 0 additions & 2 deletions Kernel/API/Syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ namespace Kernel {
S(getpgrp) \
S(fork) \
S(execve) \
S(dup) \
S(dup2) \
S(sigaction) \
S(umask) \
S(getgroups) \
Expand Down
1 change: 0 additions & 1 deletion Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ set(KERNEL_SOURCES
Syscalls/clock.cpp
Syscalls/debug.cpp
Syscalls/disown.cpp
Syscalls/dup.cpp
Syscalls/execve.cpp
Syscalls/exit.cpp
Syscalls/fcntl.cpp
Expand Down
2 changes: 0 additions & 2 deletions Kernel/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ class Process
int sys$ptsname(int fd, Userspace<char*>, size_t);
pid_t sys$fork(RegisterState&);
int sys$execve(Userspace<const Syscall::SC_execve_params*>);
int sys$dup(int oldfd);
int sys$dup2(int oldfd, int newfd);
int sys$sigaction(int signum, const sigaction* act, sigaction* old_act);
int sys$sigprocmask(int how, Userspace<const sigset_t*> set, Userspace<sigset_t*> old_set);
int sys$sigpending(Userspace<sigset_t*>);
Expand Down
57 changes: 0 additions & 57 deletions Kernel/Syscalls/dup.cpp

This file was deleted.

19 changes: 15 additions & 4 deletions Libraries/LibC/unistd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,25 @@ int isatty(int fd)

int dup(int old_fd)
{
int rc = syscall(SC_dup, old_fd);
__RETURN_WITH_ERRNO(rc, rc, -1);
return fcntl(old_fd, F_DUPFD, 0);
}

int dup2(int old_fd, int new_fd)
{
int rc = syscall(SC_dup2, old_fd, new_fd);
__RETURN_WITH_ERRNO(rc, rc, -1);
if (new_fd < 0) {
errno = EBADF;
return -1;
}

if (old_fd == new_fd)
return old_fd;

// Validate `old_fd` so we don't close `new_fd` and then fail the `F_DUPFD`.
if (fcntl(old_fd, F_GETFL) < 0)
return -1;

close(new_fd);
return fcntl(old_fd, F_DUPFD, new_fd);
}

int setgroups(size_t size, const gid_t* list)
Expand Down

0 comments on commit bf247fb

Please sign in to comment.