2020-08-15 11:54:00 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-15 11:54:00 +03:00
|
|
|
*/
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2023-02-24 20:45:37 +03:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-08-15 11:54:00 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$dup2(int old_fd, int new_fd)
|
2020-08-15 11:54:00 +03:00
|
|
|
{
|
2022-03-08 18:31:45 +03:00
|
|
|
VERIFY_NO_PROCESS_BIG_LOCK(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2022-01-29 03:29:07 +03:00
|
|
|
return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> {
|
2022-01-29 03:22:28 +03:00
|
|
|
auto description = TRY(fds.open_file_description(old_fd));
|
|
|
|
if (old_fd == new_fd)
|
|
|
|
return new_fd;
|
|
|
|
if (new_fd < 0 || static_cast<size_t>(new_fd) >= OpenFileDescriptions::max_open())
|
|
|
|
return EINVAL;
|
|
|
|
if (!fds.m_fds_metadatas[new_fd].is_allocated())
|
|
|
|
fds.m_fds_metadatas[new_fd].allocate();
|
|
|
|
fds[new_fd].set(move(description));
|
2021-05-27 22:14:57 +03:00
|
|
|
return new_fd;
|
2022-01-29 03:22:28 +03:00
|
|
|
});
|
2020-08-15 11:54:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|