From 39092c7f16fac1376cb89861db9e074c602a46da Mon Sep 17 00:00:00 2001 From: DH Date: Tue, 31 Oct 2023 14:23:21 +0300 Subject: [PATCH] [orbis-kernel] implement sys_dup2/sys_dup --- orbis-kernel/src/sys/sys_descrip.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/orbis-kernel/src/sys/sys_descrip.cpp b/orbis-kernel/src/sys/sys_descrip.cpp index e15768078..6a0d00f09 100644 --- a/orbis-kernel/src/sys/sys_descrip.cpp +++ b/orbis-kernel/src/sys/sys_descrip.cpp @@ -6,10 +6,27 @@ orbis::SysResult orbis::sys_getdtablesize(Thread *thread) { return ErrorCode::NOSYS; } orbis::SysResult orbis::sys_dup2(Thread *thread, uint from, uint to) { - return ErrorCode::NOSYS; + if (to == 1 || to == 2) { // HACK: ignore setup /dev/console to stdout/stderr + return {}; + } + + std::lock_guard lock(thread->tproc->fileDescriptors.mutex); + + auto file = thread->tproc->fileDescriptors.get(from); + if (file == nullptr) { + return ErrorCode::BADF; + } + thread->tproc->fileDescriptors.close(to); + thread->tproc->fileDescriptors.insert(to, file); + return {}; } orbis::SysResult orbis::sys_dup(Thread *thread, uint fd) { - return ErrorCode::NOSYS; + auto file = thread->tproc->fileDescriptors.get(fd); + if (file == nullptr) { + return ErrorCode::BADF; + } + thread->retval[0] = thread->tproc->fileDescriptors.insert(std::move(file)); + return {}; } orbis::SysResult orbis::sys_fcntl(Thread *thread, sint fd, sint cmd, slong arg) {