diff --git a/ports/include/noux_session/sysio.h b/ports/include/noux_session/sysio.h index 2231bd0d9f..1d3386fd0d 100644 --- a/ports/include/noux_session/sysio.h +++ b/ports/include/noux_session/sysio.h @@ -423,7 +423,7 @@ namespace Noux { { int pid; int status; }); SYSIO_DECL(pipe, { }, { int fd[2]; }); - SYSIO_DECL(dup2, { int fd; int to_fd; }, { }); + SYSIO_DECL(dup2, { int fd; int to_fd; }, { int fd; }); SYSIO_DECL(unlink, { Path path; }, { }); diff --git a/ports/src/lib/libc_noux/plugin.cc b/ports/src/lib/libc_noux/plugin.cc index 6a38e00666..e5480e4a67 100644 --- a/ports/src/lib/libc_noux/plugin.cc +++ b/ports/src/lib/libc_noux/plugin.cc @@ -176,6 +176,20 @@ extern "C" uid_t geteuid() } +extern "C" int dup(int ofd) +{ + sysio()->dup2_in.fd = ofd; + sysio()->dup2_in.to_fd = -1; + + if (!noux()->syscall(Noux::Session::SYSCALL_DUP2)) { + errno = EINVAL; + return -1; + } + + return sysio()->dup2_out.fd; +} + + /** * Utility to copy-out syscall results to buf struct * diff --git a/ports/src/noux/main.cc b/ports/src/noux/main.cc index d91c99ba55..5694853b6b 100644 --- a/ports/src/noux/main.cc +++ b/ports/src/noux/main.cc @@ -549,8 +549,11 @@ bool Noux::Child::syscall(Noux::Session::Syscall sc) case SYSCALL_DUP2: { - add_io_channel(io_channel_by_fd(_sysio->dup2_in.fd), - _sysio->dup2_in.to_fd); + int fd = add_io_channel(io_channel_by_fd(_sysio->dup2_in.fd), + _sysio->dup2_in.to_fd); + + _sysio->dup2_out.fd = fd; + return true; }