2020-08-15 08:54:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-08-15 08:54:00 +00:00
|
|
|
*/
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-08-15 08:54:00 +00:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$dup2(int old_fd, int new_fd)
|
2020-08-15 08:54:00 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_promise(Pledge::stdio));
|
2022-01-29 00:29:07 +00:00
|
|
|
return m_fds.with_exclusive([&](auto& fds) -> ErrorOr<FlatPtr> {
|
2022-01-29 00:22:28 +00: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 19:14:57 +00:00
|
|
|
return new_fd;
|
2022-01-29 00:22:28 +00:00
|
|
|
});
|
2020-08-15 08:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|