1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 08:28:11 +00:00

Kernel+LibC+UserspaceEmulator: Remove sys$dup() and sys$dup2()

We can just implement these in userspace, so yay two less syscalls!
This commit is contained in:
Andreas Kling 2020-08-15 01:17:00 +02:00
parent 5f3d384454
commit bf247fb45f
7 changed files with 15 additions and 74 deletions

View file

@ -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)